Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: browser chunking #11102

Merged
merged 23 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 6 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
4 changes: 2 additions & 2 deletions boxes/boxes/vite/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { KeyStore } from "@aztec/key-store";
import { PrivateKernelProver } from "@aztec/circuit-types";
import { L2TipsStore } from "@aztec/kv-store/stores";
import { createStore } from "@aztec/kv-store/indexeddb";
import { BBWasmPrivateKernelProver } from "@aztec/bb-prover/wasm";
import { BbWasmAsyncPrivateKernelProver } from "@aztec/bb-prover/wasm_async";

process.env = Object.keys(import.meta.env).reduce((acc, key) => {
acc[key.replace("VITE_", "")] = import.meta.env[key];
Expand All @@ -38,7 +38,7 @@ export class PrivateEnv {
const config = getPXEServiceConfig();
config.dataDirectory = "pxe";
const aztecNode = await createAztecNodeClient(this.nodeURL);
const proofCreator = new BBWasmPrivateKernelProver(16);
const proofCreator = new BbWasmAsyncPrivateKernelProver(16);
this.pxe = await this.createPXEService(aztecNode, config, proofCreator);
const encryptionPrivateKey = deriveMasterIncomingViewingSecretKey(
this.secretKey,
Expand Down
19 changes: 18 additions & 1 deletion boxes/boxes/vite/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineConfig } from "vite";
import { defineConfig, searchForWorkspaceRoot } from "vite";
import react from "@vitejs/plugin-react-swc";
import { PolyfillOptions, nodePolyfills } from "vite-plugin-node-polyfills";
import topLevelAwait from "vite-plugin-top-level-await";
Expand Down Expand Up @@ -28,12 +28,29 @@ export default defineConfig({
"Cross-Origin-Opener-Policy": "same-origin",
"Cross-Origin-Embedder-Policy": "require-corp",
},
fs: {
allow: [
searchForWorkspaceRoot(process.cwd()),
"../../../yarn-project/noir-protocol-circuits-types/artifacts",
],
},
},
plugins: [
react(),
nodePolyfillsFix({ include: ["buffer", "process", "path"] }),
topLevelAwait(),
],
build: {
rollupOptions: {
output: {
manualChunks(id: string) {
if (id.includes("bb-prover")) {
return "@aztec/bb-prover";
}
},
},
},
},
optimizeDeps: {
exclude: ["@noir-lang/acvm_js", "@noir-lang/noirc_abi", "@aztec/bb-prover"],
},
Expand Down
2 changes: 1 addition & 1 deletion noir-projects/Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ VERSION 0.8
test:
BUILD +test-protocol-circuits
BUILD +test-aztec-nr
# BUILD +test-contracts
BUILD +test-contracts

test-protocol-circuits:
FROM ../+bootstrap
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ builder/src/crs
builder/src/types
noir-protocol-circuits-types/artifacts
noir-protocol-circuits-types/src/private_kernel_reset_data.ts
noir-protocol-circuits-types/src/private_kernel_reset_vks.ts
noir-protocol-circuits-types/src/private_kernel_reset_types.ts
noir-protocol-circuits-types/src/client_artifacts_helper.ts
noir-protocol-circuits-types/src/types/
ivc-integration/artifacts
ivc-integration/src/types/
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec/src/cli/aztec_start_action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export async function aztecStart(options: any, userLog: LogFn, debugLogger: Logg
installSignalHandlers(debugLogger.info, signalHandlers);

if (Object.entries(services).length > 0) {
const rpcServer = createNamespacedSafeJsonRpcServer(services, debugLogger);
const rpcServer = createNamespacedSafeJsonRpcServer(services, false, debugLogger);
const { port } = await startHttpRpcServer(rpcServer, { port: options.port });
debugLogger.info(`Aztec Server listening on port ${port}`);
}
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec/src/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
getL1ContractsConfigEnvVars,
} from '@aztec/ethereum';
import { createLogger } from '@aztec/foundation/log';
import { getVKTreeRoot } from '@aztec/noir-protocol-circuits-types';
import { getVKTreeRoot } from '@aztec/noir-protocol-circuits-types/vks';
import { ProtocolContractAddress, protocolContractTreeRoot } from '@aztec/protocol-contracts';
import { type PXEServiceConfig, createPXEService, getPXEServiceConfig } from '@aztec/pxe';
import { type TelemetryClient } from '@aztec/telemetry-client';
Expand Down
3 changes: 2 additions & 1 deletion yarn-project/bb-prover/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"type": "module",
"exports": {
".": "./dest/index.js",
"./wasm": "./dest/wasm/index.js",
"./wasm": "./dest/wasm/sync.js",
"./wasm_async": "./dest/wasm/async.js",
"./prover": "./dest/prover/index.js",
"./verifier": "./dest/verifier/index.js",
"./test": "./dest/test/index.js",
Expand Down
10 changes: 9 additions & 1 deletion yarn-project/bb-prover/src/bb/cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { type LogFn } from '@aztec/foundation/log';
import { type ProtocolArtifact, ProtocolCircuitArtifacts } from '@aztec/noir-protocol-circuits-types';
import { ClientCircuitArtifacts } from '@aztec/noir-protocol-circuits-types/client';
import { ServerCircuitArtifacts } from '@aztec/noir-protocol-circuits-types/server';
import { type ProtocolArtifact } from '@aztec/noir-protocol-circuits-types/types';
import { type NoirCompiledCircuit } from '@aztec/types/noir';

import { Command } from 'commander';
import { promises as fs } from 'fs';
Expand All @@ -8,6 +11,11 @@ import { generateContractForCircuit, generateKeyForNoirCircuit } from './execute

const { BB_WORKING_DIRECTORY, BB_BINARY_PATH } = process.env;

export const ProtocolCircuitArtifacts: Record<ProtocolArtifact, NoirCompiledCircuit> = {
...ClientCircuitArtifacts,
...ServerCircuitArtifacts,
};

/**
* Returns commander program that defines the CLI.
* @param log - Console logger.
Expand Down
32 changes: 16 additions & 16 deletions yarn-project/bb-prover/src/honk.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
import { type ProtocolArtifact } from '@aztec/noir-protocol-circuits-types';
import { type ServerProtocolArtifact } from '@aztec/noir-protocol-circuits-types/server';

export type UltraHonkFlavor = 'ultra_honk' | 'ultra_keccak_honk' | 'ultra_rollup_honk';

const UltraKeccakHonkCircuits = ['RootRollupArtifact'] as const satisfies ProtocolArtifact[];
const UltraKeccakHonkCircuits = ['RootRollupArtifact'] as const satisfies ServerProtocolArtifact[];
const UltraHonkCircuits = [
// 'EmptyNestedArtifact',
// 'PrivateKernelEmptyArtifact',
'BaseParityArtifact',
'RootParityArtifact',
] as const satisfies ProtocolArtifact[];
] as const satisfies ServerProtocolArtifact[];

export type UltraKeccakHonkProtocolArtifact = (typeof UltraKeccakHonkCircuits)[number];
export type UltraHonkProtocolArtifact = (typeof UltraHonkCircuits)[number];
export type UltraRollupHonkProtocolArtifact = Exclude<
Exclude<ProtocolArtifact, UltraKeccakHonkProtocolArtifact>,
UltraHonkProtocolArtifact
export type UltraKeccakHonkServerProtocolArtifact = (typeof UltraKeccakHonkCircuits)[number];
export type UltraHonkServerProtocolArtifact = (typeof UltraHonkCircuits)[number];
export type UltraRollupHonkServerProtocolArtifact = Exclude<
Exclude<ServerProtocolArtifact, UltraKeccakHonkServerProtocolArtifact>,
UltraHonkServerProtocolArtifact
>;

export function getUltraHonkFlavorForCircuit(artifact: UltraKeccakHonkProtocolArtifact): 'ultra_keccak_honk';
export function getUltraHonkFlavorForCircuit(artifact: UltraHonkProtocolArtifact): 'ultra_honk';
export function getUltraHonkFlavorForCircuit(artifact: UltraRollupHonkProtocolArtifact): 'ultra_rollup_honk';
export function getUltraHonkFlavorForCircuit(artifact: ProtocolArtifact): UltraHonkFlavor;
export function getUltraHonkFlavorForCircuit(artifact: ProtocolArtifact): UltraHonkFlavor {
export function getUltraHonkFlavorForCircuit(artifact: UltraKeccakHonkServerProtocolArtifact): 'ultra_keccak_honk';
export function getUltraHonkFlavorForCircuit(artifact: UltraHonkServerProtocolArtifact): 'ultra_honk';
export function getUltraHonkFlavorForCircuit(artifact: UltraRollupHonkServerProtocolArtifact): 'ultra_rollup_honk';
export function getUltraHonkFlavorForCircuit(artifact: ServerProtocolArtifact): UltraHonkFlavor;
export function getUltraHonkFlavorForCircuit(artifact: ServerProtocolArtifact): UltraHonkFlavor {
if (isUltraKeccakHonkCircuit(artifact)) {
return 'ultra_keccak_honk';
} else if (UltraHonkCircuits.includes(artifact as UltraHonkProtocolArtifact)) {
} else if (UltraHonkCircuits.includes(artifact as UltraHonkServerProtocolArtifact)) {
return 'ultra_honk';
}
return 'ultra_rollup_honk';
}

function isUltraKeccakHonkCircuit(artifact: ProtocolArtifact): artifact is UltraKeccakHonkProtocolArtifact {
return UltraKeccakHonkCircuits.includes(artifact as UltraKeccakHonkProtocolArtifact);
function isUltraKeccakHonkCircuit(artifact: ServerProtocolArtifact): artifact is UltraKeccakHonkServerProtocolArtifact {
return UltraKeccakHonkCircuits.includes(artifact as UltraKeccakHonkServerProtocolArtifact);
}
55 changes: 3 additions & 52 deletions yarn-project/bb-prover/src/prover/bb_private_kernel_prover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@ import {
type PrivateKernelResetCircuitPrivateInputs,
type PrivateKernelTailCircuitPrivateInputs,
type PrivateKernelTailCircuitPublicInputs,
type Proof,
type VerificationKeyData,
} from '@aztec/circuits.js';
import { runInDirectory } from '@aztec/foundation/fs';
import { type Logger, createLogger } from '@aztec/foundation/log';
import { Timer } from '@aztec/foundation/timer';
import {
ClientCircuitArtifacts,
ClientCircuitVks,
type ClientProtocolArtifact,
ProtocolCircuitVks,
convertPrivateKernelInitInputsToWitnessMap,
convertPrivateKernelInitOutputsFromWitnessMap,
convertPrivateKernelInnerInputsToWitnessMap,
Expand All @@ -30,7 +27,8 @@ import {
convertPrivateKernelTailOutputsFromWitnessMap,
convertPrivateKernelTailToPublicInputsToWitnessMap,
getPrivateKernelResetArtifactName,
} from '@aztec/noir-protocol-circuits-types';
} from '@aztec/noir-protocol-circuits-types/client';
import { ClientCircuitVks } from '@aztec/noir-protocol-circuits-types/vks';
import { WASMSimulatorWithBlobs } from '@aztec/simulator';
import { type NoirCompiledCircuit } from '@aztec/types/noir';

Expand All @@ -40,9 +38,8 @@ import { type WitnessMap } from '@noir-lang/types';
import { promises as fs } from 'fs';
import path from 'path';

import { BB_RESULT, computeGateCountForCircuit, executeBbClientIvcProof, verifyProof } from '../bb/execute.js';
import { BB_RESULT, computeGateCountForCircuit, executeBbClientIvcProof } from '../bb/execute.js';
import { type BBConfig } from '../config.js';
import { type UltraHonkFlavor, getUltraHonkFlavorForCircuit } from '../honk.js';
import { mapProtocolArtifactNameToCircuitName } from '../stats.js';
import { extractVkData } from '../verification_key/verification_key_data.js';
import { readFromOutputDirectory } from './client_ivc_proof_utils.js';
Expand Down Expand Up @@ -169,35 +166,6 @@ export class BBNativePrivateKernelProver implements PrivateKernelProver {
);
}

/**
* Verifies a proof, will generate the verification key if one is not cached internally
* @param circuitType - The type of circuit whose proof is to be verified
* @param proof - The proof to be verified
*/
public async verifyProofForProtocolCircuit(circuitType: ClientProtocolArtifact, proof: Proof) {
const verificationKey = ProtocolCircuitVks[circuitType];

this.log.debug(`Verifying with key: ${verificationKey.keyAsFields.hash.toString()}`);

const logFunction = (message: string) => {
this.log.debug(`${circuitType} BB out - ${message}`);
};

const result = await this.verifyProofFromKey(
getUltraHonkFlavorForCircuit(circuitType),
verificationKey.keyAsBytes,
proof,
logFunction,
);

if (result.status === BB_RESULT.FAILURE) {
const errorMessage = `Failed to verify ${circuitType} proof!`;
throw new Error(errorMessage);
}

this.log.info(`Successfully verified ${circuitType} proof in ${Math.ceil(result.durationMs)} ms`);
}

public async computeGateCountForCircuit(bytecode: Buffer, circuitName: string): Promise<number> {
const logFunction = (message: string) => {
this.log.debug(`$bb gates ${circuitName} - ${message}`);
Expand All @@ -218,23 +186,6 @@ export class BBNativePrivateKernelProver implements PrivateKernelProver {
return result.circuitSize as number;
}

private async verifyProofFromKey(
flavor: UltraHonkFlavor,
verificationKey: Buffer,
proof: Proof,
logFunction: (message: string) => void = () => {},
) {
const operation = async (bbWorkingDirectory: string) => {
const proofFileName = `${bbWorkingDirectory}/proof`;
const verificationKeyPath = `${bbWorkingDirectory}/vk`;

await fs.writeFile(proofFileName, proof.buffer);
await fs.writeFile(verificationKeyPath, verificationKey);
return await verifyProof(this.bbBinaryPath, proofFileName, verificationKeyPath!, flavor, logFunction);
};
return await this.runInDirectory(operation);
}

/**
* Ensures our verification key cache includes the key data located at the specified directory
* @param filePath - The directory containing the verification key data files
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/bb-prover/src/prover/bb_prover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import { createLogger } from '@aztec/foundation/log';
import { BufferReader } from '@aztec/foundation/serialize';
import { Timer } from '@aztec/foundation/timer';
import {
ProtocolCircuitVks,
ServerCircuitArtifacts,
type ServerProtocolArtifact,
convertBaseParityInputsToWitnessMap,
Expand All @@ -74,7 +73,8 @@ import {
convertRootParityOutputsFromWitnessMap,
convertRootRollupInputsToWitnessMap,
convertRootRollupOutputsFromWitnessMap,
} from '@aztec/noir-protocol-circuits-types';
} from '@aztec/noir-protocol-circuits-types/server';
import { ProtocolCircuitVks } from '@aztec/noir-protocol-circuits-types/vks';
import { NativeACVMSimulator } from '@aztec/simulator';
import { Attributes, type TelemetryClient, trackSpan } from '@aztec/telemetry-client';

Expand Down
8 changes: 3 additions & 5 deletions yarn-project/bb-prover/src/stats.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import type { CircuitName } from '@aztec/circuit-types/stats';
import { type ClientProtocolArtifact, type ServerProtocolArtifact } from '@aztec/noir-protocol-circuits-types';
import { type ProtocolArtifact } from '@aztec/noir-protocol-circuits-types/types';

export function mapProtocolArtifactNameToCircuitName(
artifact: ServerProtocolArtifact | ClientProtocolArtifact,
): CircuitName {
export function mapProtocolArtifactNameToCircuitName(artifact: ProtocolArtifact): CircuitName {
switch (artifact) {
case 'BaseParityArtifact':
return 'base-parity';
Expand Down Expand Up @@ -44,7 +42,7 @@ export function mapProtocolArtifactNameToCircuitName(
}
}

export function isProtocolArtifactRecursive(artifact: ServerProtocolArtifact | ClientProtocolArtifact): boolean {
export function isProtocolArtifactRecursive(artifact: ProtocolArtifact): boolean {
switch (artifact) {
case 'EmptyNestedArtifact':
case 'PrivateKernelEmptyArtifact':
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/bb-prover/src/test/test_circuit_prover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import { createLogger } from '@aztec/foundation/log';
import { sleep } from '@aztec/foundation/sleep';
import { Timer } from '@aztec/foundation/timer';
import {
ProtocolCircuitVks,
type ServerProtocolArtifact,
SimulatedServerCircuitArtifacts,
convertBaseParityInputsToWitnessMap,
Expand All @@ -65,7 +64,8 @@ import {
convertSimulatedPrivateKernelEmptyOutputsFromWitnessMap,
convertSimulatedPublicBaseRollupInputsToWitnessMap,
convertSimulatedPublicBaseRollupOutputsFromWitnessMap,
} from '@aztec/noir-protocol-circuits-types';
} from '@aztec/noir-protocol-circuits-types/server';
import { ProtocolCircuitVks } from '@aztec/noir-protocol-circuits-types/vks';
import { type SimulationProvider, WASMSimulatorWithBlobs, emitCircuitSimulationStats } from '@aztec/simulator';
import { type TelemetryClient, trackSpan } from '@aztec/telemetry-client';

Expand Down
Loading
Loading