# Merkle Tree Preimage Collisions & Bridge Proof Forgery

> Eliminate Merkle tree second-preimage attacks, dynamic abi.encodePacked hash collisions, and AVL/IAVL range-proof forgery ($570M BNB Bridge RCA) in Solidity.

Canonical: https://nftvenueledger.com/protocols/merkle-tree-leaf-preimage-collision-bridge-proof-forgery
Last updated: 2026-11-14T19:44:19.162Z

**Publication:** NFTVenue Ledger
**Category:** Cross Chain Bridge Validator Exploits
**Published:** 2026-11-13T13:10:25.915Z
**Author:** Dr. Devon Kurosawa, PhD (PhD in Distributed Systems & Cryptographic Protocol Verification)
**Reviewer:** Not recorded

## Answer-first summary

Hashing 64-byte Merkle leaves identically to 64-byte internal branch nodes—or concatenating dynamic fields via abi.encodePacked—lets attackers forge valid proofs for unbacked mints.

## Key takeaways

- Single-hashed 64-byte Merkle leaves collide structurally with 64-byte internal branch nodes, enabling second-preimage proof truncation unless leaves are double-hashed or 0x00/0x01 domain-prefixed.
- Never pass multiple dynamic types (strings, bytes, arrays) into abi.encodePacked—use abi.encode to enforce unambiguous length and offset boundaries.
- The $570M BNB Chain bridge exploit exploited an unvalidated extra right-hash field inside Tendermint/Cosmos IAVL range proofs rather than breaking SHA-256.
- Include (chainId, address(this), epochId, leafIndex) inside every Merkle leaf and pass proof arrays as bytes32 calldata paired with 256-bit packed claim bitmaps.

## Related financial entities

- **Circle Cross-Chain Transfer Protocol (CCTP)** (CCTP) — product: Permissionless on-chain utility that facilitates USDC transfers across supported blockchains via native burning on the source chain and minting on destination.
- **Seaport Marketplace Protocol Standard** (SEAPORT) — product: Open-source, highly optimized marketplace contract suite for efficiently buying and selling fungible and non-fungible tokens.
- **Zero-Knowledge Succinct Non-Interactive Argument (zk-SNARK)** (ZK-SNARK) — product: Cryptographic proof system allowing a prover to demonstrate the validity of a mathematical statement to a verifier without revealing any secret information.

## Analysis

Merkle trees verify cross-chain bridge deposits, ZK rollup state withdrawals, [Seaport bulk orders](/tokens/seaport-marketplace-protocol), and institutional reward distributions in `O(log_2 N)` hashes. Yet subtle **Domain-Separation Omissions** inside Merkle leaf construction and **Multi-Path Range-Proof Bugs** (`such as the October 2022 $570M BNB Chain IAVL Proof Forgery!`) allow attackers to construct valid proofs against an **authentic, uncompromised Merkle Root**!

Anchored in our [cross-chain bridge, oracle & exploit RCA master guide](/protocols/cross-chain-bridge-verification-oracle-exploit-rca-guide-2026), every Solidity auditor inspects Merkle verifiers across DEXs, bridges, and airdrop claim contracts in our [marketplaces directory](/marketplaces) (from Cosmos IAVL-connected [Astroport](/marketplaces/astroport) to EVM venues like [9mm Pro](/marketplaces/9mm-pro)) against **three distinct cryptographic collision bugs**.

## Vulnerability #1: The 64-Byte Leaf vs Internal Branch **Second-Preimage Attack**!

Look at how a textbook binary Merkle verifier computes each internal parent node from two 32-byte child hashes `(L, R)`:
`parentHash = keccak256(abi.encodePacked(L, R))` — notice the preimage passed to `keccak256` is **exact `64 bytes`**!

Now watch what happens if a developer constructs a **Leaf Hash** over `(address recipient [20B padded to 32B], uint256 amount [32B])` as:
`leafHash = keccak256(abi.encode(recipient, amount))` — **ALSO exact `64 bytes` with NO domain prefix**!
- Suppose the legitimate Merkle tree has an internal branch node `B = keccak256(Child_Left || Child_Right)` at height 1.
- Suppose `uint256(Child_Left) < 2^160` (`or can be ground via vanity address so upper 12 bytes are 0x00!`): the attacker submits a fake claim passing **`recipient = address(uint160(uint256(Child_Left)))`**, **`amount = uint256(Child_Right)`**, and a **shortened Merkle proof omitting height 0**!
- The contract computes `leafHash = keccak256(abi.encode(recipient, amount)) == B`, walks the remaining valid upper branch straight to `merkleRoot`, and **mints `uint256(Child_Right)` (~10^75 tokens!) to the attacker**!

**OpenZeppelin `StandardMerkleTree` Double-Hash Fix**: Hash every leaf **twice**—`leaf = keccak256(bytes.concat(keccak256(abi.encode(addr, amt))))` (`32-byte outer preimage != 64-byte branch preimage!`), OR prepend **`0x00` to leaves and `0x01` to internal nodes** (`RFC 6962 Certificate Transparency standard`)!

![RFC 6962 / OpenZeppelin StandardMerkleTree leaf double-hashing and abi.encode collision-free pipeline](/api/figure?type=flow&title=Collision-Proof%20Merkle%20Tree%20Construction%20%26%20Verification&subtitle=Enforcing%20leaf-vs-node%20domain%20separation%2C%20abi.encode%20dynamic%20boundaries%2C%20and%20claim%20bitmaps&steps=1.%20Structured%20Encode%3A%3AUse%20abi.encode%28chainId%2C%20addr%2C%20id%2C%20amt%29%20-%3E%20NEVER%20abi.encodePacked!%7C2.%20Double-Hash%20Leaf%3A%3Aleaf%20%3D%20keccak256%28bytes.concat%28keccak256%28encoded%29%29%29%20%2832B%20!%3D%2064B%20node!%29%7C3.%20Canonical%20Pair%20Sort%3A%3ACommutative%20pair%20hash%20%28a%20%3C%20b%20%3F%20H%28a%2Cb%29%20%3A%20H%28b%2Ca%29%29%20or%20explicit%20index%20bits%7C4.%20Bitmap%20Replay%20Lock%3A%3AFlip%20claimedBitMap%5Bindex%20%3E%3E%208%5D%20%20%3D%20%281%20%3C%3C%20%28index%20%26%20255%29%29%20BEFORE%20transfer!&metrics=LEAF%20PREIMAGE%20LEN%3A%3A32B%20%28Double%20Hash%29%3A%3A100%25%20disjoint%20from%2064B%20internal%20nodes%7CPACKED%20DYNAMIC%20BUG%3A%3ABANNED!%3A%3AencodePacked%28%22ab%22%2C%22c%22%29%20%3D%3D%20%28%22a%22%2C%22bc%22%29!%7CBNB%20BRIDGE%20LOSS%3A%3A2M%20BNB%20%28%24570M%29%3A%3ACosmos%20IAVL%20ProofOp%20right-path%20bug%7CCLAIM%20STORAGE%3A%3A256%20Claims%20%2F%20Slot%3A%3ABitMaps.BitMap%20saves%2075%25%20claim%20gas)

## Vulnerability #2: `abi.encodePacked` Dynamic Array / String Collisions & Vulnerability #3: IAVL Forgery

1. **`abi.encodePacked` Boundary Shifting**: Calling `keccak256(abi.encodePacked(stringA, stringB))` or `abi.encodePacked(address path1, address path2)` strips length prefixes! `abi.encodePacked("eth", "usdc")` produces the **exact identical byte stream** `"ethusdc"` as `abi.encodePacked("ethu", "sdc")`! Always use `abi.encode(...)` when hashing variable-length types!
2. **The `$570M` BNB Bridge Cosmos `iavl:v` Multi-Path Forgery**: Tendermint/Cosmos `ics23` range proofs allowed an `InnerNode` struct containing both `left` and `right` fields. When a legitimate leaf sat on the left, `left` was empty—and the verifier hashed `left || computedChild || right`. By crafting a malicious proof injecting a bogus `right` hash into an existing tree node, the attacker tricked the verifier into matching the real `appHash` while proving a non-existent **`2,000,000 BNB` deposit**!

## Merkle & Trie Proof Vulnerability Audit Matrix

| Cryptographic Vulnerability | Root Coding Mistake | Exploit Consequence | Deterministic Solidity Fix |
| :--- | :--- | :--- | :--- |
| **64-Byte Second-Preimage** | Single `keccak256(abi.encode(a, b))` leaf | Internal node interpreted as valid leaf! | **Double-hash** leaves (`StandardMerkleTree`) or `0x00`/`0x01` prefix |
| **Dynamic Field Boundary Shift** | `keccak256(abi.encodePacked(dyn1, dyn2))` | Shift elements between `dyn1` & `dyn2` | Replace `abi.encodePacked` with **`abi.encode`** |
| **Unindexed Duplicate Leaves** | Omitting unique `uint256 leafIndex` | User claims same `(addr, 100)` leaf only once (`or replays if unbitmapped!`) | Include unique `leafIndex` + `chainId` + `contractAddr` in leaf! |
| **Malleable Proof Path** | Accepting arbitrary `left/right` fields (`ICS-23 bug`) | Forges arbitrary bridge mints (`$570M`) | Enforce strict mutually exclusive `left XOR right` spec invariants |

![On-chain gas cost for verifying Merkle inclusion across 1,000 to 1,000,000 leaves using calldata + BitMap](/api/figure?type=bars&title=On-Chain%20Merkle%20Proof%20Gas%20Scaling%20%281K%20to%201M%20Leaves%29&subtitle=Logarithmic%20calldata%20%2B%20hashing%20cost%20paired%20with%20OpenZeppelin%20256-bit%20packed%20claim%20bitmaps&steps=1%2C024%20Leaves%20%28Depth%2010%29%3A%3A~34%2C200%20gas%20total%20%2810%20hashes%20%2B%20warm%20256-bit%20bitmap%20SSTORE%29%7C65%2C536%20Leaves%20%28Depth%2016%29%3A%3A~38%2C900%20gas%20total%20%28%2B6%20proof%20hashes%20%3D%20%2B192%20bytes%20calldata%29%7C1%2C048%2C576%20Leaves%20%28Depth%2020%29%3A%3A~42%2C100%20gas%20total%20%28proves%201-in-1M%20state%20leaf%20in%20%3C%240.05%20L2%20gas!%29%7CZK-Aggregated%201%2C000%20Claims%3A%3A~250%2C000%20gas%20TOTAL%20%28~250%20gas%2Fclaim%20via%20recursive%20SNARK%20batch!%29&metrics=OZ%20LIBRARY%3A%3A%40openzeppelin%2Fmerkle-tree%3A%3AMatches%20Solidity%20StandardMerkleTree%201%3A1%7CCALLDATA%20PROOF%3A%3Abytes32%5B%5D%20calldata%3A%3ASaves%20~40%25%20memory%20copy%20gas%20vs%20memory!%7CCROSS-DROP%20REPLAY%3A%3AHash%20Epoch%2FAddr%3A%3APrevents%20Season-1%20proof%20replay%20on%20S2%7CICS-23%20PATCH%3A%3AStrict%20Spec%20Check%3A%3AForbids%20non-empty%20right%20when%20left%20empty)

## RFC 6962 / OpenZeppelin Double-Hashed Merkle Verification Formula

Always generate trees off-chain with `@openzeppelin/merkle-tree` and verify on-chain using `MerkleProof.verifyCalldata` with double-hashed, context-bound leaves:

```formula
1) Second-Preimage Proof Leaf Construction (OpenZeppelin StandardMerkleTree):
 Inner_Hash = keccak256( abi.encode( block.chainid, address(this), epochId, leafIndex, recipient, tokenAddr, amount ) )
 Leaf_Node = keccak256( bytes.concat( Inner_Hash ) ) [Preimage length = 32 bytes]

2) Internal Branch Node Construction (Sorted Pair Commutative Hash):
 Branch_Node(A, B) = (A < B) ? keccak256( abi.encodePacked(A, B) ) : keccak256( abi.encodePacked(B, A) ) [Preimage length = 64 bytes]

 Mathematical Disjointness Proof:
 Because len(Preimage(Leaf_Node)) == 32 bytes != 64 bytes == len(Preimage(Branch_Node)),
 under collision resistance of Keccak-256, NO internal node B can ever be forged as a Leaf_Node!
```

## What If a Zero-Day Proof Forgery STILL Happens? (`ERC-7265 Circuit Breakers`)

Notice what saved **`$430M` of the `$570M` minted** in the BNB Bridge proof forgery: rapid outflow throttling and chain coordination halted off-ramps before the attacker could bridge everything out! Next, pair [ZK-SNARK light-client header verification](/protocols/zk-snark-light-client-sync-committee-header-verification) with sub-second outflow caps inside Solidity via [ERC-7265 circuit breakers & rate limiters](/protocols/erc-7265-circuit-breaker-rate-limiter-outflow-throttling) inside our [Cross-Chain Bridge & Oracle Security hub](/topics/cross-chain-bridge-validator-exploits).

## Frequently asked questions

### Why does OpenZeppelin StandardMerkleTree hash every leaf twice with keccak256?

Internal Merkle tree nodes hash two 32-byte children (64 bytes total); hashing a leaf once with abi.encode(address, uint256) also hashes 64 bytes, allowing an internal node to masquerade as a leaf unless hashed a second time (32-byte preimage).

### When is abi.encodePacked safe vs unsafe in Solidity?

abi.encodePacked is safe ONLY when all arguments have fixed static byte lengths (e.g., two bytes32 hashes); passing two dynamic strings or arrays strips boundaries so ("ab","c") collides identically with ("a","bc").

### How did the $570M BNB Bridge attacker forge a Merkle proof without stealing validator keys?

A bug in the Cosmos IAVL tree proof verifier allowed specifying both a child calculation and an arbitrary extra sibling field in the same InnerOp, shifting the real state hash into a side branch while proving a fake 2M BNB leaf.

### Why should Merkle proof verifiers use verifyCalldata instead of verify?

Declaring bytes32 calldata proof and calling MerkleProof.verifyCalldata reads sibling hashes directly from transaction calldata without allocating and copying a memory array, saving ~35%–45% verification gas.


## Source URLs

- https://docs.openzeppelin.com/contracts/5.x/api/utils#MerkleProof
- https://github.com/OpenZeppelin/merkle-tree
- https://datatracker.ietf.org/doc/html/rfc6962#section-2.1

## Citation

- APA: Dr. Devon Kurosawa, PhD. (2026). Merkle Tree Second-Preimage Attacks, abi.encodePacked Collisions & Bridge Proof Forgery. NFTVenue Ledger. https://nftvenueledger.com/protocols/merkle-tree-leaf-preimage-collision-bridge-proof-forgery
- Canonical article: [Merkle Tree Second-Preimage Attacks, abi.encodePacked Collisions & Bridge Proof Forgery](https://nftvenueledger.com/protocols/merkle-tree-leaf-preimage-collision-bridge-proof-forgery)
- Site research index: [NFTVenue Ledger](https://nftvenueledger.com/llms-full.txt)
