If you are reading this blog, it means you are aware about Blockchain and eager to know about Merkle tree concept. Merkle tree is one of the most important concept that you need to know and be aware about before you proceed further in Blockchain. Merkle tree concept is not for Bitcoin Blockchain rather it is applicable to all of the Blockchain platforms.
What is Merkle Tree ?
In this blog, we will explain you what is Merkle tree and how it is used in Blockchain. Merkle tree concept is not new, it is there from long time but it came to lime light with Blockchain technology. Let's deep dive and understand about it. Below diagram represent Merkle tree so let’s try to understand it step by step.
Here T stands for Transaction and H stands for Hash of input. There can be n number of transaction at given time. For this example, we have taken 4 transactions for this first scenario.
In Merkle tree, transactions are hashed to get their corresponding hash value. Different hash algorithm used in different blockchain platform, like in Bitcoin SHA-256 is used to generate hash and in Ethereum Keccak-256. Hash output is nothing, it is long string for any input that is not human readable.
When you hash Transaction-A, you get Hash for that transaction and same for other transaction as well. As you see in the diagram, we get total 4 hashes for 4 transactions. Now these hashes are combine in pair to create new hash e.g. If you combine Hash-A and Hash-B, you will get new Hash-AB. So, you get two hashes in next step which is Hash-AB and Hash-CD. Now these hashes are also combined in pair and new hash got generated until we get single hash. E.g. Hash-AB and Hash-CD combined to generate Hash-ABCD.
This Hash-ABCD is known as Merkle root.
Now there can be scenario when we have odd transactions instead of even transactions. That is explained in below diagram where we have only 3 transactions. As you see in below diagram for transaction- C we have Hash-C but we don’t have any other Hash available to be in pair with Hash-C so in that scenario, Hash-C is taken two times, combined and generate a new Hash that is Hash-CC.
Benefits of Merkle Tree
Now question come about benefits of Merkle tree and usage of Merkle tree in Blockchain. Answer to that question is below.
- Data Integrity: Merkle tree maintain data integrity as you can easily verify is someone has tempered the data as if anyone try to change anything in the Merkle tree, that change will generate new hash and in turn Merkle root will be different.
- Need little memory / disk space and proofs are computationally easy and fast.
- Easy transaction queries: When you use Merkle tree and need to check if one transaction is included or not then you need not to download the full blocks in Blockchain, you can easily traverse through the tree and check the transaction. Light client in Bitcoin and Ethereum works on that principals only where these clients only download block header not the full blocks in Blockchain.