Open
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.
Template ModSubThree(n) accepts unexpected solutions when a - b - c + (1 << n) >= 0.
The template ModSubThree(n) receives three inputs a, b, c that can be expressed using n bits and performs the operation a - b - c. It returns the result of the operation using n bits (signal out), along with a signal indicating if there has been an underflow (borrow).
The condition a - b - c + (1 << n) >= 0 is introduced as an assert that is not checked at compile time as it is unknown (https://docs.circom.io/circom-language/code-quality/code-assertion/). The assert is only included in the witness generation code but it is not added to the constraint system describing the circuit, which may generate unexpected behaviors.
For example, for n = 3, the constraints of ModSubThree(3) for the inputs a = 0, b = 7, c = 7 are satisfied by the output borrow = 1, out = -6. However, this solution does not satisfy the specification of the circuit as out cannot be expressed using 3 bits.
The proposed fix incorporates the check a - b - c + (1 << n) >= 0 to the constraint system without adding extra constraints (same number of non-linear constraints, less linear constraints) by using a call to Num2Bits to both compute the value of borrow and perform this check.