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
173#ifndef __wasm__
174// Forward declaration — defined in bbapi_chonk.hpp
175class ChonkBatchVerifierService;
176#endif
177
179 // Current depth of the IVC stack for this request
180 uint32_t ivc_stack_depth = 0;
182 // Name of the last loaded circuit
184 // Store the parsed constraint system to get ahead of parsing before accumulate
186 // Store the verification key passed with the circuit
187 std::vector<uint8_t> loaded_circuit_vk;
188 // Policy for handling verification keys during accumulation
190 // Error message - empty string means no error
191 std::string error_message;
192#ifndef __wasm__
193 // Batch verifier service instance (persists across RPC calls)
195#endif
196};
197
202 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ErrorResponse";
203 std::string message;
205 bool operator==(const ErrorResponse&) const = default;
206};
207
211#define BBAPI_ERROR(request, msg) \
212 do { \
213 (request).error_message = (msg); \
214 return {}; \
215 } while (0)
216
217struct Shutdown {
218 static constexpr const char MSGPACK_SCHEMA_NAME[] = "Shutdown";
219 struct Response {
220 static constexpr const char MSGPACK_SCHEMA_NAME[] = "ShutdownResponse";
221 // Empty response - success indicated by no exception
222 void msgpack(auto&& pack_fn) { pack_fn(); }
223 bool operator==(const Response&) const = default;
224 };
225 void msgpack(auto&& pack_fn) { pack_fn(); }
226 Response execute(const BBApiRequest&) && { return {}; }
227 bool operator==(const Shutdown&) const = default;
228};
229
242template <typename Flavor>
244 const std::vector<uint256_t>& proof)
245{
246 using FF = typename Flavor::FF;
247 // Reject non-canonical field encodings (values >= field modulus) to ensure consistent
248 // verifier behavior across native and Solidity targets.
249 for (const auto& val : public_inputs) {
250 if (val >= FF::modulus) {
251 throw_or_abort("Non-canonical public input: value >= field modulus");
252 }
253 }
254 for (const auto& val : proof) {
255 if (val >= FF::modulus) {
256 throw_or_abort("Non-canonical proof element: value >= field modulus");
257 }
258 }
259 typename Flavor::Transcript::Proof result;
260 result.reserve(public_inputs.size() + proof.size());
261 result.insert(result.end(), public_inputs.begin(), public_inputs.end());
262 result.insert(result.end(), proof.begin(), proof.end());
263 return result;
264}
265
269template <typename VK> std::vector<uint256_t> vk_to_uint256_fields(const VK& vk)
270{
271 auto fields = vk.to_field_elements();
272 if constexpr (std::is_same_v<decltype(fields), std::vector<uint256_t>>) {
273 return fields;
274 } else {
275 return std::vector<uint256_t>(fields.begin(), fields.end());
276 }
277}
278
287{
288 if (!settings.ipa_accumulation) {
289 return; // Not a rollup circuit, no validation needed
290 }
291
292 // Rollup circuits must use poseidon2 for efficient recursive verification
293 if (settings.oracle_hash_type != "poseidon2") {
294 throw_or_abort("Rollup circuits (ipa_accumulation=true) must use oracle_hash_type='poseidon2', got '" +
295 settings.oracle_hash_type + "'");
296 }
297}
298
317template <typename Operation> auto dispatch_by_settings(const ProofSystemSettings& settings, Operation&& operation)
318{
319 // Rollup circuits: UltraFlavor with RollupIO (includes IPA accumulation for ECCVM)
320 if (settings.ipa_accumulation) {
321 validate_rollup_settings(settings);
322 return operation.template operator()<UltraFlavor, RollupIO>();
323 }
324
325 // Non-rollup circuits: route based on oracle hash type and ZK setting
326 if (settings.oracle_hash_type == "poseidon2") {
327 if (settings.disable_zk) {
328 return operation.template operator()<UltraFlavor, DefaultIO>();
329 }
330 return operation.template operator()<UltraZKFlavor, DefaultIO>();
331 }
332
333 if (settings.oracle_hash_type == "keccak") {
334 if (settings.disable_zk) {
335 return operation.template operator()<UltraKeccakFlavor, DefaultIO>();
336 }
337 return operation.template operator()<UltraKeccakZKFlavor, DefaultIO>();
338 }
339
340#ifdef STARKNET_GARAGA_FLAVORS
341 if (settings.oracle_hash_type == "starknet") {
342 if (settings.disable_zk) {
343 return operation.template operator()<UltraStarknetFlavor, DefaultIO>();
344 }
345 return operation.template operator()<UltraStarknetZKFlavor, DefaultIO>();
346 }
347#endif
348
349 // Invalid configuration
350 throw_or_abort("Invalid proof system settings: oracle_hash_type='" + settings.oracle_hash_type +
351 "', disable_zk=" + std::to_string(settings.disable_zk) +
352 ", ipa_accumulation=" + std::to_string(settings.ipa_accumulation));
353}
354
355} // 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< 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.
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)