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
9
14
19
22
23namespace bb::avm2 {
24
25// Metaprogramming to concatenate tuple types.
26template <typename... input_t> using tuple_cat_t = decltype(flat_tuple::tuple_cat(std::declval<input_t>()...));
27
28class AvmFlavor {
29 public:
33
42
43 // indicates when evaluating sumcheck, edges must be extended to be MAX_TOTAL_RELATION_LENGTH
44 static constexpr bool USE_SHORT_MONOMIALS = false;
45
46 // This flavor would not be used with ZK Sumcheck
47 static constexpr bool HasZK = false;
48
49 // To achieve fixed proof size and that the recursive verifier circuit is constant, we are using padding in Sumcheck
50 // and Shplemini
51 static constexpr bool USE_PADDING = true;
52
56 static constexpr size_t NUM_WIRES = AvmFlavorVariables::NUM_WIRES;
58
59 // In the sumcheck univariate computation, we divide the trace in chunks and each chunk is
60 // evenly processed by all the threads. This constant defines the maximum number of rows
61 // that a given thread will process per chunk. This constant is assumed to be a power of 2
62 // greater or equal to 2.
63 // The current constant 32 is the result of time measurements using 16 threads and against
64 // bulk test v2. It was performed at a stage where the trace was not large.
65 // We note that all the experiments with constants below 256 did not exhibit any significant differences.
66 // TODO: Fine-tune the following constant when avm is close to completion.
67 static constexpr size_t MAX_CHUNK_THREAD_PORTION_SIZE = 32;
68
69 // Need to be templated for recursive verifier
71
73
74 // Need to be templated for recursive verifier
76
78
79 // Need to be templated for recursive verifier
82
83 static constexpr size_t NUM_SUBRELATIONS = compute_number_of_subrelations<Relations>();
84
85 using SubrelationSeparators = std::array<FF, NUM_SUBRELATIONS - 1>;
86
87 static constexpr size_t MAX_PARTIAL_RELATION_LENGTH = compute_max_partial_relation_length<Relations>();
88
89 static_assert(MAX_PARTIAL_RELATION_LENGTH < 8, "MAX_PARTIAL_RELATION_LENGTH must be less than 8");
90
91 // BATCHED_RELATION_PARTIAL_LENGTH = algebraic degree of sumcheck relation *after* multiplying by the `pow_zeta`
92 // random polynomial e.g. For \sum(x) [A(x) * B(x) + C(x)] * PowZeta(X), relation length = 2 and random relation
93 // length = 3
96
97 static constexpr bool has_zero_row = true;
98
99 static constexpr size_t NUM_FRS_COM = field_conversion::calc_num_bn254_frs<Commitment>();
100 static constexpr size_t NUM_FRS_FR = field_conversion::calc_num_bn254_frs<FF>();
101
102 // After any circuit changes, hover `COMPUTED_AVM_PROOF_LENGTH_IN_FIELDS` in your IDE
103 // to see its value and then update `AVM_V2_PROOF_LENGTH_IN_FIELDS` in constants.nr.
104 static constexpr size_t COMPUTED_AVM_PROOF_LENGTH_IN_FIELDS =
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 // - circuit size encoded as a fr field element
121 // - num of inputs encoded as a fr field element
122 // - NUM_PRECOMPUTED_ENTITIES commitments
123 // TODO(#13390): Revive the following code once we freeze the number of colums in AVM.
124 // static_assert(AVM_V2_VERIFICATION_KEY_LENGTH_IN_FIELDS == 2 * NUM_FRS_FR + NUM_PRECOMPUTED_ENTITIES *
125 // NUM_FRS_COM,
126 // "\nUnexpected AVM V2 VK length. This might be due to some changes in the\n"
127 // "AVM circuit. In this case, modify AVM_V2_VERIFICATION_KEY_LENGTH_IN_FIELDS \n"
128 // "in constants.nr accordingly.");
129
130 template <typename DataType_> class PrecomputedEntities {
131 public:
132 using DataType = DataType_;
134 };
135
136 private:
137 template <typename DataType> class WireEntities {
138 public:
140 };
141
142 template <typename DataType> class DerivedWitnessEntities {
143 public:
145 };
146
147 template <typename DataType> class ShiftedEntities {
148 public:
150 };
151
152 template <typename DataType, typename PrecomputedAndWitnessEntitiesSuperset>
153 static auto get_to_be_shifted(PrecomputedAndWitnessEntitiesSuperset& entities)
154 {
156 }
157
158 public:
159 template <typename DataType>
169
170 template <typename DataType_>
171 class AllEntities : public PrecomputedEntities<DataType_>,
172 public WitnessEntities<DataType_>,
173 public ShiftedEntities<DataType_> {
174 public:
177
182
183 static const auto& get_unshifted_labels()
184 {
185 static const auto labels =
187 return labels;
188 }
189
190 auto get_to_be_shifted() { return AvmFlavor::get_to_be_shifted<DataType>(*this); }
193
194 // We need both const and non-const versions.
196 const DataType& get(ColumnAndShifts c) const { return get_entity_by_column(*this, c); }
197 };
198
200 public:
202
203 std::array<Commitment, NUM_WITNESS_ENTITIES> commitments;
204
206 std::array<FF, NUM_ALL_ENTITIES> sumcheck_evaluations;
207 std::vector<Commitment> gemini_fold_comms;
208 std::vector<FF> gemini_fold_evals;
211
212 Transcript() = default;
213
216 };
217
218 class ProvingKey : public PrecomputedEntities<Polynomial>, public WitnessEntities<Polynomial> {
219 public:
220 using FF = typename Polynomial::FF;
222
223 size_t circuit_size = MAX_AVM_TRACE_SIZE; // Fixed size
225
226 ProvingKey();
227
229
230 // The number of public inputs has to be the same for all instances because they are
231 // folded element by element.
232 std::vector<FF> public_inputs;
233
237 auto get_to_be_shifted() { return AvmFlavor::get_to_be_shifted<Polynomial>(*this); }
238 };
239
240 class VerificationKey : public NativeVerificationKey_<PrecomputedEntities<Commitment>, Transcript> {
241 public:
243
244 VerificationKey() = default;
245
246 VerificationKey(const std::shared_ptr<ProvingKey>& proving_key)
247 {
249 for (auto [polynomial, commitment] :
250 zip_view(proving_key->get_precomputed_polynomials(), this->get_all())) {
251 commitment = proving_key->commitment_key.commit(polynomial);
252 }
253 }
254
256 {
258 for (auto [vk_cmt, cmt] : zip_view(this->get_all(), precomputed_cmts)) {
259 vk_cmt = cmt;
260 }
261 }
262
263 std::vector<fr> to_field_elements() const override;
268 fr hash_through_transcript([[maybe_unused]] const std::string& domain_separator,
269 [[maybe_unused]] Transcript& transcript) const override
270 {
271 throw_or_abort("Not intended to be used because vk is hardcoded in circuit.");
272 }
273 };
274
275 // Used by sumcheck.
277
278 template <typename Polynomials> class PolynomialEntitiesAtFixedRow {
279 public:
282 , pp(pp)
283 {}
284
285 // Only const-access is allowed here. That's all that the logderivative library requires.
286 const auto& get(ColumnAndShifts c) const { return pp.get(c)[row_idx]; }
287
288 private:
289 const size_t row_idx;
291 };
292
296 class ProverPolynomials : public AllEntities<Polynomial> {
297 public:
298 // Define all operations as default, except copy construction/assignment
299 ProverPolynomials() = default;
302 ProverPolynomials(ProverPolynomials&& o) noexcept = default;
305
306 ProverPolynomials(ProvingKey& proving_key);
307
308 // Only const-access is allowed here. That's all that the logderivative library requires.
309 // https://github.com/AztecProtocol/aztec-packages/blob/e50d8e0/barretenberg/cpp/src/barretenberg/honk/proof_system/logderivative_library.hpp#L44.
310 PolynomialEntitiesAtFixedRow<ProverPolynomials> get_row(size_t row_idx) const { return { row_idx, *this }; }
311 };
312
313 class PartiallyEvaluatedMultivariates : public AllEntities<Polynomial> {
314 public:
316 PartiallyEvaluatedMultivariates(const size_t circuit_size);
317 PartiallyEvaluatedMultivariates(const ProverPolynomials& full_polynomials, size_t circuit_size);
318 };
319
325
331 template <size_t LENGTH, size_t SKIP_COUNT>
333
338
344
345 // Templated for use in recursive verifier
346 template <typename Commitment_, typename VerificationKey>
347 class VerifierCommitments_ : public AllEntities<Commitment_> {
348 private:
350
351 public:
352 VerifierCommitments_(const std::shared_ptr<VerificationKey>& verification_key)
353 {
354 for (auto [commitment, vk_commitment] : zip_view(this->get_precomputed(), verification_key->get_all())) {
355 commitment = vk_commitment;
356 }
357 }
358 };
359
360 // Native version of the verifier commitments
362};
363
364} // 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:152
A template class for a reference array. Behaves as if std::array<T&, N> was possible.
Definition ref_array.hpp:22
DataType & get(ColumnAndShifts c)
Definition flavor.hpp:195
static const auto & get_unshifted_labels()
Definition flavor.hpp:183
const DataType & get(ColumnAndShifts c) const
Definition flavor.hpp:196
PolynomialEntitiesAtFixedRow(const size_t row_idx, const Polynomials &pp)
Definition flavor.hpp:280
const auto & get(ColumnAndShifts c) const
Definition flavor.hpp:286
A container for the prover polynomials handles.
Definition flavor.hpp:296
PolynomialEntitiesAtFixedRow< ProverPolynomials > get_row(size_t row_idx) const
Definition flavor.hpp:310
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:232
DEFINE_COMPOUND_GET_ALL(PrecomputedEntities< Polynomial >, WitnessEntities< Polynomial >)
typename Polynomial::FF FF
Definition flavor.hpp:220
std::vector< Commitment > gemini_fold_comms
Definition flavor.hpp:207
std::array< FF, NUM_ALL_ENTITIES > sumcheck_evaluations
Definition flavor.hpp:206
std::array< Commitment, NUM_WITNESS_ENTITIES > commitments
Definition flavor.hpp:203
std::vector< bb::Univariate< FF, BATCHED_RELATION_PARTIAL_LENGTH > > sumcheck_univariates
Definition flavor.hpp:205
std::vector< FF > gemini_fold_evals
Definition flavor.hpp:208
static constexpr size_t NUM_PRECOMPUTED_COMMITMENTS
Definition flavor.hpp:242
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:268
std::vector< fr > to_field_elements() const override
Serialize verification key to field elements.
Definition flavor.cpp:106
VerificationKey(const std::shared_ptr< ProvingKey > &proving_key)
Definition flavor.hpp:246
VerificationKey(std::array< Commitment, NUM_PRECOMPUTED_COMMITMENTS > const &precomputed_cmts)
Definition flavor.hpp:255
VerifierCommitments_(const std::shared_ptr< VerificationKey > &verification_key)
Definition flavor.hpp:352
static const auto & get_derived_labels()
Definition flavor.hpp:167
static const auto & get_wires_labels()
Definition flavor.hpp:165
AvmFlavorSettings::GroupElement GroupElement
Definition flavor.hpp:37
tuple_cat_t< MainRelations_< FF_ >, LookupRelations_< FF_ > > Relations_
Definition flavor.hpp:80
static auto get_to_be_shifted(PrecomputedAndWitnessEntitiesSuperset &entities)
Definition flavor.hpp:153
static constexpr size_t COMPUTED_AVM_PROOF_LENGTH_IN_FIELDS
Definition flavor.hpp:104
AvmFlavorSettings::PolynomialHandle PolynomialHandle
Definition flavor.hpp:36
static constexpr size_t NUM_SUBRELATIONS
Definition flavor.hpp:83
static constexpr size_t NUM_SHIFTED_ENTITIES
Definition flavor.hpp:55
AvmFlavorSettings::CommitmentHandle CommitmentHandle
Definition flavor.hpp:39
AvmFlavorSettings::FF FF
Definition flavor.hpp:34
std::array< FF, NUM_SUBRELATIONS - 1 > SubrelationSeparators
Definition flavor.hpp:85
static constexpr size_t NUM_FRS_FR
Definition flavor.hpp:100
static constexpr bool USE_PADDING
Definition flavor.hpp:51
static constexpr size_t MAX_CHUNK_THREAD_PORTION_SIZE
Definition flavor.hpp:67
static constexpr bool HasZK
Definition flavor.hpp:47
static constexpr size_t NUM_RELATIONS
Definition flavor.hpp:95
static constexpr size_t NUM_WITNESS_ENTITIES
Definition flavor.hpp:54
AvmFlavorSettings::G1 G1
Definition flavor.hpp:31
static constexpr size_t MAX_PARTIAL_RELATION_LENGTH
Definition flavor.hpp:87
static constexpr size_t NUM_WIRES
Definition flavor.hpp:56
static constexpr bool USE_SHORT_MONOMIALS
Definition flavor.hpp:44
static constexpr size_t BATCHED_RELATION_PARTIAL_LENGTH
Definition flavor.hpp:94
static constexpr size_t NUM_FRS_COM
Definition flavor.hpp:99
static constexpr size_t NUM_PRECOMPUTED_ENTITIES
Definition flavor.hpp:53
AvmFlavorSettings::Commitment Commitment
Definition flavor.hpp:38
static constexpr size_t NUM_ALL_ENTITIES
Definition flavor.hpp:57
Relations_< FF > Relations
Definition flavor.hpp:81
static constexpr bool has_zero_row
Definition flavor.hpp:97
bb::VerifierCommitmentKey< Curve > VerifierCommitmentKey
G1::affine_element CommitmentHandle
bb::Polynomial< FF > Polynomial
bb::CommitmentKey< Curve > CommitmentKey
#define AVM2_DERIVED_WITNESS_ENTITIES
Definition columns.hpp:20
#define AVM2_SHIFTED_ENTITIES
Definition columns.hpp:21
#define AVM2_TO_BE_SHIFTED_E(e)
Definition columns.hpp:15
#define AVM2_WIRE_ENTITIES
Definition columns.hpp:19
#define AVM2_PRECOMPUTED_ENTITIES
Definition columns.hpp:18
Base class templates for structures that contain data parameterized by the fundamental polynomials of...
#define DEFINE_FLAVOR_MEMBERS(DataType,...)
Define the body of a flavor class, included each member and a pointer view with which to iterate the ...
#define DEFINE_COMPOUND_GET_ALL(...)
auto & get_entity_by_column(Entities &entities, ColumnAndShifts c)
Definition entities.hpp:10
decltype(flat_tuple::tuple_cat(std::declval< input_t >()...)) tuple_cat_t
Definition flavor.hpp:26
constexpr size_t MAX_AVM_TRACE_LOG_SIZE
Definition constants.hpp:9
constexpr size_t MAX_AVM_TRACE_SIZE
Definition constants.hpp:10
ColumnAndShifts
Definition columns.hpp:35
RefArray< T,(Ns+...)> constexpr concatenate(const RefArray< T, Ns > &... ref_arrays)
Concatenates multiple RefArray objects into a single RefArray.
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)