Skip to content

Commit 05e8a39

Browse files
committed
pass chunks to compile promise in pickles bindings
1 parent cdb11b3 commit 05e8a39

File tree

6 files changed

+65
-45
lines changed

6 files changed

+65
-45
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1717

1818
## [Unreleased](https://github.com/o1-labs/o1js/compare/4e23a60...HEAD)
1919

20+
### Added
21+
22+
- [PR! 1905](https://github.com/o1-labs/o1js/pull/1905) API support for circuit chunking
23+
- still requires memory optimizations to be fully functional, and
24+
- proof-systems version still needs to be updated to include [this commit](https://github.com/o1-labs/proof-systems/pull/3222/commits/8c37c293f8159eed3676964ba47fc5dc0ae6ea1e)
25+
- that fixed the zero knowledge rows mismatch across Kimchi WASM bindings
26+
2027
## [2.5.0](https://github.com/o1-labs/o1js/compare/6ff7f8470a...4e23a60)
2128

2229
### Fixed

src/bindings/ocaml/lib/pickles_bindings.ml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,8 @@ let pickles_compile (choices : pickles_rule_js array)
617617
< publicInputSize : int Js.prop
618618
; publicOutputSize : int Js.prop
619619
; storable : Cache.js_storable Js.optdef_prop
620-
; overrideWrapDomain : int Js.optdef_prop >
620+
; overrideWrapDomain : int Js.optdef_prop
621+
; numChunks : int Js.optdef_prop >
621622
Js.t ) =
622623
(* translate number of branches and recursively verified proofs from JS *)
623624
let branches = Array.length choices in
@@ -636,6 +637,9 @@ let pickles_compile (choices : pickles_rule_js array)
636637
Js.Optdef.to_option config##.overrideWrapDomain
637638
|> Option.map ~f:Pickles_base.Proofs_verified.of_int_exn
638639
in
640+
let num_chunks =
641+
Js.Optdef.get config##.numChunks (fun () -> 1)
642+
in
639643
let (Choices choices) =
640644
Choices.of_js ~public_input_size ~public_output_size choices
641645
in
@@ -658,7 +662,7 @@ let pickles_compile (choices : pickles_rule_js array)
658662
, public_input_typ public_output_size ) )
659663
~auxiliary_typ:Typ.unit
660664
~max_proofs_verified:(module Max_proofs_verified)
661-
~name ~choices ()
665+
~name ~num_chunks ~choices ()
662666
in
663667

664668
(* translate returned prover and verify functions to JS *)

src/bindings/ocaml/lib/pickles_bindings.mli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ val pickles :
6565
-> < publicInputSize : int Js.prop
6666
; publicOutputSize : int Js.prop
6767
; storable : Cache.js_storable Js.optdef_prop
68-
; overrideWrapDomain : int Js.optdef_prop >
68+
; overrideWrapDomain : int Js.optdef_prop
69+
; numChunks : int Js.optdef_prop >
6970
Js.t
7071
-> < getVerificationKey :
7172
( unit
Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Field, ZkProgram, verify } from 'o1js';
1+
import { Field, Cache, Gadgets, ZkProgram } from 'o1js';
22

33
let MyProgram = ZkProgram({
44
chunks: 1,
@@ -8,20 +8,28 @@ let MyProgram = ZkProgram({
88

99
methods: {
1010
baseCase: {
11-
privateInputs: [],
12-
async method() {
13-
return Field(0);
11+
privateInputs: [Field],
12+
async method(input: Field) {
13+
for (let i = 0; i < 1 << 16; i++) {
14+
Gadgets.rangeCheck64(Field(input).add(Field(i)));
15+
}
16+
// The above generates 2^16+2^15 rows which needs to be split into 2 chunks
17+
return {
18+
publicOutput: Field(0),
19+
};
1420
},
1521
},
1622
},
1723
});
1824

25+
console.log(await MyProgram.analyzeMethods());
26+
1927
console.log('compiling MyProgram...');
20-
let { verificationKey } = await MyProgram.compile();
28+
await MyProgram.compile({ cache: Cache.None });
2129

2230
console.log('proving base case...');
23-
let proof = await MyProgram.baseCase();
31+
let { proof } = await MyProgram.baseCase(Field(0));
2432

2533
console.log('verify...');
26-
let ok = await verify(proof.toJSON(), verificationKey);
34+
let ok = await MyProgram.verify(proof);
2735
console.log('ok?', ok);

src/lib/proof-system/zkprogram.ts

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ function ZkProgram<
233233
[I in keyof Config['methods']]: InferMethodType<Config>[I];
234234
};
235235
overrideWrapDomain?: 0 | 1 | 2;
236-
chunks?: number;
236+
numChunks?: number;
237237
}
238238
): {
239239
name: string;
@@ -244,6 +244,7 @@ function ZkProgram<
244244
forceRecompile?: boolean;
245245
proofsEnabled?: boolean;
246246
withRuntimeTables?: boolean;
247+
numChunks?: number;
247248
}) => Promise<{
248249
verificationKey: { data: string; hash: Field };
249250
}>;
@@ -387,9 +388,9 @@ function ZkProgram<
387388
cache,
388389
forceRecompile,
389390
overrideWrapDomain: config.overrideWrapDomain,
391+
numChunks: config.numChunks,
390392
state: programState,
391393
withRuntimeTables,
392-
chunks: config.chunks,
393394
});
394395

395396
compileOutput = { provers, verify, maxProofsVerified };
@@ -692,9 +693,9 @@ async function compileProgram({
692693
cache,
693694
forceRecompile,
694695
overrideWrapDomain,
696+
numChunks,
695697
state,
696698
withRuntimeTables,
697-
chunks,
698699
}: {
699700
publicInputType: Provable<any>;
700701
publicOutputType: Provable<any>;
@@ -706,9 +707,9 @@ async function compileProgram({
706707
cache: Cache;
707708
forceRecompile: boolean;
708709
overrideWrapDomain?: 0 | 1 | 2;
710+
numChunks?: number;
709711
state?: ReturnType<typeof createProgramState>;
710712
withRuntimeTables?: boolean;
711-
chunks?: number;
712713
}) {
713714
await initializeBindings();
714715
if (methodIntfs.length === 0)
@@ -753,36 +754,35 @@ If you are using a SmartContract, make sure you are using the @method decorator.
753754
MlBool(cache.canWrite),
754755
];
755756

756-
let { verificationKey, provers, verify, tag } =
757-
await prettifyStacktracePromise(
758-
withThreadPool(async () => {
759-
let result: ReturnType<typeof Pickles.compile>;
760-
let id = snarkContext.enter({ inCompile: true });
761-
setSrsCache(cache);
762-
try {
763-
result = Pickles.compile(MlArray.to(rules), {
764-
publicInputSize: publicInputType.sizeInFields(),
765-
publicOutputSize: publicOutputType.sizeInFields(),
766-
storable: picklesCache,
767-
overrideWrapDomain,
768-
chunks: chunks ?? 1,
769-
});
770-
let { getVerificationKey, provers, verify, tag } = result;
771-
CompiledTag.store(proofSystemTag, tag);
772-
let [, data, hash] = await getVerificationKey();
773-
let verificationKey = { data, hash: Field(hash) };
774-
return {
775-
verificationKey,
776-
provers: MlArray.from(provers),
777-
verify,
778-
tag,
779-
};
780-
} finally {
781-
snarkContext.leave(id);
782-
unsetSrsCache();
783-
}
784-
})
785-
);
757+
let { verificationKey, provers, verify, tag } = await prettifyStacktracePromise(
758+
withThreadPool(async () => {
759+
let result: ReturnType<typeof Pickles.compile>;
760+
let id = snarkContext.enter({ inCompile: true });
761+
setSrsCache(cache);
762+
try {
763+
result = Pickles.compile(MlArray.to(rules), {
764+
publicInputSize: publicInputType.sizeInFields(),
765+
publicOutputSize: publicOutputType.sizeInFields(),
766+
storable: picklesCache,
767+
overrideWrapDomain,
768+
numChunks: numChunks ?? 1,
769+
});
770+
let { getVerificationKey, provers, verify, tag } = result;
771+
CompiledTag.store(proofSystemTag, tag);
772+
let [, data, hash] = await getVerificationKey();
773+
let verificationKey = { data, hash: Field(hash) };
774+
return {
775+
verificationKey,
776+
provers: MlArray.from(provers),
777+
verify,
778+
tag,
779+
};
780+
} finally {
781+
snarkContext.leave(id);
782+
unsetSrsCache();
783+
}
784+
})
785+
);
786786
// wrap provers
787787
let wrappedProvers = provers.map(
788788
(prover): Pickles.Prover =>

src/snarky.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ declare const Pickles: {
702702
publicOutputSize: number;
703703
storable?: Pickles.Cache;
704704
overrideWrapDomain?: 0 | 1 | 2;
705-
chunks: number;
705+
numChunks?: number;
706706
}
707707
) => {
708708
provers: MlArray<Pickles.Prover>;

0 commit comments

Comments
 (0)