A Merkle tree is a basic component of blockchain technology. It is a mathematical data structure composed of hashes of different data blocks that serve as a summary of all transactions in the block. It also enables efficient and secure verification of the content of large amounts of data. Both Bitcoin and Ethereum use the Merkle Trees structure. Merkle Tree is also described as a Hash Tree.
The Merkle Tree concept is named after Ralph Merkle, who patented the idea in 1979. It is a tree data structure in which each leaf node is labeled with a data block hash, and a non-leaf node is labeled with the cryptographic hash labels of its child nodes. Leaf nodes are the last ending node in the tree.
Source :dev.to/
This article was published as a part of the Data Science Blogathon.
A Merkle tree stores all transactions in a block by creating a digital fingerprint of the entire set of transactions. It permits the user to confirm whether a transaction can be covered in a block or not.
Computing hash pairs of nodes develop Merkle trees until only one hash remains. This hash is described as Root Hash or Merkle Root. Merkle Trees are constructed using a bottom-up approach.
Source: researchgate.net
Each leaf node is a hash of transaction data, and a non-leaf node is a hash of its previous hashes. Merkle trees are part of a binary tree; therefore, it necessitates an even number of leaf nodes. If Transactions are in odd numbers, then the last hash will be repeated once to generate an even number of leaf nodes.
Source: developer.bitcoin.org
The above example is the most common and simplest form of a Merkle tree, i.e., a binary Merkle tree. The number of transactions in the block is respectively TX1, TX2, TX3, and TX4. Here you can see that there is a top hash, the hash of the entire tree, known as the Root Hash or Merkle Root. Each of these is repeatedly hashed and stored in each leaf node, resulting in hashes 0, 1, 2, and 3. Consecutive pairs of leaf nodes are then summarized in the parent node by hashing Hash0 and Hash1, resulting in Hash01, and separately hashing Hash2 and Hash3; the result is Hash23. The two hashes (Hash01 and Hash23) are then hashed again to create the Root Hash or Merkle Root.
The Merkle Root is deposited in the block header. A block header is the part of a Bitcoin block that is hashed in the mining process. It contains the hash of the last block, the Nonce, and the Root Hash of all transactions in the current block in the Merkle Tree. The Merkle root directory in the block header makes the transaction tamper-proof. Because this root hash contains the hashes of all transactions within the block, these transactions can save disk space.
Source: selfkey.org
Merkle Tree maintains data integrity. If any individual detail of transactions or the order of transactions changes, those changes will be reflected in the hash of that transaction. This change would cascade up the Merkle tree to the Merkle root, changing the value of the Merkle root and thereby invalidating the block. So everyone can see that a Merkle tree allows a quick and easy test of whether a particular transaction is included in the set or not.
To understand how important Merkle trees are to blockchain technology, imagine a blockchain without them. We will cover Bitcoin mainly because using Merkle Trees is fundamental to crypto-currencies and easy to understand. For example, if Bitcoin did not have Merkle trees, every node in the network would have to maintain a complete copy of every transaction that ever happened on Bitcoin. Any authentication request on Bitcoin would require an extensive packet of data to send over the network, so you have to have it yourself to verify the data. The computer used for verification would have to use a lot of processing power to compare the ledgers to ensure that no changes have been made. Merkle Trees can effectively solve this problem. They hash records in the ledger, effectively separating the evidence of the data from the data itself. Proving the validity of a transaction only involves providing a small amount of information to the network. In addition, it allows you to demonstrate that both variants of the ledger are the same for the titular amount of computing power and network bandwidth.
Importance
Merkle Trees are vital because they allow Merkle proof. These allow us to quickly verify whether an input was included in a particular data set and in what order. Merkle Trees are also effective as they allow us to compress large datasets by removing all unnecessary branches while keeping the only ones we need to prove. In the blockchain world, this means that Merkle Trees provide the following critical features:
Once you understand the basics of Merkle Trees, you can begin to appreciate the protection and efficiency of Blockchain data structures. If Merkle Trees had never been discovered, cryptocurrency and blockchain technology would never have existed. Without a suitable alternative, the amount of computing power and storage would be too expensive to operate. Merkel Trees are vital to blockchains and allow them to function while maintaining the integrity of transactions.
Key Takeaways:
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.