@aztec/bb-prover 0.0.1-commit.e6bd8901 → 0.0.1-commit.ec7ac5448

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 (38) hide show
  1. package/dest/avm_proving_tests/avm_proving_tester.d.ts +5 -4
  2. package/dest/avm_proving_tests/avm_proving_tester.d.ts.map +1 -1
  3. package/dest/avm_proving_tests/avm_proving_tester.js +9 -6
  4. package/dest/bb/execute.d.ts +2 -1
  5. package/dest/bb/execute.d.ts.map +1 -1
  6. package/dest/bb/execute.js +6 -1
  7. package/dest/config.d.ts +12 -1
  8. package/dest/config.d.ts.map +1 -1
  9. package/dest/instrumentation.d.ts +1 -1
  10. package/dest/instrumentation.d.ts.map +1 -1
  11. package/dest/instrumentation.js +12 -4
  12. package/dest/prover/client/bb_private_kernel_prover.d.ts +1 -1
  13. package/dest/prover/client/bb_private_kernel_prover.d.ts.map +1 -1
  14. package/dest/prover/client/bb_private_kernel_prover.js +18 -6
  15. package/dest/prover/server/bb_prover.d.ts +1 -1
  16. package/dest/prover/server/bb_prover.d.ts.map +1 -1
  17. package/dest/prover/server/bb_prover.js +1 -1
  18. package/dest/test/delay_values.js +1 -1
  19. package/dest/verifier/batch_chonk_verifier.d.ts +45 -0
  20. package/dest/verifier/batch_chonk_verifier.d.ts.map +1 -0
  21. package/dest/verifier/batch_chonk_verifier.js +232 -0
  22. package/dest/verifier/index.d.ts +2 -1
  23. package/dest/verifier/index.d.ts.map +1 -1
  24. package/dest/verifier/index.js +1 -0
  25. package/dest/verifier/queued_chonk_verifier.d.ts +2 -3
  26. package/dest/verifier/queued_chonk_verifier.d.ts.map +1 -1
  27. package/dest/verifier/queued_chonk_verifier.js +8 -7
  28. package/package.json +17 -16
  29. package/src/avm_proving_tests/avm_proving_tester.ts +12 -0
  30. package/src/bb/execute.ts +10 -1
  31. package/src/config.ts +11 -0
  32. package/src/instrumentation.ts +12 -4
  33. package/src/prover/client/bb_private_kernel_prover.ts +18 -5
  34. package/src/prover/server/bb_prover.ts +1 -0
  35. package/src/test/delay_values.ts +1 -1
  36. package/src/verifier/batch_chonk_verifier.ts +276 -0
  37. package/src/verifier/index.ts +1 -0
  38. package/src/verifier/queued_chonk_verifier.ts +8 -8
@@ -0,0 +1,276 @@
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 type { ClientProtocolCircuitVerifier, IVCProofVerificationResult } from '@aztec/stdlib/interfaces/server';
8
+ import type { Tx } from '@aztec/stdlib/tx';
9
+
10
+ import { Unpackr } from 'msgpackr';
11
+ import { execFile } from 'node:child_process';
12
+ import { unlinkSync } from 'node:fs';
13
+ import { unlink } from 'node:fs/promises';
14
+ import * as os from 'node:os';
15
+ import * as path from 'node:path';
16
+ import { promisify } from 'node:util';
17
+
18
+ import type { BBConfig } from '../config.js';
19
+
20
+ const execFileAsync = promisify(execFile);
21
+
22
+ /** Result from the FIFO, matching the C++ VerifyResult struct. */
23
+ interface FifoVerifyResult {
24
+ request_id: number;
25
+ status: number;
26
+ error_message: string;
27
+ time_in_verify_ms: number;
28
+ }
29
+
30
+ /** Maps client protocol artifacts used for chonk verification to VK indices. */
31
+ const CHONK_VK_ARTIFACTS = ['HidingKernelToRollup', 'HidingKernelToPublic'] as const;
32
+
33
+ interface PendingRequest {
34
+ resolve: (result: IVCProofVerificationResult) => void;
35
+ reject: (error: Error) => void;
36
+ totalTimer: Timer;
37
+ }
38
+
39
+ /**
40
+ * Batch verifier for Chonk IVC proofs. Uses the bb batch verifier service
41
+ * which batches IPA verification into a single SRS MSM for better throughput.
42
+ *
43
+ * Architecture:
44
+ * - Spawns a persistent `bb msgpack run` process via Barretenberg (native backend)
45
+ * - Sends proofs via the msgpack RPC protocol (ChonkBatchVerifierQueue)
46
+ * - Receives results via a named FIFO pipe (async, out-of-order)
47
+ * - Bisects batch failures to isolate individual bad proofs
48
+ */
49
+ export class BatchChonkVerifier implements ClientProtocolCircuitVerifier {
50
+ private bb!: Barretenberg;
51
+ private fifoPath: string;
52
+ private nextRequestId = 0;
53
+ private pendingRequests = new Map<number, PendingRequest>();
54
+ private sendQueue: SerialQueue;
55
+ private fifoReader: FifoFrameReader;
56
+ private logger = createLogger('bb-prover:batch_chonk_verifier');
57
+ /** Maps artifact name to VK index in the batch verifier. */
58
+ private vkIndexMap = new Map<string, number>();
59
+ /** Bound cleanup handler for process exit signals. */
60
+ private exitCleanup: (() => void) | null = null;
61
+
62
+ private constructor(
63
+ private config: Pick<BBConfig, 'bbChonkVerifyConcurrency'> & Partial<Pick<BBConfig, 'bbBinaryPath'>>,
64
+ private vkBuffers: Uint8Array[],
65
+ private batchSize: number,
66
+ private label: string,
67
+ ) {
68
+ this.fifoPath = path.join(os.tmpdir(), `bb-batch-${label}-${process.pid}-${Date.now()}.fifo`);
69
+ this.fifoReader = new FifoFrameReader();
70
+ this.sendQueue = new SerialQueue();
71
+ this.sendQueue.start(1);
72
+ }
73
+
74
+ /** Create and start a BatchChonkVerifier using the protocol circuit VKs. */
75
+ static async new(config: BBConfig, batchSize: number, label: string): Promise<BatchChonkVerifier> {
76
+ const vkBuffers: Uint8Array[] = [];
77
+ const vkIndexMap = new Map<string, number>();
78
+ for (const artifact of CHONK_VK_ARTIFACTS) {
79
+ const vk = ProtocolCircuitVks[artifact];
80
+ if (!vk) {
81
+ throw new Error(`Missing VK for ${artifact}`);
82
+ }
83
+ vkIndexMap.set(artifact, vkBuffers.length);
84
+ vkBuffers.push(vk.keyAsBytes);
85
+ }
86
+ const verifier = new BatchChonkVerifier(config, vkBuffers, batchSize, label);
87
+ verifier.vkIndexMap = vkIndexMap;
88
+ await verifier.start();
89
+ return verifier;
90
+ }
91
+
92
+ /** Create and start a BatchChonkVerifier with custom VKs (for testing). */
93
+ static async newForTesting(
94
+ config: Pick<BBConfig, 'bbChonkVerifyConcurrency'> & Partial<Pick<BBConfig, 'bbBinaryPath'>>,
95
+ vks: Uint8Array[],
96
+ batchSize: number,
97
+ ): Promise<BatchChonkVerifier> {
98
+ const verifier = new BatchChonkVerifier(config, vks, batchSize, 'test');
99
+ for (let i = 0; i < vks.length; i++) {
100
+ verifier.vkIndexMap.set(String(i), i);
101
+ }
102
+ await verifier.start();
103
+ return verifier;
104
+ }
105
+
106
+ private async start(): Promise<void> {
107
+ this.logger.info('Starting BatchChonkVerifier');
108
+
109
+ this.bb = await Barretenberg.new({
110
+ bbPath: this.config.bbBinaryPath,
111
+ backend: BackendType.NativeUnixSocket,
112
+ });
113
+ await this.bb.initSRSChonk();
114
+
115
+ await execFileAsync('mkfifo', [this.fifoPath]);
116
+ this.registerExitCleanup();
117
+
118
+ await this.bb.chonkBatchVerifierStart({
119
+ vks: this.vkBuffers,
120
+ numCores: this.config.bbChonkVerifyConcurrency || 0,
121
+ batchSize: this.batchSize,
122
+ fifoPath: this.fifoPath,
123
+ });
124
+
125
+ this.startFifoReader();
126
+ this.logger.info('BatchChonkVerifier started', { fifoPath: this.fifoPath });
127
+ }
128
+
129
+ public verifyProof(tx: Tx): Promise<IVCProofVerificationResult> {
130
+ const circuit = tx.data.forPublic ? 'HidingKernelToPublic' : 'HidingKernelToRollup';
131
+ const vkIndex = this.vkIndexMap.get(circuit);
132
+ if (vkIndex === undefined) {
133
+ throw new Error(`No VK index for circuit ${circuit}`);
134
+ }
135
+ const proofWithPubInputs = tx.chonkProof.attachPublicInputs(tx.data.publicInputs().toFields());
136
+ const proofFields = proofWithPubInputs.fieldsWithPublicInputs.map(f => f.toBuffer());
137
+ return this.enqueueProof(vkIndex, proofFields);
138
+ }
139
+
140
+ /** Enqueue raw proof fields for verification. Used directly by tests with custom VKs. */
141
+ public enqueueProof(vkIndex: number, proofFields: Uint8Array[]): Promise<IVCProofVerificationResult> {
142
+ const totalTimer = new Timer();
143
+ const requestId = this.nextRequestId++;
144
+
145
+ const resultPromise = new Promise<IVCProofVerificationResult>((resolve, reject) => {
146
+ this.pendingRequests.set(requestId, { resolve, reject, totalTimer });
147
+ });
148
+
149
+ void this.sendQueue
150
+ .put(async () => {
151
+ await this.bb.chonkBatchVerifierQueue({
152
+ requestId,
153
+ vkIndex,
154
+ proofFields,
155
+ });
156
+ })
157
+ .catch(err => {
158
+ const pending = this.pendingRequests.get(requestId);
159
+ if (pending) {
160
+ this.pendingRequests.delete(requestId);
161
+ pending.reject(err instanceof Error ? err : new Error(String(err)));
162
+ }
163
+ });
164
+
165
+ return resultPromise;
166
+ }
167
+
168
+ public async stop(): Promise<void> {
169
+ this.logger.info('Stopping BatchChonkVerifier');
170
+
171
+ // Stop accepting new proofs
172
+ await this.sendQueue.end();
173
+
174
+ // Stop the bb service (flushes remaining proofs)
175
+ try {
176
+ await this.bb.chonkBatchVerifierStop({});
177
+ } catch (err) {
178
+ this.logger.warn(`Error stopping batch verifier service: ${err}`);
179
+ }
180
+
181
+ // Stop FIFO reader
182
+ this.fifoReader.stop();
183
+
184
+ // Clean up FIFO file and deregister exit handler
185
+ await unlink(this.fifoPath).catch(() => {});
186
+ this.deregisterExitCleanup();
187
+
188
+ // Reject any remaining pending requests
189
+ for (const [id, pending] of this.pendingRequests) {
190
+ pending.reject(new Error('BatchChonkVerifier stopped'));
191
+ this.pendingRequests.delete(id);
192
+ }
193
+
194
+ // Destroy bb process
195
+ await this.bb.destroy();
196
+
197
+ this.logger.info('BatchChonkVerifier stopped');
198
+ }
199
+
200
+ private startFifoReader(): void {
201
+ const unpackr = new Unpackr({ useRecords: false });
202
+
203
+ this.fifoReader.on('frame', (payload: Buffer) => {
204
+ try {
205
+ const result = unpackr.unpack(payload) as FifoVerifyResult;
206
+ this.handleResult(result);
207
+ } catch (err) {
208
+ this.logger.error(`FIFO: failed to decode msgpack result: ${err}`);
209
+ }
210
+ });
211
+
212
+ this.fifoReader.on('error', (err: Error) => {
213
+ this.logger.error(`FIFO reader error: ${err}`);
214
+ });
215
+
216
+ this.fifoReader.on('end', () => {
217
+ this.logger.debug('FIFO reader: stream ended');
218
+ for (const [id, pending] of this.pendingRequests) {
219
+ pending.reject(new Error('FIFO stream ended unexpectedly'));
220
+ this.pendingRequests.delete(id);
221
+ }
222
+ });
223
+
224
+ this.fifoReader.start(this.fifoPath);
225
+ }
226
+
227
+ private handleResult(result: FifoVerifyResult): void {
228
+ const pending = this.pendingRequests.get(result.request_id);
229
+ if (!pending) {
230
+ this.logger.warn(`Received result for unknown request_id=${result.request_id}`);
231
+ return;
232
+ }
233
+ this.pendingRequests.delete(result.request_id);
234
+
235
+ const valid = result.status === 0; // VerifyStatus::OK
236
+ const durationMs = result.time_in_verify_ms;
237
+ const totalDurationMs = pending.totalTimer.ms();
238
+
239
+ const ivcResult: IVCProofVerificationResult = { valid, durationMs, totalDurationMs };
240
+
241
+ if (!valid) {
242
+ this.logger.warn(`Proof verification failed for request_id=${result.request_id}: ${result.error_message}`);
243
+ } else {
244
+ this.logger.debug(`Proof verified`, {
245
+ requestId: result.request_id,
246
+ durationMs: Math.ceil(durationMs),
247
+ totalDurationMs: Math.ceil(totalDurationMs),
248
+ });
249
+ }
250
+
251
+ pending.resolve(ivcResult);
252
+ }
253
+
254
+ private registerExitCleanup(): void {
255
+ // Signal handlers must be synchronous — unlinkSync is intentional here
256
+ this.exitCleanup = () => {
257
+ try {
258
+ unlinkSync(this.fifoPath);
259
+ } catch {
260
+ /* ignore */
261
+ }
262
+ };
263
+ process.on('exit', this.exitCleanup);
264
+ process.on('SIGINT', this.exitCleanup);
265
+ process.on('SIGTERM', this.exitCleanup);
266
+ }
267
+
268
+ private deregisterExitCleanup(): void {
269
+ if (this.exitCleanup) {
270
+ process.removeListener('exit', this.exitCleanup);
271
+ process.removeListener('SIGINT', this.exitCleanup);
272
+ process.removeListener('SIGTERM', this.exitCleanup);
273
+ this.exitCleanup = null;
274
+ }
275
+ }
276
+ }
@@ -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';
@@ -10,13 +10,12 @@ import {
10
10
  type ObservableGauge,
11
11
  type TelemetryClient,
12
12
  type UpDownCounter,
13
+ createUpDownCounterWithDefault,
13
14
  getTelemetryClient,
14
15
  } from '@aztec/telemetry-client';
15
16
 
16
17
  import { createHistogram } from 'node:perf_hooks';
17
18
 
18
- import type { BBConfig } from '../config.js';
19
-
20
19
  class IVCVerifierMetrics {
21
20
  private ivcVerificationHistogram: Histogram;
22
21
  private ivcTotalVerificationHistogram: Histogram;
@@ -39,7 +38,7 @@ class IVCVerifierMetrics {
39
38
 
40
39
  this.ivcTotalVerificationHistogram = meter.createHistogram(Metrics.IVC_VERIFIER_TOTAL_TIME);
41
40
 
42
- this.ivcFailureCount = meter.createUpDownCounter(Metrics.IVC_VERIFIER_FAILURE_COUNT);
41
+ this.ivcFailureCount = createUpDownCounterWithDefault(meter, Metrics.IVC_VERIFIER_FAILURE_COUNT);
43
42
 
44
43
  this.aggDurationMetrics = {
45
44
  avg: meter.createObservableGauge(Metrics.IVC_VERIFIER_AGG_DURATION_AVG),
@@ -85,15 +84,15 @@ export class QueuedIVCVerifier implements ClientProtocolCircuitVerifier {
85
84
  private metrics: IVCVerifierMetrics;
86
85
 
87
86
  public constructor(
88
- config: BBConfig,
89
87
  private verifier: ClientProtocolCircuitVerifier,
88
+ concurrency: number,
90
89
  private telemetry: TelemetryClient = getTelemetryClient(),
91
90
  private logger = createLogger('bb-prover:queued_chonk_verifier'),
92
91
  ) {
93
92
  this.metrics = new IVCVerifierMetrics(this.telemetry, 'QueuedIVCVerifier');
94
93
  this.queue = new SerialQueue();
95
- this.logger.info(`Starting QueuedIVCVerifier with ${config.numConcurrentIVCVerifiers} concurrent verifiers`);
96
- this.queue.start(config.numConcurrentIVCVerifiers);
94
+ this.logger.info(`Starting QueuedIVCVerifier with ${concurrency} concurrent verifiers`);
95
+ this.queue.start(concurrency);
97
96
  }
98
97
 
99
98
  public async verifyProof(tx: Tx): Promise<IVCProofVerificationResult> {
@@ -102,7 +101,8 @@ export class QueuedIVCVerifier implements ClientProtocolCircuitVerifier {
102
101
  return result;
103
102
  }
104
103
 
105
- stop(): Promise<void> {
106
- return this.queue.end();
104
+ async stop(): Promise<void> {
105
+ await this.queue.end();
106
+ await this.verifier.stop();
107
107
  }
108
108
  }