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 static constexpr size_t COMPUTED_AVM_PROOF_LENGTH_IN_FIELDS =
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 // - circuit size encoded as a fr field element
120 // - num of inputs encoded as a fr field element
121 // - NUM_PRECOMPUTED_ENTITIES commitments
122 // TODO(#13390): Revive the following code once we freeze the number of colums in AVM.
123 // static_assert(AVM_V2_VERIFICATION_KEY_LENGTH_IN_FIELDS == 2 * NUM_FRS_FR + NUM_PRECOMPUTED_ENTITIES *
124 // NUM_FRS_COM,
125 // "\nUnexpected AVM V2 VK length. This might be due to some changes in the\n"
126 // "AVM circuit. In this case, modify AVM_V2_VERIFICATION_KEY_LENGTH_IN_FIELDS \n"
127 // "in constants.nr accordingly.");
128
129 public:
151
152 // Even though we only need the witness entities, we hold all entities because it's
153 // easier and will not make much of a difference.
154 template <typename DataType> class WitnessEntities : public AllEntities<DataType> {
155 private:
156 // Obscure get_all since we redefine it.
159
160 public:
164 };
165
166 // Even though we only need the precomputed entities, we hold all entities because it's
167 // easier and will not make much of a difference.
168 template <typename DataType> class PrecomputedEntities : public AllEntities<DataType> {
169 private:
170 // Obscure get_all since we redefine it.
173
174 public:
178 };
179
181 public:
183
184 std::array<Commitment, NUM_WITNESS_ENTITIES> commitments;
185
187 std::array<FF, NUM_ALL_ENTITIES> sumcheck_evaluations;
188 std::vector<Commitment> gemini_fold_comms;
189 std::vector<FF> gemini_fold_evals;
192
193 Transcript() = default;
194
197 };
198
199 class ProvingKey : public AllEntities<Polynomial> {
200 private:
201 // Obscure get_all since it would be incorrect.
204
205 public:
206 using FF = typename Polynomial::FF;
207
208 static constexpr size_t circuit_size = MAX_AVM_TRACE_SIZE; // Fixed size
209 static constexpr size_t log_circuit_size = MAX_AVM_TRACE_LOG_SIZE;
210
211 ProvingKey();
212
216
218
219 // The number of public inputs has to be the same for all instances because they are
220 // folded element by element.
221 std::vector<FF> public_inputs;
222 };
223
225 : public NativeVerificationKey_<PrecomputedEntities<Commitment>, Transcript, VKSerializationMode::NO_METADATA> {
226 public:
228
229 VerificationKey() = default;
230
231 VerificationKey(const std::shared_ptr<ProvingKey>& proving_key)
232 {
234 for (auto [polynomial, commitment] : zip_view(proving_key->get_precomputed(), this->get_all())) {
235 commitment = proving_key->commitment_key.commit(polynomial);
236 }
237 }
238
240 {
242 for (auto [vk_cmt, cmt] : zip_view(this->get_all(), precomputed_cmts)) {
243 vk_cmt = cmt;
244 }
245 }
246
251 fr hash_with_origin_tagging([[maybe_unused]] const std::string& domain_separator,
252 [[maybe_unused]] Transcript& transcript) const override
253 {
254 throw_or_abort("Not intended to be used because vk is hardcoded in circuit.");
255 }
256 };
257
258 // Used by sumcheck.
260
261 template <typename Polynomials> class PolynomialEntitiesAtFixedRow {
262 public:
265 , pp(pp)
266 {}
267
268 // Only const-access is allowed here. That's all that the logderivative library requires.
269 const auto& get(ColumnAndShifts c) const { return pp.get(c)[row_idx]; }
270
271 private:
272 const size_t row_idx;
274 };
275
279 class ProverPolynomials : public AllEntities<Polynomial> {
280 public:
281 // Define all operations as default, except copy construction/assignment
282 ProverPolynomials() = default;
285 ProverPolynomials(ProverPolynomials&& o) noexcept = default;
288
289 ProverPolynomials(ProvingKey& proving_key);
290 // For partially evaluated multivariates.
291 // TODO(fcarreiro): Reconsider its place.
292 ProverPolynomials(const ProverPolynomials& full_polynomials, size_t circuit_size);
293
294 // Only const-access is allowed here. That's all that the logderivative library requires.
295 // https://github.com/AztecProtocol/aztec-packages/blob/e50d8e0/barretenberg/cpp/src/barretenberg/honk/proof_system/logderivative_library.hpp#L44.
296 PolynomialEntitiesAtFixedRow<ProverPolynomials> get_row(size_t row_idx) const { return { row_idx, *this }; }
297 };
298
300
306 : private AllEntities<std::unique_ptr<bb::Univariate<FF, MAX_PARTIAL_RELATION_LENGTH>>> {
307 public:
311
312 void set_current_edge(size_t edge_idx);
314
315 private:
316 size_t current_edge = 0;
317 mutable bool dirty = false;
319 };
320
325 // TODO(fcarreiro): This is only required because of the Flavor::USE_SHORT_MONOMIALS conditional in
326 // SumcheckProverRound. The conditional should be improved to not require this.
327 template <size_t LENGTH> using ProverUnivariates = int;
328
334
335 // Templated for use in recursive verifier
336 template <typename Commitment_, typename VerificationKey>
337 class VerifierCommitments_ : public AllEntities<Commitment_> {
338 private:
340
341 public:
342 VerifierCommitments_(const std::shared_ptr<VerificationKey>& verification_key)
343 {
344 for (auto [commitment, vk_commitment] : zip_view(this->get_precomputed(), verification_key->get_all())) {
345 commitment = vk_commitment;
346 }
347 }
348 };
349
350 // Native version of the verifier commitments
352};
353
354} // 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 𝔾₁.
Base Native verification key class.
Definition flavor.hpp:137
A univariate polynomial represented by its values on {0, 1,..., domain_end - 1}.
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:135
std::span< const DataType > get_all() const
Definition flavor.hpp:136
std::span< const std::string > get_labels() const
Definition flavor.hpp:137
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:148
const DataType & get(ColumnAndShifts c) const
Definition flavor.hpp:149
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:133
DEFINE_AVM_GETTER(shifted, SHIFTED_START_IDX, NUM_SHIFTED_ENTITIES)
A container for univariates used during sumcheck.
Definition flavor.hpp:306
const bb::Univariate< FF, MAX_PARTIAL_RELATION_LENGTH > & get(ColumnAndShifts c) const
Definition flavor.cpp:100
LazilyExtendedProverUnivariates(const ProverPolynomials &multivariates)
Definition flavor.hpp:308
PolynomialEntitiesAtFixedRow(const size_t row_idx, const Polynomials &pp)
Definition flavor.hpp:263
const auto & get(ColumnAndShifts c) const
Definition flavor.hpp:269
std::span< const std::string > get_labels() const
Definition flavor.hpp:177
std::span< DataType > get_all()
Definition flavor.hpp:175
std::span< const DataType > get_all() const
Definition flavor.hpp:176
A container for the prover polynomials handles.
Definition flavor.hpp:279
PolynomialEntitiesAtFixedRow< ProverPolynomials > get_row(size_t row_idx) const
Definition flavor.hpp:296
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:221
std::span< const Polynomial > get_all() const
Definition flavor.hpp:214
std::span< Polynomial > get_all()
Definition flavor.hpp:213
static constexpr size_t log_circuit_size
Definition flavor.hpp:209
static constexpr size_t circuit_size
Definition flavor.hpp:208
std::span< const std::string > get_labels() const
Definition flavor.hpp:215
typename Polynomial::FF FF
Definition flavor.hpp:206
std::vector< Commitment > gemini_fold_comms
Definition flavor.hpp:188
std::array< FF, NUM_ALL_ENTITIES > sumcheck_evaluations
Definition flavor.hpp:187
std::array< Commitment, NUM_WITNESS_ENTITIES > commitments
Definition flavor.hpp:184
std::vector< bb::Univariate< FF, BATCHED_RELATION_PARTIAL_LENGTH > > sumcheck_univariates
Definition flavor.hpp:186
std::vector< FF > gemini_fold_evals
Definition flavor.hpp:189
fr hash_with_origin_tagging(const std::string &domain_separator, Transcript &transcript) const override
Unimplemented because AVM VK is hardcoded so hash does not need to be computed. Rather,...
Definition flavor.hpp:251
static constexpr size_t NUM_PRECOMPUTED_COMMITMENTS
Definition flavor.hpp:227
VerificationKey(const std::shared_ptr< ProvingKey > &proving_key)
Definition flavor.hpp:231
VerificationKey(std::array< Commitment, NUM_PRECOMPUTED_COMMITMENTS > const &precomputed_cmts)
Definition flavor.hpp:239
VerifierCommitments_(const std::shared_ptr< VerificationKey > &verification_key)
Definition flavor.hpp:342
std::span< const std::string > get_labels() const
Definition flavor.hpp:163
std::span< DataType > get_all()
Definition flavor.hpp:161
std::span< const DataType > get_all() const
Definition flavor.hpp:162
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:103
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
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)