Skip to content

Commit

Permalink
optional proving for zkprogram
Browse files Browse the repository at this point in the history
  • Loading branch information
Trivo25 committed Aug 26, 2024
1 parent 0f57e36 commit 8cea470
Showing 1 changed file with 67 additions and 21 deletions.
88 changes: 67 additions & 21 deletions src/lib/proof-system/zkprogram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,11 @@ function ZkProgram<
}
): {
name: string;
compile: (options?: { cache?: Cache; forceRecompile?: boolean }) => Promise<{
compile: (options?: {
cache?: Cache;
forceRecompile?: boolean;
proofsEnabled?: boolean;
}) => Promise<{
verificationKey: { data: string; hash: Field };
}>;
verify: (
Expand Down Expand Up @@ -597,6 +601,8 @@ function ZkProgram<
Types[I]
>;
} {
let proofsEnabled_ = true;

let methods = config.methods;
let publicInputType: ProvablePure<any> = ProvableType.get(
config.publicInput ?? Undefined
Expand Down Expand Up @@ -656,22 +662,32 @@ function ZkProgram<
async function compile({
cache = Cache.FileSystemDefault,
forceRecompile = false,
proofsEnabled = true,
} = {}) {
proofsEnabled_ = proofsEnabled;
let methodsMeta = await analyzeMethods();
let gates = methodKeys.map((k) => methodsMeta[k].gates);
let { provers, verify, verificationKey } = await compileProgram({
publicInputType,
publicOutputType,
methodIntfs,
methods: methodFunctions,
gates,
proofSystemTag: selfTag,
cache,
forceRecompile,
overrideWrapDomain: config.overrideWrapDomain,
});
compileOutput = { provers, verify };
return { verificationKey };

if (proofsEnabled_) {
let { provers, verify, verificationKey } = await compileProgram({
publicInputType,
publicOutputType,
methodIntfs,
methods: methodFunctions,
gates,
proofSystemTag: selfTag,
cache,
forceRecompile,
overrideWrapDomain: config.overrideWrapDomain,
});

compileOutput = { provers, verify };
return { verificationKey };
} else {
return {
verificationKey: VerificationKey.empty(),
};
}
}

function toProver<K extends keyof Types & string>(
Expand All @@ -682,6 +698,12 @@ function ZkProgram<
publicInput: PublicInput,
...args: TupleToInstances<Types[typeof key]>
): Promise<Proof<PublicInput, PublicOutput>> {
class ProgramProof extends Proof<PublicInput, PublicOutput> {
static publicInputType = publicInputType;
static publicOutputType = publicOutputType;
static tag = () => selfTag;
}

let picklesProver = compileOutput?.provers?.[i];
if (picklesProver === undefined) {
throw Error(
Expand All @@ -703,30 +725,54 @@ function ZkProgram<
}
let [publicOutputFields, proof] = MlPair.from(result);
let publicOutput = fromFieldConsts(publicOutputType, publicOutputFields);
class ProgramProof extends Proof<PublicInput, PublicOutput> {
static publicInputType = publicInputType;
static publicOutputType = publicOutputType;
static tag = () => selfTag;
}

return new ProgramProof({
publicInput,
publicOutput,
proof,
maxProofsVerified,
});
}

async function dummyProve_(
publicInput: PublicInput,
...args: TupleToInstances<Types[typeof key]>
): Promise<Proof<PublicInput, PublicOutput>> {
class ProgramProof extends Proof<PublicInput, PublicOutput> {
static publicInputType = publicInputType;
static publicOutputType = publicOutputType;
static tag = () => selfTag;
}

let publicInputFields = toFieldConsts(publicInputType, publicInput);
let previousProofs = MlArray.to(
getPreviousProofsForProver(args, methodIntfs[i])
);

let publicOutput = await (methods[key].method as any)(
publicInputFields,
previousProofs
);

return ProgramProof.dummy(publicInput, publicOutput, maxProofsVerified);
}

let prove: Prover<PublicInput, PublicOutput, Types[K]>;
if (
(publicInputType as any) === Undefined ||
(publicInputType as any) === Void
) {
prove = ((...args: TupleToInstances<Types[typeof key]>) =>
(prove_ as any)(undefined, ...args)) as any;
(proofsEnabled_ ? prove_ : (dummyProve_ as any))(
undefined,
...(args as any)
)) as any;
} else {
prove = prove_ as any;
prove = (proofsEnabled_ ? prove_ : dummyProve_) as any;
}
return [key, prove];
}

let provers = Object.fromEntries(methodKeys.map(toProver)) as {
[I in keyof Types]: Prover<PublicInput, PublicOutput, Types[I]>;
};
Expand Down

0 comments on commit 8cea470

Please sign in to comment.