Hacker News new | past | comments | ask | show | jobs | submit login
GG-flip: simplest and most efficient ways to flip the sign of a number (github.com/avinassh)
89 points by shawndumas on Jan 20, 2018 | hide | past | favorite | 33 comments



> The final lib size is few GBs, so distribution may become a problem.

Hahaha!

I admit to being slow; I missed the above sentence the first time, and I actually skimmed the whole thing and made it here to the comments before realizing it was a joke. Reading it again, there are quite a few gems I missed the first time, it's quite funny.

BTW check the linked thread on Reddit for more hilarity https://www.reddit.com/r/shittyprogramming/comments/7n35m7/h...


Bitcoin Client, Critical Unflipper Vulnerability Steals Coins

https://news.ycombinator.com/item?id=16206743

Apparently, the Federal Government, Amazon, Google, and Facebook have known about the security issues of directly using unary negation without the gg-flip table driven approach for some time.

The link claims Shodan found over 20,000 vulnerable government servers and to apply gg-flip immediately.


Look at his other repos too, particularly rockstar.


>>GG Flip is a Golang library which generates the Javascript sign flip library. I preferred Go because lack of generics seemed like a good design choice.

God help us if this isn't satire.


Considering the context of the whole article. It's hilarious


I think Stack Flip is the optimal solution, as it requires very little code and can handle any number, even values the code has never encountered before. The library simply calls out to a web service that posts the question on Stack Overflow.

It should only take a few seconds for someone to post the answer. And the service will then upvote the answer, thus motivating Stack Overflow users to keep providing those results.


Maybe this is a problem we could use ethereum smart contracts to solve. Actually, is flipping a number even in P? Verification is easy - just compare against the negative - but I'm not sure if flipping the number can be done in polynomial time, enumerating all cases (as OP has done) aside.


Thanks for this idea, I will explore this further.

(author here)


Make sure it has machine learning please!


I wholeheartedly believed that the first approach was a popular npm library.


soon, it will be!

(author here)


The obvious solution would be x = ~x + 1, right? And don't even bother if x is the minimum signed integer.


The usual/obvious solution is to flip the sign bit. (for floating point numbers). I think you're right for two's complement integers.


I was kidding...but I was also thinking of the thread where people were discussing big vs little endian and the idea that since nobody uses big endian any more, it's not necessary to allow for it.


Endianess only matters for how numbers are stored in memory, but operations like bitshifts, boolean operations and arithmetic happen after the number has been loaded into a processor register, so endianess doesn't matter.

Even if that wasn't the case, flipping a two's complement integer requires only negation and addition (as you showed), and those don't really depend on data layout.


> and those don't really depend on data layout.

Not sure I understood this, but 01+09 = 10 and 10+90 = 100 and 10 ≠ fix_endianness(100)


10!=01 and 09!=90

The endian-ness is only the in-memory layout of the bits and has nothing to do with how VALUES are treated. 1+9=10, regardless of how those values are stored, similarly 1<<9=512, regardless of how the CPU internally arranges those bits. The same holds true for inverting the value, the internal arrangement of the bits is irrelevant.

Really you only care about endianness when you fail to treat numbers as numbers, for example by treating them as bytes.


Good solution. It grounds me even more to start learning Go lang.


So what if x > MAXNUMBER/2 ?

Also, how can this be faster than x=0-x; ?


This took me a second to realize this is satire too.


It's a joke. And yeah, it gives wrong answers for some numbers.

BTW, JS has unary minus, so you can write:

    x = -x


I think the author chose a more efficient table-driven approach, due to the extra 0 in your 0-x expression.


The real joke was that he was interested in Bitcoin cash.


Is this a joke?

Two's complement: flips the bits and add one

FP: flip the sign bit

OR....

x=-x


There are several security issues related to usage of the negation operator that make it undesirable to use in this situation. The solution presented in the article avoids those holes. It also has the advantage that a switch statement is more explicit than use of a language facility.


Nice, but you really should give some examples, too. Something like:

The Bitcoin client's use of unary negation has caused traders to lose $32.49 Billion over the last three week period, according to a recent study by Price Waterhouse Cooper. From a similar accounting produced by the US Office of Management and Budget, HODLers lost an amazing $1.479 Trillion - That's Trillion with a "T"!

A table-driven Unary NOT solves the issues caused by the use of Bitwise NOTs and direct operator use. To that end, we submitted a pull request. After 4 weeks of repeatedly sending the same pull request to the BC team (and being ignored), we are now disclosing this Bitcoin Client issue for everyone to see. Remember to patch this use in your Bitcoin Client, or your funds may be stolen! If you need to use an editor for this fix, and do not normally program, the use of SED will speed up your client edits tremendously. https://www.gnu.org/software/sed/manual/sed.html*

Now that this critical issue has been disclosed, we strongly urge all Bitcoin users to patch immediately, or their funds will likely be summarily depleted within a week.


It does have an enterprise edition!


Excellent! That solves the distribution problem, due to the size of the release. Nobody will notice anything off, in enterprise deployments.


Sounds suspiciously like a LUT




A satirically unneeded look-up table, but yeah, pretty much!


what.. why..




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: