@aztec/bb-prover 0.0.1-commit.e558bd1c → 0.0.1-commit.e57c76e

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 (62) 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 -110
  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 +12 -4
  21. package/dest/prover/client/bb_private_kernel_prover.d.ts +10 -2
  22. package/dest/prover/client/bb_private_kernel_prover.d.ts.map +1 -1
  23. package/dest/prover/client/bb_private_kernel_prover.js +39 -5
  24. package/dest/prover/proof_utils.d.ts +11 -1
  25. package/dest/prover/proof_utils.d.ts.map +1 -1
  26. package/dest/prover/proof_utils.js +24 -1
  27. package/dest/prover/server/bb_prover.d.ts +4 -5
  28. package/dest/prover/server/bb_prover.d.ts.map +1 -1
  29. package/dest/prover/server/bb_prover.js +207 -78
  30. package/dest/verification_key/verification_key_data.js +1 -1
  31. package/dest/verifier/batch_chonk_verifier.d.ts +56 -0
  32. package/dest/verifier/batch_chonk_verifier.d.ts.map +1 -0
  33. package/dest/verifier/batch_chonk_verifier.js +384 -0
  34. package/dest/verifier/bb_verifier.d.ts +4 -1
  35. package/dest/verifier/bb_verifier.d.ts.map +1 -1
  36. package/dest/verifier/bb_verifier.js +134 -45
  37. package/dest/verifier/index.d.ts +2 -1
  38. package/dest/verifier/index.d.ts.map +1 -1
  39. package/dest/verifier/index.js +1 -0
  40. package/dest/verifier/queued_chonk_verifier.d.ts +2 -3
  41. package/dest/verifier/queued_chonk_verifier.d.ts.map +1 -1
  42. package/dest/verifier/queued_chonk_verifier.js +6 -5
  43. package/package.json +19 -17
  44. package/src/avm_proving_tests/avm_proving_tester.ts +53 -126
  45. package/src/bb/bb_js_backend.ts +435 -0
  46. package/src/bb/bb_js_debug.ts +227 -0
  47. package/src/bb/file_names.ts +6 -0
  48. package/src/config.ts +16 -0
  49. package/src/index.ts +2 -1
  50. package/src/instrumentation.ts +12 -4
  51. package/src/prover/client/bb_private_kernel_prover.ts +116 -4
  52. package/src/prover/proof_utils.ts +41 -1
  53. package/src/prover/server/bb_prover.ts +132 -137
  54. package/src/verification_key/verification_key_data.ts +1 -1
  55. package/src/verifier/batch_chonk_verifier.ts +415 -0
  56. package/src/verifier/bb_verifier.ts +66 -76
  57. package/src/verifier/index.ts +1 -0
  58. package/src/verifier/queued_chonk_verifier.ts +6 -7
  59. package/dest/bb/execute.d.ts +0 -107
  60. package/dest/bb/execute.d.ts.map +0 -1
  61. package/dest/bb/execute.js +0 -647
  62. package/src/bb/execute.ts +0 -678
package/src/bb/execute.ts DELETED
@@ -1,678 +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: undefined, // AVM VK is fixed in the binary.
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 logger - A logger
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
- logger: Logger,
384
- ): Promise<BBFailure | BBSuccess> {
385
- // Specify the public inputs path in the case of UH verification.
386
- // Take proofFullPath and remove the suffix past the / to get the directory.
387
- const proofDir = proofFullPath.substring(0, proofFullPath.lastIndexOf('/'));
388
- const publicInputsFullPath = join(proofDir, '/public_inputs');
389
- logger.debug(`public inputs path: ${publicInputsFullPath}`);
390
-
391
- const args = [
392
- '-p',
393
- proofFullPath,
394
- '-k',
395
- verificationKeyPath,
396
- '-i',
397
- publicInputsFullPath,
398
- '--disable_zk',
399
- ...getArgs(ultraHonkFlavor),
400
- ];
401
- return await verifyProofInternal(pathToBB, `verify`, args, logger);
402
- }
403
-
404
- export async function verifyAvmProof(
405
- pathToBB: string,
406
- workingDirectory: string,
407
- proofFullPath: string,
408
- publicInputs: AvmCircuitPublicInputs,
409
- logger: Logger,
410
- ): Promise<BBFailure | BBSuccess> {
411
- const inputsBuffer = publicInputs.serializeWithMessagePack();
412
-
413
- // Write the inputs to the working directory.
414
- const filePresent = async (file: string) =>
415
- await fs
416
- .access(file, fs.constants.R_OK)
417
- .then(_ => true)
418
- .catch(_ => false);
419
- const avmInputsPath = join(workingDirectory, 'avm_public_inputs.bin');
420
- await fs.writeFile(avmInputsPath, inputsBuffer);
421
- if (!(await filePresent(avmInputsPath))) {
422
- return { status: BB_RESULT.FAILURE, reason: `Could not write avm inputs to ${avmInputsPath}` };
423
- }
424
-
425
- const args = ['-p', proofFullPath, '--avm-public-inputs', avmInputsPath];
426
- return await verifyProofInternal(pathToBB, 'avm_verify', args, logger);
427
- }
428
-
429
- /**
430
- * Verifies a ChonkProof
431
- * TODO(#7370) The verification keys should be supplied separately
432
- * @param pathToBB - The full path to the bb binary
433
- * @param targetPath - The path to the folder with the proof, accumulator, and verification keys
434
- * @param logger - A logger
435
- * @param concurrency - The number of threads to use for the verification
436
- * @returns An object containing a result indication and duration taken
437
- */
438
- export async function verifyChonkProof(
439
- pathToBB: string,
440
- proofPath: string,
441
- keyPath: string,
442
- logger: Logger,
443
- concurrency = 1,
444
- ): Promise<BBFailure | BBSuccess> {
445
- const binaryPresent = await fs
446
- .access(pathToBB, fs.constants.R_OK)
447
- .then(_ => true)
448
- .catch(_ => false);
449
- if (!binaryPresent) {
450
- return { status: BB_RESULT.FAILURE, reason: `Failed to find bb binary at ${pathToBB}` };
451
- }
452
-
453
- const args = ['--scheme', 'chonk', '-p', proofPath, '-k', keyPath, '-v'];
454
- return await verifyProofInternal(pathToBB, 'verify', args, logger, concurrency);
455
- }
456
-
457
- /**
458
- * Used for verifying proofs with BB
459
- * @param pathToBB - The full path to the bb binary
460
- * @param command - The BB command to execute (verify/avm_verify)
461
- * @param args - The arguments to pass to the command
462
- * @param logger - A logger
463
- * @param concurrency - The number of threads to use for the verification
464
- * @returns An object containing a result indication and duration taken
465
- */
466
- async function verifyProofInternal(
467
- pathToBB: string,
468
- command: 'verify' | 'avm_verify',
469
- args: string[],
470
- logger: Logger,
471
- concurrency?: number,
472
- ): Promise<BBFailure | BBSuccess> {
473
- const binaryPresent = await fs
474
- .access(pathToBB, fs.constants.R_OK)
475
- .then(_ => true)
476
- .catch(_ => false);
477
- if (!binaryPresent) {
478
- return { status: BB_RESULT.FAILURE, reason: `Failed to find bb binary at ${pathToBB}` };
479
- }
480
-
481
- const logFunction = (message: string) => {
482
- logger.verbose(`bb-prover (verify) BB out - ${message}`);
483
- };
484
-
485
- try {
486
- const loggingArg =
487
- logger.level === 'debug' || logger.level === 'trace' ? '-d' : logger.level === 'verbose' ? '-v' : '';
488
- const finalArgs = loggingArg !== '' ? [...args, loggingArg] : args;
489
-
490
- const timer = new Timer();
491
- const result = await executeBB(pathToBB, command, finalArgs, logFunction, concurrency);
492
- const duration = timer.ms();
493
- if (result.status == BB_RESULT.SUCCESS) {
494
- return { status: BB_RESULT.SUCCESS, durationMs: duration };
495
- }
496
- // Not a great error message here but it is difficult to decipher what comes from bb
497
- return {
498
- status: BB_RESULT.FAILURE,
499
- reason: `Failed to verify proof. Exit code ${result.exitCode}. Signal ${result.signal}.`,
500
- retry: !!result.signal,
501
- };
502
- } catch (error) {
503
- return { status: BB_RESULT.FAILURE, reason: `${error}` };
504
- }
505
- }
506
-
507
- export async function generateContractForVerificationKey(
508
- pathToBB: string,
509
- vkFilePath: string,
510
- contractPath: string,
511
- log: LogFn,
512
- ): Promise<BBFailure | BBSuccess> {
513
- const binaryPresent = await fs
514
- .access(pathToBB, fs.constants.R_OK)
515
- .then(_ => true)
516
- .catch(_ => false);
517
-
518
- if (!binaryPresent) {
519
- return { status: BB_RESULT.FAILURE, reason: `Failed to find bb binary at ${pathToBB}` };
520
- }
521
-
522
- const outputDir = dirname(contractPath);
523
- const contractName = basename(contractPath);
524
- // cache contract generation based on vk file and contract name
525
- const cacheKey = sha256(Buffer.concat([Buffer.from(contractName), await fs.readFile(vkFilePath)]));
526
-
527
- await fs.mkdir(outputDir, { recursive: true });
528
-
529
- const res = await fsCache<BBSuccess | BBFailure>(outputDir, cacheKey, log, false, async () => {
530
- try {
531
- const args = ['--scheme', 'ultra_honk', '-k', vkFilePath, '-o', contractPath, '-v'];
532
- const timer = new Timer();
533
- const result = await executeBB(pathToBB, 'contract', args, log);
534
- const duration = timer.ms();
535
- if (result.status == BB_RESULT.SUCCESS) {
536
- return { status: BB_RESULT.SUCCESS, durationMs: duration, contractPath };
537
- }
538
- // Not a great error message here but it is difficult to decipher what comes from bb
539
- return {
540
- status: BB_RESULT.FAILURE,
541
- reason: `Failed to write verifier contract. Exit code ${result.exitCode}. Signal ${result.signal}.`,
542
- retry: !!result.signal,
543
- };
544
- } catch (error) {
545
- return { status: BB_RESULT.FAILURE, reason: `${error}` };
546
- }
547
- });
548
-
549
- if (!res) {
550
- return {
551
- status: BB_RESULT.ALREADY_PRESENT,
552
- durationMs: 0,
553
- contractPath,
554
- };
555
- }
556
-
557
- return res;
558
- }
559
-
560
- /**
561
- * Compute bb gate count for a given circuit
562
- * @param pathToBB - The full path to the bb binary
563
- * @param workingDirectory - A temporary directory for writing the bytecode
564
- * @param circuitName - The name of the circuit
565
- * @param bytecode - The bytecode of the circuit
566
- * @param flavor - The flavor of the backend - mega_honk or ultra_honk variants
567
- * @returns An object containing the status, gate count, and time taken
568
- */
569
- export async function computeGateCountForCircuit(
570
- pathToBB: string,
571
- workingDirectory: string,
572
- circuitName: string,
573
- bytecode: Buffer,
574
- flavor: UltraHonkFlavor | 'mega_honk',
575
- log: LogFn,
576
- ): Promise<BBFailure | BBSuccess> {
577
- // Check that the working directory exists
578
- try {
579
- await fs.access(workingDirectory);
580
- } catch {
581
- return { status: BB_RESULT.FAILURE, reason: `Working directory ${workingDirectory} does not exist` };
582
- }
583
-
584
- // The bytecode is written to e.g. /workingDirectory/ParityBaseArtifact-bytecode
585
- const bytecodePath = `${workingDirectory}/${circuitName}-bytecode`;
586
-
587
- const binaryPresent = await fs
588
- .access(pathToBB, fs.constants.R_OK)
589
- .then(_ => true)
590
- .catch(_ => false);
591
- if (!binaryPresent) {
592
- return { status: BB_RESULT.FAILURE, reason: `Failed to find bb binary at ${pathToBB}` };
593
- }
594
-
595
- // Accumulate the stdout from bb
596
- let stdout = '';
597
- const logHandler = (message: string) => {
598
- stdout += message;
599
- log(message);
600
- };
601
-
602
- try {
603
- // Write the bytecode to the working directory
604
- await fs.writeFile(bytecodePath, bytecode);
605
- const timer = new Timer();
606
-
607
- const result = await executeBB(
608
- pathToBB,
609
- 'gates',
610
- ['--scheme', flavor === 'mega_honk' ? 'chonk' : 'ultra_honk', '-b', bytecodePath, '-v'],
611
- logHandler,
612
- );
613
- const duration = timer.ms();
614
-
615
- if (result.status == BB_RESULT.SUCCESS) {
616
- // Look for "circuit_size" in the stdout and parse the number
617
- const circuitSizeMatch = stdout.match(/circuit_size": (\d+)/);
618
- if (!circuitSizeMatch) {
619
- return { status: BB_RESULT.FAILURE, reason: 'Failed to parse circuit_size from bb gates stdout.' };
620
- }
621
- const circuitSize = parseInt(circuitSizeMatch[1]);
622
-
623
- return {
624
- status: BB_RESULT.SUCCESS,
625
- durationMs: duration,
626
- circuitSize: circuitSize,
627
- };
628
- }
629
-
630
- return { status: BB_RESULT.FAILURE, reason: 'Failed getting the gate count.' };
631
- } catch (error) {
632
- return { status: BB_RESULT.FAILURE, reason: `${error}` };
633
- }
634
- }
635
-
636
- const CACHE_FILENAME = '.cache';
637
- async function fsCache<T>(
638
- dir: string,
639
- expectedCacheKey: Buffer,
640
- logger: LogFn,
641
- force: boolean,
642
- action: () => Promise<T>,
643
- ): Promise<T | undefined> {
644
- const cacheFilePath = join(dir, CACHE_FILENAME);
645
-
646
- let run: boolean;
647
- if (force) {
648
- run = true;
649
- } else {
650
- try {
651
- run = !expectedCacheKey.equals(await fs.readFile(cacheFilePath));
652
- } catch (err: any) {
653
- if (err && 'code' in err && err.code === 'ENOENT') {
654
- // cache file doesn't exist, swallow error and run
655
- run = true;
656
- } else {
657
- throw err;
658
- }
659
- }
660
- }
661
-
662
- let res: T | undefined;
663
- if (run) {
664
- logger(`Cache miss or forced run. Running operation in ${dir}...`);
665
- res = await action();
666
- } else {
667
- logger(`Cache hit. Skipping operation in ${dir}...`);
668
- }
669
-
670
- try {
671
- await fs.writeFile(cacheFilePath, expectedCacheKey);
672
- } catch {
673
- logger(`Couldn't write cache data to ${cacheFilePath}. Skipping cache...`);
674
- // ignore
675
- }
676
-
677
- return res;
678
- }