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 size_t NUM_FRS_COM = FrCodec::calc_num_fields<Commitment>();
97 static constexpr size_t NUM_FRS_FR = FrCodec::calc_num_fields<FF>();
98
99 // After any circuit changes, hover `COMPUTED_AVM_PROOF_LENGTH_IN_FIELDS` in your IDE
100 // to see its value and then update `AVM_V2_PROOF_LENGTH_IN_FIELDS` in constants.nr.
101 // This formula must match the serialization in Transcript::serialize_full_transcript().
102 static constexpr size_t COMPUTED_AVM_PROOF_LENGTH_IN_FIELDS =
103 NUM_WITNESS_ENTITIES * NUM_FRS_COM + // witness commitments
104 NUM_ALL_ENTITIES * NUM_FRS_FR + // sumcheck evaluations
106 (MAX_AVM_TRACE_LOG_SIZE - 1) * NUM_FRS_COM + // gemini fold comms
107 MAX_AVM_TRACE_LOG_SIZE * NUM_FRS_FR + // gemini fold evals
108 2 * NUM_FRS_COM; // shplonk + kzg
109
111 "\n The constant AVM_V2_PROOF_LENGTH_IN_FIELDS_PADDED is now too short\n"
112 "as is smaller than the real AVM v2 proof. Increase the padded constant \n"
113 "in constants.nr accordingly.");
114
115 // TODO(#13390): Revive the following code once we freeze the number of colums in AVM.
116 // static_assert(AVM_V2_PROOF_LENGTH_IN_FIELDS == COMPUTED_AVM_PROOF_LENGTH_IN_FIELDS,
117 // "\nUnexpected AVM V2 proof length. This might be due to some changes in the\n"
118 // "AVM circuit layout. In this case, modify AVM_V2_PROOF_LENGTH_IN_FIELDS \n"
119 // "in constants.nr accordingly.");
120
121 // VK is composed of
122 // - circuit size encoded as a fr field element
123 // - num of inputs encoded as a fr field element
124 // - NUM_PRECOMPUTED_ENTITIES commitments
125 // TODO(#13390): Revive the following code once we freeze the number of colums in AVM.
126 // static_assert(AVM_V2_VERIFICATION_KEY_LENGTH_IN_FIELDS == 2 * NUM_FRS_FR + NUM_PRECOMPUTED_ENTITIES *
127 // NUM_FRS_COM,
128 // "\nUnexpected AVM V2 VK length. This might be due to some changes in the\n"
129 // "AVM circuit. In this case, modify AVM_V2_VERIFICATION_KEY_LENGTH_IN_FIELDS \n"
130 // "in constants.nr accordingly.");
131
132 public:
154
155 // Even though we only need the witness entities, we hold all entities because it's
156 // easier and will not make much of a difference.
157 template <typename DataType> class WitnessEntities : public AllEntities<DataType> {
158 private:
159 // Obscure get_all since we redefine it.
162
163 public:
167 };
168
169 // Even though we only need the precomputed entities, we hold all entities because it's
170 // easier and will not make much of a difference.
171 template <typename DataType> class PrecomputedEntities : public AllEntities<DataType> {
172 private:
173 // Obscure get_all since we redefine it.
176
177 public:
181 };
182
184 public:
186
187 std::array<Commitment, NUM_WITNESS_ENTITIES> commitments;
188
190 std::array<FF, NUM_ALL_ENTITIES> sumcheck_evaluations;
191 std::vector<Commitment> gemini_fold_comms;
192 std::vector<FF> gemini_fold_evals;
195
196 Transcript() = default;
197
200 };
201
202 class ProvingKey : public AllEntities<Polynomial> {
203 private:
204 // Obscure get_all since it would be incorrect.
207
208 public:
209 using FF = typename Polynomial::FF;
210
211 static constexpr size_t circuit_size = MAX_AVM_TRACE_SIZE; // Fixed size
212 static constexpr size_t log_circuit_size = MAX_AVM_TRACE_LOG_SIZE;
213
214 ProvingKey();
215
219
221
222 // The number of public inputs has to be the same for all instances because they are
223 // folded element by element.
224 std::vector<FF> public_inputs;
225 };
226
227 class VerificationKey : public NativeVerificationKey_<PrecomputedEntities<Commitment>,
228 typename Transcript::Codec,
229 typename Transcript::HashFunction,
230 void,
231 VKSerializationMode::NO_METADATA> {
233 typename Transcript::Codec,
235 void,
237
238 public:
240
241 VerificationKey() = default;
242
244 {
246 for (auto [vk_cmt, cmt] : zip_view(this->get_all(), precomputed_cmts)) {
247 vk_cmt = cmt;
248 }
249 }
250
251 // NOTE: This should not be used in production. You should use the fixed VK instead.
252 static VerificationKey from_proving_key(const ProvingKey& proving_key)
253 {
256 for (auto [polynomial, commitment] : zip_view(proving_key.get_precomputed(), vk.get_all())) {
257 commitment = proving_key.commitment_key.commit(polynomial);
258 }
259 return vk;
260 }
261
266 typename Base::DataType hash_with_origin_tagging([[maybe_unused]] const OriginTag& tag) const override
267 {
268 throw_or_abort("Not intended to be used because vk is hardcoded in circuit.");
269 }
270 };
271
272 // Used by sumcheck.
274
275 template <typename Polynomials> class PolynomialEntitiesAtFixedRow {
276 public:
279 , pp(pp)
280 {}
281
282 // Only const-access is allowed here. That's all that the logderivative library requires.
283 const auto& get(ColumnAndShifts c) const { return pp.get(c)[row_idx]; }
284
285 private:
286 const size_t row_idx;
288 };
289
293 class ProverPolynomials : public AllEntities<Polynomial> {
294 public:
295 // Define all operations as default, except copy construction/assignment
296 ProverPolynomials() = default;
299 ProverPolynomials(ProverPolynomials&& o) noexcept = default;
302
303 ProverPolynomials(ProvingKey& proving_key);
304 // For partially evaluated multivariates.
305 // TODO(fcarreiro): Reconsider its place.
306 ProverPolynomials(const ProverPolynomials& full_polynomials, size_t circuit_size);
307
308 // Only const-access is allowed here. That's all that the logderivative library requires.
309 // https://github.com/AztecProtocol/aztec-packages/blob/e50d8e0/barretenberg/cpp/src/barretenberg/honk/proof_system/logderivative_library.hpp#L44.
310 PolynomialEntitiesAtFixedRow<ProverPolynomials> get_row(size_t row_idx) const { return { row_idx, *this }; }
311 };
312
314
320 : private AllEntities<std::unique_ptr<bb::Univariate<FF, MAX_PARTIAL_RELATION_LENGTH>>> {
321 public:
325
326 void set_current_edge(size_t edge_idx);
328
329 private:
330 size_t current_edge = 0;
331 mutable bool dirty = false;
333 };
334
339 // TODO(fcarreiro): This is only required because of the Flavor::USE_SHORT_MONOMIALS conditional in
340 // SumcheckProverRound. The conditional should be improved to not require this.
341 template <size_t LENGTH> using ProverUnivariates = int;
342
348
349 // Templated for use in recursive verifier
350 template <typename Commitment_, typename VerificationKey>
351 class VerifierCommitments_ : public AllEntities<Commitment_> {
352 private:
354
355 public:
356 VerifierCommitments_(const std::shared_ptr<VerificationKey>& verification_key)
357 {
358 for (auto [commitment, vk_commitment] : zip_view(this->get_precomputed(), verification_key->get_all())) {
359 commitment = vk_commitment;
360 }
361 }
362 };
363
364 // Native version of the verifier commitments
366};
367
368} // 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:172
typename Codec::DataType DataType
Definition flavor.hpp:175
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:138
std::span< const DataType > get_all() const
Definition flavor.hpp:139
std::span< const std::string > get_labels() const
Definition flavor.hpp:140
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:151
const DataType & get(ColumnAndShifts c) const
Definition flavor.hpp:152
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:136
DEFINE_AVM_GETTER(shifted, SHIFTED_START_IDX, NUM_SHIFTED_ENTITIES)
A container for univariates used during sumcheck.
Definition flavor.hpp:320
const bb::Univariate< FF, MAX_PARTIAL_RELATION_LENGTH > & get(ColumnAndShifts c) const
Definition flavor.cpp:102
LazilyExtendedProverUnivariates(const ProverPolynomials &multivariates)
Definition flavor.hpp:322
PolynomialEntitiesAtFixedRow(const size_t row_idx, const Polynomials &pp)
Definition flavor.hpp:277
const auto & get(ColumnAndShifts c) const
Definition flavor.hpp:283
std::span< const std::string > get_labels() const
Definition flavor.hpp:180
std::span< DataType > get_all()
Definition flavor.hpp:178
std::span< const DataType > get_all() const
Definition flavor.hpp:179
A container for the prover polynomials handles.
Definition flavor.hpp:293
PolynomialEntitiesAtFixedRow< ProverPolynomials > get_row(size_t row_idx) const
Definition flavor.hpp:310
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:224
std::span< const Polynomial > get_all() const
Definition flavor.hpp:217
std::span< Polynomial > get_all()
Definition flavor.hpp:216
static constexpr size_t log_circuit_size
Definition flavor.hpp:212
static constexpr size_t circuit_size
Definition flavor.hpp:211
std::span< const std::string > get_labels() const
Definition flavor.hpp:218
typename Polynomial::FF FF
Definition flavor.hpp:209
std::vector< Commitment > gemini_fold_comms
Definition flavor.hpp:191
std::array< FF, NUM_ALL_ENTITIES > sumcheck_evaluations
Definition flavor.hpp:190
std::array< Commitment, NUM_WITNESS_ENTITIES > commitments
Definition flavor.hpp:187
std::vector< bb::Univariate< FF, BATCHED_RELATION_PARTIAL_LENGTH > > sumcheck_univariates
Definition flavor.hpp:189
std::vector< FF > gemini_fold_evals
Definition flavor.hpp:192
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:266
static constexpr size_t NUM_PRECOMPUTED_COMMITMENTS
Definition flavor.hpp:239
static VerificationKey from_proving_key(const ProvingKey &proving_key)
Definition flavor.hpp:252
VerificationKey(std::array< Commitment, NUM_PRECOMPUTED_COMMITMENTS > const &precomputed_cmts)
Definition flavor.hpp:243
VerifierCommitments_(const std::shared_ptr< VerificationKey > &verification_key)
Definition flavor.hpp:356
std::span< const std::string > get_labels() const
Definition flavor.hpp:166
std::span< DataType > get_all()
Definition flavor.hpp:164
std::span< const DataType > get_all() const
Definition flavor.hpp:165
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:102
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:97
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:96
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
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)