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: Completed, auditors: [Sergei], commit: }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
22#pragma once
33
34#include <array>
35#include <cstddef>
36#include <vector>
37
38// ===== Flavor forward declarations =====
39namespace bb {
40class UltraFlavor;
41class UltraZKFlavor;
42class ECCVMFlavor;
43class UltraKeccakFlavor;
44#ifdef STARKNET_GARAGA_FLAVORS
45class UltraStarknetFlavor;
46class UltraStarknetZKFlavor;
47#endif
48class UltraKeccakZKFlavor;
49class MegaFlavor;
50class MegaZKFlavor;
51class MegaAvmFlavor;
52class TranslatorFlavor;
53class TranslatorShortMonomialFlavor;
54class ECCVMShortMonomialFlavor;
55class ECCVMRecursiveFlavor;
56class TranslatorRecursiveFlavor;
57class MultilinearBatchingRecursiveFlavor;
58
59template <typename BuilderType> class UltraRecursiveFlavor_;
60template <typename BuilderType> class UltraZKRecursiveFlavor_;
61template <typename BuilderType> class MegaRecursiveFlavor_;
62template <typename BuilderType> class MegaZKRecursiveFlavor_;
63template <typename BuilderType> class MegaAvmRecursiveFlavor_;
64namespace avm2 {
65class AvmRecursiveFlavor;
66}
67} // namespace bb
68
69namespace bb {
70
71// ===== Trace metadata & precomputed data =====
72
76struct MetaData {
77 static constexpr size_t NUM_FIELDS = 3;
78 size_t dyadic_size = 0; // power-of-2 size of the execution trace
81};
82
86template <typename Polynomial, size_t NUM_PRECOMPUTED_ENTITIES> struct PrecomputedData_ {
87 RefArray<Polynomial, NUM_PRECOMPUTED_ENTITIES> polynomials; // polys whose commitments comprise the VK
88 MetaData metadata; // execution trace metadata
89};
90
91// ===== Fixed verification keys (ECCVM, Translator, AVM) =====
92
102template <typename PrecomputedCommitments, typename HashType, typename HardcodedVKAndHash>
103class FixedVKAndHash_ : public PrecomputedCommitments {
104 public:
105 using Commitment = typename PrecomputedCommitments::DataType;
106
107 bool operator==(const FixedVKAndHash_&) const = default;
108
109 // Default construct the fixed VK from hardcoded commitments and precomputed hash
111 : hash(HardcodedVKAndHash::vk_hash())
112 {
113 for (auto [vk_commitment, fixed_commitment] : zip_view(this->get_all(), HardcodedVKAndHash::get_all())) {
114 vk_commitment = fixed_commitment;
115 }
116 }
117
118 HashType get_hash() const { return hash; }
119
120 private:
121 HashType hash{};
122};
123
124// ===== Native verification key =====
125
136template <typename PrecomputedCommitments, typename Codec, typename HashFunction, typename CommitmentKey = void>
137class NativeVerificationKey_ : public PrecomputedCommitments {
138 public:
139 using Commitment = typename PrecomputedCommitments::DataType;
140 using DataType = typename Codec::DataType;
141 uint64_t log_circuit_size = 0;
142 uint64_t num_public_inputs = 0;
143 uint64_t pub_inputs_offset = 0;
144 bool operator==(const NativeVerificationKey_&) const = default;
145
146#ifndef NDEBUG
147 template <size_t NUM_PRECOMPUTED_ENTITIES, typename StringType>
150 {
151 bool is_equal = true;
152
153 if (this->log_circuit_size != other.log_circuit_size) {
154 info("Log circuit size mismatch: ", this->log_circuit_size, " vs ", other.log_circuit_size);
155 is_equal = false;
156 }
157
158 if (this->num_public_inputs != other.num_public_inputs) {
159 info("Num public inputs mismatch: ", this->num_public_inputs, " vs ", other.num_public_inputs);
160 is_equal = false;
161 }
162
163 if (this->pub_inputs_offset != other.pub_inputs_offset) {
164 info("Pub inputs offset mismatch: ", this->pub_inputs_offset, " vs ", other.pub_inputs_offset);
165 is_equal = false;
166 }
167
168 for (auto [this_comm, other_comm, label] : zip_view(this->get_all(), other.get_all(), commitment_labels)) {
169 if (this_comm != other_comm) {
170 info("Commitment mismatch: ", label);
171 is_equal = false;
172 }
173 }
174 return is_equal;
175 }
176#endif
177
178 virtual ~NativeVerificationKey_() = default;
180
185 template <typename PrecomputedData>
187 explicit NativeVerificationKey_(const PrecomputedData& precomputed)
188 : log_circuit_size(numeric::get_msb(precomputed.metadata.dyadic_size))
189 , num_public_inputs(precomputed.metadata.num_public_inputs)
190 , pub_inputs_offset(precomputed.metadata.pub_inputs_offset)
191 {
192 CommitmentKey commitment_key{ precomputed.metadata.dyadic_size };
193 for (auto [polynomial, commitment] : zip_view(precomputed.polynomials, this->get_all())) {
194 commitment = commitment_key.commit(polynomial);
195 }
196 }
197
202 static constexpr size_t calc_num_data_types()
203 {
204 size_t commitments_size = PrecomputedCommitments::size() * Codec::template calc_num_fields<Commitment>();
205 size_t metadata_size = MetaData::NUM_FIELDS * Codec::template calc_num_fields<uint64_t>();
206 return metadata_size + commitments_size;
207 }
208
214 virtual std::vector<DataType> to_field_elements() const
215 {
216
217 auto serialize = [](const auto& input, std::vector<DataType>& buffer) {
218 std::vector<DataType> input_fields = Codec::serialize_to_fields(input);
219 buffer.insert(buffer.end(), input_fields.begin(), input_fields.end());
220 };
221
222 std::vector<DataType> elements;
223
224 serialize(this->log_circuit_size, elements);
225 serialize(this->num_public_inputs, elements);
226 serialize(this->pub_inputs_offset, elements);
227
228 for (const Commitment& commitment : this->get_all()) {
229 serialize(commitment, elements);
230 }
231
232 return elements;
233 };
234
240 {
241
242 size_t idx = 0;
243 auto deserialize = [&idx, &elements]<typename T>(T& target) {
244 size_t size = Codec::template calc_num_fields<T>();
245 target = Codec::template deserialize_from_fields<T>(elements.subspan(idx, size));
246 idx += size;
247 };
248
249 deserialize(this->log_circuit_size);
250 deserialize(this->num_public_inputs);
251 deserialize(this->pub_inputs_offset);
252
253 for (Commitment& commitment : this->get_all()) {
254 deserialize(commitment);
255 }
256 return idx;
257 }
258
263 fr hash() const
264 {
265 fr vk_hash = HashFunction::hash(this->to_field_elements());
266 return vk_hash;
267 }
268
279 {
280 static constexpr bool in_circuit = InCircuit<DataType>;
281 std::vector<DataType> vk_elements;
282
283 // Tag, serialize, and append to vk_elements
284 auto tag_and_append = [&]<typename T>(const T& component) {
285 auto frs = bb::tag_and_serialize<in_circuit, Codec>(component, tag);
286 vk_elements.insert(vk_elements.end(), frs.begin(), frs.end());
287 };
288
289 // Tag and serialize VK metadata
290 tag_and_append(this->log_circuit_size);
291 tag_and_append(this->num_public_inputs);
292 tag_and_append(this->pub_inputs_offset);
293
294 // Tag and serialize VK commitments. Point-at-infinity canonicalization to (0,0) is handled by
295 // FrCodec::serialize_to_fields on the native path.
296 for (const Commitment& commitment : this->get_all()) {
297 tag_and_append(commitment);
298 }
299
300 // Sanitize free witness tags before hashing
301 bb::unset_free_witness_tags<in_circuit, DataType>(vk_elements);
302
303 // Hash the tagged elements directly
304 return HashFunction::hash(vk_elements);
305 }
306
313 template <typename Transcript> DataType hash_with_origin_tagging(const Transcript& transcript) const
314 {
315 const OriginTag tag = bb::extract_transcript_tag(transcript);
317 }
318};
319
320// ===== Fixed stdlib verification key (ECCVM, Translator, AVM) =====
321
331template <typename Builder_, typename PrecomputedCommitments, typename NativeVerificationKey>
332class FixedStdlibVKAndHash_ : public PrecomputedCommitments {
333 public:
334 using Builder = Builder_;
335 using Commitment = typename PrecomputedCommitments::DataType;
337
338 bool operator==(const FixedStdlibVKAndHash_&) const = default;
340
345 : hash(FF::from_witness(builder, native_key->get_hash()))
346 {
347 for (auto [native_comm, comm] : zip_view(native_key->get_all(), this->get_all())) {
348 comm = Commitment::from_witness(builder, native_comm);
349 }
350 // Fix all witnesses since fixed VKs are always constant
352 for (Commitment& commitment : this->get_all()) {
353 commitment.fix_witness();
354 }
355 }
356
357 FF get_hash() const { return hash; }
358
359 private:
361};
362
363// ===== Stdlib verification key =====
364
372template <typename Builder_, typename PrecomputedCommitments, typename NativeVerificationKey_ = void>
373class StdlibVerificationKey_ : public PrecomputedCommitments {
374 public:
375 using Builder = Builder_;
377 using Commitment = typename PrecomputedCommitments::DataType;
383
384 bool operator==(const StdlibVerificationKey_&) const = default;
385 virtual ~StdlibVerificationKey_() = default;
387
392 template <typename T = NativeVerificationKey_>
393 requires(!std::is_void_v<T>)
395 : log_circuit_size(FF::from_witness(builder, typename FF::native(native_key->log_circuit_size)))
396 , num_public_inputs(FF::from_witness(builder, typename FF::native(native_key->num_public_inputs)))
397 , pub_inputs_offset(FF::from_witness(builder, typename FF::native(native_key->pub_inputs_offset)))
398 {
399
400 for (auto [commitment, native_commitment] : zip_view(this->get_all(), native_key->get_all())) {
401 commitment = Commitment::from_witness(builder, native_commitment);
402 }
403 }
404
409 {
410 using Codec = stdlib::StdlibCodec<FF>;
411
412 size_t num_frs_read = 0;
413
414 this->log_circuit_size = Codec::template deserialize_from_frs<FF>(elements, num_frs_read);
415 this->num_public_inputs = Codec::template deserialize_from_frs<FF>(elements, num_frs_read);
416 this->pub_inputs_offset = Codec::template deserialize_from_frs<FF>(elements, num_frs_read);
417
418 for (Commitment& commitment : this->get_all()) {
419 commitment = Codec::template deserialize_from_frs<Commitment>(elements, num_frs_read);
420 }
421 }
422
427 const std::span<const uint32_t>& witness_indices)
428 {
429 std::vector<FF> vk_fields;
430 vk_fields.reserve(witness_indices.size());
431 for (const auto& idx : witness_indices) {
432 vk_fields.emplace_back(FF::from_witness_index(&builder, idx));
433 }
434 return StdlibVerificationKey_(vk_fields);
435 }
436
441 {
442 this->log_circuit_size.fix_witness();
443 this->num_public_inputs.fix_witness();
444 this->pub_inputs_offset.fix_witness();
445 for (Commitment& commitment : this->get_all()) {
446 commitment.fix_witness();
447 }
448 }
449
450#ifndef NDEBUG
455 template <typename T = NativeVerificationKey_>
456 requires(!std::is_void_v<T>)
457 T get_value() const
458 {
459 T native_vk;
460 native_vk.log_circuit_size = static_cast<uint64_t>(this->log_circuit_size.get_value());
461 native_vk.num_public_inputs = static_cast<uint64_t>(this->num_public_inputs.get_value());
462 native_vk.pub_inputs_offset = static_cast<uint64_t>(this->pub_inputs_offset.get_value());
463 for (auto [commitment, native_commitment] : zip_view(this->get_all(), native_vk.get_all())) {
464 native_commitment = commitment.get_value();
465 }
466 return native_vk;
467 }
468#endif
469
480 {
481 using Codec = stdlib::StdlibCodec<FF>;
482 static constexpr bool in_circuit = true; // StdlibVerificationKey_ is always in-circuit
483 std::vector<FF> vk_elements;
484
485 // Tag, serialize, and append to vk_elements
486 auto append_tagged = [&]<typename T>(const T& component) {
487 auto frs = bb::tag_and_serialize<in_circuit, Codec>(component, tag);
488 vk_elements.insert(vk_elements.end(), frs.begin(), frs.end());
489 };
490
491 // Tag and serialize VK metadata
492 append_tagged(this->log_circuit_size);
493 append_tagged(this->num_public_inputs);
494 append_tagged(this->pub_inputs_offset);
495
496 // Tag and serialize VK commitments.
497 // Note that commitments have been already deserialized and the point at infinity is constrained to (0,0)).
498 for (const Commitment& commitment : this->get_all()) {
499 append_tagged(commitment);
500 }
501
502 // Sanitize free witness tags before hashing
503 bb::unset_free_witness_tags<in_circuit, FF>(vk_elements);
504
505 // Hash the tagged elements directly
506 return stdlib::poseidon2<Builder>::hash(vk_elements);
507 }
508
515 template <typename Transcript> FF hash_with_origin_tagging(const Transcript& transcript) const
516 {
517 const OriginTag tag = bb::extract_transcript_tag(transcript);
519 }
520};
521
522// ===== VK + hash wrapper =====
523
543template <typename FF, typename VerificationKey> class VKAndHash_ {
544 public:
545 template <typename T = VerificationKey>
546 using Builder = typename std::enable_if_t<requires { typename T::Builder; }, T>::Builder;
547
548 template <typename T = VerificationKey>
550 typename std::enable_if_t<requires { typename T::NativeVerificationKey; }, T>::NativeVerificationKey;
551
552 VKAndHash_() = default;
553
557 VKAndHash_(const std::shared_ptr<VerificationKey>& vk)
558 : vk(vk)
559 , hash(vk->hash())
560 {}
561
565 VKAndHash_(const std::shared_ptr<VerificationKey>& vk, const FF& hash)
566 : vk(vk)
567 , hash(hash)
568 {}
569
573 template <typename VK = VerificationKey,
574 typename B = typename VK::Builder,
575 typename NVK = typename VK::NativeVerificationKey>
577 : vk(std::make_shared<VerificationKey>(&builder, native_vk))
578 , hash(FF::from_witness(&builder, native_vk->hash()))
579 {}
580 std::shared_ptr<VerificationKey> vk;
582};
583
584// ===== NativeVerificationKey_ Serde =====
585
586template <typename PrecomputedCommitments, typename Codec, typename HashFunction, typename CommitmentKey>
587inline void read(uint8_t const*& it,
589{
590 using serialize::read;
592
593 // Get the size directly from the static method
594 size_t num_frs = VK::calc_num_data_types();
595
596 // Read exactly num_frs field elements from the buffer
597 std::vector<typename Codec::DataType> field_elements(num_frs);
598 for (auto& element : field_elements) {
599 read(it, element);
600 }
601 // Then use from_field_elements to populate the verification key
602 vk.from_field_elements(field_elements);
603}
604
605template <typename PrecomputedCommitments, typename Codec, typename HashFunction, typename CommitmentKey>
606inline void write(std::vector<uint8_t>& buf,
608{
609 using serialize::write;
611
612 size_t before = buf.size();
613 // Convert to field elements and write them directly without length prefix
614 auto field_elements = vk.to_field_elements();
615 for (const auto& element : field_elements) {
616 write(buf, element);
617 }
618 size_t after = buf.size();
619 size_t num_frs = VK::calc_num_data_types();
620 BB_ASSERT_EQ(after - before, num_frs * sizeof(bb::fr), "VK serialization mismatch");
621}
622
623} // namespace bb
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:83
Common transcript class for both parties. Stores the data for the current round, as well as the manif...
CommitmentKey object over a pairing group 𝔾₁.
Commitment commit(PolynomialSpan< const Fr > polynomial, bool has_duplicates_hint=false) const
Uses the ProverSRS to create a commitment to p(X)
Simple stdlib verification key class for fixed-size circuits (ECCVM, Translator, AVM).
Definition flavor.hpp:332
FixedStdlibVKAndHash_(Builder *builder, const std::shared_ptr< NativeVerificationKey > &native_key)
Construct from native verification key and fix all witnesses (VK is constant for fixed circuits)
Definition flavor.hpp:344
typename PrecomputedCommitments::DataType Commitment
Definition flavor.hpp:335
bool operator==(const FixedStdlibVKAndHash_ &) const =default
Simple verification key class for fixed-size circuits (ECCVM, Translator, AVM).
Definition flavor.hpp:103
bool operator==(const FixedVKAndHash_ &) const =default
HashType get_hash() const
Definition flavor.hpp:118
typename PrecomputedCommitments::DataType Commitment
Definition flavor.hpp:105
Base Native verification key class.
Definition flavor.hpp:137
bool operator==(const NativeVerificationKey_ &) const =default
bool compare(const NativeVerificationKey_ &other, RefArray< StringType, NUM_PRECOMPUTED_ENTITIES > commitment_labels) const
Definition flavor.hpp:148
static constexpr size_t calc_num_data_types()
Calculate the number of field elements needed for serialization.
Definition flavor.hpp:202
typename Codec::DataType DataType
Definition flavor.hpp:140
virtual DataType hash_with_origin_tagging(const OriginTag &tag) const
Tag VK components and hash.
Definition flavor.hpp:278
fr hash() const
Compute VK hash.
Definition flavor.hpp:263
DataType hash_with_origin_tagging(const Transcript &transcript) const
An overload that accepts a transcript and extracts the tag internally.
Definition flavor.hpp:313
size_t from_field_elements(const std::span< const DataType > &elements)
Populate verification key from field elements.
Definition flavor.hpp:239
NativeVerificationKey_(const PrecomputedData &precomputed)
Construct VK from precomputed data by committing to polynomials.
Definition flavor.hpp:187
virtual ~NativeVerificationKey_()=default
typename PrecomputedCommitments::DataType Commitment
Definition flavor.hpp:139
virtual std::vector< DataType > to_field_elements() const
Serialize verification key to field elements.
Definition flavor.hpp:214
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:373
bool operator==(const StdlibVerificationKey_ &) const =default
T get_value() const
Get the native verification key corresponding to this stdlib verification key.
Definition flavor.hpp:457
typename PrecomputedCommitments::DataType Commitment
Definition flavor.hpp:377
void fix_witness()
Fixes witnesses of VK to be constants.
Definition flavor.hpp:440
StdlibVerificationKey_(Builder *builder, const std::shared_ptr< T > &native_key)
Construct a new Verification Key with stdlib types from a provided native verification key.
Definition flavor.hpp:394
StdlibVerificationKey_(std::span< FF > elements)
Deserialize a verification key from a vector of field elements.
Definition flavor.hpp:408
FF hash_with_origin_tagging(const Transcript &transcript) const
An overload that accepts a transcript and extracts the tag internally.
Definition flavor.hpp:515
virtual FF hash_with_origin_tagging(const OriginTag &tag) const
Tag VK components and hash.
Definition flavor.hpp:479
virtual ~StdlibVerificationKey_()=default
static StdlibVerificationKey_ from_witness_indices(Builder &builder, const std::span< const uint32_t > &witness_indices)
Construct a VerificationKey from a set of corresponding witness indices.
Definition flavor.hpp:426
Wrapper holding a verification key and its precomputed hash.
Definition flavor.hpp:543
VKAndHash_(B &builder, const std::shared_ptr< NVK > &native_vk)
Construct stdlib VKAndHash from a native VK (recursive verification keys only).
Definition flavor.hpp:576
VKAndHash_()=default
typename std::enable_if_t< requires { typename T::NativeVerificationKey NativeVerificationKey
Definition flavor.hpp:550
std::shared_ptr< VerificationKey > vk
Definition flavor.hpp:580
typename std::enable_if_t< requires { typename T::Builder Builder
Definition flavor.hpp:546
VKAndHash_(const std::shared_ptr< VerificationKey > &vk, const FF &hash)
Construct from VK and pre-provided hash.
Definition flavor.hpp:565
VKAndHash_(const std::shared_ptr< VerificationKey > &vk)
Construct from VK, auto-computing the hash.
Definition flavor.hpp:557
static FF hash(const std::vector< FF > &input)
Hashes a vector of field elements.
static field_t from_witness_index(Builder *ctx, uint32_t witness_index)
Definition field.cpp:67
bb::fr get_value() const
Given a := *this, compute its value given by a.v * a.mul + a.add.
Definition field.cpp:838
static field_t from_witness(Builder *ctx, const bb::fr &input)
Definition field.hpp:467
#define info(...)
Definition log.hpp:93
AluTraceBuilder builder
Definition alu.test.cpp:124
std::unique_ptr< uint8_t[]> buffer
Definition engine.cpp:60
UltraKeccakFlavor::VerificationKey VerificationKey
constexpr T get_msb(const T in)
Definition get_msb.hpp:50
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
void read(B &it, field2< base_field, Params > &value)
void write(B &buf, field2< base_field, Params > const &value)
OriginTag extract_transcript_tag(const TranscriptType &transcript)
Extract origin tag context from a transcript.
VerifierCommitmentKey< Curve > vk
void read(auto &it, msgpack_concepts::HasMsgPack auto &obj)
Automatically derived read for any object that defines .msgpack() (implicitly defined by SERIALIZATIO...
void write(auto &buf, const msgpack_concepts::HasMsgPack auto &obj)
Automatically derived write for any object that defines .msgpack() (implicitly defined by SERIALIZATI...
STL namespace.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
StdlibCodec for in-circuit (recursive) verification transcript handling.
Dyadic trace size and public inputs metadata; Common between prover and verifier keys.
Definition flavor.hpp:76
size_t pub_inputs_offset
Definition flavor.hpp:80
size_t num_public_inputs
Definition flavor.hpp:79
static constexpr size_t NUM_FIELDS
Definition flavor.hpp:77
size_t dyadic_size
Definition flavor.hpp:78
The precomputed data needed to compute a Honk VK.
Definition flavor.hpp:86
RefArray< Polynomial, NUM_PRECOMPUTED_ENTITIES > polynomials
Definition flavor.hpp:87