June 22, 2023
A Merkle Tree is a binary tree of hash values, where each leaf node represents a single piece of data or a hash of a piece of data. It is used to verify the integrity of large amounts of data efficiently. It was invented by Ralph Merkle in 1979 and is widely used in cryptocurrencies like Bitcoin and Ethereum.
A hash function is like a fingerprint machine. It takes any input, such as a file, a text, or a number, and produces a unique output, called a hash, that is much shorter than the input. The hash is like a fingerprint of the input. It is tough to find two different inputs that have the same hash. And it is impossible to get the original input from the hash.
But what if you have many files? Calculating and storing the hash for each file separately would be tedious. That's where Merkle trees come in handy.
A Merkle tree is like a family tree of hashes. You start with the hashes of each file as the leaves of the tree. Then you pair up the hashes and combine them to get new hashes. You repeat this process until you reach the top of the tree, where you have only one hash left. This hash is called the Merkle root.
But how do you verify a specific file? You don't need to download and check all the files in the tree. You only need to download and check a few hashes along the path from the file to the Merkle root. This path is called a Merkle proof.
Merkle Tree with Example
Then you can calculate H AB by combining H A and H B and calculate H ABCD by combining H AB and H CD . If H ABCD matches the Merkle root, you can be sure that."cat.jpg" is correct.
You can use the same analogy to verify if the transaction present in the block is tempered by verifying the Merkle root instead of cat.jpg or dog.txt. It'll be a bunch of transactions at the leaf nodes.Please note: I have truncated the hash value to 6 characters in the above example to make the diagram look clean.
This scenario can quickly be addressed by duplicating one node, thus making the total number of nodes four.
For example, suppose you have transactions. T A, T B, and T C. You can calculate their hashes. H A, H B, and H C. Then you can duplicate H C to get H C'. Afterward; you can pair up and combine the hashes to get H AB, H CC’, and H ABCC’. The Merkle root is H ABCC’.
Summing up, Merkle trees benefit blockchain and other distributed systems, where many computers must share and verify large amounts of data. By using Merkle trees, they can save bandwidth, storage, and computation time.
Here's a quick Java example to show how you can create your own Merkle tree:Merkle Example JAVA Code
Output of the above code:
Thank you for taking the time to read my article.
Part 2: Merkle Tree: A Blockchain Perspective
In a blockchain context, a Merkle tree is constructed for the transactions within a block. Let's take an example to illustrate this process. Consider a block containing four transactions:
Subscribe to the newsletter to learn more about the decentralized web, AI and technology.