@aztec/bb-prover 0.0.0-test.1 → 0.0.1-commit.017a351

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