@aztec/bb-prover 0.0.1-commit.b2a5d0dd1 → 0.0.1-commit.b3d3157a

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 (50) hide show
  1. package/dest/avm_proving_tests/avm_proving_tester.d.ts +9 -5
  2. package/dest/avm_proving_tests/avm_proving_tester.d.ts.map +1 -1
  3. package/dest/avm_proving_tests/avm_proving_tester.js +146 -104
  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 +7 -2
  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/prover/client/bb_private_kernel_prover.d.ts +10 -2
  19. package/dest/prover/client/bb_private_kernel_prover.d.ts.map +1 -1
  20. package/dest/prover/client/bb_private_kernel_prover.js +25 -1
  21. package/dest/prover/proof_utils.d.ts +11 -1
  22. package/dest/prover/proof_utils.d.ts.map +1 -1
  23. package/dest/prover/proof_utils.js +24 -1
  24. package/dest/prover/server/bb_prover.d.ts +4 -5
  25. package/dest/prover/server/bb_prover.d.ts.map +1 -1
  26. package/dest/prover/server/bb_prover.js +207 -78
  27. package/dest/verification_key/verification_key_data.js +1 -1
  28. package/dest/verifier/batch_chonk_verifier.d.ts +13 -2
  29. package/dest/verifier/batch_chonk_verifier.d.ts.map +1 -1
  30. package/dest/verifier/batch_chonk_verifier.js +210 -58
  31. package/dest/verifier/bb_verifier.d.ts +4 -1
  32. package/dest/verifier/bb_verifier.d.ts.map +1 -1
  33. package/dest/verifier/bb_verifier.js +134 -45
  34. package/package.json +18 -17
  35. package/src/avm_proving_tests/avm_proving_tester.ts +45 -126
  36. package/src/bb/bb_js_backend.ts +435 -0
  37. package/src/bb/bb_js_debug.ts +227 -0
  38. package/src/bb/file_names.ts +6 -0
  39. package/src/config.ts +6 -1
  40. package/src/index.ts +2 -1
  41. package/src/prover/client/bb_private_kernel_prover.ts +100 -0
  42. package/src/prover/proof_utils.ts +41 -1
  43. package/src/prover/server/bb_prover.ts +132 -137
  44. package/src/verification_key/verification_key_data.ts +1 -1
  45. package/src/verifier/batch_chonk_verifier.ts +204 -65
  46. package/src/verifier/bb_verifier.ts +66 -76
  47. package/dest/bb/execute.d.ts +0 -108
  48. package/dest/bb/execute.d.ts.map +0 -1
  49. package/dest/bb/execute.js +0 -652
  50. package/src/bb/execute.ts +0 -687
@@ -6,19 +6,21 @@ import { Timer } from '@aztec/foundation/timer';
6
6
  import { ProtocolCircuitVks } from '@aztec/noir-protocol-circuits-types/server/vks';
7
7
  import { Unpackr } from 'msgpackr';
8
8
  import { execFile } from 'node:child_process';
9
- import { unlinkSync } from 'node:fs';
10
- import { unlink } from 'node:fs/promises';
9
+ import { rmSync } from 'node:fs';
10
+ import { mkdtemp, rm } from 'node:fs/promises';
11
11
  import * as os from 'node:os';
12
12
  import * as path from 'node:path';
13
13
  import { promisify } from 'node:util';
14
14
  const execFileAsync = promisify(execFile);
15
+ const RESULT_TIMEOUT_MS = 5 * 60 * 1000;
16
+ const STOP_DRAIN_TIMEOUT_MS = 5_000;
15
17
  /** Maps client protocol artifacts used for chonk verification to VK indices. */ const CHONK_VK_ARTIFACTS = [
16
18
  'HidingKernelToRollup',
17
19
  'HidingKernelToPublic'
18
20
  ];
19
21
  /**
20
22
  * Batch verifier for Chonk IVC proofs. Uses the bb batch verifier service
21
- * which batches IPA verification into a single SRS MSM for better throughput.
23
+ * which batches IPA verification into a constant number of SRS MSMs for better throughput.
22
24
  *
23
25
  * Architecture:
24
26
  * - Spawns a persistent `bb msgpack run` process via Barretenberg (native backend)
@@ -31,6 +33,7 @@ const execFileAsync = promisify(execFile);
31
33
  batchSize;
32
34
  label;
33
35
  bb;
36
+ fifoDir;
34
37
  fifoPath;
35
38
  nextRequestId;
36
39
  pendingRequests;
@@ -39,17 +42,22 @@ const execFileAsync = promisify(execFile);
39
42
  logger;
40
43
  /** Maps artifact name to VK index in the batch verifier. */ vkIndexMap;
41
44
  /** Bound cleanup handler for process exit signals. */ exitCleanup;
45
+ stopped;
46
+ fatalError;
47
+ pendingDrainedResolvers;
42
48
  constructor(config, vkBuffers, batchSize, label){
43
49
  this.config = config;
44
50
  this.vkBuffers = vkBuffers;
45
51
  this.batchSize = batchSize;
46
52
  this.label = label;
53
+ this.fifoPath = '';
47
54
  this.nextRequestId = 0;
48
55
  this.pendingRequests = new Map();
49
56
  this.logger = createLogger('bb-prover:batch_chonk_verifier');
50
57
  this.vkIndexMap = new Map();
51
58
  this.exitCleanup = null;
52
- this.fifoPath = path.join(os.tmpdir(), `bb-batch-${label}-${process.pid}-${Date.now()}.fifo`);
59
+ this.stopped = false;
60
+ this.pendingDrainedResolvers = new Set();
53
61
  this.fifoReader = new FifoFrameReader();
54
62
  this.sendQueue = new SerialQueue();
55
63
  this.sendQueue.start(1);
@@ -80,47 +88,91 @@ const execFileAsync = promisify(execFile);
80
88
  }
81
89
  async start() {
82
90
  this.logger.info('Starting BatchChonkVerifier');
83
- this.bb = await Barretenberg.new({
84
- bbPath: this.config.bbBinaryPath,
85
- backend: BackendType.NativeUnixSocket
86
- });
87
- await this.bb.initSRSChonk();
88
- await execFileAsync('mkfifo', [
89
- this.fifoPath
90
- ]);
91
- this.registerExitCleanup();
92
- await this.bb.chonkBatchVerifierStart({
93
- vks: this.vkBuffers,
94
- numCores: this.config.bbChonkVerifyConcurrency || 0,
95
- batchSize: this.batchSize,
96
- fifoPath: this.fifoPath
97
- });
98
- this.startFifoReader();
91
+ try {
92
+ this.bb = await Barretenberg.new({
93
+ bbPath: this.config.bbBinaryPath,
94
+ backend: BackendType.NativeUnixSocket
95
+ });
96
+ await this.bb.initSRSChonk();
97
+ // Keep the FIFO in a private directory so cleanup has a single owner.
98
+ this.fifoDir = await mkdtemp(path.join(os.tmpdir(), `bb-batch-${this.label}-${process.pid}-`));
99
+ this.fifoPath = path.join(this.fifoDir, 'results.fifo');
100
+ await execFileAsync('mkfifo', [
101
+ this.fifoPath
102
+ ]);
103
+ this.registerExitCleanup();
104
+ this.startFifoReader();
105
+ await this.bb.chonkBatchVerifierStart({
106
+ vks: this.vkBuffers,
107
+ numCores: this.config.bbChonkVerifyConcurrency || 0,
108
+ batchSize: this.batchSize,
109
+ fifoPath: this.fifoPath
110
+ });
111
+ } catch (err) {
112
+ this.fifoReader.stop();
113
+ this.deregisterExitCleanup();
114
+ await this.cleanupFifo();
115
+ await this.bb?.destroy().catch(()=>{});
116
+ throw err;
117
+ }
99
118
  this.logger.info('BatchChonkVerifier started', {
100
119
  fifoPath: this.fifoPath
101
120
  });
102
121
  }
103
122
  verifyProof(tx) {
104
- const circuit = tx.data.forPublic ? 'HidingKernelToPublic' : 'HidingKernelToRollup';
105
- const vkIndex = this.vkIndexMap.get(circuit);
106
- if (vkIndex === undefined) {
107
- throw new Error(`No VK index for circuit ${circuit}`);
108
- }
109
- const proofWithPubInputs = tx.chonkProof.attachPublicInputs(tx.data.publicInputs().toFields());
110
- const proofFields = proofWithPubInputs.fieldsWithPublicInputs.map((f)=>f.toBuffer());
111
- return this.enqueueProof(vkIndex, proofFields);
123
+ const totalTimer = new Timer();
124
+ return (async ()=>{
125
+ const circuit = tx.data.forPublic ? 'HidingKernelToPublic' : 'HidingKernelToRollup';
126
+ const vkIndex = this.vkIndexMap.get(circuit);
127
+ if (vkIndex === undefined) {
128
+ throw new Error(`No VK index for circuit ${circuit}`);
129
+ }
130
+ const proofWithPubInputs = tx.chonkProof.attachPublicInputs(tx.data.publicInputs().toFields());
131
+ const proofFields = proofWithPubInputs.fieldsWithPublicInputs.map((f)=>f.toBuffer());
132
+ return await this.enqueueProof(vkIndex, proofFields);
133
+ })().catch((err)=>{
134
+ this.logger.warn(`Failed to verify Chonk proof for tx ${tx.getTxHash().toString()}: ${String(err)}`);
135
+ return {
136
+ valid: false,
137
+ durationMs: 0,
138
+ totalDurationMs: totalTimer.ms()
139
+ };
140
+ });
112
141
  }
113
142
  /** Enqueue raw proof fields for verification. Used directly by tests with custom VKs. */ enqueueProof(vkIndex, proofFields) {
143
+ if (this.stopped) {
144
+ return Promise.reject(new Error('BatchChonkVerifier stopped'));
145
+ }
146
+ if (this.fatalError) {
147
+ return Promise.reject(this.fatalError);
148
+ }
114
149
  const totalTimer = new Timer();
115
150
  const requestId = this.nextRequestId++;
116
151
  const resultPromise = new Promise((resolve, reject)=>{
152
+ const timeout = setTimeout(()=>{
153
+ const pending = this.pendingRequests.get(requestId);
154
+ if (!pending) {
155
+ return;
156
+ }
157
+ this.pendingRequests.delete(requestId);
158
+ pending.reject(new Error(`BatchChonkVerifier result timed out for request_id=${requestId}`));
159
+ this.notifyPendingDrained();
160
+ }, RESULT_TIMEOUT_MS);
161
+ // A pending result timer must never keep the host process alive on its own (e.g. an
162
+ // orphaned request at process exit); the FIFO reader keeps the loop alive while results
163
+ // are genuinely awaited.
164
+ timeout.unref();
117
165
  this.pendingRequests.set(requestId, {
118
166
  resolve,
119
167
  reject,
120
- totalTimer
168
+ totalTimer,
169
+ timeout
121
170
  });
122
171
  });
123
172
  void this.sendQueue.put(async ()=>{
173
+ if (this.fatalError) {
174
+ throw this.fatalError;
175
+ }
124
176
  await this.bb.chonkBatchVerifierQueue({
125
177
  requestId,
126
178
  vkIndex,
@@ -130,35 +182,62 @@ const execFileAsync = promisify(execFile);
130
182
  const pending = this.pendingRequests.get(requestId);
131
183
  if (pending) {
132
184
  this.pendingRequests.delete(requestId);
185
+ clearTimeout(pending.timeout);
133
186
  pending.reject(err instanceof Error ? err : new Error(String(err)));
187
+ this.notifyPendingDrained();
134
188
  }
135
189
  });
136
190
  return resultPromise;
137
191
  }
138
192
  async stop() {
139
193
  this.logger.info('Stopping BatchChonkVerifier');
140
- // Stop accepting new proofs
141
- await this.sendQueue.end();
142
- // Stop the bb service (flushes remaining proofs)
194
+ this.stopped = true;
143
195
  try {
144
- await this.bb.chonkBatchVerifierStop({});
196
+ // Stop accepting new proofs and flush the send queue. Bound it so an unresponsive
197
+ // native process can't block teardown of our own event-loop handles indefinitely.
198
+ await this.withTimeout(this.sendQueue.end(), STOP_DRAIN_TIMEOUT_MS, 'send queue flush');
199
+ // Stop the bb service (flushes remaining proofs).
200
+ await this.withTimeout(this.bb.chonkBatchVerifierStop({}), STOP_DRAIN_TIMEOUT_MS, 'chonkBatchVerifierStop');
201
+ // Native stop flushes callbacks; keep the FIFO open until those frames are observed.
202
+ const drained = await this.waitForPendingRequestsToDrain(STOP_DRAIN_TIMEOUT_MS);
203
+ if (!drained) {
204
+ this.rejectPendingRequests(new Error('Timed out waiting for BatchChonkVerifier results during stop'));
205
+ }
145
206
  } catch (err) {
146
- this.logger.warn(`Error stopping batch verifier service: ${err}`);
147
- }
148
- // Stop FIFO reader
149
- this.fifoReader.stop();
150
- // Clean up FIFO file and deregister exit handler
151
- await unlink(this.fifoPath).catch(()=>{});
152
- this.deregisterExitCleanup();
153
- // Reject any remaining pending requests
154
- for (const [id, pending] of this.pendingRequests){
155
- pending.reject(new Error('BatchChonkVerifier stopped'));
156
- this.pendingRequests.delete(id);
207
+ this.logger.warn(`Error during BatchChonkVerifier graceful stop: ${err}`);
208
+ this.rejectPendingRequests(err instanceof Error ? err : new Error(String(err)));
209
+ } finally{
210
+ // Always release our own event-loop handles — the FIFO read stream (a blocking
211
+ // threadpool read that can't be unref'd), the unix socket, the native process, and the
212
+ // exit handler — so the host process exits cleanly even if the native backend is wedged.
213
+ this.fifoReader.stop();
214
+ this.deregisterExitCleanup();
215
+ await this.cleanupFifo();
216
+ await this.bb.destroy().catch((err)=>this.logger.warn(`Error destroying bb backend during stop: ${err}`));
157
217
  }
158
- // Destroy bb process
159
- await this.bb.destroy();
160
218
  this.logger.info('BatchChonkVerifier stopped');
161
219
  }
220
+ /**
221
+ * Races a promise against a timeout so a wedged native backend can't block teardown. The
222
+ * timer is unref'd so it never keeps the process alive; the underlying promise stays handled
223
+ * by the race even if the timeout wins, so it cannot surface as an unhandled rejection.
224
+ */ async withTimeout(promise, timeoutMs, label) {
225
+ let timer;
226
+ const timeout = new Promise((_, reject)=>{
227
+ timer = setTimeout(()=>reject(new Error(`BatchChonkVerifier ${label} timed out after ${timeoutMs}ms`)), timeoutMs);
228
+ timer.unref();
229
+ });
230
+ try {
231
+ return await Promise.race([
232
+ promise,
233
+ timeout
234
+ ]);
235
+ } finally{
236
+ if (timer) {
237
+ clearTimeout(timer);
238
+ }
239
+ }
240
+ }
162
241
  startFifoReader() {
163
242
  const unpackr = new Unpackr({
164
243
  useRecords: false
@@ -169,16 +248,18 @@ const execFileAsync = promisify(execFile);
169
248
  this.handleResult(result);
170
249
  } catch (err) {
171
250
  this.logger.error(`FIFO: failed to decode msgpack result: ${err}`);
251
+ // A corrupt result stream cannot safely be matched to outstanding requests.
252
+ this.failVerifier(err instanceof Error ? err : new Error(String(err)));
172
253
  }
173
254
  });
174
255
  this.fifoReader.on('error', (err)=>{
175
256
  this.logger.error(`FIFO reader error: ${err}`);
257
+ this.failVerifier(err);
176
258
  });
177
259
  this.fifoReader.on('end', ()=>{
178
260
  this.logger.debug('FIFO reader: stream ended');
179
- for (const [id, pending] of this.pendingRequests){
180
- pending.reject(new Error('FIFO stream ended unexpectedly'));
181
- this.pendingRequests.delete(id);
261
+ if (!this.stopped) {
262
+ this.failVerifier(new Error('FIFO stream ended unexpectedly'));
182
263
  }
183
264
  });
184
265
  this.fifoReader.start(this.fifoPath);
@@ -190,6 +271,7 @@ const execFileAsync = promisify(execFile);
190
271
  return;
191
272
  }
192
273
  this.pendingRequests.delete(result.request_id);
274
+ clearTimeout(pending.timeout);
193
275
  const valid = result.status === 0; // VerifyStatus::OK
194
276
  const durationMs = result.time_in_verify_ms;
195
277
  const totalDurationMs = pending.totalTimer.ms();
@@ -208,25 +290,95 @@ const execFileAsync = promisify(execFile);
208
290
  });
209
291
  }
210
292
  pending.resolve(ivcResult);
293
+ this.notifyPendingDrained();
211
294
  }
212
295
  registerExitCleanup() {
213
- // Signal handlers must be synchronous — unlinkSync is intentional here
214
296
  this.exitCleanup = ()=>{
215
- try {
216
- unlinkSync(this.fifoPath);
217
- } catch {
218
- /* ignore */ }
297
+ this.cleanupFifoSync();
219
298
  };
220
299
  process.on('exit', this.exitCleanup);
221
- process.on('SIGINT', this.exitCleanup);
222
- process.on('SIGTERM', this.exitCleanup);
223
300
  }
224
301
  deregisterExitCleanup() {
225
302
  if (this.exitCleanup) {
226
303
  process.removeListener('exit', this.exitCleanup);
227
- process.removeListener('SIGINT', this.exitCleanup);
228
- process.removeListener('SIGTERM', this.exitCleanup);
229
304
  this.exitCleanup = null;
230
305
  }
231
306
  }
307
+ rejectPendingRequests(error) {
308
+ for (const [id, pending] of Array.from(this.pendingRequests)){
309
+ pending.reject(error);
310
+ clearTimeout(pending.timeout);
311
+ this.pendingRequests.delete(id);
312
+ }
313
+ this.notifyPendingDrained();
314
+ }
315
+ failVerifier(error) {
316
+ if (!this.fatalError) {
317
+ this.fatalError = error;
318
+ }
319
+ this.rejectPendingRequests(error);
320
+ }
321
+ waitForPendingRequestsToDrain(timeoutMs) {
322
+ if (this.pendingRequests.size === 0) {
323
+ return Promise.resolve(true);
324
+ }
325
+ let timeout;
326
+ let onDrain;
327
+ const drained = new Promise((resolve)=>{
328
+ onDrain = ()=>resolve(true);
329
+ this.pendingDrainedResolvers.add(onDrain);
330
+ });
331
+ const timedOut = new Promise((resolve)=>{
332
+ timeout = setTimeout(()=>resolve(false), timeoutMs);
333
+ timeout.unref();
334
+ });
335
+ return Promise.race([
336
+ drained,
337
+ timedOut
338
+ ]).finally(()=>{
339
+ if (timeout) {
340
+ clearTimeout(timeout);
341
+ }
342
+ if (onDrain) {
343
+ this.pendingDrainedResolvers.delete(onDrain);
344
+ }
345
+ });
346
+ }
347
+ notifyPendingDrained() {
348
+ if (this.pendingRequests.size > 0) {
349
+ return;
350
+ }
351
+ for (const resolve of Array.from(this.pendingDrainedResolvers)){
352
+ resolve();
353
+ }
354
+ }
355
+ async cleanupFifo() {
356
+ if (this.fifoDir) {
357
+ await rm(this.fifoDir, {
358
+ recursive: true,
359
+ force: true
360
+ }).catch(()=>{});
361
+ } else if (this.fifoPath) {
362
+ await rm(this.fifoPath, {
363
+ force: true
364
+ }).catch(()=>{});
365
+ }
366
+ this.fifoDir = undefined;
367
+ this.fifoPath = '';
368
+ }
369
+ cleanupFifoSync() {
370
+ try {
371
+ if (this.fifoDir) {
372
+ rmSync(this.fifoDir, {
373
+ recursive: true,
374
+ force: true
375
+ });
376
+ } else if (this.fifoPath) {
377
+ rmSync(this.fifoPath, {
378
+ force: true
379
+ });
380
+ }
381
+ } catch {
382
+ /* ignore */ }
383
+ }
232
384
  }
@@ -8,11 +8,14 @@ import type { BBConfig } from '../config.js';
8
8
  export declare class BBCircuitVerifier implements ClientProtocolCircuitVerifier {
9
9
  private config;
10
10
  private logger;
11
+ private bbJsFactory;
11
12
  private constructor();
12
13
  stop(): Promise<void>;
13
14
  static new(config: BBConfig, logger?: Logger): Promise<BBCircuitVerifier>;
14
15
  getVerificationKeyData(circuit: ProtocolArtifact): VerificationKeyData;
16
+ /** Verify an UltraHonk proof via bb.js API (no temp files). */
15
17
  verifyProofForCircuit(circuit: ServerProtocolArtifact, proof: Proof): Promise<void>;
18
+ /** Verify a Chonk (IVC) proof from a transaction via bb.js API. */
16
19
  verifyProof(tx: Tx): Promise<IVCProofVerificationResult>;
17
20
  }
18
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYmJfdmVyaWZpZXIuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3Ny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pE;CACF"}
1
+ {"version":3,"file":"bb_verifier.d.ts","sourceRoot":"","sources":["../../src/verifier/bb_verifier.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,MAAM,EAAgB,MAAM,uBAAuB,CAAC;AAGlE,OAAO,EAEL,KAAK,gBAAgB,EACrB,KAAK,sBAAsB,EAE5B,MAAM,2CAA2C,CAAC;AACnD,OAAO,KAAK,EAAE,6BAA6B,EAAE,0BAA0B,EAAE,MAAM,iCAAiC,CAAC;AACjH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAElD,OAAO,EAAE,EAAE,EAAE,MAAM,kBAAkB,CAAC;AACtC,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAK7D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAG7C,qBAAa,iBAAkB,YAAW,6BAA6B;IAInE,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,MAAM;IAJhB,OAAO,CAAC,WAAW,CAAc;IAEjC,OAAO,eAWN;IAEM,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAE3B;IAED,OAAoB,GAAG,CAAC,MAAM,EAAE,QAAQ,EAAE,MAAM,SAAqC,8BAMpF;IAEM,sBAAsB,CAAC,OAAO,EAAE,gBAAgB,GAAG,mBAAmB,CAM5E;IAED,+DAA+D;IAClD,qBAAqB,CAAC,OAAO,EAAE,sBAAsB,EAAE,KAAK,EAAE,KAAK,iBA4B/E;IAED,mEAAmE;IACtD,WAAW,CAAC,EAAE,EAAE,EAAE,GAAG,OAAO,CAAC,0BAA0B,CAAC,CA+BpE;CACF"}
@@ -1,22 +1,92 @@
1
- import { runInDirectory } from '@aztec/foundation/fs';
1
+ function _ts_add_disposable_resource(env, value, async) {
2
+ if (value !== null && value !== void 0) {
3
+ if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
4
+ var dispose, inner;
5
+ if (async) {
6
+ if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
7
+ dispose = value[Symbol.asyncDispose];
8
+ }
9
+ if (dispose === void 0) {
10
+ if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
11
+ dispose = value[Symbol.dispose];
12
+ if (async) inner = dispose;
13
+ }
14
+ if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
15
+ if (inner) dispose = function() {
16
+ try {
17
+ inner.call(this);
18
+ } catch (e) {
19
+ return Promise.reject(e);
20
+ }
21
+ };
22
+ env.stack.push({
23
+ value: value,
24
+ dispose: dispose,
25
+ async: async
26
+ });
27
+ } else if (async) {
28
+ env.stack.push({
29
+ async: true
30
+ });
31
+ }
32
+ return value;
33
+ }
34
+ function _ts_dispose_resources(env) {
35
+ var _SuppressedError = typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
36
+ var e = new Error(message);
37
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
38
+ };
39
+ return (_ts_dispose_resources = function _ts_dispose_resources(env) {
40
+ function fail(e) {
41
+ env.error = env.hasError ? new _SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
42
+ env.hasError = true;
43
+ }
44
+ var r, s = 0;
45
+ function next() {
46
+ while(r = env.stack.pop()){
47
+ try {
48
+ if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
49
+ if (r.dispose) {
50
+ var result = r.dispose.call(r.value);
51
+ if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) {
52
+ fail(e);
53
+ return next();
54
+ });
55
+ } else s |= 1;
56
+ } catch (e) {
57
+ fail(e);
58
+ }
59
+ }
60
+ if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
61
+ if (env.hasError) throw env.error;
62
+ }
63
+ return next();
64
+ })(env);
65
+ }
2
66
  import { createLogger } from '@aztec/foundation/log';
3
67
  import { Timer } from '@aztec/foundation/timer';
4
68
  import { ProtocolCircuitVks } from '@aztec/noir-protocol-circuits-types/server/vks';
5
69
  import { mapProtocolArtifactNameToCircuitName } from '@aztec/noir-protocol-circuits-types/types';
6
70
  import { promises as fs } from 'fs';
7
- import * as path from 'path';
8
- import { BB_RESULT, PROOF_FILENAME, PUBLIC_INPUTS_FILENAME, VK_FILENAME, verifyChonkProof, verifyProof } from '../bb/execute.js';
71
+ import { BBJsFactory } from '../bb/bb_js_backend.js';
9
72
  import { getUltraHonkFlavorForCircuit } from '../honk.js';
10
- import { writeChonkProofToPath } from '../prover/proof_utils.js';
11
73
  export class BBCircuitVerifier {
12
74
  config;
13
75
  logger;
76
+ bbJsFactory;
14
77
  constructor(config, logger){
15
78
  this.config = config;
16
79
  this.logger = logger;
80
+ // BB_NUM_IVC_VERIFIERS bounds the number of long-lived bb processes the pool keeps alive.
81
+ // If 0, fall back to spawning a fresh bb per verification.
82
+ this.bbJsFactory = new BBJsFactory(config.bbBinaryPath, {
83
+ poolSize: config.numConcurrentIVCVerifiers > 0 ? config.numConcurrentIVCVerifiers : undefined,
84
+ logger,
85
+ debugDir: config.bbDebugOutputDir
86
+ });
17
87
  }
18
88
  stop() {
19
- return Promise.resolve();
89
+ return this.bbJsFactory.destroy();
20
90
  }
21
91
  static async new(config, logger = createLogger('bb-prover:verifier')) {
22
92
  if (!config.bbWorkingDirectory) {
@@ -34,64 +104,76 @@ export class BBCircuitVerifier {
34
104
  }
35
105
  return vk;
36
106
  }
37
- async verifyProofForCircuit(circuit, proof) {
38
- const operation = async (bbWorkingDirectory)=>{
39
- const publicInputsFileName = path.join(bbWorkingDirectory, PUBLIC_INPUTS_FILENAME);
40
- const proofFileName = path.join(bbWorkingDirectory, PROOF_FILENAME);
41
- const verificationKeyPath = path.join(bbWorkingDirectory, VK_FILENAME);
107
+ /** Verify an UltraHonk proof via bb.js API (no temp files). */ async verifyProofForCircuit(circuit, proof) {
108
+ const env = {
109
+ stack: [],
110
+ error: void 0,
111
+ hasError: false
112
+ };
113
+ try {
42
114
  const verificationKey = this.getVerificationKeyData(circuit);
115
+ const flavor = getUltraHonkFlavorForCircuit(circuit);
43
116
  this.logger.debug(`${circuit} Verifying with key: ${verificationKey.keyAsFields.hash.toString()}`);
44
- // TODO(https://github.com/AztecProtocol/aztec-packages/issues/13189): Put this proof parsing logic in the proof class.
45
- await fs.writeFile(publicInputsFileName, proof.buffer.slice(0, proof.numPublicInputs * 32));
46
- await fs.writeFile(proofFileName, proof.buffer.slice(proof.numPublicInputs * 32));
47
- await fs.writeFile(verificationKeyPath, verificationKey.keyAsBytes);
48
- const result = await verifyProof(this.config.bbBinaryPath, proofFileName, verificationKeyPath, getUltraHonkFlavorForCircuit(circuit), this.logger);
49
- if (result.status === BB_RESULT.FAILURE) {
50
- const errorMessage = `Failed to verify ${circuit} proof!`;
51
- throw new Error(errorMessage);
117
+ // Split proof buffer into public input fields and proof fields (32-byte each)
118
+ const publicInputFields = splitBufferToFieldArrays(proof.buffer.subarray(0, proof.numPublicInputs * 32));
119
+ const proofFields = splitBufferToFieldArrays(proof.buffer.subarray(proof.numPublicInputs * 32));
120
+ const instance = _ts_add_disposable_resource(env, await this.bbJsFactory.getInstance(), true);
121
+ const { verified, durationMs } = await instance.verifyProof(proofFields, verificationKey.keyAsBytes, publicInputFields, flavor);
122
+ if (!verified) {
123
+ throw new Error(`Failed to verify ${circuit} proof!`);
52
124
  }
53
125
  this.logger.debug(`${circuit} verification successful`, {
54
126
  circuitName: mapProtocolArtifactNameToCircuitName(circuit),
55
- duration: result.durationMs,
127
+ duration: durationMs,
56
128
  eventName: 'circuit-verification',
57
129
  proofType: 'ultra-honk'
58
130
  });
59
- };
60
- await runInDirectory(this.config.bbWorkingDirectory, operation, this.config.bbSkipCleanup, this.logger);
131
+ } catch (e) {
132
+ env.error = e;
133
+ env.hasError = true;
134
+ } finally{
135
+ const result = _ts_dispose_resources(env);
136
+ if (result) await result;
137
+ }
61
138
  }
62
- async verifyProof(tx) {
139
+ /** Verify a Chonk (IVC) proof from a transaction via bb.js API. */ async verifyProof(tx) {
63
140
  const proofType = 'Chonk';
64
141
  try {
65
- const totalTimer = new Timer();
66
- let verificationDuration = 0;
67
- const circuit = tx.data.forPublic ? 'HidingKernelToPublic' : 'HidingKernelToRollup';
68
- // Block below is almost copy-pasted from verifyProofForCircuit
69
- const operation = async (bbWorkingDirectory)=>{
70
- const proofPath = path.join(bbWorkingDirectory, PROOF_FILENAME);
71
- await writeChonkProofToPath(tx.chonkProof.attachPublicInputs(tx.data.publicInputs().toFields()), proofPath);
72
- const verificationKeyPath = path.join(bbWorkingDirectory, VK_FILENAME);
142
+ const env = {
143
+ stack: [],
144
+ error: void 0,
145
+ hasError: false
146
+ };
147
+ try {
148
+ const totalTimer = new Timer();
149
+ const circuit = tx.data.forPublic ? 'HidingKernelToPublic' : 'HidingKernelToRollup';
73
150
  const verificationKey = this.getVerificationKeyData(circuit);
74
- await fs.writeFile(verificationKeyPath, verificationKey.keyAsBytes);
75
- const timer = new Timer();
76
- const result = await verifyChonkProof(this.config.bbBinaryPath, proofPath, verificationKeyPath, this.logger, this.config.bbIVCConcurrency);
77
- verificationDuration = timer.ms();
78
- if (result.status === BB_RESULT.FAILURE) {
79
- const errorMessage = `Failed to verify ${proofType} proof for ${circuit}!`;
80
- throw new Error(errorMessage);
151
+ // Reconstruct the full proof with public inputs prepended, then convert Fr[] to Uint8Array[]
152
+ const proofWithPubInputs = tx.chonkProof.attachPublicInputs(tx.data.publicInputs().toFields());
153
+ const fieldsAsBuffers = proofWithPubInputs.fieldsWithPublicInputs.map((f)=>new Uint8Array(f.toBuffer()));
154
+ const instance = _ts_add_disposable_resource(env, await this.bbJsFactory.getInstance(), true);
155
+ const { verified, durationMs } = await instance.verifyChonkProof(fieldsAsBuffers, verificationKey.keyAsBytes);
156
+ if (!verified) {
157
+ throw new Error(`Failed to verify ${proofType} proof for ${circuit}!`);
81
158
  }
82
159
  this.logger.debug(`${proofType} verification successful`, {
83
160
  circuitName: mapProtocolArtifactNameToCircuitName(circuit),
84
- duration: result.durationMs,
161
+ duration: durationMs,
85
162
  eventName: 'circuit-verification',
86
163
  proofType: 'chonk'
87
164
  });
88
- };
89
- await runInDirectory(this.config.bbWorkingDirectory, operation, this.config.bbSkipCleanup, this.logger);
90
- return {
91
- valid: true,
92
- durationMs: verificationDuration,
93
- totalDurationMs: totalTimer.ms()
94
- };
165
+ return {
166
+ valid: true,
167
+ durationMs,
168
+ totalDurationMs: totalTimer.ms()
169
+ };
170
+ } catch (e) {
171
+ env.error = e;
172
+ env.hasError = true;
173
+ } finally{
174
+ const result = _ts_dispose_resources(env);
175
+ if (result) await result;
176
+ }
95
177
  } catch (err) {
96
178
  this.logger.warn(`Failed to verify ${proofType} proof for tx ${tx.getTxHash().toString()}: ${String(err)}`);
97
179
  return {
@@ -102,3 +184,10 @@ export class BBCircuitVerifier {
102
184
  }
103
185
  }
104
186
  }
187
+ /** Split a buffer into 32-byte Uint8Array field elements. */ function splitBufferToFieldArrays(buffer) {
188
+ const fields = [];
189
+ for(let i = 0; i < buffer.length; i += 32){
190
+ fields.push(new Uint8Array(buffer.subarray(i, i + 32)));
191
+ }
192
+ return fields;
193
+ }