What is Mining Difficulty?
Mining Difficulty in the bitcoin actually indicates how difficult and time consuming is to find the right hash for each block; and solving the mathematical equation for the right block creation, requires huge amount of computational power.
How is Mining Difficulty calculated?
Bitcoin mining difficulty is calculated with various formulas. However, the most common one is:
Difficulty Level = Difficulty Target/Current Target
Note:
Difficulty Target is a hexadecimal notation of the target hash whose mining difficulty is 1. In contrast, the Current Target is the target hash of the most recent block of transactions. When these two values are divided, it yields a whole number which is the difficulty level of mining bitcoin.
For instance, if the answer is 24 trillion, then a miner is expected to generate approximately 24 trillion hashes before he can find the winning hash. Of course, sometimes miners can get lucky and find it with significantly fewer guesses.
OR
New difficulty = current difficulty x (expected time / actual time).
Where 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
Bitcoin was created with the intention to mine 2016 blocks take two (2) weeks to mine. If the blocks are created in shorter period of time, the difficulty is automatically adjusted. If the hash rate is shorter or longer than the intended two weeks, for example, it took only 10 days to mine 2016 blocks, it means the difficulty is too low; and therefore, the difficulty will be increased around 40% to take (in our case 2 weeks) to mine all 2016 blocks.
OR
The Bitcoin difficulty algorithm is programmed to keep the entire system stable by maintaining a 10-minute duration for finding new blocks. In essence, it takes roughly 10 minutes for one miner out of the entire network to generate a winning code and win the right to propose a new block of bitcoin transactions to be added to the blockchain.
Generally, the difficulty will decrease if the bitcoin network hash rate is too low. There are number of reasons why Satoshi has really introduced these coding. One of the reasons could be to eliminate the risks of hackers buying really fast computers to create new blocks and potentially overwrite the bitcoin system. Therefore, the adjustment of network difficulty rate either downward or upward actually depends upon the number of network participants in the mining network and their combined hash power. Thus, the mining difficulty actually determines how difficult is to solve the cryptographic mathematical puzzle in the process of bitcoin mining. The difficulty of the bitcoin network decreases or increases overtime depending on how the miners in the network work.
‘Difficulty Epoch’ is the term used to describe a period in which 2,016 blocks are added to Bitcoin's ledger, ideally in 20,160 minutes, or 14 days. If the epoch ends in less time, the network adjusts the difficulty.
Since Bitcoin’s inception, network difficulty has grown from 1 to as much as 48.71 trillion hashes that a miner would theoretically need to generate to find the winning one. This means it is 48.71 trillion times harder to mine a Bitcoin block today than when mining first began in 2009-a compound increase of 20.64% per month.
Difficulty in-depth
There are two values we need to consider:
1. Target (or bits): The target states how long a block hash must be in order to fulfill the mining condition. The higher the target, the easier it is to find a value below it, 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. A difficulty of 6,000,000 means it takes 6,000,000 times more tries (on average) to find a valid hash than 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.
The Bitcoin 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.
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.
Example calculations of difficulty:
1. Example 1
· Current difficulty: 1
· Time between the first and the last block of each difficulty period: 17,500 minutes
· 1*(20160/17500) = 1.152
· This case you will see that the difficulty rate is higher i.e., 1.152 > 1 as the time between the last 2016 blocks was too short i.e., 17,500 minutes.
2. Example 2
· Current Difficulty: 615,413
· Time between the first and last block of the difficulty period: 24,000 minutes
· 615,413*(20160/24000) = 516,946.92
· In this case, the difficulty rate is low as it took more time i.e., 24,000 minutes to find the last 2016 blocks than intended i.e., 20160 minutes.
3. Example 3 (Taken from Bitcoin blockchain)
· Timestamp block 104,832 (first block of the current difficulty period): 27.01.2011 09:916
· Timestamp block 106,847 (last block of the current difficulty period): 08.02.2011 05:45
· Current difficulty: 22,012.38
· Time between block 98784 and 100799=17906 minutes
· 22012.38*(20160/17906) = 25998.57
· In this case, the difficulty is 25998.57 i.e., higher than 22012.38 as the actual time taken to mine the last 2016 block is 17906.
Calculation of the Bitcoin Target
· Target=max_target/difficulty
· Max_target: the target, if difficulty is 1 (lowest possible difficulty), its value is 0x1d00ffff
· Difficulty as calculated in the above examples.
The target is stored in a packed hexadecimal form in the block header. To expand it to a useable value, we need to split bits-value into two parts viz., index and coefficient. The first bye is the index, the following three bytes are the coefficient. As show in the above image.
Target = coefficient * 2 ^ (8 * (index – 3))
1. Example 1
· We take block number 600000
· Given bit (target) in decimal: 387294044
· We convert them into hexadecimal and extra the index and coefficient. Using the tool Click HERE to Convert to Hexa
· Bits in hexadecimal: 1715A35C
· Coefficient: 0x15A35C
· Index: 0*17
· Target = 0x15A35C * 2 ^ (0x8 * (0x17 – 0x3)) =
· 0x15a35c000000000000000000000000000000000000000
· Where 0x is a decimal number
· To compare it with other targets and the puzzle solution, this hex number gets padded with leading zeros
· 0x00000000000000000015a35c000000000000000000000000000000000000000
· Target in decimal: 2072520395859657486634608572838975759381606196813234176
You can use Target-Bits and Difficulty converter to check your results using the following link
https://blockchain-academy.hs-mittweida.de/bitcoin-target/
Will review your comment and get back!