Skip to content

Commit

Permalink
refactor: remove AvmVerificationKeyData and tube specific types (#8569)
Browse files Browse the repository at this point in the history
Removes the `AvmVerificationKey*` classes in favour of using the vanilla
classes with custom length.
  • Loading branch information
alexghr authored Oct 18, 2024
1 parent 61e4d12 commit da6c579
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 203 deletions.
4 changes: 2 additions & 2 deletions yarn-project/bb-prover/src/avm_proving.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {
AvmCircuitInputs,
AvmVerificationKeyData,
FunctionSelector,
Gas,
GlobalVariables,
PublicKeys,
SerializableContractInstance,
VerificationKeyData,
} from '@aztec/circuits.js';
import { Fr, Point } from '@aztec/foundation/fields';
import { createDebugLogger } from '@aztec/foundation/log';
Expand Down Expand Up @@ -137,7 +137,7 @@ const proveAndVerifyAvmTestContract = async (
// Then we test VK extraction and serialization.
const succeededRes = proofRes as BBSuccess;
const vkData = await extractAvmVkData(succeededRes.vkPath!);
AvmVerificationKeyData.fromBuffer(vkData.toBuffer());
VerificationKeyData.fromBuffer(vkData.toBuffer());

// Then we verify.
const rawVkPath = path.join(succeededRes.vkPath!, 'vk');
Expand Down
18 changes: 9 additions & 9 deletions yarn-project/bb-prover/src/prover/bb_prover.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/* eslint-disable require-await */
import {
type AvmProofAndVerificationKey,
type ProofAndVerificationKey,
type PublicInputsAndRecursiveProof,
type ServerCircuitProver,
makeProofAndVerificationKey,
makePublicInputsAndRecursiveProof,
} from '@aztec/circuit-types';
import { type CircuitProvingStats, type CircuitWitnessGenerationStats } from '@aztec/circuit-types/stats';
import {
AGGREGATION_OBJECT_LENGTH,
type AvmCircuitInputs,
type AvmVerificationKeyData,
type BaseOrMergeRollupPublicInputs,
type BaseParityInputs,
type BaseRollupInputs,
Expand Down Expand Up @@ -208,7 +208,7 @@ export class BBNativeRollupProver implements ServerCircuitProver {
@trackSpan('BBNativeRollupProver.getAvmProof', inputs => ({
[Attributes.APP_CIRCUIT_NAME]: inputs.functionName,
}))
public async getAvmProof(inputs: AvmCircuitInputs): Promise<AvmProofAndVerificationKey> {
public async getAvmProof(inputs: AvmCircuitInputs): Promise<ProofAndVerificationKey<Proof>> {
const proofAndVk = await this.createAvmProof(inputs);
await this.verifyAvmProof(proofAndVk.proof, proofAndVk.verificationKey);
return proofAndVk;
Expand Down Expand Up @@ -677,8 +677,8 @@ export class BBNativeRollupProver implements ServerCircuitProver {
return provingResult;
}

private async createAvmProof(input: AvmCircuitInputs): Promise<AvmProofAndVerificationKey> {
const operation = async (bbWorkingDirectory: string): Promise<AvmProofAndVerificationKey> => {
private async createAvmProof(input: AvmCircuitInputs): Promise<ProofAndVerificationKey<Proof>> {
const operation = async (bbWorkingDirectory: string): Promise<ProofAndVerificationKey<Proof>> => {
const provingResult = await this.generateAvmProofWithBB(input, bbWorkingDirectory);

const rawProof = await fs.readFile(provingResult.proofPath!);
Expand Down Expand Up @@ -709,14 +709,14 @@ export class BBNativeRollupProver implements ServerCircuitProver {
} satisfies CircuitProvingStats,
);

return { proof, verificationKey };
return makeProofAndVerificationKey(proof, verificationKey);
};
return await this.runInDirectory(operation);
}

public async getTubeProof(
input: TubeInputs,
): Promise<{ tubeVK: VerificationKeyData; tubeProof: RecursiveProof<typeof TUBE_PROOF_LENGTH> }> {
): Promise<ProofAndVerificationKey<RecursiveProof<typeof TUBE_PROOF_LENGTH>>> {
// this probably is gonna need to call client ivc
const operation = async (bbWorkingDirectory: string) => {
logger.debug(`createTubeProof: ${bbWorkingDirectory}`);
Expand All @@ -740,7 +740,7 @@ export class BBNativeRollupProver implements ServerCircuitProver {
} fields`,
);

return { tubeVK, tubeProof };
return makeProofAndVerificationKey(tubeProof, tubeVK);
};
return await this.runInDirectory(operation);
}
Expand Down Expand Up @@ -814,7 +814,7 @@ export class BBNativeRollupProver implements ServerCircuitProver {
return await this.verifyWithKey(getUltraHonkFlavorForCircuit(circuitType), verificationKey, proof);
}

public async verifyAvmProof(proof: Proof, verificationKey: AvmVerificationKeyData) {
public async verifyAvmProof(proof: Proof, verificationKey: VerificationKeyData) {
return await this.verifyWithKeyInternal(proof, verificationKey, (proofPath, vkPath) =>
verifyAvmProof(this.config.bbBinaryPath, proofPath, vkPath, logger.debug),
);
Expand Down
19 changes: 10 additions & 9 deletions yarn-project/bb-prover/src/test/test_circuit_prover.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {
type AvmProofAndVerificationKey,
type ProofAndVerificationKey,
type PublicInputsAndRecursiveProof,
type ServerCircuitProver,
makeProofAndVerificationKey,
makePublicInputsAndRecursiveProof,
} from '@aztec/circuit-types';
import {
AVM_VERIFICATION_KEY_LENGTH_IN_FIELDS,
type AvmCircuitInputs,
AvmVerificationKeyData,
type BaseOrMergeRollupPublicInputs,
type BaseParityInputs,
type BaseRollupInputs,
Expand Down Expand Up @@ -273,12 +274,9 @@ export class TestCircuitProver implements ServerCircuitProver {

public async getTubeProof(
_tubeInput: TubeInputs,
): Promise<{ tubeVK: VerificationKeyData; tubeProof: RecursiveProof<typeof TUBE_PROOF_LENGTH> }> {
): Promise<ProofAndVerificationKey<RecursiveProof<typeof TUBE_PROOF_LENGTH>>> {
await this.delay();
return {
tubeVK: VerificationKeyData.makeFakeHonk(),
tubeProof: makeEmptyRecursiveProof(TUBE_PROOF_LENGTH),
};
return makeProofAndVerificationKey(makeEmptyRecursiveProof(TUBE_PROOF_LENGTH), VerificationKeyData.makeFakeHonk());
}

/**
Expand Down Expand Up @@ -546,12 +544,15 @@ export class TestCircuitProver implements ServerCircuitProver {
);
}

public async getAvmProof(_inputs: AvmCircuitInputs): Promise<AvmProofAndVerificationKey> {
public async getAvmProof(_inputs: AvmCircuitInputs): Promise<ProofAndVerificationKey<Proof>> {
// We can't simulate the AVM because we don't have enough context to do so (e.g., DBs).
// We just return an empty proof and VK data.
this.logger.debug('Skipping AVM simulation in TestCircuitProver.');
await this.delay();
return { proof: makeEmptyProof(), verificationKey: AvmVerificationKeyData.makeFake() };
return makeProofAndVerificationKey(
makeEmptyProof(),
VerificationKeyData.makeFake(AVM_VERIFICATION_KEY_LENGTH_IN_FIELDS),
);
}

private async delay(): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {
AVM_VERIFICATION_KEY_LENGTH_IN_FIELDS,
AvmVerificationKeyAsFields,
AvmVerificationKeyData,
Fr,
VerificationKeyAsFields,
VerificationKeyData,
Expand Down Expand Up @@ -33,7 +31,7 @@ export async function extractVkData(vkDirectoryPath: string): Promise<Verificati
}

// TODO: This was adapted from the above function. A refactor might be needed.
export async function extractAvmVkData(vkDirectoryPath: string): Promise<AvmVerificationKeyData> {
export async function extractAvmVkData(vkDirectoryPath: string): Promise<VerificationKeyData> {
const [rawFields, rawBinary] = await Promise.all([
fs.readFile(path.join(vkDirectoryPath, VK_FIELDS_FILENAME), { encoding: 'utf-8' }),
fs.readFile(path.join(vkDirectoryPath, VK_FILENAME)),
Expand All @@ -44,7 +42,7 @@ export async function extractAvmVkData(vkDirectoryPath: string): Promise<AvmVeri
// TODO: is the above actually the case?
const vkHash = fields[0];
assert(fields.length === AVM_VERIFICATION_KEY_LENGTH_IN_FIELDS, 'Invalid AVM verification key length');
const vkAsFields = new AvmVerificationKeyAsFields(fields, vkHash);
const vk = new AvmVerificationKeyData(vkAsFields, rawBinary);
const vkAsFields = new VerificationKeyAsFields(fields, vkHash);
const vk = new VerificationKeyData(vkAsFields, rawBinary);
return vk;
}
33 changes: 16 additions & 17 deletions yarn-project/circuit-types/src/interfaces/proving-job.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
type AvmCircuitInputs,
type AvmVerificationKeyData,
type BaseOrMergeRollupPublicInputs,
type BaseParityInputs,
type BaseRollupInputs,
Expand Down Expand Up @@ -31,29 +30,30 @@ import {

import { type CircuitName } from '../stats/index.js';

export type AvmProofAndVerificationKey = {
proof: Proof;
verificationKey: AvmVerificationKeyData;
};

export type PublicInputsAndRecursiveProof<T> = {
inputs: T;
proof: RecursiveProof<typeof NESTED_RECURSIVE_PROOF_LENGTH>;
export type ProofAndVerificationKey<P> = {
proof: P;
verificationKey: VerificationKeyData;
};

export type PublicInputsAndTubeProof<T> = {
export function makeProofAndVerificationKey<P>(
proof: P,
verificationKey: VerificationKeyData,
): ProofAndVerificationKey<P> {
return { proof, verificationKey };
}

export type PublicInputsAndRecursiveProof<T, N extends number = typeof NESTED_RECURSIVE_PROOF_LENGTH> = {
inputs: T;
proof: RecursiveProof<typeof TUBE_PROOF_LENGTH>;
proof: RecursiveProof<N>;
verificationKey: VerificationKeyData;
};

export function makePublicInputsAndRecursiveProof<T>(
export function makePublicInputsAndRecursiveProof<T, N extends number = typeof NESTED_RECURSIVE_PROOF_LENGTH>(
inputs: T,
proof: RecursiveProof<typeof NESTED_RECURSIVE_PROOF_LENGTH>,
proof: RecursiveProof<N>,
verificationKey: VerificationKeyData,
) {
const result: PublicInputsAndRecursiveProof<T> = {
const result: PublicInputsAndRecursiveProof<T, N> = {
inputs,
proof,
verificationKey,
Expand Down Expand Up @@ -188,7 +188,7 @@ export type ProvingRequest =

export type ProvingRequestPublicInputs = {
[ProvingRequestType.PRIVATE_KERNEL_EMPTY]: PublicInputsAndRecursiveProof<KernelCircuitPublicInputs>;
[ProvingRequestType.PUBLIC_VM]: AvmProofAndVerificationKey;
[ProvingRequestType.PUBLIC_VM]: ProofAndVerificationKey<Proof>;

[ProvingRequestType.PUBLIC_KERNEL_INNER]: PublicInputsAndRecursiveProof<VMCircuitPublicInputs>;
[ProvingRequestType.PUBLIC_KERNEL_MERGE]: PublicInputsAndRecursiveProof<PublicKernelCircuitPublicInputs>;
Expand All @@ -203,8 +203,7 @@ export type ProvingRequestPublicInputs = {

[ProvingRequestType.BASE_PARITY]: RootParityInput<typeof RECURSIVE_PROOF_LENGTH>;
[ProvingRequestType.ROOT_PARITY]: RootParityInput<typeof NESTED_RECURSIVE_PROOF_LENGTH>;
// TODO(#7369) properly structure tube proof flow
[ProvingRequestType.TUBE_PROOF]: { tubeVK: VerificationKeyData; tubeProof: RecursiveProof<typeof TUBE_PROOF_LENGTH> };
[ProvingRequestType.TUBE_PROOF]: ProofAndVerificationKey<RecursiveProof<typeof TUBE_PROOF_LENGTH>>;
};

export type ProvingRequestResult<T extends ProvingRequestType> = ProvingRequestPublicInputs[T];
Expand Down
34 changes: 16 additions & 18 deletions yarn-project/circuit-types/src/interfaces/server_circuit_prover.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
import {
type AvmProofAndVerificationKey,
type PublicInputsAndRecursiveProof,
type PublicInputsAndTubeProof,
type Tx,
} from '@aztec/circuit-types';
import {
type AvmCircuitInputs,
type BaseOrMergeRollupPublicInputs,
Expand All @@ -17,6 +11,7 @@ import {
type MergeRollupInputs,
type NESTED_RECURSIVE_PROOF_LENGTH,
type PrivateKernelEmptyInputData,
type Proof,
type PublicKernelCircuitPrivateInputs,
type PublicKernelCircuitPublicInputs,
type PublicKernelInnerCircuitPrivateInputs,
Expand All @@ -27,11 +22,14 @@ import {
type RootParityInputs,
type RootRollupInputs,
type RootRollupPublicInputs,
type TUBE_PROOF_LENGTH,
type TubeInputs,
type VMCircuitPublicInputs,
type VerificationKeyData,
} from '@aztec/circuits.js';

import type { Tx } from '../tx/tx.js';
import { type ProofAndVerificationKey, type PublicInputsAndRecursiveProof } from './proving-job.js';

/**
* Generates proofs for parity and rollup circuits.
*/
Expand Down Expand Up @@ -64,7 +62,7 @@ export interface ServerCircuitProver {
baseRollupInput: BaseRollupInputs,
signal?: AbortSignal,
epochNumber?: number,
): Promise<PublicInputsAndRecursiveProof<BaseOrMergeRollupPublicInputs>>;
): Promise<PublicInputsAndRecursiveProof<BaseOrMergeRollupPublicInputs, typeof RECURSIVE_PROOF_LENGTH>>;

/**
* Get a recursively verified client IVC proof (making it a compatible honk proof for the rest of the rollup).
Expand All @@ -74,7 +72,7 @@ export interface ServerCircuitProver {
tubeInput: TubeInputs,
signal?: AbortSignal,
epochNumber?: number,
): Promise<{ tubeVK: VerificationKeyData; tubeProof: RecursiveProof<typeof RECURSIVE_PROOF_LENGTH> }>;
): Promise<ProofAndVerificationKey<RecursiveProof<typeof RECURSIVE_PROOF_LENGTH>>>;

/**
* Creates a proof for the given input.
Expand All @@ -84,7 +82,7 @@ export interface ServerCircuitProver {
input: MergeRollupInputs,
signal?: AbortSignal,
epochNumber?: number,
): Promise<PublicInputsAndRecursiveProof<BaseOrMergeRollupPublicInputs>>;
): Promise<PublicInputsAndRecursiveProof<BaseOrMergeRollupPublicInputs, typeof RECURSIVE_PROOF_LENGTH>>;

/**
* Creates a proof for the given input.
Expand All @@ -94,7 +92,7 @@ export interface ServerCircuitProver {
input: BlockRootRollupInputs,
signal?: AbortSignal,
epochNumber?: number,
): Promise<PublicInputsAndRecursiveProof<BlockRootOrBlockMergePublicInputs>>;
): Promise<PublicInputsAndRecursiveProof<BlockRootOrBlockMergePublicInputs, typeof RECURSIVE_PROOF_LENGTH>>;

/**
* Creates a proof for the given input.
Expand All @@ -114,7 +112,7 @@ export interface ServerCircuitProver {
input: BlockMergeRollupInputs,
signal?: AbortSignal,
epochNumber?: number,
): Promise<PublicInputsAndRecursiveProof<BlockRootOrBlockMergePublicInputs>>;
): Promise<PublicInputsAndRecursiveProof<BlockRootOrBlockMergePublicInputs, typeof RECURSIVE_PROOF_LENGTH>>;

/**
* Creates a proof for the given input.
Expand All @@ -124,7 +122,7 @@ export interface ServerCircuitProver {
input: RootRollupInputs,
signal?: AbortSignal,
epochNumber?: number,
): Promise<PublicInputsAndRecursiveProof<RootRollupPublicInputs>>;
): Promise<PublicInputsAndRecursiveProof<RootRollupPublicInputs, typeof RECURSIVE_PROOF_LENGTH>>;

/**
* Create a public kernel inner proof.
Expand All @@ -140,25 +138,25 @@ export interface ServerCircuitProver {
inputs: PublicKernelCircuitPrivateInputs,
signal?: AbortSignal,
epochNumber?: number,
): Promise<PublicInputsAndRecursiveProof<PublicKernelCircuitPublicInputs>>;
): Promise<PublicInputsAndRecursiveProof<PublicKernelCircuitPublicInputs, typeof RECURSIVE_PROOF_LENGTH>>;

getPublicTailProof(
inputs: PublicKernelTailCircuitPrivateInputs,
signal?: AbortSignal,
epochNumber?: number,
): Promise<PublicInputsAndRecursiveProof<KernelCircuitPublicInputs>>;
): Promise<PublicInputsAndRecursiveProof<KernelCircuitPublicInputs, typeof RECURSIVE_PROOF_LENGTH>>;

getEmptyPrivateKernelProof(
inputs: PrivateKernelEmptyInputData,
signal?: AbortSignal,
epochNumber?: number,
): Promise<PublicInputsAndRecursiveProof<KernelCircuitPublicInputs>>;
): Promise<PublicInputsAndRecursiveProof<KernelCircuitPublicInputs, typeof RECURSIVE_PROOF_LENGTH>>;

getEmptyTubeProof(
inputs: PrivateKernelEmptyInputData,
signal?: AbortSignal,
epochNumber?: number,
): Promise<PublicInputsAndTubeProof<KernelCircuitPublicInputs>>;
): Promise<PublicInputsAndRecursiveProof<KernelCircuitPublicInputs, typeof TUBE_PROOF_LENGTH>>;

/**
* Create a proof for the AVM circuit.
Expand All @@ -168,7 +166,7 @@ export interface ServerCircuitProver {
inputs: AvmCircuitInputs,
signal?: AbortSignal,
epochNumber?: number,
): Promise<AvmProofAndVerificationKey>;
): Promise<ProofAndVerificationKey<Proof>>;
}

/**
Expand Down
3 changes: 1 addition & 2 deletions yarn-project/circuit-types/src/tx/processed_tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
EncryptedTxL2Logs,
PublicDataWrite,
type PublicInputsAndRecursiveProof,
type PublicInputsAndTubeProof,
type PublicKernelInnerRequest,
type PublicKernelMergeRequest,
type PublicKernelTailRequest,
Expand Down Expand Up @@ -198,7 +197,7 @@ export function makePaddingProcessedTx(
* @returns A valid padding processed tx.
*/
export function makePaddingProcessedTxFromTubeProof(
kernelOutput: PublicInputsAndTubeProof<KernelCircuitPublicInputs>,
kernelOutput: PublicInputsAndRecursiveProof<KernelCircuitPublicInputs, typeof TUBE_PROOF_LENGTH>,
): PaddingProcessedTxFromTube {
const hash = new TxHash(Fr.ZERO.toBuffer());
return {
Expand Down
3 changes: 2 additions & 1 deletion yarn-project/circuits.js/src/hash/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { Fr } from '@aztec/foundation/fields';
import { numToUInt8, numToUInt16BE, numToUInt32BE } from '@aztec/foundation/serialize';

import { GeneratorIndex } from '../constants.gen.js';
import { type ScopedL2ToL1Message, VerificationKey } from '../structs/index.js';
import { type ScopedL2ToL1Message } from '../structs/l2_to_l1_message.js';
import { VerificationKey } from '../structs/verification_key.js';

/**
* Computes a hash of a given verification key.
Expand Down
Loading

0 comments on commit da6c579

Please sign in to comment.