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 ECCVMRecursiveFlavor;
54class TranslatorRecursiveFlavor;
55class MultilinearBatchingRecursiveFlavor;
56
57template <typename BuilderType> class UltraRecursiveFlavor_;
58template <typename BuilderType> class UltraZKRecursiveFlavor_;
59template <typename BuilderType> class MegaRecursiveFlavor_;
60template <typename BuilderType> class MegaZKRecursiveFlavor_;
61template <typename BuilderType> class MegaAvmRecursiveFlavor_;
62namespace avm2 {
63class AvmRecursiveFlavor;
64}
65} // namespace bb
66
67namespace bb {
68
69// ===== Trace metadata & precomputed data =====
70
74struct MetaData {
75 static constexpr size_t NUM_FIELDS = 3;
76 size_t dyadic_size = 0; // power-of-2 size of the execution trace
79};
80
84template <typename Polynomial, size_t NUM_PRECOMPUTED_ENTITIES> struct PrecomputedData_ {
85 RefArray<Polynomial, NUM_PRECOMPUTED_ENTITIES> polynomials; // polys whose commitments comprise the VK
86 MetaData metadata; // execution trace metadata
87};
88
89// ===== Fixed verification keys (ECCVM, Translator, AVM) =====
90
100template <typename PrecomputedCommitments, typename HashType, typename HardcodedVKAndHash>
101class FixedVKAndHash_ : public PrecomputedCommitments {
102 public:
103 using Commitment = typename PrecomputedCommitments::DataType;
104
105 bool operator==(const FixedVKAndHash_&) const = default;
106
107 // Default construct the fixed VK from hardcoded commitments and precomputed hash
109 : hash(HardcodedVKAndHash::vk_hash())
110 {
111 for (auto [vk_commitment, fixed_commitment] : zip_view(this->get_all(), HardcodedVKAndHash::get_all())) {
112 vk_commitment = fixed_commitment;
113 }
114 }
115
116 HashType get_hash() const { return hash; }
117
118 private:
119 HashType hash{};
120};
121
122// ===== Native verification key =====
123
134template <typename PrecomputedCommitments, typename Codec, typename HashFunction, typename CommitmentKey = void>
135class NativeVerificationKey_ : public PrecomputedCommitments {
136 public:
137 using Commitment = typename PrecomputedCommitments::DataType;
138 using DataType = typename Codec::DataType;
139 uint64_t log_circuit_size = 0;
140 uint64_t num_public_inputs = 0;
141 uint64_t pub_inputs_offset = 0;
142 bool operator==(const NativeVerificationKey_&) const = default;
143
144#ifndef NDEBUG
145 template <size_t NUM_PRECOMPUTED_ENTITIES, typename StringType>
148 {
149 bool is_equal = true;
150
151 if (this->log_circuit_size != other.log_circuit_size) {
152 info("Log circuit size mismatch: ", this->log_circuit_size, " vs ", other.log_circuit_size);
153 is_equal = false;
154 }
155
156 if (this->num_public_inputs != other.num_public_inputs) {
157 info("Num public inputs mismatch: ", this->num_public_inputs, " vs ", other.num_public_inputs);
158 is_equal = false;
159 }
160
161 if (this->pub_inputs_offset != other.pub_inputs_offset) {
162 info("Pub inputs offset mismatch: ", this->pub_inputs_offset, " vs ", other.pub_inputs_offset);
163 is_equal = false;
164 }
165
166 for (auto [this_comm, other_comm, label] : zip_view(this->get_all(), other.get_all(), commitment_labels)) {
167 if (this_comm != other_comm) {
168 info("Commitment mismatch: ", label);
169 is_equal = false;
170 }
171 }
172 return is_equal;
173 }
174#endif
175
176 virtual ~NativeVerificationKey_() = default;
178
183 template <typename PrecomputedData>
185 explicit NativeVerificationKey_(const PrecomputedData& precomputed)
186 : log_circuit_size(numeric::get_msb(precomputed.metadata.dyadic_size))
187 , num_public_inputs(precomputed.metadata.num_public_inputs)
188 , pub_inputs_offset(precomputed.metadata.pub_inputs_offset)
189 {
190 CommitmentKey commitment_key{ precomputed.metadata.dyadic_size };
191 for (auto [polynomial, commitment] : zip_view(precomputed.polynomials, this->get_all())) {
192 commitment = commitment_key.commit(polynomial);
193 }
194 }
195
200 static size_t calc_num_data_types()
201 {
202 size_t commitments_size = PrecomputedCommitments::size() * Codec::template calc_num_fields<Commitment>();
203 size_t metadata_size = MetaData::NUM_FIELDS * Codec::template calc_num_fields<uint64_t>();
204 return metadata_size + commitments_size;
205 }
206
212 virtual std::vector<DataType> to_field_elements() const
213 {
214
215 auto serialize = [](const auto& input, std::vector<DataType>& buffer) {
216 std::vector<DataType> input_fields = Codec::serialize_to_fields(input);
217 buffer.insert(buffer.end(), input_fields.begin(), input_fields.end());
218 };
219
220 std::vector<DataType> elements;
221
222 serialize(this->log_circuit_size, elements);
223 serialize(this->num_public_inputs, elements);
224 serialize(this->pub_inputs_offset, elements);
225
226 for (const Commitment& commitment : this->get_all()) {
227 serialize(commitment, elements);
228 }
229
230 return elements;
231 };
232
238 {
239
240 size_t idx = 0;
241 auto deserialize = [&idx, &elements]<typename T>(T& target) {
242 size_t size = Codec::template calc_num_fields<T>();
243 target = Codec::template deserialize_from_fields<T>(elements.subspan(idx, size));
244 idx += size;
245 };
246
247 deserialize(this->log_circuit_size);
248 deserialize(this->num_public_inputs);
249 deserialize(this->pub_inputs_offset);
250
251 for (Commitment& commitment : this->get_all()) {
252 deserialize(commitment);
253 }
254 return idx;
255 }
256
261 fr hash() const
262 {
263 fr vk_hash = HashFunction::hash(this->to_field_elements());
264 return vk_hash;
265 }
266
277 {
278 static constexpr bool in_circuit = InCircuit<DataType>;
279 std::vector<DataType> vk_elements;
280
281 // Tag, serialize, and append to vk_elements
282 auto tag_and_append = [&]<typename T>(const T& component) {
283 auto frs = bb::tag_and_serialize<in_circuit, Codec>(component, tag);
284 vk_elements.insert(vk_elements.end(), frs.begin(), frs.end());
285 };
286
287 // Tag and serialize VK metadata
288 tag_and_append(this->log_circuit_size);
289 tag_and_append(this->num_public_inputs);
290 tag_and_append(this->pub_inputs_offset);
291
292 // Tag and serialize VK commitments. Point-at-infinity canonicalization to (0,0) is handled by
293 // FrCodec::serialize_to_fields on the native path.
294 for (const Commitment& commitment : this->get_all()) {
295 tag_and_append(commitment);
296 }
297
298 // Sanitize free witness tags before hashing
299 bb::unset_free_witness_tags<in_circuit, DataType>(vk_elements);
300
301 // Hash the tagged elements directly
302 return HashFunction::hash(vk_elements);
303 }
304
311 template <typename Transcript> DataType hash_with_origin_tagging(const Transcript& transcript) const
312 {
313 const OriginTag tag = bb::extract_transcript_tag(transcript);
315 }
316};
317
318// ===== Fixed stdlib verification key (ECCVM, Translator, AVM) =====
319
329template <typename Builder_, typename PrecomputedCommitments, typename NativeVerificationKey>
330class FixedStdlibVKAndHash_ : public PrecomputedCommitments {
331 public:
332 using Builder = Builder_;
333 using Commitment = typename PrecomputedCommitments::DataType;
335
336 bool operator==(const FixedStdlibVKAndHash_&) const = default;
338
343 : hash(FF::from_witness(builder, native_key->get_hash()))
344 {
345 for (auto [native_comm, comm] : zip_view(native_key->get_all(), this->get_all())) {
346 comm = Commitment::from_witness(builder, native_comm);
347 }
348 // Fix all witnesses since fixed VKs are always constant
350 for (Commitment& commitment : this->get_all()) {
351 commitment.fix_witness();
352 }
353 }
354
355 FF get_hash() const { return hash; }
356
357 private:
359};
360
361// ===== Stdlib verification key =====
362
370template <typename Builder_, typename PrecomputedCommitments, typename NativeVerificationKey_ = void>
371class StdlibVerificationKey_ : public PrecomputedCommitments {
372 public:
373 using Builder = Builder_;
375 using Commitment = typename PrecomputedCommitments::DataType;
381
382 bool operator==(const StdlibVerificationKey_&) const = default;
383 virtual ~StdlibVerificationKey_() = default;
385
390 template <typename T = NativeVerificationKey_>
391 requires(!std::is_void_v<T>)
393 : log_circuit_size(FF::from_witness(builder, typename FF::native(native_key->log_circuit_size)))
394 , num_public_inputs(FF::from_witness(builder, typename FF::native(native_key->num_public_inputs)))
395 , pub_inputs_offset(FF::from_witness(builder, typename FF::native(native_key->pub_inputs_offset)))
396 {
397
398 for (auto [commitment, native_commitment] : zip_view(this->get_all(), native_key->get_all())) {
399 commitment = Commitment::from_witness(builder, native_commitment);
400 }
401 }
402
407 {
408 using Codec = stdlib::StdlibCodec<FF>;
409
410 size_t num_frs_read = 0;
411
412 this->log_circuit_size = Codec::template deserialize_from_frs<FF>(elements, num_frs_read);
413 this->num_public_inputs = Codec::template deserialize_from_frs<FF>(elements, num_frs_read);
414 this->pub_inputs_offset = Codec::template deserialize_from_frs<FF>(elements, num_frs_read);
415
416 for (Commitment& commitment : this->get_all()) {
417 commitment = Codec::template deserialize_from_frs<Commitment>(elements, num_frs_read);
418 }
419 }
420
425 const std::span<const uint32_t>& witness_indices)
426 {
427 std::vector<FF> vk_fields;
428 vk_fields.reserve(witness_indices.size());
429 for (const auto& idx : witness_indices) {
430 vk_fields.emplace_back(FF::from_witness_index(&builder, idx));
431 }
432 return StdlibVerificationKey_(vk_fields);
433 }
434
439 {
440 this->log_circuit_size.fix_witness();
441 this->num_public_inputs.fix_witness();
442 this->pub_inputs_offset.fix_witness();
443 for (Commitment& commitment : this->get_all()) {
444 commitment.fix_witness();
445 }
446 }
447
448#ifndef NDEBUG
453 template <typename T = NativeVerificationKey_>
454 requires(!std::is_void_v<T>)
455 T get_value() const
456 {
457 T native_vk;
458 native_vk.log_circuit_size = static_cast<uint64_t>(this->log_circuit_size.get_value());
459 native_vk.num_public_inputs = static_cast<uint64_t>(this->num_public_inputs.get_value());
460 native_vk.pub_inputs_offset = static_cast<uint64_t>(this->pub_inputs_offset.get_value());
461 for (auto [commitment, native_commitment] : zip_view(this->get_all(), native_vk.get_all())) {
462 native_commitment = commitment.get_value();
463 }
464 return native_vk;
465 }
466#endif
467
478 {
479 using Codec = stdlib::StdlibCodec<FF>;
480 static constexpr bool in_circuit = true; // StdlibVerificationKey_ is always in-circuit
481 std::vector<FF> vk_elements;
482
483 // Tag, serialize, and append to vk_elements
484 auto append_tagged = [&]<typename T>(const T& component) {
485 auto frs = bb::tag_and_serialize<in_circuit, Codec>(component, tag);
486 vk_elements.insert(vk_elements.end(), frs.begin(), frs.end());
487 };
488
489 // Tag and serialize VK metadata
490 append_tagged(this->log_circuit_size);
491 append_tagged(this->num_public_inputs);
492 append_tagged(this->pub_inputs_offset);
493
494 // Tag and serialize VK commitments.
495 // Note that commitments have been already deserialized and the point at infinity is constrained to (0,0)).
496 for (const Commitment& commitment : this->get_all()) {
497 append_tagged(commitment);
498 }
499
500 // Sanitize free witness tags before hashing
501 bb::unset_free_witness_tags<in_circuit, FF>(vk_elements);
502
503 // Hash the tagged elements directly
504 return stdlib::poseidon2<Builder>::hash(vk_elements);
505 }
506
513 template <typename Transcript> FF hash_with_origin_tagging(const Transcript& transcript) const
514 {
515 const OriginTag tag = bb::extract_transcript_tag(transcript);
517 }
518};
519
520// ===== VK + hash wrapper =====
521
541template <typename FF, typename VerificationKey> class VKAndHash_ {
542 public:
543 template <typename T = VerificationKey>
544 using Builder = typename std::enable_if_t<requires { typename T::Builder; }, T>::Builder;
545
546 template <typename T = VerificationKey>
548 typename std::enable_if_t<requires { typename T::NativeVerificationKey; }, T>::NativeVerificationKey;
549
550 VKAndHash_() = default;
551
555 VKAndHash_(const std::shared_ptr<VerificationKey>& vk)
556 : vk(vk)
557 , hash(vk->hash())
558 {}
559
563 VKAndHash_(const std::shared_ptr<VerificationKey>& vk, const FF& hash)
564 : vk(vk)
565 , hash(hash)
566 {}
567
571 template <typename VK = VerificationKey,
572 typename B = typename VK::Builder,
573 typename NVK = typename VK::NativeVerificationKey>
575 : vk(std::make_shared<VerificationKey>(&builder, native_vk))
576 , hash(FF::from_witness(&builder, native_vk->hash()))
577 {}
578 std::shared_ptr<VerificationKey> vk;
580};
581
582// ===== NativeVerificationKey_ Serde =====
583
584template <typename PrecomputedCommitments, typename Codec, typename HashFunction, typename CommitmentKey>
585inline void read(uint8_t const*& it,
587{
588 using serialize::read;
590
591 // Get the size directly from the static method
592 size_t num_frs = VK::calc_num_data_types();
593
594 // Read exactly num_frs field elements from the buffer
595 std::vector<typename Codec::DataType> field_elements(num_frs);
596 for (auto& element : field_elements) {
597 read(it, element);
598 }
599 // Then use from_field_elements to populate the verification key
600 vk.from_field_elements(field_elements);
601}
602
603template <typename PrecomputedCommitments, typename Codec, typename HashFunction, typename CommitmentKey>
604inline void write(std::vector<uint8_t>& buf,
606{
607 using serialize::write;
609
610 size_t before = buf.size();
611 // Convert to field elements and write them directly without length prefix
612 auto field_elements = vk.to_field_elements();
613 for (const auto& element : field_elements) {
614 write(buf, element);
615 }
616 size_t after = buf.size();
617 size_t num_frs = VK::calc_num_data_types();
618 BB_ASSERT_EQ(after - before, num_frs * sizeof(bb::fr), "VK serialization mismatch");
619}
620
621} // 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) 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:330
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:342
typename PrecomputedCommitments::DataType Commitment
Definition flavor.hpp:333
bool operator==(const FixedStdlibVKAndHash_ &) const =default
Simple verification key class for fixed-size circuits (ECCVM, Translator, AVM).
Definition flavor.hpp:101
bool operator==(const FixedVKAndHash_ &) const =default
HashType get_hash() const
Definition flavor.hpp:116
typename PrecomputedCommitments::DataType Commitment
Definition flavor.hpp:103
Base Native verification key class.
Definition flavor.hpp:135
static size_t calc_num_data_types()
Calculate the number of field elements needed for serialization.
Definition flavor.hpp:200
bool operator==(const NativeVerificationKey_ &) const =default
bool compare(const NativeVerificationKey_ &other, RefArray< StringType, NUM_PRECOMPUTED_ENTITIES > commitment_labels) const
Definition flavor.hpp:146
typename Codec::DataType DataType
Definition flavor.hpp:138
virtual DataType hash_with_origin_tagging(const OriginTag &tag) const
Tag VK components and hash.
Definition flavor.hpp:276
fr hash() const
Compute VK hash.
Definition flavor.hpp:261
DataType hash_with_origin_tagging(const Transcript &transcript) const
An overload that accepts a transcript and extracts the tag internally.
Definition flavor.hpp:311
size_t from_field_elements(const std::span< const DataType > &elements)
Populate verification key from field elements.
Definition flavor.hpp:237
NativeVerificationKey_(const PrecomputedData &precomputed)
Construct VK from precomputed data by committing to polynomials.
Definition flavor.hpp:185
virtual ~NativeVerificationKey_()=default
typename PrecomputedCommitments::DataType Commitment
Definition flavor.hpp:137
virtual std::vector< DataType > to_field_elements() const
Serialize verification key to field elements.
Definition flavor.hpp:212
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:371
bool operator==(const StdlibVerificationKey_ &) const =default
T get_value() const
Get the native verification key corresponding to this stdlib verification key.
Definition flavor.hpp:455
typename PrecomputedCommitments::DataType Commitment
Definition flavor.hpp:375
void fix_witness()
Fixes witnesses of VK to be constants.
Definition flavor.hpp:438
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:392
StdlibVerificationKey_(std::span< FF > elements)
Deserialize a verification key from a vector of field elements.
Definition flavor.hpp:406
FF hash_with_origin_tagging(const Transcript &transcript) const
An overload that accepts a transcript and extracts the tag internally.
Definition flavor.hpp:513
virtual FF hash_with_origin_tagging(const OriginTag &tag) const
Tag VK components and hash.
Definition flavor.hpp:477
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:424
Wrapper holding a verification key and its precomputed hash.
Definition flavor.hpp:541
VKAndHash_(B &builder, const std::shared_ptr< NVK > &native_vk)
Construct stdlib VKAndHash from a native VK (recursive verification keys only).
Definition flavor.hpp:574
VKAndHash_()=default
typename std::enable_if_t< requires { typename T::NativeVerificationKey NativeVerificationKey
Definition flavor.hpp:548
std::shared_ptr< VerificationKey > vk
Definition flavor.hpp:578
typename std::enable_if_t< requires { typename T::Builder Builder
Definition flavor.hpp:544
VKAndHash_(const std::shared_ptr< VerificationKey > &vk, const FF &hash)
Construct from VK and pre-provided hash.
Definition flavor.hpp:563
VKAndHash_(const std::shared_ptr< VerificationKey > &vk)
Construct from VK, auto-computing the hash.
Definition flavor.hpp:555
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:836
static field_t from_witness(Builder *ctx, const bb::fr &input)
Definition field.hpp:466
#define info(...)
Definition log.hpp:93
AluTraceBuilder builder
Definition alu.test.cpp:124
uint8_t const * buf
Definition data_store.hpp:9
std::unique_ptr< uint8_t[]> buffer
Definition engine.cpp:50
UltraKeccakFlavor::VerificationKey VerificationKey
constexpr T get_msb(const T in)
Definition get_msb.hpp:49
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 MSGPACK_FIEL...
void write(auto &buf, const msgpack_concepts::HasMsgPack auto &obj)
Automatically derived write for any object that defines .msgpack() (implicitly defined by MSGPACK_FIE...
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:74
size_t pub_inputs_offset
Definition flavor.hpp:78
size_t num_public_inputs
Definition flavor.hpp:77
static constexpr size_t NUM_FIELDS
Definition flavor.hpp:75
size_t dyadic_size
Definition flavor.hpp:76
The precomputed data needed to compute a Honk VK.
Definition flavor.hpp:84
RefArray< Polynomial, NUM_PRECOMPUTED_ENTITIES > polynomials
Definition flavor.hpp:85