# A Semi-Technical Dive Into Bitcoin Mining

## Mining Difficulty, Hash Power, Nonce Range And The Like.

In this article, I’d like to cover some of the “nuts and bolts” of bitcoin mining. I’ll try to do this in a progressive way, so as not to get too technical too soon. Where to start from, then? Well, what about from the basics?

## What Is Blockchain?

*Blockchain* is the name of the technology at the basis of Bitcoin (as well as other cryptocurrencies). In particular, it refers to the mechanism through which the transactions of coins from one stakeholder to another are recorded in the distributed ledger that is shared by the different members of the Bitcoin network. This ledger consists of an ordered sequence of blocks — hence the name blockchain — each containing a certain number of verified transactions¹. New transactions are added to new blocks, which, in turn, are appended at the end of the existing chain.

Apart from its own list of transactions, each block also contains a timestamp, a reference to the previous block in the chain (this reference is called *previous hash*) and a number called *Nonce*. Before going into the details of what the “previous hash” and the “Nonce” are, we must first gain a general understanding of another important term: *mining*.

## What Does Mining Mean?

Before being added to the blockchain, a new block needs to be validated. Here by “validating ” we are not referring to the checks that are performed on its transactions to verify their legitimacy, but rather to some extra work that needs to be done *on top of those checks* in order to accept the new block into the existing chain. This work is what the term *mining *refers to.

So, what does this work consist of? It consists of the following: (i) *hashing* the new block and (ii) checking that the result of such operation satisfies a certain criterion. Let me explain… Hashing is the process by which a digital document is deterministically transformed into a long number, the *hash*, that somehow represents the fingerprint of the original document. In the Bitcoin network, new blocks are hashed using the SHA256 algorithm, which transforms the input (in our case the new block) into a 64 digit hexadecimal number.

Now, in order for the new block to be added to the blockchain (or, as we say, to be *mined*) the requirement is for its hash to be lower than a given target. If the hash of the new block does not satisfy this criterion (*i.e.* it is higher than the target), then the hash is considered not valid, and a new hash needs to be calculated. This is where the concept of Nonce comes into play. In order to find a new hash we need to change the content of the block that we are trying to mine, otherwise, the hashing algorithm (which is deterministic) will find again the same invalid hash found before. The trick, then, consists of changing the value of the Nonce (a 32-bit unsigned integer) and rehashing the block. This process of rehashing slightly different variations of block² is repeated until a valid hash is eventually found, as shown in Figure 4. When this happens, the block is added to the blockchain and the miner who found the valid hash is remunerated with a number of bitcoins (hence the expression *mining bitcoins), *that at the time of writing is set to 12.5, plus any fees associated with the transactions included in the block. As an important digression, here I want to point out to the reader that the number of bitcoins used to remunerate miners for finding valid hashes decreases over time by halving approximately every 4 years. This will allows a total of 21 million bitcoins to be ever released in the network. The reason for this will be a matter for another article, where I will address the economic implications of Bitcoin’s new paradigm.

The diagram in Figure 4 is a simplification of the actual mining process and it can be considered a coarse representation of the real picture. More details are given in the next section. As for now, let’s try to understand how the target for the hash is calculated and what its role and significance are.

## The Mining Difficulty

The target is calculated so as to maintain an average mining pace of one block every 10 minutes. To calculate the value of the target, the network first assesses how much hashing power is available (*i.e.* how many hashing operations per second the network is capable of), and based on this assessment the target is adjusted so as to expect a new block to be mined every ten minutes in average. If the hashing power of the network increases over time, the target will decrease in value to make mining more difficult and keep the mining pace roughly constant. Conversely, if the hashing power decreases, the target increases making mining new blocks easier.

To understand this concept let’s imagine for a moment that the hashing algorithm consists of picking up a number at random from a pool of equally possible values, like in a lottery. For the sake of simplicity, let’s say that the range of the possible values goes from 0 to 100, and that we win if we pick a number that is smaller than or equal to a target *T*. If *T *is set to 100 then we are guaranteed to win, as any number picked from the pool will have a value that is smaller than or equal to 100. However, if *T* is set to 50, then we have only 50% chances of picking a winning number (winning becomes more difficult). If *T* is set to 10, then our chances to win decrease to only 10%, and winning becomes even more difficult. Because we want to maintain a constant rate of winning extractions from the pool of numbers, the target *T* must be adjusted depending on how many numbers I am allowed to pick at every extractions (or, if you prefer, how many lottery tickets I have bought for the same extraction). The more tickets I have (correspondingly, the more hashing power I have) the lower the target *T*, and vice versa.

Although the hashing algorithm is deterministic and does not consist of picking a number at random, its characteristics are such that in some respects it looks like if it did.

# Let’s Get Technical

What explained above is a very high-level overview of the mining process. However, the way mining works is a little bit more complicated than that. If you feel geeky and want to dive into some technical details/crunch some numbers, please keep reading.

So, we said that the hash of a block (as calculated by the SHA256 algorithm) is a 64 digit long hexadecimal number. This means that the range of its possible values goes from 0 to 16⁶⁴.

Number of possible hash values =

16⁶⁴

Given the current mining difficulty in the Bitcoin network, the hashes that are considered valid must have 18 leading zeros, hence the total number of *valid* hashes is 16⁴⁶ (where 46 = 64–18).

Total number of valid hashes =

16⁴⁶

This means that the probability that a randomly picked hash is a valid hash is 16⁴⁶ / 16⁶⁴ = 16⁻¹⁸ ≈ 2 x 10⁻²².

Probability of a random hash to be valid =

2 x 10⁻²²

Finally, the Nonce is a 32-bit unsigned integer. As such its range of possible values spans the interval from 0 to 2³² (or from 0 to approximately **4 *10⁹**).

Total number of Nonces =

4 *10⁹.

Now the question is, what is the probability to find a valid hash (hence the probability to mine a new block) by simply going through the entire range of Nonces? This probability can be easily calculated by multiplying the total number of Nonces by the probability of picking a valid hash by chance, *i.e.*4 x 10⁹ x 2 x 10⁻²² = 8 * 10⁻¹³.

Probability to find a valid hash by spanning the entire range of Nonces ≈

10⁻¹² = 0.0000000001%

This number is extremely low. In fact, it is so low that you surely wonder how on Earth a new block gets ever mined! To answer this question let’s first consider the example of a miner with a modest hashing power of 100 million hashes per second (100 MH/s). For such a miner it would take about 40 seconds to go through the entire range of Nonces, notably a much shorter time than the average 10 minutes required to mine a block. Now, as we have seen just a few lines above, it is extremely unlikely that the miner finds a valid hash just by going through the entire range of Nonces. So we can safely assume that the miner was unable to find a valid hash. Now, what should the miner do after those first 40 seconds? It would make no sense to go through the entire range of Nonces again, as this would result in exactly the same hashes as before. Unless…. something else changes in the content of the block! And this is indeed what happens. The Nonce is not the only thing that changes in the block. The timestamp changes as well, and it does so every 1 second. Just a reminder, the timestamp is expressed in Unix time and represents the number of seconds passed since 01 Jan 1970. Because the timestamp changes every second and is part of the information that is hashed, we can go through the same range of Nonces again and again and still find different hashes as long as the time stamp changes. Going back to our example, the miner starts hashing the block recursively for increasing values of the Nonce. After one second (hence, before he gets to go through the entire range of Nonces, which would require him 40 seconds) the timestamp changes. This means that the miner can start hashing again from the beginning of the Nonce range and be assured to find different hashes than before in virtue of the fact that the timestamp is different than one second ago. After another second, the timestamp will change again and the miner will start hashing the block again from the beginning of the Nonce range. This process will continue until a valid hash is found by either the miner in question or another miner.

The process I’ve just described is a viable solution for a miner with a relatively small hashing power. But what about big mining farms or mining pools, where the collective hashing power is much larger than the 4 billion hashes per second required to span the entire range of Nonces before the timestamp changes? The solution described before would not be suitable for these farms/pools, as they would keep going through the entire Nonce range multiple times before the timestamp changes. This would make pointless to have such a massive hashing power, to begin with. Unless… somehting else changes in the block!

The other thing that can change is the list of transactions contained in the block. Such transactions come from a memory pool, also known in short as *Mempool*. The Mempool contains all the unverified transactions, *i.e.* all the transactions that have not been added to any mined block yet. The Mempool can than be though of as a staging area for transactions. The miner constructs a new block by picking from the Mempool the transactions with the highest fees. If the miner goes through the entire Nonce range in less that one second (as it happens for mining pools and mining farms with big hashing power), the transaction with the lowest fee among those previously chosen is replaced with the transaction with the next highest fee among those left in the Mempool, and the miner will go again through the Nonce range. If, after going through the Nonce range again the timestamp changed, then no transaction will be replaced and the miner will hash the block going through the Nonce range again but with the updated timestamp, otherwise, the lowest-fee transaction will be replaced using the same mechanism explained above.

Explaining all this in plain English might be quite challenging, so the diagram below shows you how the mining algorithm works in a format that may be more easy to grasp. Consider this diagram as the more detailed and accurate version of the diagram shown in Figure 4.

## Mining And Consensus Protocol (A Topic For Another Article)

What has been said so far about mining does not take into account the fact that there are many miners in the Bitcoin network, all competing with each other in the race to find a valid hash for the next block and receive the corresponding remuneration.

Also, we didn’t explain why mining is necessary and we can’t just add blocks to the existing chain without the hassle of finding a valid hash, nor why the Bitcoin’s distributed ledger is structured as a chain of blocks in the first place.

To address these points we need to broaden the discussion about mining so as to encompass other important and related topics such as *blockchain security* the *consensus protocol*. I believe, however, that this article contains already enough material as it stands, and I will leave the discussion of its ramifications to a different piece (**now available ****here**).

Stay tuned!

## Footnotes

- The number of transactions in each block is constrained by the allowed maximum size of the block itself. The block maximum size is 1 MB for Bitcoin and 8 MB for Bitcoin Cash.
- It is important to notice that even slight variations in the input of the hashing algorithm result in completely different outputs (hashes).