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_TOTAL_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 using SubrelationSeparators = std::array<FF, NUM_SUBRELATIONS - 1>;
87
88 static constexpr size_t MAX_PARTIAL_RELATION_LENGTH = compute_max_partial_relation_length<Relations>();
89
90 static_assert(MAX_PARTIAL_RELATION_LENGTH < 8, "MAX_PARTIAL_RELATION_LENGTH must be less than 8");
91
92 // BATCHED_RELATION_PARTIAL_LENGTH = algebraic degree of sumcheck relation *after* multiplying by the `pow_zeta`
93 // random polynomial e.g. For \sum(x) [A(x) * B(x) + C(x)] * PowZeta(X), relation length = 2 and random relation
94 // length = 3
97
98 static constexpr bool has_zero_row = true;
99
100 static constexpr size_t NUM_FRS_COM = FrCodec::calc_num_fields<Commitment>();
101 static constexpr size_t NUM_FRS_FR = FrCodec::calc_num_fields<FF>();
102
103 // After any circuit changes, hover `COMPUTED_AVM_PROOF_LENGTH_IN_FIELDS` in your IDE
104 // to see its value and then update `AVM_V2_PROOF_LENGTH_IN_FIELDS` in constants.nr.
105 static constexpr size_t COMPUTED_AVM_PROOF_LENGTH_IN_FIELDS =
108
110 "\n The constant AVM_V2_PROOF_LENGTH_IN_FIELDS_PADDED is now too short\n"
111 "as is smaller than the real AVM v2 proof. Increase the padded constant \n"
112 "in constants.nr accordingly.");
113
114 // TODO(#13390): Revive the following code once we freeze the number of colums in AVM.
115 // static_assert(AVM_V2_PROOF_LENGTH_IN_FIELDS == COMPUTED_AVM_PROOF_LENGTH_IN_FIELDS,
116 // "\nUnexpected AVM V2 proof length. This might be due to some changes in the\n"
117 // "AVM circuit layout. In this case, modify AVM_V2_PROOF_LENGTH_IN_FIELDS \n"
118 // "in constants.nr accordingly.");
119
120 // VK is composed of
121 // - circuit size encoded as a fr field element
122 // - num of inputs encoded as a fr field element
123 // - NUM_PRECOMPUTED_ENTITIES commitments
124 // TODO(#13390): Revive the following code once we freeze the number of colums in AVM.
125 // static_assert(AVM_V2_VERIFICATION_KEY_LENGTH_IN_FIELDS == 2 * NUM_FRS_FR + NUM_PRECOMPUTED_ENTITIES *
126 // NUM_FRS_COM,
127 // "\nUnexpected AVM V2 VK length. This might be due to some changes in the\n"
128 // "AVM circuit. In this case, modify AVM_V2_VERIFICATION_KEY_LENGTH_IN_FIELDS \n"
129 // "in constants.nr accordingly.");
130
131 public:
153
154 // Even though we only need the witness entities, we hold all entities because it's
155 // easier and will not make much of a difference.
156 template <typename DataType> class WitnessEntities : public AllEntities<DataType> {
157 private:
158 // Obscure get_all since we redefine it.
161
162 public:
166 };
167
168 // Even though we only need the precomputed entities, we hold all entities because it's
169 // easier and will not make much of a difference.
170 template <typename DataType> class PrecomputedEntities : public AllEntities<DataType> {
171 private:
172 // Obscure get_all since we redefine it.
175
176 public:
180 };
181
183 public:
185
186 std::array<Commitment, NUM_WITNESS_ENTITIES> commitments;
187
189 std::array<FF, NUM_ALL_ENTITIES> sumcheck_evaluations;
190 std::vector<Commitment> gemini_fold_comms;
191 std::vector<FF> gemini_fold_evals;
194
195 Transcript() = default;
196
199 };
200
201 class ProvingKey : public AllEntities<Polynomial> {
202 private:
203 // Obscure get_all since it would be incorrect.
206
207 public:
208 using FF = typename Polynomial::FF;
209
210 static constexpr size_t circuit_size = MAX_AVM_TRACE_SIZE; // Fixed size
211 static constexpr size_t log_circuit_size = MAX_AVM_TRACE_LOG_SIZE;
212
213 ProvingKey();
214
218
220
221 // The number of public inputs has to be the same for all instances because they are
222 // folded element by element.
223 std::vector<FF> public_inputs;
224 };
225
227 : public NativeVerificationKey_<PrecomputedEntities<Commitment>, Transcript, VKSerializationMode::NO_METADATA> {
228 public:
230
231 VerificationKey() = default;
232
233 VerificationKey(const std::shared_ptr<ProvingKey>& proving_key)
234 {
236 for (auto [polynomial, commitment] : zip_view(proving_key->get_precomputed(), this->get_all())) {
237 commitment = proving_key->commitment_key.commit(polynomial);
238 }
239 }
240
242 {
244 for (auto [vk_cmt, cmt] : zip_view(this->get_all(), precomputed_cmts)) {
245 vk_cmt = cmt;
246 }
247 }
248
253 fr hash_through_transcript([[maybe_unused]] const std::string& domain_separator,
254 [[maybe_unused]] Transcript& transcript) const override
255 {
256 throw_or_abort("Not intended to be used because vk is hardcoded in circuit.");
257 }
258 };
259
260 // Used by sumcheck.
262
263 template <typename Polynomials> class PolynomialEntitiesAtFixedRow {
264 public:
267 , pp(pp)
268 {}
269
270 // Only const-access is allowed here. That's all that the logderivative library requires.
271 const auto& get(ColumnAndShifts c) const { return pp.get(c)[row_idx]; }
272
273 private:
274 const size_t row_idx;
276 };
277
281 class ProverPolynomials : public AllEntities<Polynomial> {
282 public:
283 // Define all operations as default, except copy construction/assignment
284 ProverPolynomials() = default;
287 ProverPolynomials(ProverPolynomials&& o) noexcept = default;
290
291 ProverPolynomials(ProvingKey& proving_key);
292 // For partially evaluated multivariates.
293 // TODO(fcarreiro): Reconsider its place.
294 ProverPolynomials(const ProverPolynomials& full_polynomials, size_t circuit_size);
295
296 // Only const-access is allowed here. That's all that the logderivative library requires.
297 // https://github.com/AztecProtocol/aztec-packages/blob/e50d8e0/barretenberg/cpp/src/barretenberg/honk/proof_system/logderivative_library.hpp#L44.
298 PolynomialEntitiesAtFixedRow<ProverPolynomials> get_row(size_t row_idx) const { return { row_idx, *this }; }
299 };
300
302
308 : private AllEntities<std::unique_ptr<bb::Univariate<FF, MAX_PARTIAL_RELATION_LENGTH>>> {
309 public:
313
314 void set_current_edge(size_t edge_idx);
316
317 private:
318 size_t current_edge = 0;
319 mutable bool dirty = false;
321 };
322
327 // TODO(fcarreiro): This is only required because of the Flavor::USE_SHORT_MONOMIALS conditional in
328 // SumcheckProverRound. The conditional should be improved to not require this.
329 template <size_t LENGTH> using ProverUnivariates = int;
330
336
337 // Templated for use in recursive verifier
338 template <typename Commitment_, typename VerificationKey>
339 class VerifierCommitments_ : public AllEntities<Commitment_> {
340 private:
342
343 public:
344 VerifierCommitments_(const std::shared_ptr<VerificationKey>& verification_key)
345 {
346 for (auto [commitment, vk_commitment] : zip_view(this->get_precomputed(), verification_key->get_all())) {
347 commitment = vk_commitment;
348 }
349 }
350 };
351
352 // Native version of the verifier commitments
354};
355
356} // 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:161
A univariate polynomial represented by its values on {domain_start, domain_start + 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:137
std::span< const DataType > get_all() const
Definition flavor.hpp:138
std::span< const std::string > get_labels() const
Definition flavor.hpp:139
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:150
const DataType & get(ColumnAndShifts c) const
Definition flavor.hpp:151
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:135
DEFINE_AVM_GETTER(shifted, SHIFTED_START_IDX, NUM_SHIFTED_ENTITIES)
A container for univariates used during sumcheck.
Definition flavor.hpp:308
const bb::Univariate< FF, MAX_PARTIAL_RELATION_LENGTH > & get(ColumnAndShifts c) const
Definition flavor.cpp:100
LazilyExtendedProverUnivariates(const ProverPolynomials &multivariates)
Definition flavor.hpp:310
PolynomialEntitiesAtFixedRow(const size_t row_idx, const Polynomials &pp)
Definition flavor.hpp:265
const auto & get(ColumnAndShifts c) const
Definition flavor.hpp:271
std::span< const std::string > get_labels() const
Definition flavor.hpp:179
std::span< DataType > get_all()
Definition flavor.hpp:177
std::span< const DataType > get_all() const
Definition flavor.hpp:178
A container for the prover polynomials handles.
Definition flavor.hpp:281
PolynomialEntitiesAtFixedRow< ProverPolynomials > get_row(size_t row_idx) const
Definition flavor.hpp:298
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:223
std::span< const Polynomial > get_all() const
Definition flavor.hpp:216
std::span< Polynomial > get_all()
Definition flavor.hpp:215
static constexpr size_t log_circuit_size
Definition flavor.hpp:211
static constexpr size_t circuit_size
Definition flavor.hpp:210
std::span< const std::string > get_labels() const
Definition flavor.hpp:217
typename Polynomial::FF FF
Definition flavor.hpp:208
std::vector< Commitment > gemini_fold_comms
Definition flavor.hpp:190
std::array< FF, NUM_ALL_ENTITIES > sumcheck_evaluations
Definition flavor.hpp:189
std::array< Commitment, NUM_WITNESS_ENTITIES > commitments
Definition flavor.hpp:186
std::vector< bb::Univariate< FF, BATCHED_RELATION_PARTIAL_LENGTH > > sumcheck_univariates
Definition flavor.hpp:188
std::vector< FF > gemini_fold_evals
Definition flavor.hpp:191
static constexpr size_t NUM_PRECOMPUTED_COMMITMENTS
Definition flavor.hpp:229
fr hash_through_transcript(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:253
VerificationKey(const std::shared_ptr< ProvingKey > &proving_key)
Definition flavor.hpp:233
VerificationKey(std::array< Commitment, NUM_PRECOMPUTED_COMMITMENTS > const &precomputed_cmts)
Definition flavor.hpp:241
VerifierCommitments_(const std::shared_ptr< VerificationKey > &verification_key)
Definition flavor.hpp:344
std::span< const std::string > get_labels() const
Definition flavor.hpp:165
std::span< DataType > get_all()
Definition flavor.hpp:163
std::span< const DataType > get_all() const
Definition flavor.hpp:164
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:105
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
std::array< FF, NUM_SUBRELATIONS - 1 > SubrelationSeparators
Definition flavor.hpp:86
static constexpr size_t NUM_FRS_FR
Definition flavor.hpp:101
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:96
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:88
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:95
static constexpr size_t NUM_FRS_COM
Definition flavor.hpp:100
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:98
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:11
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:10
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)