fix!: AVM was missing range check on remainder for div in ALU#21074
Merged
Conversation
Contributor
Author
This stack of pull requests is managed by Graphite. Learn more about stacking. |
jeanmon
approved these changes
Mar 4, 2026
Co-authored-by: Jean M <132435771+jeanmon@users.noreply.github.com>
Merged
github-merge-queue Bot
pushed a commit
that referenced
this pull request
Mar 5, 2026
BEGIN_COMMIT_OVERRIDE fix(avm)!: memory pre-audit (#21058) fix(avm)!: memory trace changes (#21059) fix!: AVM was missing range check on remainder for div in ALU (#21074) feat: run AVM NAPI simulations on dedicated threads instead of libuv pool (#21138) feat(avm)!: Unify nullifier, written slots and retrieved bytecodes tree traces (#20949) fix(avm)!: public inputs pre-audit (#21162) END_COMMIT_OVERRIDE
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.

Cherry-picked from v4 PR: #21066
The Bug
The AVM's ALU division circuit (
alu.pil) constrains integer divisiona / b = cwith remainderr(stored inhelper1) via:This enforces
b * c + r = a(i.e.,a - r = b * c). A separate greater-than check ensuresr < b.However, the remainder
helper1was never range-checked to fit within the operand's bit-width (max_bits). A malicious prover could sethelper1to an arbitrarily large field element (e.g., close to the field modulus) that satisfiesb * c + r = a (mod p)andr < bin the field, but does not represent a valid unsigned integer remainder. This would allow forging incorrect division results.The Exploit
For non-u128 integer division (u8, u16, u32, u64), a prover can choose a remainder
rthat is a large field element (not a valid unsigned integer) but still satisfies:b * c + r ≡ a (mod p)— the main division relationr < b— the greater-than check (which operates over the full field)Without a range check on
r, the prover can manipulate the quotientcto be any value they want, completely breaking the soundness of integer division.The Fix
Added a lookup-based range check on the remainder:
This ensures
helper1(the remainder) fits withinmax_bits(e.g., 32 bits for u32), preventing the prover from using oversized field elements as the remainder.The simulation (
alu.cpp) was also updated to emit the correspondingassert_rangeevent, and unit tests were updated to expect the new range check call.