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
Because Bitcoin stores the target as a float type, this is truncated to be
You may ask why the maximum is not
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
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 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
then difficulty is
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.