Summary
What are Merkle Trees?
 Definition: A Merkle tree is a data structure that hashes individual pieces of data and combines them hierarchically to form a single root hash.
 Benefits: Efficient verification, security, and reduced data storage.
 Use Cases: State and transaction verification, reducing data storage, and proof of inclusion in Ethereum.
How Are They Calculated?
 Each piece of data is hashed (generally using Keccak256) 2. Hashes are paired and hashed together until a single root hash is obtained. 3. The hierarchical structure formed is known as a Merkle tree.
Merkle Proofs
 A cryptographic proof that a specific piece of data is part of a Merkle tree.
 Consists of the data, the path to the root hash, and sibling hashes.
 Allows verifying data inclusion without revealing the entire tree.
 Only requires logarithmic space compared to the full tree.
If you’ve dipped your toes into the world of Ethereum dApp development, you’ve likely heard the term “Merkle tree” thrown around. But what exactly is a Merkle tree, and why should you care? Let’s unravel this fascinating concept in a way that’s easy to understand, even if you’re still mastering the basics of Ethereum.
What Are Merkle Trees?
Imagine you have a giant puzzle, and you want to verify if a particular piece belongs to it without assembling the whole puzzle. Merkle trees are like a special type of puzzle where you can verify if a piece belongs to it without seeing the entire picture.
At its core, a Merkle tree is a data structure used in computer science and cryptography. It’s named after Ralph Merkle, who invented it in the late 1970s. In simple terms, a Merkle tree takes a bunch of individual pieces of data, hashes them (which is like giving each piece a unique digital fingerprint), and then combines these hashes in a hierarchical structure until you get a single hash, known as the “root hash.”
How Are They Calculated?
Let’s break down the process of calculating a Merkle tree step by step:

Hashing: Each individual piece of data (let’s call them “leaves”) is hashed. This means each piece gets transformed into a unique string of characters of fixed length.

Pairing and Hashing Again: These hashed pieces are then paired up, and each pair is hashed together. If there’s an odd number of pieces, the last one is hashed with itself. This process continues until there’s only one hash left, the root hash.

Building the Tree: This pairing and hashing process forms a hierarchical structure, resembling a tree, hence the name “Merkle tree.”
Here’s a graphic that might help visualize the process:
Benefits of Merkle Trees
Merkle trees offer several benefits:

Efficient Verification: You can verify whether a particular piece of data is part of the Merkle tree without needing to know the entire dataset. This is incredibly useful for verifying large sets of data efficiently.

Security: Any alteration to the data will change the hash values, making it easy to detect tampering and also where it happened. This property is particularly valuable in blockchain technology, where data integrity is paramount.
What Are They Used For?
In Ethereum, Merkle trees play a crucial role in multiple aspects, including:

State and Transaction Verification: Ethereum uses Merkle trees to efficiently verify the state of accounts and transactions. This helps ensure the integrity of the blockchain without needing to store every transaction’s details onchain. (This is where terms like “block hash” come from — they’re the root hash of the Merkle tree of transactions in a block.)

Reducing Data Storage: By using Merkle trees, Ethereum can store compact proofs of transactions and states, reducing the amount of data that needs to be stored on the blockchain directly.

Proof of Inclusion: When you interact with a decentralized application (dApp) on Ethereum, Merkle trees are often used to prove that your transaction or data is included in the blockchain without revealing unnecessary information.
In essence, Merkle trees are like the secret sauce behind Ethereum’s efficient and secure operation, quietly working behind the scenes to ensure everything runs smoothly.
However, there is one more concept that is closely related to Merkle trees, extensively used in dApp development: Merkle proofs.
Merkle Proofs
A Merkle proof is a cryptographic proof that a specific piece of data is part of a Merkle tree. It consists of the data itself, the path from the data to the root hash, and the sibling hashes along that path. By providing a Merkle proof, you can demonstrate that your data is part of the Merkle tree without revealing the entire tree.
Let’s revisit our puzzle analogy: a Merkle proof is like showing someone a piece of the puzzle, along with the surrounding pieces and the path to the corner of the puzzle. They can verify that your piece fits without needing to see the entire picture. You can easily tell that these pieces connect correctly, even if you don’t know what the whole puzzle looks like.
To explain how this works, let’s take a look at the visual again:
In this example, say that you want to prove that the data leaf L3
is part of the Merkle tree. You would provide either L3
or its hash (here referenced as Hash 10
), and the sibling hashes along the path to the root hash. In this case, we would have the following Merkle proof:
L3
(orHash 10
): The data you want to prove.Hash 11
: The sibling ofHash 10
.Hash 0
: The sibling ofHash 1
. Note that we don’t need to giveHash 1
, because it can be calculated fromHash 10
andHash 11
Also notice that we don’t need to give the Top Hash
because that’s the root hash, which is already known. With this information, anyone can verify that L3
is part of the Merkle tree without needing to see the entire tree.
Obviously, that means the full Merkle tree is still needed to calculate the proof. Generally, the full tree is stored offchain (remember, the whole point is to not need to store all data onchain!), and the proof is calculated when needed. For example, there might be a central server that stores the full tree and provides proofs to users when requested.
An interesting thing about Merkle Proofs is that they are logarithmic in size compared to the size of the Merkle tree. This means that even for a large tree, the proof size is relatively small, making them efficient to use in practice (have you seen Ethereum gas prices lately?!).
How to Use Merkle Them
If you’re building a dApp or working with Ethereum, you might wonder how to use Merkle trees and proofs in your projects. There are a few main libraries to help you work with Merkle trees in Ethereum:
 JavaScript
merkletreejs
: A JavaScript library for creating and verifying Merkle trees.OpenZeppelin/merkletree
: A library from OpenZeppelin for working with Merkle trees in JavaScript, compatible with itsMerkleProof
smart contract.
 Solidity
MerkleProof.sol
: A contract from OpenZeppelin that provides functions for verifying Merkle proofs in Solidity. This is generally used as the defacto standard for Merkle proofs in Ethereum smart contracts.
Wrapping Up
So, there you have it! Merkle trees might seem like a complex topic at first glance, but at their core, they’re a simple yet powerful tool for ensuring data integrity and efficiency, especially in the context of Ethereum and blockchain technology.
Merkle trees are also a gateway to understanding more advanced concepts like Merkle proofs, which are essential for building secure and efficient decentralized applications.
Whether you’re building dApps or simply curious about how Ethereum works under the hood, understanding Merkle trees is a valuable piece of the puzzle.