Back

Blog details

Merkle DAG vs Merkle Tree: What IPFS Changes and Why

AIOZ Network
8 min readNovember 30, 2026
aioz-pin
AIOZ Pin cover image reading Merkle DAG vs Merkle Tree

In a Merkle DAG vs Merkle tree comparison, the main difference is sharing. A Merkle tree gives each node at most one parent and keeps data in the leaves. A Merkle DAG lets many parents link one node, and any node can carry data. IPFS uses the DAG form.

TL;DR:

  • Bitcoin and Certificate Transparency build binary Merkle trees, where a proof is one sibling hash per level.
  • A Merkle DAG drops the one-parent and balance rules, so a repeated block is stored once and linked many times.
  • IPFS links nodes by CID, and a UnixFS file root carries data of its own, such as the size of each child.
  • An IPFS proof is the chain of parent blocks from the root to your block, which a trustless gateway sends as a CAR.
  • Our standard-library script builds both over one 4 MiB file, and its DAG root matches a real UnixFS importer.

What is the difference between a Merkle tree and a Merkle DAG?

A Merkle tree is a hash tree: each leaf is the hash of a piece of data, and each inner node is the hash of its children. Ralph Merkle's patent on the idea was filed in 1979, as part of a method for digital signatures. A Merkle DAG keeps the hashing rule and drops the shape rules.

The IPFS docs put it in one line: Merkle DAGs are similar to Merkle trees, "but there are no balance requirements, and every node can carry a payload." Branches in a DAG can also re-converge, so one node can have several parents.

PropertyBinary Merkle tree (Bitcoin, CT)Merkle DAG (IPFS)
Parents per nodeAt most oneAny number
Children per nodeUp to twoAny number (Kubo default: up to 174 per file node)
Where data livesLeaves onlyAny node; a UnixFS root stores file size and block sizes
Node nameA bare hashA CID: version, codec and multihash
Repeated dataHashed again at every positionStored once, linked many times
Inclusion proofOne sibling hash per levelEvery parent block on the path
ShapeFixed by the leaf countSet by the importer: chunker, width and layout
Labelled diagram of the same four 1 MiB chunks a, b, a, a in two structures. Left, a binary Merkle tree per RFC 9162 with leaf hashes 28a56ef5, 1efd8985, 28a56ef5, 28a56ef5, inner nodes 2218a3be and 36f2c79b and root 6ace90e6; the proof for chunk 2 is 2 hashes, 64 bytes. Right, a UnixFS Merkle DAG with a 209 byte dag-pb root whose links 0, 2 and 3 point to one raw block and link 1 to another; the proof for chunk 2 is the root block, 209 bytes

In the diagram, the tree hashes chunk a in three positions, while the DAG stores it once and links it three times.

How does a binary Merkle tree work?

Bitcoin is the textbook case. A block header holds a merkle root built from the block's transaction IDs, paired and hashed with double SHA-256 row by row, and an odd last hash is paired with a copy of itself. The Bitcoin paper uses the tree so a light client can check a payment with block headers and one Merkle branch.

Certificate Transparency logs, specified in RFC 9162, add one rule. A leaf hash is SHA-256 over a 0x00 byte plus the entry, and a node hash is SHA-256 over a 0x01 byte plus its two children. The RFC says this domain separation "is required to give second preimage resistance", so an inner node can never pass as a leaf. The leaf count doesn't have to be a power of two, and the number of leaves alone fixes the shape.

That fixed shape keeps proofs small. To show that one entry is in the log, you send the sibling hash at each level, so a tree with 1,024 leaves needs 10 hashes, or 320 bytes with SHA-256.

What can a Merkle DAG do that a tree cannot?

A DAG lets one node have many parents, and that is what makes sharing possible. When two folders contain the same file, both folder nodes link the same file CID, so you store its blocks once. When you edit one file in a large folder, the new folder root still links every unchanged file from the old version. Our guide to how UnixFS represents files and directories shows those directory nodes.

Git follows the same pattern under a confusing name. It calls a directory listing a "tree object", yet its store is a DAG, with each object named by the SHA-1 of a header plus its content. In the Pro Git book, a new tree includes an earlier tree as a subdirectory named bak, so that earlier tree ends up with two parents.

Any DAG node can also carry data. A UnixFS file root stores its total size and each child's byte size, so a reader can find the blocks for a byte range. Links are full CIDs, so one DAG can mix raw leaves (0x55) under dag-pb parents (0x70). Our pillar post on how IPFS turns files into Merkle DAGs follows those blocks through the network.

Build both structures over the same chunks

The script below needs only the Python standard library. It cuts a 4 MiB file into four 1 MiB chunks, where chunks 0, 2 and 3 are identical. It then computes the RFC 9162 tree hash and a UnixFS dag-pb file root that links the chunks as raw leaves, the layout the unixfs-v1-2025 profile produces for this file.

import base64, hashlib

CHUNK = 1024 * 1024                      # 1 MiB, as in the unixfs-v1-2025 profile
A, B = b"a" * CHUNK, b"b" * CHUNK
data = A + B + A + A                     # 4 MiB file: chunks 0, 2 and 3 are identical
open("demo.bin", "wb").write(data)        # the same bytes, for ipfs add
chunks = [data[i:i + CHUNK] for i in range(0, len(data), CHUNK)]
sha = lambda b: hashlib.sha256(b).digest()

# 1. Binary Merkle tree, the Merkle Tree Hash of RFC 9162 section 2.1.1
def mth(items):
    if len(items) == 1:
        return sha(b"\x00" + items[0])                    # leaf hash
    k = 1 << ((len(items) - 1).bit_length() - 1)          # largest power of 2 below n
    return sha(b"\x01" + mth(items[:k]) + mth(items[k:]))  # node hash

# 2. Merkle DAG: a UnixFS file root (dag-pb) linking raw leaves, CIDv1
def varint(n):
    out = b""
    while n > 0x7F:
        out += bytes([n & 0x7F | 0x80])
        n >>= 7
    return out + bytes([n])

def field(tag, payload):                 # one length-delimited protobuf field
    return bytes([tag]) + varint(len(payload)) + payload

def cid(codec, block):                   # version 1, codec, sha2-256 multihash
    raw = bytes([0x01, codec, 0x12, 0x20]) + sha(block)
    return raw, "b" + base64.b32encode(raw).decode().lower().rstrip("=")

blocks, links = {}, b""
for c in chunks:
    raw, text = cid(0x55, c)             # 0x55 = raw codec
    blocks[text] = c
    links += field(0x12, field(0x0A, raw) + field(0x12, b"") + b"\x18" + varint(len(c)))
unixfs = b"\x08\x02\x18" + varint(len(data)) + b"".join(b"\x20" + varint(len(c)) for c in chunks)
root = links + field(0x0A, unixfs)       # Links first, then Data
root_cid = cid(0x70, root)[1]            # 0x70 = dag-pb codec
blocks[root_cid] = root

print("tree root    ", mth(chunks).hex())
print("DAG root CID ", root_cid)
print("DAG blocks   ", len(blocks), "unique,", len(root), "bytes in the root block")

We ran it with Python 3.12.3 and got this output:

tree root     6ace90e6a7a2d31fa5eb770849ff783c0ecbe02fe8d79c22becfde6d0d5b74fb
DAG root CID  bafybeihmgvufc2kbdvyi6n3evv6o2cbceupsm32emlqixc6pewh4a5ljpu
DAG blocks    3 unique, 209 bytes in the root block

The DAG holds 3 blocks, because the identical chunks share one CID. To check that the root is real UnixFS, we imported the same bytes with ipfs-unixfs-importer 17.1.1, the importer behind Helia's UnixFS module, set to the same profile. It returned the same root CID and 3 blocks. The tree function also matched all 8 root hashes in the transparency-dev/merkle test vectors.

Change chunk 1 from b to c, and the tree root changes to one starting 7568a714 while the DAG root becomes bafybeiffxov7q54xxe6yhiseel5g533izgen5azzhjsmypftf6aihdshzq. Both structures catch the edit, and they differ only in what you store and what you send as proof.

With Kubo, you can add the demo.bin file the script writes using the commands below, quoted from the Kubo config docs and not run here (this machine has no IPFS binary). IPIP-0499 says the same input and profile yield the same CID in every implementation.

ipfs config profile apply unixfs-v1-2025
ipfs add demo.bin

Chunking and DAG layout decide the shape

A Merkle DAG's shape depends on choices the importer makes. The chunker sets the leaves: Kubo's default cuts 256 KiB blocks, and our post on fixed vs content-defined chunking compares the options. The width caps the links per node before the importer adds a level, 174 by default in Kubo and 1,024 in unixfs-v1-2025.

IPIP-0499 says the wider profile gives a 1 TiB file 3 levels instead of 4, with about four times fewer nodes. The layout then places the leaves: both profiles use a balanced layout, where every leaf sits the same number of hops from the root. Kubo's docs describe the other option, trickle, as optimized for streaming. See why the same file used to get different CIDs for how these settings move a root CID.

Proving a block belongs to a file

In a binary tree you send sibling hashes, while in IPFS you send the parent blocks themselves, because each parent's CID covers its whole list of links. In our script the proof for chunk 2 is the 209-byte root block, against 64 bytes for the tree's two sibling hashes.

The gap grows with width. We computed a full unixfs-v1-2025 node with 1,024 raw 1 MiB leaves at 51,211 bytes, which is the root of any 1 GiB file in that profile. Proving one chunk then costs about 50 KB, where a binary tree over 1,024 chunks needs 320 bytes.

The Trustless Gateway spec makes this work over HTTP. A CAR request with dag-scope=block returns the blocks that verify each path segment plus the final block, and entity-bytes returns the minimal set of blocks that verify a byte range. Those responses use the CAR file format for whole DAGs.

How AIOZ Pin handles it

AIOZ Pin pins IPFS content, so these DAG rules apply to every file you pin with us. When you upload with POST https://api.aiozpin.network/api/pinning, the response includes the cid of your file's root, and the options object accepts a cid_version. If you already have a root CID, pinByHash takes it as hash_to_pin, and that one string covers every block under it.

What the docs don't cover yet:

  • the chunk size, link width and DAG layout the upload endpoint uses;
  • whether uploads store leaves as raw blocks.

So compare the cid in the upload response with any CID you computed locally before you treat them as equal. The AIOZ Pin pinning API reference lists every request and response field.

Frequently Asked Questions

Is a Merkle tree a special case of a Merkle DAG?

Yes. A tree is a DAG in which every node has at most one parent, so every Merkle tree is a Merkle DAG, and not the other way round.

Does IPFS use binary Merkle trees?

Not for files and directories. IPFS stores them as UnixFS Merkle DAGs, whose nodes carry data, link by CID and can hold up to 174 or 1,024 links.

Is Git a Merkle tree or a Merkle DAG?

Git is a Merkle DAG. It names every object by hash, and one tree or blob can be linked by many commits and trees.

Can a Merkle DAG contain a cycle?

No. A parent's CID depends on its children's CIDs, so a child cannot link a parent that doesn't exist yet. The IPFS docs say the hash function's properties rule cycles out.

Do the same chunks always produce the same root CID?

Only with the same settings. Chunk size, link width, layout, CID version and raw leaves all change the root, which is why IPIP-0499 defines profiles such as unixfs-v1-2025.

References

We only send updates when meaningful changes ship, and you can unsubscribe anytime

Related Content

blog thumbnail

ENS IPFS Contenthash: Point an .eth Name at a Website

An ENS IPFS contenthash points your .eth name at a site on IPFS. See the byte format, a real encoding, ipfs vs ipns records, gas used, and common mistakes.

7 min readDecember 04, 2026
blog thumbnail

ERC-721 Metadata JSON: Fields, Images and ipfs:// URIs

ERC-721 metadata JSON explained: the three fields EIP-721 defines, OpenSea attributes and animation_url, ipfs:// image links, and folder CIDs for collections.

8 min readDecember 02, 2026
blog thumbnail

Merkle DAG vs Merkle Tree: What IPFS Changes and Why

Merkle DAG vs Merkle tree explained: a tree gives each node one parent and short proofs, while the DAG IPFS uses shares blocks, carries data and links by CID.

8 min readNovember 30, 2026
blog thumbnail

What Is IPNI? How the IPFS Network Indexer Finds Providers

IPNI is the IPFS network indexer behind cid.contact. See how index providers publish signed advertisement chains and how one HTTP query finds providers.

8 min readNovember 27, 2026
blog thumbnail

On-Chain vs Off-Chain NFT Metadata: Where the Data Lives

On-chain vs off-chain NFT metadata: what the contract stores, what lives on IPFS or a web server, what each costs in gas, and how to check any token yourself.

6 min readNovember 25, 2026
blog thumbnail

What Is a Kademlia DHT? The Routing Algorithm Behind IPFS

A Kademlia DHT finds peers by XOR distance. See how k-buckets, k = 20 and iterative lookups let IPFS locate who holds a CID, and how to watch it on Kubo.

6 min readNovember 23, 2026