-
Notifications
You must be signed in to change notification settings - Fork 213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add SafeLessThan
template to comparators with range checks over inputs
#86
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,6 +98,23 @@ template LessThan(n) { | |
out <== 1-n2b.out[n]; | ||
} | ||
|
||
// Do Range checks on the inputs to avoid overflow | ||
template SafeLessThan(n) { | ||
assert(n <= 252); | ||
signal input in[2]; | ||
signal output out; | ||
|
||
component aInRange = Num2Bits(252); | ||
aInRange.in <== in[0]; | ||
component bInRange = Num2Bits(252); | ||
bInRange.in <== in[1]; | ||
|
||
component n2b = Num2Bits(n+1); | ||
|
||
n2b.in <== in[0]+ (1<<n) - in[1]; | ||
|
||
out <== 1-n2b.out[n]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps we could use a normal There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree, applied change in this commit |
||
} | ||
|
||
|
||
// N is the number of bits the input have. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can save some constraints by restricting to
n
bits instead. It will always be safe sincen <= 252
anyway. Also, that way we can interpretSafeLessThan(n)
as meaning "safely compare two n-bit numbers." This makes it easier to understand whyin[0]+ (1<<n) - in[1]
should be an n+1 bit number.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the comment. Good point. Added to the PR.