A Merkle tree might sound like a mystical tree from a fantasy novel. But it is not. Imagine you have a large dataset or file and want to confirm that a particular piece has not been illegally modified without searching the entire file. That is where the Merkle tree comes in. So what is a Merkle tree, and how does it relate to blockchain?
What Is a Merkle Tree?
A Merkle tree—alternatively called a binary hash tree—is the arrangement and summarization of hash values in a hierarchical order. The arrangement starts with Merkle leaves (processed from actual data sets) paired two by two to form Merkle branches, which are paired two by two until the Merkle root (top hash) is derived.
In the diagram above, the Merkle leaves are hashes 0-0, 0-1, 1-0, and 1-1. Hashes 0 and 1 are Merkle branches. Then, the top hash is the Merkle root.
Merkle branches are the intermediate hashes placed between the Merkle leaves and the Merkle root; there does not seem to be a limit to the number of Merkle leaves and branches that can be in each Merkle tree.
Is Every Blockchain a Merkle Tree?
Blockchains contain Merkle trees; they are not Merkle trees.
Many cryptocurrency blocks have a Merkle tree that represents the transactions on the blocks. Merkle trees enable the creation of a single hash that can be used to verify the entire block. Several cryptocurrency blockchains use Merkle trees, including Bitcoin, Ripple, and Ethereum.
Meanwhile, every blockchain is an unchangeable digital ledger. Blockchains have nodes—not hashes—containing the hashes of previous nodes. Unlike the hashes created in Merkle trees, blockchain nodes can contain other information, including timestamps and nonces.
In addition, blocks in a blockchain are formed by a preceding block, not by two preceding blocks, as in Merkle trees, where two preceding hashes create new hashes. Moreover, blocks are generated starting from the genesis block (the root block), while hashes are formed from the total data until a root hash is derived.
How Merkle Trees Work?
Merkle trees employ cryptography—using codes to secure data and communications—to create the Merkle root, which is the hash of all hashes of the transactions in a given block. The transactions or information in each block are organized into leaf nodes, which are hashed and paired two by two to create intermediate hashes. This hashing and pairing are repeated until a single hash is generated.
After the single hash (root hash) is derived, it is stored on the blockchain to represent the data in the block. With Merkle trees, you don’t need to verify all the transactions or information in each block; you only need to verify the affected hash, which can be compared to the root hash and other hashes in the row.
For blocks to be recorded, network nodes have to verify them. Verification requires assigning unique data (hashes) to transactions, so they can be easily verified by matching the hash values. However, each block has numerous transactions. So verifying the hash value of each transaction would be immensely time-consuming. The Merkle tree solves this problem by enabling the verification of blocks using a single hash value. And due to the hierarchical structure of the Merkle tree, changing any transaction/hash changes the root hash.
The Essence of Merkle Trees in Blockchain Technology
Merkle trees arrange transactions and information while enabling swift and easy verification. It also aids the identification of tampered data and ensures immutability and data integrity. For example, you can verify your balance using the root hash and a little data. Asides from verifying cryptocurrency transactions and information, Merkle trees enable NFT projects to whitelist addresses in smart contracts efficiently.
Personal Note From MEXC Team
Check out our MEXC trading page and find out what we have to offer! You can learn more about crypto industry news. There are also a ton of interesting articles to get you up to speed with the crypto world. Lastly, join our MEXC Creators project and share your opinion about everything crypto! Happy trading!
Join MEXC and Start Trading Today!