@aztec/bb-prover 0.0.0-test.0 → 0.0.1-commit.21caa21

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 (109) hide show
  1. package/dest/avm_proving_tests/avm_proving_tester.d.ts +14 -18
  2. package/dest/avm_proving_tests/avm_proving_tester.d.ts.map +1 -1
  3. package/dest/avm_proving_tests/avm_proving_tester.js +130 -79
  4. package/dest/bb/cli.d.ts +1 -1
  5. package/dest/bb/execute.d.ts +14 -47
  6. package/dest/bb/execute.d.ts.map +1 -1
  7. package/dest/bb/execute.js +132 -236
  8. package/dest/bb/index.d.ts +1 -1
  9. package/dest/config.d.ts +3 -1
  10. package/dest/config.d.ts.map +1 -1
  11. package/dest/honk.d.ts +3 -3
  12. package/dest/honk.d.ts.map +1 -1
  13. package/dest/honk.js +3 -2
  14. package/dest/index.d.ts +2 -1
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +1 -0
  17. package/dest/instrumentation.d.ts +3 -3
  18. package/dest/instrumentation.d.ts.map +1 -1
  19. package/dest/instrumentation.js +2 -4
  20. package/dest/prover/client/bb_private_kernel_prover.d.ts +32 -0
  21. package/dest/prover/client/bb_private_kernel_prover.d.ts.map +1 -0
  22. package/dest/prover/{bb_private_kernel_prover.js → client/bb_private_kernel_prover.js} +42 -20
  23. package/dest/prover/client/bundle.d.ts +6 -0
  24. package/dest/prover/client/bundle.d.ts.map +1 -0
  25. package/dest/prover/client/bundle.js +8 -0
  26. package/dest/prover/client/lazy.d.ts +6 -0
  27. package/dest/prover/client/lazy.d.ts.map +1 -0
  28. package/dest/prover/client/lazy.js +8 -0
  29. package/dest/prover/index.d.ts +3 -4
  30. package/dest/prover/index.d.ts.map +1 -1
  31. package/dest/prover/index.js +2 -3
  32. package/dest/prover/proof_utils.d.ts +19 -0
  33. package/dest/prover/proof_utils.d.ts.map +1 -0
  34. package/dest/prover/proof_utils.js +72 -0
  35. package/dest/prover/server/bb_prover.d.ts +100 -0
  36. package/dest/prover/server/bb_prover.d.ts.map +1 -0
  37. package/dest/prover/server/bb_prover.js +339 -0
  38. package/dest/test/delay_values.d.ts +1 -1
  39. package/dest/test/delay_values.d.ts.map +1 -1
  40. package/dest/test/delay_values.js +33 -21
  41. package/dest/test/index.d.ts +1 -1
  42. package/dest/test/test_circuit_prover.d.ts +25 -34
  43. package/dest/test/test_circuit_prover.d.ts.map +1 -1
  44. package/dest/test/test_circuit_prover.js +85 -59
  45. package/dest/test/test_verifier.d.ts +6 -3
  46. package/dest/test/test_verifier.d.ts.map +1 -1
  47. package/dest/test/test_verifier.js +23 -1
  48. package/dest/verification_key/verification_key_data.d.ts +7 -1
  49. package/dest/verification_key/verification_key_data.d.ts.map +1 -1
  50. package/dest/verification_key/verification_key_data.js +23 -29
  51. package/dest/verifier/bb_verifier.d.ts +6 -5
  52. package/dest/verifier/bb_verifier.d.ts.map +1 -1
  53. package/dest/verifier/bb_verifier.js +46 -25
  54. package/dest/verifier/index.d.ts +2 -1
  55. package/dest/verifier/index.d.ts.map +1 -1
  56. package/dest/verifier/index.js +1 -0
  57. package/dest/verifier/queued_chonk_verifier.d.ts +15 -0
  58. package/dest/verifier/queued_chonk_verifier.d.ts.map +1 -0
  59. package/dest/verifier/queued_chonk_verifier.js +132 -0
  60. package/package.json +35 -33
  61. package/src/avm_proving_tests/avm_proving_tester.ts +210 -104
  62. package/src/bb/execute.ts +103 -250
  63. package/src/config.ts +2 -0
  64. package/src/honk.ts +3 -2
  65. package/src/index.ts +1 -0
  66. package/src/instrumentation.ts +2 -4
  67. package/src/prover/{bb_private_kernel_prover.ts → client/bb_private_kernel_prover.ts} +78 -30
  68. package/src/prover/client/bundle.ts +11 -0
  69. package/src/prover/client/lazy.ts +11 -0
  70. package/src/prover/index.ts +2 -3
  71. package/src/prover/proof_utils.ts +115 -0
  72. package/src/prover/server/bb_prover.ts +733 -0
  73. package/src/test/delay_values.ts +33 -21
  74. package/src/test/test_circuit_prover.ts +260 -147
  75. package/src/test/test_verifier.ts +15 -3
  76. package/src/verification_key/verification_key_data.ts +29 -24
  77. package/src/verifier/bb_verifier.ts +63 -32
  78. package/src/verifier/index.ts +1 -0
  79. package/src/verifier/queued_chonk_verifier.ts +140 -0
  80. package/dest/prover/bb_native_private_kernel_prover.d.ts +0 -25
  81. package/dest/prover/bb_native_private_kernel_prover.d.ts.map +0 -1
  82. package/dest/prover/bb_native_private_kernel_prover.js +0 -69
  83. package/dest/prover/bb_private_kernel_prover.d.ts +0 -32
  84. package/dest/prover/bb_private_kernel_prover.d.ts.map +0 -1
  85. package/dest/prover/bb_prover.d.ts +0 -120
  86. package/dest/prover/bb_prover.d.ts.map +0 -1
  87. package/dest/prover/bb_prover.js +0 -423
  88. package/dest/prover/client_ivc_proof_utils.d.ts +0 -25
  89. package/dest/prover/client_ivc_proof_utils.d.ts.map +0 -1
  90. package/dest/prover/client_ivc_proof_utils.js +0 -43
  91. package/dest/stats.d.ts +0 -5
  92. package/dest/stats.d.ts.map +0 -1
  93. package/dest/stats.js +0 -62
  94. package/dest/wasm/bb_wasm_private_kernel_prover.d.ts +0 -17
  95. package/dest/wasm/bb_wasm_private_kernel_prover.d.ts.map +0 -1
  96. package/dest/wasm/bb_wasm_private_kernel_prover.js +0 -46
  97. package/dest/wasm/bundle.d.ts +0 -6
  98. package/dest/wasm/bundle.d.ts.map +0 -1
  99. package/dest/wasm/bundle.js +0 -8
  100. package/dest/wasm/lazy.d.ts +0 -6
  101. package/dest/wasm/lazy.d.ts.map +0 -1
  102. package/dest/wasm/lazy.js +0 -8
  103. package/src/prover/bb_native_private_kernel_prover.ts +0 -119
  104. package/src/prover/bb_prover.ts +0 -781
  105. package/src/prover/client_ivc_proof_utils.ts +0 -42
  106. package/src/stats.ts +0 -64
  107. package/src/wasm/bb_wasm_private_kernel_prover.ts +0 -55
  108. package/src/wasm/bundle.ts +0 -11
  109. package/src/wasm/lazy.ts +0 -11
@@ -0,0 +1,733 @@
1
+ import {
2
+ AVM_V2_PROOF_LENGTH_IN_FIELDS_PADDED,
3
+ NESTED_RECURSIVE_PROOF_LENGTH,
4
+ NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH,
5
+ PAIRING_POINTS_SIZE,
6
+ RECURSIVE_PROOF_LENGTH,
7
+ ULTRA_KECCAK_PROOF_LENGTH,
8
+ } from '@aztec/constants';
9
+ import { Fr } from '@aztec/foundation/fields';
10
+ import { runInDirectory } from '@aztec/foundation/fs';
11
+ import { createLogger } from '@aztec/foundation/log';
12
+ import { BufferReader } from '@aztec/foundation/serialize';
13
+ import {
14
+ type ServerProtocolArtifact,
15
+ convertBlockMergeRollupOutputsFromWitnessMap,
16
+ convertBlockMergeRollupPrivateInputsToWitnessMap,
17
+ convertBlockRootEmptyTxFirstRollupOutputsFromWitnessMap,
18
+ convertBlockRootEmptyTxFirstRollupPrivateInputsToWitnessMap,
19
+ convertBlockRootFirstRollupOutputsFromWitnessMap,
20
+ convertBlockRootFirstRollupPrivateInputsToWitnessMap,
21
+ convertBlockRootRollupOutputsFromWitnessMap,
22
+ convertBlockRootRollupPrivateInputsToWitnessMap,
23
+ convertBlockRootSingleTxFirstRollupOutputsFromWitnessMap,
24
+ convertBlockRootSingleTxFirstRollupPrivateInputsToWitnessMap,
25
+ convertBlockRootSingleTxRollupOutputsFromWitnessMap,
26
+ convertBlockRootSingleTxRollupPrivateInputsToWitnessMap,
27
+ convertCheckpointMergeRollupOutputsFromWitnessMap,
28
+ convertCheckpointMergeRollupPrivateInputsToWitnessMap,
29
+ convertCheckpointPaddingRollupOutputsFromWitnessMap,
30
+ convertCheckpointPaddingRollupPrivateInputsToWitnessMap,
31
+ convertCheckpointRootRollupOutputsFromWitnessMap,
32
+ convertCheckpointRootRollupPrivateInputsToWitnessMap,
33
+ convertCheckpointRootSingleBlockRollupOutputsFromWitnessMap,
34
+ convertCheckpointRootSingleBlockRollupPrivateInputsToWitnessMap,
35
+ convertParityBaseOutputsFromWitnessMap,
36
+ convertParityBasePrivateInputsToWitnessMap,
37
+ convertParityRootOutputsFromWitnessMap,
38
+ convertParityRootPrivateInputsToWitnessMap,
39
+ convertPrivateTxBaseRollupOutputsFromWitnessMap,
40
+ convertPrivateTxBaseRollupPrivateInputsToWitnessMap,
41
+ convertPublicChonkVerifierOutputsFromWitnessMap,
42
+ convertPublicChonkVerifierPrivateInputsToWitnessMap,
43
+ convertPublicTxBaseRollupOutputsFromWitnessMap,
44
+ convertPublicTxBaseRollupPrivateInputsToWitnessMap,
45
+ convertRootRollupOutputsFromWitnessMap,
46
+ convertRootRollupPrivateInputsToWitnessMap,
47
+ convertTxMergeRollupOutputsFromWitnessMap,
48
+ convertTxMergeRollupPrivateInputsToWitnessMap,
49
+ getServerCircuitArtifact,
50
+ } from '@aztec/noir-protocol-circuits-types/server';
51
+ import { ServerCircuitVks } from '@aztec/noir-protocol-circuits-types/server/vks';
52
+ import { mapProtocolArtifactNameToCircuitName } from '@aztec/noir-protocol-circuits-types/types';
53
+ import type { WitnessMap } from '@aztec/noir-types';
54
+ import { NativeACVMSimulator } from '@aztec/simulator/server';
55
+ import type { AvmCircuitInputs, AvmCircuitPublicInputs } from '@aztec/stdlib/avm';
56
+ import { ProvingError } from '@aztec/stdlib/errors';
57
+ import {
58
+ type ProofAndVerificationKey,
59
+ type PublicInputsAndRecursiveProof,
60
+ type ServerCircuitProver,
61
+ makeProofAndVerificationKey,
62
+ makePublicInputsAndRecursiveProof,
63
+ } from '@aztec/stdlib/interfaces/server';
64
+ import type { ParityBasePrivateInputs, ParityPublicInputs, ParityRootPrivateInputs } from '@aztec/stdlib/parity';
65
+ import { Proof, RecursiveProof, makeRecursiveProofFromBinary } from '@aztec/stdlib/proofs';
66
+ import {
67
+ BlockMergeRollupPrivateInputs,
68
+ BlockRollupPublicInputs,
69
+ BlockRootEmptyTxFirstRollupPrivateInputs,
70
+ BlockRootFirstRollupPrivateInputs,
71
+ BlockRootRollupPrivateInputs,
72
+ BlockRootSingleTxFirstRollupPrivateInputs,
73
+ BlockRootSingleTxRollupPrivateInputs,
74
+ CheckpointMergeRollupPrivateInputs,
75
+ CheckpointPaddingRollupPrivateInputs,
76
+ CheckpointRollupPublicInputs,
77
+ CheckpointRootRollupPrivateInputs,
78
+ CheckpointRootSingleBlockRollupPrivateInputs,
79
+ type PrivateTxBaseRollupPrivateInputs,
80
+ PublicChonkVerifierPrivateInputs,
81
+ PublicChonkVerifierPublicInputs,
82
+ PublicTxBaseRollupPrivateInputs,
83
+ type RootRollupPrivateInputs,
84
+ type RootRollupPublicInputs,
85
+ type TxMergeRollupPrivateInputs,
86
+ type TxRollupPublicInputs,
87
+ enhanceProofWithPiValidationFlag,
88
+ } from '@aztec/stdlib/rollup';
89
+ import type { CircuitProvingStats, CircuitWitnessGenerationStats } from '@aztec/stdlib/stats';
90
+ import type { VerificationKeyData } from '@aztec/stdlib/vks';
91
+ import { Attributes, type TelemetryClient, getTelemetryClient, trackSpan } from '@aztec/telemetry-client';
92
+
93
+ import { promises as fs } from 'fs';
94
+ import * as path from 'path';
95
+
96
+ import {
97
+ type BBFailure,
98
+ type BBSuccess,
99
+ BB_RESULT,
100
+ PROOF_FILENAME,
101
+ PUBLIC_INPUTS_FILENAME,
102
+ VK_FILENAME,
103
+ generateAvmProof,
104
+ generateProof,
105
+ verifyAvmProof,
106
+ verifyProof,
107
+ } from '../../bb/execute.js';
108
+ import type { ACVMConfig, BBConfig } from '../../config.js';
109
+ import { type UltraHonkFlavor, getUltraHonkFlavorForCircuit } from '../../honk.js';
110
+ import { ProverInstrumentation } from '../../instrumentation.js';
111
+ import { extractAvmVkData } from '../../verification_key/verification_key_data.js';
112
+ import { readProofsFromOutputDirectory } from '../proof_utils.js';
113
+
114
+ const logger = createLogger('bb-prover');
115
+
116
+ export interface BBProverConfig extends BBConfig, ACVMConfig {
117
+ // list of circuits supported by this prover. defaults to all circuits if empty
118
+ circuitFilter?: ServerProtocolArtifact[];
119
+ }
120
+
121
+ /**
122
+ * Prover implementation that uses barretenberg native proving
123
+ */
124
+ export class BBNativeRollupProver implements ServerCircuitProver {
125
+ private instrumentation: ProverInstrumentation;
126
+
127
+ constructor(
128
+ private config: BBProverConfig,
129
+ telemetry: TelemetryClient,
130
+ ) {
131
+ this.instrumentation = new ProverInstrumentation(telemetry, 'BBNativeRollupProver');
132
+ }
133
+
134
+ get tracer() {
135
+ return this.instrumentation.tracer;
136
+ }
137
+
138
+ static async new(config: BBProverConfig, telemetry: TelemetryClient = getTelemetryClient()) {
139
+ await fs.access(config.acvmBinaryPath, fs.constants.R_OK);
140
+ await fs.mkdir(config.acvmWorkingDirectory, { recursive: true });
141
+ await fs.access(config.bbBinaryPath, fs.constants.R_OK);
142
+ await fs.mkdir(config.bbWorkingDirectory, { recursive: true });
143
+ logger.info(`Using native BB at ${config.bbBinaryPath} and working directory ${config.bbWorkingDirectory}`);
144
+ logger.info(`Using native ACVM at ${config.acvmBinaryPath} and working directory ${config.acvmWorkingDirectory}`);
145
+
146
+ return new BBNativeRollupProver(config, telemetry);
147
+ }
148
+
149
+ /**
150
+ * Simulates the base parity circuit from its inputs.
151
+ * @param inputs - Inputs to the circuit.
152
+ * @returns The public inputs of the parity circuit.
153
+ */
154
+ @trackSpan('BBNativeRollupProver.getBaseParityProof', { [Attributes.PROTOCOL_CIRCUIT_NAME]: 'parity-base' })
155
+ public getBaseParityProof(
156
+ inputs: ParityBasePrivateInputs,
157
+ ): Promise<PublicInputsAndRecursiveProof<ParityPublicInputs, typeof RECURSIVE_PROOF_LENGTH>> {
158
+ return this.createRecursiveProofAndVerify(
159
+ inputs,
160
+ 'ParityBaseArtifact',
161
+ RECURSIVE_PROOF_LENGTH,
162
+ convertParityBasePrivateInputsToWitnessMap,
163
+ convertParityBaseOutputsFromWitnessMap,
164
+ );
165
+ }
166
+
167
+ /**
168
+ * Simulates the root parity circuit from its inputs.
169
+ * @param inputs - Inputs to the circuit.
170
+ * @returns The public inputs of the parity circuit.
171
+ */
172
+ @trackSpan('BBNativeRollupProver.getRootParityProof', { [Attributes.PROTOCOL_CIRCUIT_NAME]: 'parity-root' })
173
+ public getRootParityProof(
174
+ inputs: ParityRootPrivateInputs,
175
+ ): Promise<PublicInputsAndRecursiveProof<ParityPublicInputs, typeof NESTED_RECURSIVE_PROOF_LENGTH>> {
176
+ return this.createRecursiveProofAndVerify(
177
+ inputs,
178
+ 'ParityRootArtifact',
179
+ NESTED_RECURSIVE_PROOF_LENGTH,
180
+ convertParityRootPrivateInputsToWitnessMap,
181
+ convertParityRootOutputsFromWitnessMap,
182
+ );
183
+ }
184
+
185
+ /**
186
+ * Creates an AVM proof and verifies it.
187
+ * @param inputs - The inputs to the AVM circuit.
188
+ * @returns The proof.
189
+ */
190
+ @trackSpan('BBNativeRollupProver.getAvmProof', inputs => ({
191
+ [Attributes.APP_CIRCUIT_NAME]: inputs.hints.tx.hash,
192
+ }))
193
+ public async getAvmProof(
194
+ inputs: AvmCircuitInputs,
195
+ skipPublicInputsValidation: boolean = false,
196
+ ): Promise<ProofAndVerificationKey<typeof AVM_V2_PROOF_LENGTH_IN_FIELDS_PADDED>> {
197
+ const proofAndVk = await this.createAvmProof(inputs);
198
+ await this.verifyAvmProof(proofAndVk.proof.binaryProof, proofAndVk.verificationKey, inputs.publicInputs);
199
+
200
+ // TODO(#14234)[Unconditional PIs validation]: remove next lines and directly return proofAndVk
201
+ proofAndVk.proof.proof = enhanceProofWithPiValidationFlag(proofAndVk.proof.proof, skipPublicInputsValidation);
202
+ return proofAndVk;
203
+ }
204
+
205
+ public async getPublicChonkVerifierProof(
206
+ inputs: PublicChonkVerifierPrivateInputs,
207
+ ): Promise<
208
+ PublicInputsAndRecursiveProof<PublicChonkVerifierPublicInputs, typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH>
209
+ > {
210
+ const artifactName = 'PublicChonkVerifier';
211
+
212
+ const { circuitOutput, proof } = await this.createRecursiveProof(
213
+ inputs,
214
+ artifactName,
215
+ NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH,
216
+ convertPublicChonkVerifierPrivateInputsToWitnessMap,
217
+ convertPublicChonkVerifierOutputsFromWitnessMap,
218
+ );
219
+
220
+ const verificationKey = this.getVerificationKeyDataForCircuit(artifactName);
221
+
222
+ await this.verifyProof(artifactName, proof.binaryProof);
223
+
224
+ return makePublicInputsAndRecursiveProof(circuitOutput, proof, verificationKey);
225
+ }
226
+
227
+ /**
228
+ * Simulates the base rollup circuit from its inputs.
229
+ * @param inputs - Inputs to the circuit.
230
+ * @returns The public inputs as outputs of the simulation.
231
+ */
232
+ public getPrivateTxBaseRollupProof(
233
+ inputs: PrivateTxBaseRollupPrivateInputs,
234
+ ): Promise<PublicInputsAndRecursiveProof<TxRollupPublicInputs, typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH>> {
235
+ return this.createRecursiveProofAndVerify(
236
+ inputs,
237
+ 'PrivateTxBaseRollupArtifact',
238
+ NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH,
239
+ convertPrivateTxBaseRollupPrivateInputsToWitnessMap,
240
+ convertPrivateTxBaseRollupOutputsFromWitnessMap,
241
+ );
242
+ }
243
+
244
+ /**
245
+ * Requests that the public kernel tail circuit be executed and the proof generated
246
+ * @param kernelRequest - The object encapsulating the request for a proof
247
+ * @returns The requested circuit's public inputs and proof
248
+ */
249
+ public getPublicTxBaseRollupProof(
250
+ inputs: PublicTxBaseRollupPrivateInputs,
251
+ ): Promise<PublicInputsAndRecursiveProof<TxRollupPublicInputs, typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH>> {
252
+ return this.createRecursiveProofAndVerify(
253
+ inputs,
254
+ 'PublicTxBaseRollupArtifact',
255
+ NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH,
256
+ convertPublicTxBaseRollupPrivateInputsToWitnessMap,
257
+ convertPublicTxBaseRollupOutputsFromWitnessMap,
258
+ );
259
+ }
260
+
261
+ /**
262
+ * Simulates the merge rollup circuit from its inputs.
263
+ * @param input - Inputs to the circuit.
264
+ * @returns The public inputs as outputs of the simulation.
265
+ */
266
+ public getTxMergeRollupProof(
267
+ input: TxMergeRollupPrivateInputs,
268
+ ): Promise<PublicInputsAndRecursiveProof<TxRollupPublicInputs, typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH>> {
269
+ return this.createRecursiveProofAndVerify(
270
+ input,
271
+ 'TxMergeRollupArtifact',
272
+ NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH,
273
+ convertTxMergeRollupPrivateInputsToWitnessMap,
274
+ convertTxMergeRollupOutputsFromWitnessMap,
275
+ );
276
+ }
277
+
278
+ public getBlockRootFirstRollupProof(
279
+ input: BlockRootFirstRollupPrivateInputs,
280
+ ): Promise<PublicInputsAndRecursiveProof<BlockRollupPublicInputs, typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH>> {
281
+ return this.createRecursiveProofAndVerify(
282
+ input,
283
+ 'BlockRootFirstRollupArtifact',
284
+ NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH,
285
+ convertBlockRootFirstRollupPrivateInputsToWitnessMap,
286
+ convertBlockRootFirstRollupOutputsFromWitnessMap,
287
+ );
288
+ }
289
+
290
+ public getBlockRootSingleTxFirstRollupProof(
291
+ input: BlockRootSingleTxFirstRollupPrivateInputs,
292
+ ): Promise<PublicInputsAndRecursiveProof<BlockRollupPublicInputs, typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH>> {
293
+ return this.createRecursiveProofAndVerify(
294
+ input,
295
+ 'BlockRootSingleTxFirstRollupArtifact',
296
+ NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH,
297
+ convertBlockRootSingleTxFirstRollupPrivateInputsToWitnessMap,
298
+ convertBlockRootSingleTxFirstRollupOutputsFromWitnessMap,
299
+ );
300
+ }
301
+
302
+ public getBlockRootEmptyTxFirstRollupProof(
303
+ input: BlockRootEmptyTxFirstRollupPrivateInputs,
304
+ ): Promise<PublicInputsAndRecursiveProof<BlockRollupPublicInputs, typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH>> {
305
+ return this.createRecursiveProofAndVerify(
306
+ input,
307
+ 'BlockRootEmptyTxFirstRollupArtifact',
308
+ NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH,
309
+ convertBlockRootEmptyTxFirstRollupPrivateInputsToWitnessMap,
310
+ convertBlockRootEmptyTxFirstRollupOutputsFromWitnessMap,
311
+ );
312
+ }
313
+
314
+ public getBlockRootRollupProof(
315
+ input: BlockRootRollupPrivateInputs,
316
+ ): Promise<PublicInputsAndRecursiveProof<BlockRollupPublicInputs, typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH>> {
317
+ return this.createRecursiveProofAndVerify(
318
+ input,
319
+ 'BlockRootRollupArtifact',
320
+ NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH,
321
+ convertBlockRootRollupPrivateInputsToWitnessMap,
322
+ convertBlockRootRollupOutputsFromWitnessMap,
323
+ );
324
+ }
325
+
326
+ public getBlockRootSingleTxRollupProof(
327
+ input: BlockRootSingleTxRollupPrivateInputs,
328
+ ): Promise<PublicInputsAndRecursiveProof<BlockRollupPublicInputs, typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH>> {
329
+ return this.createRecursiveProofAndVerify(
330
+ input,
331
+ 'BlockRootSingleTxRollupArtifact',
332
+ NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH,
333
+ convertBlockRootSingleTxRollupPrivateInputsToWitnessMap,
334
+ convertBlockRootSingleTxRollupOutputsFromWitnessMap,
335
+ );
336
+ }
337
+
338
+ public getBlockMergeRollupProof(
339
+ input: BlockMergeRollupPrivateInputs,
340
+ ): Promise<PublicInputsAndRecursiveProof<BlockRollupPublicInputs, typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH>> {
341
+ return this.createRecursiveProofAndVerify(
342
+ input,
343
+ 'BlockMergeRollupArtifact',
344
+ NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH,
345
+ convertBlockMergeRollupPrivateInputsToWitnessMap,
346
+ convertBlockMergeRollupOutputsFromWitnessMap,
347
+ );
348
+ }
349
+
350
+ public getCheckpointRootRollupProof(
351
+ input: CheckpointRootRollupPrivateInputs,
352
+ ): Promise<
353
+ PublicInputsAndRecursiveProof<CheckpointRollupPublicInputs, typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH>
354
+ > {
355
+ return this.createRecursiveProofAndVerify(
356
+ input,
357
+ 'CheckpointRootRollupArtifact',
358
+ NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH,
359
+ convertCheckpointRootRollupPrivateInputsToWitnessMap,
360
+ convertCheckpointRootRollupOutputsFromWitnessMap,
361
+ );
362
+ }
363
+
364
+ public getCheckpointRootSingleBlockRollupProof(
365
+ input: CheckpointRootSingleBlockRollupPrivateInputs,
366
+ ): Promise<
367
+ PublicInputsAndRecursiveProof<CheckpointRollupPublicInputs, typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH>
368
+ > {
369
+ return this.createRecursiveProofAndVerify(
370
+ input,
371
+ 'CheckpointRootSingleBlockRollupArtifact',
372
+ NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH,
373
+ convertCheckpointRootSingleBlockRollupPrivateInputsToWitnessMap,
374
+ convertCheckpointRootSingleBlockRollupOutputsFromWitnessMap,
375
+ );
376
+ }
377
+
378
+ public getCheckpointPaddingRollupProof(
379
+ input: CheckpointPaddingRollupPrivateInputs,
380
+ ): Promise<
381
+ PublicInputsAndRecursiveProof<CheckpointRollupPublicInputs, typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH>
382
+ > {
383
+ return this.createRecursiveProofAndVerify(
384
+ input,
385
+ 'CheckpointPaddingRollupArtifact',
386
+ NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH,
387
+ convertCheckpointPaddingRollupPrivateInputsToWitnessMap,
388
+ convertCheckpointPaddingRollupOutputsFromWitnessMap,
389
+ );
390
+ }
391
+
392
+ public getCheckpointMergeRollupProof(
393
+ input: CheckpointMergeRollupPrivateInputs,
394
+ ): Promise<
395
+ PublicInputsAndRecursiveProof<CheckpointRollupPublicInputs, typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH>
396
+ > {
397
+ return this.createRecursiveProofAndVerify(
398
+ input,
399
+ 'CheckpointMergeRollupArtifact',
400
+ NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH,
401
+ convertCheckpointMergeRollupPrivateInputsToWitnessMap,
402
+ convertCheckpointMergeRollupOutputsFromWitnessMap,
403
+ );
404
+ }
405
+
406
+ /**
407
+ * Simulates the root rollup circuit from its inputs.
408
+ * @param input - Inputs to the circuit.
409
+ * @returns The public inputs as outputs of the simulation.
410
+ */
411
+ public async getRootRollupProof(
412
+ input: RootRollupPrivateInputs,
413
+ ): Promise<PublicInputsAndRecursiveProof<RootRollupPublicInputs, typeof NESTED_RECURSIVE_PROOF_LENGTH>> {
414
+ const { proof, ...output } = await this.createRecursiveProofAndVerify(
415
+ input,
416
+ 'RootRollupArtifact',
417
+ ULTRA_KECCAK_PROOF_LENGTH,
418
+ convertRootRollupPrivateInputsToWitnessMap,
419
+ convertRootRollupOutputsFromWitnessMap,
420
+ );
421
+
422
+ const recursiveProof = makeRecursiveProofFromBinary(proof.binaryProof, NESTED_RECURSIVE_PROOF_LENGTH);
423
+ // TODO(https://github.com/AztecProtocol/aztec-packages/issues/13188): Remove this hack.
424
+ recursiveProof.binaryProof.numPublicInputs += PAIRING_POINTS_SIZE;
425
+
426
+ return { ...output, proof: recursiveProof };
427
+ }
428
+
429
+ private async createRecursiveProofAndVerify<
430
+ PROOF_LENGTH extends number,
431
+ CircuitInputType extends { toBuffer: () => Buffer },
432
+ CircuitOutputType extends { toBuffer: () => Buffer },
433
+ >(
434
+ input: CircuitInputType,
435
+ artifactName: ServerProtocolArtifact,
436
+ proofLength: PROOF_LENGTH,
437
+ convertInput: (input: CircuitInputType) => WitnessMap,
438
+ convertOutput: (outputWitness: WitnessMap) => CircuitOutputType,
439
+ ) {
440
+ const { circuitOutput, proof } = await this.createRecursiveProof(
441
+ input,
442
+ artifactName,
443
+ proofLength,
444
+ convertInput,
445
+ convertOutput,
446
+ );
447
+
448
+ await this.verifyProof(artifactName, proof.binaryProof);
449
+
450
+ const verificationKey = this.getVerificationKeyDataForCircuit(artifactName);
451
+
452
+ return makePublicInputsAndRecursiveProof(circuitOutput, proof, verificationKey);
453
+ }
454
+
455
+ private async generateProofWithBB<
456
+ Input extends { toBuffer: () => Buffer },
457
+ Output extends { toBuffer: () => Buffer },
458
+ >(
459
+ input: Input,
460
+ circuitType: ServerProtocolArtifact,
461
+ convertInput: (input: Input) => WitnessMap,
462
+ convertOutput: (outputWitness: WitnessMap) => Output,
463
+ workingDirectory: string,
464
+ ): Promise<{ circuitOutput: Output; provingResult: BBSuccess }> {
465
+ // Have the ACVM write the partial witness here
466
+ const outputWitnessFile = path.join(workingDirectory, 'partial-witness.gz');
467
+
468
+ // Generate the partial witness using the ACVM
469
+ // A further temp directory will be created beneath ours and then cleaned up after the partial witness has been copied to our specified location
470
+ const simulator = new NativeACVMSimulator(
471
+ this.config.acvmWorkingDirectory,
472
+ this.config.acvmBinaryPath,
473
+ outputWitnessFile,
474
+ );
475
+
476
+ const artifact = getServerCircuitArtifact(circuitType);
477
+
478
+ logger.debug(`Generating witness data for ${circuitType}`);
479
+
480
+ const inputWitness = convertInput(input);
481
+ const foreignCallHandler = undefined; // We don't handle foreign calls in the native ACVM simulator
482
+ const witnessResult = await simulator.executeProtocolCircuit(inputWitness, artifact, foreignCallHandler);
483
+ const output = convertOutput(witnessResult.witness);
484
+
485
+ const circuitName = mapProtocolArtifactNameToCircuitName(circuitType);
486
+ this.instrumentation.recordDuration('witGenDuration', circuitName, witnessResult.duration);
487
+ this.instrumentation.recordSize('witGenInputSize', circuitName, input.toBuffer().length);
488
+ this.instrumentation.recordSize('witGenOutputSize', circuitName, output.toBuffer().length);
489
+
490
+ logger.info(`Generated witness`, {
491
+ circuitName,
492
+ duration: witnessResult.duration,
493
+ inputSize: input.toBuffer().length,
494
+ outputSize: output.toBuffer().length,
495
+ eventName: 'circuit-witness-generation',
496
+ } satisfies CircuitWitnessGenerationStats);
497
+
498
+ // Now prove the circuit from the generated witness
499
+ logger.debug(`Proving ${circuitType}...`);
500
+
501
+ const provingResult = await generateProof(
502
+ this.config.bbBinaryPath,
503
+ workingDirectory,
504
+ circuitType,
505
+ Buffer.from(artifact.bytecode, 'base64'),
506
+ this.getVerificationKeyDataForCircuit(circuitType).keyAsBytes,
507
+ outputWitnessFile,
508
+ getUltraHonkFlavorForCircuit(circuitType),
509
+ logger,
510
+ );
511
+
512
+ if (provingResult.status === BB_RESULT.FAILURE) {
513
+ logger.error(`Failed to generate proof for ${circuitType}: ${provingResult.reason}`);
514
+ throw new ProvingError(provingResult.reason, provingResult, provingResult.retry);
515
+ }
516
+
517
+ return {
518
+ circuitOutput: output,
519
+ provingResult,
520
+ };
521
+ }
522
+
523
+ private async generateAvmProofWithBB(input: AvmCircuitInputs, workingDirectory: string): Promise<BBSuccess> {
524
+ logger.info(`Proving avm-circuit for TX ${input.hints.tx.hash}...`);
525
+
526
+ const provingResult = await generateAvmProof(this.config.bbBinaryPath, workingDirectory, input, logger);
527
+
528
+ if (provingResult.status === BB_RESULT.FAILURE) {
529
+ logger.error(`Failed to generate AVM proof for TX ${input.hints.tx.hash}: ${provingResult.reason}`);
530
+ throw new ProvingError(provingResult.reason, provingResult, provingResult.retry);
531
+ }
532
+
533
+ return provingResult;
534
+ }
535
+
536
+ private async createAvmProof(
537
+ input: AvmCircuitInputs,
538
+ ): Promise<ProofAndVerificationKey<typeof AVM_V2_PROOF_LENGTH_IN_FIELDS_PADDED>> {
539
+ const operation = async (bbWorkingDirectory: string) => {
540
+ const provingResult = await this.generateAvmProofWithBB(input, bbWorkingDirectory);
541
+
542
+ const avmVK = await extractAvmVkData(provingResult.vkDirectoryPath!);
543
+ const avmProof = await this.readAvmProofAsFields(provingResult.proofPath!);
544
+
545
+ const circuitType = 'avm-circuit' as const;
546
+ const appCircuitName = 'unknown' as const;
547
+ this.instrumentation.recordAvmDuration('provingDuration', appCircuitName, provingResult.durationMs);
548
+ this.instrumentation.recordAvmSize('proofSize', appCircuitName, avmProof.binaryProof.buffer.length);
549
+
550
+ logger.info(
551
+ `Generated proof for ${circuitType}(${input.hints.tx.hash}) in ${Math.ceil(provingResult.durationMs)} ms`,
552
+ {
553
+ circuitName: circuitType,
554
+ appCircuitName: input.hints.tx.hash,
555
+ // does not include reading the proof from disk
556
+ duration: provingResult.durationMs,
557
+ proofSize: avmProof.binaryProof.buffer.length,
558
+ eventName: 'circuit-proving',
559
+ inputSize: input.serializeWithMessagePack().length,
560
+ circuitSize: 1 << 21,
561
+ numPublicInputs: 0,
562
+ } satisfies CircuitProvingStats,
563
+ );
564
+
565
+ return makeProofAndVerificationKey(avmProof, avmVK);
566
+ };
567
+ return await this.runInDirectory(operation);
568
+ }
569
+
570
+ /**
571
+ * Executes a circuit and returns its outputs and corresponding proof with embedded aggregation object
572
+ * @param witnessMap - The input witness
573
+ * @param circuitType - The type of circuit to be executed
574
+ * @param proofLength - The length of the proof to be generated. This is a dummy parameter to aid in type checking
575
+ * @param convertInput - Function for mapping the input object to a witness map.
576
+ * @param convertOutput - Function for parsing the output witness to it's corresponding object
577
+ * @returns The circuits output object and it's proof
578
+ */
579
+ private async createRecursiveProof<
580
+ PROOF_LENGTH extends number,
581
+ CircuitInputType extends { toBuffer: () => Buffer },
582
+ CircuitOutputType extends { toBuffer: () => Buffer },
583
+ >(
584
+ input: CircuitInputType,
585
+ circuitType: ServerProtocolArtifact,
586
+ proofLength: PROOF_LENGTH,
587
+ convertInput: (input: CircuitInputType) => WitnessMap,
588
+ convertOutput: (outputWitness: WitnessMap) => CircuitOutputType,
589
+ ): Promise<{ circuitOutput: CircuitOutputType; proof: RecursiveProof<PROOF_LENGTH> }> {
590
+ // this probably is gonna need to call chonk
591
+ const operation = async (bbWorkingDirectory: string) => {
592
+ const { provingResult, circuitOutput: output } = await this.generateProofWithBB(
593
+ input,
594
+ circuitType,
595
+ convertInput,
596
+ convertOutput,
597
+ bbWorkingDirectory,
598
+ );
599
+
600
+ const vkData = this.getVerificationKeyDataForCircuit(circuitType);
601
+ // Read the proof as fields
602
+ const proof = await readProofsFromOutputDirectory(provingResult.proofPath!, vkData, proofLength, logger);
603
+
604
+ const circuitName = mapProtocolArtifactNameToCircuitName(circuitType);
605
+ this.instrumentation.recordDuration('provingDuration', circuitName, provingResult.durationMs);
606
+ this.instrumentation.recordSize('proofSize', circuitName, proof.binaryProof.buffer.length);
607
+ this.instrumentation.recordSize('circuitPublicInputCount', circuitName, vkData.numPublicInputs);
608
+ this.instrumentation.recordSize('circuitSize', circuitName, vkData.circuitSize);
609
+ logger.info(
610
+ `Generated proof for ${circuitType} in ${Math.ceil(provingResult.durationMs)} ms, size: ${
611
+ proof.proof.length
612
+ } fields`,
613
+ {
614
+ circuitName,
615
+ circuitSize: vkData.circuitSize,
616
+ duration: provingResult.durationMs,
617
+ inputSize: output.toBuffer().length,
618
+ proofSize: proof.binaryProof.buffer.length,
619
+ eventName: 'circuit-proving',
620
+ numPublicInputs: vkData.numPublicInputs,
621
+ } satisfies CircuitProvingStats,
622
+ );
623
+
624
+ return {
625
+ circuitOutput: output,
626
+ proof,
627
+ };
628
+ };
629
+ return await this.runInDirectory(operation);
630
+ }
631
+
632
+ /**
633
+ * Verifies a proof, will generate the verification key if one is not cached internally
634
+ * @param circuitType - The type of circuit whose proof is to be verified
635
+ * @param proof - The proof to be verified
636
+ */
637
+ public async verifyProof(circuitType: ServerProtocolArtifact, proof: Proof) {
638
+ const verificationKey = this.getVerificationKeyDataForCircuit(circuitType);
639
+ return await this.verifyWithKey(getUltraHonkFlavorForCircuit(circuitType), verificationKey, proof);
640
+ }
641
+
642
+ public async verifyAvmProof(
643
+ proof: Proof,
644
+ verificationKey: VerificationKeyData,
645
+ publicInputs: AvmCircuitPublicInputs,
646
+ ) {
647
+ return await this.verifyWithKeyInternal(proof, verificationKey, (proofPath, vkPath) =>
648
+ verifyAvmProof(this.config.bbBinaryPath, this.config.bbWorkingDirectory, proofPath, publicInputs, vkPath, logger),
649
+ );
650
+ }
651
+
652
+ public async verifyWithKey(flavor: UltraHonkFlavor, verificationKey: VerificationKeyData, proof: Proof) {
653
+ return await this.verifyWithKeyInternal(proof, verificationKey, (proofPath, vkPath) =>
654
+ verifyProof(this.config.bbBinaryPath, proofPath, vkPath, flavor, logger),
655
+ );
656
+ }
657
+
658
+ private async verifyWithKeyInternal(
659
+ proof: Proof,
660
+ verificationKey: { keyAsBytes: Buffer },
661
+ verificationFunction: (proofPath: string, vkPath: string) => Promise<BBFailure | BBSuccess>,
662
+ ) {
663
+ const operation = async (bbWorkingDirectory: string) => {
664
+ const publicInputsFileName = path.join(bbWorkingDirectory, PUBLIC_INPUTS_FILENAME);
665
+ const proofFileName = path.join(bbWorkingDirectory, PROOF_FILENAME);
666
+ const verificationKeyPath = path.join(bbWorkingDirectory, VK_FILENAME);
667
+ // TODO(https://github.com/AztecProtocol/aztec-packages/issues/13189): Put this proof parsing logic in the proof class.
668
+ await fs.writeFile(publicInputsFileName, proof.buffer.slice(0, proof.numPublicInputs * 32));
669
+ await fs.writeFile(proofFileName, proof.buffer.slice(proof.numPublicInputs * 32));
670
+ await fs.writeFile(verificationKeyPath, verificationKey.keyAsBytes);
671
+
672
+ const result = await verificationFunction(proofFileName, verificationKeyPath!);
673
+
674
+ if (result.status === BB_RESULT.FAILURE) {
675
+ const errorMessage = `Failed to verify proof from key!`;
676
+ throw new ProvingError(errorMessage, result, result.retry);
677
+ }
678
+
679
+ logger.info(`Successfully verified proof from key in ${result.durationMs} ms`);
680
+ };
681
+
682
+ await this.runInDirectory(operation);
683
+ }
684
+
685
+ /**
686
+ * Returns the verification key data for a circuit.
687
+ * @param circuitType - The type of circuit for which the verification key is required
688
+ * @returns The verification key data
689
+ */
690
+ private getVerificationKeyDataForCircuit(circuitType: ServerProtocolArtifact): VerificationKeyData {
691
+ const vk = ServerCircuitVks[circuitType];
692
+ if (vk === undefined) {
693
+ throw new Error('Could not find VK for server artifact ' + circuitType);
694
+ }
695
+ return vk;
696
+ }
697
+
698
+ private async readAvmProofAsFields(
699
+ proofFilename: string,
700
+ ): Promise<RecursiveProof<typeof AVM_V2_PROOF_LENGTH_IN_FIELDS_PADDED>> {
701
+ const rawProofBuffer = await fs.readFile(proofFilename);
702
+ const reader = BufferReader.asReader(rawProofBuffer);
703
+ const proofFields = reader.readArray(rawProofBuffer.length / Fr.SIZE_IN_BYTES, Fr);
704
+
705
+ // We extend to a fixed-size padded proof as during development any new AVM circuit column changes the
706
+ // proof length and we do not have a mechanism to feedback a cpp constant to noir/TS.
707
+ // TODO(#13390): Revive a non-padded AVM proof
708
+ if (proofFields.length > AVM_V2_PROOF_LENGTH_IN_FIELDS_PADDED) {
709
+ throw new Error(
710
+ `Proof has ${proofFields.length} fields, expected no more than ${AVM_V2_PROOF_LENGTH_IN_FIELDS_PADDED}.`,
711
+ );
712
+ }
713
+ const proofFieldsPadded = proofFields.concat(
714
+ Array(AVM_V2_PROOF_LENGTH_IN_FIELDS_PADDED - proofFields.length).fill(new Fr(0)),
715
+ );
716
+
717
+ const proof = new Proof(rawProofBuffer, /*numPublicInputs=*/ 0);
718
+ return new RecursiveProof(proofFieldsPadded, proof, true, AVM_V2_PROOF_LENGTH_IN_FIELDS_PADDED);
719
+ }
720
+
721
+ private runInDirectory<T>(fn: (dir: string) => Promise<T>) {
722
+ return runInDirectory(
723
+ this.config.bbWorkingDirectory,
724
+ (dir: string) =>
725
+ fn(dir).catch(err => {
726
+ logger.error(`Error running operation at ${dir}: ${err}`);
727
+ throw err;
728
+ }),
729
+ this.config.bbSkipCleanup,
730
+ logger,
731
+ );
732
+ }
733
+ }