Bitcoin Forum
August 17, 2019, 12:10:29 PM *
News: Latest Bitcoin Core release: 0.18.0 [Torrent] (New!)
   Home   Help Search Login Register More  
Pages: [1]
Author Topic: [script] I'm confused about negative zero!  (Read 147 times)
Coding Enthusiast
Hero Member
Offline Offline

Activity: 659
Merit: 983

Novice C♯ Coder

View Profile WWW
April 02, 2019, 02:49:44 PM
Merited by Foxpup (2), bones261 (2), ETFbitcoin (1)

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?

Projects List+Suggestion box
Donation link using BIP21
Bech32 Donation link!
BitcoinTransactionTool (0.9.2):  Ann - Source Code
Watch Only Bitcoin Wallet (supporting SegWit) (3.1.0):  Ann - Source Code
SharpPusher (broadcast transactions) (0.10.0): Ann - Source Code

Hero Member
Offline Offline

Posts: 1566043829

View Profile Personal Message (Offline)

Reply with quote  #2

Report to moderator
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
Offline Offline

Activity: 1862
Merit: 2631


View Profile WWW
April 02, 2019, 05:00:22 PM
Merited by Foxpup (4), Carlton Banks (4), dbshck (4), bones261 (2), Coding Enthusiast (2), ETFbitcoin (1)

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.

Offline Offline

Activity: 230
Merit: 13

View Profile
April 07, 2019, 03:09:58 PM

There is no negative zero defined for integers!
There is. In Bitcoin scripting and in some other places.
Pages: [1]
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!