A Merkle Root is the hash of all the transactions in a block by hashing transactions and again hashing those results in pair till the time there exists only a single hash result for the entire block.
These values play a critical role in ensuing blockchain security and are also used to create Zero Knowledge rollups and Blobs in Sharding of the Ethereum Blockchain.
The map of all the hashes are called as the Merkle Tree or even Hash Trees as they are obtained from hashing. The concepts were invented by computer scientist Ralph Merkle in 1998. You can read his original paper here.
Table of Contents
How is a Merkle Root obtained?
In a block, there exists multiple transactions. To generate a Merkle Root from all the transactions,:
- The transactions are first hashed to obtain a transaction result.
- Then the results are again hashed.
- Each step reduces the number of valued to be hashed by half.
- This is continued till the time there exists only a single hash result.
Suppose there are four transactions in a block which are hashed to generate hash results A, B, C and D.
I have depicted in the diagram above that transaction hashes A and B are hashed again to obtain a hash result X and similarly transaction hashes C and D are hashed to obtain hash result Y. Now, hash results X and Y are again hashed together to obtain hash result R. Since there is no other value left to hash with R, it is considered to be the Merkle Root.
How Merkle Root Secures the Blockchain?
Each transaction is unique in the blockchain and the hashing of two unique transactions produce a unique result.
If there is a slight change in one transaction detail, this change will be reflected in its hash. This change in hash value will continue till it changes the value of the old Merkle Root to a new value.
Now. if you compare the original Merkle tree(left) and the changed Merkle Tree(right) after transaction 1 changes to 1*, you will notice that there is a series of changes as a result.
- A changes to A*
- X changes to X*
- Merkle Root R changes to R*
Therefore, if any hacker changes the value of a transaction, it will soon be reflected in the new Merkle root and will be identified soon because other it will not match with the Merkle root of that block with other verifiers.
As soon as an anomaly is detected in the Merkle root, it can then be traced which transactions were tampered with. In our example, the tampered transaction 1 can be detected by starting at Merkle Root and going upwards.