@aztec/bb-prover 0.0.1-commit.03f7ef2 → 0.0.1-commit.04d373f

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 (84) hide show
  1. package/dest/avm_proving_tests/avm_proving_tester.d.ts +13 -8
  2. package/dest/avm_proving_tests/avm_proving_tester.d.ts.map +1 -1
  3. package/dest/avm_proving_tests/avm_proving_tester.js +152 -107
  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/file_names.d.ts +4 -0
  11. package/dest/bb/file_names.d.ts.map +1 -0
  12. package/dest/bb/file_names.js +5 -0
  13. package/dest/config.d.ts +17 -1
  14. package/dest/config.d.ts.map +1 -1
  15. package/dest/index.d.ts +3 -2
  16. package/dest/index.d.ts.map +1 -1
  17. package/dest/index.js +2 -1
  18. package/dest/instrumentation.d.ts +1 -1
  19. package/dest/instrumentation.d.ts.map +1 -1
  20. package/dest/instrumentation.js +21 -43
  21. package/dest/prover/client/bb_private_kernel_prover.d.ts +18 -4
  22. package/dest/prover/client/bb_private_kernel_prover.d.ts.map +1 -1
  23. package/dest/prover/client/bb_private_kernel_prover.js +54 -8
  24. package/dest/prover/client/bundle.d.ts +3 -3
  25. package/dest/prover/client/bundle.d.ts.map +1 -1
  26. package/dest/prover/client/bundle.js +2 -3
  27. package/dest/prover/client/lazy.d.ts +3 -3
  28. package/dest/prover/client/lazy.d.ts.map +1 -1
  29. package/dest/prover/client/lazy.js +2 -3
  30. package/dest/prover/proof_utils.d.ts +11 -1
  31. package/dest/prover/proof_utils.d.ts.map +1 -1
  32. package/dest/prover/proof_utils.js +24 -1
  33. package/dest/prover/server/bb_prover.d.ts +8 -12
  34. package/dest/prover/server/bb_prover.d.ts.map +1 -1
  35. package/dest/prover/server/bb_prover.js +612 -107
  36. package/dest/test/delay_values.d.ts +1 -1
  37. package/dest/test/delay_values.d.ts.map +1 -1
  38. package/dest/test/delay_values.js +27 -25
  39. package/dest/test/index.d.ts +2 -1
  40. package/dest/test/index.d.ts.map +1 -1
  41. package/dest/test/index.js +1 -0
  42. package/dest/test/test_circuit_prover.d.ts +4 -4
  43. package/dest/test/test_circuit_prover.d.ts.map +1 -1
  44. package/dest/test/test_circuit_prover.js +462 -59
  45. package/dest/verification_key/verification_key_data.d.ts +1 -8
  46. package/dest/verification_key/verification_key_data.d.ts.map +1 -1
  47. package/dest/verification_key/verification_key_data.js +1 -20
  48. package/dest/verifier/batch_chonk_verifier.d.ts +45 -0
  49. package/dest/verifier/batch_chonk_verifier.d.ts.map +1 -0
  50. package/dest/verifier/batch_chonk_verifier.js +232 -0
  51. package/dest/verifier/bb_verifier.d.ts +4 -1
  52. package/dest/verifier/bb_verifier.d.ts.map +1 -1
  53. package/dest/verifier/bb_verifier.js +134 -48
  54. package/dest/verifier/index.d.ts +2 -1
  55. package/dest/verifier/index.d.ts.map +1 -1
  56. package/dest/verifier/index.js +1 -0
  57. package/dest/verifier/queued_chonk_verifier.d.ts +2 -3
  58. package/dest/verifier/queued_chonk_verifier.d.ts.map +1 -1
  59. package/dest/verifier/queued_chonk_verifier.js +15 -45
  60. package/package.json +20 -18
  61. package/src/avm_proving_tests/avm_proving_tester.ts +53 -136
  62. package/src/bb/bb_js_backend.ts +435 -0
  63. package/src/bb/bb_js_debug.ts +227 -0
  64. package/src/bb/file_names.ts +6 -0
  65. package/src/config.ts +16 -0
  66. package/src/index.ts +2 -1
  67. package/src/instrumentation.ts +20 -43
  68. package/src/prover/client/bb_private_kernel_prover.ts +135 -9
  69. package/src/prover/client/bundle.ts +3 -4
  70. package/src/prover/client/lazy.ts +3 -4
  71. package/src/prover/proof_utils.ts +41 -1
  72. package/src/prover/server/bb_prover.ts +138 -153
  73. package/src/test/delay_values.ts +28 -24
  74. package/src/test/index.ts +1 -0
  75. package/src/test/test_circuit_prover.ts +10 -13
  76. package/src/verification_key/verification_key_data.ts +1 -26
  77. package/src/verifier/batch_chonk_verifier.ts +276 -0
  78. package/src/verifier/bb_verifier.ts +66 -80
  79. package/src/verifier/index.ts +1 -0
  80. package/src/verifier/queued_chonk_verifier.ts +15 -47
  81. package/dest/bb/execute.d.ts +0 -107
  82. package/dest/bb/execute.d.ts.map +0 -1
  83. package/dest/bb/execute.js +0 -676
  84. package/src/bb/execute.ts +0 -706
package/src/bb/execute.ts DELETED
@@ -1,706 +0,0 @@
1
- import { sha256 } from '@aztec/foundation/crypto/sha256';
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
- import readline from 'readline';
10
-
11
- import type { UltraHonkFlavor } from '../honk.js';
12
-
13
- export const VK_FILENAME = 'vk';
14
- export const PUBLIC_INPUTS_FILENAME = 'public_inputs';
15
- export const PROOF_FILENAME = 'proof';
16
- export const AVM_INPUTS_FILENAME = 'avm_inputs.bin';
17
- export const AVM_BYTECODE_FILENAME = 'avm_bytecode.bin';
18
- export const AVM_PUBLIC_INPUTS_FILENAME = 'avm_public_inputs.bin';
19
-
20
- export enum BB_RESULT {
21
- SUCCESS,
22
- FAILURE,
23
- ALREADY_PRESENT,
24
- }
25
-
26
- export type BBSuccess = {
27
- status: BB_RESULT.SUCCESS | BB_RESULT.ALREADY_PRESENT;
28
- durationMs: number;
29
- /** Full path of the public key. */
30
- pkPath?: string;
31
- /** Base directory for the VKs (raw, fields). */
32
- vkDirectoryPath?: string;
33
- /** Full path of the proof. */
34
- proofPath?: string;
35
- /** Full path of the contract. */
36
- contractPath?: string;
37
- /** The number of gates in the circuit. */
38
- circuitSize?: number;
39
- };
40
-
41
- export type BBFailure = {
42
- status: BB_RESULT.FAILURE;
43
- reason: string;
44
- retry?: boolean;
45
- };
46
-
47
- export type BBResult = BBSuccess | BBFailure;
48
-
49
- type BBExecResult = {
50
- status: BB_RESULT;
51
- exitCode: number;
52
- signal: string | undefined;
53
- };
54
-
55
- /**
56
- * Invokes the Barretenberg binary with the provided command and args
57
- * @param pathToBB - The path to the BB binary
58
- * @param command - The command to execute
59
- * @param args - The arguments to pass
60
- * @param logger - A log function
61
- * @param timeout - An optional timeout before killing the BB process
62
- * @param resultParser - An optional handler for detecting success or failure
63
- * @returns The completed partial witness outputted from the circuit
64
- */
65
- export function executeBB(
66
- pathToBB: string,
67
- command: string,
68
- args: string[],
69
- logger: LogFn,
70
- concurrency?: number,
71
- timeout?: number,
72
- resultParser = (code: number) => code === 0,
73
- ): Promise<BBExecResult> {
74
- return new Promise<BBExecResult>(resolve => {
75
- // spawn the bb process
76
- const { HARDWARE_CONCURRENCY: _, ...envWithoutConcurrency } = process.env;
77
-
78
- const env = envWithoutConcurrency;
79
- // We prioritise the concurrency argument if provided and > 0
80
- if (concurrency && concurrency > 0) {
81
- env.HARDWARE_CONCURRENCY = concurrency.toString();
82
- } else if (process.env.HARDWARE_CONCURRENCY) {
83
- env.HARDWARE_CONCURRENCY = process.env.HARDWARE_CONCURRENCY;
84
- }
85
-
86
- logger(`BB concurrency: ${env.HARDWARE_CONCURRENCY}`);
87
- logger(`Executing BB with: ${pathToBB} ${command} ${args.join(' ')}`);
88
- const bb = proc.spawn(pathToBB, [command, ...args], {
89
- stdio: ['ignore', 'pipe', 'pipe'],
90
- env,
91
- });
92
-
93
- let timeoutId: NodeJS.Timeout | undefined;
94
- if (timeout !== undefined) {
95
- timeoutId = setTimeout(() => {
96
- logger(`BB execution timed out after ${timeout}ms, killing process`);
97
- if (bb.pid) {
98
- bb.kill('SIGKILL');
99
- }
100
- resolve({ status: BB_RESULT.FAILURE, exitCode: -1, signal: 'TIMEOUT' });
101
- }, timeout);
102
- }
103
-
104
- readline.createInterface({ input: bb.stdout }).on('line', logger);
105
- readline.createInterface({ input: bb.stderr }).on('line', logger);
106
-
107
- bb.on('close', (exitCode: number, signal?: string) => {
108
- if (timeoutId) {
109
- clearTimeout(timeoutId);
110
- }
111
- if (resultParser(exitCode)) {
112
- resolve({ status: BB_RESULT.SUCCESS, exitCode, signal });
113
- } else {
114
- resolve({ status: BB_RESULT.FAILURE, exitCode, signal });
115
- }
116
- });
117
- }).catch(_ => ({ status: BB_RESULT.FAILURE, exitCode: -1, signal: undefined }));
118
- }
119
-
120
- export async function executeBbChonkProof(
121
- pathToBB: string,
122
- workingDirectory: string,
123
- inputsPath: string,
124
- log: LogFn,
125
- writeVk = false,
126
- ): Promise<BBFailure | BBSuccess> {
127
- // Check that the working directory exists
128
- try {
129
- await fs.access(workingDirectory);
130
- } catch {
131
- return { status: BB_RESULT.FAILURE, reason: `Working directory ${workingDirectory} does not exist` };
132
- }
133
-
134
- // The proof is written to e.g. /workingDirectory/proof
135
- const outputPath = `${workingDirectory}`;
136
-
137
- const binaryPresent = await fs
138
- .access(pathToBB, fs.constants.R_OK)
139
- .then(_ => true)
140
- .catch(_ => false);
141
- if (!binaryPresent) {
142
- return { status: BB_RESULT.FAILURE, reason: `Failed to find bb binary at ${pathToBB}` };
143
- }
144
-
145
- try {
146
- // Write the bytecode to the working directory
147
- log(`inputsPath ${inputsPath}`);
148
- const timer = new Timer();
149
- const logFunction = (message: string) => {
150
- log(`bb - ${message}`);
151
- };
152
-
153
- const args = ['-o', outputPath, '--ivc_inputs_path', inputsPath, '-v', '--scheme', 'chonk'];
154
- if (writeVk) {
155
- args.push('--write_vk');
156
- }
157
- const result = await executeBB(pathToBB, 'prove', args, logFunction);
158
- const durationMs = timer.ms();
159
-
160
- if (result.status == BB_RESULT.SUCCESS) {
161
- return {
162
- status: BB_RESULT.SUCCESS,
163
- durationMs,
164
- proofPath: `${outputPath}`,
165
- pkPath: undefined,
166
- vkDirectoryPath: `${outputPath}`,
167
- };
168
- }
169
- // Not a great error message here but it is difficult to decipher what comes from bb
170
- return {
171
- status: BB_RESULT.FAILURE,
172
- reason: `Failed to generate proof. Exit code ${result.exitCode}. Signal ${result.signal}.`,
173
- retry: !!result.signal,
174
- };
175
- } catch (error) {
176
- return { status: BB_RESULT.FAILURE, reason: `${error}` };
177
- }
178
- }
179
-
180
- function getArgs(flavor: UltraHonkFlavor) {
181
- switch (flavor) {
182
- case 'ultra_honk': {
183
- return ['--scheme', 'ultra_honk', '--oracle_hash', 'poseidon2'];
184
- }
185
- case 'ultra_keccak_honk': {
186
- return ['--scheme', 'ultra_honk', '--oracle_hash', 'keccak'];
187
- }
188
- case 'ultra_starknet_honk': {
189
- return ['--scheme', 'ultra_honk', '--oracle_hash', 'starknet'];
190
- }
191
- case 'ultra_rollup_honk': {
192
- return ['--scheme', 'ultra_honk', '--oracle_hash', 'poseidon2', '--ipa_accumulation'];
193
- }
194
- }
195
- }
196
-
197
- /**
198
- * Used for generating proofs of noir circuits.
199
- * It is assumed that the working directory is a temporary and/or random directory used solely for generating this proof.
200
- * @param pathToBB - The full path to the bb binary
201
- * @param workingDirectory - A working directory for use by bb
202
- * @param circuitName - An identifier for the circuit
203
- * @param bytecode - The compiled circuit bytecode
204
- * @param inputWitnessFile - The circuit input witness
205
- * @param log - A logging function
206
- * @returns An object containing a result indication, the location of the proof and the duration taken
207
- */
208
- export async function generateProof(
209
- pathToBB: string,
210
- workingDirectory: string,
211
- circuitName: string,
212
- bytecode: Buffer,
213
- verificationKey: Buffer,
214
- inputWitnessFile: string,
215
- flavor: UltraHonkFlavor,
216
- log: Logger,
217
- ): Promise<BBFailure | BBSuccess> {
218
- // Check that the working directory exists
219
- try {
220
- await fs.access(workingDirectory);
221
- } catch {
222
- return { status: BB_RESULT.FAILURE, reason: `Working directory ${workingDirectory} does not exist` };
223
- }
224
-
225
- // The bytecode is written to e.g. /workingDirectory/ParityBaseArtifact-bytecode
226
- const bytecodePath = `${workingDirectory}/${circuitName}-bytecode`;
227
- const vkPath = `${workingDirectory}/${circuitName}-vk`;
228
-
229
- // The proof is written to e.g. /workingDirectory/ultra_honk/proof
230
- const outputPath = `${workingDirectory}`;
231
-
232
- const binaryPresent = await fs
233
- .access(pathToBB, fs.constants.R_OK)
234
- .then(_ => true)
235
- .catch(_ => false);
236
- if (!binaryPresent) {
237
- return { status: BB_RESULT.FAILURE, reason: `Failed to find bb binary at ${pathToBB}` };
238
- }
239
-
240
- try {
241
- // Write the bytecode and vk to the working directory
242
- await Promise.all([fs.writeFile(bytecodePath, bytecode), fs.writeFile(vkPath, verificationKey)]);
243
- const args = getArgs(flavor).concat([
244
- '--disable_zk',
245
- '-o',
246
- outputPath,
247
- '-b',
248
- bytecodePath,
249
- '-k',
250
- vkPath,
251
- '-w',
252
- inputWitnessFile,
253
- '-v',
254
- ]);
255
- const loggingArg = log.level === 'debug' || log.level === 'trace' ? '-d' : log.level === 'verbose' ? '-v' : '';
256
- if (loggingArg !== '') {
257
- args.push(loggingArg);
258
- }
259
-
260
- const timer = new Timer();
261
- const logFunction = (message: string) => {
262
- log.info(`${circuitName} BB out - ${message}`);
263
- };
264
- const result = await executeBB(pathToBB, `prove`, args, logFunction);
265
- const duration = timer.ms();
266
-
267
- if (result.status == BB_RESULT.SUCCESS) {
268
- return {
269
- status: BB_RESULT.SUCCESS,
270
- durationMs: duration,
271
- proofPath: `${outputPath}`,
272
- pkPath: undefined,
273
- vkDirectoryPath: `${outputPath}`,
274
- };
275
- }
276
- // Not a great error message here but it is difficult to decipher what comes from bb
277
- return {
278
- status: BB_RESULT.FAILURE,
279
- reason: `Failed to generate proof. Exit code ${result.exitCode}. Signal ${result.signal}.`,
280
- retry: !!result.signal,
281
- };
282
- } catch (error) {
283
- return { status: BB_RESULT.FAILURE, reason: `${error}` };
284
- }
285
- }
286
-
287
- /**
288
- * Used for generating AVM proofs.
289
- * It is assumed that the working directory is a temporary and/or random directory used solely for generating this proof.
290
- * @param pathToBB - The full path to the bb binary
291
- * @param workingDirectory - A working directory for use by bb
292
- * @param input - The inputs for the public function to be proven
293
- * @param logger - A logging function
294
- * @param checkCircuitOnly - A boolean to toggle a "check-circuit only" operation instead of proving.
295
- * @returns An object containing a result indication, the location of the proof and the duration taken
296
- */
297
- export async function generateAvmProof(
298
- pathToBB: string,
299
- workingDirectory: string,
300
- input: AvmCircuitInputs,
301
- logger: Logger,
302
- checkCircuitOnly: boolean = false,
303
- ): Promise<BBFailure | BBSuccess> {
304
- // Check that the working directory exists
305
- try {
306
- await fs.access(workingDirectory);
307
- } catch {
308
- return { status: BB_RESULT.FAILURE, reason: `Working directory ${workingDirectory} does not exist` };
309
- }
310
-
311
- // The proof is written to e.g. /workingDirectory/proof
312
- const outputPath = workingDirectory;
313
-
314
- const filePresent = async (file: string) =>
315
- await fs
316
- .access(file, fs.constants.R_OK)
317
- .then(_ => true)
318
- .catch(_ => false);
319
-
320
- const binaryPresent = await filePresent(pathToBB);
321
- if (!binaryPresent) {
322
- return { status: BB_RESULT.FAILURE, reason: `Failed to find bb binary at ${pathToBB}` };
323
- }
324
-
325
- const inputsBuffer = input.serializeWithMessagePack();
326
-
327
- try {
328
- // Write the inputs to the working directory.
329
- const avmInputsPath = join(workingDirectory, AVM_INPUTS_FILENAME);
330
- await fs.writeFile(avmInputsPath, inputsBuffer);
331
- if (!(await filePresent(avmInputsPath))) {
332
- return { status: BB_RESULT.FAILURE, reason: `Could not write avm inputs to ${avmInputsPath}` };
333
- }
334
-
335
- const args = checkCircuitOnly ? ['--avm-inputs', avmInputsPath] : ['--avm-inputs', avmInputsPath, '-o', outputPath];
336
- const loggingArg =
337
- logger.level === 'debug' || logger.level === 'trace' ? '-d' : logger.level === 'verbose' ? '-v' : '';
338
- if (loggingArg !== '') {
339
- args.push(loggingArg);
340
- }
341
- const timer = new Timer();
342
-
343
- const cmd = checkCircuitOnly ? 'avm_check_circuit' : 'avm_prove';
344
- const logFunction = (message: string) => {
345
- logger.verbose(`AvmCircuit (${cmd}) BB out - ${message}`);
346
- };
347
- const result = await executeBB(pathToBB, cmd, args, logFunction);
348
- const duration = timer.ms();
349
-
350
- if (result.status == BB_RESULT.SUCCESS) {
351
- return {
352
- status: BB_RESULT.SUCCESS,
353
- durationMs: duration,
354
- proofPath: join(outputPath, PROOF_FILENAME),
355
- pkPath: undefined,
356
- vkDirectoryPath: outputPath,
357
- };
358
- }
359
- // Not a great error message here but it is difficult to decipher what comes from bb
360
- return {
361
- status: BB_RESULT.FAILURE,
362
- reason: `Failed to generate proof. AVM proof for TX hash ${input.hints.tx.hash}. Exit code ${result.exitCode}. Signal ${result.signal}.`,
363
- retry: result.signal === 'SIGKILL', // retry on SIGKILL because the oomkiller might have stopped the process
364
- };
365
- } catch (error) {
366
- return { status: BB_RESULT.FAILURE, reason: `${error}` };
367
- }
368
- }
369
-
370
- /**
371
- * Used for verifying proofs of noir circuits
372
- * @param pathToBB - The full path to the bb binary
373
- * @param proofFullPath - The full path to the proof to be verified
374
- * @param verificationKeyPath - The full path to the circuit verification key
375
- * @param log - A logging function
376
- * @returns An object containing a result indication and duration taken
377
- */
378
- export async function verifyProof(
379
- pathToBB: string,
380
- proofFullPath: string,
381
- verificationKeyPath: string,
382
- ultraHonkFlavor: UltraHonkFlavor,
383
- log: Logger,
384
- ): Promise<BBFailure | BBSuccess> {
385
- return await verifyProofInternal(
386
- pathToBB,
387
- proofFullPath,
388
- verificationKeyPath,
389
- `verify`,
390
- log,
391
- getArgs(ultraHonkFlavor),
392
- );
393
- }
394
-
395
- export async function verifyAvmProof(
396
- pathToBB: string,
397
- workingDirectory: string,
398
- proofFullPath: string,
399
- publicInputs: AvmCircuitPublicInputs,
400
- verificationKeyPath: string,
401
- logger: Logger,
402
- ): Promise<BBFailure | BBSuccess> {
403
- const inputsBuffer = publicInputs.serializeWithMessagePack();
404
-
405
- // Write the inputs to the working directory.
406
- const filePresent = async (file: string) =>
407
- await fs
408
- .access(file, fs.constants.R_OK)
409
- .then(_ => true)
410
- .catch(_ => false);
411
- const avmInputsPath = join(workingDirectory, 'avm_public_inputs.bin');
412
- await fs.writeFile(avmInputsPath, inputsBuffer);
413
- if (!(await filePresent(avmInputsPath))) {
414
- return { status: BB_RESULT.FAILURE, reason: `Could not write avm inputs to ${avmInputsPath}` };
415
- }
416
-
417
- return await verifyProofInternal(pathToBB, proofFullPath, verificationKeyPath, 'avm_verify', logger, [
418
- '--avm-public-inputs',
419
- avmInputsPath,
420
- ]);
421
- }
422
-
423
- /**
424
- * Verifies a ChonkProof
425
- * TODO(#7370) The verification keys should be supplied separately
426
- * @param pathToBB - The full path to the bb binary
427
- * @param targetPath - The path to the folder with the proof, accumulator, and verification keys
428
- * @param log - A logging function
429
- * @param concurrency - The number of threads to use for the verification
430
- * @returns An object containing a result indication and duration taken
431
- */
432
- export async function verifyChonkProof(
433
- pathToBB: string,
434
- proofPath: string,
435
- keyPath: string,
436
- log: LogFn,
437
- concurrency = 1,
438
- ): Promise<BBFailure | BBSuccess> {
439
- const binaryPresent = await fs
440
- .access(pathToBB, fs.constants.R_OK)
441
- .then(_ => true)
442
- .catch(_ => false);
443
- if (!binaryPresent) {
444
- return { status: BB_RESULT.FAILURE, reason: `Failed to find bb binary at ${pathToBB}` };
445
- }
446
-
447
- try {
448
- const args = ['--scheme', 'chonk', '-p', proofPath, '-k', keyPath, '-v'];
449
- const timer = new Timer();
450
- const command = 'verify';
451
-
452
- const result = await executeBB(pathToBB, command, args, log, concurrency);
453
- const duration = timer.ms();
454
- if (result.status == BB_RESULT.SUCCESS) {
455
- return { status: BB_RESULT.SUCCESS, durationMs: duration };
456
- }
457
- // Not a great error message here but it is difficult to decipher what comes from bb
458
- return {
459
- status: BB_RESULT.FAILURE,
460
- reason: `Failed to verify proof. Exit code ${result.exitCode}. Signal ${result.signal}.`,
461
- retry: !!result.signal,
462
- };
463
- } catch (error) {
464
- return { status: BB_RESULT.FAILURE, reason: `${error}` };
465
- }
466
- }
467
-
468
- /**
469
- * Used for verifying proofs with BB
470
- * @param pathToBB - The full path to the bb binary
471
- * @param proofFullPath - The full path to the proof to be verified
472
- * @param verificationKeyPath - The full path to the circuit verification key
473
- * @param command - The BB command to execute (verify/avm_verify)
474
- * @param log - A logging function
475
- * @returns An object containing a result indication and duration taken
476
- */
477
- async function verifyProofInternal(
478
- pathToBB: string,
479
- proofFullPath: string,
480
- verificationKeyPath: string,
481
- command: 'verify' | 'avm_verify',
482
- logger: Logger,
483
- extraArgs: string[] = [],
484
- ): Promise<BBFailure | BBSuccess> {
485
- const binaryPresent = await fs
486
- .access(pathToBB, fs.constants.R_OK)
487
- .then(_ => true)
488
- .catch(_ => false);
489
- if (!binaryPresent) {
490
- return { status: BB_RESULT.FAILURE, reason: `Failed to find bb binary at ${pathToBB}` };
491
- }
492
-
493
- const logFunction = (message: string) => {
494
- logger.verbose(`bb-prover (verify) BB out - ${message}`);
495
- };
496
-
497
- try {
498
- let args;
499
-
500
- if (command == 'verify') {
501
- // Specify the public inputs path in the case of UH verification.
502
- // Take proofFullPath and remove the suffix past the / to get the directory.
503
- const proofDir = proofFullPath.substring(0, proofFullPath.lastIndexOf('/'));
504
- const publicInputsFullPath = join(proofDir, '/public_inputs');
505
- logger.debug(`public inputs path: ${publicInputsFullPath}`);
506
-
507
- args = ['-p', proofFullPath, '-k', verificationKeyPath, '-i', publicInputsFullPath, '--disable_zk', ...extraArgs];
508
- } else {
509
- args = ['-p', proofFullPath, '-k', verificationKeyPath, ...extraArgs];
510
- }
511
-
512
- const loggingArg =
513
- logger.level === 'debug' || logger.level === 'trace' ? '-d' : logger.level === 'verbose' ? '-v' : '';
514
- if (loggingArg !== '') {
515
- args.push(loggingArg);
516
- }
517
-
518
- const timer = new Timer();
519
- const result = await executeBB(pathToBB, command, args, logFunction);
520
- const duration = timer.ms();
521
- if (result.status == BB_RESULT.SUCCESS) {
522
- return { status: BB_RESULT.SUCCESS, durationMs: duration };
523
- }
524
- // Not a great error message here but it is difficult to decipher what comes from bb
525
- return {
526
- status: BB_RESULT.FAILURE,
527
- reason: `Failed to verify proof. Exit code ${result.exitCode}. Signal ${result.signal}.`,
528
- retry: !!result.signal,
529
- };
530
- } catch (error) {
531
- return { status: BB_RESULT.FAILURE, reason: `${error}` };
532
- }
533
- }
534
-
535
- export async function generateContractForVerificationKey(
536
- pathToBB: string,
537
- vkFilePath: string,
538
- contractPath: string,
539
- log: LogFn,
540
- ): Promise<BBFailure | BBSuccess> {
541
- const binaryPresent = await fs
542
- .access(pathToBB, fs.constants.R_OK)
543
- .then(_ => true)
544
- .catch(_ => false);
545
-
546
- if (!binaryPresent) {
547
- return { status: BB_RESULT.FAILURE, reason: `Failed to find bb binary at ${pathToBB}` };
548
- }
549
-
550
- const outputDir = dirname(contractPath);
551
- const contractName = basename(contractPath);
552
- // cache contract generation based on vk file and contract name
553
- const cacheKey = sha256(Buffer.concat([Buffer.from(contractName), await fs.readFile(vkFilePath)]));
554
-
555
- await fs.mkdir(outputDir, { recursive: true });
556
-
557
- const res = await fsCache<BBSuccess | BBFailure>(outputDir, cacheKey, log, false, async () => {
558
- try {
559
- const args = ['--scheme', 'ultra_honk', '-k', vkFilePath, '-o', contractPath, '-v'];
560
- const timer = new Timer();
561
- const result = await executeBB(pathToBB, 'contract', args, log);
562
- const duration = timer.ms();
563
- if (result.status == BB_RESULT.SUCCESS) {
564
- return { status: BB_RESULT.SUCCESS, durationMs: duration, contractPath };
565
- }
566
- // Not a great error message here but it is difficult to decipher what comes from bb
567
- return {
568
- status: BB_RESULT.FAILURE,
569
- reason: `Failed to write verifier contract. Exit code ${result.exitCode}. Signal ${result.signal}.`,
570
- retry: !!result.signal,
571
- };
572
- } catch (error) {
573
- return { status: BB_RESULT.FAILURE, reason: `${error}` };
574
- }
575
- });
576
-
577
- if (!res) {
578
- return {
579
- status: BB_RESULT.ALREADY_PRESENT,
580
- durationMs: 0,
581
- contractPath,
582
- };
583
- }
584
-
585
- return res;
586
- }
587
-
588
- /**
589
- * Compute bb gate count for a given circuit
590
- * @param pathToBB - The full path to the bb binary
591
- * @param workingDirectory - A temporary directory for writing the bytecode
592
- * @param circuitName - The name of the circuit
593
- * @param bytecode - The bytecode of the circuit
594
- * @param flavor - The flavor of the backend - mega_honk or ultra_honk variants
595
- * @returns An object containing the status, gate count, and time taken
596
- */
597
- export async function computeGateCountForCircuit(
598
- pathToBB: string,
599
- workingDirectory: string,
600
- circuitName: string,
601
- bytecode: Buffer,
602
- flavor: UltraHonkFlavor | 'mega_honk',
603
- log: LogFn,
604
- ): Promise<BBFailure | BBSuccess> {
605
- // Check that the working directory exists
606
- try {
607
- await fs.access(workingDirectory);
608
- } catch {
609
- return { status: BB_RESULT.FAILURE, reason: `Working directory ${workingDirectory} does not exist` };
610
- }
611
-
612
- // The bytecode is written to e.g. /workingDirectory/ParityBaseArtifact-bytecode
613
- const bytecodePath = `${workingDirectory}/${circuitName}-bytecode`;
614
-
615
- const binaryPresent = await fs
616
- .access(pathToBB, fs.constants.R_OK)
617
- .then(_ => true)
618
- .catch(_ => false);
619
- if (!binaryPresent) {
620
- return { status: BB_RESULT.FAILURE, reason: `Failed to find bb binary at ${pathToBB}` };
621
- }
622
-
623
- // Accumulate the stdout from bb
624
- let stdout = '';
625
- const logHandler = (message: string) => {
626
- stdout += message;
627
- log(message);
628
- };
629
-
630
- try {
631
- // Write the bytecode to the working directory
632
- await fs.writeFile(bytecodePath, bytecode);
633
- const timer = new Timer();
634
-
635
- const result = await executeBB(
636
- pathToBB,
637
- 'gates',
638
- ['--scheme', flavor === 'mega_honk' ? 'chonk' : 'ultra_honk', '-b', bytecodePath, '-v'],
639
- logHandler,
640
- );
641
- const duration = timer.ms();
642
-
643
- if (result.status == BB_RESULT.SUCCESS) {
644
- // Look for "circuit_size" in the stdout and parse the number
645
- const circuitSizeMatch = stdout.match(/circuit_size": (\d+)/);
646
- if (!circuitSizeMatch) {
647
- return { status: BB_RESULT.FAILURE, reason: 'Failed to parse circuit_size from bb gates stdout.' };
648
- }
649
- const circuitSize = parseInt(circuitSizeMatch[1]);
650
-
651
- return {
652
- status: BB_RESULT.SUCCESS,
653
- durationMs: duration,
654
- circuitSize: circuitSize,
655
- };
656
- }
657
-
658
- return { status: BB_RESULT.FAILURE, reason: 'Failed getting the gate count.' };
659
- } catch (error) {
660
- return { status: BB_RESULT.FAILURE, reason: `${error}` };
661
- }
662
- }
663
-
664
- const CACHE_FILENAME = '.cache';
665
- async function fsCache<T>(
666
- dir: string,
667
- expectedCacheKey: Buffer,
668
- logger: LogFn,
669
- force: boolean,
670
- action: () => Promise<T>,
671
- ): Promise<T | undefined> {
672
- const cacheFilePath = join(dir, CACHE_FILENAME);
673
-
674
- let run: boolean;
675
- if (force) {
676
- run = true;
677
- } else {
678
- try {
679
- run = !expectedCacheKey.equals(await fs.readFile(cacheFilePath));
680
- } catch (err: any) {
681
- if (err && 'code' in err && err.code === 'ENOENT') {
682
- // cache file doesn't exist, swallow error and run
683
- run = true;
684
- } else {
685
- throw err;
686
- }
687
- }
688
- }
689
-
690
- let res: T | undefined;
691
- if (run) {
692
- logger(`Cache miss or forced run. Running operation in ${dir}...`);
693
- res = await action();
694
- } else {
695
- logger(`Cache hit. Skipping operation in ${dir}...`);
696
- }
697
-
698
- try {
699
- await fs.writeFile(cacheFilePath, expectedCacheKey);
700
- } catch {
701
- logger(`Couldn't write cache data to ${cacheFilePath}. Skipping cache...`);
702
- // ignore
703
- }
704
-
705
- return res;
706
- }