Repository navigation
fix: hash zero-valued leaves in BinaryStateTree.merkelize - #690
Open
gomesalexandre wants to merge 1 commit into
Open
gomesalexandre wants to merge 1 commit into
gomesalexandre wants to merge 1 commit into
Conversation
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>
|
@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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
BinaryStateTree.merkelizetreated a leaf holding 32 zero bytes as if it wasn't there. The internalhashhelper returned 32 zero bytes for any input that was all zeros. EIP-7864 only special-cases the 64-byte input:The reference
_hashisif data in (None, b"\x00" * 64): return b"\x00" * 32. So a leaf that was set to zero still hashes asblake3(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_DATAleaf 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 to0x00…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:0x03ecb31f…3a830x0000…00000x03ecb31f…3a830x01…leaf, same stem0x8b1fd0f3…b8c10xb419a75c…f4e00x8b1fd0f3…b8c1I 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