Skip to content

fix: hash zero-valued leaves in BinaryStateTree.merkelize - #690

Open
gomesalexandre wants to merge 1 commit into
wevm:mainfrom
gomesalexandre:fix_bst_zero_leaf
Open

gomesalexandre wants to merge 1 commit into
wevm:mainfrom
gomesalexandre:fix_bst_zero_leaf

Conversation

@gomesalexandre

Copy link
Copy Markdown
Contributor

BinaryStateTree.merkelize treated a leaf holding 32 zero bytes as if it wasn't there. The internal hash helper returned 32 zero bytes for any input that was all zeros. EIP-7864 only special-cases the 64-byte input:

  • hash([0x00] * 64) = [0x00] * 32
  • hash(value) = H(value), where H is the selected cryptographic hash function.

The reference _hash is if data in (None, b"\x00" * 64): return b"\x00" * 32. So a leaf that was set to zero still hashes as blake3(0x00 * 32). A leaf that was never set (None) is the only one that hashes to zero. The rule is the same in the EIP's first commit (e8d6056815, 2025-01-22), so this isn't spec drift.

This comes up in practice: the BASIC_DATA leaf of an account with version, code size, nonce and balance all zero is 32 zero bytes. Before this fix, a zero leaf added nothing to its stem's hash, so every tree holding one got the wrong root. A tree with a single zero leaf at the zero key merkelized to 0x00…00, the same root as an empty tree.

The fix only applies the zero shortcut to 64-byte inputs. Internal and stem node hashes are always 64 bytes, so they behave exactly as before, and all existing snapshots are unchanged.

To check it, I ran the EIP's two Python code blocks verbatim with blake3. They reproduce the existing ox snapshots (0x6945…1cd5, 0xe93c…2218), and they give:

tree EIP reference ox before ox after
one zero leaf 0x03ecb31f…3a83 0x0000…0000 0x03ecb31f…3a83
zero leaf + 0x01… leaf, same stem 0x8b1fd0f3…b8c1 0xb419a75c…f4e0 0x8b1fd0f3…b8c1

I also ran a seeded differential over 300 random trees (shared stems, the zero stem, about 40% zero values). Before the fix, ox matched the reference on 26 of 300, and every mismatch contained a zero value. After the fix it matches on 300 of 300. The two tests added here are the two rows of the table above.

This PR leaves out value-length validation: the reference asserts on values that aren't 32 or 64 bytes, and ox doesn't check that.

🤖 Generated with Claude Code

EIP-7864 only maps the 64-byte zero input to the zero hash, so a leaf set
to 32 zero bytes is a real leaf. `hash` zeroed any all-zero input, which
merkelized zero-valued leaves as empty (a lone zero leaf at the zero key
gave the empty-tree root).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vercel

vercel Bot commented Nov 16, 2026

Copy link
Copy Markdown

@gomesalexandre is attempting to deploy a commit to the Wevm Team on Vercel.

A member of the Team first needs to authorize it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant