Blockchain Mining Difficulty
By Hethu NanayakkaraThe cryptocurrency Bitcoin recorded an alltime high at USD 4,300 in MidAugust 2017. The popularity of cryptocurrencies like Bitcoin and Ether (Etherium) have tremendously increased adaptation over the years. Blockchain is the underlying technology that powers these digital cryptocurrencies.
This post is to explain some technical concepts behind blockchain and explain the mining difficulty adjustments that takes place in the network to keep the growth of the blockchain stable.
Blockchain
A “blockchain” is a public database of transactions shared in a peertopeer network between participating nodes secured by cryptography. A transaction in the blockchain is permanently timestamped and is immutable. The blockchain consists of “blocks” of data, with each block containing validated transactions with a reference to the previous block. This creates a chain of blocks, thus the name “blockchain”. This chaining allows anyone to validate the transactions up to the beginning of the chain, all the way back to the very first “genesis block”.
Miners
Blockchain “miners” are special nodes in the network that validates new transactions to create new blocks on the blockchain. To create a new block, miners require huge amounts of computing resources or processing power. Miners are required to create a block that satisfies certain criteria set by the network. They need to prove that they created a new block (the “proofofwork”) by showing their candidate block. The new block gets added to the blockchain only if the majority of the network nodes agree on the validity of the block.
The Block
To explain the concepts further, we will use the Bitcoin blockchain as an example. A block’s main content is a list of validated transactions. The block header contains a reference to the previous block and few other meta data. The header includes an important value called the “nonce”. Nonce is a random value generated by the miners and included in the block so that the blockhash satisfies the hash requirement (hash target).
The SHA256 hash algorithm generates a 64digit hexadecimal hash value. The current Bitcoin blockchain requirement is to find a nonce that creates a blockhash with 17 leading zeros.
Note: The actual hash difficulty is not about the leading zeros. It is about finding a hash less than the hash target set by the network. However, this is approximately about finding a hash having a specific number of leading zeros.
Bitcoin Mining
Miners reward themselves with the “block reward” for creating new blocks. The block reward started at 50 BTC when the blockchain began in 2009 and halves every 210,000 blocks, roughly every 4 years. Current block reward is 12.5 BTC. The block reward will ultimately reach zero and a total of 21 Million Bitcoins would be mined by the year 2140.
Up to now, miners have added around 480,000 blocks and generated 16.5 Million Bitcoins. This is the total amount of Bitcoins currently in circulation and owned by all Bitcoin users.
The Blockchain Supercomputer
Finding a block with 17 leading zeros is an extremely difficult resourceintensive task for any single computer. However, with all the miners in the network with their combined computing power, it takes an average of 10 minutes to construct a new block. Think of this as one supercomputer generating blocks every 10 minutes.
When new mining nodes with faster and faster CPUs are added to the network, the blockchain super computer becomes more and more powerful. Naturally, it should produce new blocks faster. However, the Bitcoin network ensures that new blocks are created at a constant pace by recalibrating the hashdifficulty so that the new blocks will be created at an average of 10 minutes.
For example, when the Bitcoin blockchain started in 2009, the initial requirement was to generate a block with a blockhash having 8 leading zeros. Today, due to the much faster supercomputer, it needs to find a block having 17 leading zeros.
Understanding the Blockchain Hashing Difficulty
A SHA256 generates a 256bit hash (64 hexdigits). The Bitcoin network defines a “hash target” every 2016 blocks. This is roughly every 2 weeks when a block is created every 10 minutes.
One block every 10 mins = 6 blocks per hour, 6 x 24 in a day, and 6 x 24 x 14 = 2016 blocks in two weeks.
The miners are looking for a block hash (a binary number) that is less than or equal to this “hash target”. For example, if the hash target is 0000a1b2c3e4f5, any hash less than or equal to this number is a valid block hash. Many hashes would satisfy this requirement and any one of those would be a valid. However, it is an extremely difficult task to find such a hash. Lesser the hash target, the more difficult it is to find a satisfying hash.
As an example, if we were to find a hash less than or equal to 0FFFF, we have 65,536 choices. However, if we were to find a hash less than or equal to 000FF, we have only 256 choices. A lower target number means fewer choices. Visually, more leading zeros we require in our hash, much harder it is to find a satisfying hash.
You can view the current Bitcoin blockchain information by visiting blockchain.info. Search for any block by giving the block number. For example, if we look at a recent Block #456789, we will see two important values related to the difficulty calculation: Bits and Difficulty
Source: blockchain.info
The Bits
“Bits” is a value represented in compact notation indicating the “hash target”. Bits is a fourbyte value. The first byte indicates the number of bytes (length) of the hash target while the following three bytes indicate the most significant bytes.
In our Block #456789 example;
Bits: 402809567 (decimal) => 18 02 62 DF (hex)
Each byte has two hex digits. The first byte represents the length of the hash target: 18 (hex) => 24 (decimal). Which means the hash target is 24 bytes in length, with the most significant bytes specified in the subsequent three bytes.
Hash Target
When the three significant bytes are written as a 24byte value:
Hash target: 02 62 DF 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
The miners are attempting to find a block hash less than or equal to this hash target.
SHA256 hashes have 64 hex digits. When the hash target is represented as a full 32byte value (64 hex digits):
Visually for simplicity, this can thought of as miners trying to find blockhashes containing 17 or more leading zeros.
Adjusting the Hash Target
As mentioned before, the hash target is adjusted every 2016 blocks so that the time to generate blocks remain at an average of 10 minutes.
If it has taken less than 10 minutes average time to generate the last 2016 blocks, the formula will yield a smaller hash target, which is harder to find.
The Difficulty
In the example block above, we see a large value called “Difficulty”. The “difficulty” gives a relative figure of the hash difficulty of mining a block compared to mining the very first block. The difficulty is a ratio between the block #0 (genesis block) hash target and the current hash target:
The genesis block Bits were hardcoded to 486604799 (0x 1D 00 FF FF). This results in a hash target of 8 zeros followed by FF FF followed by 26 zeros. This is a very large hash target. The hash target becomes smaller and smaller over time; there by making the difficulty becomes larger.
The difficulty of the block #0 was 1. Since the Difficulty depends on the hash target, its value changes every 2016 blocks.
Source: blockchain.info
The difficulty increases exponentially. Today, your typical generalpurpose computer would take thousands of years to mine a new block on its own. The combined mining power of the Bitcoin supercomputer can do this in 10 minutes.
References

Blockchain explorer: https://blockchain.info