@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.
- package/dest/avm_proving_tests/avm_proving_tester.d.ts +13 -8
- package/dest/avm_proving_tests/avm_proving_tester.d.ts.map +1 -1
- package/dest/avm_proving_tests/avm_proving_tester.js +152 -110
- package/dest/bb/bb_js_backend.d.ts +196 -0
- package/dest/bb/bb_js_backend.d.ts.map +1 -0
- package/dest/bb/bb_js_backend.js +379 -0
- package/dest/bb/bb_js_debug.d.ts +52 -0
- package/dest/bb/bb_js_debug.d.ts.map +1 -0
- package/dest/bb/bb_js_debug.js +176 -0
- package/dest/bb/file_names.d.ts +4 -0
- package/dest/bb/file_names.d.ts.map +1 -0
- package/dest/bb/file_names.js +5 -0
- package/dest/config.d.ts +17 -1
- package/dest/config.d.ts.map +1 -1
- package/dest/index.d.ts +3 -2
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +2 -1
- package/dest/instrumentation.d.ts +1 -1
- package/dest/instrumentation.d.ts.map +1 -1
- package/dest/instrumentation.js +12 -4
- package/dest/prover/client/bb_private_kernel_prover.d.ts +10 -2
- package/dest/prover/client/bb_private_kernel_prover.d.ts.map +1 -1
- package/dest/prover/client/bb_private_kernel_prover.js +39 -5
- package/dest/prover/proof_utils.d.ts +11 -1
- package/dest/prover/proof_utils.d.ts.map +1 -1
- package/dest/prover/proof_utils.js +24 -1
- package/dest/prover/server/bb_prover.d.ts +4 -5
- package/dest/prover/server/bb_prover.d.ts.map +1 -1
- package/dest/prover/server/bb_prover.js +207 -78
- package/dest/verification_key/verification_key_data.js +1 -1
- package/dest/verifier/batch_chonk_verifier.d.ts +56 -0
- package/dest/verifier/batch_chonk_verifier.d.ts.map +1 -0
- package/dest/verifier/batch_chonk_verifier.js +384 -0
- package/dest/verifier/bb_verifier.d.ts +4 -1
- package/dest/verifier/bb_verifier.d.ts.map +1 -1
- package/dest/verifier/bb_verifier.js +134 -45
- package/dest/verifier/index.d.ts +2 -1
- package/dest/verifier/index.d.ts.map +1 -1
- package/dest/verifier/index.js +1 -0
- package/dest/verifier/queued_chonk_verifier.d.ts +2 -3
- package/dest/verifier/queued_chonk_verifier.d.ts.map +1 -1
- package/dest/verifier/queued_chonk_verifier.js +6 -5
- package/package.json +19 -17
- package/src/avm_proving_tests/avm_proving_tester.ts +53 -126
- package/src/bb/bb_js_backend.ts +435 -0
- package/src/bb/bb_js_debug.ts +227 -0
- package/src/bb/file_names.ts +6 -0
- package/src/config.ts +16 -0
- package/src/index.ts +2 -1
- package/src/instrumentation.ts +12 -4
- package/src/prover/client/bb_private_kernel_prover.ts +116 -4
- package/src/prover/proof_utils.ts +41 -1
- package/src/prover/server/bb_prover.ts +132 -137
- package/src/verification_key/verification_key_data.ts +1 -1
- package/src/verifier/batch_chonk_verifier.ts +415 -0
- package/src/verifier/bb_verifier.ts +66 -76
- package/src/verifier/index.ts +1 -0
- package/src/verifier/queued_chonk_verifier.ts +6 -7
- package/dest/bb/execute.d.ts +0 -107
- package/dest/bb/execute.d.ts.map +0 -1
- package/dest/bb/execute.js +0 -647
- package/src/bb/execute.ts +0 -678
|
@@ -0,0 +1,415 @@
|
|
|
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 { rmSync } from 'node:fs';
|
|
13
|
+
import { mkdtemp, rm } 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
|
+
const RESULT_TIMEOUT_MS = 5 * 60 * 1000;
|
|
22
|
+
const STOP_DRAIN_TIMEOUT_MS = 5_000;
|
|
23
|
+
|
|
24
|
+
/** Result from the FIFO, matching the C++ VerifyResult struct. */
|
|
25
|
+
interface FifoVerifyResult {
|
|
26
|
+
request_id: number;
|
|
27
|
+
status: number;
|
|
28
|
+
error_message: string;
|
|
29
|
+
time_in_verify_ms: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Maps client protocol artifacts used for chonk verification to VK indices. */
|
|
33
|
+
const CHONK_VK_ARTIFACTS = ['HidingKernelToRollup', 'HidingKernelToPublic'] as const;
|
|
34
|
+
|
|
35
|
+
interface PendingRequest {
|
|
36
|
+
resolve: (result: IVCProofVerificationResult) => void;
|
|
37
|
+
reject: (error: Error) => void;
|
|
38
|
+
totalTimer: Timer;
|
|
39
|
+
timeout: ReturnType<typeof setTimeout>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Batch verifier for Chonk IVC proofs. Uses the bb batch verifier service
|
|
44
|
+
* which batches IPA verification into a constant number of SRS MSMs for better throughput.
|
|
45
|
+
*
|
|
46
|
+
* Architecture:
|
|
47
|
+
* - Spawns a persistent `bb msgpack run` process via Barretenberg (native backend)
|
|
48
|
+
* - Sends proofs via the msgpack RPC protocol (ChonkBatchVerifierQueue)
|
|
49
|
+
* - Receives results via a named FIFO pipe (async, out-of-order)
|
|
50
|
+
* - Bisects batch failures to isolate individual bad proofs
|
|
51
|
+
*/
|
|
52
|
+
export class BatchChonkVerifier implements ClientProtocolCircuitVerifier {
|
|
53
|
+
private bb!: Barretenberg;
|
|
54
|
+
private fifoDir: string | undefined;
|
|
55
|
+
private fifoPath = '';
|
|
56
|
+
private nextRequestId = 0;
|
|
57
|
+
private pendingRequests = new Map<number, PendingRequest>();
|
|
58
|
+
private sendQueue: SerialQueue;
|
|
59
|
+
private fifoReader: FifoFrameReader;
|
|
60
|
+
private logger = createLogger('bb-prover:batch_chonk_verifier');
|
|
61
|
+
/** Maps artifact name to VK index in the batch verifier. */
|
|
62
|
+
private vkIndexMap = new Map<string, number>();
|
|
63
|
+
/** Bound cleanup handler for process exit signals. */
|
|
64
|
+
private exitCleanup: (() => void) | null = null;
|
|
65
|
+
private stopped = false;
|
|
66
|
+
private fatalError: Error | undefined;
|
|
67
|
+
private pendingDrainedResolvers = new Set<() => void>();
|
|
68
|
+
|
|
69
|
+
private constructor(
|
|
70
|
+
private config: Pick<BBConfig, 'bbChonkVerifyConcurrency'> & Partial<Pick<BBConfig, 'bbBinaryPath'>>,
|
|
71
|
+
private vkBuffers: Uint8Array[],
|
|
72
|
+
private batchSize: number,
|
|
73
|
+
private label: string,
|
|
74
|
+
) {
|
|
75
|
+
this.fifoReader = new FifoFrameReader();
|
|
76
|
+
this.sendQueue = new SerialQueue();
|
|
77
|
+
this.sendQueue.start(1);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Create and start a BatchChonkVerifier using the protocol circuit VKs. */
|
|
81
|
+
static async new(config: BBConfig, batchSize: number, label: string): Promise<BatchChonkVerifier> {
|
|
82
|
+
const vkBuffers: Uint8Array[] = [];
|
|
83
|
+
const vkIndexMap = new Map<string, number>();
|
|
84
|
+
for (const artifact of CHONK_VK_ARTIFACTS) {
|
|
85
|
+
const vk = ProtocolCircuitVks[artifact];
|
|
86
|
+
if (!vk) {
|
|
87
|
+
throw new Error(`Missing VK for ${artifact}`);
|
|
88
|
+
}
|
|
89
|
+
vkIndexMap.set(artifact, vkBuffers.length);
|
|
90
|
+
vkBuffers.push(vk.keyAsBytes);
|
|
91
|
+
}
|
|
92
|
+
const verifier = new BatchChonkVerifier(config, vkBuffers, batchSize, label);
|
|
93
|
+
verifier.vkIndexMap = vkIndexMap;
|
|
94
|
+
await verifier.start();
|
|
95
|
+
return verifier;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Create and start a BatchChonkVerifier with custom VKs (for testing). */
|
|
99
|
+
static async newForTesting(
|
|
100
|
+
config: Pick<BBConfig, 'bbChonkVerifyConcurrency'> & Partial<Pick<BBConfig, 'bbBinaryPath'>>,
|
|
101
|
+
vks: Uint8Array[],
|
|
102
|
+
batchSize: number,
|
|
103
|
+
): Promise<BatchChonkVerifier> {
|
|
104
|
+
const verifier = new BatchChonkVerifier(config, vks, batchSize, 'test');
|
|
105
|
+
for (let i = 0; i < vks.length; i++) {
|
|
106
|
+
verifier.vkIndexMap.set(String(i), i);
|
|
107
|
+
}
|
|
108
|
+
await verifier.start();
|
|
109
|
+
return verifier;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
private async start(): Promise<void> {
|
|
113
|
+
this.logger.info('Starting BatchChonkVerifier');
|
|
114
|
+
|
|
115
|
+
try {
|
|
116
|
+
this.bb = await Barretenberg.new({
|
|
117
|
+
bbPath: this.config.bbBinaryPath,
|
|
118
|
+
backend: BackendType.NativeUnixSocket,
|
|
119
|
+
});
|
|
120
|
+
await this.bb.initSRSChonk();
|
|
121
|
+
|
|
122
|
+
// Keep the FIFO in a private directory so cleanup has a single owner.
|
|
123
|
+
this.fifoDir = await mkdtemp(path.join(os.tmpdir(), `bb-batch-${this.label}-${process.pid}-`));
|
|
124
|
+
this.fifoPath = path.join(this.fifoDir, 'results.fifo');
|
|
125
|
+
await execFileAsync('mkfifo', [this.fifoPath]);
|
|
126
|
+
this.registerExitCleanup();
|
|
127
|
+
this.startFifoReader();
|
|
128
|
+
|
|
129
|
+
await this.bb.chonkBatchVerifierStart({
|
|
130
|
+
vks: this.vkBuffers,
|
|
131
|
+
numCores: this.config.bbChonkVerifyConcurrency || 0,
|
|
132
|
+
batchSize: this.batchSize,
|
|
133
|
+
fifoPath: this.fifoPath,
|
|
134
|
+
});
|
|
135
|
+
} catch (err) {
|
|
136
|
+
this.fifoReader.stop();
|
|
137
|
+
this.deregisterExitCleanup();
|
|
138
|
+
await this.cleanupFifo();
|
|
139
|
+
await this.bb?.destroy().catch(() => {});
|
|
140
|
+
throw err;
|
|
141
|
+
}
|
|
142
|
+
this.logger.info('BatchChonkVerifier started', { fifoPath: this.fifoPath });
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
public verifyProof(tx: Tx): Promise<IVCProofVerificationResult> {
|
|
146
|
+
const totalTimer = new Timer();
|
|
147
|
+
return (async () => {
|
|
148
|
+
const circuit = tx.data.forPublic ? 'HidingKernelToPublic' : 'HidingKernelToRollup';
|
|
149
|
+
const vkIndex = this.vkIndexMap.get(circuit);
|
|
150
|
+
if (vkIndex === undefined) {
|
|
151
|
+
throw new Error(`No VK index for circuit ${circuit}`);
|
|
152
|
+
}
|
|
153
|
+
const proofWithPubInputs = tx.chonkProof.attachPublicInputs(tx.data.publicInputs().toFields());
|
|
154
|
+
const proofFields = proofWithPubInputs.fieldsWithPublicInputs.map(f => f.toBuffer());
|
|
155
|
+
return await this.enqueueProof(vkIndex, proofFields);
|
|
156
|
+
})().catch(err => {
|
|
157
|
+
this.logger.warn(`Failed to verify Chonk proof for tx ${tx.getTxHash().toString()}: ${String(err)}`);
|
|
158
|
+
return { valid: false, durationMs: 0, totalDurationMs: totalTimer.ms() };
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Enqueue raw proof fields for verification. Used directly by tests with custom VKs. */
|
|
163
|
+
public enqueueProof(vkIndex: number, proofFields: Uint8Array[]): Promise<IVCProofVerificationResult> {
|
|
164
|
+
if (this.stopped) {
|
|
165
|
+
return Promise.reject(new Error('BatchChonkVerifier stopped'));
|
|
166
|
+
}
|
|
167
|
+
if (this.fatalError) {
|
|
168
|
+
return Promise.reject(this.fatalError);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const totalTimer = new Timer();
|
|
172
|
+
const requestId = this.nextRequestId++;
|
|
173
|
+
|
|
174
|
+
const resultPromise = new Promise<IVCProofVerificationResult>((resolve, reject) => {
|
|
175
|
+
const timeout = setTimeout(() => {
|
|
176
|
+
const pending = this.pendingRequests.get(requestId);
|
|
177
|
+
if (!pending) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
this.pendingRequests.delete(requestId);
|
|
181
|
+
pending.reject(new Error(`BatchChonkVerifier result timed out for request_id=${requestId}`));
|
|
182
|
+
this.notifyPendingDrained();
|
|
183
|
+
}, RESULT_TIMEOUT_MS);
|
|
184
|
+
// A pending result timer must never keep the host process alive on its own (e.g. an
|
|
185
|
+
// orphaned request at process exit); the FIFO reader keeps the loop alive while results
|
|
186
|
+
// are genuinely awaited.
|
|
187
|
+
timeout.unref();
|
|
188
|
+
this.pendingRequests.set(requestId, { resolve, reject, totalTimer, timeout });
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
void this.sendQueue
|
|
192
|
+
.put(async () => {
|
|
193
|
+
if (this.fatalError) {
|
|
194
|
+
throw this.fatalError;
|
|
195
|
+
}
|
|
196
|
+
await this.bb.chonkBatchVerifierQueue({
|
|
197
|
+
requestId,
|
|
198
|
+
vkIndex,
|
|
199
|
+
proofFields,
|
|
200
|
+
});
|
|
201
|
+
})
|
|
202
|
+
.catch(err => {
|
|
203
|
+
const pending = this.pendingRequests.get(requestId);
|
|
204
|
+
if (pending) {
|
|
205
|
+
this.pendingRequests.delete(requestId);
|
|
206
|
+
clearTimeout(pending.timeout);
|
|
207
|
+
pending.reject(err instanceof Error ? err : new Error(String(err)));
|
|
208
|
+
this.notifyPendingDrained();
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
return resultPromise;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
public async stop(): Promise<void> {
|
|
216
|
+
this.logger.info('Stopping BatchChonkVerifier');
|
|
217
|
+
this.stopped = true;
|
|
218
|
+
|
|
219
|
+
try {
|
|
220
|
+
// Stop accepting new proofs and flush the send queue. Bound it so an unresponsive
|
|
221
|
+
// native process can't block teardown of our own event-loop handles indefinitely.
|
|
222
|
+
await this.withTimeout(this.sendQueue.end(), STOP_DRAIN_TIMEOUT_MS, 'send queue flush');
|
|
223
|
+
|
|
224
|
+
// Stop the bb service (flushes remaining proofs).
|
|
225
|
+
await this.withTimeout(this.bb.chonkBatchVerifierStop({}), STOP_DRAIN_TIMEOUT_MS, 'chonkBatchVerifierStop');
|
|
226
|
+
|
|
227
|
+
// Native stop flushes callbacks; keep the FIFO open until those frames are observed.
|
|
228
|
+
const drained = await this.waitForPendingRequestsToDrain(STOP_DRAIN_TIMEOUT_MS);
|
|
229
|
+
if (!drained) {
|
|
230
|
+
this.rejectPendingRequests(new Error('Timed out waiting for BatchChonkVerifier results during stop'));
|
|
231
|
+
}
|
|
232
|
+
} catch (err) {
|
|
233
|
+
this.logger.warn(`Error during BatchChonkVerifier graceful stop: ${err}`);
|
|
234
|
+
this.rejectPendingRequests(err instanceof Error ? err : new Error(String(err)));
|
|
235
|
+
} finally {
|
|
236
|
+
// Always release our own event-loop handles — the FIFO read stream (a blocking
|
|
237
|
+
// threadpool read that can't be unref'd), the unix socket, the native process, and the
|
|
238
|
+
// exit handler — so the host process exits cleanly even if the native backend is wedged.
|
|
239
|
+
this.fifoReader.stop();
|
|
240
|
+
this.deregisterExitCleanup();
|
|
241
|
+
await this.cleanupFifo();
|
|
242
|
+
await this.bb.destroy().catch(err => this.logger.warn(`Error destroying bb backend during stop: ${err}`));
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
this.logger.info('BatchChonkVerifier stopped');
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Races a promise against a timeout so a wedged native backend can't block teardown. The
|
|
250
|
+
* timer is unref'd so it never keeps the process alive; the underlying promise stays handled
|
|
251
|
+
* by the race even if the timeout wins, so it cannot surface as an unhandled rejection.
|
|
252
|
+
*/
|
|
253
|
+
private async withTimeout<T>(promise: Promise<T>, timeoutMs: number, label: string): Promise<T | void> {
|
|
254
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
255
|
+
const timeout = new Promise<void>((_, reject) => {
|
|
256
|
+
timer = setTimeout(
|
|
257
|
+
() => reject(new Error(`BatchChonkVerifier ${label} timed out after ${timeoutMs}ms`)),
|
|
258
|
+
timeoutMs,
|
|
259
|
+
);
|
|
260
|
+
timer.unref();
|
|
261
|
+
});
|
|
262
|
+
try {
|
|
263
|
+
return await Promise.race([promise, timeout]);
|
|
264
|
+
} finally {
|
|
265
|
+
if (timer) {
|
|
266
|
+
clearTimeout(timer);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
private startFifoReader(): void {
|
|
272
|
+
const unpackr = new Unpackr({ useRecords: false });
|
|
273
|
+
|
|
274
|
+
this.fifoReader.on('frame', (payload: Buffer) => {
|
|
275
|
+
try {
|
|
276
|
+
const result = unpackr.unpack(payload) as FifoVerifyResult;
|
|
277
|
+
this.handleResult(result);
|
|
278
|
+
} catch (err) {
|
|
279
|
+
this.logger.error(`FIFO: failed to decode msgpack result: ${err}`);
|
|
280
|
+
// A corrupt result stream cannot safely be matched to outstanding requests.
|
|
281
|
+
this.failVerifier(err instanceof Error ? err : new Error(String(err)));
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
this.fifoReader.on('error', (err: Error) => {
|
|
286
|
+
this.logger.error(`FIFO reader error: ${err}`);
|
|
287
|
+
this.failVerifier(err);
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
this.fifoReader.on('end', () => {
|
|
291
|
+
this.logger.debug('FIFO reader: stream ended');
|
|
292
|
+
if (!this.stopped) {
|
|
293
|
+
this.failVerifier(new Error('FIFO stream ended unexpectedly'));
|
|
294
|
+
}
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
this.fifoReader.start(this.fifoPath);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
private handleResult(result: FifoVerifyResult): void {
|
|
301
|
+
const pending = this.pendingRequests.get(result.request_id);
|
|
302
|
+
if (!pending) {
|
|
303
|
+
this.logger.warn(`Received result for unknown request_id=${result.request_id}`);
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
this.pendingRequests.delete(result.request_id);
|
|
307
|
+
clearTimeout(pending.timeout);
|
|
308
|
+
|
|
309
|
+
const valid = result.status === 0; // VerifyStatus::OK
|
|
310
|
+
const durationMs = result.time_in_verify_ms;
|
|
311
|
+
const totalDurationMs = pending.totalTimer.ms();
|
|
312
|
+
|
|
313
|
+
const ivcResult: IVCProofVerificationResult = { valid, durationMs, totalDurationMs };
|
|
314
|
+
|
|
315
|
+
if (!valid) {
|
|
316
|
+
this.logger.warn(`Proof verification failed for request_id=${result.request_id}: ${result.error_message}`);
|
|
317
|
+
} else {
|
|
318
|
+
this.logger.debug(`Proof verified`, {
|
|
319
|
+
requestId: result.request_id,
|
|
320
|
+
durationMs: Math.ceil(durationMs),
|
|
321
|
+
totalDurationMs: Math.ceil(totalDurationMs),
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
pending.resolve(ivcResult);
|
|
326
|
+
this.notifyPendingDrained();
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
private registerExitCleanup(): void {
|
|
330
|
+
this.exitCleanup = () => {
|
|
331
|
+
this.cleanupFifoSync();
|
|
332
|
+
};
|
|
333
|
+
process.on('exit', this.exitCleanup);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
private deregisterExitCleanup(): void {
|
|
337
|
+
if (this.exitCleanup) {
|
|
338
|
+
process.removeListener('exit', this.exitCleanup);
|
|
339
|
+
this.exitCleanup = null;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
private rejectPendingRequests(error: Error): void {
|
|
344
|
+
for (const [id, pending] of Array.from(this.pendingRequests)) {
|
|
345
|
+
pending.reject(error);
|
|
346
|
+
clearTimeout(pending.timeout);
|
|
347
|
+
this.pendingRequests.delete(id);
|
|
348
|
+
}
|
|
349
|
+
this.notifyPendingDrained();
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
private failVerifier(error: Error): void {
|
|
353
|
+
if (!this.fatalError) {
|
|
354
|
+
this.fatalError = error;
|
|
355
|
+
}
|
|
356
|
+
this.rejectPendingRequests(error);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
private waitForPendingRequestsToDrain(timeoutMs: number): Promise<boolean> {
|
|
360
|
+
if (this.pendingRequests.size === 0) {
|
|
361
|
+
return Promise.resolve(true);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
let timeout: ReturnType<typeof setTimeout> | undefined;
|
|
365
|
+
let onDrain: (() => void) | undefined;
|
|
366
|
+
const drained = new Promise<boolean>(resolve => {
|
|
367
|
+
onDrain = () => resolve(true);
|
|
368
|
+
this.pendingDrainedResolvers.add(onDrain);
|
|
369
|
+
});
|
|
370
|
+
const timedOut = new Promise<boolean>(resolve => {
|
|
371
|
+
timeout = setTimeout(() => resolve(false), timeoutMs);
|
|
372
|
+
timeout.unref();
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
return Promise.race([drained, timedOut]).finally(() => {
|
|
376
|
+
if (timeout) {
|
|
377
|
+
clearTimeout(timeout);
|
|
378
|
+
}
|
|
379
|
+
if (onDrain) {
|
|
380
|
+
this.pendingDrainedResolvers.delete(onDrain);
|
|
381
|
+
}
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
private notifyPendingDrained(): void {
|
|
386
|
+
if (this.pendingRequests.size > 0) {
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
for (const resolve of Array.from(this.pendingDrainedResolvers)) {
|
|
390
|
+
resolve();
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
private async cleanupFifo(): Promise<void> {
|
|
395
|
+
if (this.fifoDir) {
|
|
396
|
+
await rm(this.fifoDir, { recursive: true, force: true }).catch(() => {});
|
|
397
|
+
} else if (this.fifoPath) {
|
|
398
|
+
await rm(this.fifoPath, { force: true }).catch(() => {});
|
|
399
|
+
}
|
|
400
|
+
this.fifoDir = undefined;
|
|
401
|
+
this.fifoPath = '';
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
private cleanupFifoSync(): void {
|
|
405
|
+
try {
|
|
406
|
+
if (this.fifoDir) {
|
|
407
|
+
rmSync(this.fifoDir, { recursive: true, force: true });
|
|
408
|
+
} else if (this.fifoPath) {
|
|
409
|
+
rmSync(this.fifoPath, { force: true });
|
|
410
|
+
}
|
|
411
|
+
} catch {
|
|
412
|
+
/* ignore */
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { runInDirectory } from '@aztec/foundation/fs';
|
|
2
1
|
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
3
2
|
import { Timer } from '@aztec/foundation/timer';
|
|
4
3
|
import { ProtocolCircuitVks } from '@aztec/noir-protocol-circuits-types/server/vks';
|
|
@@ -15,28 +14,29 @@ import { Tx } from '@aztec/stdlib/tx';
|
|
|
15
14
|
import type { VerificationKeyData } from '@aztec/stdlib/vks';
|
|
16
15
|
|
|
17
16
|
import { promises as fs } from 'fs';
|
|
18
|
-
import * as path from 'path';
|
|
19
17
|
|
|
20
|
-
import {
|
|
21
|
-
BB_RESULT,
|
|
22
|
-
PROOF_FILENAME,
|
|
23
|
-
PUBLIC_INPUTS_FILENAME,
|
|
24
|
-
VK_FILENAME,
|
|
25
|
-
verifyChonkProof,
|
|
26
|
-
verifyProof,
|
|
27
|
-
} from '../bb/execute.js';
|
|
18
|
+
import { BBJsFactory } from '../bb/bb_js_backend.js';
|
|
28
19
|
import type { BBConfig } from '../config.js';
|
|
29
20
|
import { getUltraHonkFlavorForCircuit } from '../honk.js';
|
|
30
|
-
import { writeChonkProofToPath } from '../prover/proof_utils.js';
|
|
31
21
|
|
|
32
22
|
export class BBCircuitVerifier implements ClientProtocolCircuitVerifier {
|
|
23
|
+
private bbJsFactory: BBJsFactory;
|
|
24
|
+
|
|
33
25
|
private constructor(
|
|
34
26
|
private config: BBConfig,
|
|
35
27
|
private logger: Logger,
|
|
36
|
-
) {
|
|
28
|
+
) {
|
|
29
|
+
// BB_NUM_IVC_VERIFIERS bounds the number of long-lived bb processes the pool keeps alive.
|
|
30
|
+
// If 0, fall back to spawning a fresh bb per verification.
|
|
31
|
+
this.bbJsFactory = new BBJsFactory(config.bbBinaryPath, {
|
|
32
|
+
poolSize: config.numConcurrentIVCVerifiers > 0 ? config.numConcurrentIVCVerifiers : undefined,
|
|
33
|
+
logger,
|
|
34
|
+
debugDir: config.bbDebugOutputDir,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
37
|
|
|
38
38
|
public stop(): Promise<void> {
|
|
39
|
-
return
|
|
39
|
+
return this.bbJsFactory.destroy();
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
public static async new(config: BBConfig, logger = createLogger('bb-prover:verifier')) {
|
|
@@ -55,87 +55,77 @@ export class BBCircuitVerifier implements ClientProtocolCircuitVerifier {
|
|
|
55
55
|
return vk;
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
+
/** Verify an UltraHonk proof via bb.js API (no temp files). */
|
|
58
59
|
public async verifyProofForCircuit(circuit: ServerProtocolArtifact, proof: Proof) {
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
const proofFileName = path.join(bbWorkingDirectory, PROOF_FILENAME);
|
|
62
|
-
const verificationKeyPath = path.join(bbWorkingDirectory, VK_FILENAME);
|
|
63
|
-
const verificationKey = this.getVerificationKeyData(circuit);
|
|
60
|
+
const verificationKey = this.getVerificationKeyData(circuit);
|
|
61
|
+
const flavor = getUltraHonkFlavorForCircuit(circuit);
|
|
64
62
|
|
|
65
|
-
|
|
63
|
+
this.logger.debug(`${circuit} Verifying with key: ${verificationKey.keyAsFields.hash.toString()}`);
|
|
66
64
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
await fs.writeFile(verificationKeyPath, verificationKey.keyAsBytes);
|
|
65
|
+
// Split proof buffer into public input fields and proof fields (32-byte each)
|
|
66
|
+
const publicInputFields = splitBufferToFieldArrays(proof.buffer.subarray(0, proof.numPublicInputs * 32));
|
|
67
|
+
const proofFields = splitBufferToFieldArrays(proof.buffer.subarray(proof.numPublicInputs * 32));
|
|
71
68
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
69
|
+
await using instance = await this.bbJsFactory.getInstance();
|
|
70
|
+
const { verified, durationMs } = await instance.verifyProof(
|
|
71
|
+
proofFields,
|
|
72
|
+
verificationKey.keyAsBytes,
|
|
73
|
+
publicInputFields,
|
|
74
|
+
flavor,
|
|
75
|
+
);
|
|
79
76
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}
|
|
77
|
+
if (!verified) {
|
|
78
|
+
throw new Error(`Failed to verify ${circuit} proof!`);
|
|
79
|
+
}
|
|
84
80
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
};
|
|
92
|
-
await runInDirectory(this.config.bbWorkingDirectory, operation, this.config.bbSkipCleanup, this.logger);
|
|
81
|
+
this.logger.debug(`${circuit} verification successful`, {
|
|
82
|
+
circuitName: mapProtocolArtifactNameToCircuitName(circuit),
|
|
83
|
+
duration: durationMs,
|
|
84
|
+
eventName: 'circuit-verification',
|
|
85
|
+
proofType: 'ultra-honk',
|
|
86
|
+
} satisfies CircuitVerificationStats);
|
|
93
87
|
}
|
|
94
88
|
|
|
89
|
+
/** Verify a Chonk (IVC) proof from a transaction via bb.js API. */
|
|
95
90
|
public async verifyProof(tx: Tx): Promise<IVCProofVerificationResult> {
|
|
96
91
|
const proofType = 'Chonk';
|
|
97
92
|
try {
|
|
98
93
|
const totalTimer = new Timer();
|
|
99
|
-
let verificationDuration = 0;
|
|
100
94
|
|
|
101
95
|
const circuit: ClientProtocolArtifact = tx.data.forPublic ? 'HidingKernelToPublic' : 'HidingKernelToRollup';
|
|
96
|
+
const verificationKey = this.getVerificationKeyData(circuit);
|
|
97
|
+
|
|
98
|
+
// Reconstruct the full proof with public inputs prepended, then convert Fr[] to Uint8Array[]
|
|
99
|
+
const proofWithPubInputs = tx.chonkProof.attachPublicInputs(tx.data.publicInputs().toFields());
|
|
100
|
+
const fieldsAsBuffers = proofWithPubInputs.fieldsWithPublicInputs.map(f => new Uint8Array(f.toBuffer()));
|
|
102
101
|
|
|
103
|
-
|
|
104
|
-
const
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
this.config.bbIVCConcurrency,
|
|
119
|
-
);
|
|
120
|
-
verificationDuration = timer.ms();
|
|
121
|
-
|
|
122
|
-
if (result.status === BB_RESULT.FAILURE) {
|
|
123
|
-
const errorMessage = `Failed to verify ${proofType} proof for ${circuit}!`;
|
|
124
|
-
throw new Error(errorMessage);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
this.logger.debug(`${proofType} verification successful`, {
|
|
128
|
-
circuitName: mapProtocolArtifactNameToCircuitName(circuit),
|
|
129
|
-
duration: result.durationMs,
|
|
130
|
-
eventName: 'circuit-verification',
|
|
131
|
-
proofType: 'chonk',
|
|
132
|
-
} satisfies CircuitVerificationStats);
|
|
133
|
-
};
|
|
134
|
-
await runInDirectory(this.config.bbWorkingDirectory, operation, this.config.bbSkipCleanup, this.logger);
|
|
135
|
-
return { valid: true, durationMs: verificationDuration, totalDurationMs: totalTimer.ms() };
|
|
102
|
+
await using instance = await this.bbJsFactory.getInstance();
|
|
103
|
+
const { verified, durationMs } = await instance.verifyChonkProof(fieldsAsBuffers, verificationKey.keyAsBytes);
|
|
104
|
+
|
|
105
|
+
if (!verified) {
|
|
106
|
+
throw new Error(`Failed to verify ${proofType} proof for ${circuit}!`);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
this.logger.debug(`${proofType} verification successful`, {
|
|
110
|
+
circuitName: mapProtocolArtifactNameToCircuitName(circuit),
|
|
111
|
+
duration: durationMs,
|
|
112
|
+
eventName: 'circuit-verification',
|
|
113
|
+
proofType: 'chonk',
|
|
114
|
+
} satisfies CircuitVerificationStats);
|
|
115
|
+
|
|
116
|
+
return { valid: true, durationMs, totalDurationMs: totalTimer.ms() };
|
|
136
117
|
} catch (err) {
|
|
137
118
|
this.logger.warn(`Failed to verify ${proofType} proof for tx ${tx.getTxHash().toString()}: ${String(err)}`);
|
|
138
119
|
return { valid: false, durationMs: 0, totalDurationMs: 0 };
|
|
139
120
|
}
|
|
140
121
|
}
|
|
141
122
|
}
|
|
123
|
+
|
|
124
|
+
/** Split a buffer into 32-byte Uint8Array field elements. */
|
|
125
|
+
function splitBufferToFieldArrays(buffer: Buffer): Uint8Array[] {
|
|
126
|
+
const fields: Uint8Array[] = [];
|
|
127
|
+
for (let i = 0; i < buffer.length; i += 32) {
|
|
128
|
+
fields.push(new Uint8Array(buffer.subarray(i, i + 32)));
|
|
129
|
+
}
|
|
130
|
+
return fields;
|
|
131
|
+
}
|
package/src/verifier/index.ts
CHANGED
|
@@ -16,8 +16,6 @@ import {
|
|
|
16
16
|
|
|
17
17
|
import { createHistogram } from 'node:perf_hooks';
|
|
18
18
|
|
|
19
|
-
import type { BBConfig } from '../config.js';
|
|
20
|
-
|
|
21
19
|
class IVCVerifierMetrics {
|
|
22
20
|
private ivcVerificationHistogram: Histogram;
|
|
23
21
|
private ivcTotalVerificationHistogram: Histogram;
|
|
@@ -86,15 +84,15 @@ export class QueuedIVCVerifier implements ClientProtocolCircuitVerifier {
|
|
|
86
84
|
private metrics: IVCVerifierMetrics;
|
|
87
85
|
|
|
88
86
|
public constructor(
|
|
89
|
-
config: BBConfig,
|
|
90
87
|
private verifier: ClientProtocolCircuitVerifier,
|
|
88
|
+
concurrency: number,
|
|
91
89
|
private telemetry: TelemetryClient = getTelemetryClient(),
|
|
92
90
|
private logger = createLogger('bb-prover:queued_chonk_verifier'),
|
|
93
91
|
) {
|
|
94
92
|
this.metrics = new IVCVerifierMetrics(this.telemetry, 'QueuedIVCVerifier');
|
|
95
93
|
this.queue = new SerialQueue();
|
|
96
|
-
this.logger.info(`Starting QueuedIVCVerifier with ${
|
|
97
|
-
this.queue.start(
|
|
94
|
+
this.logger.info(`Starting QueuedIVCVerifier with ${concurrency} concurrent verifiers`);
|
|
95
|
+
this.queue.start(concurrency);
|
|
98
96
|
}
|
|
99
97
|
|
|
100
98
|
public async verifyProof(tx: Tx): Promise<IVCProofVerificationResult> {
|
|
@@ -103,7 +101,8 @@ export class QueuedIVCVerifier implements ClientProtocolCircuitVerifier {
|
|
|
103
101
|
return result;
|
|
104
102
|
}
|
|
105
103
|
|
|
106
|
-
stop(): Promise<void> {
|
|
107
|
-
|
|
104
|
+
async stop(): Promise<void> {
|
|
105
|
+
await this.queue.end();
|
|
106
|
+
await this.verifier.stop();
|
|
108
107
|
}
|
|
109
108
|
}
|