Bitcoin Mining Difficulty - What is it And How Does it Work?

Ameer Rosic

4 months ago
Bitcoin Mining Difficulty - What is it And How Does it Work?

Before we even begin to understand what bitcoin mining difficulty means, we need to know how mining works. We have covered this topic in detail before, so we will just give you a little overview before getting into the different nuances of difficulty. Following that, we will look at how mining difficulty is calculated and how it changes to suit the network’s needs.

How does mining work? How long does it take to mine 1 Bitcoin?

Bitcoin’s network has several specialized nodes called “miners” who use specialized equipment to solve cryptographically hard puzzles. If they are successful, then they will get the opportunity to add blocks to the BTC blockchain successfully. This is how it works:

  • The miner picks up transactions waiting in the mempool and hashes them.
  • They add a random hexadecimal value to the front of the hash and hashes the entire value.

This hash needs to be less than a particular value, which is called “difficulty.”

What determines bitcoin mining difficulty? Why does BTC difficulty increase?

#1 To maintain network integrity

The level of Bitcoin mining difficulty increases or decreases according to the ease of mining within the protocol. Remember, Bitcoin needs to have a consistent block time of 10 minutes. In other words, new BTC can be injected into the circulating supply every 10 minutes. To make sure that this timing doesn’t change the Bitcoin protocol:

  • Increases network difficulty when it becomes easier for miners to mine.
  • Decrease network difficulty when it becomes harder for miners to mine.

The Bitcoin network has a universal block difficulty. All valid blocks must have a hash below the target. Mining pools also have a pool-specific share difficulty setting a lower limit for shares.

#2 Relationship with hash rate

One of the critical metrics in judging the health of a proof-of-work network is hash rate. Simply put, hashrate shows you how powerful the miners are within the network. Higher the bitcoin network hashrate, higher it’s overall security and speed. However, these networks need to keep their hashrate under control for consistent block production. This is why, when hashrate becomes high, the bitcoin difficulty eventually gets higher as well, making it tougher for miners to mine easily within the network.

The inverse is also true.

If Bitcoin’s hashrate decreases, the network difficulty will reduce as well. Hashrate may decrease because of the following reasons:

  • Bitcoin currently has a high difficulty, which is why the miners are having a tough time mining in the system.
  • The price of BTC went down, which is why a lot of miners quit mining.

To understand the correlation between the two, let’s check out their graphs. Up first, we have the hash rate.

After that, we have the bitcoin difficulty chart:

As you can see, there is a very close correlation between the two. Around March 26, the network difficulty fell by 16% from 16.55 trillion to 13.9 trillion. This was the largest crash in network difficulty since early 2013. To understand why this happened this time around, look at how the hashrate dropped as well just before the bitcoin difficulty drop. This dip occurred because of Bitcoin’s price crash, which forced a lot of miners to quit operations.

How does Bitcoin calculate difficulty?

Bitcoin’s network difficulty changes every 2016 blocks. The formula used by the network to calculate difficulty goes like this:

difficulty = difficulty_1_target / current_target

In the formula above:

  • target is a 256-bit number. As per Bitcoin’s protocol, the targets are a custom floating-point type with limited accuracy. Bitcoin clients approximate difficulty based on this fact. This value is also known as bdiff.
  • difficulty_1_target can be different depending on how you choose to measure difficulty. Traditionally, it represents a hash where the leading 32 bits are zero and the rest are one. In fact, this value is also known as pool difficulty or pdiff.

Every single block stores a packed representation of bitcoin difficulty in their blocks called “Bits.” This target usually appear as 0x1b0404cb (stored in little-endian order: cb 04 04 1b).

A block calculates the target value via a predetermined formula. Eg. With the packed target given above, i.e. 0x1b0404cb. The hexadecimal target is:

0x0404cb * 2**(8*(0x1b – 3)) = 0x00000000000404CB000000000000000000000000000000000000000000000000

Now let’s calculate bdiff and pdiff.

The highest possible target (difficulty_1_target) is defined as 0x1d00ffff or, in hex form:

0x00ffff * 2**(8*(0x1d – 3)) = 0x00000000FFFF0000000000000000000000000000000000000000000000000000

Now that we know this value, we can use this to calculate our bdiff using the difficulty = difficulty_1_target / current_target formula

Now, as we have defined in the previous section, the current_target is 0x1b0404cb or 0x00000000000404CB000000000000000000000000000000000000000000000000.

So, to calculate current difficulty:

0x00000000FFFF0000000000000000000000000000000000000000000000000000 /

0x00000000000404CB000000000000000000000000000000000000000000000000

= 16307.420938523983

Hence, bdiff is 16307.420938523983.

Now, let’s calculate the pdiff. Mining pools tend to use non-truncated targets which puts difficulty_1_target at 0x00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF.

If that’s the case then for the same current_target, our pdiff will be:

0x00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF /

0x00000000000404CB000000000000000000000000000000000000000000000000

= 16307.669773817162

Here is a program code taken from Bitcoin wiki which relies on logs to make difficulty calculation easier:

#include <iostream>

#include <cmath>

inline float fast_log(float val)

{

int * const exp_ptr = reinterpret_cast <int *>(&val);

int x = *exp_ptr;

const int log_2 = ((x >> 23) & 255) – 128;

x &= ~(255 << 23);

x += 127 << 23;

*exp_ptr = x;

 

val = ((-1.0f/3) * val + 2) * val – 2.0f/3;

return ((val + log_2) * 0.69314718f);

}

float difficulty(unsigned int bits)

{

static double max_body = fast_log(0x00ffff), scaland = fast_log(256);

return exp(max_body – fast_log(bits & 0x00ffffff) + scaland * (0x1d – ((bits & 0xff000000) >> 24)));

}

int main()

{

std::cout << difficulty(0x1b0404cb) << std::endl;

return 0;

}

How do you set a mining difficulty?

Miners use specialized ASIC hardware to mine Bitcoins. These machines are extremely fast and produce tetrahashes every single second. It will be extremely impractical for a system to painstakingly check every single one of them to see if they satisfy all the necessary conditions, or not. This is exponentially true for mining pools. They can’t check all the hashes produced by a bitcoin miner every single second. This is why mining pools use a concept called “Share Time.”

So, let’s imagine that your bitcoin mining pool has set a Share Time of 5 seconds. This means that, on average, your mining pool will require miners to submit a share to them every 5 seconds.

How exactly is this done?

Your bitcoin mining pool will set a value called Share Difficulty for every miner. The share difficulty of a miner is directly proportional to their individual hashrate. As such, higher the miner’s hashrate, higher their Share Difficulty. The idea is that the miner will use their equipment to generate tons of hashes. The moment they find a hash that meets the target Share Difficulty, they will send the hash to the pool.

How are the miners rewarded?

Miners in the pool are rewarded on a “Pay per share” (PPS) basis. In this system, the miners get rewarded for the shares they submit. The values of the shares are entirely dependent on how difficult it was to discover the share.

Let’s take an example to see how this works:

  • Suppose you are a miner with an individual hashrate of 50 TH/s.
  • The mining pool that you have joined has set your Share Difficulty at 1,000,000.
  • The moment that you get shares above 1,000,000, you’ll be rewarded by the pool.
  • The pool may change your difficulty to make sure that you are not submitting your shares too quickly.
  • Now, if you buy some new equipment and increase your hashrate to 150 TH/s, the pool will increase your difficulty to 3,000,000. You will be submitting shares at the same rate that you were previously submitting. However, you’ll get 3 times the reward that you were previously receiving for the shares you submit.
  • The reason why pools recommend higher difficulties for faster hardware is to reduce network load on both the miner’s system and the pool. It also reduces decreases the restart delay for your mining hardware as it prepares for the next work unit. At the same time, the pool must be careful not to set the difficulty too high which will result in a lot of stale shares.

NOTE: Share Target = 1 / Share Difficulty

The Importance of Difficulty in Nakamoto consensus

To understand how critical difficulty is to Bitcoin’s ecosystem, you need to know how Nakamoto consensus works. For a wide area network with no centralized entity, consensus protocols are the only way to maintain any form of governance. Traditional consensus algorithms like Raft are not ideal for maintaining a wide-area cryptoeconomic protocol. This is why Satoshi Nakamoto, the creator of Bitcoin, came up with Nakamoto consensus. The central tenet of the Nakamoto consensus is that to participate in the system, one must pay a price. In the case of proof-of-work (POW), i.e., Bitcoin’s consensus, miners pay a price with “work.” Work, in this case, is the heavy amount of computational energy that a miner must spend to mine one Bitcoin. This is where difficulty comes in.  Difficulty is the metric that makes Bitcoin mining hard, plus, this is what Nakamoto consensus leverages to solve the double spending problem.

What is double spending?

Double spending is the reason why all the attempts at creating a decentralized cryptocurrency had failed miserably before Bitcoin. In simple terms, it is a flaw that can allow one Bitcoin to be spent more than once at the same time. We never encountered this issue while dealing with physical cash. After all, if you are buying something with a $10 note, you can’t simultaneously purchase something else with that same note, right?

However, a digital token has digital files that can be easily duplicated, leading to inevitable double spending. As you can imagine, double spending can have several devastating effects on the ecosystem’s economy:

  • Firstly, it inflates the total supply of the coins within the ecosystem, which throws the supply-demand equation out of control.
  • Secondly, if anyone, anywhere can spend the same coin without restriction, it will reduce the people’s faith in the sanctity of that currency.

Bitcoin requires all the transactions to be included in the blockchain, without fail. This makes sure that anyone in the network can trace every single Bitcoin right to its very source. Such a high level of transparency ensures no one will be able to double spend without the entire network noticing. However, let’s think of something more diabolical. Suppose, someone decides to hijack the blockchain by forking out and try to double spend all the Bitcoins.

What happens then?

Well, it turns out that due to network difficulty, the amount of resources and money that the attacker will need to take over the chain will be exponential. As such, it will simply not be economically worth it for them to act against the interests of the system. This is how network difficulty gives Nakamoto Consensus the firepower it needs to maintain network security and integrity.

Conclusion – Bitcoin Mining Difficulty

We hope that you found a lot of value in this article. If you have some doubts, then feel free to reach out to us at any time.

Like what you read? Give us one like or share it to your friends and get +16

59
Hungry for knowledge?
New guides and courses each week
Looking to invest?
Market data, analysis, and reports
Just curious?
A community of blockchain experts to help

Get started today and earn 128 bonus blocks

Already have an account? Sign In