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