Skip to content

Commit cdb11b3

Browse files
Trivo25querolita
authored andcommitted
enable chunking
1 parent 63ec194 commit cdb11b3

File tree

3 files changed

+62
-28
lines changed

3 files changed

+62
-28
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Field, ZkProgram, verify } from 'o1js';
2+
3+
let MyProgram = ZkProgram({
4+
chunks: 1,
5+
overrideWrapDomain: 0,
6+
name: 'example-with-chunking',
7+
publicOutput: Field,
8+
9+
methods: {
10+
baseCase: {
11+
privateInputs: [],
12+
async method() {
13+
return Field(0);
14+
},
15+
},
16+
},
17+
});
18+
19+
console.log('compiling MyProgram...');
20+
let { verificationKey } = await MyProgram.compile();
21+
22+
console.log('proving base case...');
23+
let proof = await MyProgram.baseCase();
24+
25+
console.log('verify...');
26+
let ok = await verify(proof.toJSON(), verificationKey);
27+
console.log('ok?', ok);

src/lib/proof-system/zkprogram.ts

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ function ZkProgram<
233233
[I in keyof Config['methods']]: InferMethodType<Config>[I];
234234
};
235235
overrideWrapDomain?: 0 | 1 | 2;
236+
chunks?: number;
236237
}
237238
): {
238239
name: string;
@@ -388,6 +389,7 @@ function ZkProgram<
388389
overrideWrapDomain: config.overrideWrapDomain,
389390
state: programState,
390391
withRuntimeTables,
392+
chunks: config.chunks,
391393
});
392394

393395
compileOutput = { provers, verify, maxProofsVerified };
@@ -692,6 +694,7 @@ async function compileProgram({
692694
overrideWrapDomain,
693695
state,
694696
withRuntimeTables,
697+
chunks,
695698
}: {
696699
publicInputType: Provable<any>;
697700
publicOutputType: Provable<any>;
@@ -705,6 +708,7 @@ async function compileProgram({
705708
overrideWrapDomain?: 0 | 1 | 2;
706709
state?: ReturnType<typeof createProgramState>;
707710
withRuntimeTables?: boolean;
711+
chunks?: number;
708712
}) {
709713
await initializeBindings();
710714
if (methodIntfs.length === 0)
@@ -749,34 +753,36 @@ If you are using a SmartContract, make sure you are using the @method decorator.
749753
MlBool(cache.canWrite),
750754
];
751755

752-
let { verificationKey, provers, verify, tag } = await prettifyStacktracePromise(
753-
withThreadPool(async () => {
754-
let result: ReturnType<typeof Pickles.compile>;
755-
let id = snarkContext.enter({ inCompile: true });
756-
setSrsCache(cache);
757-
try {
758-
result = Pickles.compile(MlArray.to(rules), {
759-
publicInputSize: publicInputType.sizeInFields(),
760-
publicOutputSize: publicOutputType.sizeInFields(),
761-
storable: picklesCache,
762-
overrideWrapDomain,
763-
});
764-
let { getVerificationKey, provers, verify, tag } = result;
765-
CompiledTag.store(proofSystemTag, tag);
766-
let [, data, hash] = await getVerificationKey();
767-
let verificationKey = { data, hash: Field(hash) };
768-
return {
769-
verificationKey,
770-
provers: MlArray.from(provers),
771-
verify,
772-
tag,
773-
};
774-
} finally {
775-
snarkContext.leave(id);
776-
unsetSrsCache();
777-
}
778-
})
779-
);
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+
);
780786
// wrap provers
781787
let wrappedProvers = provers.map(
782788
(prover): Pickles.Prover =>

src/snarky.d.ts

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

0 commit comments

Comments
 (0)