-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
2,567 additions
and
359 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,27 @@ | ||
# Range Check | ||
# `AssertInRange` | ||
|
||
TODO | ||
```cs | ||
template AssertInRange(MIN, MAX) { | ||
assert(MIN < MAX); | ||
signal input in; | ||
|
||
var b = nbits(MAX); | ||
|
||
component lowerBound = AssertBits(b); | ||
lowerBound.in <== in - MIN; | ||
|
||
component upperBound = AssertBits(b); | ||
upperBound.in <== in + (2 ** b) - MAX - 1; | ||
} | ||
``` | ||
|
||
A range-check is a common utility, asserting that a number is in some range `[MIN, MAX]`. Above is one way of doing that. Our approach here is to check: | ||
|
||
- `in - MIN` is a b-bit value | ||
- `in + 2^b - 1 - MAX` is a b-bit value | ||
|
||
where `b` is the minimum number of bits required to represent `MAX`. The `nbits` function is described within the [bits section](../bits/). | ||
|
||
If `in < MIN`, the operation `in - MIN` will underflow and we will have a huge number, definitely not be b-bit representable (assuming a not-so-large MAX). As an edge case, if `in == MIN` we get 0, which is definitely b-bits. | ||
|
||
If `in > MAX`, the operation `in + 2^b - 1 - MAX` becomes larger than `2^b - 1`, which is not b-bit representable. As an edge case, if `in == MAX` we get `2^b - 1`, which is the largest b-bit number. |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
pragma circom 2.1.0; | ||
|
||
include "../bits/index.circom"; | ||
|
||
// Checks that `in` is in range [MIN, MAX] | ||
// In other words, `MIN <= in <= MAX`. | ||
template AssertInRange(MIN, MAX) { | ||
assert(MIN < MAX); | ||
signal input in; | ||
|
||
var b = nbits(MAX); | ||
|
||
// find `in - MIN`: | ||
// if `in > MIN` this is a b-bit value | ||
// otherwise, it will underflow due to negation, | ||
// and may be larger than MAX | ||
component lowerBound = AssertBits(b); | ||
lowerBound.in <== in - MIN; // e.g. 1 - 1 = 0 (for 0 <= in) | ||
|
||
// find `in + 2^b - MAX - 1` | ||
// if `in < MAX` this will result in a value less than `2^b - 1` | ||
// ensuring that it is a b-bit value, otherwise this will be a value | ||
// larger than b-bits | ||
component upperBound = AssertBits(b); | ||
upperBound.in <== in + (2 ** b) - MAX - 1; // e.g. 9 + (15 - 9) = 15 (for in <= 15) | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import type { JestConfigWithTsJest } from "ts-jest"; | ||
|
||
const config: JestConfigWithTsJest = { | ||
// ts-jest defaults | ||
preset: "ts-jest", | ||
testEnvironment: "node", | ||
transform: { | ||
"^.+\\.(ts|js)$": "ts-jest", | ||
}, | ||
verbose: true, | ||
testTimeout: 40_000, | ||
testPathIgnorePatterns: ["/node_modules/", "/examples/", "/book/", "/circuits/"], | ||
}; | ||
|
||
export default config; |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.