Web3 Learning

What Is a Merkle Tree?

By Safeheron Team

What is a Merkle tree? It is a tree structure that summarizes data layer by layer using hashes: the bottom layer holds the hash of each piece of data, these are combined in pairs and hashed again, and the process repeats until only one value remains, called the Merkle root. If any single piece of data changes, the Merkle root changes too, so one short root can represent a large batch of data. Bitcoin uses it to summarize the transactions in a block into the block header, and exchanges use it to summarize customer balances in proof of reserves.

How Is a Merkle Tree Built?

Take four pieces of data, A, B, C and D. First, compute the hash of each one to get four “leaves.” Then concatenate the hashes of A and B and hash the result to get AB, and do the same with C and D to get CD. Finally, concatenate AB and CD and hash them to get the Merkle root. With more data, the method is the same; there are just more layers.

The key to this structure is a property of hash functions: even the smallest change to the input produces a completely different output. So once A is altered, its leaf, AB and the Merkle root all change, making tampering easy to detect. The security of public blockchains such as Bitcoin and Ethereum rests on cryptographic foundations such as hashing and elliptic curves, and a Merkle tree is one way of organizing hashes.

What Is a Merkle Proof?

A Merkle proof shows that a particular piece of data really is in the tree, without needing all the data. Using the same example, to prove that A is in the tree, you only need to provide the hash of B and CD: the verifier computes the hash of A, combines it with the hash of B to get AB, then combines that with CD to get the root. If the result matches the published Merkle root, A was indeed included.

The hashes needed along the way are called the “Merkle path” or “Merkle branch,” and its length depends only on the number of layers in the tree: doubling the data adds just one layer. And because the verifier only sees a string of hashes, not the original contents of B, C and D, it both saves resources and protects the privacy of the other data.

How Are Merkle Trees Used in Blockchains?

The classic example is Bitcoin. The Bitcoin white paper states that the transactions in a block are hashed into a Merkle tree, with only the Merkle root included in the block’s hash. This brings two benefits. The first is saving space: once old spent transactions are buried under enough blocks, they can be discarded without breaking the block’s hash; the white paper estimates that a block header with no transactions is only about 80 bytes. The second is lightweight verification, which the white paper calls simplified payment verification (SPV): users do not need to run a full node, and only need to keep the block headers of the longest chain and obtain the Merkle branch linking a transaction to the block it is in, to see that the network has accepted that transaction.

Ethereum’s execution layer uses a variant of the Merkle tree called the Merkle Patricia Trie. According to Ethereum’s official developer documentation, the block header records the roots of three separate trees for state, transactions and receipts (stateRoot, transactionsRoot, receiptsRoot). Whatever the form, the idea is the same: use one short root to represent a large amount of data, then use a Merkle proof to verify any single item on its own.

How Do Merkle Trees Relate to Proof of Reserves?

When exchanges carry out proof of reserves, they use a Merkle tree to aggregate customer balances. Taking Kraken’s published approach as an example, a third-party accountant places customer balances at the time of the snapshot into a Merkle tree anonymously, and each customer can run the calculation with their own record and Merkle path to confirm that their balance was included in the total, without seeing anyone else’s balance.

However, a Merkle tree answers only one question: was my balance counted? Whether the on-chain assets are sufficient, whether liabilities are complete and what happened after the snapshot all depend on the other parts of proof of reserves and on external review.

Conclusion

A Merkle tree uses hashes to summarize large amounts of data layer by layer into a single Merkle root, where any change is reflected, and a Merkle proof lets anyone verify a single item with only a few hashes. Bitcoin uses it to organize the transactions in a block and support lightweight verification, and exchanges use it to check customer balances in proof of reserves. Safeheron’s MPC Wallet-as-a-Service provides businesses with MPC key management and a policy engine.

Does your business need to prove the security and transparency of its assets to customers?

Contact Safeheron’s Sales Team to Book a 1:1 Demo

Share

Looking for a secure way to manage digital assets?

Talk to our experts and find the right solution for your business.

Request a Demo