[root@localhost /]# cyb3r.sh ▉

Merkle Trees, CT and MTCs: Part 1 - Merkle Trees

Brandon James

2026-11-11

Merkle Trees are a simple, elegant and useful data structure when you need to validate that something is part of an underlying set of data, while minimizing the amount of data you need to verify when proving inclusion.

Building Merkle Trees

This series covers the append only binary tree variant of Merkle Trees as used by certificate transparency with the goal of building enough intuition to understand both certificate transparency and Merkle Tree Certificates (MTC). Other variants of Merkle Trees are used by systems like the bitcoin ledger and the ZFS file system. For a more academic introduction, see The Handbook of Applied Cryptography chapter 13.4.1 (this happens to be one of the sample chapters!).

I’ve purposely chosen a non-cryptographic hash function (crc32) in the above example in order to make the tree fit in a smaller space and make it easy to follow along. CRC32 cannot protect integrity against deliberate manipulation and must not be used for cryptographic purposes. If you’d like to build the tree by hand, you can use this bash function to compute the hashes. This is portable-ish. My Fedora 44 system shipped with GNU cksum, your box may not.

_crc_raw() {
    cksum -a crc32b | { read -r sum _; printf '%08x\n' "$sum"; }
}

crc() {
    if [ "$1" = -x ]; then
        shift
        local h
        if [ $# -gt 0 ]; then h=$(printf '%s' "$@"); else h=$(cat); fi
        h=${h//[[:space:]]/}
        if [[ ! $h =~ ^([0-9a-fA-F]{2})*$ ]]; then
            echo "crc: -x needs an even number of hex digits" >&2
            return 1
        fi
        printf '%b' "$(sed 's/../\\x&/g' <<<"$h")" | _crc_raw
    else
        if [ $# -gt 0 ]; then printf '%s' "$@"; else cat; fi | _crc_raw
    fi
}

Once you’ve pasted it into your shell it works as follows:

To hash a string crc foo yields 8c736521. To hash hex formatted bytes crc -x 8c736521, which yields 849f8f2a. You can pass in multiple values for instance crc -x 8c736521 76ff8caa, which concatenates the bytes prior to hashing, this yields 49574b0b. So while following along, hash input data without the -x, hash sibling nodes to generate parent nodes with the -x.

A few notes about the construction of this tree:

Proof of Inclusion

Proving an object is included in a Merkle Tree requires relatively little data. If we have an object, we need the hash of its sibling, the hash of its parent’s sibling and so on, up to the root. This means we need at most h = ceiling(log_2(n)) hashes, along with left/right ordering to recompute a root hash for comparison against an authenticated root.

Proof of Consistency

When dealing with Merkle Trees it is useful to be able to validate that the current state of a tree is consistent with the previous state of the tree. This can be done very efficiently. Given the previous root hash and tree size (defined as the number of leaves) the consistency proof is at most ceiling(log_2n) + 1 hashes (where n is the number of leaves), but in many circumstances it’s less than that. Given a previous tree size of m, a current tree size of n and k the largest power of two smaller than n we have three scenarios to consider.1

In all cases verification must establish agreement with the stored old root and the new root.

  1. m = k in this scenario, the previous tree consists of the complete subtree of the left child of the new root. Validating consistency only requires the new right child and we can confirm the new root by calculating H(stored root || provided right hash).
  2. m > k the old tree already had the complete left child of the new tree and a prefix of the new right subtree. We are provided the intermediate hashes of the right tree and the root hash of the left subtree, then we can recursively validate that a) our old root can be calculated from the left subtree root and the provided right intermediate hashes and b) that the new root hash can be calculated from the left subtree root and the right intermediate hashes. Then we store the new root hash and tree size.
  3. m < k the old tree is entirely contained in the left child of the new tree. We are provided with intermediate hashes for both the left subtree and root of the right subtree. We recursively verify the left subtree and ensure that we can recalculate our old root hash, then we calculate the new root from the new left and right child hashes and finally store the new root hash and size.

An inclusion proof tells us an object belongs to a tree represented by a root hash. A consistency proof tells us the older tree’s entries remain an unchanged prefix of the newer tree. Authentication of that root and integrity of an overall tree are different problems. In the next article we’ll look at how Certificate Transparency addresses those problems.


  1. For all 0 < m < n↩︎