@aztec/bb-prover 0.39.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/dest/bb/cli.d.ts +9 -0
  2. package/dest/bb/cli.d.ts.map +1 -0
  3. package/dest/bb/cli.js +64 -0
  4. package/dest/bb/execute.d.ts +88 -0
  5. package/dest/bb/execute.d.ts.map +1 -0
  6. package/dest/bb/execute.js +288 -0
  7. package/dest/bb/index.d.ts +3 -0
  8. package/dest/bb/index.d.ts.map +1 -0
  9. package/dest/bb/index.js +18 -0
  10. package/dest/index.d.ts +3 -0
  11. package/dest/index.d.ts.map +1 -0
  12. package/dest/index.js +3 -0
  13. package/dest/mappings/mappings.d.ts +12 -0
  14. package/dest/mappings/mappings.d.ts.map +1 -0
  15. package/dest/mappings/mappings.js +22 -0
  16. package/dest/prover/bb_native_proof_creator.d.ts +58 -0
  17. package/dest/prover/bb_native_proof_creator.d.ts.map +1 -0
  18. package/dest/prover/bb_native_proof_creator.js +238 -0
  19. package/dest/prover/bb_prover.d.ts +110 -0
  20. package/dest/prover/bb_prover.d.ts.map +1 -0
  21. package/dest/prover/bb_prover.js +321 -0
  22. package/dest/prover/index.d.ts +3 -0
  23. package/dest/prover/index.d.ts.map +1 -0
  24. package/dest/prover/index.js +3 -0
  25. package/dest/prover/verification_key_data.d.ts +16 -0
  26. package/dest/prover/verification_key_data.d.ts.map +1 -0
  27. package/dest/prover/verification_key_data.js +5 -0
  28. package/dest/stats.d.ts +9 -0
  29. package/dest/stats.d.ts.map +1 -0
  30. package/dest/stats.js +61 -0
  31. package/dest/test/index.d.ts +2 -0
  32. package/dest/test/index.d.ts.map +1 -0
  33. package/dest/test/index.js +2 -0
  34. package/dest/test/test_circuit_prover.d.ts +48 -0
  35. package/dest/test/test_circuit_prover.d.ts.map +1 -0
  36. package/dest/test/test_circuit_prover.js +128 -0
  37. package/package.json +85 -0
  38. package/src/bb/cli.ts +92 -0
  39. package/src/bb/execute.ts +359 -0
  40. package/src/bb/index.ts +23 -0
  41. package/src/index.ts +2 -0
  42. package/src/mappings/mappings.ts +41 -0
  43. package/src/prover/bb_native_proof_creator.ts +382 -0
  44. package/src/prover/bb_prover.ts +557 -0
  45. package/src/prover/index.ts +2 -0
  46. package/src/prover/verification_key_data.ts +16 -0
  47. package/src/stats.ts +82 -0
  48. package/src/test/index.ts +1 -0
  49. package/src/test/test_circuit_prover.ts +268 -0
@@ -0,0 +1,557 @@
1
+ /* eslint-disable require-await */
2
+ import {
3
+ type PublicInputsAndProof,
4
+ type PublicKernelNonTailRequest,
5
+ type PublicKernelTailRequest,
6
+ PublicKernelType,
7
+ type ServerCircuitProver,
8
+ makePublicInputsAndProof,
9
+ } from '@aztec/circuit-types';
10
+ import {
11
+ type BaseOrMergeRollupPublicInputs,
12
+ type BaseParityInputs,
13
+ type BaseRollupInputs,
14
+ Fr,
15
+ type KernelCircuitPublicInputs,
16
+ type MergeRollupInputs,
17
+ type NESTED_RECURSIVE_PROOF_LENGTH,
18
+ type ParityPublicInputs,
19
+ type PreviousRollupData,
20
+ Proof,
21
+ type PublicKernelCircuitPublicInputs,
22
+ type RECURSIVE_PROOF_LENGTH,
23
+ RecursiveProof,
24
+ RollupTypes,
25
+ RootParityInput,
26
+ type RootParityInputs,
27
+ type RootRollupInputs,
28
+ type RootRollupPublicInputs,
29
+ type VERIFICATION_KEY_LENGTH_IN_FIELDS,
30
+ VerificationKeyAsFields,
31
+ } from '@aztec/circuits.js';
32
+ import { randomBytes } from '@aztec/foundation/crypto';
33
+ import { createDebugLogger } from '@aztec/foundation/log';
34
+ import { type Tuple } from '@aztec/foundation/serialize';
35
+ import { Timer } from '@aztec/foundation/timer';
36
+ import {
37
+ ServerCircuitArtifacts,
38
+ type ServerProtocolArtifact,
39
+ convertBaseParityInputsToWitnessMap,
40
+ convertBaseParityOutputsFromWitnessMap,
41
+ convertBaseRollupInputsToWitnessMap,
42
+ convertBaseRollupOutputsFromWitnessMap,
43
+ convertMergeRollupInputsToWitnessMap,
44
+ convertMergeRollupOutputsFromWitnessMap,
45
+ convertPublicTailInputsToWitnessMap,
46
+ convertPublicTailOutputFromWitnessMap,
47
+ convertRootParityInputsToWitnessMap,
48
+ convertRootParityOutputsFromWitnessMap,
49
+ convertRootRollupInputsToWitnessMap,
50
+ convertRootRollupOutputsFromWitnessMap,
51
+ } from '@aztec/noir-protocol-circuits-types';
52
+ import { NativeACVMSimulator } from '@aztec/simulator';
53
+
54
+ import { type WitnessMap } from '@noir-lang/types';
55
+ import * as fs from 'fs/promises';
56
+
57
+ import {
58
+ BB_RESULT,
59
+ PROOF_FIELDS_FILENAME,
60
+ PROOF_FILENAME,
61
+ VK_FIELDS_FILENAME,
62
+ VK_FILENAME,
63
+ generateKeyForNoirCircuit,
64
+ generateProof,
65
+ verifyProof,
66
+ } from '../bb/execute.js';
67
+ import { PublicKernelArtifactMapping } from '../mappings/mappings.js';
68
+ import { circuitTypeToCircuitName, emitCircuitProvingStats, emitCircuitWitnessGenerationStats } from '../stats.js';
69
+ import {
70
+ AGGREGATION_OBJECT_SIZE,
71
+ CIRCUIT_PUBLIC_INPUTS_INDEX,
72
+ CIRCUIT_RECURSIVE_INDEX,
73
+ CIRCUIT_SIZE_INDEX,
74
+ type VerificationKeyData,
75
+ } from './verification_key_data.js';
76
+
77
+ const logger = createDebugLogger('aztec:bb-prover');
78
+
79
+ const CIRCUITS_WITHOUT_AGGREGATION: Set<ServerProtocolArtifact> = new Set(['BaseParityArtifact']);
80
+
81
+ export type BBProverConfig = {
82
+ bbBinaryPath: string;
83
+ bbWorkingDirectory: string;
84
+ acvmBinaryPath: string;
85
+ acvmWorkingDirectory: string;
86
+ // list of circuits supported by this prover. defaults to all circuits if empty
87
+ circuitFilter?: ServerProtocolArtifact[];
88
+ };
89
+
90
+ /**
91
+ * Prover implementation that uses barretenberg native proving
92
+ */
93
+ export class BBNativeRollupProver implements ServerCircuitProver {
94
+ private verificationKeys: Map<ServerProtocolArtifact, Promise<VerificationKeyData>> = new Map<
95
+ ServerProtocolArtifact,
96
+ Promise<VerificationKeyData>
97
+ >();
98
+ constructor(private config: BBProverConfig) {}
99
+
100
+ static async new(config: BBProverConfig) {
101
+ await fs.access(config.acvmBinaryPath, fs.constants.R_OK);
102
+ await fs.mkdir(config.acvmWorkingDirectory, { recursive: true });
103
+ await fs.access(config.bbBinaryPath, fs.constants.R_OK);
104
+ await fs.mkdir(config.bbWorkingDirectory, { recursive: true });
105
+ logger.info(`Using native BB at ${config.bbBinaryPath} and working directory ${config.bbWorkingDirectory}`);
106
+ logger.info(`Using native ACVM at ${config.acvmBinaryPath} and working directory ${config.acvmWorkingDirectory}`);
107
+
108
+ return new BBNativeRollupProver(config);
109
+ }
110
+
111
+ /**
112
+ * Simulates the base parity circuit from its inputs.
113
+ * @param inputs - Inputs to the circuit.
114
+ * @returns The public inputs of the parity circuit.
115
+ */
116
+ public async getBaseParityProof(inputs: BaseParityInputs): Promise<RootParityInput<typeof RECURSIVE_PROOF_LENGTH>> {
117
+ const witnessMap = convertBaseParityInputsToWitnessMap(inputs);
118
+
119
+ const [circuitOutput, proof] = await this.createRecursiveProof<typeof RECURSIVE_PROOF_LENGTH, ParityPublicInputs>(
120
+ witnessMap,
121
+ 'BaseParityArtifact',
122
+ convertBaseParityOutputsFromWitnessMap,
123
+ );
124
+
125
+ const verificationKey = await this.getVerificationKeyDataForCircuit('BaseParityArtifact');
126
+
127
+ const vk = new VerificationKeyAsFields(verificationKey.keyAsFields, verificationKey.hash);
128
+
129
+ return new RootParityInput(proof, vk, circuitOutput);
130
+ }
131
+
132
+ /**
133
+ * Simulates the root parity circuit from its inputs.
134
+ * @param inputs - Inputs to the circuit.
135
+ * @returns The public inputs of the parity circuit.
136
+ */
137
+ public async getRootParityProof(
138
+ inputs: RootParityInputs,
139
+ ): Promise<RootParityInput<typeof NESTED_RECURSIVE_PROOF_LENGTH>> {
140
+ const witnessMap = convertRootParityInputsToWitnessMap(inputs);
141
+
142
+ const [circuitOutput, proof] = await this.createRecursiveProof<
143
+ typeof NESTED_RECURSIVE_PROOF_LENGTH,
144
+ ParityPublicInputs
145
+ >(witnessMap, 'RootParityArtifact', convertRootParityOutputsFromWitnessMap);
146
+
147
+ const verificationKey = await this.getVerificationKeyDataForCircuit('RootParityArtifact');
148
+
149
+ const vk = new VerificationKeyAsFields(verificationKey.keyAsFields, verificationKey.hash);
150
+
151
+ return new RootParityInput(proof, vk, circuitOutput);
152
+ }
153
+
154
+ /**
155
+ * Requests that a public kernel circuit be executed and the proof generated
156
+ * @param kernelRequest - The object encapsulating the request for a proof
157
+ * @returns The requested circuit's public inputs and proof
158
+ */
159
+ public async getPublicKernelProof(
160
+ kernelRequest: PublicKernelNonTailRequest,
161
+ ): Promise<PublicInputsAndProof<PublicKernelCircuitPublicInputs>> {
162
+ const kernelOps = PublicKernelArtifactMapping[kernelRequest.type];
163
+ if (kernelOps === undefined) {
164
+ throw new Error(`Unable to prove kernel type ${PublicKernelType[kernelRequest.type]}`);
165
+ }
166
+ const witnessMap = kernelOps.convertInputs(kernelRequest.inputs);
167
+
168
+ const [outputWitness, proof] = await this.createProof(witnessMap, kernelOps.artifact);
169
+
170
+ const result = kernelOps.convertOutputs(outputWitness);
171
+ return makePublicInputsAndProof(result, proof);
172
+ }
173
+
174
+ /**
175
+ * Requests that the public kernel tail circuit be executed and the proof generated
176
+ * @param kernelRequest - The object encapsulating the request for a proof
177
+ * @returns The requested circuit's public inputs and proof
178
+ */
179
+ public async getPublicTailProof(
180
+ kernelRequest: PublicKernelTailRequest,
181
+ ): Promise<PublicInputsAndProof<KernelCircuitPublicInputs>> {
182
+ const witnessMap = convertPublicTailInputsToWitnessMap(kernelRequest.inputs);
183
+
184
+ const [outputWitness, proof] = await this.createProof(witnessMap, 'PublicKernelTailArtifact');
185
+
186
+ const result = convertPublicTailOutputFromWitnessMap(outputWitness);
187
+ return makePublicInputsAndProof(result, proof);
188
+ }
189
+
190
+ /**
191
+ * Simulates the base rollup circuit from its inputs.
192
+ * @param input - Inputs to the circuit.
193
+ * @returns The public inputs as outputs of the simulation.
194
+ */
195
+ public async getBaseRollupProof(
196
+ input: BaseRollupInputs,
197
+ ): Promise<PublicInputsAndProof<BaseOrMergeRollupPublicInputs>> {
198
+ const witnessMap = convertBaseRollupInputsToWitnessMap(input);
199
+
200
+ const [outputWitness, proof] = await this.createProof(witnessMap, 'BaseRollupArtifact');
201
+
202
+ const result = convertBaseRollupOutputsFromWitnessMap(outputWitness);
203
+
204
+ return makePublicInputsAndProof(result, proof);
205
+ }
206
+ /**
207
+ * Simulates the merge rollup circuit from its inputs.
208
+ * @param input - Inputs to the circuit.
209
+ * @returns The public inputs as outputs of the simulation.
210
+ */
211
+ public async getMergeRollupProof(
212
+ input: MergeRollupInputs,
213
+ ): Promise<PublicInputsAndProof<BaseOrMergeRollupPublicInputs>> {
214
+ // verify both inputs
215
+ await Promise.all(input.previousRollupData.map(prev => this.verifyPreviousRollupProof(prev)));
216
+
217
+ const witnessMap = convertMergeRollupInputsToWitnessMap(input);
218
+
219
+ const [outputWitness, proof] = await this.createProof(witnessMap, 'MergeRollupArtifact');
220
+
221
+ const result = convertMergeRollupOutputsFromWitnessMap(outputWitness);
222
+
223
+ return makePublicInputsAndProof(result, proof);
224
+ }
225
+
226
+ /**
227
+ * Simulates the root rollup circuit from its inputs.
228
+ * @param input - Inputs to the circuit.
229
+ * @returns The public inputs as outputs of the simulation.
230
+ */
231
+ public async getRootRollupProof(input: RootRollupInputs): Promise<PublicInputsAndProof<RootRollupPublicInputs>> {
232
+ // verify the inputs
233
+ await Promise.all(input.previousRollupData.map(prev => this.verifyPreviousRollupProof(prev)));
234
+
235
+ const witnessMap = convertRootRollupInputsToWitnessMap(input);
236
+
237
+ const [outputWitness, proof] = await this.createProof(witnessMap, 'RootRollupArtifact');
238
+
239
+ await this.verifyProof('RootRollupArtifact', proof);
240
+
241
+ const result = convertRootRollupOutputsFromWitnessMap(outputWitness);
242
+ return makePublicInputsAndProof(result, proof);
243
+ }
244
+
245
+ // TODO(@PhilWindle): Delete when no longer required
246
+ public async createProof(witnessMap: WitnessMap, circuitType: ServerProtocolArtifact): Promise<[WitnessMap, Proof]> {
247
+ // Create random directory to be used for temp files
248
+ const bbWorkingDirectory = `${this.config.bbWorkingDirectory}/${randomBytes(8).toString('hex')}`;
249
+ await fs.mkdir(bbWorkingDirectory, { recursive: true });
250
+
251
+ await fs.access(bbWorkingDirectory);
252
+
253
+ // Have the ACVM write the partial witness here
254
+ const outputWitnessFile = `${bbWorkingDirectory}/partial-witness.gz`;
255
+
256
+ // Generate the partial witness using the ACVM
257
+ // A further temp directory will be created beneath ours and then cleaned up after the partial witness has been copied to our specified location
258
+ const simulator = new NativeACVMSimulator(
259
+ this.config.acvmWorkingDirectory,
260
+ this.config.acvmBinaryPath,
261
+ outputWitnessFile,
262
+ );
263
+
264
+ const artifact = ServerCircuitArtifacts[circuitType];
265
+
266
+ logger.debug(`Generating witness data for ${circuitType}`);
267
+
268
+ const timer = new Timer();
269
+ const outputWitness = await simulator.simulateCircuit(witnessMap, artifact);
270
+ emitCircuitWitnessGenerationStats(
271
+ circuitTypeToCircuitName(circuitType),
272
+ timer.ms(),
273
+ witnessMap.size * Fr.SIZE_IN_BYTES,
274
+ outputWitness.size * Fr.SIZE_IN_BYTES,
275
+ logger,
276
+ );
277
+
278
+ // Now prove the circuit from the generated witness
279
+ logger.debug(`Proving ${circuitType}...`);
280
+
281
+ const provingResult = await generateProof(
282
+ this.config.bbBinaryPath,
283
+ bbWorkingDirectory,
284
+ circuitType,
285
+ Buffer.from(artifact.bytecode, 'base64'),
286
+ outputWitnessFile,
287
+ logger.debug,
288
+ );
289
+
290
+ if (provingResult.status === BB_RESULT.FAILURE) {
291
+ logger.error(`Failed to generate proof for ${circuitType}: ${provingResult.reason}`);
292
+ throw new Error(provingResult.reason);
293
+ }
294
+
295
+ // Ensure our vk cache is up to date
296
+ await this.updateVerificationKeyAfterProof(provingResult.vkPath!, circuitType);
297
+
298
+ // Read the proof and then cleanup up our temporary directory
299
+ const proof = await fs.readFile(`${provingResult.proofPath!}/${PROOF_FILENAME}`);
300
+
301
+ // does not include reading the proof from disk above because duration comes from the bb wrapper
302
+ emitCircuitProvingStats(
303
+ circuitTypeToCircuitName(circuitType),
304
+ provingResult.duration,
305
+ witnessMap.size * Fr.SIZE_IN_BYTES,
306
+ outputWitness.size * Fr.SIZE_IN_BYTES,
307
+ proof.length,
308
+ logger,
309
+ );
310
+
311
+ await fs.rm(bbWorkingDirectory, { recursive: true, force: true });
312
+
313
+ logger.info(`Generated proof for ${circuitType} in ${provingResult.duration} ms, size: ${proof.length} fields`);
314
+
315
+ return [outputWitness, new Proof(proof)];
316
+ }
317
+
318
+ /**
319
+ * Executes a circuit and returns it's outputs and corresponding proof with embedded aggregation object
320
+ * @param witnessMap - The input witness
321
+ * @param circuitType - The type of circuit to be executed
322
+ * @param convertOutput - Function for parsing the output witness to it's corresponding object
323
+ * @returns The circuits output object and it's proof
324
+ */
325
+ public async createRecursiveProof<PROOF_LENGTH extends number, CircuitOutputType>(
326
+ witnessMap: WitnessMap,
327
+ circuitType: ServerProtocolArtifact,
328
+ convertOutput: (outputWitness: WitnessMap) => CircuitOutputType,
329
+ ): Promise<[CircuitOutputType, RecursiveProof<PROOF_LENGTH>]> {
330
+ // Create random directory to be used for temp files
331
+ const bbWorkingDirectory = `${this.config.bbWorkingDirectory}/${randomBytes(8).toString('hex')}`;
332
+ await fs.mkdir(bbWorkingDirectory, { recursive: true });
333
+
334
+ await fs.access(bbWorkingDirectory);
335
+
336
+ try {
337
+ // Have the ACVM write the partial witness here
338
+ const outputWitnessFile = `${bbWorkingDirectory}/partial-witness.gz`;
339
+
340
+ // Generate the partial witness using the ACVM
341
+ // A further temp directory will be created beneath ours and then cleaned up after the partial witness has been copied to our specified location
342
+ const simulator = new NativeACVMSimulator(
343
+ this.config.acvmWorkingDirectory,
344
+ this.config.acvmBinaryPath,
345
+ outputWitnessFile,
346
+ );
347
+
348
+ const artifact = ServerCircuitArtifacts[circuitType];
349
+
350
+ logger.debug(`Generating witness data for ${circuitType}`);
351
+
352
+ const timer = new Timer();
353
+ const outputWitness = await simulator.simulateCircuit(witnessMap, artifact);
354
+
355
+ emitCircuitWitnessGenerationStats(
356
+ circuitTypeToCircuitName(circuitType),
357
+ timer.ms(),
358
+ witnessMap.size * Fr.SIZE_IN_BYTES,
359
+ outputWitness.size * Fr.SIZE_IN_BYTES,
360
+ logger,
361
+ );
362
+
363
+ const outputType = convertOutput(outputWitness);
364
+
365
+ // Now prove the circuit from the generated witness
366
+ logger.debug(`Proving ${circuitType}...`);
367
+
368
+ const provingResult = await generateProof(
369
+ this.config.bbBinaryPath,
370
+ bbWorkingDirectory,
371
+ circuitType,
372
+ Buffer.from(artifact.bytecode, 'base64'),
373
+ outputWitnessFile,
374
+ logger.debug,
375
+ );
376
+
377
+ if (provingResult.status === BB_RESULT.FAILURE) {
378
+ logger.error(`Failed to generate proof for ${circuitType}: ${provingResult.reason}`);
379
+ throw new Error(provingResult.reason);
380
+ }
381
+
382
+ // Ensure our vk cache is up to date
383
+ await this.updateVerificationKeyAfterProof(provingResult.vkPath!, circuitType);
384
+
385
+ // Read the proof and then cleanup up our temporary directory
386
+ const proof = await this.readProofAsFields<PROOF_LENGTH>(provingResult.proofPath!, circuitType);
387
+
388
+ logger.info(
389
+ `Generated proof for ${circuitType} in ${provingResult.duration} ms, size: ${proof.proof.length} fields`,
390
+ );
391
+
392
+ emitCircuitProvingStats(
393
+ circuitTypeToCircuitName(circuitType),
394
+ provingResult.duration,
395
+ witnessMap.size * Fr.SIZE_IN_BYTES,
396
+ outputWitness.size * Fr.SIZE_IN_BYTES,
397
+ proof.binaryProof.buffer.length,
398
+ logger,
399
+ );
400
+
401
+ return [outputType, proof];
402
+ } finally {
403
+ await fs.rm(bbWorkingDirectory, { recursive: true, force: true });
404
+ }
405
+ }
406
+
407
+ /**
408
+ * Verifies a proof, will generate the verification key if one is not cached internally
409
+ * @param circuitType - The type of circuit whose proof is to be verified
410
+ * @param proof - The proof to be verified
411
+ */
412
+ public async verifyProof(circuitType: ServerProtocolArtifact, proof: Proof) {
413
+ // Create random directory to be used for temp files
414
+ const bbWorkingDirectory = `${this.config.bbWorkingDirectory}/${randomBytes(8).toString('hex')}`;
415
+ await fs.mkdir(bbWorkingDirectory, { recursive: true });
416
+
417
+ const proofFileName = `${bbWorkingDirectory}/proof`;
418
+ const verificationKeyPath = `${bbWorkingDirectory}/vk`;
419
+ const verificationKey = await this.getVerificationKeyDataForCircuit(circuitType);
420
+
421
+ logger.debug(`Verifying with key: ${verificationKey.hash.toString()}`);
422
+
423
+ await fs.writeFile(proofFileName, proof.buffer);
424
+ await fs.writeFile(verificationKeyPath, verificationKey.keyAsBytes);
425
+
426
+ const logFunction = (message: string) => {
427
+ logger.debug(`${circuitType} BB out - ${message}`);
428
+ };
429
+
430
+ const result = await verifyProof(this.config.bbBinaryPath, proofFileName, verificationKeyPath!, logFunction);
431
+
432
+ await fs.rm(bbWorkingDirectory, { recursive: true, force: true });
433
+
434
+ if (result.status === BB_RESULT.FAILURE) {
435
+ const errorMessage = `Failed to verify ${circuitType} proof!`;
436
+ throw new Error(errorMessage);
437
+ }
438
+
439
+ logger.info(`Successfully verified ${circuitType} proof in ${result.duration} ms`);
440
+ }
441
+
442
+ /**
443
+ * Returns the verification key for a circuit, will generate it if not cached internally
444
+ * @param circuitType - The type of circuit for which the verification key is required
445
+ * @returns The verification key
446
+ */
447
+ public async getVerificationKeyForCircuit(circuitType: ServerProtocolArtifact): Promise<VerificationKeyAsFields> {
448
+ const vkData = await this.getVerificationKeyDataForCircuit(circuitType);
449
+ return new VerificationKeyAsFields(vkData.keyAsFields, vkData.hash);
450
+ }
451
+
452
+ private async verifyPreviousRollupProof(previousRollupData: PreviousRollupData) {
453
+ const proof = previousRollupData.proof;
454
+ const circuitType =
455
+ previousRollupData.baseOrMergeRollupPublicInputs.rollupType === RollupTypes.Base
456
+ ? 'BaseRollupArtifact'
457
+ : 'MergeRollupArtifact';
458
+ await this.verifyProof(circuitType, proof);
459
+ }
460
+
461
+ /**
462
+ * Returns the verification key data for a circuit, will generate and cache it if not cached internally
463
+ * @param circuitType - The type of circuit for which the verification key is required
464
+ * @returns The verification key data
465
+ */
466
+ private async getVerificationKeyDataForCircuit(circuitType: ServerProtocolArtifact): Promise<VerificationKeyData> {
467
+ let promise = this.verificationKeys.get(circuitType);
468
+ if (!promise) {
469
+ promise = generateKeyForNoirCircuit(
470
+ this.config.bbBinaryPath,
471
+ this.config.bbWorkingDirectory,
472
+ circuitType,
473
+ ServerCircuitArtifacts[circuitType],
474
+ 'vk',
475
+ logger.debug,
476
+ ).then(result => {
477
+ if (result.status === BB_RESULT.FAILURE) {
478
+ throw new Error(`Failed to generate verification key for ${circuitType}, ${result.reason}`);
479
+ }
480
+ return this.convertVk(result.vkPath!);
481
+ });
482
+ this.verificationKeys.set(circuitType, promise);
483
+ }
484
+ return await promise;
485
+ }
486
+
487
+ /**
488
+ * Reads the verification key data stored at the specified location and parses into a VerificationKeyData
489
+ * @param filePath - The directory containing the verification key data files
490
+ * @returns The verification key data
491
+ */
492
+ private async convertVk(filePath: string): Promise<VerificationKeyData> {
493
+ const [rawFields, rawBinary] = await Promise.all([
494
+ fs.readFile(`${filePath}/${VK_FIELDS_FILENAME}`, { encoding: 'utf-8' }),
495
+ fs.readFile(`${filePath}/${VK_FILENAME}`),
496
+ ]);
497
+ const fieldsJson = JSON.parse(rawFields);
498
+ const fields = fieldsJson.map(Fr.fromString);
499
+ // The first item is the hash, this is not part of the actual VK
500
+ const vkHash = fields[0];
501
+ const actualVk = fields.slice(1);
502
+ const vk: VerificationKeyData = {
503
+ hash: vkHash,
504
+ keyAsFields: actualVk as Tuple<Fr, typeof VERIFICATION_KEY_LENGTH_IN_FIELDS>,
505
+ keyAsBytes: rawBinary,
506
+ numPublicInputs: Number(actualVk[CIRCUIT_PUBLIC_INPUTS_INDEX]),
507
+ circuitSize: Number(actualVk[CIRCUIT_SIZE_INDEX]),
508
+ isRecursive: actualVk[CIRCUIT_RECURSIVE_INDEX] == Fr.ONE,
509
+ };
510
+ return vk;
511
+ }
512
+
513
+ /**
514
+ * Ensures our verification key cache includes the key data located at the specified directory
515
+ * @param filePath - The directory containing the verification key data files
516
+ * @param circuitType - The type of circuit to which the verification key corresponds
517
+ */
518
+ private async updateVerificationKeyAfterProof(filePath: string, circuitType: ServerProtocolArtifact) {
519
+ let promise = this.verificationKeys.get(circuitType);
520
+ if (!promise) {
521
+ promise = this.convertVk(filePath);
522
+ this.verificationKeys.set(circuitType, promise);
523
+ }
524
+ await promise;
525
+ }
526
+
527
+ /**
528
+ * Parses and returns the proof data stored at the specified directory
529
+ * @param filePath - The directory containing the proof data
530
+ * @param circuitType - The type of circuit proven
531
+ * @returns The proof
532
+ */
533
+ private async readProofAsFields<PROOF_LENGTH extends number>(
534
+ filePath: string,
535
+ circuitType: ServerProtocolArtifact,
536
+ ): Promise<RecursiveProof<PROOF_LENGTH>> {
537
+ const [binaryProof, proofString] = await Promise.all([
538
+ fs.readFile(`${filePath}/${PROOF_FILENAME}`),
539
+ fs.readFile(`${filePath}/${PROOF_FIELDS_FILENAME}`, { encoding: 'utf-8' }),
540
+ ]);
541
+ const json = JSON.parse(proofString);
542
+ const fields = json.map(Fr.fromString);
543
+ const vkData = await this.verificationKeys.get(circuitType);
544
+ if (!vkData) {
545
+ throw new Error(`Invalid verification key for ${circuitType}`);
546
+ }
547
+ const numPublicInputs = CIRCUITS_WITHOUT_AGGREGATION.has(circuitType)
548
+ ? vkData.numPublicInputs
549
+ : vkData.numPublicInputs - AGGREGATION_OBJECT_SIZE;
550
+ const fieldsWithoutPublicInputs = fields.slice(numPublicInputs);
551
+ logger.debug(
552
+ `Circuit type: ${circuitType}, complete proof length: ${fields.length}, without public inputs: ${fieldsWithoutPublicInputs.length}, num public inputs: ${numPublicInputs}, circuit size: ${vkData.circuitSize}, is recursive: ${vkData.isRecursive}, raw length: ${binaryProof.length}`,
553
+ );
554
+ const proof = new RecursiveProof<PROOF_LENGTH>(fieldsWithoutPublicInputs, new Proof(binaryProof));
555
+ return proof;
556
+ }
557
+ }
@@ -0,0 +1,2 @@
1
+ export * from './bb_prover.js';
2
+ export * from './bb_native_proof_creator.js';
@@ -0,0 +1,16 @@
1
+ import { type Fr, type VERIFICATION_KEY_LENGTH_IN_FIELDS } from '@aztec/circuits.js';
2
+ import { type Tuple } from '@aztec/foundation/serialize';
3
+
4
+ export const AGGREGATION_OBJECT_SIZE = 16;
5
+ export const CIRCUIT_SIZE_INDEX = 3;
6
+ export const CIRCUIT_PUBLIC_INPUTS_INDEX = 4;
7
+ export const CIRCUIT_RECURSIVE_INDEX = 5;
8
+
9
+ export type VerificationKeyData = {
10
+ hash: Fr;
11
+ keyAsFields: Tuple<Fr, typeof VERIFICATION_KEY_LENGTH_IN_FIELDS>;
12
+ keyAsBytes: Buffer;
13
+ numPublicInputs: number;
14
+ circuitSize: number;
15
+ isRecursive: boolean;
16
+ };
package/src/stats.ts ADDED
@@ -0,0 +1,82 @@
1
+ import { type PublicKernelRequest, PublicKernelType } from '@aztec/circuit-types';
2
+ import type { CircuitName, CircuitProvingStats, CircuitWitnessGenerationStats } from '@aztec/circuit-types/stats';
3
+ import { type Logger } from '@aztec/foundation/log';
4
+ import { type ServerProtocolArtifact } from '@aztec/noir-protocol-circuits-types';
5
+
6
+ export function emitCircuitWitnessGenerationStats(
7
+ circuitName: CircuitName,
8
+ duration: number,
9
+ inputSize: number,
10
+ outputSize: number,
11
+ logger: Logger,
12
+ ) {
13
+ const stats: CircuitWitnessGenerationStats = {
14
+ eventName: 'circuit-witness-generation',
15
+ circuitName,
16
+ inputSize,
17
+ outputSize,
18
+ duration,
19
+ };
20
+
21
+ logger.debug('Circuit witness generation stats', stats);
22
+ }
23
+
24
+ export function emitCircuitProvingStats(
25
+ circuitName: CircuitName,
26
+ duration: number,
27
+ inputSize: number,
28
+ outputSize: number,
29
+ proofSize: number,
30
+ logger: Logger,
31
+ ) {
32
+ const stats: CircuitProvingStats = {
33
+ eventName: 'circuit-proving',
34
+ circuitName,
35
+ duration,
36
+ inputSize,
37
+ outputSize,
38
+ proofSize,
39
+ };
40
+
41
+ logger.debug('Circuit proving stats', stats);
42
+ }
43
+
44
+ export function mapPublicKernelToCircuitName(kernelType: PublicKernelRequest['type']): CircuitName {
45
+ switch (kernelType) {
46
+ case PublicKernelType.SETUP:
47
+ return 'public-kernel-setup';
48
+ case PublicKernelType.APP_LOGIC:
49
+ return 'public-kernel-app-logic';
50
+ case PublicKernelType.TEARDOWN:
51
+ return 'public-kernel-teardown';
52
+ case PublicKernelType.TAIL:
53
+ return 'public-kernel-tail';
54
+ default:
55
+ throw new Error(`Unknown kernel type: ${kernelType}`);
56
+ }
57
+ }
58
+
59
+ export function circuitTypeToCircuitName(circuitType: ServerProtocolArtifact): CircuitName {
60
+ switch (circuitType) {
61
+ case 'BaseParityArtifact':
62
+ return 'base-parity';
63
+ case 'RootParityArtifact':
64
+ return 'root-parity';
65
+ case 'BaseRollupArtifact':
66
+ return 'base-rollup';
67
+ case 'MergeRollupArtifact':
68
+ return 'merge-rollup';
69
+ case 'RootRollupArtifact':
70
+ return 'root-rollup';
71
+ case 'PublicKernelSetupArtifact':
72
+ return 'public-kernel-setup';
73
+ case 'PublicKernelAppLogicArtifact':
74
+ return 'public-kernel-app-logic';
75
+ case 'PublicKernelTeardownArtifact':
76
+ return 'public-kernel-teardown';
77
+ case 'PublicKernelTailArtifact':
78
+ return 'public-kernel-tail';
79
+ default:
80
+ throw new Error(`Unknown circuit type: ${circuitType}`);
81
+ }
82
+ }
@@ -0,0 +1 @@
1
+ export * from './test_circuit_prover.js';