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 <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 // Number of circuits to be accumulated.
49 uint32_t num_circuits;
50 Response execute(BBApiRequest& request) &&;
52 bool operator==(const ChonkStart&) const = default;
53};
54
59struct ChonkLoad {
60 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkLoad";
61
66 struct Response {
67 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkLoadResponse";
68 // Empty response - success indicated by no exception
69 void msgpack(auto&& pack_fn) { pack_fn(); }
70 bool operator==(const Response&) const = default;
71 };
72
75 Response execute(BBApiRequest& request) &&;
77 bool operator==(const ChonkLoad&) const = default;
78};
79
85 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkAccumulate";
86
91 struct Response {
92 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkAccumulateResponse";
93 // Empty response - success indicated by no exception
94 void msgpack(auto&& pack_fn) { pack_fn(); }
95 bool operator==(const Response&) const = default;
96 };
97
99 std::vector<uint8_t> witness;
100 Response execute(BBApiRequest& request) &&;
102 bool operator==(const ChonkAccumulate&) const = default;
103};
104
110 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkProve";
111
116 struct Response {
117 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkProveResponse";
118
122 bool operator==(const Response&) const = default;
123 };
124 Response execute(BBApiRequest& request) &&;
125 void msgpack(auto&& pack_fn) { pack_fn(); }
126 bool operator==(const ChonkProve&) const = default;
127};
128
137 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkVerify";
138
143 struct Response {
144 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkVerifyResponse";
145
147 bool valid;
149 bool operator==(const Response&) const = default;
150 };
151
155 std::vector<uint8_t> vk;
156 Response execute(const BBApiRequest& request = {}) &&;
158 bool operator==(const ChonkVerify&) const = default;
159};
160
171 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkVerifyFromFields";
172
173 struct Response {
174 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkVerifyFromFieldsResponse";
175
177 bool valid;
179 bool operator==(const Response&) const = default;
180 };
181
183 std::vector<bb::fr> proof;
185 std::vector<uint8_t> vk;
186 Response execute(const BBApiRequest& request = {}) &&;
188 bool operator==(const ChonkVerifyFromFields&) const = default;
189};
190
200 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkComputeVk";
201
206 struct Response {
207 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkComputeVkResponse";
208
210 std::vector<uint8_t> bytes;
212 std::vector<bb::fr> fields;
214 bool operator==(const Response&) const = default;
215 };
216
220 bool use_zk_flavor = false;
221 Response execute([[maybe_unused]] const BBApiRequest& request = {}) &&;
223 bool operator==(const ChonkComputeVk&) const = default;
224};
225
231 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkCheckPrecomputedVk";
232
237 struct Response {
238 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkCheckPrecomputedVkResponse";
239
241 bool valid;
243 std::vector<uint8_t> actual_vk;
245 bool operator==(const Response&) const = default;
246 };
247
252 bool use_zk_flavor = false;
253
254 Response execute(const BBApiRequest& request = {}) &&;
256 bool operator==(const ChonkCheckPrecomputedVk&) const = default;
257};
258
264 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkStats";
265
270 struct Response {
271 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkStatsResponse";
272
274 uint32_t acir_opcodes;
276 uint32_t circuit_size;
278 std::vector<uint32_t> gates_per_opcode;
280 bool operator==(const Response&) const = default;
281 };
282
287 Response execute(BBApiRequest& request) &&;
289 bool operator==(const ChonkStats&) const = default;
290};
291
297 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkBatchVerify";
298
299 struct Response {
300 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkBatchVerifyResponse";
301 bool valid;
303 bool operator==(const Response&) const = default;
304 };
305
308 Response execute(const BBApiRequest& request = {}) &&;
310 bool operator==(const ChonkBatchVerify&) const = default;
311};
312
320 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkCompressProof";
321
322 struct Response {
323 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkCompressProofResponse";
324 std::vector<uint8_t> compressed_proof;
326 bool operator==(const Response&) const = default;
327 };
328
330 Response execute(const BBApiRequest& request = {}) &&;
332 bool operator==(const ChonkCompressProof&) const = default;
333};
334
342 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkDecompressProof";
343
344 struct Response {
345 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkDecompressProofResponse";
348 bool operator==(const Response&) const = default;
349 };
350
351 std::vector<uint8_t> compressed_proof;
352 Response execute(const BBApiRequest& request = {}) &&;
354 bool operator==(const ChonkDecompressProof&) const = default;
355};
356
357#ifndef __wasm__
367 public:
370
373
375 uint32_t num_cores,
376 uint32_t batch_size,
377 const std::string& fifo_path);
378 void enqueue(VerifyRequest request);
379 void fail_request(uint64_t request_id, std::string error_message);
380 void stop();
381 bool is_running() const { return running_.load(); }
382
383 private:
384 bool write_result(VerifyResult result);
385 bool ensure_fifo_open();
386 void close_fifo_locked();
387 bool fail_fifo_locked(const std::string& message);
388
390
391 std::mutex fifo_mutex_;
392 std::string fifo_path_;
393 int fifo_fd_ = -1;
396};
397#endif // __wasm__
398
404 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkBatchVerifierStart";
405
406 struct Response {
407 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkBatchVerifierStartResponse";
408 void msgpack(auto&& pack_fn) { pack_fn(); }
409 bool operator==(const Response&) const = default;
410 };
411
412 std::vector<std::vector<uint8_t>> vks; // Serialized verification keys
413 uint32_t num_cores = 0; // 0 = auto
414 uint32_t batch_size = 8;
415 std::string fifo_path;
416
417 Response execute(BBApiRequest& request) &&;
419 bool operator==(const ChonkBatchVerifierStart&) const = default;
420};
421
427 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkBatchVerifierQueue";
428
429 struct Response {
430 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkBatchVerifierQueueResponse";
431 void msgpack(auto&& pack_fn) { pack_fn(); }
432 bool operator==(const Response&) const = default;
433 };
434
435 uint64_t request_id = 0;
436 uint32_t vk_index = 0;
437 std::vector<bb::fr> proof_fields;
438
439 Response execute(BBApiRequest& request) &&;
441 bool operator==(const ChonkBatchVerifierQueue&) const = default;
442};
443
449 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkBatchVerifierStop";
450
451 struct Response {
452 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ChonkBatchVerifierStopResponse";
453 void msgpack(auto&& pack_fn) { pack_fn(); }
454 bool operator==(const Response&) const = default;
455 };
456
457 Response execute(BBApiRequest& request) &&;
458 void msgpack(auto&& pack_fn) { pack_fn(); }
459 bool operator==(const ChonkBatchVerifierStop&) const = default;
460};
461
462} // 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 fail_request(uint64_t request_id, std::string error_message)
bool write_result(VerifyResult result)
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
bool fail_fifo_locked(const std::string &message)
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={}) &&
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.