fix(r1cs): constrain wide unsigned division bitwise - #250
Draft
NullWitnessZK wants to merge 1 commit into
Draft
Conversation
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.
Fixes #243.
Minimal reproducer
Unsigned division:
Unsigned remainder uses the same trigger with
A % B = R.Choose:
Source semantics require:
But the R1CS also admits the quotient/remainder obtained by dividing
A + pbyB:These values are individually within 128 bits and satisfy
q' * B + r' = A + p, so they satisfy the current relation modulopeven though they are not the source result.Expected
Only
(q, r) = (0, 1)is accepted.Actual
A custom witness using
(q', r')satisfies all 651 pre- and post-reduction constraints. The normal witness computation still produces the correct pair, so exploitation requires a custom witness.Why it happens
The gadget constrains
q * B == A - rin the scalar field. Foru128,q * Bcan exceed the modulus, making this a modular rather than integer equality.Impact: custom-witness underconstraint/soundness.
Proposed fix
fix/u128-divmod-field-wrapat032a74aimplements unsigned restoring division over bit wires, including bitwise subtraction and borrow propagation.Self-contained regression:
cargo test --features r1cs target::r1cs::trans::testAll 18 focused R1CS translation tests pass.