-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mention That nBits Can Be Negative #3
Comments
Hey David, this is very strange. According to the block header definition, the nBits field is of an unsigned integer type, in 0.9.0 it was "unsigned int" and in the current code it is "uint32_t", to be precise. I'll look into that a bit deeper and see how it unfolds. Warm greetings, |
You are indeed correct. The compact floating point representation includes a sign bit. significand = compact & 0x007FFFFF this can be found exactly in here here. The full target is then:
When the next target is computed and stored in nBits, the sign bit is essentially ignored. I'll adopt these changes asap. Thanks for the help. Warm greetings, |
@minium that description looks accurate to me. Thanks! |
Hi Krzysztof,
The description of nBits should probaly mention that the high bit of the
mantissa indicates sign---otherwise implementors might accidentally
encode a different target value than they intended. For example, an
nBits of 0x01803456 would be parsed by the formula in the reference as:
But the Bitcoin Core
src/test/bignum_tests.cpp
file says:There can even be negative nBits:
I'm pasting a draft copy of some text I've written about this for the
Bitcoin.org developer docs; it provides some more background and
explains how Bitcoin Core deals with the situation based on my reading
of the code (which could be wrong, of course). It also has references to
the relevant code. Please feel free to use any text you find useful; no
credit is required.
Before I start pasting, I want to thank you for producing the excellent
reference doc. It's always one of the first places I check when I'm
trying to figure something out.
Target nBits
The target threshold is a 256-bit unsigned integer compared the 256-bit
SHA256(SHA256()) header hash (treated also as an unsigned integer).
However, the header field nBits provides only 32 bits of space, so the
target number uses a less precise format called "compact". A naïve (but
incomplete) breakdown of nBits shows it to be a sort of scientific
notation to provide an approximate value.
As a base-256 number, nBits can be quickly parsed as bytes the same way
you might parse a decimal number in base-10 scientific notation:
Although the target threshold should be an unsigned integer, the nBits
header field uses a signed data type, allowing the target threshold to
be negative if the high bit of the significand is set. However, because
the header hash is treated as an unsigned number, it can never be equal
to or lower than a negative target threshold. Bitcoin Core deals with
this in two ways:
threshold into a target of zero, which the header hash can equal (in
theory, at least).
produce an nBits which will be interpreted as negative; if so, it
divides the significand by 256 and increases the exponent by 1 to
produce the same number with a different encoding.
Some more examples taken from the Bitcoin Core test cases:
The text was updated successfully, but these errors were encountered: