@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
@@ -0,0 +1,384 @@
1
+ import { BackendType, Barretenberg } from '@aztec/bb.js';
2
+ import { FifoFrameReader } from '@aztec/foundation/fifo';
3
+ import { createLogger } from '@aztec/foundation/log';
4
+ import { SerialQueue } from '@aztec/foundation/queue';
5
+ import { Timer } from '@aztec/foundation/timer';
6
+ import { ProtocolCircuitVks } from '@aztec/noir-protocol-circuits-types/server/vks';
7
+ import { Unpackr } from 'msgpackr';
8
+ import { execFile } from 'node:child_process';
9
+ import { rmSync } from 'node:fs';
10
+ import { mkdtemp, rm } from 'node:fs/promises';
11
+ import * as os from 'node:os';
12
+ import * as path from 'node:path';
13
+ import { promisify } from 'node:util';
14
+ const execFileAsync = promisify(execFile);
15
+ const RESULT_TIMEOUT_MS = 5 * 60 * 1000;
16
+ const STOP_DRAIN_TIMEOUT_MS = 5_000;
17
+ /** Maps client protocol artifacts used for chonk verification to VK indices. */ const CHONK_VK_ARTIFACTS = [
18
+ 'HidingKernelToRollup',
19
+ 'HidingKernelToPublic'
20
+ ];
21
+ /**
22
+ * Batch verifier for Chonk IVC proofs. Uses the bb batch verifier service
23
+ * which batches IPA verification into a constant number of SRS MSMs for better throughput.
24
+ *
25
+ * Architecture:
26
+ * - Spawns a persistent `bb msgpack run` process via Barretenberg (native backend)
27
+ * - Sends proofs via the msgpack RPC protocol (ChonkBatchVerifierQueue)
28
+ * - Receives results via a named FIFO pipe (async, out-of-order)
29
+ * - Bisects batch failures to isolate individual bad proofs
30
+ */ export class BatchChonkVerifier {
31
+ config;
32
+ vkBuffers;
33
+ batchSize;
34
+ label;
35
+ bb;
36
+ fifoDir;
37
+ fifoPath;
38
+ nextRequestId;
39
+ pendingRequests;
40
+ sendQueue;
41
+ fifoReader;
42
+ logger;
43
+ /** Maps artifact name to VK index in the batch verifier. */ vkIndexMap;
44
+ /** Bound cleanup handler for process exit signals. */ exitCleanup;
45
+ stopped;
46
+ fatalError;
47
+ pendingDrainedResolvers;
48
+ constructor(config, vkBuffers, batchSize, label){
49
+ this.config = config;
50
+ this.vkBuffers = vkBuffers;
51
+ this.batchSize = batchSize;
52
+ this.label = label;
53
+ this.fifoPath = '';
54
+ this.nextRequestId = 0;
55
+ this.pendingRequests = new Map();
56
+ this.logger = createLogger('bb-prover:batch_chonk_verifier');
57
+ this.vkIndexMap = new Map();
58
+ this.exitCleanup = null;
59
+ this.stopped = false;
60
+ this.pendingDrainedResolvers = new Set();
61
+ this.fifoReader = new FifoFrameReader();
62
+ this.sendQueue = new SerialQueue();
63
+ this.sendQueue.start(1);
64
+ }
65
+ /** Create and start a BatchChonkVerifier using the protocol circuit VKs. */ static async new(config, batchSize, label) {
66
+ const vkBuffers = [];
67
+ const vkIndexMap = new Map();
68
+ for (const artifact of CHONK_VK_ARTIFACTS){
69
+ const vk = ProtocolCircuitVks[artifact];
70
+ if (!vk) {
71
+ throw new Error(`Missing VK for ${artifact}`);
72
+ }
73
+ vkIndexMap.set(artifact, vkBuffers.length);
74
+ vkBuffers.push(vk.keyAsBytes);
75
+ }
76
+ const verifier = new BatchChonkVerifier(config, vkBuffers, batchSize, label);
77
+ verifier.vkIndexMap = vkIndexMap;
78
+ await verifier.start();
79
+ return verifier;
80
+ }
81
+ /** Create and start a BatchChonkVerifier with custom VKs (for testing). */ static async newForTesting(config, vks, batchSize) {
82
+ const verifier = new BatchChonkVerifier(config, vks, batchSize, 'test');
83
+ for(let i = 0; i < vks.length; i++){
84
+ verifier.vkIndexMap.set(String(i), i);
85
+ }
86
+ await verifier.start();
87
+ return verifier;
88
+ }
89
+ async start() {
90
+ this.logger.info('Starting BatchChonkVerifier');
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
+ }
118
+ this.logger.info('BatchChonkVerifier started', {
119
+ fifoPath: this.fifoPath
120
+ });
121
+ }
122
+ verifyProof(tx) {
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
+ });
141
+ }
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
+ }
149
+ const totalTimer = new Timer();
150
+ const requestId = this.nextRequestId++;
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();
165
+ this.pendingRequests.set(requestId, {
166
+ resolve,
167
+ reject,
168
+ totalTimer,
169
+ timeout
170
+ });
171
+ });
172
+ void this.sendQueue.put(async ()=>{
173
+ if (this.fatalError) {
174
+ throw this.fatalError;
175
+ }
176
+ await this.bb.chonkBatchVerifierQueue({
177
+ requestId,
178
+ vkIndex,
179
+ proofFields
180
+ });
181
+ }).catch((err)=>{
182
+ const pending = this.pendingRequests.get(requestId);
183
+ if (pending) {
184
+ this.pendingRequests.delete(requestId);
185
+ clearTimeout(pending.timeout);
186
+ pending.reject(err instanceof Error ? err : new Error(String(err)));
187
+ this.notifyPendingDrained();
188
+ }
189
+ });
190
+ return resultPromise;
191
+ }
192
+ async stop() {
193
+ this.logger.info('Stopping BatchChonkVerifier');
194
+ this.stopped = true;
195
+ try {
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
+ }
206
+ } catch (err) {
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}`));
217
+ }
218
+ this.logger.info('BatchChonkVerifier stopped');
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
+ }
241
+ startFifoReader() {
242
+ const unpackr = new Unpackr({
243
+ useRecords: false
244
+ });
245
+ this.fifoReader.on('frame', (payload)=>{
246
+ try {
247
+ const result = unpackr.unpack(payload);
248
+ this.handleResult(result);
249
+ } catch (err) {
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)));
253
+ }
254
+ });
255
+ this.fifoReader.on('error', (err)=>{
256
+ this.logger.error(`FIFO reader error: ${err}`);
257
+ this.failVerifier(err);
258
+ });
259
+ this.fifoReader.on('end', ()=>{
260
+ this.logger.debug('FIFO reader: stream ended');
261
+ if (!this.stopped) {
262
+ this.failVerifier(new Error('FIFO stream ended unexpectedly'));
263
+ }
264
+ });
265
+ this.fifoReader.start(this.fifoPath);
266
+ }
267
+ handleResult(result) {
268
+ const pending = this.pendingRequests.get(result.request_id);
269
+ if (!pending) {
270
+ this.logger.warn(`Received result for unknown request_id=${result.request_id}`);
271
+ return;
272
+ }
273
+ this.pendingRequests.delete(result.request_id);
274
+ clearTimeout(pending.timeout);
275
+ const valid = result.status === 0; // VerifyStatus::OK
276
+ const durationMs = result.time_in_verify_ms;
277
+ const totalDurationMs = pending.totalTimer.ms();
278
+ const ivcResult = {
279
+ valid,
280
+ durationMs,
281
+ totalDurationMs
282
+ };
283
+ if (!valid) {
284
+ this.logger.warn(`Proof verification failed for request_id=${result.request_id}: ${result.error_message}`);
285
+ } else {
286
+ this.logger.debug(`Proof verified`, {
287
+ requestId: result.request_id,
288
+ durationMs: Math.ceil(durationMs),
289
+ totalDurationMs: Math.ceil(totalDurationMs)
290
+ });
291
+ }
292
+ pending.resolve(ivcResult);
293
+ this.notifyPendingDrained();
294
+ }
295
+ registerExitCleanup() {
296
+ this.exitCleanup = ()=>{
297
+ this.cleanupFifoSync();
298
+ };
299
+ process.on('exit', this.exitCleanup);
300
+ }
301
+ deregisterExitCleanup() {
302
+ if (this.exitCleanup) {
303
+ process.removeListener('exit', this.exitCleanup);
304
+ this.exitCleanup = null;
305
+ }
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
+ }
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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYmJfdmVyaWZpZXIuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy92ZXJpZmllci9iYl92ZXJpZmllci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFDQSxPQUFP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VBQUUsS0FBSyxFQUFFLEtBQUssaUJBNEIvRTtJQUVELG1FQUFtRTtJQUN0RCxXQUFXLENBQUMsRUFBRSxFQUFFLEVBQUUsR0FBRyxPQUFPLENBQUMsMEJBQTBCLENBQUMsQ0ErQnBFO0NBQ0YifQ==
@@ -1 +1 @@
1
- {"version":3,"file":"bb_verifier.d.ts","sourceRoot":"","sources":["../../src/verifier/bb_verifier.ts"],"names":[],"mappings":"AACA,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;AAa7D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAI7C,qBAAa,iBAAkB,YAAW,6BAA6B;IAEnE,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,MAAM;IAFhB,OAAO,eAGH;IAEG,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;IAEY,qBAAqB,CAAC,OAAO,EAAE,sBAAsB,EAAE,KAAK,EAAE,KAAK,iBAmC/E;IAEY,WAAW,CAAC,EAAE,EAAE,EAAE,GAAG,OAAO,CAAC,0BAA0B,CAAC,CA6CpE;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
+ }
@@ -1,3 +1,4 @@
1
+ export * from './batch_chonk_verifier.js';
1
2
  export * from './bb_verifier.js';
2
3
  export * from './queued_chonk_verifier.js';
3
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy92ZXJpZmllci9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxjQUFjLGtCQUFrQixDQUFDO0FBQ2pDLGNBQWMsNEJBQTRCLENBQUMifQ==
4
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy92ZXJpZmllci9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxjQUFjLDJCQUEyQixDQUFDO0FBQzFDLGNBQWMsa0JBQWtCLENBQUM7QUFDakMsY0FBYyw0QkFBNEIsQ0FBQyJ9
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/verifier/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,4BAA4B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/verifier/index.ts"],"names":[],"mappings":"AAAA,cAAc,2BAA2B,CAAC;AAC1C,cAAc,kBAAkB,CAAC;AACjC,cAAc,4BAA4B,CAAC"}
@@ -1,2 +1,3 @@
1
+ export * from './batch_chonk_verifier.js';
1
2
  export * from './bb_verifier.js';
2
3
  export * from './queued_chonk_verifier.js';