I've created some javascript code as a result of my research in trying to understand how target difficulty works. It seems a good idea to share after finding this post.
First it creates the range of SHA256 powers of 2 thresholds for you to visualize the shear magnitude of these numbers.
Then the bitsToHex function is used to decompress "bits".
Then decompressed "bits" are casted as a BigInt in order to get the integer used for hashing comparison.
Code comments should clarify the rest.
console.log("\nSHA256 in powers of 2\n");
// from easy to hard
for(let i = 256n; i >= 0n; i-=4n) {
// calc decimal representaion
const dec = BigInt(2n**i-1n);
// calc hexadecimal representation
const hex = BigInt(2n**i-1n).toString(16).padStart(64, "0");
// beautify console
console.log(`2n**${i}-1n D`.padStart(12, " "), dec.toLocaleString("en-US").padStart(103, "."));
console.log(`2n**${i}-1n H`.padStart(12, " "), `0x${hex}`.padEnd(116, " "));
}
/**
* @dev Takes a Bitcoin block header property "bits" and converts it into a 256-bit hex.
*
* The bits property of a Bitcoin block header is a 32-bit integer that
* represents the difficulty target for mining that block.
* The first three bytes of the bits value are the exponent (in base 256) of
* the difficulty target, and the last byte is the coefficient.
* To convert the bits value into a 256-bit hex, we need to extract the
* exponent and coefficient from the bits value and calculate the target value.
* Then we can convert the target value into a 64-character hex string.
*
*/
function bitsToHex(bits) {
// calc target value from compressed "bits".
const target = (bits & 0x007fffff) * 2 ** (8 * ((bits >>> 24) - 3));
// convert to 256bit hex
return target.toString(16).padStart(64, "0");
}
// console spacer
console.log("\nBitcoin Target Difficulty\n");
// Satoshi's genesis block "bits" A.K.A target hash
// for details see https://bitcoinexplorer.org/block-height/1#JSON
// "bits": "1d00ffff",
const genesisTargetDifficultyHEX = bitsToHex(0x1d00ffff);
const genesisTargetDifficultyDEC = BigInt(`0x${genesisTargetDifficultyHEX}`);
console.log('genesis block target difficulty HEX:', genesisTargetDifficultyHEX);
console.log('genesis block target difficulty DEC:', genesisTargetDifficultyDEC);
// Bitcoins's current block "bits" A.K.A target hash
// for details see https://bitcoinexplorer.org/block-height/776774#JSON
// "bits": "17073039",
const currentTargetDifficultyHEX = bitsToHex(0x17073039);
const currentTargetDifficultyDEC = BigInt(`0x${currentTargetDifficultyHEX}`);
console.log('current block target difficulty HEX:', currentTargetDifficultyHEX);
console.log('current block target difficulty DEC:', currentTargetDifficultyDEC);
...console output:
genesis block target difficulty HEX: 00000000ffff0000000000000000000000000000000000000000000000000000
genesis block target difficulty DEC: 26959535291011309493156476344723991336010898738574164086137773096960n
current block target difficulty HEX: 0000000000000000000730390000000000000000000000000000000000000000
current block target difficulty DEC: 688509036841676372057001313638142781710850853198364672n
As you can see, difficulty has gotten a lot harder since genesis block.