Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
bbapi_shared.hpp
Go to the documentation of this file.
1#pragma once
20#ifdef STARKNET_GARAGA_FLAVORS
21#include "barretenberg/flavor/ultra_starknet_flavor.hpp"
22#include "barretenberg/flavor/ultra_starknet_zk_flavor.hpp"
23#endif
24#include <cstdint>
25#include <string>
26#include <vector>
27
28// The FIFO-streaming Chonk batch verifier service relies on POSIX named pipes and signals
29// (open/lstat/SIGPIPE), which are unavailable on wasm and on Windows (MinGW). Those platforms
30// compile throwing stubs instead. This macro guards the service across bbapi_shared.hpp,
31// bbapi_chonk.hpp, and bbapi_chonk.cpp.
32#if !defined(__wasm__) && !defined(_WIN32)
33#define BB_HAS_BATCH_VERIFIER_SERVICE
34#endif
35
36namespace bb::bbapi {
37
44template <typename VK> inline void validate_vk_size(const std::vector<uint8_t>& vk_bytes)
45{
46 const size_t expected_size = VK::calc_num_data_types() * sizeof(bb::fr);
47 if (vk_bytes.size() != expected_size) {
48 throw_or_abort("verification key has wrong size: expected " + std::to_string(expected_size) + ", got " +
49 std::to_string(vk_bytes.size()));
50 }
51}
52
57enum class VkPolicy {
58 DEFAULT, // Use the provided VK as-is (default behavior)
59 CHECK, // Verify the provided VK matches the computed VK, throw error if mismatch
60 RECOMPUTE, // Always ignore the provided VK and treat it as nullptr
61 REWRITE // Check the VK and rewrite the input file with correct VK if mismatch (for check command)
62};
63
75 std::string name;
76
83 std::vector<uint8_t> bytecode;
84
86 bool operator==(const CircuitInputNoVK& other) const = default;
87};
88
100 std::string name;
101
108 std::vector<uint8_t> bytecode;
109
114 std::vector<uint8_t> verification_key;
115
117 bool operator==(const CircuitInput& other) const = default;
118};
119
125 bool ipa_accumulation = false;
126
133 std::string oracle_hash_type = "poseidon2";
134
139 bool disable_zk = false;
140
141 // TODO(md): remove this once considered stable
143
145 bool operator==(const ProofSystemSettings& other) const = default;
146};
147
152
154{
155 if (type == "keccak") {
157 }
158 if (type == "starknet") {
160 }
161 return OracleHashType::POSEIDON2; // default
162}
163
167inline VkPolicy parse_vk_policy(const std::string& policy)
168{
169 if (policy == "check") {
170 return VkPolicy::CHECK;
171 }
172 if (policy == "recompute") {
173 return VkPolicy::RECOMPUTE;
174 }
175 if (policy == "rewrite") {
176 return VkPolicy::REWRITE;
177 }
178 return VkPolicy::DEFAULT; // default
179}
180
181#ifdef BB_HAS_BATCH_VERIFIER_SERVICE
182// Forward declaration — defined in bbapi_chonk.hpp
183class ChonkBatchVerifierService;
184#endif
185
188 // Name of the last loaded circuit
190 // Store the parsed constraint system to get ahead of parsing before accumulate
192 // Store the verification key passed with the circuit
193 std::vector<uint8_t> loaded_circuit_vk;
195 // Policy for handling verification keys during accumulation
197 // Error message - empty string means no error
198 std::string error_message;
199#ifdef BB_HAS_BATCH_VERIFIER_SERVICE
200 // Batch verifier service instance (persists across RPC calls)
201 std::shared_ptr<ChonkBatchVerifierService> batch_verifier_service;
202#endif
203};
204
209 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ErrorResponse";
210 std::string message;
212 bool operator==(const ErrorResponse&) const = default;
213};
214
218#define BBAPI_ERROR(request, msg) \
219 do { \
220 (request).error_message = (msg); \
221 return {}; \
222 } while (0)
223
224struct Shutdown {
225 static constexpr const char MSGPACK_SCHEMA_NAME[] = "Shutdown";
226 struct Response {
227 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ShutdownResponse";
228 // Empty response - success indicated by no exception
229 void msgpack(auto&& pack_fn) { pack_fn(); }
230 bool operator==(const Response&) const = default;
231 };
232 void msgpack(auto&& pack_fn) { pack_fn(); }
233 Response execute(const BBApiRequest&) && { return {}; }
234 bool operator==(const Shutdown&) const = default;
235};
236
249template <typename Flavor>
251 const std::vector<uint256_t>& proof)
252{
253 using FF = typename Flavor::FF;
254 // Reject non-canonical field encodings (values >= field modulus) to ensure consistent
255 // verifier behavior across native and Solidity targets.
256 for (const auto& val : public_inputs) {
257 if (val >= FF::modulus) {
258 throw_or_abort("Non-canonical public input: value >= field modulus");
259 }
260 }
261 for (const auto& val : proof) {
262 if (val >= FF::modulus) {
263 throw_or_abort("Non-canonical proof element: value >= field modulus");
264 }
265 }
267 result.reserve(public_inputs.size() + proof.size());
268 result.insert(result.end(), public_inputs.begin(), public_inputs.end());
269 result.insert(result.end(), proof.begin(), proof.end());
270 return result;
271}
272
276template <typename VK> std::vector<uint256_t> vk_to_uint256_fields(const VK& vk)
277{
278 auto fields = vk.to_field_elements();
279 if constexpr (std::is_same_v<decltype(fields), std::vector<uint256_t>>) {
280 return fields;
281 } else {
282 return std::vector<uint256_t>(fields.begin(), fields.end());
283 }
284}
285
294{
295 if (!settings.ipa_accumulation) {
296 return; // Not a rollup circuit, no validation needed
297 }
298
299 // Rollup circuits must use poseidon2 for efficient recursive verification
300 if (settings.oracle_hash_type != "poseidon2") {
301 throw_or_abort("Rollup circuits (ipa_accumulation=true) must use oracle_hash_type='poseidon2', got '" +
302 settings.oracle_hash_type + "'");
303 }
304}
305
324template <typename Operation> auto dispatch_by_settings(const ProofSystemSettings& settings, Operation&& operation)
325{
326 // Rollup circuits: UltraFlavor with RollupIO (includes IPA accumulation for ECCVM)
327 if (settings.ipa_accumulation) {
328 validate_rollup_settings(settings);
329 return operation.template operator()<UltraFlavor, RollupIO>();
330 }
331
332 // Non-rollup circuits: route based on oracle hash type and ZK setting
333 if (settings.oracle_hash_type == "poseidon2") {
334 if (settings.disable_zk) {
335 return operation.template operator()<UltraFlavor, DefaultIO>();
336 }
337 return operation.template operator()<UltraZKFlavor, DefaultIO>();
338 }
339
340 if (settings.oracle_hash_type == "keccak") {
341 if (settings.disable_zk) {
342 return operation.template operator()<UltraKeccakFlavor, DefaultIO>();
343 }
344 return operation.template operator()<UltraKeccakZKFlavor, DefaultIO>();
345 }
346
347#ifdef STARKNET_GARAGA_FLAVORS
348 if (settings.oracle_hash_type == "starknet") {
349 if (settings.disable_zk) {
350 return operation.template operator()<UltraStarknetFlavor, DefaultIO>();
351 }
352 return operation.template operator()<UltraStarknetZKFlavor, DefaultIO>();
353 }
354#endif
355
356 // Invalid configuration
357 throw_or_abort("Invalid proof system settings: oracle_hash_type='" + settings.oracle_hash_type +
358 "', disable_zk=" + std::to_string(settings.disable_zk) +
359 ", ipa_accumulation=" + std::to_string(settings.ipa_accumulation));
360}
361
362} // namespace bb::bbapi
Manages the data that is propagated on the public inputs of an application/function circuit.
typename Curve::ScalarField FF
The data that is propagated on the public inputs of a rollup circuit.
Child class of UltraFlavor that runs with ZK Sumcheck.
OracleHashType
Convert oracle hash type string to enum for internal use.
VkPolicy
Policy for handling verification keys during IVC accumulation.
void validate_rollup_settings(const ProofSystemSettings &settings)
Validate rollup circuit settings.
std::vector< uint256_t > vk_to_uint256_fields(const VK &vk)
Convert VK to uint256 field elements, handling flavor-specific return types.
VkPolicy parse_vk_policy(const std::string &policy)
Convert VK policy string to enum for internal use.
Flavor::Transcript::Proof concatenate_proof(const std::vector< uint256_t > &public_inputs, const std::vector< uint256_t > &proof)
Concatenate public inputs and proof into a complete proof for verification.
auto dispatch_by_settings(const ProofSystemSettings &settings, Operation &&operation)
Dispatch to the correct Flavor and IO type based on proof system settings.
OracleHashType parse_oracle_hash_type(const std::string &type)
void validate_vk_size(const std::vector< uint8_t > &vk_bytes)
Validate verification key size before deserialization.
field< Bn254FrParams > fr
Definition fr.hpp:155
VerifierCommitmentKey< Curve > vk
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::string to_string(bb::avm2::ValueTag tag)
std::shared_ptr< ChonkBatchVerifierService > batch_verifier_service
std::shared_ptr< ChonkStepProcessor > ivc_in_progress
std::vector< uint8_t > loaded_circuit_vk
std::optional< acir_format::AcirFormat > loaded_circuit_constraints
A circuit to be used in either ultrahonk or Chonk proving.
bool operator==(const CircuitInput &other) const =default
std::vector< uint8_t > verification_key
Verification key of the circuit. This could be derived, but it is more efficient to have it fixed ahe...
std::string name
Human-readable name for the circuit.
SERIALIZATION_FIELDS(name, bytecode, verification_key)
std::vector< uint8_t > bytecode
Serialized bytecode representation of the circuit.
A circuit to be used in either ultrahonk or chonk verification key derivation.
std::string name
Human-readable name for the circuit.
bool operator==(const CircuitInputNoVK &other) const =default
SERIALIZATION_FIELDS(name, bytecode)
std::vector< uint8_t > bytecode
Serialized bytecode representation of the circuit.
Error response returned when a command fails.
bool operator==(const ErrorResponse &) const =default
static constexpr const char MSGPACK_SCHEMA_NAME[]
bool ipa_accumulation
Optional flag to indicate if the proof should be generated with IPA accumulation (i....
SERIALIZATION_FIELDS(ipa_accumulation, oracle_hash_type, disable_zk, optimized_solidity_verifier)
bool operator==(const ProofSystemSettings &other) const =default
std::string oracle_hash_type
The oracle hash type to be used for the proof.
bool disable_zk
Flag to disable blinding of the proof. Useful for cases that don't require privacy,...
static constexpr const char MSGPACK_SCHEMA_NAME[]
bool operator==(const Response &) const =default
void msgpack(auto &&pack_fn)
void msgpack(auto &&pack_fn)
static constexpr const char MSGPACK_SCHEMA_NAME[]
bool operator==(const Shutdown &) const =default
Response execute(const BBApiRequest &) &&
static constexpr uint256_t modulus
void throw_or_abort(std::string const &err)
VectorField result