@aztec/bb-prover 0.0.0-test.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/dest/avm_proving_tests/avm_proving_tester.d.ts +25 -0
  2. package/dest/avm_proving_tests/avm_proving_tester.d.ts.map +1 -0
  3. package/dest/avm_proving_tests/avm_proving_tester.js +105 -0
  4. package/dest/bb/cli.d.ts +12 -0
  5. package/dest/bb/cli.d.ts.map +1 -0
  6. package/dest/bb/cli.js +19 -0
  7. package/dest/bb/execute.d.ts +140 -0
  8. package/dest/bb/execute.d.ts.map +1 -0
  9. package/dest/bb/execute.js +780 -0
  10. package/dest/bb/index.d.ts +3 -0
  11. package/dest/bb/index.d.ts.map +1 -0
  12. package/dest/bb/index.js +16 -0
  13. package/dest/config.d.ts +13 -0
  14. package/dest/config.d.ts.map +1 -0
  15. package/dest/config.js +1 -0
  16. package/dest/honk.d.ts +13 -0
  17. package/dest/honk.d.ts.map +1 -0
  18. package/dest/honk.js +18 -0
  19. package/dest/index.d.ts +8 -0
  20. package/dest/index.d.ts.map +1 -0
  21. package/dest/index.js +6 -0
  22. package/dest/instrumentation.d.ts +47 -0
  23. package/dest/instrumentation.d.ts.map +1 -0
  24. package/dest/instrumentation.js +100 -0
  25. package/dest/prover/bb_native_private_kernel_prover.d.ts +25 -0
  26. package/dest/prover/bb_native_private_kernel_prover.d.ts.map +1 -0
  27. package/dest/prover/bb_native_private_kernel_prover.js +69 -0
  28. package/dest/prover/bb_private_kernel_prover.d.ts +32 -0
  29. package/dest/prover/bb_private_kernel_prover.d.ts.map +1 -0
  30. package/dest/prover/bb_private_kernel_prover.js +109 -0
  31. package/dest/prover/bb_prover.d.ts +120 -0
  32. package/dest/prover/bb_prover.d.ts.map +1 -0
  33. package/dest/prover/bb_prover.js +423 -0
  34. package/dest/prover/client_ivc_proof_utils.d.ts +25 -0
  35. package/dest/prover/client_ivc_proof_utils.d.ts.map +1 -0
  36. package/dest/prover/client_ivc_proof_utils.js +43 -0
  37. package/dest/prover/index.d.ts +4 -0
  38. package/dest/prover/index.d.ts.map +1 -0
  39. package/dest/prover/index.js +3 -0
  40. package/dest/stats.d.ts +5 -0
  41. package/dest/stats.d.ts.map +1 -0
  42. package/dest/stats.js +62 -0
  43. package/dest/test/delay_values.d.ts +4 -0
  44. package/dest/test/delay_values.d.ts.map +1 -0
  45. package/dest/test/delay_values.js +29 -0
  46. package/dest/test/index.d.ts +3 -0
  47. package/dest/test/index.d.ts.map +1 -0
  48. package/dest/test/index.js +2 -0
  49. package/dest/test/test_circuit_prover.d.ts +81 -0
  50. package/dest/test/test_circuit_prover.d.ts.map +1 -0
  51. package/dest/test/test_circuit_prover.js +175 -0
  52. package/dest/test/test_verifier.d.ts +6 -0
  53. package/dest/test/test_verifier.d.ts.map +1 -0
  54. package/dest/test/test_verifier.js +5 -0
  55. package/dest/verification_key/verification_key_data.d.ts +9 -0
  56. package/dest/verification_key/verification_key_data.d.ts.map +1 -0
  57. package/dest/verification_key/verification_key_data.js +44 -0
  58. package/dest/verifier/bb_verifier.d.ts +17 -0
  59. package/dest/verifier/bb_verifier.d.ts.map +1 -0
  60. package/dest/verifier/bb_verifier.js +86 -0
  61. package/dest/verifier/index.d.ts +2 -0
  62. package/dest/verifier/index.d.ts.map +1 -0
  63. package/dest/verifier/index.js +1 -0
  64. package/dest/wasm/bb_wasm_private_kernel_prover.d.ts +17 -0
  65. package/dest/wasm/bb_wasm_private_kernel_prover.d.ts.map +1 -0
  66. package/dest/wasm/bb_wasm_private_kernel_prover.js +46 -0
  67. package/dest/wasm/bundle.d.ts +6 -0
  68. package/dest/wasm/bundle.d.ts.map +1 -0
  69. package/dest/wasm/bundle.js +8 -0
  70. package/dest/wasm/lazy.d.ts +6 -0
  71. package/dest/wasm/lazy.d.ts.map +1 -0
  72. package/dest/wasm/lazy.js +8 -0
  73. package/package.json +111 -0
  74. package/src/avm_proving_tests/avm_proving_tester.ts +170 -0
  75. package/src/bb/cli.ts +32 -0
  76. package/src/bb/execute.ts +853 -0
  77. package/src/bb/index.ts +23 -0
  78. package/src/config.ts +13 -0
  79. package/src/honk.ts +30 -0
  80. package/src/index.ts +8 -0
  81. package/src/instrumentation.ts +144 -0
  82. package/src/prover/bb_native_private_kernel_prover.ts +119 -0
  83. package/src/prover/bb_private_kernel_prover.ts +249 -0
  84. package/src/prover/bb_prover.ts +781 -0
  85. package/src/prover/client_ivc_proof_utils.ts +42 -0
  86. package/src/prover/index.ts +3 -0
  87. package/src/stats.ts +64 -0
  88. package/src/test/delay_values.ts +31 -0
  89. package/src/test/index.ts +2 -0
  90. package/src/test/test_circuit_prover.ts +368 -0
  91. package/src/test/test_verifier.ts +8 -0
  92. package/src/verification_key/verification_key_data.ts +45 -0
  93. package/src/verifier/bb_verifier.ts +114 -0
  94. package/src/verifier/index.ts +1 -0
  95. package/src/wasm/bb_wasm_private_kernel_prover.ts +55 -0
  96. package/src/wasm/bundle.ts +11 -0
  97. package/src/wasm/lazy.ts +11 -0
@@ -0,0 +1,853 @@
1
+ import { sha256 } from '@aztec/foundation/crypto';
2
+ import type { LogFn, Logger } from '@aztec/foundation/log';
3
+ import { Timer } from '@aztec/foundation/timer';
4
+ import type { AvmCircuitInputs, AvmCircuitPublicInputs } from '@aztec/stdlib/avm';
5
+
6
+ import * as proc from 'child_process';
7
+ import { promises as fs } from 'fs';
8
+ import { basename, dirname, join } from 'path';
9
+
10
+ import type { UltraHonkFlavor } from '../honk.js';
11
+ import { CLIENT_IVC_PROOF_FILE_NAME, CLIENT_IVC_VK_FILE_NAME } from '../prover/client_ivc_proof_utils.js';
12
+
13
+ export const VK_FILENAME = 'vk';
14
+ export const VK_FIELDS_FILENAME = 'vk_fields.json';
15
+ export const PROOF_FILENAME = 'proof';
16
+ export const PROOF_FIELDS_FILENAME = 'proof_fields.json';
17
+ export const AVM_INPUTS_FILENAME = 'avm_inputs.bin';
18
+ export const AVM_BYTECODE_FILENAME = 'avm_bytecode.bin';
19
+ export const AVM_PUBLIC_INPUTS_FILENAME = 'avm_public_inputs.bin';
20
+ export const AVM_HINTS_FILENAME = 'avm_hints.bin';
21
+
22
+ export enum BB_RESULT {
23
+ SUCCESS,
24
+ FAILURE,
25
+ ALREADY_PRESENT,
26
+ }
27
+
28
+ export type BBSuccess = {
29
+ status: BB_RESULT.SUCCESS | BB_RESULT.ALREADY_PRESENT;
30
+ durationMs: number;
31
+ /** Full path of the public key. */
32
+ pkPath?: string;
33
+ /** Base directory for the VKs (raw, fields). */
34
+ vkPath?: string;
35
+ /** Full path of the proof. */
36
+ proofPath?: string;
37
+ /** Full path of the contract. */
38
+ contractPath?: string;
39
+ /** The number of gates in the circuit. */
40
+ circuitSize?: number;
41
+ };
42
+
43
+ export type BBFailure = {
44
+ status: BB_RESULT.FAILURE;
45
+ reason: string;
46
+ retry?: boolean;
47
+ };
48
+
49
+ export type BBResult = BBSuccess | BBFailure;
50
+
51
+ export type VerificationFunction = typeof verifyProof | typeof verifyAvmProof;
52
+
53
+ type BBExecResult = {
54
+ status: BB_RESULT;
55
+ exitCode: number;
56
+ signal: string | undefined;
57
+ };
58
+
59
+ /**
60
+ * Invokes the Barretenberg binary with the provided command and args
61
+ * @param pathToBB - The path to the BB binary
62
+ * @param command - The command to execute
63
+ * @param args - The arguments to pass
64
+ * @param logger - A log function
65
+ * @param resultParser - An optional handler for detecting success or failure
66
+ * @returns The completed partial witness outputted from the circuit
67
+ */
68
+ export function executeBB(
69
+ pathToBB: string,
70
+ command: string,
71
+ args: string[],
72
+ logger: LogFn,
73
+ resultParser = (code: number) => code === 0,
74
+ ): Promise<BBExecResult> {
75
+ return new Promise<BBExecResult>(resolve => {
76
+ // spawn the bb process
77
+ const { HARDWARE_CONCURRENCY: _, ...envWithoutConcurrency } = process.env;
78
+ const env = process.env.HARDWARE_CONCURRENCY ? process.env : envWithoutConcurrency;
79
+ logger(`Executing BB with: ${pathToBB} ${command} ${args.join(' ')}`);
80
+ const bb = proc.spawn(pathToBB, [command, ...args], {
81
+ env,
82
+ });
83
+ bb.stdout.on('data', data => {
84
+ const message = data.toString('utf-8').replace(/\n$/, '');
85
+ logger(message);
86
+ });
87
+ bb.stderr.on('data', data => {
88
+ const message = data.toString('utf-8').replace(/\n$/, '');
89
+ logger(message);
90
+ });
91
+ bb.on('close', (exitCode: number, signal?: string) => {
92
+ if (resultParser(exitCode)) {
93
+ resolve({ status: BB_RESULT.SUCCESS, exitCode, signal });
94
+ } else {
95
+ resolve({ status: BB_RESULT.FAILURE, exitCode, signal });
96
+ }
97
+ });
98
+ }).catch(_ => ({ status: BB_RESULT.FAILURE, exitCode: -1, signal: undefined }));
99
+ }
100
+
101
+ // TODO(#7369) comment this etc (really just take inspiration from this and rewrite it all O:))
102
+ export async function executeBbClientIvcProof(
103
+ pathToBB: string,
104
+ workingDirectory: string,
105
+ bytecodeStackPath: string,
106
+ witnessStackPath: string,
107
+ log: LogFn,
108
+ ): Promise<BBFailure | BBSuccess> {
109
+ // Check that the working directory exists
110
+ try {
111
+ await fs.access(workingDirectory);
112
+ } catch (error) {
113
+ return { status: BB_RESULT.FAILURE, reason: `Working directory ${workingDirectory} does not exist` };
114
+ }
115
+
116
+ // The proof is written to e.g. /workingDirectory/proof
117
+ const outputPath = `${workingDirectory}`;
118
+
119
+ const binaryPresent = await fs
120
+ .access(pathToBB, fs.constants.R_OK)
121
+ .then(_ => true)
122
+ .catch(_ => false);
123
+ if (!binaryPresent) {
124
+ return { status: BB_RESULT.FAILURE, reason: `Failed to find bb binary at ${pathToBB}` };
125
+ }
126
+
127
+ try {
128
+ // Write the bytecode to the working directory
129
+ log(`bytecodePath ${bytecodeStackPath}`);
130
+ log(`outputPath ${outputPath}`);
131
+ const args = [
132
+ '-o',
133
+ outputPath,
134
+ '-b',
135
+ bytecodeStackPath,
136
+ '-w',
137
+ witnessStackPath,
138
+ '-v',
139
+ '--scheme',
140
+ 'client_ivc',
141
+ '--input_type',
142
+ 'runtime_stack',
143
+ '--write_vk',
144
+ ];
145
+
146
+ const timer = new Timer();
147
+ const logFunction = (message: string) => {
148
+ log(`bb - ${message}`);
149
+ };
150
+
151
+ const result = await executeBB(pathToBB, 'prove', args, logFunction);
152
+ const durationMs = timer.ms();
153
+
154
+ if (result.status == BB_RESULT.SUCCESS) {
155
+ return {
156
+ status: BB_RESULT.SUCCESS,
157
+ durationMs,
158
+ proofPath: `${outputPath}`,
159
+ pkPath: undefined,
160
+ vkPath: `${outputPath}`,
161
+ };
162
+ }
163
+ // Not a great error message here but it is difficult to decipher what comes from bb
164
+ return {
165
+ status: BB_RESULT.FAILURE,
166
+ reason: `Failed to generate proof. Exit code ${result.exitCode}. Signal ${result.signal}.`,
167
+ retry: !!result.signal,
168
+ };
169
+ } catch (error) {
170
+ return { status: BB_RESULT.FAILURE, reason: `${error}` };
171
+ }
172
+ }
173
+
174
+ function getArgs(flavor: UltraHonkFlavor) {
175
+ switch (flavor) {
176
+ case 'ultra_honk': {
177
+ return ['--scheme', 'ultra_honk', '--oracle_hash', 'poseidon2'];
178
+ }
179
+ case 'ultra_keccak_honk': {
180
+ return ['--scheme', 'ultra_honk', '--oracle_hash', 'keccak'];
181
+ }
182
+ case 'ultra_rollup_honk': {
183
+ return ['--scheme', 'ultra_honk', '--oracle_hash', 'poseidon2', '--ipa_accumulation'];
184
+ }
185
+ }
186
+ }
187
+
188
+ /**
189
+ * Used for generating proofs of noir circuits.
190
+ * It is assumed that the working directory is a temporary and/or random directory used solely for generating this proof.
191
+ * @param pathToBB - The full path to the bb binary
192
+ * @param workingDirectory - A working directory for use by bb
193
+ * @param circuitName - An identifier for the circuit
194
+ * @param bytecode - The compiled circuit bytecode
195
+ * @param inputWitnessFile - The circuit input witness
196
+ * @param log - A logging function
197
+ * @returns An object containing a result indication, the location of the proof and the duration taken
198
+ */
199
+ export async function generateProof(
200
+ pathToBB: string,
201
+ workingDirectory: string,
202
+ circuitName: string,
203
+ bytecode: Buffer,
204
+ recursive: boolean,
205
+ inputWitnessFile: string,
206
+ flavor: UltraHonkFlavor,
207
+ log: LogFn,
208
+ ): Promise<BBFailure | BBSuccess> {
209
+ // Check that the working directory exists
210
+ try {
211
+ await fs.access(workingDirectory);
212
+ } catch (error) {
213
+ return { status: BB_RESULT.FAILURE, reason: `Working directory ${workingDirectory} does not exist` };
214
+ }
215
+
216
+ // The bytecode is written to e.g. /workingDirectory/BaseParityArtifact-bytecode
217
+ const bytecodePath = `${workingDirectory}/${circuitName}-bytecode`;
218
+
219
+ // The proof is written to e.g. /workingDirectory/ultra_honk/proof
220
+ const outputPath = `${workingDirectory}`;
221
+
222
+ const binaryPresent = await fs
223
+ .access(pathToBB, fs.constants.R_OK)
224
+ .then(_ => true)
225
+ .catch(_ => false);
226
+ if (!binaryPresent) {
227
+ return { status: BB_RESULT.FAILURE, reason: `Failed to find bb binary at ${pathToBB}` };
228
+ }
229
+
230
+ try {
231
+ // Write the bytecode to the working directory
232
+ await fs.writeFile(bytecodePath, bytecode);
233
+ const args = getArgs(flavor).concat([
234
+ '--output_format',
235
+ 'bytes_and_fields',
236
+ '--write_vk',
237
+ '-o',
238
+ outputPath,
239
+ '-b',
240
+ bytecodePath,
241
+ '-w',
242
+ inputWitnessFile,
243
+ '-v',
244
+ ]);
245
+ if (recursive) {
246
+ args.push('--init_kzg_accumulator');
247
+ }
248
+ const timer = new Timer();
249
+ const logFunction = (message: string) => {
250
+ log(`${circuitName} BB out - ${message}`);
251
+ };
252
+ const result = await executeBB(pathToBB, `prove`, args, logFunction);
253
+ const duration = timer.ms();
254
+
255
+ if (result.status == BB_RESULT.SUCCESS) {
256
+ return {
257
+ status: BB_RESULT.SUCCESS,
258
+ durationMs: duration,
259
+ proofPath: `${outputPath}`,
260
+ pkPath: undefined,
261
+ vkPath: `${outputPath}`,
262
+ };
263
+ }
264
+ // Not a great error message here but it is difficult to decipher what comes from bb
265
+ return {
266
+ status: BB_RESULT.FAILURE,
267
+ reason: `Failed to generate proof. Exit code ${result.exitCode}. Signal ${result.signal}.`,
268
+ retry: !!result.signal,
269
+ };
270
+ } catch (error) {
271
+ return { status: BB_RESULT.FAILURE, reason: `${error}` };
272
+ }
273
+ }
274
+
275
+ /**
276
+ * Used for generating proofs of the tube circuit
277
+ * It is assumed that the working directory is a temporary and/or random directory used solely for generating this proof.
278
+ * @param pathToBB - The full path to the bb binary
279
+ * @param workingDirectory - A working directory for use by bb
280
+ * @param circuitName - An identifier for the circuit
281
+ * @param bytecode - The compiled circuit bytecode
282
+ * @param inputWitnessFile - The circuit input witness
283
+ * @param log - A logging function
284
+ * @returns An object containing a result indication, the location of the proof and the duration taken
285
+ */
286
+ export async function generateTubeProof(
287
+ pathToBB: string,
288
+ workingDirectory: string,
289
+ log: LogFn,
290
+ ): Promise<BBFailure | BBSuccess> {
291
+ // Check that the working directory exists
292
+ try {
293
+ await fs.access(workingDirectory);
294
+ } catch (error) {
295
+ return { status: BB_RESULT.FAILURE, reason: `Working directory ${workingDirectory} does not exist` };
296
+ }
297
+
298
+ // // Paths for the inputs
299
+ const vkPath = join(workingDirectory, CLIENT_IVC_VK_FILE_NAME);
300
+ const proofPath = join(workingDirectory, CLIENT_IVC_PROOF_FILE_NAME);
301
+
302
+ // The proof is written to e.g. /workingDirectory/proof
303
+ const outputPath = workingDirectory;
304
+ const filePresent = async (file: string) =>
305
+ await fs
306
+ .access(file, fs.constants.R_OK)
307
+ .then(_ => true)
308
+ .catch(_ => false);
309
+
310
+ const binaryPresent = await filePresent(pathToBB);
311
+ if (!binaryPresent) {
312
+ return { status: BB_RESULT.FAILURE, reason: `Failed to find bb binary at ${pathToBB}` };
313
+ }
314
+
315
+ try {
316
+ if (!(await filePresent(vkPath)) || !(await filePresent(proofPath))) {
317
+ return { status: BB_RESULT.FAILURE, reason: `Client IVC input files not present in ${workingDirectory}` };
318
+ }
319
+ const args = ['-o', outputPath, '-v'];
320
+
321
+ const timer = new Timer();
322
+ const logFunction = (message: string) => {
323
+ log(`TubeCircuit (prove) BB out - ${message}`);
324
+ };
325
+ const result = await executeBB(pathToBB, 'prove_tube', args, logFunction);
326
+ const durationMs = timer.ms();
327
+
328
+ if (result.status == BB_RESULT.SUCCESS) {
329
+ return {
330
+ status: BB_RESULT.SUCCESS,
331
+ durationMs,
332
+ proofPath: outputPath,
333
+ pkPath: undefined,
334
+ vkPath: outputPath,
335
+ };
336
+ }
337
+ // Not a great error message here but it is difficult to decipher what comes from bb
338
+ return {
339
+ status: BB_RESULT.FAILURE,
340
+ reason: `Failed to generate proof. Exit code ${result.exitCode}. Signal ${result.signal}.`,
341
+ retry: !!result.signal,
342
+ };
343
+ } catch (error) {
344
+ return { status: BB_RESULT.FAILURE, reason: `${error}` };
345
+ }
346
+ }
347
+
348
+ /**
349
+ * Used for generating AVM proofs.
350
+ * It is assumed that the working directory is a temporary and/or random directory used solely for generating this proof.
351
+ * @param pathToBB - The full path to the bb binary
352
+ * @param workingDirectory - A working directory for use by bb
353
+ * @param input - The inputs for the public function to be proven
354
+ * @param log - A logging function
355
+ * @returns An object containing a result indication, the location of the proof and the duration taken
356
+ */
357
+ export async function generateAvmProofV2(
358
+ pathToBB: string,
359
+ workingDirectory: string,
360
+ input: AvmCircuitInputs,
361
+ logger: Logger,
362
+ ): Promise<BBFailure | BBSuccess> {
363
+ // Check that the working directory exists
364
+ try {
365
+ await fs.access(workingDirectory);
366
+ } catch (error) {
367
+ return { status: BB_RESULT.FAILURE, reason: `Working directory ${workingDirectory} does not exist` };
368
+ }
369
+
370
+ // The proof is written to e.g. /workingDirectory/proof
371
+ const outputPath = workingDirectory;
372
+
373
+ const filePresent = async (file: string) =>
374
+ await fs
375
+ .access(file, fs.constants.R_OK)
376
+ .then(_ => true)
377
+ .catch(_ => false);
378
+
379
+ const binaryPresent = await filePresent(pathToBB);
380
+ if (!binaryPresent) {
381
+ return { status: BB_RESULT.FAILURE, reason: `Failed to find bb binary at ${pathToBB}` };
382
+ }
383
+
384
+ const inputsBuffer = input.serializeWithMessagePack();
385
+
386
+ try {
387
+ // Write the inputs to the working directory.
388
+ const avmInputsPath = join(workingDirectory, AVM_INPUTS_FILENAME);
389
+ await fs.writeFile(avmInputsPath, inputsBuffer);
390
+ if (!(await filePresent(avmInputsPath))) {
391
+ return { status: BB_RESULT.FAILURE, reason: `Could not write avm inputs to ${avmInputsPath}` };
392
+ }
393
+
394
+ const args = ['--avm-inputs', avmInputsPath, '-o', outputPath];
395
+ const loggingArg =
396
+ logger.level === 'debug' || logger.level === 'trace' ? '-d' : logger.level === 'verbose' ? '-v' : '';
397
+ if (loggingArg !== '') {
398
+ args.push(loggingArg);
399
+ }
400
+ const timer = new Timer();
401
+ const logFunction = (message: string) => {
402
+ logger.verbose(`AvmCircuit (prove) BB out - ${message}`);
403
+ };
404
+ const result = await executeBB(pathToBB, 'avm2_prove', args, logFunction);
405
+ const duration = timer.ms();
406
+
407
+ if (result.status == BB_RESULT.SUCCESS) {
408
+ return {
409
+ status: BB_RESULT.SUCCESS,
410
+ durationMs: duration,
411
+ proofPath: join(outputPath, PROOF_FILENAME),
412
+ pkPath: undefined,
413
+ vkPath: outputPath,
414
+ };
415
+ }
416
+ // Not a great error message here but it is difficult to decipher what comes from bb
417
+ return {
418
+ status: BB_RESULT.FAILURE,
419
+ reason: `Failed to generate proof. Exit code ${result.exitCode}. Signal ${result.signal}.`,
420
+ retry: !!result.signal,
421
+ };
422
+ } catch (error) {
423
+ return { status: BB_RESULT.FAILURE, reason: `${error}` };
424
+ }
425
+ }
426
+
427
+ /**
428
+ * Used for generating AVM proofs (or doing check-circuit).
429
+ * It is assumed that the working directory is a temporary and/or random directory used solely for generating this proof.
430
+ * @param pathToBB - The full path to the bb binary
431
+ * @param workingDirectory - A working directory for use by bb
432
+ * @param bytecode - The AVM bytecode for the public function to be proven (expected to be decompressed)
433
+ * @param log - A logging function
434
+ * @returns An object containing a result indication, the location of the proof and the duration taken
435
+ */
436
+ export async function generateAvmProof(
437
+ pathToBB: string,
438
+ workingDirectory: string,
439
+ _input: AvmCircuitInputs,
440
+ logger: Logger,
441
+ checkCircuitOnly: boolean = false,
442
+ ): Promise<BBFailure | BBSuccess> {
443
+ // Check that the working directory exists
444
+ try {
445
+ await fs.access(workingDirectory);
446
+ } catch (error) {
447
+ return { status: BB_RESULT.FAILURE, reason: `Working directory ${workingDirectory} does not exist` };
448
+ }
449
+
450
+ // Paths for the inputs
451
+ const publicInputsPath = join(workingDirectory, AVM_PUBLIC_INPUTS_FILENAME);
452
+ const avmHintsPath = join(workingDirectory, AVM_HINTS_FILENAME);
453
+
454
+ // The proof is written to e.g. /workingDirectory/proof
455
+ const outputPath = workingDirectory;
456
+
457
+ const filePresent = async (file: string) =>
458
+ await fs
459
+ .access(file, fs.constants.R_OK)
460
+ .then(_ => true)
461
+ .catch(_ => false);
462
+
463
+ const binaryPresent = await filePresent(pathToBB);
464
+ if (!binaryPresent) {
465
+ return { status: BB_RESULT.FAILURE, reason: `Failed to find bb binary at ${pathToBB}` };
466
+ }
467
+
468
+ try {
469
+ // Write the inputs to the working directory.
470
+
471
+ // WARNING: Not writing the inputs since VM1 is disabled!
472
+ // await fs.writeFile(publicInputsPath, input.publicInputs.toBuffer());
473
+ // if (!(await filePresent(publicInputsPath))) {
474
+ // return { status: BB_RESULT.FAILURE, reason: `Could not write publicInputs at ${publicInputsPath}` };
475
+ // }
476
+
477
+ // await fs.writeFile(avmHintsPath, input.avmHints.toBuffer());
478
+ // if (!(await filePresent(avmHintsPath))) {
479
+ // return { status: BB_RESULT.FAILURE, reason: `Could not write avmHints at ${avmHintsPath}` };
480
+ // }
481
+
482
+ const args = ['--avm-public-inputs', publicInputsPath, '--avm-hints', avmHintsPath, '-o', outputPath];
483
+ const loggingArg =
484
+ logger.level === 'debug' || logger.level === 'trace' ? '-d' : logger.level === 'verbose' ? '-v' : '';
485
+ if (loggingArg !== '') {
486
+ args.push(loggingArg);
487
+ }
488
+
489
+ const timer = new Timer();
490
+ const cmd = checkCircuitOnly ? 'check_circuit' : 'prove';
491
+ const logFunction = (message: string) => {
492
+ logger.verbose(`AvmCircuit (${cmd}) BB out - ${message}`);
493
+ };
494
+ const result = await executeBB(pathToBB, `avm_${cmd}`, args, logFunction);
495
+ const duration = timer.ms();
496
+
497
+ if (result.status == BB_RESULT.SUCCESS) {
498
+ return {
499
+ status: BB_RESULT.SUCCESS,
500
+ durationMs: duration,
501
+ proofPath: join(outputPath, PROOF_FILENAME),
502
+ pkPath: undefined,
503
+ vkPath: outputPath,
504
+ };
505
+ }
506
+ // Not a great error message here but it is difficult to decipher what comes from bb
507
+ return {
508
+ status: BB_RESULT.FAILURE,
509
+ reason: `Failed to generate proof. Exit code ${result.exitCode}. Signal ${result.signal}.`,
510
+ retry: !!result.signal,
511
+ };
512
+ } catch (error) {
513
+ return { status: BB_RESULT.FAILURE, reason: `${error}` };
514
+ }
515
+ }
516
+
517
+ /**
518
+ * Used for verifying proofs of noir circuits
519
+ * @param pathToBB - The full path to the bb binary
520
+ * @param proofFullPath - The full path to the proof to be verified
521
+ * @param verificationKeyPath - The full path to the circuit verification key
522
+ * @param log - A logging function
523
+ * @returns An object containing a result indication and duration taken
524
+ */
525
+ export async function verifyProof(
526
+ pathToBB: string,
527
+ proofFullPath: string,
528
+ verificationKeyPath: string,
529
+ ultraHonkFlavor: UltraHonkFlavor,
530
+ log: Logger,
531
+ ): Promise<BBFailure | BBSuccess> {
532
+ return await verifyProofInternal(
533
+ pathToBB,
534
+ proofFullPath,
535
+ verificationKeyPath,
536
+ `verify`,
537
+ log,
538
+ getArgs(ultraHonkFlavor),
539
+ );
540
+ }
541
+
542
+ /**
543
+ * Used for verifying proofs of the AVM
544
+ * @param pathToBB - The full path to the bb binary
545
+ * @param proofFullPath - The full path to the proof to be verified
546
+ * @param verificationKeyPath - The full path to the circuit verification key
547
+ * @param log - A logging function
548
+ * @returns An object containing a result indication and duration taken
549
+ */
550
+ export async function verifyAvmProof(
551
+ pathToBB: string,
552
+ proofFullPath: string,
553
+ verificationKeyPath: string,
554
+ logger: Logger,
555
+ ): Promise<BBFailure | BBSuccess> {
556
+ return await verifyProofInternal(pathToBB, proofFullPath, verificationKeyPath, 'avm_verify', logger);
557
+ }
558
+
559
+ export async function verifyAvmProofV2(
560
+ pathToBB: string,
561
+ workingDirectory: string,
562
+ proofFullPath: string,
563
+ publicInputs: AvmCircuitPublicInputs,
564
+ verificationKeyPath: string,
565
+ logger: Logger,
566
+ ): Promise<BBFailure | BBSuccess> {
567
+ const inputsBuffer = publicInputs.serializeWithMessagePack();
568
+
569
+ // Write the inputs to the working directory.
570
+ const filePresent = async (file: string) =>
571
+ await fs
572
+ .access(file, fs.constants.R_OK)
573
+ .then(_ => true)
574
+ .catch(_ => false);
575
+ const avmInputsPath = join(workingDirectory, 'avm_public_inputs.bin');
576
+ await fs.writeFile(avmInputsPath, inputsBuffer);
577
+ if (!(await filePresent(avmInputsPath))) {
578
+ return { status: BB_RESULT.FAILURE, reason: `Could not write avm inputs to ${avmInputsPath}` };
579
+ }
580
+
581
+ return await verifyProofInternal(pathToBB, proofFullPath, verificationKeyPath, 'avm2_verify', logger, [
582
+ '--avm-public-inputs',
583
+ avmInputsPath,
584
+ ]);
585
+ }
586
+
587
+ /**
588
+ * Verifies a ClientIvcProof
589
+ * TODO(#7370) The verification keys should be supplied separately
590
+ * @param pathToBB - The full path to the bb binary
591
+ * @param targetPath - The path to the folder with the proof, accumulator, and verification keys
592
+ * @param log - A logging function
593
+ * @returns An object containing a result indication and duration taken
594
+ */
595
+ export async function verifyClientIvcProof(
596
+ pathToBB: string,
597
+ proofPath: string,
598
+ keyPath: string,
599
+ log: LogFn,
600
+ ): Promise<BBFailure | BBSuccess> {
601
+ const binaryPresent = await fs
602
+ .access(pathToBB, fs.constants.R_OK)
603
+ .then(_ => true)
604
+ .catch(_ => false);
605
+ if (!binaryPresent) {
606
+ return { status: BB_RESULT.FAILURE, reason: `Failed to find bb binary at ${pathToBB}` };
607
+ }
608
+
609
+ try {
610
+ const args = ['--scheme', 'client_ivc', '-p', proofPath, '-k', keyPath];
611
+ const timer = new Timer();
612
+ const command = 'verify';
613
+ const result = await executeBB(pathToBB, command, args, log);
614
+ const duration = timer.ms();
615
+ if (result.status == BB_RESULT.SUCCESS) {
616
+ return { status: BB_RESULT.SUCCESS, durationMs: duration };
617
+ }
618
+ // Not a great error message here but it is difficult to decipher what comes from bb
619
+ return {
620
+ status: BB_RESULT.FAILURE,
621
+ reason: `Failed to verify proof. Exit code ${result.exitCode}. Signal ${result.signal}.`,
622
+ retry: !!result.signal,
623
+ };
624
+ } catch (error) {
625
+ return { status: BB_RESULT.FAILURE, reason: `${error}` };
626
+ }
627
+ }
628
+
629
+ /**
630
+ * Used for verifying proofs with BB
631
+ * @param pathToBB - The full path to the bb binary
632
+ * @param proofFullPath - The full path to the proof to be verified
633
+ * @param verificationKeyPath - The full path to the circuit verification key
634
+ * @param command - The BB command to execute (verify/avm_verify)
635
+ * @param log - A logging function
636
+ * @returns An object containing a result indication and duration taken
637
+ */
638
+ async function verifyProofInternal(
639
+ pathToBB: string,
640
+ proofFullPath: string,
641
+ verificationKeyPath: string,
642
+ command: 'verify' | 'avm_verify' | 'avm2_verify',
643
+ logger: Logger,
644
+ extraArgs: string[] = [],
645
+ ): Promise<BBFailure | BBSuccess> {
646
+ const binaryPresent = await fs
647
+ .access(pathToBB, fs.constants.R_OK)
648
+ .then(_ => true)
649
+ .catch(_ => false);
650
+ if (!binaryPresent) {
651
+ return { status: BB_RESULT.FAILURE, reason: `Failed to find bb binary at ${pathToBB}` };
652
+ }
653
+
654
+ const logFunction = (message: string) => {
655
+ logger.verbose(`bb-prover (verify) BB out - ${message}`);
656
+ };
657
+
658
+ try {
659
+ const args = ['-p', proofFullPath, '-k', verificationKeyPath, ...extraArgs];
660
+ const loggingArg =
661
+ logger.level === 'debug' || logger.level === 'trace' ? '-d' : logger.level === 'verbose' ? '-v' : '';
662
+ if (loggingArg !== '') {
663
+ args.push(loggingArg);
664
+ }
665
+ const timer = new Timer();
666
+ const result = await executeBB(pathToBB, command, args, logFunction);
667
+ const duration = timer.ms();
668
+ if (result.status == BB_RESULT.SUCCESS) {
669
+ return { status: BB_RESULT.SUCCESS, durationMs: duration };
670
+ }
671
+ // Not a great error message here but it is difficult to decipher what comes from bb
672
+ return {
673
+ status: BB_RESULT.FAILURE,
674
+ reason: `Failed to verify proof. Exit code ${result.exitCode}. Signal ${result.signal}.`,
675
+ retry: !!result.signal,
676
+ };
677
+ } catch (error) {
678
+ return { status: BB_RESULT.FAILURE, reason: `${error}` };
679
+ }
680
+ }
681
+
682
+ export async function generateContractForVerificationKey(
683
+ pathToBB: string,
684
+ vkFilePath: string,
685
+ contractPath: string,
686
+ log: LogFn,
687
+ ): Promise<BBFailure | BBSuccess> {
688
+ const binaryPresent = await fs
689
+ .access(pathToBB, fs.constants.R_OK)
690
+ .then(_ => true)
691
+ .catch(_ => false);
692
+
693
+ if (!binaryPresent) {
694
+ return { status: BB_RESULT.FAILURE, reason: `Failed to find bb binary at ${pathToBB}` };
695
+ }
696
+
697
+ const outputDir = dirname(contractPath);
698
+ const contractName = basename(contractPath);
699
+ // cache contract generation based on vk file and contract name
700
+ const cacheKey = sha256(Buffer.concat([Buffer.from(contractName), await fs.readFile(vkFilePath)]));
701
+
702
+ await fs.mkdir(outputDir, { recursive: true });
703
+
704
+ const res = await fsCache<BBSuccess | BBFailure>(outputDir, cacheKey, log, false, async () => {
705
+ try {
706
+ const args = ['--scheme', 'ultra_honk', '-k', vkFilePath, '-o', contractPath, '-v'];
707
+ const timer = new Timer();
708
+ const result = await executeBB(pathToBB, 'contract', args, log);
709
+ const duration = timer.ms();
710
+ if (result.status == BB_RESULT.SUCCESS) {
711
+ return { status: BB_RESULT.SUCCESS, durationMs: duration, contractPath };
712
+ }
713
+ // Not a great error message here but it is difficult to decipher what comes from bb
714
+ return {
715
+ status: BB_RESULT.FAILURE,
716
+ reason: `Failed to write verifier contract. Exit code ${result.exitCode}. Signal ${result.signal}.`,
717
+ retry: !!result.signal,
718
+ };
719
+ } catch (error) {
720
+ return { status: BB_RESULT.FAILURE, reason: `${error}` };
721
+ }
722
+ });
723
+
724
+ if (!res) {
725
+ return {
726
+ status: BB_RESULT.ALREADY_PRESENT,
727
+ durationMs: 0,
728
+ contractPath,
729
+ };
730
+ }
731
+
732
+ return res;
733
+ }
734
+
735
+ /**
736
+ * Compute bb gate count for a given circuit
737
+ * @param pathToBB - The full path to the bb binary
738
+ * @param workingDirectory - A temporary directory for writing the bytecode
739
+ * @param circuitName - The name of the circuit
740
+ * @param bytecode - The bytecode of the circuit
741
+ * @param flavor - The flavor of the backend - mega_honk or ultra_honk variants
742
+ * @returns An object containing the status, gate count, and time taken
743
+ */
744
+ export async function computeGateCountForCircuit(
745
+ pathToBB: string,
746
+ workingDirectory: string,
747
+ circuitName: string,
748
+ bytecode: Buffer,
749
+ flavor: UltraHonkFlavor | 'mega_honk',
750
+ log: LogFn,
751
+ ): Promise<BBFailure | BBSuccess> {
752
+ // Check that the working directory exists
753
+ try {
754
+ await fs.access(workingDirectory);
755
+ } catch (error) {
756
+ return { status: BB_RESULT.FAILURE, reason: `Working directory ${workingDirectory} does not exist` };
757
+ }
758
+
759
+ // The bytecode is written to e.g. /workingDirectory/BaseParityArtifact-bytecode
760
+ const bytecodePath = `${workingDirectory}/${circuitName}-bytecode`;
761
+
762
+ const binaryPresent = await fs
763
+ .access(pathToBB, fs.constants.R_OK)
764
+ .then(_ => true)
765
+ .catch(_ => false);
766
+ if (!binaryPresent) {
767
+ return { status: BB_RESULT.FAILURE, reason: `Failed to find bb binary at ${pathToBB}` };
768
+ }
769
+
770
+ // Accumulate the stdout from bb
771
+ let stdout = '';
772
+ const logHandler = (message: string) => {
773
+ stdout += message;
774
+ log(message);
775
+ };
776
+
777
+ try {
778
+ // Write the bytecode to the working directory
779
+ await fs.writeFile(bytecodePath, bytecode);
780
+ const timer = new Timer();
781
+
782
+ const result = await executeBB(
783
+ pathToBB,
784
+ 'gates',
785
+ ['--scheme', flavor === 'mega_honk' ? 'client_ivc' : 'ultra_honk', '-b', bytecodePath, '-v'],
786
+ logHandler,
787
+ );
788
+ const duration = timer.ms();
789
+
790
+ if (result.status == BB_RESULT.SUCCESS) {
791
+ // Look for "circuit_size" in the stdout and parse the number
792
+ const circuitSizeMatch = stdout.match(/circuit_size": (\d+)/);
793
+ if (!circuitSizeMatch) {
794
+ return { status: BB_RESULT.FAILURE, reason: 'Failed to parse circuit_size from bb gates stdout.' };
795
+ }
796
+ const circuitSize = parseInt(circuitSizeMatch[1]);
797
+
798
+ return {
799
+ status: BB_RESULT.SUCCESS,
800
+ durationMs: duration,
801
+ circuitSize: circuitSize,
802
+ };
803
+ }
804
+
805
+ return { status: BB_RESULT.FAILURE, reason: 'Failed getting the gate count.' };
806
+ } catch (error) {
807
+ return { status: BB_RESULT.FAILURE, reason: `${error}` };
808
+ }
809
+ }
810
+
811
+ const CACHE_FILENAME = '.cache';
812
+ async function fsCache<T>(
813
+ dir: string,
814
+ expectedCacheKey: Buffer,
815
+ logger: LogFn,
816
+ force: boolean,
817
+ action: () => Promise<T>,
818
+ ): Promise<T | undefined> {
819
+ const cacheFilePath = join(dir, CACHE_FILENAME);
820
+
821
+ let run: boolean;
822
+ if (force) {
823
+ run = true;
824
+ } else {
825
+ try {
826
+ run = !expectedCacheKey.equals(await fs.readFile(cacheFilePath));
827
+ } catch (err: any) {
828
+ if (err && 'code' in err && err.code === 'ENOENT') {
829
+ // cache file doesn't exist, swallow error and run
830
+ run = true;
831
+ } else {
832
+ throw err;
833
+ }
834
+ }
835
+ }
836
+
837
+ let res: T | undefined;
838
+ if (run) {
839
+ logger(`Cache miss or forced run. Running operation in ${dir}...`);
840
+ res = await action();
841
+ } else {
842
+ logger(`Cache hit. Skipping operation in ${dir}...`);
843
+ }
844
+
845
+ try {
846
+ await fs.writeFile(cacheFilePath, expectedCacheKey);
847
+ } catch (err) {
848
+ logger(`Couldn't write cache data to ${cacheFilePath}. Skipping cache...`);
849
+ // ignore
850
+ }
851
+
852
+ return res;
853
+ }