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