@aztec/bb-prover 0.0.1-commit.03f7ef2 → 0.0.1-commit.04d373f
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 -107
- 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 +21 -43
- package/dest/prover/client/bb_private_kernel_prover.d.ts +18 -4
- package/dest/prover/client/bb_private_kernel_prover.d.ts.map +1 -1
- package/dest/prover/client/bb_private_kernel_prover.js +54 -8
- package/dest/prover/client/bundle.d.ts +3 -3
- package/dest/prover/client/bundle.d.ts.map +1 -1
- package/dest/prover/client/bundle.js +2 -3
- package/dest/prover/client/lazy.d.ts +3 -3
- package/dest/prover/client/lazy.d.ts.map +1 -1
- package/dest/prover/client/lazy.js +2 -3
- 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 +8 -12
- package/dest/prover/server/bb_prover.d.ts.map +1 -1
- package/dest/prover/server/bb_prover.js +612 -107
- package/dest/test/delay_values.d.ts +1 -1
- package/dest/test/delay_values.d.ts.map +1 -1
- package/dest/test/delay_values.js +27 -25
- package/dest/test/index.d.ts +2 -1
- package/dest/test/index.d.ts.map +1 -1
- package/dest/test/index.js +1 -0
- package/dest/test/test_circuit_prover.d.ts +4 -4
- package/dest/test/test_circuit_prover.d.ts.map +1 -1
- package/dest/test/test_circuit_prover.js +462 -59
- package/dest/verification_key/verification_key_data.d.ts +1 -8
- package/dest/verification_key/verification_key_data.d.ts.map +1 -1
- package/dest/verification_key/verification_key_data.js +1 -20
- package/dest/verifier/batch_chonk_verifier.d.ts +45 -0
- package/dest/verifier/batch_chonk_verifier.d.ts.map +1 -0
- package/dest/verifier/batch_chonk_verifier.js +232 -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 -48
- 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 +15 -45
- package/package.json +20 -18
- package/src/avm_proving_tests/avm_proving_tester.ts +53 -136
- 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 +20 -43
- package/src/prover/client/bb_private_kernel_prover.ts +135 -9
- package/src/prover/client/bundle.ts +3 -4
- package/src/prover/client/lazy.ts +3 -4
- package/src/prover/proof_utils.ts +41 -1
- package/src/prover/server/bb_prover.ts +138 -153
- package/src/test/delay_values.ts +28 -24
- package/src/test/index.ts +1 -0
- package/src/test/test_circuit_prover.ts +10 -13
- package/src/verification_key/verification_key_data.ts +1 -26
- package/src/verifier/batch_chonk_verifier.ts +276 -0
- package/src/verifier/bb_verifier.ts +66 -80
- package/src/verifier/index.ts +1 -0
- package/src/verifier/queued_chonk_verifier.ts +15 -47
- package/dest/bb/execute.d.ts +0 -107
- package/dest/bb/execute.d.ts.map +0 -1
- package/dest/bb/execute.js +0 -676
- package/src/bb/execute.ts +0 -706
|
@@ -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,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,91 +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.bbBinaryPath,
|
|
119
|
-
proofPath,
|
|
120
|
-
verificationKeyPath,
|
|
121
|
-
logFunction,
|
|
122
|
-
this.config.bbIVCConcurrency,
|
|
123
|
-
);
|
|
124
|
-
verificationDuration = timer.ms();
|
|
125
|
-
|
|
126
|
-
if (result.status === BB_RESULT.FAILURE) {
|
|
127
|
-
const errorMessage = `Failed to verify ${proofType} proof for ${circuit}!`;
|
|
128
|
-
throw new Error(errorMessage);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
this.logger.debug(`${proofType} verification successful`, {
|
|
132
|
-
circuitName: mapProtocolArtifactNameToCircuitName(circuit),
|
|
133
|
-
duration: result.durationMs,
|
|
134
|
-
eventName: 'circuit-verification',
|
|
135
|
-
proofType: 'chonk',
|
|
136
|
-
} satisfies CircuitVerificationStats);
|
|
137
|
-
};
|
|
138
|
-
await runInDirectory(this.config.bbWorkingDirectory, operation, this.config.bbSkipCleanup, this.logger);
|
|
139
|
-
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() };
|
|
140
117
|
} catch (err) {
|
|
141
118
|
this.logger.warn(`Failed to verify ${proofType} proof for tx ${tx.getTxHash().toString()}: ${String(err)}`);
|
|
142
119
|
return { valid: false, durationMs: 0, totalDurationMs: 0 };
|
|
143
120
|
}
|
|
144
121
|
}
|
|
145
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
|
@@ -10,14 +10,12 @@ import {
|
|
|
10
10
|
type ObservableGauge,
|
|
11
11
|
type TelemetryClient,
|
|
12
12
|
type UpDownCounter,
|
|
13
|
-
|
|
13
|
+
createUpDownCounterWithDefault,
|
|
14
14
|
getTelemetryClient,
|
|
15
15
|
} from '@aztec/telemetry-client';
|
|
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;
|
|
@@ -36,49 +34,18 @@ class IVCVerifierMetrics {
|
|
|
36
34
|
constructor(client: TelemetryClient, name = 'QueuedIVCVerifier') {
|
|
37
35
|
const meter = client.getMeter(name);
|
|
38
36
|
|
|
39
|
-
this.ivcVerificationHistogram = meter.createHistogram(Metrics.IVC_VERIFIER_TIME
|
|
40
|
-
unit: 'ms',
|
|
41
|
-
description: 'Duration to verify chonk proofs',
|
|
42
|
-
valueType: ValueType.INT,
|
|
43
|
-
});
|
|
37
|
+
this.ivcVerificationHistogram = meter.createHistogram(Metrics.IVC_VERIFIER_TIME);
|
|
44
38
|
|
|
45
|
-
this.ivcTotalVerificationHistogram = meter.createHistogram(Metrics.IVC_VERIFIER_TOTAL_TIME
|
|
46
|
-
unit: 'ms',
|
|
47
|
-
description: 'Total duration to verify chonk proofs, including serde',
|
|
48
|
-
valueType: ValueType.INT,
|
|
49
|
-
});
|
|
39
|
+
this.ivcTotalVerificationHistogram = meter.createHistogram(Metrics.IVC_VERIFIER_TOTAL_TIME);
|
|
50
40
|
|
|
51
|
-
this.ivcFailureCount = meter
|
|
52
|
-
description: 'Count of failed IVC proof verifications',
|
|
53
|
-
valueType: ValueType.INT,
|
|
54
|
-
});
|
|
41
|
+
this.ivcFailureCount = createUpDownCounterWithDefault(meter, Metrics.IVC_VERIFIER_FAILURE_COUNT);
|
|
55
42
|
|
|
56
43
|
this.aggDurationMetrics = {
|
|
57
|
-
avg: meter.createObservableGauge(Metrics.IVC_VERIFIER_AGG_DURATION_AVG,
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
max: meter.createObservableGauge(Metrics.IVC_VERIFIER_AGG_DURATION_MAX, {
|
|
63
|
-
valueType: ValueType.DOUBLE,
|
|
64
|
-
description: 'MAX ivc verification',
|
|
65
|
-
unit: 'ms',
|
|
66
|
-
}),
|
|
67
|
-
min: meter.createObservableGauge(Metrics.IVC_VERIFIER_AGG_DURATION_MIN, {
|
|
68
|
-
valueType: ValueType.DOUBLE,
|
|
69
|
-
description: 'MIN ivc verification',
|
|
70
|
-
unit: 'ms',
|
|
71
|
-
}),
|
|
72
|
-
p50: meter.createObservableGauge(Metrics.IVC_VERIFIER_AGG_DURATION_P50, {
|
|
73
|
-
valueType: ValueType.DOUBLE,
|
|
74
|
-
description: 'P50 ivc verification',
|
|
75
|
-
unit: 'ms',
|
|
76
|
-
}),
|
|
77
|
-
p90: meter.createObservableGauge(Metrics.IVC_VERIFIER_AGG_DURATION_P90, {
|
|
78
|
-
valueType: ValueType.DOUBLE,
|
|
79
|
-
description: 'P90 ivc verification',
|
|
80
|
-
unit: 'ms',
|
|
81
|
-
}),
|
|
44
|
+
avg: meter.createObservableGauge(Metrics.IVC_VERIFIER_AGG_DURATION_AVG),
|
|
45
|
+
max: meter.createObservableGauge(Metrics.IVC_VERIFIER_AGG_DURATION_MAX),
|
|
46
|
+
min: meter.createObservableGauge(Metrics.IVC_VERIFIER_AGG_DURATION_MIN),
|
|
47
|
+
p50: meter.createObservableGauge(Metrics.IVC_VERIFIER_AGG_DURATION_P50),
|
|
48
|
+
p90: meter.createObservableGauge(Metrics.IVC_VERIFIER_AGG_DURATION_P90),
|
|
82
49
|
};
|
|
83
50
|
|
|
84
51
|
meter.addBatchObservableCallback(this.aggregate, Object.values(this.aggDurationMetrics));
|
|
@@ -117,15 +84,15 @@ export class QueuedIVCVerifier implements ClientProtocolCircuitVerifier {
|
|
|
117
84
|
private metrics: IVCVerifierMetrics;
|
|
118
85
|
|
|
119
86
|
public constructor(
|
|
120
|
-
config: BBConfig,
|
|
121
87
|
private verifier: ClientProtocolCircuitVerifier,
|
|
88
|
+
concurrency: number,
|
|
122
89
|
private telemetry: TelemetryClient = getTelemetryClient(),
|
|
123
90
|
private logger = createLogger('bb-prover:queued_chonk_verifier'),
|
|
124
91
|
) {
|
|
125
92
|
this.metrics = new IVCVerifierMetrics(this.telemetry, 'QueuedIVCVerifier');
|
|
126
93
|
this.queue = new SerialQueue();
|
|
127
|
-
this.logger.info(`Starting QueuedIVCVerifier with ${
|
|
128
|
-
this.queue.start(
|
|
94
|
+
this.logger.info(`Starting QueuedIVCVerifier with ${concurrency} concurrent verifiers`);
|
|
95
|
+
this.queue.start(concurrency);
|
|
129
96
|
}
|
|
130
97
|
|
|
131
98
|
public async verifyProof(tx: Tx): Promise<IVCProofVerificationResult> {
|
|
@@ -134,7 +101,8 @@ export class QueuedIVCVerifier implements ClientProtocolCircuitVerifier {
|
|
|
134
101
|
return result;
|
|
135
102
|
}
|
|
136
103
|
|
|
137
|
-
stop(): Promise<void> {
|
|
138
|
-
|
|
104
|
+
async stop(): Promise<void> {
|
|
105
|
+
await this.queue.end();
|
|
106
|
+
await this.verifier.stop();
|
|
139
107
|
}
|
|
140
108
|
}
|