What is Merkle tree in Blockchain? And how it works

What Is a Merkle tree?

A Merkle tree (also known as a hash tree) is a cryptographic structure used in blockchains, cryptographic protocols, and data verification. It organizes data into a tree-like structure, allowing for efficient and secure verification of large datasets.

Named after Ralph Merkle, who patented the concept in 1979, Merkle trees are a core component of Bitcoin, Ethereum, and other blockchain networks.

Understanding how Merkle tree works

  • Transaction data is hashed: Each transaction in a block is converted into a cryptographic hash (a fixed-length digital fingerprint).
  • Hashes are paired and rehashed: Each pair of hashes is combined and hashed again, creating parent nodes.
  • Process Repeats Until a Single Hash Remains: This final hash, known as the Merkle Root, represents all transactions in the block.

The Merkle Root is stored in the block header, ensuring data integrity without requiring full transaction history verification.

Example of a Merkle tree structure

Suppose a block contains four transactions (A, B, C, D):

  1. Hash each transaction → H(A), H(B), H(C), H(D).
  2. Pair and hash them → H(AB) = H(H(A) + H(B)), H(CD) = H(H(C) + H(D)).
  3. Hash again to get the Merkle root → H(ABCD) = H(H(AB) + H(CD)).

Visual representation

Merkle Root
──────
/ \
H(AB) H(CD)
/ \ / \
H(A) H(B) H(C) H(D)

The Merkle root provides a single reference point for verifying all transactions in a block.

Why Merkle trees are important in blockchain

1. Efficient data verification

  • Instead of downloading all transactions, a node can verify a transaction with just a Merkle proof (a small subset of hashes).
  • This is crucial for lightweight or mobile wallets, which cannot store full blockchain data.

2. Secure data integrity

  • If a single transaction is altered, its hash changes, affecting the entire Merkle tree and Merkle root.
  • This makes tampering easily detectable.

3. Optimized storage

  • Nodes do not need to store all transaction data, reducing memory and processing requirements.

Merkle tree in Bitcoin & Ethereum

FeatureBitcoinEthereum
RoleUsed in block headers for transaction verificationUsed in Patricia Merkle trees for smart contracts & account states
EfficiencyEnables lightweight nodes (SPV wallets)Enhances Ethereum's scalability & data integrity
StructureSimple Merkle TreeModified Merkle Trie

Ethereum extends the Merkle tree concept into a Patricia Merkle trie, optimizing smart contract execution.

Merkle proof: How transactions are verified

A Merkle proof allows users to verify a transaction without downloading an entire blockchain.

Example:

  • A Bitcoin SPV wallet wants to check if transaction H(A) is in a block.
  • Instead of checking all transactions, it only needs:
    • H(B) (its pair)
    • H(CD) (the sibling node above)
    • Merkle Root (from the block header)
  • If these hashes reconstruct the Merkle Root, the transaction is valid.

This makes Merkle Trees critical for lightweight nodes and scalability.

FAQs

Why do blockchains use Merkle Trees?

Merkle Trees allow efficient, secure, and scalable transaction verification without requiring full blockchain data storage.

What happens if one transaction changes?

Any change in a transaction modifies its hash, affecting the entire tree and making tampering easily detectable.

How does a Merkle Tree help with scalability?

It allows partial data verification, reducing the need for full-node storage, making SPV wallets and fast syncing possible.

Other Glossary Terms