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
28namespace bb::bbapi {
29
36template <typename VK> inline void validate_vk_size(const std::vector<uint8_t>& vk_bytes)
37{
38 const size_t expected_size = VK::calc_num_data_types() * sizeof(bb::fr);
39 if (vk_bytes.size() != expected_size) {
40 throw_or_abort("verification key has wrong size: expected " + std::to_string(expected_size) + ", got " +
41 std::to_string(vk_bytes.size()));
42 }
43}
44
49enum class VkPolicy {
50 DEFAULT, // Use the provided VK as-is (default behavior)
51 CHECK, // Verify the provided VK matches the computed VK, throw error if mismatch
52 RECOMPUTE, // Always ignore the provided VK and treat it as nullptr
53 REWRITE // Check the VK and rewrite the input file with correct VK if mismatch (for check command)
54};
55
67 std::string name;
68
75 std::vector<uint8_t> bytecode;
76
78 bool operator==(const CircuitInputNoVK& other) const = default;
79};
80
92 std::string name;
93
100 std::vector<uint8_t> bytecode;
101
106 std::vector<uint8_t> verification_key;
107
109 bool operator==(const CircuitInput& other) const = default;
110};
111
117 bool ipa_accumulation = false;
118
125 std::string oracle_hash_type = "poseidon2";
126
131 bool disable_zk = false;
132
133 // TODO(md): remove this once considered stable
135
137 bool operator==(const ProofSystemSettings& other) const = default;
138};
139
144
146{
147 if (type == "keccak") {
149 }
150 if (type == "starknet") {
152 }
153 return OracleHashType::POSEIDON2; // default
154}
155
159inline VkPolicy parse_vk_policy(const std::string& policy)
160{
161 if (policy == "check") {
162 return VkPolicy::CHECK;
163 }
164 if (policy == "recompute") {
165 return VkPolicy::RECOMPUTE;
166 }
167 if (policy == "rewrite") {
168 return VkPolicy::REWRITE;
169 }
170 return VkPolicy::DEFAULT; // default
171}
172
174 // Current depth of the IVC stack for this request
175 uint32_t ivc_stack_depth = 0;
177 // Name of the last loaded circuit
179 // Store the parsed constraint system to get ahead of parsing before accumulate
181 // Store the verification key passed with the circuit
182 std::vector<uint8_t> loaded_circuit_vk;
183 // Policy for handling verification keys during accumulation
185 // Error message - empty string means no error
186 std::string error_message;
187};
188
193 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ErrorResponse";
194 std::string message;
196 bool operator==(const ErrorResponse&) const = default;
197};
198
202#define BBAPI_ERROR(request, msg) \
203 do { \
204 (request).error_message = (msg); \
205 return {}; \
206 } while (0)
207
208struct Shutdown {
209 static constexpr const char MSGPACK_SCHEMA_NAME[] = "Shutdown";
210 struct Response {
211 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ShutdownResponse";
212 // Empty response - success indicated by no exception
213 void msgpack(auto&& pack_fn) { pack_fn(); }
214 bool operator==(const Response&) const = default;
215 };
216 void msgpack(auto&& pack_fn) { pack_fn(); }
217 Response execute(const BBApiRequest&) && { return {}; }
218 bool operator==(const Shutdown&) const = default;
219};
220
233template <typename Flavor>
235 const std::vector<uint256_t>& proof)
236{
237 typename Flavor::Transcript::Proof result;
238 result.reserve(public_inputs.size() + proof.size());
239 result.insert(result.end(), public_inputs.begin(), public_inputs.end());
240 result.insert(result.end(), proof.begin(), proof.end());
241 return result;
242}
243
247template <typename VK> std::vector<uint256_t> vk_to_uint256_fields(const VK& vk)
248{
249 auto fields = vk.to_field_elements();
250 if constexpr (std::is_same_v<decltype(fields), std::vector<uint256_t>>) {
251 return fields;
252 } else {
253 return std::vector<uint256_t>(fields.begin(), fields.end());
254 }
255}
256
265{
266 if (!settings.ipa_accumulation) {
267 return; // Not a rollup circuit, no validation needed
268 }
269
270 // Rollup circuits must use poseidon2 for efficient recursive verification
271 if (settings.oracle_hash_type != "poseidon2") {
272 throw_or_abort("Rollup circuits (ipa_accumulation=true) must use oracle_hash_type='poseidon2', got '" +
273 settings.oracle_hash_type + "'");
274 }
275}
276
295template <typename Operation> auto dispatch_by_settings(const ProofSystemSettings& settings, Operation&& operation)
296{
297 // Rollup circuits: UltraFlavor with RollupIO (includes IPA accumulation for ECCVM)
298 if (settings.ipa_accumulation) {
299 validate_rollup_settings(settings);
300 return operation.template operator()<UltraFlavor, RollupIO>();
301 }
302
303 // Non-rollup circuits: route based on oracle hash type and ZK setting
304 if (settings.oracle_hash_type == "poseidon2") {
305 if (settings.disable_zk) {
306 return operation.template operator()<UltraFlavor, DefaultIO>();
307 }
308 return operation.template operator()<UltraZKFlavor, DefaultIO>();
309 }
310
311 if (settings.oracle_hash_type == "keccak") {
312 if (settings.disable_zk) {
313 return operation.template operator()<UltraKeccakFlavor, DefaultIO>();
314 }
315 return operation.template operator()<UltraKeccakZKFlavor, DefaultIO>();
316 }
317
318#ifdef STARKNET_GARAGA_FLAVORS
319 if (settings.oracle_hash_type == "starknet") {
320 if (settings.disable_zk) {
321 return operation.template operator()<UltraStarknetFlavor, DefaultIO>();
322 }
323 return operation.template operator()<UltraStarknetZKFlavor, DefaultIO>();
324 }
325#endif
326
327 // Invalid configuration
328 throw_or_abort("Invalid proof system settings: oracle_hash_type='" + settings.oracle_hash_type +
329 "', disable_zk=" + std::to_string(settings.disable_zk) +
330 ", ipa_accumulation=" + std::to_string(settings.ipa_accumulation));
331}
332
333} // namespace bb::bbapi
Manages the data that is propagated on the public inputs of an application/function circuit.
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< IVCBase > 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.
MSGPACK_FIELDS(name, bytecode, verification_key)
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.
std::vector< uint8_t > bytecode
Serialized bytecode representation of the circuit.
A circuit to be used in either ultrahonk or chonk verification key derivation.
MSGPACK_FIELDS(name, bytecode)
std::string name
Human-readable name for the circuit.
bool operator==(const CircuitInputNoVK &other) const =default
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....
bool operator==(const ProofSystemSettings &other) const =default
MSGPACK_FIELDS(ipa_accumulation, oracle_hash_type, disable_zk, optimized_solidity_verifier)
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 &) &&
void throw_or_abort(std::string const &err)