fix(r1cs): lower unsafe wide arithmetic bitwise - #253
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 #245.
Minimal reproducer
Save as
main.pl:Use:
The two results are:
At CirC
271f911, the raw evaluator accepts the source result and rejects the second. The R1CS and stock witness computation do the opposite.A sibling trigger exercises wide addition:
For
A = B = 2^254, source semantics return0 mod 2^255, while the circuit returns2^255 - p.Why it happens
When the intermediate is too wide, multiplication/addition is still performed in the scalar field and only then bit-decomposed and truncated. Field reduction therefore occurs before bit-vector reduction.
Impact: wrong relation; the bundled witness computation follows the wrong result.
Proposed fix
fix/u128-mul-field-wrapatc08defckeeps the scalar fast path only when the intermediate is provably below the modulus. Otherwise it uses ripple-carry addition and shifted partial products over bit wires.Self-contained regression:
cargo test --features r1cs target::r1cs::trans::testAll 18 focused R1CS translation tests pass.