In our previous article on Bitcoin Mining, we looked at the overall functioning of the bitcoin mining process. In this article, we will take a deeper dive into the technical aspect to understand how Bitcoin mining work?
What is the Byzantine Fault Tolerance(BFT) Problem?
In simple words, BFT lets a decentralized system function normally in an adverse situation when there is a malicious user in the system, or a fraction of the system fails.
When the majority of users in a system are ethical personals (i.e., people who follow the rules), they can override the malicious work done by non-ethical individuals. So for a system to function normally, at least 51% of users should adhere to the rules set up in the system. This gives rise to consensus, which is the general acceptance of specific rules and regulations.
In Bitcoin mining, we use Proof of Work(PoW) as the consensus algorithm. The most critical part of PoW is the hash function. So, let us have a look at the hash function.
Also Read: A Candid Explanation of Bitcoin
What is a hash function?
A hash function is a one-way function. The best way to understand hash is to use the analogy of a butterfly and caterpillar. A caterpillar can grow up to be a butterfly, but the reverse is not possible. Different caterpillars will grow to be a different butterfly.
Similarly, in the case of a hash function, when input is fed into the hash function, it will provide a specific output, but there is no way to produce the input from the output. And different input produces different output.
In the case of bitcoin mining, we use a SHA256 hashing algorithm.
Specifications of hashing function-
- It is a one-way function.
- Provides a fixed-length output for any length of the input.
- It is unique for every input.
Let’s have a look at hashing function in action if we provide CoinCodeCap as input to a hashing function.
SHA256 (CoinCodeCap) = a3e5eff2907bf65e78fbd57920b4061a9a8afcf4025cab279a1c4c0672e79939
If we make a minor change in the name, for example, coinCodeCap, then.
SHA256 (coinCodeCap) = 61f80a0951c7e8eddfd3ab110f0d9be085506b6ae8dff9719fb7281a3bbd45ec
which is completely different from the previous one.
If we increase the word length to CoinCodeCap is the best platform to explore crypto-related products, then.
SHA256 (CoinCodeCap is the best platform to explore crypto related products) = cb162f23986f1bfb7aa0f2214285899bc8b0bfaf357c3b528e6b52290e60140b
All the outputs above have a fixed length of 256 characters.
Now that we have looked at the hash function used in bitcoin, which is SHA256, let’s have a look at the mathematical puzzle and how it is solved in mining.
Find the Target Hash
So when miners validate transactions, they create a block by grouping those validated transactions. Then miners need to search for a target hash for this block.
The target hash is set by defining the number of zeros to be present in the hashed value. For example – A target Hash, which has six zeros at the start.
The input fed into the hashing function is-
- Transaction data- The data size should not exceed 1 MB.
- Nonce — It stands for Nonsense. It is just a random integer number.
Let us consider a scenario where it is mandatory to have 4 zeros at the beginning, followed by any combination of hexadecimal for a block to be mined.
A miner collects transactions of 1 MB from mempool. Along with the transaction data, timestamp and nonce are feed into the SHA256 function.
This spits out a hexadecimal character. Chances are there that the output may not start with 4 zeros. In that case, the nonce is incremented and then checked if it begins with 4 zeros or not.
Since every input has a unique output, variation in nonce and timestamp would produce a different hash value. This step is repeated until a suitable hash starting with 4 initial zero is produced.
Then that miner will be allowed to add the block to the blockchain network containing –
- Transaction Data
- Calculated Nonce
One of the key features of PoW is that the proof should be difficult to produce but easy to verify.
Once it is propagated into the bitcoin network, the hash is verified by every node by imputing the transaction data, nonce and timestamp present in the block and compared with the hash present in the block.
If they match, then it is accepted otherwise, it is rejected. Since the proof is tough to produce, miners use massive computational power to try a different kind of permutation and combination of nonce and transaction data to get the desired output.
In the case of mining rigs, multiple computers can calculate hash at the same time by varying the nonce over a range.
For example, consider a mining rig with four computers. Each computer is assigned with a range of nonce. Once they pick up 1 MB worth of transactions, each computer varies the nonce at the same time over the same transaction data to reach the output faster.
Also Read: An ultimate guide to Bitcoin Paper Wallet
So, Computer 1 may vary the nonce from 1–55. Computer 2 varies nonce from 56–100 and so on. At every timestamp, the corresponding hash is calculated by every system by varying their assigned nonce.
This helps in reaching the result faster than a single mining machine. Today, Bitcoin mining is not possible on standard computers because of enormous difficulty. There are machines called ASIC (Application-specific integrated circuit) that are now used for Bitcoin mining.
If you want to know more in-depth articles on how Bitcoin mining work, let us know in the comment sections.
If you want to learn more about the Crypto ecosystem, sign up for the weekly newsletter.