Thursday, December 26, 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 accomplished 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 problem -> bits conversion in vanilla Javascript by mimicking the goal -> bits conversion the place attainable, plus some extra checks:

perform difficulty2bits(issue) { 
    if (issue < 0) throw 'issue can't be destructive';
    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 perform offers right 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 identical bits worth then the difficulty2bits() perform is right. The one exception is when (bits & 0x00800000) != 0, since which means that bits is a destructive quantity, whereas issue is at all times a constructive quantity in Bitcoin.

I’ve examined the above difficulty2bits() perform and it does return the identical end result as the unique bits worth. If you wish to do the exams your self then I’ve created a stay conversion software 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 problem 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 exams from Bitcoin’s bitcoin/src/check/blockchain_tests.cpp and bitcoin/src/check/arith_uint256_tests.cpp recordsdata on the weblog just under the aforementioned software – all exams move.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles