Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
bbapi_chonk.hpp
Go to the documentation of this file.
1#pragma once
15
16#ifdef BB_HAS_BATCH_VERIFIER_SERVICE
20#include <atomic>
21#include <mutex>
22#endif
23
24#include <string>
25#include <vector>
26
27namespace bb::bbapi {
28
35struct ChonkStart {
36 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkStart";
37
42 struct Response {
43 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkStartResponse";
44 // Empty response - success indicated by no exception
45 void msgpack(auto&& pack_fn) { pack_fn(); }
46 bool operator==(const Response&) const = default;
47 };
48 // Kind of every circuit to be accumulated, in accumulation order. The IVC needs the full stack layout
49 // upfront so the prover can tell, while accumulating a circuit, whether a kernel follows (which triggers
50 // the group's multilinear batching proof).
51 std::vector<CircuitKind> kinds;
52 Response execute(BBApiRequest& request) &&;
54 bool operator==(const ChonkStart&) const = default;
55};
56
61struct ChonkLoad {
62 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkLoad";
63
68 struct Response {
69 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkLoadResponse";
70 // Empty response - success indicated by no exception
71 void msgpack(auto&& pack_fn) { pack_fn(); }
72 bool operator==(const Response&) const = default;
73 };
74
79 Response execute(BBApiRequest& request) &&;
81 bool operator==(const ChonkLoad&) const = default;
82};
83
89 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkAccumulate";
90
95 struct Response {
96 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkAccumulateResponse";
97 // Empty response - success indicated by no exception
98 void msgpack(auto&& pack_fn) { pack_fn(); }
99 bool operator==(const Response&) const = default;
100 };
101
103 std::vector<uint8_t> witness;
104 Response execute(BBApiRequest& request) &&;
106 bool operator==(const ChonkAccumulate&) const = default;
107};
108
114 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkProve";
115
120 struct Response {
121 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkProveResponse";
122
126 bool operator==(const Response&) const = default;
127 };
128 Response execute(BBApiRequest& request) &&;
129 void msgpack(auto&& pack_fn) { pack_fn(); }
130 bool operator==(const ChonkProve&) const = default;
131};
132
141 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkVerify";
142
147 struct Response {
148 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkVerifyResponse";
149
151 bool valid;
153 bool operator==(const Response&) const = default;
154 };
155
159 std::vector<uint8_t> vk;
160 Response execute(const BBApiRequest& request = {}) &&;
162 bool operator==(const ChonkVerify&) const = default;
163};
164
175 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkVerifyFromFields";
176
177 struct Response {
178 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkVerifyFromFieldsResponse";
179
181 bool valid;
183 bool operator==(const Response&) const = default;
184 };
185
187 std::vector<bb::fr> proof;
189 std::vector<uint8_t> vk;
190 Response execute(const BBApiRequest& request = {}) &&;
192 bool operator==(const ChonkVerifyFromFields&) const = default;
193};
194
203 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkComputeVk";
204
209 struct Response {
210 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkComputeVkResponse";
211
213 std::vector<uint8_t> bytes;
215 std::vector<bb::fr> fields;
217 bool operator==(const Response&) const = default;
218 };
219
221 // CircuitKind tag selecting the per-kind slim flavor (App / Kernel / HidingKernel).
223 Response execute([[maybe_unused]] const BBApiRequest& request = {}) &&;
225 bool operator==(const ChonkComputeVk&) const = default;
226};
227
233 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkCheckPrecomputedVk";
234
239 struct Response {
240 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkCheckPrecomputedVkResponse";
241
243 bool valid;
245 std::vector<uint8_t> actual_vk;
247 bool operator==(const Response&) const = default;
248 };
249
254
255 Response execute(const BBApiRequest& request = {}) &&;
257 bool operator==(const ChonkCheckPrecomputedVk&) const = default;
258};
259
265 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkStats";
266
271 struct Response {
272 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkStatsResponse";
273
275 uint32_t acir_opcodes;
277 uint32_t circuit_size;
279 std::vector<uint32_t> gates_per_opcode;
281 bool operator==(const Response&) const = default;
282 };
283
288 Response execute(BBApiRequest& request) &&;
290 bool operator==(const ChonkStats&) const = default;
291};
292
298 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkBatchVerify";
299
300 struct Response {
301 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkBatchVerifyResponse";
302 bool valid;
304 bool operator==(const Response&) const = default;
305 };
306
309 Response execute(const BBApiRequest& request = {}) &&;
311 bool operator==(const ChonkBatchVerify&) const = default;
312};
313
321 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkCompressProof";
322
323 struct Response {
324 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkCompressProofResponse";
325 std::vector<uint8_t> compressed_proof;
327 bool operator==(const Response&) const = default;
328 };
329
331 Response execute(const BBApiRequest& request = {}) &&;
333 bool operator==(const ChonkCompressProof&) const = default;
334};
335
343 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkDecompressProof";
344
345 struct Response {
346 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkDecompressProofResponse";
349 bool operator==(const Response&) const = default;
350 };
351
352 std::vector<uint8_t> compressed_proof;
353 Response execute(const BBApiRequest& request = {}) &&;
355 bool operator==(const ChonkDecompressProof&) const = default;
356};
357
358#ifdef BB_HAS_BATCH_VERIFIER_SERVICE
367class ChonkBatchVerifierService {
368 public:
369 ChonkBatchVerifierService() = default;
370 ~ChonkBatchVerifierService();
371
372 ChonkBatchVerifierService(const ChonkBatchVerifierService&) = delete;
373 ChonkBatchVerifierService& operator=(const ChonkBatchVerifierService&) = delete;
374
375 void start(std::vector<std::shared_ptr<MegaZKFlavor::VKAndHash>> vks,
376 uint32_t num_cores,
377 uint32_t batch_size,
378 const std::string& fifo_path);
379 void enqueue(VerifyRequest request);
380 void fail_request(uint64_t request_id, std::string error_message);
381 void stop();
382 bool is_running() const { return running_.load(); }
383
384 private:
385 bool write_result(VerifyResult result);
386 bool ensure_fifo_open();
387 void close_fifo_locked();
388 bool fail_fifo_locked(const std::string& message);
389
390 ChonkBatchVerifier verifier_;
391
392 std::mutex fifo_mutex_;
393 std::string fifo_path_;
394 int fifo_fd_ = -1;
395 std::atomic_bool running_ = false;
396 std::atomic_bool fifo_failed_ = false;
397};
398#endif // BB_HAS_BATCH_VERIFIER_SERVICE
399
405 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkBatchVerifierStart";
406
407 struct Response {
408 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkBatchVerifierStartResponse";
409 void msgpack(auto&& pack_fn) { pack_fn(); }
410 bool operator==(const Response&) const = default;
411 };
412
413 std::vector<std::vector<uint8_t>> vks; // Serialized verification keys
414 uint32_t num_cores = 0; // 0 = auto
415 uint32_t batch_size = 8;
416 std::string fifo_path;
417
418 Response execute(BBApiRequest& request) &&;
420 bool operator==(const ChonkBatchVerifierStart&) const = default;
421};
422
428 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkBatchVerifierQueue";
429
430 struct Response {
431 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkBatchVerifierQueueResponse";
432 void msgpack(auto&& pack_fn) { pack_fn(); }
433 bool operator==(const Response&) const = default;
434 };
435
436 uint64_t request_id = 0;
437 uint32_t vk_index = 0;
438 std::vector<bb::fr> proof_fields;
439
440 Response execute(BBApiRequest& request) &&;
442 bool operator==(const ChonkBatchVerifierQueue&) const = default;
443};
444
450 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkBatchVerifierStop";
451
452 struct Response {
453 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkBatchVerifierStopResponse";
454 void msgpack(auto&& pack_fn) { pack_fn(); }
455 bool operator==(const Response&) const = default;
456 };
457
458 Response execute(BBApiRequest& request) &&;
459 void msgpack(auto&& pack_fn) { pack_fn(); }
460 bool operator==(const ChonkBatchVerifierStop&) const = default;
461};
462
463} // namespace bb::bbapi
Shared type definitions for the Barretenberg RPC API.
Asynchronous batch verifier for Chonk IVC proofs.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
A request to verify a single Chonk proof.
Result of verifying a single proof within a batch.
Empty response indicating successful circuit accumulation.
bool operator==(const Response &) const =default
static constexpr const char MSGPACK_SCHEMA_NAME[]
Accumulate the previously loaded circuit into the IVC proof.
static constexpr const char MSGPACK_SCHEMA_NAME[]
std::vector< uint8_t > witness
Serialized witness data for the last loaded circuit.
Response execute(BBApiRequest &request) &&
bool operator==(const ChonkAccumulate &) const =default
bool operator==(const Response &) const =default
static constexpr const char MSGPACK_SCHEMA_NAME[]
Enqueue a proof for batch verification.
Response execute(BBApiRequest &request) &&
std::vector< bb::fr > proof_fields
static constexpr const char MSGPACK_SCHEMA_NAME[]
bool operator==(const ChonkBatchVerifierQueue &) const =default
SERIALIZATION_FIELDS(request_id, vk_index, proof_fields)
static constexpr const char MSGPACK_SCHEMA_NAME[]
bool operator==(const Response &) const =default
Start the batch verifier service.
SERIALIZATION_FIELDS(vks, num_cores, batch_size, fifo_path)
Response execute(BBApiRequest &request) &&
bool operator==(const ChonkBatchVerifierStart &) const =default
std::vector< std::vector< uint8_t > > vks
static constexpr const char MSGPACK_SCHEMA_NAME[]
bool operator==(const Response &) const =default
static constexpr const char MSGPACK_SCHEMA_NAME[]
Stop the batch verifier service.
bool operator==(const ChonkBatchVerifierStop &) const =default
static constexpr const char MSGPACK_SCHEMA_NAME[]
Response execute(BBApiRequest &request) &&
bool operator==(const Response &) const =default
static constexpr const char MSGPACK_SCHEMA_NAME[]
Batch-verify multiple Chonk proofs with batched IPA SRS MSMs.
std::vector< std::vector< uint8_t > > vks
bool operator==(const ChonkBatchVerify &) const =default
SERIALIZATION_FIELDS(proofs, vks)
Response execute(const BBApiRequest &request={}) &&
static constexpr const char MSGPACK_SCHEMA_NAME[]
std::vector< ChonkProof > proofs
Contains the validation result.
std::vector< uint8_t > actual_vk
The actual VK it should be.
bool valid
True if the precomputed VK matches the circuit.
bool operator==(const Response &) const =default
static constexpr const char MSGPACK_SCHEMA_NAME[]
Verify that a precomputed verification key matches the circuit.
bool operator==(const ChonkCheckPrecomputedVk &) const =default
Response execute(const BBApiRequest &request={}) &&
CircuitKind kind
CircuitKind tag selecting the per-kind flavor (App / Kernel / HidingKernel).
CircuitInput circuit
Circuit with its precomputed verification key.
static constexpr const char MSGPACK_SCHEMA_NAME[]
static constexpr const char MSGPACK_SCHEMA_NAME[]
bool operator==(const Response &) const =default
Compress a Chonk proof to a compact byte representation.
bool operator==(const ChonkCompressProof &) const =default
static constexpr const char MSGPACK_SCHEMA_NAME[]
Response execute(const BBApiRequest &request={}) &&
Contains the computed verification key in multiple formats.
static constexpr const char MSGPACK_SCHEMA_NAME[]
std::vector< uint8_t > bytes
Serialized Chonk verification key in binary format.
std::vector< bb::fr > fields
Verification key as array of field elements.
bool operator==(const Response &) const =default
Compute MegaHonk verification key for a circuit to be accumulated in Chonk.
static constexpr const char MSGPACK_SCHEMA_NAME[]
Response execute(const BBApiRequest &request={}) &&
SERIALIZATION_FIELDS(circuit, kind)
bool operator==(const ChonkComputeVk &) const =default
bool operator==(const Response &) const =default
static constexpr const char MSGPACK_SCHEMA_NAME[]
Decompress a compressed Chonk proof back to field elements.
std::vector< uint8_t > compressed_proof
SERIALIZATION_FIELDS(compressed_proof)
bool operator==(const ChonkDecompressProof &) const =default
static constexpr const char MSGPACK_SCHEMA_NAME[]
Response execute(const BBApiRequest &request={}) &&
Empty response indicating successful circuit loading.
void msgpack(auto &&pack_fn)
static constexpr const char MSGPACK_SCHEMA_NAME[]
bool operator==(const Response &) const =default
Load a circuit into the Chonk instance for accumulation.
static constexpr const char MSGPACK_SCHEMA_NAME[]
bool operator==(const ChonkLoad &) const =default
CircuitKind kind
CircuitKind tag selecting the per-kind slim flavor (App / Kernel / HidingKernel).
SERIALIZATION_FIELDS(circuit, kind)
Response execute(BBApiRequest &request) &&
CircuitInput circuit
Circuit to be loaded with its bytecode and verification key.
Contains the generated IVC proof.
bool operator==(const Response &) const =default
static constexpr const char MSGPACK_SCHEMA_NAME[]
ChonkProof proof
Complete IVC proof for all accumulated circuits.
Generate a proof for all accumulated circuits.
static constexpr const char MSGPACK_SCHEMA_NAME[]
void msgpack(auto &&pack_fn)
bool operator==(const ChonkProve &) const =default
Response execute(BBApiRequest &request) &&
Empty response indicating successful initialization.
bool operator==(const Response &) const =default
void msgpack(auto &&pack_fn)
static constexpr const char MSGPACK_SCHEMA_NAME[]
Initialize a new Chonk instance for incremental proof accumulation.
Response execute(BBApiRequest &request) &&
bool operator==(const ChonkStart &) const =default
std::vector< CircuitKind > kinds
static constexpr const char MSGPACK_SCHEMA_NAME[]
Contains gate count information.
SERIALIZATION_FIELDS(acir_opcodes, circuit_size, gates_per_opcode)
uint32_t circuit_size
Circuit size (total number of gates)
static constexpr const char MSGPACK_SCHEMA_NAME[]
uint32_t acir_opcodes
Number of ACIR opcodes.
std::vector< uint32_t > gates_per_opcode
Optional: gate counts per opcode.
bool operator==(const Response &) const =default
Get gate counts for a circuit.
bool include_gates_per_opcode
Whether to include detailed gate counts per opcode.
static constexpr const char MSGPACK_SCHEMA_NAME[]
bool operator==(const ChonkStats &) const =default
CircuitInputNoVK circuit
The circuit to analyze.
SERIALIZATION_FIELDS(circuit, include_gates_per_opcode)
Response execute(BBApiRequest &request) &&
Contains the verification result.
bool valid
True if the proof is valid.
static constexpr const char MSGPACK_SCHEMA_NAME[]
bool operator==(const Response &) const =default
static constexpr const char MSGPACK_SCHEMA_NAME[]
bool valid
True if the proof is valid.
bool operator==(const Response &) const =default
Verify a Chonk proof passed as a flat field-element array (with public inputs prepended).
std::vector< uint8_t > vk
The verification key.
bool operator==(const ChonkVerifyFromFields &) const =default
std::vector< bb::fr > proof
Flat proof field elements with public inputs prepended.
static constexpr const char MSGPACK_SCHEMA_NAME[]
Response execute(const BBApiRequest &request={}) &&
Verify a Chonk proof with its verification key.
static constexpr const char MSGPACK_SCHEMA_NAME[]
std::vector< uint8_t > vk
The verification key.
SERIALIZATION_FIELDS(proof, vk)
Response execute(const BBApiRequest &request={}) &&
bool operator==(const ChonkVerify &) const =default
ChonkProof proof
The Chonk proof to verify.
A circuit to be used in either ultrahonk or Chonk proving.
A circuit to be used in either ultrahonk or chonk verification key derivation.
VectorField result