Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
flavor.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: not started, auditors: [], date: YYYY-MM-DD }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
5// =====================
6
72#pragma once
92
93#include <array>
94#include <concepts>
95#include <cstddef>
96#include <numeric>
97#include <utility>
98#include <vector>
99
100namespace bb {
101
102// Specifies the regions of the execution trace containing non-trivial wire values
104 void add_range(const size_t start, const size_t end)
105 {
106 BB_ASSERT_GTE(start, current_end, "Ranges should be non-overlapping and increasing.");
107 ranges.emplace_back(start, end);
108 for (size_t i = start; i < end; ++i) {
109 idxs.push_back(i);
110 }
111 current_end = end;
112 }
113
115 size_t get_idx(const size_t idx) const { return idxs[idx]; }
116 std::pair<size_t, size_t> get_range(const size_t idx) const { return ranges.at(idx); }
117 size_t size() const { return idxs.size(); }
118 size_t num_ranges() const { return ranges.size(); }
119
120 private:
121 std::vector<std::pair<size_t, size_t>> ranges; // active ranges [start_i, end_i) of the execution trace
122 std::vector<size_t> idxs; // full set of poly indices corresposponding to active ranges
123 size_t current_end{ 0 }; // end of last range; for ensuring monotonicity of ranges
124};
125
129struct MetaData {
130 size_t dyadic_size = 0; // power-of-2 size of the execution trace
133};
134
138template <typename Polynomial, size_t NUM_PRECOMPUTED_ENTITIES> struct PrecomputedData_ {
139 RefArray<Polynomial, NUM_PRECOMPUTED_ENTITIES> polynomials; // polys whose commitments comprise the VK
140 MetaData metadata; // execution trace metadata
141};
142
151template <typename PrecomputedCommitments, typename Transcript>
152class NativeVerificationKey_ : public PrecomputedCommitments {
153 public:
154 using Commitment = typename PrecomputedCommitments::DataType;
155 uint64_t log_circuit_size = 0;
156 uint64_t num_public_inputs = 0;
157 uint64_t pub_inputs_offset = 0;
158
159 bool operator==(const NativeVerificationKey_&) const = default;
160 virtual ~NativeVerificationKey_() = default;
162 NativeVerificationKey_(const size_t circuit_size, const size_t num_public_inputs)
163 {
164 this->log_circuit_size = numeric::get_msb(circuit_size);
165 this->num_public_inputs = num_public_inputs;
166 };
167
174 {
175 using namespace bb::field_conversion;
176
177 auto serialize = [](const auto& input, std::vector<typename Transcript::DataType>& buffer) {
179 buffer.insert(buffer.end(), input_fields.begin(), input_fields.end());
180 };
181
183
184 serialize(this->log_circuit_size, elements);
185 serialize(this->num_public_inputs, elements);
186 serialize(this->pub_inputs_offset, elements);
187
188 for (const Commitment& commitment : this->get_all()) {
189 serialize(commitment, elements);
190 }
191
192 return elements;
193 };
194
200 fr hash() const
201 {
202 // TODO(https://github.com/AztecProtocol/barretenberg/issues/1498): should hash be dependent on transcript?
203 fr vk_hash = Transcript::hash(this->to_field_elements());
204 return vk_hash;
205 }
206
219 virtual typename Transcript::DataType hash_through_transcript(const std::string& domain_separator,
220 Transcript& transcript) const
221 {
222 transcript.add_to_independent_hash_buffer(domain_separator + "vk_log_circuit_size", this->log_circuit_size);
223 transcript.add_to_independent_hash_buffer(domain_separator + "vk_num_public_inputs", this->num_public_inputs);
224 transcript.add_to_independent_hash_buffer(domain_separator + "vk_pub_inputs_offset", this->pub_inputs_offset);
225
226 for (const Commitment& commitment : this->get_all()) {
227 transcript.add_to_independent_hash_buffer(domain_separator + "vk_commitment", commitment);
228 }
229
230 return transcript.hash_independent_buffer();
231 }
232};
233
241template <typename Builder_, typename PrecomputedCommitments>
242class StdlibVerificationKey_ : public PrecomputedCommitments {
243 public:
244 using Builder = Builder_;
246 using Commitment = typename PrecomputedCommitments::DataType;
251
252 bool operator==(const StdlibVerificationKey_&) const = default;
253 virtual ~StdlibVerificationKey_() = default;
255 StdlibVerificationKey_(const size_t circuit_size, const size_t num_public_inputs)
256 {
257 this->log_circuit_size = numeric::get_msb(circuit_size);
258 this->num_public_inputs = num_public_inputs;
259 };
260
266 virtual std::vector<FF> to_field_elements() const
267 {
268 using namespace bb::stdlib::field_conversion;
269
270 auto serialize_to_field_buffer = []<typename T>(const T& input, std::vector<FF>& buffer) {
271 std::vector<FF> input_fields = convert_to_bn254_frs<Builder, T>(input);
272 buffer.insert(buffer.end(), input_fields.begin(), input_fields.end());
273 };
274
275 std::vector<FF> elements;
276
277 serialize_to_field_buffer(this->log_circuit_size, elements);
278 serialize_to_field_buffer(this->num_public_inputs, elements);
279 serialize_to_field_buffer(this->pub_inputs_offset, elements);
280
281 for (const Commitment& commitment : this->get_all()) {
282 serialize_to_field_buffer(commitment, elements);
283 }
284
285 return elements;
286 };
287
295 {
297 return vk_hash;
298 }
299
312 virtual FF hash_through_transcript(const std::string& domain_separator, Transcript& transcript) const
313 {
314 transcript.add_to_independent_hash_buffer(domain_separator + "vk_log_circuit_size", this->log_circuit_size);
315 transcript.add_to_independent_hash_buffer(domain_separator + "vk_num_public_inputs", this->num_public_inputs);
316 transcript.add_to_independent_hash_buffer(domain_separator + "vk_pub_inputs_offset", this->pub_inputs_offset);
317 for (const Commitment& commitment : this->get_all()) {
318 transcript.add_to_independent_hash_buffer(domain_separator + "vk_commitment", commitment);
319 }
320 return transcript.hash_independent_buffer();
321 }
322};
323
324template <typename FF, typename VerificationKey> class VKAndHash_ {
325 public:
326 using Builder = VerificationKey::Builder;
327 using NativeVerificationKey = VerificationKey::NativeVerificationKey;
328
329 VKAndHash_() = default;
330 VKAndHash_(const std::shared_ptr<VerificationKey>& vk)
331 : vk(vk)
332 , hash(vk->hash())
333 {}
334
335 VKAndHash_(const std::shared_ptr<VerificationKey>& vk, const FF& hash)
336 : vk(vk)
337 , hash(hash)
338 {}
339
341 : vk(std::make_shared<VerificationKey>(&builder, native_vk))
342 , hash(FF::from_witness(&builder, native_vk->hash()))
343 {}
344 std::shared_ptr<VerificationKey> vk;
346};
347
348// Because of how Gemini is written, it is important to put the polynomials out in this order.
349auto get_unshifted_then_shifted(const auto& all_entities)
350{
351 return concatenate(all_entities.get_unshifted(), all_entities.get_shifted());
352};
353
359template <typename Tuple> constexpr size_t compute_max_partial_relation_length()
360{
362 return []<std::size_t... Is>(std::index_sequence<Is...>) {
363 return std::max({ std::tuple_element_t<Is, Tuple>::RELATION_LENGTH... });
364 }(seq);
365}
366
372template <typename Tuple> constexpr size_t compute_max_total_relation_length()
373{
375 return []<std::size_t... Is>(std::index_sequence<Is...>) {
376 return std::max({ std::tuple_element_t<Is, Tuple>::TOTAL_RELATION_LENGTH... });
377 }(seq);
378}
379
383template <typename Tuple> constexpr size_t compute_number_of_subrelations()
384{
386 return []<std::size_t... I>(std::index_sequence<I...>) {
387 return (0 + ... + std::tuple_element_t<I, Tuple>::SUBRELATION_PARTIAL_LENGTHS.size());
388 }(seq);
389}
390
399template <typename Tuple, size_t NUM_KEYS, bool optimized = false>
401{
403 return []<size_t... I>(std::index_sequence<I...>) {
404 if constexpr (optimized) {
406 typename std::tuple_element_t<I, Tuple>::template ProtogalaxyTupleOfUnivariatesOverSubrelations<
407 NUM_KEYS>{}...);
408 } else {
411 template ProtogalaxyTupleOfUnivariatesOverSubrelationsNoOptimisticSkipping<NUM_KEYS>{}...);
412 }
413 }(seq);
414}
415
422template <typename RelationsTuple> constexpr auto create_sumcheck_tuple_of_tuples_of_univariates()
423{
425 return []<size_t... I>(std::index_sequence<I...>) {
427 typename std::tuple_element_t<I, RelationsTuple>::SumcheckTupleOfUnivariatesOverSubrelations{}...);
428 }(seq);
429}
430
436template <typename RelationsTuple> constexpr auto create_tuple_of_arrays_of_values()
437{
439 return []<size_t... I>(std::index_sequence<I...>) {
441 typename std::tuple_element_t<I, RelationsTuple>::SumcheckArrayOfValuesOverSubrelations{}...);
442 }(seq);
443}
444
445} // namespace bb
446
447// Forward declare honk flavors
448namespace bb {
449class UltraFlavor;
450class UltraZKFlavor;
452class ECCVMFlavor;
454#ifdef STARKNET_GARAGA_FLAVORS
455class UltraStarknetFlavor;
456class UltraStarknetZKFlavor;
457#endif
459class MegaFlavor;
460class MegaZKFlavor;
461class TranslatorFlavor;
464class AvmRecursiveFlavor;
465
466template <typename BuilderType> class UltraRecursiveFlavor_;
467template <typename BuilderType> class UltraZKRecursiveFlavor_;
468template <typename BuilderType> class UltraRollupRecursiveFlavor_;
469template <typename BuilderType> class MegaRecursiveFlavor_;
470template <typename BuilderType> class MegaZKRecursiveFlavor_;
471
472namespace avm2 {
474}
475
476} // namespace bb
#define BB_ASSERT_GTE(left, right,...)
Definition assert.hpp:101
Common transcript class for both parties. Stores the data for the current round, as well as the manif...
DataType hash_independent_buffer()
Hashes the independent hash buffer and clears it.
void add_to_independent_hash_buffer(const std::string &label, const T &element)
Adds an element to an independent hash buffer.
static std::vector< DataType > serialize(const T &element)
Serialize a size_t to a vector of field elements.
TranscriptParams::DataType DataType
static DataType hash(const std::vector< DataType > &data)
Static hash method that forwards to TranscriptParams hash.
The recursive counterpart to the "native" Mega flavor.
Child class of MegaFlavor that runs with ZK Sumcheck. See more in Sumcheck Outline.
The recursive counterpart to the "native" MegaZKFlavor.
Base Native verification key class.
Definition flavor.hpp:152
virtual Transcript::DataType hash_through_transcript(const std::string &domain_separator, Transcript &transcript) const
Hashes the vk using the transcript's independent buffer and returns the hash.
Definition flavor.hpp:219
NativeVerificationKey_(const size_t circuit_size, const size_t num_public_inputs)
Definition flavor.hpp:162
fr hash() const
A model function to show how to compute the VK hash(without the Transcript abstracting things away)
Definition flavor.hpp:200
virtual std::vector< typename Transcript::DataType > to_field_elements() const
Serialize verification key to field elements.
Definition flavor.hpp:173
typename PrecomputedCommitments::DataType Commitment
Definition flavor.hpp:154
bool operator==(const NativeVerificationKey_ &) const =default
virtual ~NativeVerificationKey_()=default
A template class for a reference array. Behaves as if std::array<T&, N> was possible.
Definition ref_array.hpp:22
Base Stdlib verification key class.
Definition flavor.hpp:242
virtual FF hash_through_transcript(const std::string &domain_separator, Transcript &transcript) const
Hashes the vk using the transcript's independent buffer and returns the hash.
Definition flavor.hpp:312
bool operator==(const StdlibVerificationKey_ &) const =default
virtual std::vector< FF > to_field_elements() const
Serialize verification key to field elements.
Definition flavor.hpp:266
FF hash(Builder &builder)
A model function to show how to compute the VK hash (without the Transcript abstracting things away).
Definition flavor.hpp:294
typename PrecomputedCommitments::DataType Commitment
Definition flavor.hpp:246
virtual ~StdlibVerificationKey_()=default
StdlibVerificationKey_(const size_t circuit_size, const size_t num_public_inputs)
Definition flavor.hpp:255
The recursive counterpart of the native Translator flavor.
The recursive counterpart to the "native" Ultra flavor.
The recursive counterpart to the "native" UltraRollupFlavor.
Child class of UltraFlavor that runs with ZK Sumcheck.
The recursive counterpart to the Ultra flavor with ZK.
VerificationKey::NativeVerificationKey NativeVerificationKey
Definition flavor.hpp:327
VKAndHash_()=default
VKAndHash_(Builder &builder, const std::shared_ptr< NativeVerificationKey > &native_vk)
Definition flavor.hpp:340
std::shared_ptr< VerificationKey > vk
Definition flavor.hpp:344
VKAndHash_(const std::shared_ptr< VerificationKey > &vk, const FF &hash)
Definition flavor.hpp:335
VerificationKey::Builder Builder
Definition flavor.hpp:326
VKAndHash_(const std::shared_ptr< VerificationKey > &vk)
Definition flavor.hpp:330
static FF hash(const std::vector< FF > &input)
Hashes a vector of field elements.
constexpr size_t NUM_KEYS
AluTraceBuilder builder
Definition alu.test.cpp:123
uint8_t buffer[RANDOM_BUFFER_SIZE]
Definition engine.cpp:34
UltraKeccakFlavor::VerificationKey VerificationKey
constexpr T get_msb(const T in)
Definition get_msb.hpp:47
Entry point for Barretenberg command-line interface.
typename Flavor::FF FF
constexpr auto create_protogalaxy_tuple_of_tuples_of_univariates()
Utility function to construct a container for the subrelation accumulators of Protogalaxy folding.
Definition flavor.hpp:400
RefArray< T,(Ns+...)> constexpr concatenate(const RefArray< T, Ns > &... ref_arrays)
Concatenates multiple RefArray objects into a single RefArray.
constexpr size_t compute_number_of_subrelations()
Utility function to find the number of subrelations.
Definition flavor.hpp:383
constexpr auto create_tuple_of_arrays_of_values()
Construct tuple of arrays.
Definition flavor.hpp:436
constexpr size_t compute_max_partial_relation_length()
Utility function to find max PARTIAL_RELATION_LENGTH tuples of Relations.
Definition flavor.hpp:359
constexpr size_t compute_max_total_relation_length()
Utility function to find max TOTAL_RELATION_LENGTH among tuples of Relations.
Definition flavor.hpp:372
constexpr auto create_sumcheck_tuple_of_tuples_of_univariates()
Utility function to construct a container for the subrelation accumulators of sumcheck proving.
Definition flavor.hpp:422
auto get_unshifted_then_shifted(const auto &all_entities)
Definition flavor.hpp:349
STL namespace.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
TUPLET_INLINE constexpr auto make_tuple(Ts &&... args)
Definition tuplet.hpp:1062
std::pair< size_t, size_t > get_range(const size_t idx) const
Definition flavor.hpp:116
size_t get_idx(const size_t idx) const
Definition flavor.hpp:115
std::vector< std::pair< size_t, size_t > > ranges
Definition flavor.hpp:121
size_t size() const
Definition flavor.hpp:117
size_t num_ranges() const
Definition flavor.hpp:118
void add_range(const size_t start, const size_t end)
Definition flavor.hpp:104
std::vector< size_t > idxs
Definition flavor.hpp:122
std::vector< std::pair< size_t, size_t > > get_ranges() const
Definition flavor.hpp:114
Dyadic trace size and public inputs metadata; Common between prover and verifier keys.
Definition flavor.hpp:129
size_t pub_inputs_offset
Definition flavor.hpp:132
size_t num_public_inputs
Definition flavor.hpp:131
size_t dyadic_size
Definition flavor.hpp:130
The precomputed data needed to compute a Honk VK.
Definition flavor.hpp:138
RefArray< Polynomial, NUM_PRECOMPUTED_ENTITIES > polynomials
Definition flavor.hpp:139