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: [Federico], commit: }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6#pragma once
7
8#include <array>
9#include <span>
10
17
21
27
30
31namespace bb::avm2 {
32
33// Metaprogramming to concatenate tuple types.
34template <typename... input_t> using tuple_cat_t = decltype(flat_tuple::tuple_cat(std::declval<input_t>()...));
35
36class AvmFlavor {
37 public:
41
50
51 // To help BB check if a flavor is AVM, even without including this flavor.
52 static constexpr bool IS_AVM = true;
53 // indicates when evaluating sumcheck, edges must be extended to be MAX_PARTIAL_RELATION_LENGTH
54 static constexpr bool USE_SHORT_MONOMIALS = false;
55 // This flavor would not be used with ZK Sumcheck
56 static constexpr bool HasZK = false;
57 // Padding in Sumcheck and Shplemini
58 static constexpr bool USE_PADDING = true;
59
63 static constexpr size_t NUM_WIRES = AvmFlavorVariables::NUM_WIRES;
65
66 // Need to be templated for recursive verifier
68
70
71 // Need to be templated for recursive verifier
73
75
76 // Need to be templated for recursive verifier
79
80 static constexpr size_t NUM_SUBRELATIONS = compute_number_of_subrelations<Relations>();
81
82 static constexpr size_t MAX_PARTIAL_RELATION_LENGTH = compute_max_partial_relation_length<Relations>();
83
84 static_assert(MAX_PARTIAL_RELATION_LENGTH < 8, "MAX_PARTIAL_RELATION_LENGTH must be less than 8");
85
86 // BATCHED_RELATION_PARTIAL_LENGTH = algebraic degree of sumcheck relation *after* multiplying by the `pow_zeta`
87 // random polynomial e.g. For \sum(x) [A(x) * B(x) + C(x)] * PowZeta(X), relation length = 2 and random relation
88 // length = 3
91
92 static constexpr size_t NUM_FRS_COM = FrCodec::calc_num_fields<Commitment>();
93 static constexpr size_t NUM_FRS_FR = FrCodec::calc_num_fields<FF>();
94
95 // After any circuit changes, hover `COMPUTED_AVM_PROOF_LENGTH_IN_FIELDS` in your IDE
96 // to see its value and then update `AVM_V2_PROOF_LENGTH_IN_FIELDS` in constants.nr.
97 // This formula must match the serialization in Transcript::serialize_full_transcript().
98 static constexpr size_t COMPUTED_AVM_PROOF_LENGTH_IN_FIELDS =
99 NUM_WITNESS_ENTITIES * NUM_FRS_COM + // witness commitments
100 NUM_ALL_ENTITIES * NUM_FRS_FR + // sumcheck evaluations
102 (MAX_AVM_TRACE_LOG_SIZE - 1) * NUM_FRS_COM + // gemini fold comms
103 MAX_AVM_TRACE_LOG_SIZE * NUM_FRS_FR + // gemini fold evals
104 2 * NUM_FRS_COM; // shplonk + kzg
105
107 "\n The constant AVM_V2_PROOF_LENGTH_IN_FIELDS_PADDED is now too short\n"
108 "as is smaller than the real AVM v2 proof. Increase the padded constant \n"
109 "in constants.nr accordingly.");
110
111 // TODO(#13390): Revive the following code once we freeze the number of colums in AVM.
112 // static_assert(AVM_V2_PROOF_LENGTH_IN_FIELDS == COMPUTED_AVM_PROOF_LENGTH_IN_FIELDS,
113 // "\nUnexpected AVM V2 proof length. This might be due to some changes in the\n"
114 // "AVM circuit layout. In this case, modify AVM_V2_PROOF_LENGTH_IN_FIELDS \n"
115 // "in constants.nr accordingly.");
116
117 // VK is composed of
118 // - NUM_PRECOMPUTED_ENTITIES commitments
119 // TODO(#13390): Revive the following code once we freeze the number of colums in AVM.
120 // static_assert(AVM_V2_VERIFICATION_KEY_LENGTH_IN_FIELDS == NUM_PRECOMPUTED_ENTITIES * NUM_FRS_COM,
121 // "\nUnexpected AVM V2 VK length. This might be due to some changes in the\n"
122 // "AVM circuit. In this case, modify AVM_V2_VERIFICATION_KEY_LENGTH_IN_FIELDS \n"
123 // "in constants.nr accordingly.");
124
125 public:
147
148 // Even though we only need the witness entities, we hold all entities because it's
149 // easier and will not make much of a difference.
150 template <typename DataType> class WitnessEntities : public AllEntities<DataType> {
151 private:
152 // Obscure get_all since we redefine it.
155
156 public:
160 };
161
162 // Even though we only need the precomputed entities, we hold all entities because it's
163 // easier and will not make much of a difference.
164 template <typename DataType> class PrecomputedEntities : public AllEntities<DataType> {
165 private:
166 // Obscure get_all since we redefine it.
169
170 public:
174 };
175
177 public:
179
180 std::array<Commitment, NUM_WITNESS_ENTITIES> commitments;
181
183 std::array<FF, NUM_ALL_ENTITIES> sumcheck_evaluations;
184 std::vector<Commitment> gemini_fold_comms;
185 std::vector<FF> gemini_fold_evals;
188
189 Transcript() = default;
190
193 };
194
195 class ProvingKey : public AllEntities<Polynomial> {
196 private:
197 // Obscure get_all since it would be incorrect.
200
201 public:
202 using FF = typename Polynomial::FF;
203
204 static constexpr size_t circuit_size = MAX_AVM_TRACE_SIZE; // Fixed size
205 static constexpr size_t log_circuit_size = MAX_AVM_TRACE_LOG_SIZE;
206
207 ProvingKey();
208
212
214
215 // The number of public inputs has to be the same for all instances because they are
216 // folded element by element.
217 std::vector<FF> public_inputs;
218 };
219
226
227 // Used by sumcheck.
229
230 template <typename Polynomials> class PolynomialEntitiesAtFixedRow {
231 public:
234 , pp(pp)
235 {}
236
237 // Only const-access is allowed here. That's all that the logderivative library requires.
238 const auto& get(ColumnAndShifts c) const { return pp.get(c)[row_idx]; }
239
240 private:
241 const size_t row_idx;
243 };
244
248 class ProverPolynomials : public AllEntities<Polynomial> {
249 public:
250 // Define all operations as default, except copy construction/assignment
251 ProverPolynomials() = default;
254 ProverPolynomials(ProverPolynomials&& o) noexcept = default;
257
258 ProverPolynomials(ProvingKey& proving_key);
259 // For partially evaluated multivariates.
260 // TODO(fcarreiro): Reconsider its place.
261 ProverPolynomials(const ProverPolynomials& full_polynomials, size_t circuit_size);
262
263 // Only const-access is allowed here. That's all that the logderivative library requires.
264 // https://github.com/AztecProtocol/aztec-packages/blob/e50d8e0/barretenberg/cpp/src/barretenberg/honk/proof_system/logderivative_library.hpp#L44.
265 PolynomialEntitiesAtFixedRow<ProverPolynomials> get_row(size_t row_idx) const { return { row_idx, *this }; }
266 };
267
269
275 : private AllEntities<std::unique_ptr<bb::Univariate<FF, MAX_PARTIAL_RELATION_LENGTH>>> {
276 public:
280
281 void set_current_edge(size_t edge_idx);
283
284 private:
285 size_t current_edge = 0;
286 mutable bool dirty = false;
288 };
289
294 // TODO(fcarreiro): This is only required because of the Flavor::USE_SHORT_MONOMIALS conditional in
295 // SumcheckProverRound. The conditional should be improved to not require this.
296 template <size_t LENGTH> using ProverUnivariates = int;
297
303
304 // Templated for use in recursive verifier
305 template <typename Commitment_, typename VerificationKey>
306 class VerifierCommitments_ : public AllEntities<Commitment_> {
307 private:
309
310 public:
311 VerifierCommitments_(const std::shared_ptr<VerificationKey>& verification_key)
312 {
313 for (auto [commitment, vk_commitment] : zip_view(this->get_precomputed(), verification_key->get_all())) {
314 commitment = vk_commitment;
315 }
316 }
317 };
318
319 // Native version of the verifier commitments
321};
322
323} // 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...
CommitmentKey object over a pairing group 𝔾₁.
Simple verification key class for fixed-size circuits (ECCVM, Translator).
Definition flavor.hpp:136
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:131
std::span< const DataType > get_all() const
Definition flavor.hpp:132
std::span< const std::string > get_labels() const
Definition flavor.hpp:133
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:144
const DataType & get(ColumnAndShifts c) const
Definition flavor.hpp:145
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:129
DEFINE_AVM_GETTER(shifted, SHIFTED_START_IDX, NUM_SHIFTED_ENTITIES)
A container for univariates used during sumcheck.
Definition flavor.hpp:275
const bb::Univariate< FF, MAX_PARTIAL_RELATION_LENGTH > & get(ColumnAndShifts c) const
Definition flavor.cpp:107
LazilyExtendedProverUnivariates(const ProverPolynomials &multivariates)
Definition flavor.hpp:277
PolynomialEntitiesAtFixedRow(const size_t row_idx, const Polynomials &pp)
Definition flavor.hpp:232
const auto & get(ColumnAndShifts c) const
Definition flavor.hpp:238
std::span< const std::string > get_labels() const
Definition flavor.hpp:173
std::span< DataType > get_all()
Definition flavor.hpp:171
std::span< const DataType > get_all() const
Definition flavor.hpp:172
A container for the prover polynomials handles.
Definition flavor.hpp:248
PolynomialEntitiesAtFixedRow< ProverPolynomials > get_row(size_t row_idx) const
Definition flavor.hpp:265
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:217
std::span< const Polynomial > get_all() const
Definition flavor.hpp:210
std::span< Polynomial > get_all()
Definition flavor.hpp:209
static constexpr size_t log_circuit_size
Definition flavor.hpp:205
static constexpr size_t circuit_size
Definition flavor.hpp:204
std::span< const std::string > get_labels() const
Definition flavor.hpp:211
typename Polynomial::FF FF
Definition flavor.hpp:202
std::vector< Commitment > gemini_fold_comms
Definition flavor.hpp:184
std::array< FF, NUM_ALL_ENTITIES > sumcheck_evaluations
Definition flavor.hpp:183
std::array< Commitment, NUM_WITNESS_ENTITIES > commitments
Definition flavor.hpp:180
std::vector< bb::Univariate< FF, BATCHED_RELATION_PARTIAL_LENGTH > > sumcheck_univariates
Definition flavor.hpp:182
std::vector< FF > gemini_fold_evals
Definition flavor.hpp:185
VerifierCommitments_(const std::shared_ptr< VerificationKey > &verification_key)
Definition flavor.hpp:311
std::span< const std::string > get_labels() const
Definition flavor.hpp:159
std::span< DataType > get_all()
Definition flavor.hpp:157
std::span< const DataType > get_all() const
Definition flavor.hpp:158
AvmFlavorSettings::GroupElement GroupElement
Definition flavor.hpp:45
static constexpr bool IS_AVM
Definition flavor.hpp:52
tuple_cat_t< MainRelations_< FF_ >, LookupRelations_< FF_ > > Relations_
Definition flavor.hpp:77
static constexpr size_t COMPUTED_AVM_PROOF_LENGTH_IN_FIELDS
Definition flavor.hpp:98
AvmFlavorSettings::PolynomialHandle PolynomialHandle
Definition flavor.hpp:44
static constexpr size_t NUM_SUBRELATIONS
Definition flavor.hpp:80
static constexpr size_t NUM_SHIFTED_ENTITIES
Definition flavor.hpp:62
AvmFlavorSettings::CommitmentHandle CommitmentHandle
Definition flavor.hpp:47
AvmFlavorSettings::FF FF
Definition flavor.hpp:42
static constexpr size_t NUM_FRS_FR
Definition flavor.hpp:93
static constexpr bool USE_PADDING
Definition flavor.hpp:58
static constexpr bool HasZK
Definition flavor.hpp:56
static constexpr size_t NUM_RELATIONS
Definition flavor.hpp:90
static constexpr size_t NUM_WITNESS_ENTITIES
Definition flavor.hpp:61
AvmFlavorSettings::G1 G1
Definition flavor.hpp:39
static constexpr size_t MAX_PARTIAL_RELATION_LENGTH
Definition flavor.hpp:82
static constexpr size_t NUM_WIRES
Definition flavor.hpp:63
static constexpr bool USE_SHORT_MONOMIALS
Definition flavor.hpp:54
static constexpr size_t BATCHED_RELATION_PARTIAL_LENGTH
Definition flavor.hpp:89
static constexpr size_t NUM_FRS_COM
Definition flavor.hpp:92
static constexpr size_t NUM_PRECOMPUTED_ENTITIES
Definition flavor.hpp:60
AvmFlavorSettings::Commitment Commitment
Definition flavor.hpp:46
static constexpr size_t NUM_ALL_ENTITIES
Definition flavor.hpp:64
Relations_< FF > Relations
Definition flavor.hpp:78
bb::VerifierCommitmentKey< Curve > VerifierCommitmentKey
G1::affine_element CommitmentHandle
bb::Polynomial< FF > Polynomial
bb::CommitmentKey< Curve > CommitmentKey
Stores the fixed AVM VK commitments (to precomputed polynomials) that depend only on the precomputed ...
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:34
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
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