@aztec/bb-prover 0.0.0-test.0 → 0.0.1-commit.0208eb9

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