Bitcoin Mining — The Nonsense of Finding the Right ‘Nonce’

Arjit Agarwal
4 min readMay 22, 2021

--

All of you might have heard about term called ‘Bitcoin Mining’. This article aims at explaining Bitcoin mining.

What is Bitcoin mining?

Bitcoin mining is a process in which nodes validates the incoming transactions in Bitcoin network by combining a few transactions (in order of fee attached) in a block and finding a valid hash for this block. Once a node finds valid hash for this block it sends its block to network and the block is added to the blockchain. In return for the hard work, miners receive block rewards (currently 6.25 BTC per block) and transaction fees.

Bitcoin Transaction Lifecycle

. A typical mining setup looks something like this:

4x Radeon Miner with Water cooling

So, what do these machines with huge processing power and water cooling actually do? The short answer is they keep generating random numbers and calculate a hash (SHA-256) with it unless they find a valid hash. That’s all.

Before diving deep into technical details of mining we first need to look at what is inside a block. A block has following data:

1. Block Number -> Incremental starting from 0 for genesis block.

2. Unix Timestamp -> Seconds elapsed since 00:00 UTC Jan 1, 1970

3. Nonce ->The number for which miner is looking.

4. Transaction Data

5. Previous Block Hash

6. The hash of this block.

SHA256(block number, timestamp, nonce, transaction data, prev. block hash)

What is hash?

A hash is a 64 digit hexadecimal number like this:
000000000000000000072a4f409c23a1f6316f00d84df82b5d4d93ffec2cb049

Since it is a hexadecimal number it has 16⁶⁴ or 2²⁵⁶ possible values from 0 to 16⁶⁴-1

Now, we have all data in the block and we simply need a number ‘nonce’ that generates a valid hash. But what is a valid hash?

Around every 2000 blocks Bitcoin network sets something called ‘target’. A ‘target’ is a hash all the hashes below which can be valid block hashes. All the hashes produced above ‘target’ are invalid.

All hashes above target are rejected.

So miners have to find a ‘nonce’ that generates a hash below target and this is what all these miners do, i.e., guessing random numbers and calculate SHA256. Although from the diagram it doesn’t seems that this task is difficult in actual it is and Bitcoin network keeps adjusting this ‘difficulty’ by adjusting number of 0s at the starting of valid hash (minimum 8 zeroes should be always there).

Let’s do some maths and calculate that a given nonce finds a valid hash if target is highest , i.e., 0x00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff

Clearly the probability that the generated as would be valid is:

p = 16⁴⁸/16⁶⁴ = 1/16¹⁶ = 1/2⁶⁴

And this is when we have maximum target. If we reduce the target ‘difficulty’ increases even more.

More About Nonce

The ‘nonce’ which miners find is a 32 bit number so it have only 2³² possible values which roughly equals ~4 billion. There are two problems associated to it:

  1. A simple home computer can calculate hundred millions of hashes per second and so they can get exhaust nonce range in just 40 seconds. Imagine those super mining rigs who can do trillions of hashes per second.
  2. There is a possibility that all the 2³² possible values of nonce do not generate a valid hash as the invalid hash space is very large when compared to nonce values.

So, what happens if a node exhausts nonce space without finding a valid hash. Does this mean giving up the race? No.

Two things are possible here:

  1. For machines who cannot exhaust nonce space in less than 1 second the timestamp for the block changes. Since, timestamp is also used to calculate SHA256, nonce space automatically resets for these machines after each second.
  2. But what about those super mining machines who can outrun their nonce space in small fraction of second? They can reconfigure transactions for the block to reset their nonce space.

When a transaction is sent to the Bitcoin network, it arrives at an unconfirmed transaction mempool. Miners cannot pick all the transactions as block size is limited to 2 MB and so they pick those transactions first with which high fees is attached.

Since transaction data is also used to calculate SHA256 miners can simply pick new set of transactions to reset their nonce space even if the timestamp has not changed.

--

--

No responses yet