Friday, November 22, 2024

blockchain – What are the equations to transform between bits and issue?

There are 3 representations of the identical factor (with various levels of precision) in Bitcoin:

  • bits – unsigned int 32-bit
  • goal – unsigned int 256-bit
  • issue – double-precision float (64-bit)

and 6 strategies are essential to convert between any two of those:

  • bits -> goal (SetCompact() in bitcoin/src/arith_uint256.cpp)
  • bits -> issue (GetDifficulty() in bitcoin/src/rpc/blockchain.cpp)
  • goal -> bits (GetCompact() in bitcoin/src/arith_uint256.cpp)
  • goal -> issue (similar as goal -> bits -> issue)
  • issue -> bits (not executed in bitcoin/src)
  • issue -> goal (similar as issue -> bits -> goal)

The Bitcoin supply code can do the conversion from bits -> issue as requested within the query, however can not do the conversion from issue -> bits as additionally requested within the query.

I’ve written my very own implementation of the problem -> bits conversion in vanilla Javascript by mimicking the goal -> bits conversion the place attainable, plus some further checks:

operate difficulty2bits(issue) { 
    if (issue < 0) throw 'issue can't be adverse';
    if (!isFinite(issue)) throw 'issue can't be infinite';
    for (var shiftBytes = 1; true; shiftBytes++) {
        var phrase = (0x00ffff * Math.pow(0x100, shiftBytes)) / issue;
        if (phrase >= 0xffff) break;
    }
    phrase &= 0xffffff; // convert to int < 0xffffff
    var dimension = 0x1d - shiftBytes;
    // the 0x00800000 bit denotes the signal, so whether it is already set, divide the
    // mantissa by 0x100 and improve the scale by a byte
    if (phrase & 0x800000) {
        phrase >>= 8;
        dimension++;
    }
    if ((phrase & ~0x007fffff) != 0) throw 'the 'bits' 'phrase' is out of bounds';
    if (dimension > 0xff) throw 'the 'bits' 'dimension' is out of bounds';
    var bits = (dimension << 24) | phrase;
    return bits;
}

It’s attainable to validate that the above operate provides appropriate solutions by doing the next conversion:

bits -> issue -> bits

The place bits -> issue is finished utilizing Bitcoin’s GetDifficulty() and issue -> bits is finished utilizing difficulty2bits() above. If we arrive again on the similar bits worth then the difficulty2bits() operate is appropriate. The one exception is when (bits & 0x00800000) != 0, since because of this bits is a adverse quantity, whereas issue is at all times a optimistic quantity in Bitcoin.

I’ve examined the above difficulty2bits() operate and it does return the identical outcome as the unique bits worth. If you wish to do the assessments your self then I’ve created a reside conversion instrument on my weblog the place you are able to do any of the 6 conversions listed above in actual time (I’ve transcribed Bitcoin’s SetCompact(), GetDifficulty() and GetCompact() into Javascript): https://evaluation.null.place/how-do-the-bitcoin-mining-algorithms-work/#form7

Notice that numbers in Javascript are IEEE 754 double precision – the identical precision as the problem within the Bitcoin supply, so Javascript is as correct because the Bitcoin supply for all bits/issue/goal conversions. Nevertheless, to assuage scepticism I’ve additionally included the related unit assessments from Bitcoin’s bitcoin/src/take a look at/blockchain_tests.cpp and bitcoin/src/take a look at/arith_uint256_tests.cpp information on the weblog just under the aforementioned instrument – all assessments go.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles