-
Notifications
You must be signed in to change notification settings - Fork 118
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 #1216 from o1-labs/feature/multi-range-check
Foreign fields 1: Multi-range-check gadgets
- Loading branch information
Showing
8 changed files
with
345 additions
and
29 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 e17660 to 49c3f0
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,49 +1,127 @@ | ||
import type { Gate } from '../../snarky.js'; | ||
import { mod } from '../../bindings/crypto/finite_field.js'; | ||
import { Field } from '../../lib/core.js'; | ||
import { ZkProgram } from '../proof_system.js'; | ||
import { Provable } from '../provable.js'; | ||
import { | ||
Spec, | ||
boolean, | ||
equivalentAsync, | ||
field, | ||
fieldWithRng, | ||
} from '../testing/equivalent.js'; | ||
import { Random } from '../testing/property.js'; | ||
import { assert, exists } from './common.js'; | ||
import { Gadgets } from './gadgets.js'; | ||
import { L } from './range-check.js'; | ||
import { expect } from 'expect'; | ||
|
||
let maybeUint64: Spec<bigint, Field> = { | ||
...field, | ||
rng: Random.map(Random.oneOf(Random.uint64, Random.uint64.invalid), (x) => | ||
mod(x, Field.ORDER) | ||
), | ||
let uint = (n: number | bigint): Spec<bigint, Field> => { | ||
let uint = Random.bignat((1n << BigInt(n)) - 1n); | ||
return fieldWithRng(uint); | ||
}; | ||
|
||
let maybeUint = (n: number | bigint): Spec<bigint, Field> => { | ||
let uint = Random.bignat((1n << BigInt(n)) - 1n); | ||
return fieldWithRng( | ||
Random.map(Random.oneOf(uint, uint.invalid), (x) => mod(x, Field.ORDER)) | ||
); | ||
}; | ||
|
||
// constraint system sanity check | ||
|
||
function csWithoutGenerics(gates: Gate[]) { | ||
return gates.map((g) => g.type).filter((type) => type !== 'Generic'); | ||
} | ||
|
||
let check64 = Provable.constraintSystem(() => { | ||
let [x] = exists(1, () => [0n]); | ||
Gadgets.rangeCheck64(x); | ||
}); | ||
let multi = Provable.constraintSystem(() => { | ||
let x = exists(3, () => [0n, 0n, 0n]); | ||
Gadgets.multiRangeCheck(x); | ||
}); | ||
let compact = Provable.constraintSystem(() => { | ||
let [xy, z] = exists(2, () => [0n, 0n]); | ||
Gadgets.compactMultiRangeCheck(xy, z); | ||
}); | ||
|
||
let expectedLayout64 = ['RangeCheck0']; | ||
let expectedLayoutMulti = ['RangeCheck0', 'RangeCheck0', 'RangeCheck1', 'Zero']; | ||
|
||
expect(csWithoutGenerics(check64.gates)).toEqual(expectedLayout64); | ||
expect(csWithoutGenerics(multi.gates)).toEqual(expectedLayoutMulti); | ||
expect(csWithoutGenerics(compact.gates)).toEqual(expectedLayoutMulti); | ||
|
||
// TODO: make a ZkFunction or something that doesn't go through Pickles | ||
// -------------------------- | ||
// RangeCheck64 Gate | ||
// -------------------------- | ||
|
||
let RangeCheck64 = ZkProgram({ | ||
name: 'range-check-64', | ||
let RangeCheck = ZkProgram({ | ||
name: 'range-check', | ||
methods: { | ||
run: { | ||
check64: { | ||
privateInputs: [Field], | ||
method(x) { | ||
Gadgets.rangeCheck64(x); | ||
}, | ||
}, | ||
checkMulti: { | ||
privateInputs: [Field, Field, Field], | ||
method(x, y, z) { | ||
Gadgets.multiRangeCheck([x, y, z]); | ||
}, | ||
}, | ||
checkCompact: { | ||
privateInputs: [Field, Field], | ||
method(xy, z) { | ||
let [x, y] = Gadgets.compactMultiRangeCheck(xy, z); | ||
x.add(y.mul(1n << L)).assertEquals(xy); | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
await RangeCheck64.compile(); | ||
await RangeCheck.compile(); | ||
|
||
// TODO: we use this as a test because there's no way to check custom gates quickly :( | ||
await equivalentAsync({ from: [maybeUint64], to: boolean }, { runs: 3 })( | ||
|
||
await equivalentAsync({ from: [maybeUint(64)], to: boolean }, { runs: 3 })( | ||
(x) => { | ||
if (x >= 1n << 64n) throw Error('expected 64 bits'); | ||
assert(x < 1n << 64n); | ||
return true; | ||
}, | ||
async (x) => { | ||
let proof = await RangeCheck64.run(x); | ||
return await RangeCheck64.verify(proof); | ||
let proof = await RangeCheck.check64(x); | ||
return await RangeCheck.verify(proof); | ||
} | ||
); | ||
|
||
await equivalentAsync( | ||
{ from: [maybeUint(L), uint(L), uint(L)], to: boolean }, | ||
{ runs: 3 } | ||
)( | ||
(x, y, z) => { | ||
assert(!(x >> L) && !(y >> L) && !(z >> L), 'multi: not out of range'); | ||
return true; | ||
}, | ||
async (x, y, z) => { | ||
let proof = await RangeCheck.checkMulti(x, y, z); | ||
return await RangeCheck.verify(proof); | ||
} | ||
); | ||
|
||
await equivalentAsync( | ||
{ from: [maybeUint(2n * L), uint(L)], to: boolean }, | ||
{ runs: 3 } | ||
)( | ||
(xy, z) => { | ||
assert(!(xy >> (2n * L)) && !(z >> L), 'compact: not out of range'); | ||
return true; | ||
}, | ||
async (xy, z) => { | ||
let proof = await RangeCheck.checkCompact(xy, z); | ||
return await RangeCheck.verify(proof); | ||
} | ||
); |
Oops, something went wrong.