This project shows how to calculate those three values that are used in the Bitcoin protocol.
This is the compressed value that is used in the block header. It's a 4 byte (32 bits) value defined by:
exponent
: the first byte. It tells how "far to the left" is the value;coefficient
: the last 3 bytes. It is the value itself (with some precision, but not full).
Example:
From https://learnmeabitcoin.com/technical/block/bits/
Note that the nBits is a value that is not 100% accurate, but nevertheless it is used by the miners.
This is the uncompressed value that miners use to validate the block found, i.e., if the block hash is below it. It's a 32-byte (256 bits) value.
It is defined by:
This value actually does not exist in the protocol, it is more to provide a human-readable information to how difficult it is to mine a block. It has the inverse meaning of the target, i.e., higher the target, lower the difficulty (easier it is to mine a block); lower it is the target, higher the difficulty (harder to mine a block).
It is defined by:
where:
max_target
: hardcoded value defined in the protocol that has the value of0x00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff
(but because of how the protocol is implemented, it is truncated to0x00000000ffff0000000000000000000000000000000000000000000000000000
;current_target
: the current (uncompressed) value.
The time to mine a valid block is an equation that brings into consideration the current hashrate. The formula is given by:
If the hashrate is given in hashes/seconds, then the result will be in seconds.
With this information, you can check in how many blocks (estatistically) the miner will find a valid block by knowing that each block is mined roughly every 10 minutes: