@aztec/bb-prover 0.0.1-commit.24de95ac → 0.0.1-commit.2606882

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