Karya Semi
HomeBlogSearchCategoriesAboutContact
Karya Semi

Less noise. More notes.

HomeBlogAboutContactPrivacy PolicyDisclaimer

© 2026 Karya Semi. All rights reserved.

XGitHubLinkedIn
  1. Home
  2. /Categories
  3. /Web3

Merkle Trees Explained for Blockchain Developers

A practical guide to Merkle trees for blockchain developers. How they work, why they matter, and where they're used beyond crypto.

Dian Rijal Asyrof/August 12, 2026/6 min read
Illustration for Merkle Trees Explained for Blockchain Developers

If you've ever looked at a Bitcoin block explorer and wondered what that "merkle root" field actually does, you're not alone. Merkle trees are one of those concepts that sound academic until you realize they're the reason your wallet can verify a transaction without downloading 500GB of blockchain data.

Here's how they work, why they matter, and where they show up in places you probably didn't expect.

What Is a Merkle Tree?

A Merkle tree (also called a hash tree) is a binary tree structure where every leaf node contains the hash of a data block, and every non-leaf node contains the hash of its children's hashes. The top node, the root, represents a cryptographic fingerprint of all the data below it.

Let's make it concrete. Say you have four transactions: TX_A, TX_B, TX_C, TX_D.

First, hash each one:

  • hash(TX_A) → H_A
  • hash(TX_B) → H_B
  • hash(TX_C) → H_C
  • hash(TX_D) → H_D

Then pair them up and hash the pairs:

  • hash(H_A + H_B) → H_AB
  • hash(H_C + H_D) → H_CD

Then hash those two together:

  • hash(H_AB + H_CD) → H_root

That H_root is your Merkle root. One single hash that represents all four transactions. Change any single byte in any transaction, and the root changes completely.

The tree structure is what makes this interesting, not just the hashing.

Why Not Just Hash Everything Together?

You could concatenate all your transactions and hash the result. You'd get a single fingerprint too. But here's the problem: if you want to verify that one specific transaction is part of that data, you'd need the entire dataset to recompute the hash.

Merkle trees solve this with something called a Merkle proof. To prove TX_B is in the tree, you only need three hashes: H_A, H_CD, and the known root. You hash H_A with your own H_B to get H_AB, then hash that with H_CD to get a new root. Compare it against the known root. If they match, TX_B is verified.

That's O(log n) data instead of O(n). For a block with 2,000 transactions, you need roughly 11 hashes instead of 2,000. For a blockchain with millions of transactions, the savings are massive.

How Ethereum Uses Merkle Trees

Bitcoin uses a single Merkle tree per block for transaction verification. Pretty straightforward.

Ethereum went further. It actually uses three separate Merkle trees per block:

  1. State Trie - all account balances, nonces, code hashes. Updated with every single transaction.
  2. Transactions Trie - the ordered list of transactions in the block.
  3. Receipts Trie - logs and execution results for each transaction.

These aren't simple binary Merkle trees either. Ethereum uses Merkle Patricia Tries (MPT) - a combination of a Merkle tree and a Patricia trie (a compressed prefix tree). This lets Ethereum prove things like "what is the balance of address 0xABC?" without revealing the entire state.

This is how light clients work. Your phone running a wallet doesn't download the full Ethereum state (which sits at 100GB+). It requests a Merkle proof from a full node, verifies it against the block header's state root, and trusts the result. The math checks out. No trust required.

Why does this matter practically? Because without MPT, every Ethereum wallet would need to be a full node. Mobile wallets, browser extensions, hardware devices - they'd all be impossible at current chain sizes. For developers focused on usability and cost, understanding these layers is key, especially when exploring architectures like Ethereum Layer 2 rollups explained for developers.

Merkle Proofs in Practice

Let's walk through a real verification flow. You're building a DApp and you want to prove a user's token balance without trusting a centralized API.

  1. Get the block header, which contains the state root hash.
  2. Request the Merkle proof from a node. This is a list of sibling hashes along the path from the account's leaf to the root.
  3. Starting from the account data (balance, nonce, etc.), hash it to get the leaf hash.
  4. Walk up the proof, combining your computed hash with each sibling hash.
  5. If your final computed root matches the state root in the block header, the proof is valid.

The account data is authentic. Nobody could have faked it without changing the state root, which would mean changing the block header, which would mean breaking the entire chain's cryptographic guarantees.

That's the core trust model of blockchain. And Merkle trees make it efficient enough to actually work. Verifying that the transaction you're about to sign is what you think it is is a critical security step, which is why concepts like Ethereum clear signing transactions are so important for safer approvals.

Beyond Crypto: Where Else Merkle Trees Show Up

Merkle trees weren't invented for blockchain. Ralph Merkle patented the concept in 1979. They show up in a bunch of systems you probably already use.

Git. Every Git commit has a tree object structured as a Merkle tree. Each file's content is hashed, those hashes form leaves, and the root hash is what gets signed in the commit. If someone tampers with any file in any commit, the root hash changes and the entire history breaks. That's how git verify-commit works. Merkle verification all the way down.

IPFS. Files in IPFS are split into 256KB chunks, each chunk gets hashed, and the hashes form a Merkle DAG (Directed Acyclic Graph). The root hash becomes the file's Content Identifier (CID). Two files with identical content always produce the same CID, enabling deduplication across the entire network.

Certificate Transparency. Google's Certificate Transparency logs use Merkle trees to audit TLS certificates. When a Certificate Authority issues a cert, it gets appended to a public log. The log's Merkle root is periodically published, and anyone can verify that a specific certificate was included. This is how you catch malicious or misissued certificates.

Amazon DynamoDB. Uses Merkle trees for anti-entropy - the process of detecting inconsistencies between replicas. Each node maintains a Merkle tree of its data ranges. Two replicas compare roots. If they differ, they walk the tree to find exactly which key ranges are out of sync, instead of comparing entire datasets.

ZFS. The file system uses Merkle trees to verify data integrity. Every block is hashed, and parent blocks store their children's hashes. On read, ZFS recomputes and compares. Silent data corruption, which plagues traditional RAID arrays, gets caught immediately.

Building a Basic Merkle Tree

If you want to implement one yourself (in JavaScript, since most blockchain devs live there), the core logic is maybe 30 lines:

const { keccak256 } = require('ethereumjs-util');
 
function buildMerkleTree(leaves) {
  if (leaves.length === 0) return Buffer.alloc(32);
  
  let currentLevel = leaves.map(l => keccak256(l));
  
  while (currentLevel.length > 1) {
    const nextLevel = [];
    for (let i = 0; i < currentLevel.length; i += 2) {
      const left = currentLevel[i];
      const right = currentLevel[i + 1] || left; // duplicate if odd
      nextLevel.push(keccak256(Buffer.concat([left, right])));
    }
    currentLevel = nextLevel;
  }
  
  return currentLevel[0]; // root
}

Notice the odd-node handling. When you have an odd number of nodes at any level, you duplicate the last one. This is standard behavior. Bitcoin and Ethereum both do it. It's a subtle detail that trips up a lot of first-time implementations.

For a Merkle proof, you need to track which sibling (left or right) you paired with at each level. Store the proof as an array of { hash, position } objects, and verification walks the path back up.

The Gotchas

A few things catch people off guard.

Second preimage attacks. If your leaf nodes and internal nodes use the same hash function, an attacker can craft a fake internal node that looks like a leaf. The fix: prefix leaf hashes with 0x00 and internal hashes with 0x01 before hashing. Bitcoin doesn't do this (it hashes leaves twice), but prefixing is considered best practice for new implementations.

Empty trees. What happens when you have zero leaves? Some implementations return a zero hash, others throw. Define this behavior explicitly in your code. Ambiguity here leads to consensus bugs.

Sorted Merkle trees. Some protocols sort leaves before building the tree. This makes proofs position-independent but breaks ordering guarantees. Decide early whether order matters for your use case.

Why You Should Care

If you're building anything on a blockchain - smart contracts, L2 rollups, bridges, light clients - you'll eventually need to generate or verify Merkle proofs. They're the underlying mechanism for:

  • Airdrop eligibility verification (Merkle distribution contracts)
  • L2 state roots posted to L1
  • Cross-chain message verification
  • NFT whitelist proofs
  • Governance snapshot proofs

Understanding the tree structure isn't just academic. It directly affects gas costs (proof depth = verification cost), security (wrong hash prefix = exploitable tree), and architecture decisions (single tree vs. multiple tries). These architectural choices are especially relevant when considering the design of systems like ERC-7579 smart accounts for Ethereum developers.

Merkle trees are one of those rare data structures that are both elegant and incredibly practical. They've been around for 45 years, and they're more relevant today than Merkle ever probably imagined. If you're working in the blockchain space, they're not optional knowledge. They're foundational.

DR

Dian Rijal Asyrof

Writes about useful AI tools, programming practice, and the craft of building reliable software.

Previous articleGo Language AI-Assisted Software Engineering in 2026Next articleRedis Pub/Sub vs Streams: Choosing the Right Event-Driven Pattern
Web3BlockchainMerkle TreesCryptographyData Structures
On this page↓
  1. What Is a Merkle Tree?
  2. Why Not Just Hash Everything Together?
  3. How Ethereum Uses Merkle Trees
  4. Merkle Proofs in Practice
  5. Beyond Crypto: Where Else Merkle Trees Show Up
  6. Building a Basic Merkle Tree
  7. The Gotchas
  8. Why You Should Care

On this page

  1. What Is a Merkle Tree?
  2. Why Not Just Hash Everything Together?
  3. How Ethereum Uses Merkle Trees
  4. Merkle Proofs in Practice
  5. Beyond Crypto: Where Else Merkle Trees Show Up
  6. Building a Basic Merkle Tree
  7. The Gotchas
  8. Why You Should Care

See also

Illustration for Ethereum's Glamsterdam Hard Fork Enters Final Testing Phase
Web3/Aug 1, 2026

Ethereum's Glamsterdam Hard Fork Enters Final Testing Phase

Ethereum developers kick off devnet-5 for the Glamsterdam hard fork, introducing EIP-7954 to expand contract code limits and EIP-7702 for authorization signing.

3 min read
Web3Cryptography
Illustration for Solana Just Got Serious About On-Chain Governance
Web3/Jul 3, 2026

Solana Just Got Serious About On-Chain Governance

The Solana Foundation launched a formal protocol-level governance framework. Here is why it matters for DeFi builders, validators, and anyone building on Solana.

3 min read
SolanaWeb3
Illustration for Designing a Micro-Payment Pipeline for AI Agents Using USDT
Web3/Aug 10, 2026

Designing a Micro-Payment Pipeline for AI Agents Using USDT

Deploy a secure usdt micropayments ai agents automated worker payout architecture. Scale your autonomous workflows with low-fee stablecoin smart contracts.

6 min read
Web3Usdt