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