Understanding Bitcoin Mining Difficulty

Nicolas Paugam
Sesterce
Published in
8 min readNov 15, 2022

Introduction

Blockchain is the technology behind Bitcoin, which is built on a peer-to-peer network. It uses a chained data structure to store data, a distributed consensus algorithm to generate and update data, and cryptography for security. The Bitcoin network circulates cryptocurrency via miners who solve a cryptographic puzzle with their computational power. As global computing power increases, blocks are likely to be created at a much faster rate, which will negatively impact Bitcoin’s stability and security. As a solution to this problem, Kraft [1] studied the Bitcoin network’s difficulty, and proposed an update method that ensured that there was a stable time for finding new blocks. Bitcoin difficulty maintains a 10-minutes time duration for finding new blocks to maintain system stability. The purpose of this article is to describe the difficulty’s computing process, as well as analyze its influence factors.

Each block of the Bitcoin blockchain network contains three fields, namely the block size, the block header, and the block body. Actual block size is recorded in the block size field. Generally, a block header consists of six components, said to be “Version”, “PrevBlock hash”, “MerkleRoot hash”, Time stamp or “n-Time”, Difficulty target or “nBits” and “Nonce”. As for the block header size, the field values of “hashPrevBlock” and “hashMerkleRoot” are both 32 bytes, with the other fields being 4 bytes each.

Fig.1. Bitcoin block header components

There are two parts to the block body: the number of transactions and the transaction list. The number of transactions represents the number of transactions that have been verified by the network and packed into the block. Each transaction that has been verified in a block is recorded in the transaction list. In this group of transactions, the first transaction, called a coinbase transaction, is a special transaction since it allows the miners to assign the input address for their own. A block header contains a target value in which the result must be less than or equal to the value calculated by the miners to pack the transactions into the block.

Fig.2. Block body [2]

Miner competes for the accounting rights of Bitcoin transactions, and whoever finds the next valid block receives coinbase rewards and all transaction fees. Blocks that meet the target value’s SHA256 hash value are “valid blocks”. In detail, Bitcoin mining involves the following steps:

  • Create a new block of transactions by selecting the transactions you would like to include. Transactions can be selected by the miners from the transaction lists to be confirmed. Regardless, since each block has a limit on capacity, the miners rank transactions according to the amount of their transaction fees, and then to maximize their transaction fees, they choose transactions with higher transaction fees.
  • Construct the Coinbase transaction. A total amount of transaction fees can be calculated for the new block after identifying the transactions that are included in it.
  • The Merkle Root hash for all transactions selected are constructed.
  • Complete the rest of the block header fields.
  • The block header should be hashed, i.e., SHA256D should be performed.
  • Test to see if the result meets the difficulty. If it does, broadcast the new block to the entire network. Upon detecting that the result does not meet the difficulty, miners may change more than one field based on a specific strategy before continuing with the next Hash operation.
Fig.3. Bitcoin mining steps

The difficulty of bitcoin mining

The difficulty of Bitcoin mining refers to the difficulty of finding a valid block, which is unified across the whole network and is updated every 2016. In this section, we present a method of calculating the difficulty of a block, and examine the factors that affect it.

  • Target of A Block: A block header’s nBits field indicates whether the current hash value is less than or equal to the target. The block header hash result is 256 bits, whereas nBits is only 32 bits (4 bytes). The purpose of this section is to introduce the process of computing the target based on the value of nBits. Hexadecimal numbers with a prefix ‘0x’ are usually used to represent block N’s nBits value. It is possible to divide nBits into two parts, with the first two digits representing the exponent and the last six representing the coefficient. In a block, the nBits are denoted with, the exponent part with E, the coefficient part with c, and the target of the block is denoted by T. Following is the formula we can use to calculate the target of a block N based on its nBits value

𝑇(𝑁) = 𝑐(𝜔(𝑁)) × 256𝐸(𝜔(𝑁))-3.

Blocks with unchanged exponents will have linearly increasing targets, while blocks with unchanged coefficient parts will show monotonic growth. for example see figure 4.

Fig.3. Example target calculation

Example: Consider the block 𝑁1 = 277316. The 𝑛𝐵𝑖𝑡𝑠 value of the block 𝑁1 is 0𝑥1903𝑎30𝑐, thus, the exponent part and the coefficient part are

𝐸(𝜔(𝑁1)) = 0𝑥19, 𝑐(𝜔(𝑁1)) = 0𝑥03𝑎30c

the target of the block 𝑁1 is

𝑇(𝑁) = 𝑐(𝜔(𝑁)) × 256𝐸(𝜔(𝑁))-3

= 0𝑥03𝑎30c × 2560𝑥19–3=238, 348 ×25622

  • Difficulty of A Block: It is generally assumed that the difficulty value for the genesis block of the Bitcoin blockchain network is 1, as it is the first block found in the blockchain network. Denote the genesis block as 𝑁0, the difficulty of a block as 𝐷. This formula can be used to calculate the difficulty of each block N1 𝐷(𝑁1) = 𝑇0 𝑇(𝜔(𝑁1)) , where 𝑇0 is the hex target of the genesis block 𝑁0, i.e., 𝑇0 = 𝑇(𝜔(𝑁0)). As the target of the block changes, the difficulty of the entire Bitcoin blockchain network will change, reflecting the difficulty for the miners to find the next new valid block.

How bitcoin mining difficulty affects bitcoin mining

By definition, bitcoin mining difficulty refers to the difficulty of finding new bitcoin blocks through mining. Due to Bitcoin’s decentralized nature, an algorithm hardcoded into the source code by Bitcoin’s creator Satoshi Nakamoto is used to verify transactions. In order to ensure that blocks are discovered steadily, this algorithm constantly adjusts the level of difficulty in response to the number of miners on the network. It is always harder to mine bitcoin when a flood of miners or mining rigs comes into play. The protocol reduces the mining difficulty for the remaining miners if the number of miners drops (that is, if fewer miners compete to find new blocks).

By adding or reducing zeros to the target hash, the mining difficulty of the bitcoin network can be adjusted. Each miner tries to beat the target hash (fixed-length code) each time they start mining. Winners are selected according to who generates the first random code with an equal or higher number of zeros than the target hash.

A lack of such a system would likely lead to blocks being discovered faster and faster as more miners joined the network with ever-more sophisticated equipment. A potential outcome of this would be the influx of new bitcoin into circulation at a rapid rate, which could dampen its value hike. It usually takes Bitcoin two weeks to update its mining difficulty, which occurs every 2,016 blocks. During every 2,016 block interval, the network determines whether the times taken to mine a new block have decreased or increased based on the mining activity for the previous two weeks. A mining difficulty increase will be applied if it takes less than 10 minutes and vice versa. It means that the Bitcoin difficulty algorithm maintains a 10-minute block time for finding new blocks in order to keep the system stable. The figure below shows the bitcoin mining difficulty (orange) and the bitcoin price (black).

Fig.4. Bitcoin mining difficulty and bitcoin price [3]

How does the difficulty adjustment work?

A miner encodes the Timestamp into each block header whenever they find a new block. In a 2016 block difficulty window, the Bitcoin protocol measures the average time between blocks, and then performs the difficulty adjustment as follows:

  • When the average block interval exceeds 10 minutes, the difficulty needs to increase.
  • An average block interval slower than 10 minutes needs to result in a reduction in difficulty.

It means that during the mining of the last 2,016 blocks, the network calculates the total time it takes. A standard 20,160 minutes (10 minutes x 2,016 blocks) is divided by the time taken to scale the last difficulty epoch, then the most recent difficulty level is multiplied by it. Based on the calculation, we can determine the required percentage change in mining difficulty to achieve the 10 minute block time.

The formula is:

new difficulty = current difficulty * expected time / actual time

  • new difficulty: difficulty for the next 2016 blocks
  • current difficulty: difficulty for the last 2016 blocks
  • actual time: time difference between the first block of the difficulty period and the 2016th block of this period
  • expected time: 20160 minutes (as stated in the reference implementation)

In the chart below, we can see the Mean Block Interval (orange), the 14-day Average (black), and the 10min target (600 seconds, dark blue). It is clear that Bitcoin’s Mean Block Interval has been lower than its target for most of its history. A faster mining industry leads to faster blocks, which increases difficulty. That’s because efficiency grows in the mining industry. When bearish markets or shock events occur, there are times when the Mean Block Interval is higher than the target for an extended period.

Fig.5. Mean block interval and target of 600s [3]

Basically, difficulty measures how hard it is to find a hash below a given target when mining a Bitcoin block. When difficulty is high, mining the same number of blocks will require more computing power, improving the security of the network against hacks. There is a direct relationship between difficulty adjustment and total estimated mining power in the chart of Mean Hash Rate (TH/s).

Fig.6. Mean hashrate [3]

Conclusion:

Miners must find a new valid block within a certain timeframe because the difficulty plays an important role. Approximately 10 minutes is the maximum time for finding a new block in the Bitcoin network. The difficulty will be adjusted every 2016 blocks. Consequently, difficulty plays a crucial role in ensuring Bitcoin ecosystem stability. As a preliminary step, we examined the principles and influence factors of the difficulty in Bitcoin mining.

References

[1] Wang, F.-Y., Computational Social Systems in a New Period: A Fast Transition Into the Third Axial Age, IEEE Transactions on Computational Social Systems, 4(3): 52- 53, 2017.

[2] https://marcsteiner-consulting.ch/description-of-bitcoin-blocks-and-transactions

[3] https://studio.glassnode.com/dashboards/btc-miners

--

--