@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
@@ -1,4 +1,4 @@
1
- import type { LogFn, LogLevel, Logger } from '@aztec/foundation/log';
1
+ import type { AvmStat } from '@aztec/bb.js';
2
2
  import { Timer } from '@aztec/foundation/timer';
3
3
  import {
4
4
  PublicTxSimulationTester,
@@ -8,88 +8,31 @@ import {
8
8
  type TestPrivateInsertions,
9
9
  } from '@aztec/simulator/public/fixtures';
10
10
  import type { PublicTxResult } from '@aztec/simulator/server';
11
- import { type AvmCircuitInputs, AvmCircuitPublicInputs } from '@aztec/stdlib/avm';
11
+ import { AvmCircuitInputs, AvmCircuitPublicInputs, PublicSimulatorConfig } from '@aztec/stdlib/avm';
12
12
  import { AztecAddress } from '@aztec/stdlib/aztec-address';
13
+ import type { Gas } from '@aztec/stdlib/gas';
13
14
  import type { MerkleTreeWriteOperations } from '@aztec/stdlib/interfaces/server';
14
15
  import type { GlobalVariables } from '@aztec/stdlib/tx';
15
16
  import { NativeWorldStateService } from '@aztec/world-state';
16
17
 
17
- import fs from 'node:fs/promises';
18
- import { tmpdir } from 'node:os';
19
18
  import path from 'path';
20
19
 
21
- import {
22
- type BBResult,
23
- type BBSuccess,
24
- BB_RESULT,
25
- VK_FILENAME,
26
- generateAvmProof,
27
- verifyAvmProof,
28
- } from '../bb/execute.js';
20
+ import { BBJsFactory } from '../bb/bb_js_backend.js';
29
21
 
30
22
  const BB_PATH = path.resolve('../../barretenberg/cpp/build/bin/bb-avm');
31
23
 
32
- // An InterceptingLogger that records all log messages and forwards them to a wrapped logger.
33
- class InterceptingLogger implements Logger {
34
- public readonly logs: string[] = [];
35
- public level: LogLevel;
36
- public module: string;
37
-
38
- private logger: Logger;
39
-
40
- constructor(logger: Logger) {
41
- this.logger = logger;
42
- this.level = logger.level;
43
- this.module = logger.module;
44
- }
45
-
46
- isLevelEnabled(level: LogLevel): boolean {
47
- return this.logger.isLevelEnabled(level);
48
- }
49
-
50
- createChild(_childModule: string): Logger {
51
- throw new Error('Not implemented');
52
- }
53
-
54
- private intercept(level: LogLevel, msg: string, ...args: any[]) {
55
- this.logs.push(...msg.split('\n'));
56
- // Forward to the wrapped logger
57
- (this.logger[level] as LogFn)(msg, ...args);
58
- }
59
-
60
- // Log methods for each level
61
- silent(msg: string, ...args: any[]) {
62
- this.intercept('silent', msg, ...args);
63
- }
64
- fatal(msg: string, ...args: any[]) {
65
- this.intercept('fatal', msg, ...args);
66
- }
67
- warn(msg: string, ...args: any[]) {
68
- this.intercept('warn', msg, ...args);
69
- }
70
- info(msg: string, ...args: any[]) {
71
- this.intercept('info', msg, ...args);
72
- }
73
- verbose(msg: string, ...args: any[]) {
74
- this.intercept('verbose', msg, ...args);
75
- }
76
- debug(msg: string, ...args: any[]) {
77
- this.intercept('debug', msg, ...args);
78
- }
79
- trace(msg: string, ...args: any[]) {
80
- this.intercept('trace', msg, ...args);
81
- }
82
-
83
- // Error log function can be string or Error
84
- error(err: Error | string, ...args: any[]) {
85
- const msg = typeof err === 'string' ? err : err.message;
86
- this.logs.push(msg);
87
- this.logger.error(msg, err, ...args);
88
- }
89
- }
24
+ // Config with collectHints enabled for proving tests
25
+ const provingConfig: PublicSimulatorConfig = PublicSimulatorConfig.from({
26
+ skipFeeEnforcement: false,
27
+ collectCallMetadata: true, // For results.
28
+ collectDebugLogs: false,
29
+ collectHints: true, // Required for proving!
30
+ collectPublicInputs: true, // Required for proving!
31
+ collectStatistics: false,
32
+ });
90
33
 
91
34
  export class AvmProvingTester extends PublicTxSimulationTester {
92
- private bbWorkingDirectory: string = '';
35
+ private readonly bbJsFactory = new BBJsFactory(BB_PATH);
93
36
 
94
37
  constructor(
95
38
  private checkCircuitOnly: boolean,
@@ -98,7 +41,8 @@ export class AvmProvingTester extends PublicTxSimulationTester {
98
41
  globals?: GlobalVariables,
99
42
  metrics?: TestExecutorMetrics,
100
43
  ) {
101
- super(merkleTrees, contractDataSource, globals, metrics);
44
+ // simulator factory is undefined because for proving, we use the default C++ simulator
45
+ super(merkleTrees, contractDataSource, globals, metrics, /*simulatorFactory=*/ undefined, provingConfig);
102
46
  }
103
47
 
104
48
  static async new(
@@ -112,53 +56,57 @@ export class AvmProvingTester extends PublicTxSimulationTester {
112
56
  return new AvmProvingTester(checkCircuitOnly, contractDataSource, merkleTrees, globals, metrics);
113
57
  }
114
58
 
115
- async prove(avmCircuitInputs: AvmCircuitInputs, txLabel: string = 'unlabeledTx'): Promise<BBResult> {
116
- // We use a new working directory for each proof.
117
- this.bbWorkingDirectory = await fs.mkdtemp(path.join(tmpdir(), 'bb-'));
59
+ /**
60
+ * Generate an AVM proof (or run check-circuit if configured). Records per-stage timings in the test metrics.
61
+ * Returns the in-memory proof fields on success; throws via jest expect() on failure.
62
+ */
63
+ async prove(avmCircuitInputs: AvmCircuitInputs, txLabel: string = 'unlabeledTx'): Promise<Uint8Array[]> {
64
+ const inputsBuffer = avmCircuitInputs.serializeWithMessagePack();
65
+
66
+ if (this.checkCircuitOnly) {
67
+ await using instance = await this.bbJsFactory.getInstance();
68
+ const { passed, stats } = await instance.checkAvmCircuit(inputsBuffer);
69
+ this.recordProverMetrics(stats, txLabel);
70
+ expect(passed).toBe(true);
71
+ return [];
72
+ }
118
73
 
119
- const interceptingLogger = new InterceptingLogger(this.logger);
74
+ await using instance = await this.bbJsFactory.getInstance();
75
+ const { proof, stats } = await instance.generateAvmProof(inputsBuffer);
76
+ this.recordProverMetrics(stats, txLabel);
77
+ return proof;
78
+ }
120
79
 
121
- // Then we prove.
122
- const proofRes = await generateAvmProof(
123
- BB_PATH,
124
- this.bbWorkingDirectory,
125
- avmCircuitInputs,
126
- interceptingLogger,
127
- this.checkCircuitOnly,
128
- );
129
- if (proofRes.status === BB_RESULT.FAILURE) {
130
- this.logger.error(`Proof generation failed: ${proofRes.reason}`);
80
+ async verify(proof: Uint8Array[], publicInputs: AvmCircuitPublicInputs): Promise<void> {
81
+ if (this.checkCircuitOnly) {
82
+ // Check-circuit did not generate a proof; nothing to verify.
83
+ return;
131
84
  }
132
- expect(proofRes.status).toEqual(BB_RESULT.SUCCESS);
85
+ const piBuffer = publicInputs.serializeWithMessagePack();
86
+ await using instance = await this.bbJsFactory.getInstance();
87
+ const { verified } = await instance.verifyAvmProof(proof, piBuffer);
88
+ expect(verified).toBe(true);
89
+ }
133
90
 
134
- // Parse the logs into a structured format.
135
- const logs = interceptingLogger.logs;
136
- // const traceSizes: { name: string; size: number }[] = [];
137
- // logs.forEach(log => {
138
- // const match = log.match(/\b(\w+): (\d+) \(~2/);
139
- // if (match) {
140
- // traceSizes.push({
141
- // name: match[1],
142
- // size: parseInt(match[2]),
143
- // });
144
- // }
145
- // });
146
- const times: { [key: string]: number } = {};
147
- logs.forEach(log => {
148
- const match = log.match(/\b([\w/]+)_ms: (\d+)/);
149
- if (match) {
150
- times[match[1]] = parseInt(match[2]);
151
- }
152
- });
91
+ public async proveVerify(avmCircuitInputs: AvmCircuitInputs, txLabel: string = 'unlabeledTx') {
92
+ const proof = await this.prove(avmCircuitInputs, txLabel);
93
+ await this.verify(proof, avmCircuitInputs.publicInputs);
94
+ }
153
95
 
154
- // Throw if logs did not contain any times.
96
+ private recordProverMetrics(stats: AvmStat[], txLabel: string) {
97
+ // Build a lookup keyed on the stage name with the `_ms` suffix stripped, matching the legacy
98
+ // stdout-scraped shape. bb::avm2::Stats::time() stores keys with `_ms` appended.
99
+ const times: { [key: string]: number } = {};
100
+ for (const { name, valueMs } of stats) {
101
+ const key = name.endsWith('_ms') ? name.slice(0, -'_ms'.length) : name;
102
+ times[key] = valueMs;
103
+ }
155
104
  if (Object.keys(times).length === 0) {
156
- throw new Error('AVM stdout did not contain any proving times in the stats!');
105
+ throw new Error('AVM response did not contain any proving-stage timings!');
157
106
  }
158
-
159
107
  // Hack to make labels match.
160
108
  const txLabelWithCount = `${txLabel}/${this.txCount - 1}`;
161
- // I need to cast because TS doesnt realize metrics is protected not private.
109
+ // Cast because TS doesn't realize `metrics` is protected, not private on the parent class.
162
110
  (this as any).metrics?.recordProverMetrics(txLabelWithCount, {
163
111
  proverSimulationStepMs: times['simulation/all'],
164
112
  proverProvingStepMs: times['proving/all'],
@@ -171,33 +119,6 @@ export class AvmProvingTester extends PublicTxSimulationTester {
171
119
  provingLogDerivativeInverseCommitmentsMs: times['prove/log_derivative_inverse_commitments_round'],
172
120
  provingWireCommitmentsMs: times['prove/wire_commitments_round'],
173
121
  });
174
-
175
- return proofRes as BBSuccess;
176
- }
177
-
178
- async verify(proofRes: BBSuccess, publicInputs: AvmCircuitPublicInputs): Promise<BBResult> {
179
- if (this.checkCircuitOnly) {
180
- // Skip verification if we are only checking the circuit.
181
- // Check-circuit does not generate a proof to verify.
182
- return proofRes;
183
- }
184
-
185
- return await verifyAvmProof(
186
- BB_PATH,
187
- this.bbWorkingDirectory,
188
- proofRes.proofPath!,
189
- publicInputs,
190
- path.join(proofRes.vkDirectoryPath!, VK_FILENAME),
191
- this.logger,
192
- );
193
- }
194
-
195
- public async proveVerify(avmCircuitInputs: AvmCircuitInputs, txLabel: string = 'unlabeledTx') {
196
- const provingRes = await this.prove(avmCircuitInputs, txLabel);
197
- expect(provingRes.status).toEqual(BB_RESULT.SUCCESS);
198
-
199
- const verificationRes = await this.verify(provingRes as BBSuccess, avmCircuitInputs.publicInputs);
200
- expect(verificationRes.status).toBe(BB_RESULT.SUCCESS);
201
122
  }
202
123
 
203
124
  public async simProveVerify(
@@ -210,7 +131,9 @@ export class AvmProvingTester extends PublicTxSimulationTester {
210
131
  privateInsertions?: TestPrivateInsertions,
211
132
  txLabel: string = 'unlabeledTx',
212
133
  disableRevertCheck: boolean = false,
134
+ gasLimits?: Gas,
213
135
  ): Promise<PublicTxResult> {
136
+ const simTimer = new Timer();
214
137
  const simRes = await this.simulateTx(
215
138
  sender,
216
139
  setupCalls,
@@ -219,7 +142,10 @@ export class AvmProvingTester extends PublicTxSimulationTester {
219
142
  feePayer,
220
143
  privateInsertions,
221
144
  txLabel,
145
+ gasLimits,
222
146
  );
147
+ const simDuration = simTimer.ms();
148
+ this.logger.info(`Simulation took ${simDuration} ms for tx ${txLabel}`);
223
149
 
224
150
  if (!disableRevertCheck) {
225
151
  expect(simRes.revertCode.isOK()).toBe(expectRevert ? false : true);
@@ -227,7 +153,7 @@ export class AvmProvingTester extends PublicTxSimulationTester {
227
153
 
228
154
  const opString = this.checkCircuitOnly ? 'Check circuit' : 'Proving and verification';
229
155
 
230
- const avmCircuitInputs = simRes.avmProvingRequest.inputs;
156
+ const avmCircuitInputs = new AvmCircuitInputs(simRes.hints!, simRes.publicInputs!);
231
157
  const timer = new Timer();
232
158
  await this.proveVerify(avmCircuitInputs, txLabel);
233
159
  this.logger.info(`${opString} took ${timer.ms()} ms for tx ${txLabel}`);
@@ -243,6 +169,7 @@ export class AvmProvingTester extends PublicTxSimulationTester {
243
169
  teardownCall?: TestEnqueuedCall,
244
170
  feePayer?: AztecAddress,
245
171
  privateInsertions?: TestPrivateInsertions,
172
+ gasLimits?: Gas,
246
173
  ) {
247
174
  return await this.simProveVerify(
248
175
  sender,
@@ -254,6 +181,7 @@ export class AvmProvingTester extends PublicTxSimulationTester {
254
181
  privateInsertions,
255
182
  txLabel,
256
183
  true,
184
+ gasLimits,
257
185
  );
258
186
  }
259
187
 
@@ -261,6 +189,7 @@ export class AvmProvingTester extends PublicTxSimulationTester {
261
189
  appCall: TestEnqueuedCall,
262
190
  expectRevert?: boolean,
263
191
  txLabel: string = 'unlabeledTx',
192
+ gasLimits?: Gas,
264
193
  ) {
265
194
  await this.simProveVerify(
266
195
  /*sender=*/ AztecAddress.fromNumber(42),
@@ -271,6 +200,8 @@ export class AvmProvingTester extends PublicTxSimulationTester {
271
200
  /*feePayer=*/ undefined,
272
201
  /*privateInsertions=*/ undefined,
273
202
  txLabel,
203
+ /*disableRevertCheck=*/ false,
204
+ gasLimits,
274
205
  );
275
206
  }
276
207
  }