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
169 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkComputeVk";
170
175 struct Response {
176 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkComputeVkResponse";
177
179 std::vector<uint8_t> bytes;
181 std::vector<bb::fr> fields;
183 bool operator==(const Response&) const = default;
184 };
185
189 bool use_zk_flavor = false;
190 Response execute([[maybe_unused]] const BBApiRequest& request = {}) &&;
192 bool operator==(const ChonkComputeVk&) const = default;
193};
194
200 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkCheckPrecomputedVk";
201
206 struct Response {
207 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkCheckPrecomputedVkResponse";
208
210 bool valid;
212 std::vector<uint8_t> actual_vk;
214 bool operator==(const Response&) const = default;
215 };
216
221 bool use_zk_flavor = false;
222
223 Response execute(const BBApiRequest& request = {}) &&;
225 bool operator==(const ChonkCheckPrecomputedVk&) const = default;
226};
227
233 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkStats";
234
239 struct Response {
240 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkStatsResponse";
241
243 uint32_t acir_opcodes;
245 uint32_t circuit_size;
247 std::vector<uint32_t> gates_per_opcode;
249 bool operator==(const Response&) const = default;
250 };
251
256 Response execute(BBApiRequest& request) &&;
258 bool operator==(const ChonkStats&) const = default;
259};
260
266 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkBatchVerify";
267
268 struct Response {
269 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkBatchVerifyResponse";
270 bool valid;
272 bool operator==(const Response&) const = default;
273 };
274
277 Response execute(const BBApiRequest& request = {}) &&;
279 bool operator==(const ChonkBatchVerify&) const = default;
280};
281
289 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkCompressProof";
290
291 struct Response {
292 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkCompressProofResponse";
293 std::vector<uint8_t> compressed_proof;
295 bool operator==(const Response&) const = default;
296 };
297
299 Response execute(const BBApiRequest& request = {}) &&;
301 bool operator==(const ChonkCompressProof&) const = default;
302};
303
311 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkDecompressProof";
312
313 struct Response {
314 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkDecompressProofResponse";
317 bool operator==(const Response&) const = default;
318 };
319
320 std::vector<uint8_t> compressed_proof;
321 Response execute(const BBApiRequest& request = {}) &&;
323 bool operator==(const ChonkDecompressProof&) const = default;
324};
325
326#ifndef __wasm__
336 public:
339
342
344 uint32_t num_cores,
345 uint32_t batch_size,
346 const std::string& fifo_path);
347 void enqueue(VerifyRequest request);
348 void stop();
349 bool is_running() const { return running_; }
350
351 private:
352 void writer_loop(const std::string& fifo_path);
353
355
356 std::mutex result_mutex_;
359 bool writer_shutdown_ = false;
360 std::thread writer_thread_;
361
362 bool running_ = false;
363};
364#endif // __wasm__
365
371 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkBatchVerifierStart";
372
373 struct Response {
374 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkBatchVerifierStartResponse";
375 void msgpack(auto&& pack_fn) { pack_fn(); }
376 bool operator==(const Response&) const = default;
377 };
378
379 std::vector<std::vector<uint8_t>> vks; // Serialized verification keys
380 uint32_t num_cores = 0; // 0 = auto
381 uint32_t batch_size = 8;
382 std::string fifo_path;
383
384 Response execute(BBApiRequest& request) &&;
386 bool operator==(const ChonkBatchVerifierStart&) const = default;
387};
388
394 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkBatchVerifierQueue";
395
396 struct Response {
397 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkBatchVerifierQueueResponse";
398 void msgpack(auto&& pack_fn) { pack_fn(); }
399 bool operator==(const Response&) const = default;
400 };
401
402 uint64_t request_id = 0;
403 uint32_t vk_index = 0;
404 std::vector<bb::fr> proof_fields;
405
406 Response execute(BBApiRequest& request) &&;
408 bool operator==(const ChonkBatchVerifierQueue&) const = default;
409};
410
416 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkBatchVerifierStop";
417
418 struct Response {
419 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkBatchVerifierStopResponse";
420 void msgpack(auto&& pack_fn) { pack_fn(); }
421 bool operator==(const Response&) const = default;
422 };
423
424 Response execute(BBApiRequest& request) &&;
425 void msgpack(auto&& pack_fn) { pack_fn(); }
426 bool operator==(const ChonkBatchVerifierStop&) const = default;
427};
428
429} // 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
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.