Everything about hashing in Blockchain

Apr 16, 2022 By Puneet verma

The process of converting an arbitrary size of data into a fixed-sized value is called hashing and it is a one-way process, i.e. the input can't be retrieved from the output(hash). Read more about hashing from here.  

It is an integral part of blockchain technology. We have a lot of hashing algorithms that can be used for hashing. Bitcoin uses SHA-256(Secure hashing algorithms). 

Hashing in Blockchain 

Data present in the block header is an input for the hashing. Typically each block header has 

  • Version number 
  • Timestamp
  • Previous hash 
  • Nonce, which is the value the miners need to create a block
  • A hash of a Merkle root
  • Target Hash 

In bitcoin, miners are responsible for finding out the hashing.

Why hashing is required in blockchain? 

The “chain” part of the blockchain is hashing. 

Blockchain is similar to a linked list. A linked list consists of nodes where each node contains a data field and a reference(link) to the next node in the list. In the blockchain, hashing is used for the purpose of identifying the next block i.e each block contains a hash of the previous block. 

 

hashing in blockchain

Hashing makes a block immutable.

If an attacker attempt to change any data, the hash of that block will be changed and this forces the attacker to redo all the work in all of the subsequent blocks. Let's understand this by an example

We have five blocks, A, B, C ... 

If an attacker modifies the content of block A, the hash of block A will be changed, Now block B has to update its previous hash value to the new hash of block A, by changing the content of block B, its hash also changed.  Soon we have to redo all the work till the last block.

The interesting part is that attackers have to rework all this before a new block is added to the existing chain by any other miner. It involves a huge computation power i.e greater than the entire network combined

What's the mathematical puzzle in mining?

The puzzle is to find a magic number (the nonce) that results in a hash satisfying specific criteria when added to a block of transactions. These criteria are also referred to as difficulty levels.  

These puzzles can be like  

  • Finding a hash such that should be equal to less than 2112ea765ffacdeef
  • Finding a hash containing a particular number of leading zeros.

Let's explore the case where we require four leading zero

Miner starts with brute-forcing the value of nonce till the criteria are met. Generally, the miner begins with a Nonce value of 1 and continues incrementing it until the generated hash meets the specified criteria.

Mining puzzle

Anders Brownworth Hash Program: https://andersbrownworth.com/blockchain/block

At the time(2022) of writing this blog, the bitcoin network requires approx 10min to solve the puzzles. 

Note: The blockchain network adjusts the puzzle's difficulty level as per the requirement. 

Why puzzle is required to solve in blockchain?

The puzzle is required to prevent Sybil attack.

You know these contests where you submit a photo, and the poster of the most upvoted photo gets a reward? A Sybil attack is when you create thousands of fake accounts to bring your own photo to the top.

Blockchains also rely on voting, in a way; the valid chain of transactions is defined as the consensus chain of transactions. To prevent the scenario above, they just make voting very, very computationally expensive, so if e.g. to cast 1 vote you need 1 day of computational power, creating a second account just shifts this to 2 votes for 2 days of computational power (since now your computer needs to do double the amount of work); you can create as many fake accounts as you want, the end result is still 1 vote per 1 day. The only solution to increase your voting power is to buy more hardware, and this costs money (unlike creating fake accounts, which is practically free).

Common Myths

1. If we got the solution to the puzzle, that means the block is valid and all transactions are legitimate

The puzzle is used to get a hash based on certain conditions and it doesn't validate a block or check the authenticity of any transaction. A famous example is  the "Value overflow incident"  it happened in the bitcoin network where the block is mined successfully  but the data is invalid  

2. Puzzle gives bitcoin value as it creates scarcity

yes puzzles keep bitcoin scarce, but ultimately, it is people that give it value. The fact that something is scarce doesn’t necessarily mean it has value. Gold is expensive not because it's scarce, but it's expensive because people value it. 

Do read these interesting article

Why aren't all scarce products are valuable

Why Has Gold Always Been Valuable?

Why does the puzzle become harder over time? 

In order to maintain the balance between supply and demand bitcoin adopted this.

Let's understand this with a simple example when there is plenty of money in the market, then the value of money decreases. Then you will need to give more money to buy the daily commodities. The price of commodities will keep on rising.

Due to this reason, governments do not print plenty of cash. It will create inflation and the price of everything will go high and the price of money will fall down. This makes the economical system fail.