Target
The target is a 256-bit number that all Bitcoin clients share. The SHA-256 hash of a block’s header must be lower than or equal to the current target for the block to be accepted by the network. The lower the target, the more difficult it is to generate a block.
The maximum target used by bitcoin miners is
1 | 0x00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF |
Because Bitcoin stores the target as a float type, this is truncated to be bash 0x00000000FFFF0000000000000000000000000000000000000000000000000000
You may ask why the maximum is not
1 | 2^256 - 1 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF |
There is no definite answer, but the most promising one I found is here. Basically if the maximum target is set to be
1 | 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF |
initially, it means you can mine block the first time you generate a hash, because any SHA-256 hash satisfy hash <= target
. This defeats the purpose of proof-of-work(which I’ll talk about in later posts) enforced by bitcoin blockchain.
Difficulty
Difficulty is a measure of how difficult it is to find a hash below a given target. The goal of difficulty is to make sure bitcoin generate one block every 10 minutes. So as you imagine, difficulty is not a constant number. In fact, the bitcoin rule is to change it every 2016 blocks. It is easy to figure out that to mine 2016, it takes 2 weeks time (2016 block * 10 mins/block). So difficulty is adjusted every 2 weeks.
How difficulty is calculated? It is very easy difficulty = maximum_target / current_target
. Assuming current_target
is
1 | 0x00000000000404CB000000000000000000000000000000000000000000000000 |
then difficulty is
1 | 0x00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF / 0x00000000000404CB000000000000000000000000000000000000000000000000 |
At the end, I want to show you a very interesting graph to illustrate difficulty and mining time relationship and reason behind those. As you can see, every time difficulty is adjusted, the average minting time resets back to 600 seconds which is exactly where we want it to be. Over time, more and more power added by miners, they find block quicker and quicker. After two weeks, everything resets again.
Reference:
https://en.bitcoin.it/wiki/Difficulty
https://en.bitcoin.it/wiki/Target
https://www.coursera.org/learn/cryptocurrency/lecture/0htpQ/the-task-of-bitcoin-miners
https://bitcoinwisdom.com/bitcoin/difficulty