In-depth explanation of Bitcoin Mining Difficulty

Published by Mario Oettler on

There are two values we need to consider:

  1. Target (or bits): The target states how low a block hash must be in order to fulfill the mining condition. Remember in Bitcoin it is a bit like in a Limbo dance where you need to underpass a certain threshold. The higher the target, the easier it is to find a value below it, and the lower is the difficulty.
  2. Difficulty: This is a numerical expression of how difficult it is to find a valid hash compared to the easiest difficulty of 1. For individuals, the difficulty value is more comfortable to interpret than the target. A difficulty of 6,000,000 means it takes 6,000,000 times more tries (on average) to find a valid hash than with the easiest difficulty of 1. With a constant hash rate in the network, this would lead to a 6,000,000 times longer block finding time.

Difficulty and target can be converted into each other. The formula is given below. But first, we have a detailed look into the target.

The Bitcoin Target

The Bitcoin difficulty target is simply a 256-bit number, and it is part of the block header. There it is stored a packed hexadecimal value in a field called bits.

The target defines the max height of a block header to be a valid block. This means, the higher the target, the lower is the difficulty. You can imagine that like a Limbo dance.

It gets adjusted according to the hash rate to maintain a constant block finding time and dispense the block reward not too fast or too slow.

Where is the Difficulty Stored in Bitcoin Blocks?

To make the difficulty target forgery-proof, it is part of the block header and serves as the input value of the SHA256 hash function during mining.

The difficulty target of each block is stored in a field called bits.

Typically, the target is stored in hexadecimal format. Some block explorers however, give the target as a decimal number. It is possible to convert both values. The following screenshots show both difficulty and target in two blocks.

Difficulty and target of block 1000.
Some block explorers convert the packed hexadecimal value of the target into a decimal value.

Difficulty Adjustment in Bitcoin

The difficulty is adjusted periodically every 2016 blocks. The average block finding time is given in the reference client with 10 minutes. This results in 20160 minutes, which is 14 days. (The whitepaper https://bitcoin.org/bitcoin.pdf mentions only a moving average without concrete numbers).

If the time for 2016 blocks is shorter than 20160 minutes it means the difficulty is too low, and the miners increase it. If the block’s time is above 20160 minutes, the difficulty is too high, and it gets lowered. The time is calculated by the timestamps each block contains.

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)

To prevent large changes in the difficulty, a single retarget doesn’t change the new target by more than a factor of 4 in either way.

Difficulty chart by btc.com

Calculation Example 1

Here, we calculate the difficulty with given values:

  • current difficulty: 1
  • Time between the first and last block of each difficulty period: 17,500 minutes.

1.152 = 1 * 20,160 / 17,500

The new difficulty would be 1.152. As you can see it is higher than the current difficulty. The reason is that the time between the last 2016 blocks was too short.

Calculation Example 2

The following values are given:

  • Current difficulty 615,413
  • Time between the first and last block of the difficulty period: 24,000 minutes

516,946.92 = 615,413 * 20,160 / 24,000

Here, the new difficulty is lower because it took more time to find the last 2016 blocks than intended.

Calculation Example 3

This example is taken from the Bitcoin blockchain.

  • Timestamp block 104,832 (first block of the current difficulty period): 27.01.2011 09:16
  • Timestamp block 106,847 (last block of the current difficulty period): 08.02.2011 05:45
  • Current difficulty: 22,012.38

Time between block 98,784 and 100,799 = 17,906 minutes

25,998.56938 = 22,012.38 * 20,160 / 17,906

You can compare this with the difficulty given in the block which is 25,997.88. The small difference is because of rounding errors.

Calculation of the Bitcoin Target

The following formula is used to calculate the target:

target = max_target / difficulty

  • max_target: the target, if difficulty is 1 (lowest possible difficulty). Its value is 0x1d00ffff.
  • difficulty: see section difficulty

The target is stored in a packed hexadecimal form in the block header. To “expand” it to a useable value, we need to split the bits-value into two parts – index and coefficient. The first byte is the index; the following three bytes are the coefficient.

target = coefficient * 2 ^ (8 * (index — 3))

Example 1

We take block number 600,000

  • Given bit (target) in decimal: 387,294,044

Now we convert it into hexadecimal and extract the index and coefficient. You can use the Hex-Converter Tool here.

  • Bits in hexadecimal: 1715A35C
  • Index: 0x17
  • Coefficient: 0x15A35C

target = 0x15A35C * 2^(0x8*(0x17 – 0x3)) = 0x15a35c0000000000000000000000000000000000000000

The 0x means it is a hexadecimal number.

To compare it with other targets and the puzzle solution, this hex number gets padded with leading zeros.

0x00000000000000000015a35c0000000000000000000000000000000000000000

Target in decimal: 2072520395859657486634608572838975759381606196813234176 You can use our Target-Bits and Difficulty converter to check your results:

Bitcoin Target Calculator

Bitcoin Target Calculator

Categories:

if()