Introduction

Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.

I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.

I also build and publish a variety of tools, libraries, and open-source projects.

This article introduces Merkle trees, a data structure used in blockchain and cryptocurrency systems.

The illustrations are intended for readers interested in blockchain, cryptocurrencies, or algorithms who want to understand how Merkle trees work. The original figures are retained and may contain Japanese labels.

https://chaldene.net/python-book

What Is a Merkle Tree?

We begin with the structure of a Merkle tree and the terminology needed to understand it.

Overview

A data structure for efficiently verifying data integrity.

That is the basic idea behind a Merkle tree.

Suppose we have multiple pieces of data and want to verify them. Checking each piece individually is possible, but can take considerable time. A Merkle tree supports efficient verification using a tree structure.

As its name suggests, a typical Merkle tree is organized as a binary tree, like the following illustration.

A binary tree

We can adapt this illustration to show how a Merkle tree works. Because the purpose is verification, it is helpful to show the computation flowing from the bottom toward the top rather than in the opposite direction.

A binary tree with bottom-up computation

The elements are combined from the bottom up until a single value remains. In the illustration, the value at node A serves as the verification value.

The next illustration makes the combination process more concrete.

Combining the values of the leaf nodes

The elements are combined repeatedly, yielding the illustrative value ABCDEFGH at the root.

In an actual Merkle tree, these values are hashes, not simply concatenated letters. For an introduction to hashing, see the following article.

https://chaldene.net/hash

The letters above are used only to make the computation easier to follow. A more realistic representation looks like this.

Combining pairs of hashes to produce a Merkle root

At each level, two hash values are combined and hashed again. The process repeats until one value remains.

A fixed-output hash function returns a value of the same length regardless of the input length. As shown in the illustration, all the hashes have the same size. Consequently, the root has a fixed length regardless of the size or number of inputs.

This raises another question.

What if there is an odd number of data items?

In the illustrated construction, each parent is formed from a pair of child nodes. An odd number of nodes leaves one node without a partner.

An odd number of leaves

One common convention is to duplicate the final node.

Duplicating the final node to complete the pair

In this example, the last element, G, is duplicated and the pair is combined. The resulting hash is used for verification under this construction's agreed-upon duplication rule.

Further Reading

The remainder of this explanation is available in the following original Japanese article, which covers Merkle trees in more detail.

https://chaldene.net/markle-tree