Author

Topic: [script] I'm confused about negative zero! (Read 226 times)

member
Activity: 229
Merit: 13
April 07, 2019, 10:09:58 AM
#3
There is no negative zero defined for integers!
There is. In Bitcoin scripting and in some other places.
staff
Activity: 3458
Merit: 6793
Just writing some code
No, Bitcoin doesn't follow existing standards for integers. Integers are not fixed length (i.e. they are not just int32 or int64). They are variable length. Any stack item of any length can be an integer. Such a stack item is interpreted as a variable length, little endian integer, with the sign determined by the most significant bit of the integer.

Due to how the script interpreter was originally implemented, negative zero is possible because the most significant bit is not part of the integer value itself and is just a sign bit. So you can have all but the MSB be 0, indicating the integer is 0, and have the MSB be 1, indicated it is negative. Hence negative zero.

Zero and negative zero have the same meaning in a script. However this means that scripts using zero can be malleated by replacing the zero with a negative zero.
legendary
Activity: 1042
Merit: 2805
Bitcoin and C♯ Enthusiast
All these numbers we are pushing on the stack are integers (Int32 or Int64 where there is an overflow chance). right?
There is no negative zero defined for integers! The negative zero in IEEE 754 is defined for floating point numbers (float and decimal)!
So what these lines are doing with [0, 0, 0, 0x80] is actually interpreting a completely different value (-2147483648) as so called negative zero by mistake.

What am I missing here?
Jump to: