Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .prettierrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ module.exports = {
},
},
],
plugins: [require.resolve('prettier-plugin-organize-imports')],
};
2 changes: 1 addition & 1 deletion npmDepsHash
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sha256-CmWUZ+hMItoWISfwCwRNMkAgLm7E7jhUSxncSG/lkNI=
sha256-bk5zmCDgTNA7BOV7babhANh6psLbHvTuicF99lhPsgs=
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
"oxlint": "^1.0.0",
"pkg-pr-new": "^0.0.9",
"prettier": "^3.6.2",
"prettier-plugin-organize-imports": "^4.3.0",
"replace-in-file": "^6.3.5",
"rimraf": "^3.0.2",
"ts-jest": "^28.0.8",
Expand Down
18 changes: 18 additions & 0 deletions src/lib/proof-system/zkdriver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { CompileOptions, ConfigBaseType, ZkProgram, verify as zkprogramVerify } from './zkprogram.js';

class ZKDriverImpl {
_options: CompileOptions

constructor(options: CompileOptions = {}) {
this._options = options
}

verify = zkprogramVerify
compile<C extends ConfigBaseType>(program: ZkProgram<C>, userOptions: CompileOptions = {}) {
// locality takes precedence
const options = { ...this._options, ...userOptions }
return program.compile(options)
}
}

export class ZKDriver extends ZKDriverImpl { };
31 changes: 31 additions & 0 deletions src/lib/proof-system/zkdriver.unit-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import assert from "node:assert";
import { describe, it } from "node:test";
import { Field } from 'o1js';
import { ZKDriver } from "./zkdriver.js";
import { ZkProgram } from "./zkprogram.js";


const SimpleProgram = ZkProgram({
name: "simple-program",
publicOutput: Field,
methods: {
baseCase: {
privateInputs: [],
async method() {
return {
publicOutput: Field(1),
}
}
}
}
})

describe("zkDriver", () => {
it("should drive properly with no options", async () => {
const driver = new ZKDriver()
const { verificationKey: vk } = await driver.compile(SimpleProgram)
const result = await SimpleProgram.baseCase();
const ok = await driver.verify(result.proof, vk);
assert.equal(ok, true, "driver should prove properly");
})
})
143 changes: 76 additions & 67 deletions src/lib/proof-system/zkprogram.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
import { EmptyUndefined, EmptyVoid } from '../../bindings/lib/generic.js';
import {
Base64ProofString,
Base64VerificationKeyString,
Snarky,
Gate,
initializeBindings,
Pickles,
Snarky,
withThreadPool,
} from '../../bindings.js';
import { Pickles, Gate } from '../../bindings.js';
import { Field } from '../provable/wrapped.js';
import { FlexibleProvable, InferProvable, ProvablePureExtended } from '../provable/types/struct.js';
import { InferProvableType } from '../provable/types/provable-derivers.js';
import { Provable } from '../provable/provable.js';
import { assert, prettifyStacktracePromise } from '../util/errors.js';
import { setSrsCache, unsetSrsCache } from '../../bindings/crypto/bindings/srs.js';
import { prefixes } from '../../bindings/crypto/constants.js';
import { prefixToField } from '../../bindings/lib/binable.js';
import { EmptyUndefined, EmptyVoid } from '../../bindings/lib/generic.js';
import { From, InferValue } from '../../bindings/lib/provable-generic.js';
import { MlArray, MlBool, MlPair, MlResult } from '../ml/base.js';
import { MlFieldArray, MlFieldConstArray } from '../ml/fields.js';
import { FieldConst, FieldVar } from '../provable/core/fieldvar.js';
import { ConstraintSystemSummary, snarkContext } from '../provable/core/provable-context.js';
import { hashConstant } from '../provable/crypto/poseidon.js';
import { MlArray, MlBool, MlResult, MlPair } from '../ml/base.js';
import { MlFieldArray, MlFieldConstArray } from '../ml/fields.js';
import { FieldVar, FieldConst } from '../provable/core/fieldvar.js';
import { Cache, readCache, writeCache } from './cache.js';
import { decodeProverKey, encodeProverKey, parseHeader } from './prover-keys.js';
import { setSrsCache, unsetSrsCache } from '../../bindings/crypto/bindings/srs.js';
import { Provable } from '../provable/provable.js';
import { InferProvableType } from '../provable/types/provable-derivers.js';
import { ProvableType, ToProvable } from '../provable/types/provable-intf.js';
import { prefixToField } from '../../bindings/lib/binable.js';
import { prefixes } from '../../bindings/crypto/constants.js';
import { Subclass, Tuple, Get } from '../util/types.js';
import { FlexibleProvable, InferProvable, ProvablePureExtended } from '../provable/types/struct.js';
import { emptyWitness } from '../provable/types/util.js';
import { Field } from '../provable/wrapped.js';
import { mapObject, mapToObject, zip } from '../util/arrays.js';
import { assert, prettifyStacktracePromise } from '../util/errors.js';
import { Get, Subclass, Tuple } from '../util/types.js';
import { Cache, readCache, writeCache } from './cache.js';
import { featureFlagsFromGates, featureFlagsToMlOption } from './feature-flags.js';
import {
dummyProof,
DynamicProof,
Expand All @@ -34,33 +38,30 @@ import {
ProofClass,
ProofValue,
} from './proof.js';
import { featureFlagsFromGates, featureFlagsToMlOption } from './feature-flags.js';
import { emptyWitness } from '../provable/types/util.js';
import { From, InferValue } from '../../bindings/lib/provable-generic.js';
import { DeclaredProof, ZkProgramContext } from './zkprogram-context.js';
import { mapObject, mapToObject, zip } from '../util/arrays.js';
import { decodeProverKey, encodeProverKey, parseHeader } from './prover-keys.js';
import { VerificationKey } from './verification-key.js';
import { DeclaredProof, ZkProgramContext } from './zkprogram-context.js';

// public API
export { SelfProof, JsonProof, ZkProgram, verify, Empty, Undefined, Void, Method };
export { Empty, JsonProof, Method, SelfProof, Undefined, verify, Void, ZkProgram };

// internal API
export {
analyzeMethod,
CompiledTag,
sortMethodArguments,
compileProgram,
computeMaxProofsVerified,
dummyBase64Proof,
inCircuitVkHash,
MethodInterface,
MethodReturnType,
picklesRuleFromFunction,
compileProgram,
analyzeMethod,
PrivateInput,
Proof,
Prover,
dummyBase64Proof,
computeMaxProofsVerified,
RegularProver,
sortMethodArguments,
TupleToInstances,
PrivateInput,
Proof,
inCircuitVkHash,
};

type Undefined = undefined;
Expand Down Expand Up @@ -167,7 +168,7 @@ let SideloadedTag = {
},
};

type ConfigBaseType = {
export type ConfigBaseType = {
publicInput?: ProvableType;
publicOutput?: ProvableType;
methods: {
Expand Down Expand Up @@ -196,6 +197,21 @@ type InferMethodType<Config extends ConfigBaseType> = {
>;
};

export type CompileOptions = {
cache?: Cache;
forceRecompile?: boolean;
proofsEnabled?: boolean;
withRuntimeTables?: boolean;
lazyMode?: boolean;
};
export const CompileOptionsDefault: Required<CompileOptions> = {
cache: Cache.FileSystemDefault,
forceRecompile: false,
proofsEnabled: false,
withRuntimeTables: false,
lazyMode: false,
};

/**
* Wraps config + provable code into a program capable of producing {@link Proof}s.
*
Expand Down Expand Up @@ -225,7 +241,7 @@ type InferMethodType<Config extends ConfigBaseType> = {
*/
function ZkProgram<
Config extends ConfigBaseType,
_ extends unknown = unknown // weird hack that makes methods infer correctly when their inputs are not annotated
_ extends unknown = unknown, // weird hack that makes methods infer correctly when their inputs are not annotated
>(
config: Config & {
name: string;
Expand All @@ -239,14 +255,7 @@ function ZkProgram<
name: string;
maxProofsVerified(): Promise<0 | 1 | 2>;

compile: (options?: {
cache?: Cache;
forceRecompile?: boolean;
proofsEnabled?: boolean;
withRuntimeTables?: boolean;
numChunks?: number;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use this config option?

lazyMode?: boolean;
}) => Promise<{
compile: (options?: CompileOptions) => Promise<{
verificationKey: { data: string; hash: Field };
}>;
verify: (
Expand Down Expand Up @@ -378,13 +387,12 @@ function ZkProgram<

const programState = createProgramState();

async function compile({
cache = Cache.FileSystemDefault,
forceRecompile = false,
proofsEnabled = undefined as boolean | undefined,
withRuntimeTables = false,
lazyMode = false,
} = {}) {
async function compile(options: CompileOptions = {}) {
const { cache, forceRecompile, proofsEnabled, withRuntimeTables, lazyMode } = {
...CompileOptionsDefault,
...options,
};

doProving = proofsEnabled ?? doProving;

if (doProving) {
Expand Down Expand Up @@ -603,7 +611,7 @@ type ZkProgram<
auxiliaryOutput?: ProvableType;
};
};
}
},
> = ReturnType<typeof ZkProgram<Config>>;

/**
Expand Down Expand Up @@ -1059,7 +1067,7 @@ function toFieldAndAuxConsts<T>(type: Provable<T>, value: T) {

ZkProgram.Proof = function <
PublicInputType extends FlexibleProvable<any>,
PublicOutputType extends FlexibleProvable<any>
PublicOutputType extends FlexibleProvable<any>,
>(program: {
name: string;
publicInputType: PublicInputType;
Expand Down Expand Up @@ -1111,11 +1119,12 @@ function Prover<ProverData>() {

// helper types

type Infer<T> = T extends Subclass<typeof ProofBase>
? InstanceType<T>
: T extends ProvableType
? InferProvableType<T>
: never;
type Infer<T> =
T extends Subclass<typeof ProofBase>
? InstanceType<T>
: T extends ProvableType
? InferProvableType<T>
: never;

type TupleToInstances<T> = {
[I in keyof T]: Infer<T[I]>;
Expand All @@ -1133,21 +1142,21 @@ type MethodReturnType<PublicOutput, AuxiliaryOutput> = PublicOutput extends void
auxiliaryOutput: AuxiliaryOutput;
}
: AuxiliaryOutput extends undefined
? {
publicOutput: PublicOutput;
}
: {
publicOutput: PublicOutput;
auxiliaryOutput: AuxiliaryOutput;
};
? {
publicOutput: PublicOutput;
}
: {
publicOutput: PublicOutput;
auxiliaryOutput: AuxiliaryOutput;
};

type Method<
PublicInput,
PublicOutput,
MethodSignature extends {
privateInputs: Tuple<PrivateInput>;
auxiliaryOutput?: ProvableType;
}
},
> = PublicInput extends undefined
? {
method(
Expand Down Expand Up @@ -1176,7 +1185,7 @@ type RegularProver<
PublicInputType,
PublicOutput,
Args extends Tuple<PrivateInput>,
AuxiliaryOutput
AuxiliaryOutput,
> = (
publicInput: From<PublicInputType>,
...args: TupleFrom<Args>
Expand All @@ -1190,7 +1199,7 @@ type Prover<
PublicInputType,
PublicOutput,
Args extends Tuple<PrivateInput>,
AuxiliaryOutput
AuxiliaryOutput,
> = PublicInput extends undefined
? (...args: TupleFrom<Args>) => Promise<{
proof: Proof<PublicInput, PublicOutput>;
Expand All @@ -1210,8 +1219,8 @@ type ProvableOrVoid<A> = A extends undefined ? typeof Void : ToProvable<A>;
type InferProvableOrUndefined<A> = A extends undefined
? undefined
: A extends ProvableType
? InferProvable<A>
: InferProvable<A> | undefined;
? InferProvable<A>
: InferProvable<A> | undefined;
type InferProvableOrVoid<A> = A extends undefined ? void : InferProvable<A>;

type UnwrapPromise<P> = P extends Promise<infer T> ? T : never;
Loading