Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
flavor.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <span>
5
12
16
21
24
25namespace bb::avm2 {
26
27// Metaprogramming to concatenate tuple types.
28template <typename... input_t> using tuple_cat_t = decltype(flat_tuple::tuple_cat(std::declval<input_t>()...));
29
30class AvmFlavor {
31 public:
35
44
45 // To help BB check if a flavor is AVM, even without including this flavor.
46 static constexpr bool IS_AVM = true;
47 // indicates when evaluating sumcheck, edges must be extended to be MAX_PARTIAL_RELATION_LENGTH
48 static constexpr bool USE_SHORT_MONOMIALS = false;
49 // This flavor would not be used with ZK Sumcheck
50 static constexpr bool HasZK = false;
51 // Padding in Sumcheck and Shplemini
52 static constexpr bool USE_PADDING = true;
53
57 static constexpr size_t NUM_WIRES = AvmFlavorVariables::NUM_WIRES;
59
60 // In the sumcheck univariate computation, we divide the trace in chunks and each chunk is
61 // evenly processed by all the threads. This constant defines the maximum number of rows
62 // that a given thread will process per chunk. This constant is assumed to be a power of 2
63 // greater or equal to 2.
64 // The current constant 32 is the result of time measurements using 16 threads and against
65 // bulk test v2. It was performed at a stage where the trace was not large.
66 // We note that all the experiments with constants below 256 did not exhibit any significant differences.
67 // TODO: Fine-tune the following constant when avm is close to completion.
68 static constexpr size_t MAX_CHUNK_THREAD_PORTION_SIZE = 32;
69
70 // Need to be templated for recursive verifier
72
74
75 // Need to be templated for recursive verifier
77
79
80 // Need to be templated for recursive verifier
83
84 static constexpr size_t NUM_SUBRELATIONS = compute_number_of_subrelations<Relations>();
85
86 static constexpr size_t MAX_PARTIAL_RELATION_LENGTH = compute_max_partial_relation_length<Relations>();
87
88 static_assert(MAX_PARTIAL_RELATION_LENGTH < 8, "MAX_PARTIAL_RELATION_LENGTH must be less than 8");
89
90 // BATCHED_RELATION_PARTIAL_LENGTH = algebraic degree of sumcheck relation *after* multiplying by the `pow_zeta`
91 // random polynomial e.g. For \sum(x) [A(x) * B(x) + C(x)] * PowZeta(X), relation length = 2 and random relation
92 // length = 3
95
96 static constexpr bool has_zero_row = true;
97
98 static constexpr size_t NUM_FRS_COM = FrCodec::calc_num_fields<Commitment>();
99 static constexpr size_t NUM_FRS_FR = FrCodec::calc_num_fields<FF>();
100
101 // After any circuit changes, hover `COMPUTED_AVM_PROOF_LENGTH_IN_FIELDS` in your IDE
102 // to see its value and then update `AVM_V2_PROOF_LENGTH_IN_FIELDS` in constants.nr.
103 // This formula must match the serialization in Transcript::serialize_full_transcript().
104 static constexpr size_t COMPUTED_AVM_PROOF_LENGTH_IN_FIELDS =
105 NUM_WITNESS_ENTITIES * NUM_FRS_COM + // witness commitments
106 NUM_ALL_ENTITIES * NUM_FRS_FR + // sumcheck evaluations
108 (MAX_AVM_TRACE_LOG_SIZE - 1) * NUM_FRS_COM + // gemini fold comms
109 MAX_AVM_TRACE_LOG_SIZE * NUM_FRS_FR + // gemini fold evals
110 2 * NUM_FRS_COM; // shplonk + kzg
111
113 "\n The constant AVM_V2_PROOF_LENGTH_IN_FIELDS_PADDED is now too short\n"
114 "as is smaller than the real AVM v2 proof. Increase the padded constant \n"
115 "in constants.nr accordingly.");
116
117 // TODO(#13390): Revive the following code once we freeze the number of colums in AVM.
118 // static_assert(AVM_V2_PROOF_LENGTH_IN_FIELDS == COMPUTED_AVM_PROOF_LENGTH_IN_FIELDS,
119 // "\nUnexpected AVM V2 proof length. This might be due to some changes in the\n"
120 // "AVM circuit layout. In this case, modify AVM_V2_PROOF_LENGTH_IN_FIELDS \n"
121 // "in constants.nr accordingly.");
122
123 // VK is composed of
124 // - circuit size encoded as a fr field element
125 // - num of inputs encoded as a fr field element
126 // - NUM_PRECOMPUTED_ENTITIES commitments
127 // TODO(#13390): Revive the following code once we freeze the number of colums in AVM.
128 // static_assert(AVM_V2_VERIFICATION_KEY_LENGTH_IN_FIELDS == 2 * NUM_FRS_FR + NUM_PRECOMPUTED_ENTITIES *
129 // NUM_FRS_COM,
130 // "\nUnexpected AVM V2 VK length. This might be due to some changes in the\n"
131 // "AVM circuit. In this case, modify AVM_V2_VERIFICATION_KEY_LENGTH_IN_FIELDS \n"
132 // "in constants.nr accordingly.");
133
134 public:
156
157 // Even though we only need the witness entities, we hold all entities because it's
158 // easier and will not make much of a difference.
159 template <typename DataType> class WitnessEntities : public AllEntities<DataType> {
160 private:
161 // Obscure get_all since we redefine it.
164
165 public:
169 };
170
171 // Even though we only need the precomputed entities, we hold all entities because it's
172 // easier and will not make much of a difference.
173 template <typename DataType> class PrecomputedEntities : public AllEntities<DataType> {
174 private:
175 // Obscure get_all since we redefine it.
178
179 public:
183 };
184
186 public:
188
189 std::array<Commitment, NUM_WITNESS_ENTITIES> commitments;
190
192 std::array<FF, NUM_ALL_ENTITIES> sumcheck_evaluations;
193 std::vector<Commitment> gemini_fold_comms;
194 std::vector<FF> gemini_fold_evals;
197
198 Transcript() = default;
199
202 };
203
204 class ProvingKey : public AllEntities<Polynomial> {
205 private:
206 // Obscure get_all since it would be incorrect.
209
210 public:
211 using FF = typename Polynomial::FF;
212
213 static constexpr size_t circuit_size = MAX_AVM_TRACE_SIZE; // Fixed size
214 static constexpr size_t log_circuit_size = MAX_AVM_TRACE_LOG_SIZE;
215
216 ProvingKey();
217
221
223
224 // The number of public inputs has to be the same for all instances because they are
225 // folded element by element.
226 std::vector<FF> public_inputs;
227 };
228
229 class VerificationKey : public NativeVerificationKey_<PrecomputedEntities<Commitment>,
230 typename Transcript::Codec,
231 typename Transcript::HashFunction,
232 void,
233 VKSerializationMode::NO_METADATA> {
235 typename Transcript::Codec,
237 void,
239
240 public:
242
243 VerificationKey() = default;
244
246 {
248 for (auto [vk_cmt, cmt] : zip_view(this->get_all(), precomputed_cmts)) {
249 vk_cmt = cmt;
250 }
251 }
252
253 // NOTE: This should not be used in production. You should use the fixed VK instead.
254 static VerificationKey from_proving_key(const ProvingKey& proving_key)
255 {
258 for (auto [polynomial, commitment] : zip_view(proving_key.get_precomputed(), vk.get_all())) {
259 commitment = proving_key.commitment_key.commit(polynomial);
260 }
261 return vk;
262 }
263
268 typename Base::DataType hash_with_origin_tagging([[maybe_unused]] const OriginTag& tag) const override
269 {
270 throw_or_abort("Not intended to be used because vk is hardcoded in circuit.");
271 }
272 };
273
274 // Used by sumcheck.
276
277 template <typename Polynomials> class PolynomialEntitiesAtFixedRow {
278 public:
281 , pp(pp)
282 {}
283
284 // Only const-access is allowed here. That's all that the logderivative library requires.
285 const auto& get(ColumnAndShifts c) const { return pp.get(c)[row_idx]; }
286
287 private:
288 const size_t row_idx;
290 };
291
295 class ProverPolynomials : public AllEntities<Polynomial> {
296 public:
297 // Define all operations as default, except copy construction/assignment
298 ProverPolynomials() = default;
301 ProverPolynomials(ProverPolynomials&& o) noexcept = default;
304
305 ProverPolynomials(ProvingKey& proving_key);
306 // For partially evaluated multivariates.
307 // TODO(fcarreiro): Reconsider its place.
308 ProverPolynomials(const ProverPolynomials& full_polynomials, size_t circuit_size);
309
310 // Only const-access is allowed here. That's all that the logderivative library requires.
311 // https://github.com/AztecProtocol/aztec-packages/blob/e50d8e0/barretenberg/cpp/src/barretenberg/honk/proof_system/logderivative_library.hpp#L44.
312 PolynomialEntitiesAtFixedRow<ProverPolynomials> get_row(size_t row_idx) const { return { row_idx, *this }; }
313 };
314
316
322 : private AllEntities<std::unique_ptr<bb::Univariate<FF, MAX_PARTIAL_RELATION_LENGTH>>> {
323 public:
327
328 void set_current_edge(size_t edge_idx);
330
331 private:
332 size_t current_edge = 0;
333 mutable bool dirty = false;
335 };
336
341 // TODO(fcarreiro): This is only required because of the Flavor::USE_SHORT_MONOMIALS conditional in
342 // SumcheckProverRound. The conditional should be improved to not require this.
343 template <size_t LENGTH> using ProverUnivariates = int;
344
350
351 // Templated for use in recursive verifier
352 template <typename Commitment_, typename VerificationKey>
353 class VerifierCommitments_ : public AllEntities<Commitment_> {
354 private:
356
357 public:
358 VerifierCommitments_(const std::shared_ptr<VerificationKey>& verification_key)
359 {
360 for (auto [commitment, vk_commitment] : zip_view(this->get_precomputed(), verification_key->get_all())) {
361 commitment = vk_commitment;
362 }
363 }
364 };
365
366 // Native version of the verifier commitments
368};
369
370} // namespace bb::avm2
#define AVM_V2_PROOF_LENGTH_IN_FIELDS_PADDED
Common transcript class for both parties. Stores the data for the current round, as well as the manif...
HashFunction_ HashFunction
CommitmentKey object over a pairing group 𝔾₁.
Commitment commit(PolynomialSpan< const Fr > polynomial) const
Uses the ProverSRS to create a commitment to p(X)
Base Native verification key class.
Definition flavor.hpp:141
typename Codec::DataType DataType
Definition flavor.hpp:144
A univariate polynomial represented by its values on {0, 1,..., domain_end - 1}.
Representation of the Grumpkin Verifier Commitment Key inside a bn254 circuit.
DEFINE_AVM_GETTER(unshifted, UNSHIFTED_START_IDX, NUM_UNSHIFTED_ENTITIES)
DEFINE_AVM_GETTER(precomputed, PRECOMPUTED_START_IDX, NUM_PRECOMPUTED_ENTITIES)
std::span< DataType > get_all()
Definition flavor.hpp:140
std::span< const DataType > get_all() const
Definition flavor.hpp:141
std::span< const std::string > get_labels() const
Definition flavor.hpp:142
DEFINE_AVM_GETTER(witness, WITNESS_START_IDX, NUM_WITNESS_ENTITIES)
DEFINE_AVM_GETTER(wires, WIRE_START_IDX, NUM_WIRE_ENTITIES)
DataType & get(ColumnAndShifts c)
Definition flavor.hpp:153
const DataType & get(ColumnAndShifts c) const
Definition flavor.hpp:154
DEFINE_AVM_GETTER(to_be_shifted, WIRES_TO_BE_SHIFTED_START_IDX, NUM_WIRES_TO_BE_SHIFTED)
DEFINE_AVM_GETTER(derived, DERIVED_START_IDX, NUM_DERIVED_ENTITIES)
std::array< DataType, NUM_ALL_ENTITIES > entities
Definition flavor.hpp:138
DEFINE_AVM_GETTER(shifted, SHIFTED_START_IDX, NUM_SHIFTED_ENTITIES)
A container for univariates used during sumcheck.
Definition flavor.hpp:322
const bb::Univariate< FF, MAX_PARTIAL_RELATION_LENGTH > & get(ColumnAndShifts c) const
Definition flavor.cpp:102
LazilyExtendedProverUnivariates(const ProverPolynomials &multivariates)
Definition flavor.hpp:324
PolynomialEntitiesAtFixedRow(const size_t row_idx, const Polynomials &pp)
Definition flavor.hpp:279
const auto & get(ColumnAndShifts c) const
Definition flavor.hpp:285
std::span< const std::string > get_labels() const
Definition flavor.hpp:182
std::span< DataType > get_all()
Definition flavor.hpp:180
std::span< const DataType > get_all() const
Definition flavor.hpp:181
A container for the prover polynomials handles.
Definition flavor.hpp:295
PolynomialEntitiesAtFixedRow< ProverPolynomials > get_row(size_t row_idx) const
Definition flavor.hpp:312
ProverPolynomials & operator=(ProverPolynomials &&o) noexcept=default
ProverPolynomials & operator=(const ProverPolynomials &)=delete
ProverPolynomials(const ProverPolynomials &o)=delete
ProverPolynomials(ProverPolynomials &&o) noexcept=default
std::vector< FF > public_inputs
Definition flavor.hpp:226
std::span< const Polynomial > get_all() const
Definition flavor.hpp:219
std::span< Polynomial > get_all()
Definition flavor.hpp:218
static constexpr size_t log_circuit_size
Definition flavor.hpp:214
static constexpr size_t circuit_size
Definition flavor.hpp:213
std::span< const std::string > get_labels() const
Definition flavor.hpp:220
typename Polynomial::FF FF
Definition flavor.hpp:211
std::vector< Commitment > gemini_fold_comms
Definition flavor.hpp:193
std::array< FF, NUM_ALL_ENTITIES > sumcheck_evaluations
Definition flavor.hpp:192
std::array< Commitment, NUM_WITNESS_ENTITIES > commitments
Definition flavor.hpp:189
std::vector< bb::Univariate< FF, BATCHED_RELATION_PARTIAL_LENGTH > > sumcheck_univariates
Definition flavor.hpp:191
std::vector< FF > gemini_fold_evals
Definition flavor.hpp:194
Base::DataType hash_with_origin_tagging(const OriginTag &tag) const override
Unimplemented because AVM VK is hardcoded so hash does not need to be computed. Rather,...
Definition flavor.hpp:268
static constexpr size_t NUM_PRECOMPUTED_COMMITMENTS
Definition flavor.hpp:241
static VerificationKey from_proving_key(const ProvingKey &proving_key)
Definition flavor.hpp:254
VerificationKey(std::array< Commitment, NUM_PRECOMPUTED_COMMITMENTS > const &precomputed_cmts)
Definition flavor.hpp:245
VerifierCommitments_(const std::shared_ptr< VerificationKey > &verification_key)
Definition flavor.hpp:358
std::span< const std::string > get_labels() const
Definition flavor.hpp:168
std::span< DataType > get_all()
Definition flavor.hpp:166
std::span< const DataType > get_all() const
Definition flavor.hpp:167
AvmFlavorSettings::GroupElement GroupElement
Definition flavor.hpp:39
static constexpr bool IS_AVM
Definition flavor.hpp:46
tuple_cat_t< MainRelations_< FF_ >, LookupRelations_< FF_ > > Relations_
Definition flavor.hpp:81
static constexpr size_t COMPUTED_AVM_PROOF_LENGTH_IN_FIELDS
Definition flavor.hpp:104
AvmFlavorSettings::PolynomialHandle PolynomialHandle
Definition flavor.hpp:38
static constexpr size_t NUM_SUBRELATIONS
Definition flavor.hpp:84
static constexpr size_t NUM_SHIFTED_ENTITIES
Definition flavor.hpp:56
AvmFlavorSettings::CommitmentHandle CommitmentHandle
Definition flavor.hpp:41
AvmFlavorSettings::FF FF
Definition flavor.hpp:36
static constexpr size_t NUM_FRS_FR
Definition flavor.hpp:99
static constexpr bool USE_PADDING
Definition flavor.hpp:52
static constexpr size_t MAX_CHUNK_THREAD_PORTION_SIZE
Definition flavor.hpp:68
static constexpr bool HasZK
Definition flavor.hpp:50
static constexpr size_t NUM_RELATIONS
Definition flavor.hpp:94
static constexpr size_t NUM_WITNESS_ENTITIES
Definition flavor.hpp:55
AvmFlavorSettings::G1 G1
Definition flavor.hpp:33
static constexpr size_t MAX_PARTIAL_RELATION_LENGTH
Definition flavor.hpp:86
static constexpr size_t NUM_WIRES
Definition flavor.hpp:57
static constexpr bool USE_SHORT_MONOMIALS
Definition flavor.hpp:48
static constexpr size_t BATCHED_RELATION_PARTIAL_LENGTH
Definition flavor.hpp:93
static constexpr size_t NUM_FRS_COM
Definition flavor.hpp:98
static constexpr size_t NUM_PRECOMPUTED_ENTITIES
Definition flavor.hpp:54
AvmFlavorSettings::Commitment Commitment
Definition flavor.hpp:40
static constexpr size_t NUM_ALL_ENTITIES
Definition flavor.hpp:58
Relations_< FF > Relations
Definition flavor.hpp:82
static constexpr bool has_zero_row
Definition flavor.hpp:96
bb::VerifierCommitmentKey< Curve > VerifierCommitmentKey
G1::affine_element CommitmentHandle
bb::Polynomial< FF > Polynomial
bb::CommitmentKey< Curve > CommitmentKey
Base class templates for structures that contain data parameterized by the fundamental polynomials of...
constexpr auto NUM_WIRE_ENTITIES
Definition columns.hpp:42
decltype(flat_tuple::tuple_cat(std::declval< input_t >()...)) tuple_cat_t
Definition flavor.hpp:28
constexpr auto NUM_UNSHIFTED_ENTITIES
Definition columns.hpp:47
constexpr std::size_t MAX_AVM_TRACE_SIZE
Definition constants.hpp:13
constexpr auto WITNESS_START_IDX
Definition columns.hpp:72
constexpr auto NUM_WIRES_TO_BE_SHIFTED
Definition columns.hpp:45
constexpr std::size_t MAX_AVM_TRACE_LOG_SIZE
Definition constants.hpp:12
constexpr auto NUM_DERIVED_ENTITIES
Definition columns.hpp:43
constexpr auto WIRES_TO_BE_SHIFTED_START_IDX
Definition columns.hpp:66
constexpr auto DERIVED_START_IDX
Definition columns.hpp:68
const std::vector< std::string > & COLUMN_NAMES
Definition columns.hpp:84
constexpr auto PRECOMPUTED_START_IDX
Definition columns.hpp:62
constexpr auto UNSHIFTED_START_IDX
Definition columns.hpp:74
constexpr auto WIRE_START_IDX
Definition columns.hpp:64
ColumnAndShifts
Definition columns.hpp:34
constexpr auto SHIFTED_START_IDX
Definition columns.hpp:70
VerifierCommitmentKey< Curve > vk
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
constexpr auto tuple_cat(T &&... ts)
Definition tuplet.hpp:1101
static constexpr size_t NUM_SHIFTED_ENTITIES
static constexpr size_t NUM_WIRES
static constexpr size_t NUM_ALL_ENTITIES
static constexpr size_t NUM_PRECOMPUTED_ENTITIES
static constexpr size_t NUM_WITNESS_ENTITIES
void throw_or_abort(std::string const &err)