-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1181 from o1-labs/feature/refactor-equivalence-tests
Expose and test rangeCheck64 gadget
- Loading branch information
Showing
10 changed files
with
435 additions
and
162 deletions.
There are no files selected for viewing
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
Submodule bindings
updated
from 851d3d to 69904a
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,36 @@ | ||
/** | ||
* Wrapper file for various gadgets, with a namespace and doccomments. | ||
*/ | ||
import { rangeCheck64 } from './range-check.js'; | ||
import { Field } from '../core.js'; | ||
|
||
export { Gadgets }; | ||
|
||
const Gadgets = { | ||
/** | ||
* Asserts that the input value is in the range [0, 2^64). | ||
* | ||
* This function proves that the provided field element can be represented with 64 bits. | ||
* If the field element exceeds 64 bits, an error is thrown. | ||
* | ||
* @param x - The value to be range-checked. | ||
* | ||
* @throws Throws an error if the input value exceeds 64 bits. | ||
* | ||
* @example | ||
* ```ts | ||
* const x = Provable.witness(Field, () => Field(12345678n)); | ||
* rangeCheck64(x); // successfully proves 64-bit range | ||
* | ||
* const xLarge = Provable.witness(Field, () => Field(12345678901234567890123456789012345678n)); | ||
* rangeCheck64(xLarge); // throws an error since input exceeds 64 bits | ||
* ``` | ||
* | ||
* **Note**: Small "negative" field element inputs are interpreted as large integers close to the field size, | ||
* and don't pass the 64-bit check. If you want to prove that a value lies in the int64 range [-2^63, 2^63), | ||
* you could use `rangeCheck64(x.add(1n << 63n))`. | ||
*/ | ||
rangeCheck64(x: Field) { | ||
return rangeCheck64(x); | ||
}, | ||
}; |
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,47 @@ | ||
import { mod } from '../../bindings/crypto/finite_field.js'; | ||
import { Field } from '../field.js'; | ||
import { ZkProgram } from '../proof_system.js'; | ||
import { | ||
Spec, | ||
boolean, | ||
equivalentAsync, | ||
field, | ||
} from '../testing/equivalent.js'; | ||
import { Random } from '../testing/random.js'; | ||
import { Gadgets } from './gadgets.js'; | ||
|
||
// TODO: make a ZkFunction or something that doesn't go through Pickles | ||
|
||
let RangeCheck64 = ZkProgram({ | ||
methods: { | ||
run: { | ||
privateInputs: [Field], | ||
method(x) { | ||
Gadgets.rangeCheck64(x); | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
await RangeCheck64.compile(); | ||
|
||
let maybeUint64: Spec<bigint, Field> = { | ||
...field, | ||
rng: Random.map(Random.oneOf(Random.uint64, Random.uint64.invalid), (x) => | ||
mod(x, Field.ORDER) | ||
), | ||
}; | ||
|
||
// do a couple of proofs | ||
// TODO: we use this as a test because there's no way to check custom gates quickly :( | ||
|
||
equivalentAsync({ from: [maybeUint64], to: boolean }, { runs: 3 })( | ||
(x) => { | ||
if (x >= 1n << 64n) throw Error('expected 64 bits'); | ||
return true; | ||
}, | ||
async (x) => { | ||
let proof = await RangeCheck64.run(x); | ||
return await RangeCheck64.verify(proof); | ||
} | ||
); |
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.