arthrop0d

Brief articles on varied topics.

Ethereum’s Merkle Patricia Trie

cryptocurrencycryptography

Ethereum’s Merkle Patricia trie is a tree that stores Ethereum’s key-value state while reducing the entire tree to one cryptographic commitment: the root hash. Given that root, another party can prove that a particular account or storage value exists—or does not exist—without sending the whole state.

It combines three ideas:

Ethereum uses this structure for its world state and also for related collections such as transactions and receipts. This article focuses on account and contract storage state.

The problem: committing to a large state

Ethereum state is conceptually a map. An account address maps to an account record, and a contract’s storage maps slots to values. A node may hold millions of such entries.

A plain database can answer lookups efficiently, but it does not give a compact, independently checkable statement about the database’s contents. If a node says, “Address X has this balance,” a verifier needs some way to check that claim against the state selected by a block—without trusting the node or downloading every other account.

A Merkle structure solves this by hashing data upward. Change one value, and the hashes on the path above it change, producing a new root. The block header stores the root, called stateRoot, so the root commits to the whole account state.

Keys become paths of nibbles

A nibble is four bits, or one hexadecimal digit. A 256-bit hash therefore contains 64 nibbles. The trie uses those nibbles as path components: the first nibble chooses the first branch, the second chooses the next, and so on.

Ethereum uses a secure form of the trie. Rather than using an account address directly as the path, it generally uses the Keccak-256 hash of the address. For contract storage, it uses the Keccak-256 hash of the 32-byte storage slot key. This makes paths uniformly distributed and avoids allowing specially shaped keys to create inefficient tree structures.

The hashed key is an important distinction: the trie’s path is not normally the readable address or the raw Solidity slot number. The value stored at the end still contains the account or storage data associated with that key.

The node types

Ethereum’s trie is hexary, meaning each ordinary branch can select one of 16 hexadecimal digits. It has three practical node forms:

The path in an extension or leaf is stored using hex-prefix encoding, a compact format that records both the nibbles and whether the node is a leaf or an extension. It also records whether the path has an odd or even number of nibbles, because paths are being packed into bytes.

Node contents are serialized with Recursive Length Prefix (RLP), Ethereum’s encoding for nested byte strings and lists. The exact byte encoding matters: nodes are hashed after their canonical RLP representation is produced.

How hashes bind the tree together

A node’s reference is either the node itself, when its encoded form is small enough, or the Keccak-256 hash of its encoded form. In the common case, a parent stores a 32-byte hash pointing to a child. This rule keeps small nodes compact while allowing large nodes to be addressed by hash.

The root is calculated from the bottom upward:

  1. Encode a leaf or branch node with RLP.
  2. Hash it when a hash reference is required.
  3. Place that reference in its parent.
  4. Continue until the root node produces the final root hash.

Because every ancestor depends on its descendants, changing an account balance changes that account’s leaf, then its parents, and finally stateRoot. A node cannot alter a value while keeping the same valid root unless it can defeat the hash function.

An account value commonly contains the account nonce, balance, code hash, and storageRoot. The storageRoot is itself the root of another Merkle Patricia trie containing that contract’s nonzero storage values. This nesting lets the account trie commit to contract storage without placing every storage slot directly in the top-level tree.

Proofs without downloading the state

A Merkle proof is the set of encoded nodes needed to walk from a known root to a target value. To prove an account:

  1. The verifier starts with the trusted stateRoot from a block header.
  2. It checks the hash of the supplied root node.
  3. It follows the nibble path for the hashed address.
  4. At each branch, it selects the child corresponding to the next nibble.
  5. It verifies each child node’s hash and checks the final value.

The proof includes the nodes along that route, not the unrelated subtrees. A branch node may contain references to many other children, but the verifier does not need to retrieve those children merely to verify the selected path. The proof is therefore proportional mainly to the path and the nodes on it, rather than to the size of the entire state.

The same process can prove non-membership. For example, a proof may reach a branch whose relevant child is empty, or a leaf whose remaining path does not match the requested key. The verifier can then conclude that the key is absent under that root.

To prove a contract storage value, a verifier first proves the account and obtains its storageRoot, then verifies a second path through that storage trie. This is why clients, light clients, and state-query tools sometimes report proof nodes or errors involving a “missing trie node”: the requested path cannot be reconstructed from the local node database.

Updates preserve old roots

An update does not require rewriting every node. The implementation follows the old path, changes or creates the affected leaf, and rebuilds only the nodes from that leaf back to the root. Unchanged subtrees remain represented by their existing hashes.

This is a form of path copying: the new version shares untouched structure with the old version, while the old root still identifies the previous state. Once the rebuilt root is recorded in a block header, it becomes the commitment for that block’s state.

The result is more than a database index. The Merkle Patricia trie gives Ethereum a compact, hash-addressed representation of state, an independently verifiable root, proofs for individual entries, and updates whose cost follows the changed paths rather than the full world state.

← All articles