Expr: sound disjointness rule for readWord/WriteWord with bounded offset#1065
Merged
Conversation
Adds a new readWord clause that elides a WriteWord whose offset is of the form `Add (Lit c) X`, when X has a derivable static upper bound and the write region is provably disjoint from the read in EVM (mod 2^256) arithmetic. Backed by a new conservative `upperBound :: Expr EWord -> Maybe W256` helper that handles Lit, And-with-Lit, Mod-by-Lit, Div-by- Lit, Mul/Add with one Lit operand, and SHR by a Lit shift. The disjointness check uses Integer arithmetic to detect wrap on both the read and write byte ranges before concluding the write can be skipped, so the rule does not assume non-wrapping addition. Tests (5 local Expr unit tests in memoryTests): * positive: WriteWord at Add(Lit, And-bounded) skipped * positive: WriteWord at Add(Lit, Mul-And-bounded) skipped * negative: unbounded X — not skipped * negative: wrap-risk near maxBound — not skipped * negative: read region overlaps write min offset — not skipped Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
blishko
reviewed
May 21, 2026
blishko
left a comment
Collaborator
There was a problem hiding this comment.
Looks good!
I am suggesting one small change that I think makes the condition more readable.
Cleanup Cleanup
Co-authored-by: blishko <blishko@argot.org>
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.
Summary
Adds a new
readWordclause that elides aWriteWordwhose offset is of the formAdd (Lit c) X, whenXhas a derivable static upper bound and the write region is provably disjoint from the read in EVM (mod 2^256) arithmetic.upperBound :: Expr EWord -> Maybe W256— conservative static upper bound handlingLit,And-with-Lit,Mod-by-Lit,Div-by-Lit,Mul/Addwith oneLitoperand, andSHRby aLitshift.writeDisjointFromReadWord :: W256 -> Expr EWord -> Bool— usesIntegerarithmetic to detect wrap on both the read and write byte ranges before concluding the write can be skipped, so the rule does not assume non-wrapping addition.readWordat theWriteWordcase.Split out from #1063 so the disjointness rule is reviewable on its own; #1063 will be rebased on top of this.
Tests
5 new unit tests in
test/EVM/Expr/ExprTests.hs:WriteWordatAdd(Lit, And-bounded)skippedWriteWordatAdd(Lit, Mul-And-bounded)skippedX— not skippedmaxBound— not skippedChecklist
🤖 Generated with Claude Code