Merkle Tree

A Merkle tree hashes pieces of data in pairs, layer by layer, until one root hash is left. Blockchains use it to prove a transaction is in a block without downloading the whole block.

A block can hold thousands of transactions, yet its header has room for just one hash that stands for all of them. That hash is the Merkle root, the top of a Merkle tree. Because any change to any transaction changes the root, a short chain of hashes is enough to prove that one transaction is in a block. The same trick lets light wallets check payments and lets exchanges show customers that their balance was counted in a proof of reserves.

Category
Cryptocurrency and Blockchain
Reading time
6 min
Also called
Hash tree, Merkle root, Merkle proof
Updated

A Merkle tree in plain words

A Merkle tree is built by hashing data in pairs, then hashing those results in pairs again, until a single hash remains.1 That last hash is the Merkle root, and in Bitcoin the data at the bottom are the transactions of one block.1

Each block header stores the Merkle root, so changing any transaction in the block would change the root and break the header.2 See Block.

The Bitcoin whitepaper used a Merkle tree so that old transactions could be pruned while the block's hash stayed valid, and so that light users could check payments with a short Merkle branch.3 The structure is named after Ralph Merkle, whose 1980 paper the whitepaper cites.3

How a Merkle root is built and used, step by step

  1. Hash every transaction

    Each transaction is hashed to give its ID, the txid. These are the leaves of the tree.2

  2. Hash the leaves in pairs

    Two neighbouring txids are joined and hashed together, giving one hash per pair.2

  3. Handle an odd count

    In Bitcoin, if a level has an odd number of hashes, the last one is paired with a copy of itself.2

  4. Repeat until one hash is left

    Each level halves the number of hashes until only the Merkle root remains.1

  5. Put the root in the header

    The block header stores the root, so it commits to every transaction in the block.1

  6. Prove one item with a branch

    To prove a transaction is included, supply the sibling hashes on its path to the root; anyone can rehash them and compare.4

Worked examples

Example 1 — Four transactions

Tree size

A hypothetical block holds 4 transactions: A, B, C and D. How many hashes does the tree have, and how many does a proof for C need?

Example 1 — Four transactions — calculation
Leaves (txids)4
Pair hashes (AB, CD)4 ÷ 2 = 2
Root (ABCD)2 ÷ 2 = 1
Hashes in the tree4 + 2 + 1 = 7
Proof for C2 hashes (D and AB)

The verifier hashes C with D, then that result with AB, and checks it equals the root in the header.

Example 2 — An odd number of transactions

Odd count

A hypothetical block holds 5 transactions. Bitcoin pairs a lone hash with a copy of itself.2

Example 2 — An odd number of transactions — calculation
Level 0 (txids)5
Level 1(5 + 1) ÷ 2 = 3
Level 2(3 + 1) ÷ 2 = 2
Level 3 (root)2 ÷ 2 = 1

Duplicating the last hash keeps every level in pairs, so the tree always ends in a single root.

Example 3 — Headers for a light wallet

Storage

The whitepaper estimated that a header without transactions is about 80 bytes, with a block every 10 minutes.3

Example 3 — Headers for a light wallet — calculation
Blocks per hour6
Bytes per day80 × 6 × 24 = 11,520
Bytes per year11,520 × 365 = 4,204,800
About4.2 MB a year

A light wallet keeps only these headers and asks for Merkle branches, which is why it can run on a phone.3

Merkle proof vs. downloading the block

AspectMerkle proofFull block download
Data neededHeaders plus a short branch of hashesEvery transaction in the block
What it provesThe transaction is in the blockThe whole block is valid
Who uses itLight wallets, proof-of-reserves checksFull nodes
Main weaknessTrusts that the longest chain is honestNeeds more storage and bandwidth

Where Merkle trees are used

  • Block headers

    Bitcoin headers carry a Merkle root that covers every transaction in the block.1

  • Light wallets

    Simplified payment verification checks a transaction with headers and a Merkle branch.1 See Crypto Wallet.

  • Ethereum state

    Ethereum headers hold state, transactions and receipts roots built from a Merkle Patricia trie.4 See Ethereum (ETH).

  • Proof of reserves

    Exchanges put customer balances into a Merkle tree so each customer can check their own entry.5 See Proof of Reserves.

Advantages and disadvantages

Advantages

  • One short hash commits to a whole set of data.
  • A proof grows only by one hash each time the data set doubles.
  • Any change to the data changes the root, so tampering shows.
  • Lets light clients check payments without storing full blocks.

Disadvantages

  • A proof shows inclusion, not that the data itself is valid.
  • Light clients still trust the chain with the most work.
  • Proof of reserves built on it covers balances, not hidden debts.
  • Implementation details, like odd-count rules, differ between chains.

Going deeper

Read as far as you need. Each level builds on the one before it.

A fingerprint of fingerprints

Beginner

A hash is a short fingerprint of some data. A Merkle tree takes fingerprints of fingerprints until one is left at the top.1 See Hash.

Change one transaction and its fingerprint changes, which changes every fingerprint above it, all the way to the root.2

Simplified payment verification

Intermediate

The whitepaper describes how a user can verify a payment without a full node: keep the headers of the longest proof-of-work chain and get the Merkle branch that links the transaction to its block.3

This check is reliable only while honest nodes control the network; an attacker with more hash power could fool a light client for as long as the attack lasts.3 See 51% Attack.

Patricia tries and proof of reserves

Advanced

Ethereum stores key-value data such as account balances in a Merkle Patricia trie, whose root changes whenever any value changes, so a known root lets anyone verify a claimed value with the hashes along its path.4

In Kraken's proof of reserves, an independent accountant builds a Merkle tree of anonymised client balances, and each client recomputes their own leaf and follows the sibling hashes up to the published root.5

Kraken also lists the limits: the check cannot prove exclusive control of keys or rule out borrowed funds and hidden encumbrances.5

Common mistakes

  1. Thinking a proof validates the transaction

    A Merkle branch shows the transaction is in a block; it does not re-check every rule like a full node does.3

  2. Confusing the root with the block hash

    The root covers the transactions; the block hash is the hash of the whole header, which includes the root.

  3. Reading proof of reserves as solvency

    Inclusion in the tree says your balance was counted, not that all liabilities are covered.5

  4. Assuming all chains build trees alike

    Bitcoin pairs transactions in a binary tree, while Ethereum uses a Merkle Patricia trie for its state.4

Check your understanding

Test yourself before moving on. Each answer comes with an explanation.

Q1A Bitcoin block has 8 transactions. How many sibling hashes does a Merkle proof for one transaction need?
Q2What happens to the Merkle root if one transaction in the block changes?

Key points to remember

  1. A Merkle tree hashes data in pairs until one root hash is left.
  2. Each Bitcoin block header stores the Merkle root of its transactions.
  3. A short branch of sibling hashes proves one item is in the set.
  4. Light wallets use headers and Merkle branches instead of full blocks.
  5. Proof-of-reserves checks use Merkle trees, but they do not prove solvency.

Frequently asked questions

What is a Merkle root?

The single hash at the top of a Merkle tree. In Bitcoin it sits in the block header and covers every transaction in the block.1

Why does a light wallet need Merkle proofs?

It does not store full blocks, so it uses headers plus a Merkle branch to confirm a payment was included.3

Who invented the Merkle tree?

It is named after Ralph Merkle; the Bitcoin whitepaper cites his 1980 paper on public-key protocols.3

Further learning

  • Block

    Where the Merkle root sits in a block header.

  • Proof of Reserves

    How exchanges use Merkle trees to show balances.

  • Hash In preparation

    The fingerprint function the tree is built from.

  • Sidechain

    Sidechain pegs rely on SPV proofs built on Merkle branches.

Sources

This article is written in our own words. The sources below support the specific facts, rules and figures marked with a number in the text. Links open the original publisher’s page.

  1. Bitcoin Project (developer.bitcoin.org). Glossary (opens in a new tab) Checked .
  2. Bitcoin Project (developer.bitcoin.org). Block Chain developer guide (opens in a new tab) Checked .
  3. Satoshi Nakamoto. Bitcoin: A Peer-to-Peer Electronic Cash System (opens in a new tab) Checked .
  4. ethereum.org. Merkle Patricia Trie (opens in a new tab) Checked .
  5. Kraken. Proof of Reserves (opens in a new tab) Checked .

Educational content, not financial advice. Examples use hypothetical prices and simplified costs; actual fees, margin rules and regulations vary by broker, market and country. We are not affiliated with any publisher linked above. Last reviewed . Report an error or request a source credit.