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
19#ifdef STARKNET_GARAGA_FLAVORS
20#include "barretenberg/flavor/ultra_starknet_flavor.hpp"
21#include "barretenberg/flavor/ultra_starknet_zk_flavor.hpp"
22#endif
23#include <cstdint>
24#include <memory>
25#include <optional>
26#include <string>
27#include <vector>
28
29namespace bb::bbapi {
30
37template <typename VK> inline void validate_vk_size(const std::vector<uint8_t>& vk_bytes)
38{
39 const size_t expected_size = VK::calc_num_data_types() * sizeof(bb::fr);
40 if (vk_bytes.size() != expected_size) {
41 throw_or_abort("verification key has wrong size: expected " + std::to_string(expected_size) + ", got " +
42 std::to_string(vk_bytes.size()));
43 }
44}
45
50enum class VkPolicy {
51 DEFAULT, // Use the provided VK as-is (default behavior)
52 CHECK, // Verify the provided VK matches the computed VK, throw error if mismatch
53 RECOMPUTE, // Always ignore the provided VK and treat it as nullptr
54 REWRITE // Check the VK and rewrite the input file with correct VK if mismatch (for check command)
55};
56
60inline VkPolicy parse_vk_policy(const std::string& policy)
61{
62 if (policy == "check") {
63 return VkPolicy::CHECK;
64 }
65 if (policy == "recompute") {
67 }
68 if (policy == "rewrite") {
69 return VkPolicy::REWRITE;
70 }
71 return VkPolicy::DEFAULT; // default
72}
73
74#ifndef __wasm__
75// Forward declaration — defined in bbapi_chonk.hpp
76class ChonkBatchVerifierService;
77#endif
78
80 // Current depth of the IVC stack for this request
81 uint32_t ivc_stack_depth = 0;
83 // Name of the last loaded circuit
85 // Store the parsed constraint system to get ahead of parsing before accumulate
87 // Store the verification key passed with the circuit
88 std::vector<uint8_t> loaded_circuit_vk;
89 // Policy for handling verification keys during accumulation
91 // Error message - empty string means no error
92 std::string error_message;
93#ifndef __wasm__
94 // Batch verifier service instance (persists across RPC calls)
96#endif
97};
98
102#define BBAPI_ERROR(request, msg) \
103 do { \
104 (request).error_message = (msg); \
105 return {}; \
106 } while (0)
107
120template <typename Flavor>
122 const std::vector<uint256_t>& proof)
123{
124 using FF = typename Flavor::FF;
125 // Reject non-canonical field encodings (values >= field modulus) to ensure consistent
126 // verifier behavior across native and Solidity targets.
127 for (const auto& val : public_inputs) {
128 if (val >= FF::modulus) {
129 throw_or_abort("Non-canonical public input: value >= field modulus");
130 }
131 }
132 for (const auto& val : proof) {
133 if (val >= FF::modulus) {
134 throw_or_abort("Non-canonical proof element: value >= field modulus");
135 }
136 }
137 typename Flavor::Transcript::Proof result;
138 result.reserve(public_inputs.size() + proof.size());
139 result.insert(result.end(), public_inputs.begin(), public_inputs.end());
140 result.insert(result.end(), proof.begin(), proof.end());
141 return result;
142}
143
147template <typename VK> std::vector<uint256_t> vk_to_uint256_fields(const VK& vk)
148{
149 auto fields = vk.to_field_elements();
150 if constexpr (std::is_same_v<decltype(fields), std::vector<uint256_t>>) {
151 return fields;
152 } else {
153 return std::vector<uint256_t>(fields.begin(), fields.end());
154 }
155}
156
164template <typename Settings> inline void validate_rollup_settings(const Settings& settings)
165{
166 if (!settings.ipa_accumulation) {
167 return; // Not a rollup circuit, no validation needed
168 }
169
170 // Rollup circuits must use poseidon2 for efficient recursive verification
171 if (settings.oracle_hash_type != "poseidon2") {
172 throw_or_abort("Rollup circuits (ipa_accumulation=true) must use oracle_hash_type='poseidon2', got '" +
173 settings.oracle_hash_type + "'");
174 }
175}
176
195template <typename Settings, typename Operation>
196auto dispatch_by_settings(const Settings& settings, Operation&& operation)
197{
198 // Rollup circuits: UltraFlavor with RollupIO (includes IPA accumulation for ECCVM)
199 if (settings.ipa_accumulation) {
200 validate_rollup_settings(settings);
201 return operation.template operator()<UltraFlavor, RollupIO>();
202 }
203
204 // Non-rollup circuits: route based on oracle hash type and ZK setting
205 if (settings.oracle_hash_type == "poseidon2") {
206 if (settings.disable_zk) {
207 return operation.template operator()<UltraFlavor, DefaultIO>();
208 }
209 return operation.template operator()<UltraZKFlavor, DefaultIO>();
210 }
211
212 if (settings.oracle_hash_type == "keccak") {
213 if (settings.disable_zk) {
214 return operation.template operator()<UltraKeccakFlavor, DefaultIO>();
215 }
216 return operation.template operator()<UltraKeccakZKFlavor, DefaultIO>();
217 }
218
219#ifdef STARKNET_GARAGA_FLAVORS
220 if (settings.oracle_hash_type == "starknet") {
221 if (settings.disable_zk) {
222 return operation.template operator()<UltraStarknetFlavor, DefaultIO>();
223 }
224 return operation.template operator()<UltraStarknetZKFlavor, DefaultIO>();
225 }
226#endif
227
228 // Invalid configuration
229 throw_or_abort("Invalid proof system settings: oracle_hash_type='" + settings.oracle_hash_type +
230 "', disable_zk=" + std::to_string(settings.disable_zk) +
231 ", ipa_accumulation=" + std::to_string(settings.ipa_accumulation));
232}
233
234} // 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.
VkPolicy
Policy for handling verification keys during IVC accumulation.
auto dispatch_by_settings(const Settings &settings, Operation &&operation)
Dispatch to the correct Flavor and IO type based on proof system settings.
void validate_rollup_settings(const Settings &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.
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::string loaded_circuit_name
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
static constexpr uint256_t modulus
void throw_or_abort(std::string const &err)