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#ifndef __wasm__
20#include <condition_variable>
21#include <mutex>
22#include <queue>
23#include <thread>
24#endif
25
26#include <string>
27#include <vector>
28
29namespace bb::bbapi {
30
37struct ChonkStart {
38 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkStart";
39
44 struct Response {
45 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkStartResponse";
46 // Empty response - success indicated by no exception
47 void msgpack(auto&& pack_fn) { pack_fn(); }
48 bool operator==(const Response&) const = default;
49 };
50 // Number of circuits to be accumulated.
51 uint32_t num_circuits;
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
77 Response execute(BBApiRequest& request) &&;
79 bool operator==(const ChonkLoad&) const = default;
80};
81
87 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkAccumulate";
88
93 struct Response {
94 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkAccumulateResponse";
95 // Empty response - success indicated by no exception
96 void msgpack(auto&& pack_fn) { pack_fn(); }
97 bool operator==(const Response&) const = default;
98 };
99
101 std::vector<uint8_t> witness;
102 Response execute(BBApiRequest& request) &&;
104 bool operator==(const ChonkAccumulate&) const = default;
105};
106
112 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkProve";
113
118 struct Response {
119 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkProveResponse";
120
124 bool operator==(const Response&) const = default;
125 };
126 Response execute(BBApiRequest& request) &&;
127 void msgpack(auto&& pack_fn) { pack_fn(); }
128 bool operator==(const ChonkProve&) const = default;
129};
130
136 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkVerify";
137
142 struct Response {
143 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkVerifyResponse";
144
146 bool valid;
148 bool operator==(const Response&) const = default;
149 };
150
154 std::vector<uint8_t> vk;
155 Response execute(const BBApiRequest& request = {}) &&;
157 bool operator==(const ChonkVerify&) const = default;
158};
159
170 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkVerifyFromFields";
171
172 struct Response {
173 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkVerifyFromFieldsResponse";
174
176 bool valid;
178 bool operator==(const Response&) const = default;
179 };
180
182 std::vector<bb::fr> proof;
184 std::vector<uint8_t> vk;
185 Response execute(const BBApiRequest& request = {}) &&;
187 bool operator==(const ChonkVerifyFromFields&) const = default;
188};
189
199 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkComputeVk";
200
205 struct Response {
206 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkComputeVkResponse";
207
209 std::vector<uint8_t> bytes;
211 std::vector<bb::fr> fields;
213 bool operator==(const Response&) const = default;
214 };
215
219 bool use_zk_flavor = false;
220 Response execute([[maybe_unused]] const BBApiRequest& request = {}) &&;
222 bool operator==(const ChonkComputeVk&) const = default;
223};
224
230 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkCheckPrecomputedVk";
231
236 struct Response {
237 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkCheckPrecomputedVkResponse";
238
240 bool valid;
242 std::vector<uint8_t> actual_vk;
244 bool operator==(const Response&) const = default;
245 };
246
251 bool use_zk_flavor = false;
252
253 Response execute(const BBApiRequest& request = {}) &&;
255 bool operator==(const ChonkCheckPrecomputedVk&) const = default;
256};
257
263 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkStats";
264
269 struct Response {
270 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkStatsResponse";
271
273 uint32_t acir_opcodes;
275 uint32_t circuit_size;
277 std::vector<uint32_t> gates_per_opcode;
279 bool operator==(const Response&) const = default;
280 };
281
286 Response execute(BBApiRequest& request) &&;
288 bool operator==(const ChonkStats&) const = default;
289};
290
296 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkBatchVerify";
297
298 struct Response {
299 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkBatchVerifyResponse";
300 bool valid;
302 bool operator==(const Response&) const = default;
303 };
304
307 Response execute(const BBApiRequest& request = {}) &&;
309 bool operator==(const ChonkBatchVerify&) const = default;
310};
311
319 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkCompressProof";
320
321 struct Response {
322 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkCompressProofResponse";
323 std::vector<uint8_t> compressed_proof;
325 bool operator==(const Response&) const = default;
326 };
327
329 Response execute(const BBApiRequest& request = {}) &&;
331 bool operator==(const ChonkCompressProof&) const = default;
332};
333
341 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkDecompressProof";
342
343 struct Response {
344 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkDecompressProofResponse";
347 bool operator==(const Response&) const = default;
348 };
349
350 std::vector<uint8_t> compressed_proof;
351 Response execute(const BBApiRequest& request = {}) &&;
353 bool operator==(const ChonkDecompressProof&) const = default;
354};
355
356#ifndef __wasm__
366 public:
369
372
374 uint32_t num_cores,
375 uint32_t batch_size,
376 const std::string& fifo_path);
377 void enqueue(VerifyRequest request);
378 void stop();
379 bool is_running() const { return running_; }
380
381 private:
382 void writer_loop(const std::string& fifo_path);
383
385
386 std::mutex result_mutex_;
389 bool writer_shutdown_ = false;
390 std::thread writer_thread_;
391
392 bool running_ = false;
393};
394#endif // __wasm__
395
401 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkBatchVerifierStart";
402
403 struct Response {
404 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkBatchVerifierStartResponse";
405 void msgpack(auto&& pack_fn) { pack_fn(); }
406 bool operator==(const Response&) const = default;
407 };
408
409 std::vector<std::vector<uint8_t>> vks; // Serialized verification keys
410 uint32_t num_cores = 0; // 0 = auto
411 uint32_t batch_size = 8;
412 std::string fifo_path;
413
414 Response execute(BBApiRequest& request) &&;
416 bool operator==(const ChonkBatchVerifierStart&) const = default;
417};
418
424 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkBatchVerifierQueue";
425
426 struct Response {
427 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkBatchVerifierQueueResponse";
428 void msgpack(auto&& pack_fn) { pack_fn(); }
429 bool operator==(const Response&) const = default;
430 };
431
432 uint64_t request_id = 0;
433 uint32_t vk_index = 0;
434 std::vector<bb::fr> proof_fields;
435
436 Response execute(BBApiRequest& request) &&;
438 bool operator==(const ChonkBatchVerifierQueue&) const = default;
439};
440
446 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkBatchVerifierStop";
447
448 struct Response {
449 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkBatchVerifierStopResponse";
450 void msgpack(auto&& pack_fn) { pack_fn(); }
451 bool operator==(const Response&) const = default;
452 };
453
454 Response execute(BBApiRequest& request) &&;
455 void msgpack(auto&& pack_fn) { pack_fn(); }
456 bool operator==(const ChonkBatchVerifierStop&) const = default;
457};
458
459} // namespace bb::bbapi
Shared type definitions for the Barretenberg RPC API.
Asynchronous batch verifier for Chonk IVC proofs.
FIFO-streaming batch verification service for Chonk proofs.
void enqueue(VerifyRequest request)
ChonkBatchVerifierService(const ChonkBatchVerifierService &)=delete
void writer_loop(const std::string &fifo_path)
std::queue< VerifyResult > result_queue_
std::condition_variable result_cv_
void start(std::vector< std::shared_ptr< MegaZKFlavor::VKAndHash > > vks, uint32_t num_cores, uint32_t batch_size, const std::string &fifo_path)
ChonkBatchVerifierService & operator=(const ChonkBatchVerifierService &)=delete
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
A request to verify a single Chonk proof.
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 a single IPA SRS MSM.
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={}) &&
SERIALIZATION_FIELDS(circuit, use_zk_flavor)
bool use_zk_flavor
When true, derive VK using MegaZKFlavor; otherwise MegaFlavor. The caller sets this to true for the h...
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 MegaVerificationKey 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[]
SERIALIZATION_FIELDS(circuit, use_zk_flavor)
Response execute(const BBApiRequest &request={}) &&
bool operator==(const ChonkComputeVk &) const =default
bool use_zk_flavor
When true, derive VK using MegaZKFlavor; otherwise MegaFlavor. The caller sets this to true for the h...
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
SERIALIZATION_FIELDS(circuit)
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
static constexpr const char MSGPACK_SCHEMA_NAME[]
SERIALIZATION_FIELDS(num_circuits)
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.