Skip to content

Commit

Permalink
Dynamically set proofs enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
ejMina226 committed Jan 8, 2025
1 parent f7df3c5 commit efaf83e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 20 deletions.
1 change: 1 addition & 0 deletions packages/common/src/zkProgram/WithZkProgram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export interface PlainZkProgram<PublicInput = undefined, PublicOutput = void> {
Record<string, Awaited<ReturnType<typeof Provable.constraintSystem>>>
>;
proofsEnabled: boolean;
setProofsEnabled(proofsEnabled: boolean): void;
}

export interface ZkProgramFactory<PublicInput, PublicOutput> {
Expand Down
39 changes: 30 additions & 9 deletions packages/module/src/runtime/Runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ export interface RuntimeDefinition<Modules extends RuntimeModulesRecord> {
export class RuntimeZkProgramFactory<Modules extends RuntimeModulesRecord>
implements ZkProgramFactory<undefined, MethodPublicOutput>
{
public constructor(public runtime: Runtime<Modules>) {}
public constructor(
public runtime: Runtime<Modules>,
private readonly areProofsEnabled: boolean
) {}

public get appChain() {
return this.runtime.appChain;
Expand Down Expand Up @@ -231,7 +234,7 @@ export class RuntimeZkProgramFactory<Modules extends RuntimeModulesRecord>
publicOutput: MethodPublicOutput,
methods: bucket,
});

program.setProofsEnabled(this.areProofsEnabled);
const SelfProof = ZkProgram.Proof(program);

const methods = Object.keys(bucket).reduce<Record<string, any>>(
Expand All @@ -246,8 +249,8 @@ export class RuntimeZkProgramFactory<Modules extends RuntimeModulesRecord>
compile: program.compile.bind(program),
verify: program.verify.bind(program),
analyzeMethods: program.analyzeMethods.bind(program),
// TODO set this dynamically somehow
proofsEnabled: true,
proofsEnabled: program.proofsEnabled,
setProofsEnabled: program.setProofsEnabled.bind(program),
Proof: SelfProof,
methods,
};
Expand All @@ -274,12 +277,25 @@ export class Runtime<Modules extends RuntimeModulesRecord>
};
}

// runtime modules composed into a ZkProgram
public zkProgram: PlainZkProgram<undefined, MethodPublicOutput>[];
private _zkProgram?: PlainZkProgram<undefined, MethodPublicOutput>[];

private _zkProgramFactory?: ZkProgramFactory<undefined, MethodPublicOutput>;

public definition: RuntimeDefinition<Modules>;

public zkProgramFactory: ZkProgramFactory<undefined, MethodPublicOutput>;
// runtime modules composed into a ZkProgram
public get zkProgram(): PlainZkProgram<undefined, MethodPublicOutput>[] {
// eslint-disable-next-line no-underscore-dangle
return this._zkProgram!;
}

public get zkProgramFactory(): ZkProgramFactory<
undefined,
MethodPublicOutput
> {
// eslint-disable-next-line no-underscore-dangle
return this._zkProgramFactory!;
}

/**
* Creates a new Runtime from the provided config
Expand All @@ -289,15 +305,20 @@ export class Runtime<Modules extends RuntimeModulesRecord>
public constructor(definition: RuntimeDefinition<Modules>) {
super(definition);
this.definition = definition;
this.zkProgramFactory = new RuntimeZkProgramFactory<Modules>(this);
this.zkProgram = this.zkProgramFactory.zkProgramFactory();
}

// TODO Remove after changing DFs to type-based approach
public create(childContainerProvider: ChildContainerProvider) {
super.create(childContainerProvider);

this.useDependencyFactory(this.container.resolve(MethodIdFactory));
// eslint-disable-next-line no-underscore-dangle
this._zkProgramFactory = new RuntimeZkProgramFactory<Modules>(
this,
this.appChain?.areProofsEnabled!
);
// eslint-disable-next-line no-underscore-dangle
this._zkProgram = this.zkProgramFactory.zkProgramFactory();
}

public get appChain(): AreProofsEnabled | undefined {
Expand Down
15 changes: 10 additions & 5 deletions packages/protocol/src/prover/block/BlockProver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ export class BlockProverFactory
public readonly runtime: PlainZkProgram<undefined, MethodPublicOutput>[],
private readonly transactionHooks: ProvableTransactionHook<unknown>[],
private readonly blockHooks: ProvableBlockHook<unknown>[],
private readonly verificationKeyService: MinimalVKTreeService
private readonly verificationKeyService: MinimalVKTreeService,
private readonly areProofsEnabled: boolean
) {}

public get appChain(): AreProofsEnabled | undefined {
Expand Down Expand Up @@ -864,6 +865,7 @@ export class BlockProverFactory
},
},
});
program.setProofsEnabled(this.areProofsEnabled);

const methods = {
proveTransaction: program.proveTransaction,
Expand All @@ -878,8 +880,8 @@ export class BlockProverFactory
verify: program.verify.bind(program),
analyzeMethods: program.analyzeMethods.bind(program),
Proof: SelfProofClass,
// TODO Set this dynamically
proofsEnabled: true,
proofsEnabled: program.proofsEnabled,
setProofsEnabled: program.setProofsEnabled.bind(program),
methods,
},
];
Expand Down Expand Up @@ -912,7 +914,9 @@ export class BlockProver extends ProtocolModule implements BlockProvable {
transactionHooks: ProvableTransactionHook<unknown>[],
@injectAll("ProvableBlockHook")
blockHooks: ProvableBlockHook<unknown>[],
verificationKeyService: RuntimeVerificationKeyRootService
verificationKeyService: RuntimeVerificationKeyRootService,
@inject("AreProofsEnabled")
proofsEnabled: AreProofsEnabled
) {
super();
this.zkProgramFactory = new BlockProverFactory(
Expand All @@ -922,7 +926,8 @@ export class BlockProver extends ProtocolModule implements BlockProvable {
runtime.zkProgram,
transactionHooks,
blockHooks,
verificationKeyService
verificationKeyService,
proofsEnabled.areProofsEnabled
);
this.zkProgram = this.zkProgramFactory.zkProgramFactory();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ZkProgramFactory,
} from "@proto-kit/common";
import { Field, Provable, SelfProof, ZkProgram } from "o1js";
import { injectable } from "tsyringe";
import { inject, injectable } from "tsyringe";

import { constants } from "../../Constants";
import { ProvableStateTransition } from "../../model/StateTransition";
Expand Down Expand Up @@ -64,7 +64,8 @@ export class StateTransitionProverFactory
>
{
public constructor(
private readonly stateTransitionProver: StateTransitionProver
private readonly stateTransitionProver: StateTransitionProver,
private readonly areProofsEnabled: boolean
) {}

public get appChain(): AreProofsEnabled | undefined {
Expand Down Expand Up @@ -113,6 +114,7 @@ export class StateTransitionProverFactory
},
},
});
program.setProofsEnabled(this.areProofsEnabled);

const methods = {
proveBatch: program.proveBatch.bind(program),
Expand All @@ -127,8 +129,8 @@ export class StateTransitionProverFactory
verify: program.verify.bind(program),
analyzeMethods: program.analyzeMethods.bind(program),
Proof: SelfProofClass,
// TODO Set this dynamically
proofsEnabled: true,
proofsEnabled: program.proofsEnabled,
setProofsEnabled: program.setProofsEnabled.bind(program),
methods,
},
];
Expand Down Expand Up @@ -344,9 +346,15 @@ export class StateTransitionProver
StateTransitionProverPublicOutput
>[];

public constructor() {
public constructor(
@inject("AreProofsEnabled")
proofsEnabled: AreProofsEnabled
) {
super();
this.zkProgramFactory = new StateTransitionProverFactory(this);
this.zkProgramFactory = new StateTransitionProverFactory(
this,
proofsEnabled.areProofsEnabled
);
this.zkProgram = this.zkProgramFactory.zkProgramFactory();
}

Expand Down

0 comments on commit efaf83e

Please sign in to comment.