Merkle trees, staples of the computer science ecosystem, have found usage in the cryptocurrency space. Interest in them has recently increased within the crypto sector as a result of the FTX debacle.
This article is an in-depth exploration of Merkle trees in blockchain, explaining what they are, how they work, and their applications in the industry.
What Are Merkle Trees?
A Merkle tree, also a hash tree or a binary hash tree, is a data format with applications in computer science and cryptography.
At its core is the hierarchical layout of data blocks defined by a cryptographic hash. This layered arrangement of data blocks gives it a tree-like appearance.
The concept of Merkle trees is the brainchild of Ralph Merkle, who patented it in 1979, thus its name. They are crucial in safeguarding data purity and security in sectors dealing with large datasets like blockchain.
The Structure of Merkle Trees in Blockchain
To better understand the structure of Merkle trees in blockchain, imagine an upturned tree with three primary levels. At the top is the Merkle root, the non-leaf nodes occupy the middle part, and the leaf nodes are at the bottom.
The leaf nodes are the building blocks of the Merkle tree. They are the hashes of every transaction occurring in a given block. You may know them better as transaction IDs (TXIDs) that are viewable via a block explorer.
Non-leaf nodes, forming the second level of a Merkle tree, are bundles of paired leaf node hashes. They derive their name from the fact that they don’t contain TXIDs. Instead, they only store the transaction hashes of the two leaf nodes making them.
Finally, at the tree’s topmost non-leaf node is the Merkle root. It is a single hash representing all the block’s transactions hashes. The Merkle root is the block’s exclusive identifier and is crucial in verifying its authenticity.
How Do Merkle Trees in Blockchain Work?
Here is how Merkle trees in blockchain work:
Step 1: Transaction Hashing
A cryptographic hash function, say SHA-256, hashes all the transactions in the block. This process produces a unique identity (hash) for each, making that data incorruptible.
Step 2: Pairing the Hashes
The same function pairs and hashes two transaction hashes to create a new one. This pairing and hashing repeats at each level, with each child node forming a new parent node.
Step 3: Formation of the Merkle Root
The last two parent (non-leaf) nodes pair up to form a single hash, the Merkle root. This is the entire block’s cryptographic fingerprint and headlines it.
Step 4: Verifying a Block’s Integrity
You can verify the integrity of a given transaction by obtaining its corresponding hash from the Merkle root. Starting there and following the parent nodes, you can recreate the Merkle root. If the two match, then the transactions in the block are authentic.
Let’s consider a simple example with four transactions, A, B, C, and D, occurring in a given block:
- Hashing Transactions: Each transaction is hashed:
- hashA = hash(A)
- hashB = hash(B)
- hashC = hash(C)
- hashD = hash(D)
- Pairing and Hashing: The hashes are paired and hashed together:
- hashAB = hash(hashA + hashB)
- hashCD = hash(hashC + hashD)
- Creating the Merkle Root: The resulting hashes are hashed together to create the Merkle root:
- MerkleRoot = hash(hashAB + hashCD)
Merkle Trees as Proof-of-Reserve
Proof-of-Reserve (PoR) is a bookkeeping practice centralized exchanges (CEXs) and other crypto custodians use to prove their financial health. It’s an open report of the company’s crypto holdings. Calls for PoR audits have grown with the increase in crypto-related fraud cases.
Given that CEXs hold large sums of crypto, Merkle trees are a convenient way of proving their reserves. Here is how they help them achieve that:
- Gathering user balances: The CEX or custodian compiles a list of all users and their balances.
- Hashing user balances: It then uses a cryptographic function to hash each user’s holdings.
- Constructing the Merkle tree: The CEX arranges the hashed user balances in a hierarchical structure. It then hashes these in pairs, forming a Merkle root.
- Publishing the Merkle root: The next step is sharing the Merkle root publicly. This activity enables users to verify their balances without revealing the entire list of their credits.
- Verification: Users can verify their balances by obtaining their Merkle proofs, which are paths from their balance hashes to the Merkle root.
Why Are Merkle Trees Important for Blockchains?
Merkle trees ensure data integrity, efficiency, and scalability within blockchain networks. By hashing individual transactions and combining them into a Merkle root, they create a tamper-proof identifier for each block. So, any correction to a transaction alters the Merkle root, alerting the network of potential tampering.
Moreover, they help in achieving verification efficiency. Users can quickly check a transaction’s probity by comparing its hashes with those in the Merkle tree. Furthermore, they don’t need to download and compare entire blocks.
They also contribute to a blockchain’s scalability by reducing data transmission and verification requirements. This way, they help improve the network’s performance. Besides, they minimize storage needs by storing only the Merkle root and relevant transaction hashes. Thus, they benefit nodes with limited storage capacity.