13.8 C
New York
Wednesday, April 17, 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 (identical as goal -> bits -> issue)
  • issue -> bits (not carried out in bitcoin/src)
  • issue -> goal (identical 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 issue -> 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 unfavorable';
    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 enhance the dimensions 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 offers appropriate solutions by doing the next conversion:

bits -> issue -> bits

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

I’ve examined the above difficulty2bits() operate and it does return the identical consequence as the unique bits worth. If you wish to do the checks your self then I’ve created a reside conversion device 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

Be aware that numbers in Javascript are IEEE 754 double precision – the identical precision as the issue within the Bitcoin supply, so Javascript is as correct because the Bitcoin supply for all bits/issue/goal conversions. Nonetheless, to assuage scepticism I’ve additionally included the related unit checks from Bitcoin’s bitcoin/src/check/blockchain_tests.cpp and bitcoin/src/check/arith_uint256_tests.cpp information on the weblog slightly below the aforementioned device – all checks move.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles