@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,435 @@
|
|
|
1
|
+
import { type AvmStat, type BackendOptions, BackendType, Barretenberg } from '@aztec/bb.js';
|
|
2
|
+
import type { LogFn, Logger } from '@aztec/foundation/log';
|
|
3
|
+
import { FifoMemoryQueue } from '@aztec/foundation/queue';
|
|
4
|
+
import { Timer } from '@aztec/foundation/timer';
|
|
5
|
+
|
|
6
|
+
import type { UltraHonkFlavor } from '../honk.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Maps UltraHonkFlavor to the bb.js ProofSystemSettings.
|
|
10
|
+
* All server-side proofs use disableZk: true.
|
|
11
|
+
*/
|
|
12
|
+
function getProofSettings(flavor: UltraHonkFlavor) {
|
|
13
|
+
const base = { disableZk: true, optimizedSolidityVerifier: false };
|
|
14
|
+
switch (flavor) {
|
|
15
|
+
case 'ultra_honk':
|
|
16
|
+
return { ...base, oracleHashType: 'poseidon2' as const, ipaAccumulation: false };
|
|
17
|
+
case 'ultra_keccak_honk':
|
|
18
|
+
return { ...base, oracleHashType: 'keccak' as const, ipaAccumulation: false };
|
|
19
|
+
case 'ultra_starknet_honk':
|
|
20
|
+
return { ...base, oracleHashType: 'starknet' as const, ipaAccumulation: false };
|
|
21
|
+
case 'ultra_rollup_honk':
|
|
22
|
+
return { ...base, oracleHashType: 'poseidon2' as const, ipaAccumulation: true };
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Result of a successful proof generation via bb.js. */
|
|
27
|
+
export type BBJsProofResult = {
|
|
28
|
+
/** Proof fields as 32-byte Uint8Arrays. */
|
|
29
|
+
proofFields: Uint8Array[];
|
|
30
|
+
/** Public input fields as 32-byte Uint8Arrays. */
|
|
31
|
+
publicInputFields: Uint8Array[];
|
|
32
|
+
/** Duration of the proving operation in ms. */
|
|
33
|
+
durationMs: number;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/** Public API surface of a bb.js instance, used by the factory and debug wrapper. */
|
|
37
|
+
export interface BBJsApi {
|
|
38
|
+
generateProof(
|
|
39
|
+
circuitName: string,
|
|
40
|
+
bytecode: Uint8Array,
|
|
41
|
+
verificationKey: Uint8Array,
|
|
42
|
+
witness: Uint8Array,
|
|
43
|
+
flavor: UltraHonkFlavor,
|
|
44
|
+
): Promise<BBJsProofResult>;
|
|
45
|
+
verifyProof(
|
|
46
|
+
proofFields: Uint8Array[],
|
|
47
|
+
verificationKey: Uint8Array,
|
|
48
|
+
publicInputFields: Uint8Array[],
|
|
49
|
+
flavor: UltraHonkFlavor,
|
|
50
|
+
): Promise<{ verified: boolean; durationMs: number }>;
|
|
51
|
+
verifyChonkProof(
|
|
52
|
+
fieldsWithPublicInputs: Uint8Array[],
|
|
53
|
+
verificationKey: Uint8Array,
|
|
54
|
+
): Promise<{ verified: boolean; durationMs: number }>;
|
|
55
|
+
computeGateCount(
|
|
56
|
+
circuitName: string,
|
|
57
|
+
bytecode: Uint8Array,
|
|
58
|
+
flavor: UltraHonkFlavor | 'mega_honk',
|
|
59
|
+
): Promise<{ circuitSize: number; durationMs: number }>;
|
|
60
|
+
generateContract(verificationKey: Uint8Array): Promise<{ solidityCode: string; durationMs: number }>;
|
|
61
|
+
/** Generate an AVM proof from serialized inputs. Callers should call verifyAvmProof separately. */
|
|
62
|
+
generateAvmProof(inputs: Uint8Array): Promise<{ proof: Uint8Array[]; stats: AvmStat[]; durationMs: number }>;
|
|
63
|
+
/** Verify an AVM proof against serialized public inputs. */
|
|
64
|
+
verifyAvmProof(proof: Uint8Array[], publicInputs: Uint8Array): Promise<{ verified: boolean; durationMs: number }>;
|
|
65
|
+
/** Check the AVM circuit from serialized inputs. Returns pass/fail and per-stage timings. */
|
|
66
|
+
checkAvmCircuit(inputs: Uint8Array): Promise<{ passed: boolean; stats: AvmStat[]; durationMs: number }>;
|
|
67
|
+
destroy(): Promise<void>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Thin wrapper around a single Barretenberg instance.
|
|
72
|
+
* Each instance spawns its own bb process via the NativeUnixSocket backend.
|
|
73
|
+
*/
|
|
74
|
+
export class BBJsInstance implements BBJsApi {
|
|
75
|
+
private constructor(private api: Barretenberg) {}
|
|
76
|
+
|
|
77
|
+
/** Creates a new Barretenberg instance connected to a fresh bb process. */
|
|
78
|
+
static async create(bbPath: string, logger?: LogFn, threads?: number): Promise<BBJsInstance> {
|
|
79
|
+
const options: BackendOptions = {
|
|
80
|
+
bbPath,
|
|
81
|
+
backend: BackendType.NativeUnixSocket,
|
|
82
|
+
logger,
|
|
83
|
+
};
|
|
84
|
+
if (threads !== undefined) {
|
|
85
|
+
options.threads = threads;
|
|
86
|
+
}
|
|
87
|
+
const api = await Barretenberg.new(options);
|
|
88
|
+
return new BBJsInstance(api);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Generate an UltraHonk proof for a circuit.
|
|
93
|
+
* @param circuitName - Identifier for the circuit (used by bb internally).
|
|
94
|
+
* @param bytecode - Uncompressed ACIR bytecode.
|
|
95
|
+
* @param verificationKey - The circuit's verification key bytes.
|
|
96
|
+
* @param witness - Uncompressed witness bytes.
|
|
97
|
+
* @param flavor - The UltraHonk flavor to use.
|
|
98
|
+
*/
|
|
99
|
+
async generateProof(
|
|
100
|
+
circuitName: string,
|
|
101
|
+
bytecode: Uint8Array,
|
|
102
|
+
verificationKey: Uint8Array,
|
|
103
|
+
witness: Uint8Array,
|
|
104
|
+
flavor: UltraHonkFlavor,
|
|
105
|
+
): Promise<BBJsProofResult> {
|
|
106
|
+
const timer = new Timer();
|
|
107
|
+
const result = await this.api.circuitProve({
|
|
108
|
+
circuit: {
|
|
109
|
+
name: circuitName,
|
|
110
|
+
bytecode,
|
|
111
|
+
verificationKey,
|
|
112
|
+
},
|
|
113
|
+
witness,
|
|
114
|
+
settings: getProofSettings(flavor),
|
|
115
|
+
});
|
|
116
|
+
return {
|
|
117
|
+
proofFields: result.proof,
|
|
118
|
+
publicInputFields: result.publicInputs,
|
|
119
|
+
durationMs: timer.ms(),
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Verify an UltraHonk proof.
|
|
125
|
+
* @param proofFields - Proof fields as 32-byte Uint8Arrays.
|
|
126
|
+
* @param verificationKey - The VK bytes.
|
|
127
|
+
* @param publicInputFields - Public input fields as 32-byte Uint8Arrays.
|
|
128
|
+
* @param flavor - The UltraHonk flavor.
|
|
129
|
+
* @returns Whether the proof is valid.
|
|
130
|
+
*/
|
|
131
|
+
async verifyProof(
|
|
132
|
+
proofFields: Uint8Array[],
|
|
133
|
+
verificationKey: Uint8Array,
|
|
134
|
+
publicInputFields: Uint8Array[],
|
|
135
|
+
flavor: UltraHonkFlavor,
|
|
136
|
+
): Promise<{ verified: boolean; durationMs: number }> {
|
|
137
|
+
const timer = new Timer();
|
|
138
|
+
const result = await this.api.circuitVerify({
|
|
139
|
+
verificationKey,
|
|
140
|
+
publicInputs: publicInputFields,
|
|
141
|
+
proof: proofFields,
|
|
142
|
+
settings: getProofSettings(flavor),
|
|
143
|
+
});
|
|
144
|
+
return { verified: result.verified, durationMs: timer.ms() };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Compute circuit gate count / circuit size.
|
|
149
|
+
* @param circuitName - Identifier for the circuit.
|
|
150
|
+
* @param bytecode - Uncompressed ACIR bytecode.
|
|
151
|
+
* @param flavor - 'mega_honk' for chonk circuits, or an UltraHonk flavor.
|
|
152
|
+
* @returns The dyadic circuit size (next power of 2 of gate count).
|
|
153
|
+
*/
|
|
154
|
+
async computeGateCount(
|
|
155
|
+
circuitName: string,
|
|
156
|
+
bytecode: Uint8Array,
|
|
157
|
+
flavor: UltraHonkFlavor | 'mega_honk',
|
|
158
|
+
): Promise<{ circuitSize: number; durationMs: number }> {
|
|
159
|
+
const timer = new Timer();
|
|
160
|
+
if (flavor === 'mega_honk') {
|
|
161
|
+
const result = await this.api.chonkStats({
|
|
162
|
+
circuit: { name: circuitName, bytecode },
|
|
163
|
+
includeGatesPerOpcode: false,
|
|
164
|
+
});
|
|
165
|
+
return { circuitSize: result.circuitSize, durationMs: timer.ms() };
|
|
166
|
+
}
|
|
167
|
+
const result = await this.api.circuitStats({
|
|
168
|
+
circuit: { name: circuitName, bytecode, verificationKey: new Uint8Array(0) },
|
|
169
|
+
includeGatesPerOpcode: false,
|
|
170
|
+
settings: getProofSettings(flavor),
|
|
171
|
+
});
|
|
172
|
+
return { circuitSize: result.numGatesDyadic, durationMs: timer.ms() };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Generate a Solidity verifier contract from a verification key.
|
|
177
|
+
* @param verificationKey - The VK bytes.
|
|
178
|
+
* @returns The Solidity source code.
|
|
179
|
+
*/
|
|
180
|
+
async generateContract(verificationKey: Uint8Array): Promise<{ solidityCode: string; durationMs: number }> {
|
|
181
|
+
const timer = new Timer();
|
|
182
|
+
const result = await this.api.circuitWriteSolidityVerifier({
|
|
183
|
+
verificationKey,
|
|
184
|
+
settings: {
|
|
185
|
+
ipaAccumulation: false,
|
|
186
|
+
oracleHashType: 'poseidon2',
|
|
187
|
+
disableZk: true,
|
|
188
|
+
optimizedSolidityVerifier: false,
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
return { solidityCode: result.solidityCode, durationMs: timer.ms() };
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Verify a Chonk (IVC) proof passed as flat field elements (with public inputs prepended).
|
|
196
|
+
* The split into structured sub-proofs happens server-side in `ChonkProof::from_field_elements`,
|
|
197
|
+
* so this layer doesn't need to know per-component sub-proof sizes.
|
|
198
|
+
* @param fieldsWithPublicInputs - Flat proof fields as 32-byte Uint8Arrays (public inputs prepended).
|
|
199
|
+
* @param verificationKey - The VK bytes.
|
|
200
|
+
*/
|
|
201
|
+
async verifyChonkProof(
|
|
202
|
+
fieldsWithPublicInputs: Uint8Array[],
|
|
203
|
+
verificationKey: Uint8Array,
|
|
204
|
+
): Promise<{ verified: boolean; durationMs: number }> {
|
|
205
|
+
const timer = new Timer();
|
|
206
|
+
const result = await this.api.chonkVerifyFromFields({ proof: fieldsWithPublicInputs, vk: verificationKey });
|
|
207
|
+
return { verified: result.valid, durationMs: timer.ms() };
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** Generate an AVM proof from serialized inputs. */
|
|
211
|
+
async generateAvmProof(inputs: Uint8Array): Promise<{ proof: Uint8Array[]; stats: AvmStat[]; durationMs: number }> {
|
|
212
|
+
const timer = new Timer();
|
|
213
|
+
const result = await this.api.avmProve({ inputs });
|
|
214
|
+
return { proof: result.proof, stats: result.stats, durationMs: timer.ms() };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** Verify an AVM proof against serialized public inputs. */
|
|
218
|
+
async verifyAvmProof(
|
|
219
|
+
proof: Uint8Array[],
|
|
220
|
+
publicInputs: Uint8Array,
|
|
221
|
+
): Promise<{ verified: boolean; durationMs: number }> {
|
|
222
|
+
const timer = new Timer();
|
|
223
|
+
const result = await this.api.avmVerify({ proof, publicInputs });
|
|
224
|
+
return { verified: result.verified, durationMs: timer.ms() };
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** Check the AVM circuit from serialized inputs. */
|
|
228
|
+
async checkAvmCircuit(inputs: Uint8Array): Promise<{ passed: boolean; stats: AvmStat[]; durationMs: number }> {
|
|
229
|
+
const timer = new Timer();
|
|
230
|
+
const result = await this.api.avmCheckCircuit({ inputs });
|
|
231
|
+
return { passed: result.passed, stats: result.stats, durationMs: timer.ms() };
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** Destroy this instance and kill the underlying bb process. */
|
|
235
|
+
async destroy(): Promise<void> {
|
|
236
|
+
await this.api.destroy();
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** Options for {@link BBJsFactory}. */
|
|
241
|
+
export interface BBJsFactoryOptions {
|
|
242
|
+
/**
|
|
243
|
+
* Number of long-lived bb processes to keep in the pool.
|
|
244
|
+
* If omitted, every `getInstance()` call spawns a fresh bb that is destroyed on dispose.
|
|
245
|
+
*/
|
|
246
|
+
poolSize?: number;
|
|
247
|
+
logger?: Logger;
|
|
248
|
+
threads?: number;
|
|
249
|
+
debugDir?: string;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Manages bb.js instance lifecycle. By default every `getInstance()` call spawns a fresh
|
|
254
|
+
* bb process that is destroyed when the borrow is disposed. Pass `poolSize` to keep a fixed
|
|
255
|
+
* set of long-lived bb processes that are reused across calls — useful when the per-call
|
|
256
|
+
* bb startup cost dominates the workload (e.g. high-rate IVC verification).
|
|
257
|
+
*
|
|
258
|
+
* Idiomatic usage:
|
|
259
|
+
* ```
|
|
260
|
+
* await using inst = await factory.getInstance();
|
|
261
|
+
* await inst.someMethod(...);
|
|
262
|
+
* // disposed automatically when `inst` goes out of scope
|
|
263
|
+
* ```
|
|
264
|
+
*/
|
|
265
|
+
export class BBJsFactory {
|
|
266
|
+
private readonly poolSize?: number;
|
|
267
|
+
private readonly logger?: Logger;
|
|
268
|
+
private readonly threads?: number;
|
|
269
|
+
private readonly debugDir?: string;
|
|
270
|
+
|
|
271
|
+
/** Available pooled instances when poolSize is set; otherwise undefined. */
|
|
272
|
+
private pool?: FifoMemoryQueue<BBJsApi>;
|
|
273
|
+
/** Lazily-resolved on first `getInstance()` call to prevent racing pool initialization. */
|
|
274
|
+
private initPromise?: Promise<void>;
|
|
275
|
+
private destroyed = false;
|
|
276
|
+
|
|
277
|
+
constructor(
|
|
278
|
+
private bbPath: string,
|
|
279
|
+
options: BBJsFactoryOptions = {},
|
|
280
|
+
) {
|
|
281
|
+
this.poolSize = options.poolSize;
|
|
282
|
+
this.logger = options.logger;
|
|
283
|
+
this.threads = options.threads;
|
|
284
|
+
this.debugDir = options.debugDir;
|
|
285
|
+
if (this.poolSize !== undefined && this.poolSize < 1) {
|
|
286
|
+
throw new Error(`BBJsFactory poolSize must be >= 1, got ${this.poolSize}`);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Acquire a bb instance. The returned object implements `BBJsApi` and `AsyncDisposable`.
|
|
292
|
+
* With no pool: spawns a fresh bb that is destroyed on dispose. With a pool: borrows from
|
|
293
|
+
* the pool and returns to it on dispose.
|
|
294
|
+
*/
|
|
295
|
+
async getInstance(): Promise<BBJsApi & AsyncDisposable> {
|
|
296
|
+
if (this.destroyed) {
|
|
297
|
+
throw new Error('BBJsFactory has been destroyed');
|
|
298
|
+
}
|
|
299
|
+
if (this.poolSize === undefined) {
|
|
300
|
+
// No pool: fresh-per-call, dispose destroys.
|
|
301
|
+
const instance = await this.createInstance();
|
|
302
|
+
return this.makeOwned(instance);
|
|
303
|
+
}
|
|
304
|
+
if (!this.initPromise) {
|
|
305
|
+
this.initPromise = this.initPool();
|
|
306
|
+
}
|
|
307
|
+
await this.initPromise;
|
|
308
|
+
const pool = this.pool;
|
|
309
|
+
if (!pool) {
|
|
310
|
+
throw new Error('BBJsFactory has been destroyed');
|
|
311
|
+
}
|
|
312
|
+
const instance = await pool.get();
|
|
313
|
+
if (!instance) {
|
|
314
|
+
throw new Error('BBJsFactory was destroyed while waiting for an instance');
|
|
315
|
+
}
|
|
316
|
+
return this.makeBorrowed(instance);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Tear down all pooled instances. Idempotent. No-op when no pool is configured (fresh-per-call
|
|
321
|
+
* instances are destroyed by their own dispose callbacks). Instances currently held by an
|
|
322
|
+
* in-flight pooled borrow are destroyed by their dispose callback when released.
|
|
323
|
+
*/
|
|
324
|
+
async destroy(): Promise<void> {
|
|
325
|
+
if (this.destroyed) {
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
this.destroyed = true;
|
|
329
|
+
const pool = this.pool;
|
|
330
|
+
this.pool = undefined;
|
|
331
|
+
if (!pool) {
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
const idle: BBJsApi[] = [];
|
|
335
|
+
while (pool.length() > 0) {
|
|
336
|
+
const item = pool.getImmediate();
|
|
337
|
+
if (item) {
|
|
338
|
+
idle.push(item);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
pool.cancel();
|
|
342
|
+
// Aggregate teardown failures so a single bb child that fails to shut down doesn't mask others.
|
|
343
|
+
const results = await Promise.allSettled(idle.map(item => item.destroy()));
|
|
344
|
+
const errors = results.filter((r): r is PromiseRejectedResult => r.status === 'rejected').map(r => r.reason);
|
|
345
|
+
if (errors.length > 0) {
|
|
346
|
+
throw new AggregateError(errors, `BBJsFactory.destroy: ${errors.length} bb instance(s) failed to shut down`);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
private async initPool(): Promise<void> {
|
|
351
|
+
// Use allSettled so that if any createInstance() rejects we can destroy the rest instead of
|
|
352
|
+
// leaking bb child processes whose creation succeeded.
|
|
353
|
+
const results = await Promise.allSettled(Array.from({ length: this.poolSize! }, () => this.createInstance()));
|
|
354
|
+
const items: BBJsApi[] = [];
|
|
355
|
+
const errors: unknown[] = [];
|
|
356
|
+
for (const result of results) {
|
|
357
|
+
if (result.status === 'fulfilled') {
|
|
358
|
+
items.push(result.value);
|
|
359
|
+
} else {
|
|
360
|
+
errors.push(result.reason);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
if (errors.length > 0 || this.destroyed) {
|
|
364
|
+
// Either creation failed or destroy() raced ahead — clean up everything we successfully spawned.
|
|
365
|
+
await Promise.all(items.map(item => item.destroy()));
|
|
366
|
+
if (errors.length > 0) {
|
|
367
|
+
throw errors[0];
|
|
368
|
+
}
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
const pool = new FifoMemoryQueue<BBJsApi>();
|
|
372
|
+
for (const item of items) {
|
|
373
|
+
pool.put(item);
|
|
374
|
+
}
|
|
375
|
+
this.pool = pool;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
private async createInstance(): Promise<BBJsApi> {
|
|
379
|
+
const logFn = this.logger ? (msg: string) => this.logger!.verbose(`bb.js - ${msg}`) : undefined;
|
|
380
|
+
const raw = await BBJsInstance.create(this.bbPath, logFn, this.threads);
|
|
381
|
+
return this.maybeWrapDebug(raw);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/** Wrap the instance in a debug wrapper if debugDir is configured. */
|
|
385
|
+
private async maybeWrapDebug(instance: BBJsInstance): Promise<BBJsApi> {
|
|
386
|
+
if (this.debugDir && this.logger) {
|
|
387
|
+
const { DebugBBJsInstance } = await import('./bb_js_debug.js');
|
|
388
|
+
return new DebugBBJsInstance(instance, this.debugDir, this.bbPath, this.logger);
|
|
389
|
+
}
|
|
390
|
+
return instance;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* Wrap a fresh instance with an `AsyncDisposable` that destroys it on dispose. Used when no
|
|
395
|
+
* pool is configured. Destroy errors are propagated so that a teardown failure (e.g. a bb child
|
|
396
|
+
* that didn't shut down cleanly) surfaces instead of being silently swallowed.
|
|
397
|
+
*/
|
|
398
|
+
private makeOwned(instance: BBJsApi): BBJsApi & AsyncDisposable {
|
|
399
|
+
return this.makeDisposable(instance, () => instance.destroy());
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Wrap a pooled instance with an `AsyncDisposable` that returns it to the pool (or destroys it
|
|
404
|
+
* if the factory was destroyed in the meantime). Destroy errors are propagated.
|
|
405
|
+
*/
|
|
406
|
+
private makeBorrowed(instance: BBJsApi): BBJsApi & AsyncDisposable {
|
|
407
|
+
return this.makeDisposable(instance, async () => {
|
|
408
|
+
const pool = this.pool;
|
|
409
|
+
if (pool && !this.destroyed) {
|
|
410
|
+
pool.put(instance);
|
|
411
|
+
} else {
|
|
412
|
+
await instance.destroy();
|
|
413
|
+
}
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
private makeDisposable(instance: BBJsApi, onDispose: () => void | Promise<void>): BBJsApi & AsyncDisposable {
|
|
418
|
+
let disposed = false;
|
|
419
|
+
const dispose = async (): Promise<void> => {
|
|
420
|
+
if (disposed) {
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
disposed = true;
|
|
424
|
+
await onDispose();
|
|
425
|
+
};
|
|
426
|
+
return new Proxy(instance as BBJsApi & AsyncDisposable, {
|
|
427
|
+
get(target, prop, receiver) {
|
|
428
|
+
if (prop === Symbol.asyncDispose) {
|
|
429
|
+
return dispose;
|
|
430
|
+
}
|
|
431
|
+
return Reflect.get(target, prop, receiver);
|
|
432
|
+
},
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import type { AvmStat } from '@aztec/bb.js';
|
|
2
|
+
import type { Logger } from '@aztec/foundation/log';
|
|
3
|
+
|
|
4
|
+
import { promises as fs } from 'fs';
|
|
5
|
+
import * as path from 'path';
|
|
6
|
+
import { gzipSync } from 'zlib';
|
|
7
|
+
|
|
8
|
+
import type { UltraHonkFlavor } from '../honk.js';
|
|
9
|
+
import type { BBJsApi, BBJsProofResult } from './bb_js_backend.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Maps UltraHonk flavors to the CLI flags used by the bb binary.
|
|
13
|
+
* The CLI always uses `--scheme ultra_honk`; flavors are expressed via
|
|
14
|
+
* `--oracle_hash` and `--ipa_accumulation`.
|
|
15
|
+
*/
|
|
16
|
+
function getCliFlags(flavor: UltraHonkFlavor): string {
|
|
17
|
+
const base = '--scheme ultra_honk --disable_zk';
|
|
18
|
+
switch (flavor) {
|
|
19
|
+
case 'ultra_honk':
|
|
20
|
+
return `${base} --oracle_hash poseidon2`;
|
|
21
|
+
case 'ultra_keccak_honk':
|
|
22
|
+
return `${base} --oracle_hash keccak`;
|
|
23
|
+
case 'ultra_starknet_honk':
|
|
24
|
+
return `${base} --oracle_hash starknet`;
|
|
25
|
+
case 'ultra_rollup_honk':
|
|
26
|
+
return `${base} --oracle_hash poseidon2 --ipa_accumulation`;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Concatenate an array of 32-byte field elements into a single buffer. */
|
|
31
|
+
function concatFields(fields: Uint8Array[]): Buffer {
|
|
32
|
+
const totalLen = fields.reduce((sum, f) => sum + f.length, 0);
|
|
33
|
+
const buf = Buffer.alloc(totalLen);
|
|
34
|
+
let offset = 0;
|
|
35
|
+
for (const f of fields) {
|
|
36
|
+
buf.set(f, offset);
|
|
37
|
+
offset += f.length;
|
|
38
|
+
}
|
|
39
|
+
return buf;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Wraps a BBJsApi instance to write debug files and log equivalent CLI commands.
|
|
44
|
+
* Activated when BB_DEBUG_OUTPUT_DIR is set. Each operation writes its inputs
|
|
45
|
+
* and outputs to a numbered subdirectory, enabling offline reproduction.
|
|
46
|
+
*/
|
|
47
|
+
export class DebugBBJsInstance implements BBJsApi {
|
|
48
|
+
private counter = 0;
|
|
49
|
+
|
|
50
|
+
constructor(
|
|
51
|
+
private inner: BBJsApi,
|
|
52
|
+
private debugDir: string,
|
|
53
|
+
private bbPath: string,
|
|
54
|
+
private logger: Logger,
|
|
55
|
+
) {}
|
|
56
|
+
|
|
57
|
+
private nextDir(prefix: string): string {
|
|
58
|
+
this.counter++;
|
|
59
|
+
const padded = String(this.counter).padStart(3, '0');
|
|
60
|
+
return path.join(this.debugDir, `${prefix}-${padded}`);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Write a command string to both the logger and a command.sh file in the given directory. */
|
|
64
|
+
private async logCommand(dir: string, command: string): Promise<void> {
|
|
65
|
+
this.logger.info(`Executing BB with: ${command}`);
|
|
66
|
+
await fs.writeFile(path.join(dir, 'command.sh'), `#!/bin/bash\n${command}\n`, { mode: 0o755 });
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async generateProof(
|
|
70
|
+
circuitName: string,
|
|
71
|
+
bytecode: Uint8Array,
|
|
72
|
+
verificationKey: Uint8Array,
|
|
73
|
+
witness: Uint8Array,
|
|
74
|
+
flavor: UltraHonkFlavor,
|
|
75
|
+
): Promise<BBJsProofResult> {
|
|
76
|
+
const dir = this.nextDir(circuitName);
|
|
77
|
+
await fs.mkdir(dir, { recursive: true });
|
|
78
|
+
|
|
79
|
+
const bytecodePath = path.join(dir, `${circuitName}-bytecode.gz`);
|
|
80
|
+
const vkPath = path.join(dir, `${circuitName}-vk`);
|
|
81
|
+
const witnessPath = path.join(dir, 'partial-witness.gz');
|
|
82
|
+
|
|
83
|
+
await Promise.all([
|
|
84
|
+
fs.writeFile(bytecodePath, gzipSync(bytecode)),
|
|
85
|
+
fs.writeFile(vkPath, verificationKey),
|
|
86
|
+
fs.writeFile(witnessPath, gzipSync(witness)),
|
|
87
|
+
]);
|
|
88
|
+
|
|
89
|
+
const flags = getCliFlags(flavor);
|
|
90
|
+
await this.logCommand(
|
|
91
|
+
dir,
|
|
92
|
+
`${this.bbPath} prove ${flags} -o ${dir} -b ${bytecodePath} -k ${vkPath} -w ${witnessPath}`,
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
const result = await this.inner.generateProof(circuitName, bytecode, verificationKey, witness, flavor);
|
|
96
|
+
|
|
97
|
+
const proofBuf = concatFields(result.proofFields);
|
|
98
|
+
const publicInputsBuf = concatFields(result.publicInputFields);
|
|
99
|
+
await Promise.all([
|
|
100
|
+
fs.writeFile(path.join(dir, 'proof'), proofBuf),
|
|
101
|
+
fs.writeFile(path.join(dir, 'public_inputs'), publicInputsBuf),
|
|
102
|
+
]);
|
|
103
|
+
|
|
104
|
+
return result;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async verifyProof(
|
|
108
|
+
proofFields: Uint8Array[],
|
|
109
|
+
verificationKey: Uint8Array,
|
|
110
|
+
publicInputFields: Uint8Array[],
|
|
111
|
+
flavor: UltraHonkFlavor,
|
|
112
|
+
): Promise<{ verified: boolean; durationMs: number }> {
|
|
113
|
+
const dir = this.nextDir(`verify-${flavor}`);
|
|
114
|
+
await fs.mkdir(dir, { recursive: true });
|
|
115
|
+
|
|
116
|
+
const proofPath = path.join(dir, 'proof');
|
|
117
|
+
const vkPath = path.join(dir, 'vk');
|
|
118
|
+
const publicInputsPath = path.join(dir, 'public_inputs');
|
|
119
|
+
|
|
120
|
+
const proofBuf = concatFields(proofFields);
|
|
121
|
+
const publicInputsBuf = concatFields(publicInputFields);
|
|
122
|
+
await Promise.all([
|
|
123
|
+
fs.writeFile(proofPath, proofBuf),
|
|
124
|
+
fs.writeFile(vkPath, verificationKey),
|
|
125
|
+
fs.writeFile(publicInputsPath, publicInputsBuf),
|
|
126
|
+
]);
|
|
127
|
+
|
|
128
|
+
const flags = getCliFlags(flavor);
|
|
129
|
+
await this.logCommand(dir, `${this.bbPath} verify ${flags} -p ${proofPath} -k ${vkPath} -i ${publicInputsPath}`);
|
|
130
|
+
|
|
131
|
+
return this.inner.verifyProof(proofFields, verificationKey, publicInputFields, flavor);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async verifyChonkProof(
|
|
135
|
+
fieldsWithPublicInputs: Uint8Array[],
|
|
136
|
+
verificationKey: Uint8Array,
|
|
137
|
+
): Promise<{ verified: boolean; durationMs: number }> {
|
|
138
|
+
const dir = this.nextDir('verify-chonk');
|
|
139
|
+
await fs.mkdir(dir, { recursive: true });
|
|
140
|
+
|
|
141
|
+
const proofPath = path.join(dir, 'proof');
|
|
142
|
+
const vkPath = path.join(dir, 'vk');
|
|
143
|
+
|
|
144
|
+
const proofBuf = concatFields(fieldsWithPublicInputs);
|
|
145
|
+
await Promise.all([fs.writeFile(proofPath, proofBuf), fs.writeFile(vkPath, verificationKey)]);
|
|
146
|
+
|
|
147
|
+
await this.logCommand(dir, `${this.bbPath} verify --scheme chonk -p ${proofPath} -k ${vkPath} -v`);
|
|
148
|
+
|
|
149
|
+
return this.inner.verifyChonkProof(fieldsWithPublicInputs, verificationKey);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async computeGateCount(
|
|
153
|
+
circuitName: string,
|
|
154
|
+
bytecode: Uint8Array,
|
|
155
|
+
flavor: UltraHonkFlavor | 'mega_honk',
|
|
156
|
+
): Promise<{ circuitSize: number; durationMs: number }> {
|
|
157
|
+
const dir = this.nextDir(`gates-${circuitName}`);
|
|
158
|
+
await fs.mkdir(dir, { recursive: true });
|
|
159
|
+
|
|
160
|
+
const bytecodePath = path.join(dir, `${circuitName}-bytecode.gz`);
|
|
161
|
+
await fs.writeFile(bytecodePath, gzipSync(bytecode));
|
|
162
|
+
|
|
163
|
+
if (flavor === 'mega_honk') {
|
|
164
|
+
await this.logCommand(dir, `${this.bbPath} gates --scheme chonk -b ${bytecodePath}`);
|
|
165
|
+
} else {
|
|
166
|
+
const flags = getCliFlags(flavor);
|
|
167
|
+
await this.logCommand(dir, `${this.bbPath} gates ${flags} -b ${bytecodePath}`);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return this.inner.computeGateCount(circuitName, bytecode, flavor);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async generateAvmProof(inputs: Uint8Array): Promise<{ proof: Uint8Array[]; stats: AvmStat[]; durationMs: number }> {
|
|
174
|
+
const dir = this.nextDir('avm-prove');
|
|
175
|
+
await fs.mkdir(dir, { recursive: true });
|
|
176
|
+
|
|
177
|
+
const inputsPath = path.join(dir, 'avm_inputs.bin');
|
|
178
|
+
await fs.writeFile(inputsPath, inputs);
|
|
179
|
+
|
|
180
|
+
await this.logCommand(dir, `${this.bbPath} avm_prove --avm-inputs ${inputsPath} -o ${dir}`);
|
|
181
|
+
|
|
182
|
+
const result = await this.inner.generateAvmProof(inputs);
|
|
183
|
+
|
|
184
|
+
const proofBuf = concatFields(result.proof);
|
|
185
|
+
await fs.writeFile(path.join(dir, 'proof'), proofBuf);
|
|
186
|
+
|
|
187
|
+
return result;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async verifyAvmProof(
|
|
191
|
+
proof: Uint8Array[],
|
|
192
|
+
publicInputs: Uint8Array,
|
|
193
|
+
): Promise<{ verified: boolean; durationMs: number }> {
|
|
194
|
+
const dir = this.nextDir('avm-verify');
|
|
195
|
+
await fs.mkdir(dir, { recursive: true });
|
|
196
|
+
|
|
197
|
+
const proofPath = path.join(dir, 'proof');
|
|
198
|
+
const publicInputsPath = path.join(dir, 'avm_public_inputs.bin');
|
|
199
|
+
|
|
200
|
+
const proofBuf = concatFields(proof);
|
|
201
|
+
await Promise.all([fs.writeFile(proofPath, proofBuf), fs.writeFile(publicInputsPath, publicInputs)]);
|
|
202
|
+
|
|
203
|
+
await this.logCommand(dir, `${this.bbPath} avm_verify -p ${proofPath} --avm-public-inputs ${publicInputsPath}`);
|
|
204
|
+
|
|
205
|
+
return this.inner.verifyAvmProof(proof, publicInputs);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
async checkAvmCircuit(inputs: Uint8Array): Promise<{ passed: boolean; stats: AvmStat[]; durationMs: number }> {
|
|
209
|
+
const dir = this.nextDir('avm-check-circuit');
|
|
210
|
+
await fs.mkdir(dir, { recursive: true });
|
|
211
|
+
|
|
212
|
+
const inputsPath = path.join(dir, 'avm_inputs.bin');
|
|
213
|
+
await fs.writeFile(inputsPath, inputs);
|
|
214
|
+
|
|
215
|
+
await this.logCommand(dir, `${this.bbPath} avm_check_circuit --avm-inputs ${inputsPath}`);
|
|
216
|
+
|
|
217
|
+
return this.inner.checkAvmCircuit(inputs);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
generateContract(verificationKey: Uint8Array): Promise<{ solidityCode: string; durationMs: number }> {
|
|
221
|
+
return this.inner.generateContract(verificationKey);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
destroy(): Promise<void> {
|
|
225
|
+
return this.inner.destroy();
|
|
226
|
+
}
|
|
227
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// File-naming conventions used by `bb` CLI output directories. Retained for consumers that still
|
|
2
|
+
// read/write proofs to disk alongside bb-prover's in-memory bb.js flows.
|
|
3
|
+
|
|
4
|
+
export const VK_FILENAME = 'vk';
|
|
5
|
+
export const PROOF_FILENAME = 'proof';
|
|
6
|
+
export const PUBLIC_INPUTS_FILENAME = 'public_inputs';
|
package/src/config.ts
CHANGED
|
@@ -3,8 +3,24 @@ export interface BBConfig {
|
|
|
3
3
|
bbWorkingDirectory: string;
|
|
4
4
|
/** Whether to skip tmp dir cleanup for debugging purposes */
|
|
5
5
|
bbSkipCleanup: boolean;
|
|
6
|
+
/**
|
|
7
|
+
* Number of long-lived bb processes pooled by the RPC verifier (BBCircuitVerifier).
|
|
8
|
+
* Also caps concurrent verifications via the wrapping QueuedIVCVerifier.
|
|
9
|
+
*/
|
|
6
10
|
numConcurrentIVCVerifiers: number;
|
|
11
|
+
/** Thread count for the RPC IVC verifier. */
|
|
7
12
|
bbIVCConcurrency: number;
|
|
13
|
+
/**
|
|
14
|
+
* Upper bound on proofs per batch for the peer chonk batch verifier.
|
|
15
|
+
* Proofs are verified immediately as they arrive — this only caps how many
|
|
16
|
+
* can accumulate while a batch is already being processed.
|
|
17
|
+
* Default 16: at 4 cores, a full batch of 16 verifies in ~245ms wall time.
|
|
18
|
+
*/
|
|
19
|
+
bbChonkVerifyMaxBatch: number;
|
|
20
|
+
/** Thread count for the peer batch verifier parallel reduce. Default 6 to leave cores for the rest of the node. */
|
|
21
|
+
bbChonkVerifyConcurrency: number;
|
|
22
|
+
/** When set, bb.js operations write input/output files and log equivalent CLI commands to this directory. */
|
|
23
|
+
bbDebugOutputDir?: string;
|
|
8
24
|
}
|
|
9
25
|
|
|
10
26
|
export interface ACVMConfig {
|