-
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 #1176 from o1-labs/feature/range-check-64-develop
Activate custom gates in JS and add rangeCheck64 gadget
- Loading branch information
Showing
11 changed files
with
311 additions
and
42 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 046712 to 851d3d
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,42 @@ | ||
import { | ||
Field, | ||
Circuit, | ||
circuitMain, | ||
public_, | ||
UInt64, | ||
Experimental, | ||
} from 'o1js'; | ||
|
||
let { ZkProgram } = Experimental; | ||
|
||
const Main = ZkProgram({ | ||
publicInput: Field, | ||
methods: { | ||
main: { | ||
privateInputs: [UInt64], | ||
method(y: Field, x: UInt64) { | ||
let y3 = y.square().mul(y); | ||
y3.assertEquals(x.value); | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
console.log('generating keypair...'); | ||
console.time('generating keypair...'); | ||
const kp = await Main.compile(); | ||
console.timeEnd('generating keypair...'); | ||
|
||
console.log('prove...'); | ||
console.time('prove...'); | ||
const x = UInt64.from(8); | ||
const y = new Field(2); | ||
const proof = await Main.main(y, x); | ||
console.timeEnd('prove...'); | ||
|
||
console.log('verify...'); | ||
console.time('verify...'); | ||
let ok = await Main.verify(proof); | ||
console.timeEnd('verify...'); | ||
|
||
console.log('ok?', ok); |
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,14 @@ | ||
import { type Field } from '../field.js'; | ||
import * as Gates from '../gates.js'; | ||
|
||
export { rangeCheck64 }; | ||
|
||
function rangeCheck64(x: Field) { | ||
if (x.isConstant()) { | ||
if (x.toBigInt() >= 1n << 64n) { | ||
throw Error(`rangeCheck64: expected field to fit in 64 bits, got ${x}`); | ||
} | ||
} else { | ||
Gates.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,49 @@ | ||
import { Snarky } from '../snarky.js'; | ||
import { FieldVar, FieldConst, type Field } from './field.js'; | ||
|
||
export { rangeCheck64 }; | ||
|
||
/** | ||
* Asserts that x is at most 64 bits | ||
*/ | ||
function rangeCheck64(x: Field) { | ||
let [, x0, x2, x4, x6, x8, x10, x12, x14] = Snarky.exists(8, () => { | ||
let xx = x.toBigInt(); | ||
// crumbs (2-bit limbs) | ||
return [ | ||
0, | ||
getBits(xx, 0, 2), | ||
getBits(xx, 2, 2), | ||
getBits(xx, 4, 2), | ||
getBits(xx, 6, 2), | ||
getBits(xx, 8, 2), | ||
getBits(xx, 10, 2), | ||
getBits(xx, 12, 2), | ||
getBits(xx, 14, 2), | ||
]; | ||
}); | ||
// 12-bit limbs | ||
let [, x16, x28, x40, x52] = Snarky.exists(4, () => { | ||
let xx = x.toBigInt(); | ||
return [ | ||
0, | ||
getBits(xx, 16, 12), | ||
getBits(xx, 28, 12), | ||
getBits(xx, 40, 12), | ||
getBits(xx, 52, 12), | ||
]; | ||
}); | ||
Snarky.gates.rangeCheck0( | ||
x.value, | ||
[0, FieldVar[0], FieldVar[0], x52, x40, x28, x16], | ||
[0, x14, x12, x10, x8, x6, x4, x2, x0], | ||
// not using compact mode | ||
FieldConst[0] | ||
); | ||
} | ||
|
||
function getBits(x: bigint, start: number, length: number) { | ||
return FieldConst.fromBigint( | ||
(x >> BigInt(start)) & ((1n << BigInt(length)) - 1n) | ||
); | ||
} |
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.