Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
mega_flavor.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: Planned, auditors: [], commit: }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
7#pragma once
8#include <utility>
9
31
32namespace bb {
33
35 public:
41 using PCS = KZG<Curve>;
46 using Codec = FrCodec;
49
50 // An upper bound on the size of the Mega-circuits. `CONST_FOLDING_LOG_N` bounds the log circuit sizes in the Chonk
51 // context.
52 static constexpr size_t VIRTUAL_LOG_N = CONST_FOLDING_LOG_N;
53 // indicates when evaluating sumcheck, edges can be left as degree-1 monomials
54 static constexpr bool USE_SHORT_MONOMIALS = true;
55 // Indicates that this flavor runs with non-ZK Sumcheck.
56 static constexpr bool HasZK = false;
57 // To achieve fixed proof size and that the recursive verifier circuit is constant, we are using padding in Sumcheck
58 // and Shplemini
59 static constexpr bool USE_PADDING = true;
60 static constexpr size_t NUM_WIRES = CircuitBuilder::NUM_WIRES;
61 // The number of multivariate polynomials on which a sumcheck prover sumcheck operates (including shifts). We often
62 // need containers of this size to hold related data, so we choose a name more agnostic than `NUM_POLYNOMIALS`.
63 static constexpr size_t NUM_ALL_ENTITIES = 60;
64 // The number of polynomials precomputed to describe a circuit and to aid a prover in constructing a satisfying
65 // assignment of witnesses. We again choose a neutral name.
66 static constexpr size_t NUM_PRECOMPUTED_ENTITIES = 31;
67 // The total number of witness entities not including shifts.
68 static constexpr size_t NUM_WITNESS_ENTITIES = 24;
69 // The number of shifted witness entities including derived witness entities
70 static constexpr size_t NUM_SHIFTED_ENTITIES = 5;
71 // The number of unshifted witness entities
73
76
77 // Size of the final PCS MSM after KZG adds quotient commitment:
78 // 1 (Shplonk Q) + NUM_UNSHIFTED + (log_n - 1) Gemini folds + 1 (G1 identity) + 1 (KZG W)
79 // (shifted commitments are removed as duplicates)
80 static constexpr size_t FINAL_PCS_MSM_SIZE(size_t log_n = VIRTUAL_LOG_N)
81 {
82 return NUM_UNSHIFTED_ENTITIES + log_n + 2;
83 }
84
85 // define the tuple of Relations that comprise the Sumcheck relation
86 // Note: made generic for use in MegaRecursive.
87 template <typename FF>
100
101 static constexpr size_t MAX_PARTIAL_RELATION_LENGTH = compute_max_partial_relation_length<Relations>();
102 // BATCHED_RELATION_PARTIAL_LENGTH = algebraic degree of sumcheck relation *after* multiplying by the `pow_zeta`
103 // random polynomial e.g. For \sum(x) [A(x) * B(x) + C(x)] * PowZeta(X), relation length = 2 and random relation
104 // length = 3
107
108 static constexpr size_t num_frs_comm = FrCodec::calc_num_fields<Commitment>();
109 static constexpr size_t num_frs_fr = FrCodec::calc_num_fields<FF>();
110
111 // A challenge whose powers are used to batch subrelation contributions during Sumcheck
112 static constexpr size_t NUM_SUBRELATIONS = compute_number_of_subrelations<Relations>();
114
131 template <typename DataType_> class PrecomputedEntities {
132 public:
133 bool operator==(const PrecomputedEntities&) const = default;
134 using DataType = DataType_;
136 q_m, // column 0
137 q_c, // column 1
138 q_l, // column 2
139 q_r, // column 3
140 q_o, // column 4
141 q_4, // column 5
142 q_busread, // column 6
143 q_lookup, // column 7
144 q_arith, // column 8
145 q_delta_range, // column 9
146 q_elliptic, // column 10
147 q_memory, // column 11
148 q_nnf, // column 12
149 q_poseidon2_external, // column 13
150 q_poseidon2_internal, // column 14
151 sigma_1, // column 15
152 sigma_2, // column 16
153 sigma_3, // column 17
154 sigma_4, // column 18
155 id_1, // column 19
156 id_2, // column 20
157 id_3, // column 21
158 id_4, // column 22
159 table_1, // column 23
160 table_2, // column 24
161 table_3, // column 25
162 table_4, // column 26
163 lagrange_first, // column 27
164 lagrange_last, // column 28
165 lagrange_ecc_op, // column 29 // indicator poly for ecc op gates
166 databus_id // column 30 // id polynomial, i.e. id_i = i
167 )
168
169 auto get_non_gate_selectors() { return RefArray{ q_m, q_c, q_l, q_r, q_o, q_4 }; };
171 {
172 return RefArray{
173 q_busread,
174 q_lookup,
175 q_arith,
176 q_delta_range,
177 q_elliptic,
178 q_memory,
179 q_nnf,
180 q_poseidon2_external,
181 q_poseidon2_internal,
182 };
183 }
184 auto get_selectors() { return concatenate(get_non_gate_selectors(), get_gate_selectors()); }
185
186 auto get_sigmas() { return RefArray{ sigma_1, sigma_2, sigma_3, sigma_4 }; };
187 auto get_ids() { return RefArray{ id_1, id_2, id_3, id_4 }; };
188 auto get_tables() { return RefArray{ table_1, table_2, table_3, table_4 }; };
189 };
190
191 // Mega needs to expose more public classes than most flavors due to MegaRecursive reuse, but these
192 // are internal:
193
194 // WireEntities for basic witness entities
195 template <typename DataType> class WireEntities {
196 public:
198 w_l, // column 0
199 w_r, // column 1
200 w_o, // column 2
201 w_4); // column 3
202 };
203
204 // DerivedEntities for derived witness entities
205 template <typename DataType> class DerivedEntities {
206 public:
208 z_perm, // column 4
209 lookup_inverses, // column 5
210 lookup_read_counts, // column 6
211 lookup_read_tags, // column 7
212 ecc_op_wire_1, // column 8
213 ecc_op_wire_2, // column 9
214 ecc_op_wire_3, // column 10
215 ecc_op_wire_4, // column 11
216 calldata, // column 12
217 calldata_read_counts, // column 13
218 calldata_read_tags, // column 14
219 calldata_inverses, // column 15
220 secondary_calldata, // column 16
221 secondary_calldata_read_counts, // column 17
222 secondary_calldata_read_tags, // column 18
223 secondary_calldata_inverses, // column 19
224 return_data, // column 20
225 return_data_read_counts, // column 21
226 return_data_read_tags, // column 22
227 return_data_inverses); // column 23
228 auto get_to_be_shifted() { return RefArray{ z_perm }; };
229 };
230
235 template <typename DataType, bool HasZK_ = false> class MaskingEntities {
236 public:
237 // When ZK is disabled, this class is empty
238 auto get_all() { return RefArray<DataType, 0>{}; }
239 auto get_all() const { return RefArray<const DataType, 0>{}; }
240 static auto get_labels() { return std::vector<std::string>{}; }
241 };
242
243 // Specialization for when ZK is enabled
244 template <typename DataType> class MaskingEntities<DataType, true> {
245 public:
246 DEFINE_FLAVOR_MEMBERS(DataType, gemini_masking_poly)
247 };
248
254 template <typename DataType>
255 class WitnessEntities_ : public WireEntities<DataType>, public DerivedEntities<DataType> {
256 public:
258
261 {
262 return RefArray{ this->ecc_op_wire_1, this->ecc_op_wire_2, this->ecc_op_wire_3, this->ecc_op_wire_4 };
263 }
264 auto get_databus_entities() // Excludes the derived inverse polynomials
265 {
266 return RefArray{
267 this->calldata, this->calldata_read_counts, this->calldata_read_tags,
268 this->secondary_calldata, this->secondary_calldata_read_counts, this->secondary_calldata_read_tags,
269 this->return_data, this->return_data_read_counts, this->return_data_read_tags
270 };
271 }
272
274 {
275 return RefArray{
276 this->calldata_inverses,
277 this->secondary_calldata_inverses,
278 this->return_data_inverses,
279 };
280 }
285 };
286
287 // Default WitnessEntities alias
288 template <typename DataType> using WitnessEntities = WitnessEntities_<DataType>;
289
293 template <typename DataType> class ShiftedEntities {
294 public:
295 DEFINE_FLAVOR_MEMBERS(DataType,
296 w_l_shift, // column 0
297 w_r_shift, // column 1
298 w_o_shift, // column 2
299 w_4_shift, // column 3
300 z_perm_shift) // column 4
301 };
302
303 public:
313 template <typename DataType, bool HasZK_ = HasZK>
335
336 // Default AllEntities alias (no ZK)
337 template <typename DataType> using AllEntities = AllEntities_<DataType, HasZK>;
338
343 template <bool HasZK_ = HasZK> class AllValues_ : public AllEntities_<FF, HasZK_> {
344 public:
346 using Base::Base;
347 };
348
350
354 template <bool HasZK_ = HasZK> class ProverPolynomials_ : public AllEntities_<Polynomial, HasZK_> {
355 public:
356 // Define all operations as default, except copy construction/assignment
358 // fully-formed constructor
359 ProverPolynomials_(size_t circuit_size)
360 {
361 BB_BENCH_NAME("ProverPolynomials(size_t)");
362
363 for (auto& poly : this->get_to_be_shifted()) {
364 poly = Polynomial{ /*memory size*/ circuit_size - 1,
365 /*largest possible index*/ circuit_size,
366 /* offset */ 1 };
367 }
368 // catch-all with fully formed polynomials
369 for (auto& poly : this->get_unshifted()) {
370 if (poly.is_empty()) {
371 // Not set above
372 poly = Polynomial{ /*memory size*/ circuit_size, /*largest possible index*/ circuit_size };
373 }
374 }
375 set_shifted();
376 }
379 ProverPolynomials_(ProverPolynomials_&& o) noexcept = default;
382 [[nodiscard]] size_t get_polynomial_size() const { return this->q_c.size(); }
383 [[nodiscard]] AllValues_<HasZK_> get_row(size_t row_idx) const
384 {
385 AllValues_<HasZK_> result;
386 for (auto [result_field, polynomial] : zip_view(result.get_all(), this->get_all())) {
387 result_field = polynomial[row_idx];
388 }
389 return result;
390 }
391
393 {
394 AllValues_<HasZK_> result;
395 for (auto [result_field, polynomial] : zip_view(result.get_sigmas(), this->get_sigmas())) {
396 result_field = polynomial[row_idx];
397 }
398 for (auto [result_field, polynomial] : zip_view(result.get_ids(), this->get_ids())) {
399 result_field = polynomial[row_idx];
400 }
401 for (auto [result_field, polynomial] : zip_view(result.get_wires(), this->get_wires())) {
402 result_field = polynomial[row_idx];
403 }
404 return result;
405 }
406
408 {
409 for (auto [shifted, to_be_shifted] : zip_view(this->get_shifted(), this->get_to_be_shifted())) {
410 shifted = to_be_shifted.shifted();
411 }
412 }
413
414 void increase_polynomials_virtual_size(const size_t size_in)
415 {
416 for (auto& polynomial : this->get_all()) {
417 polynomial.increase_virtual_size(size_in);
418 }
419 }
420 };
421
423
425
436
438
442 template <bool HasZK_ = HasZK>
445
447
453
458
463
470 class CommitmentLabels : public AllEntities<std::string> {
471 public:
473 {
474 w_l = "W_L";
475 w_r = "W_R";
476 w_o = "W_O";
477 w_4 = "W_4";
478 z_perm = "Z_PERM";
479 lookup_inverses = "LOOKUP_INVERSES";
480 lookup_read_counts = "LOOKUP_READ_COUNTS";
481 lookup_read_tags = "LOOKUP_READ_TAGS";
482 ecc_op_wire_1 = "ECC_OP_WIRE_1";
483 ecc_op_wire_2 = "ECC_OP_WIRE_2";
484 ecc_op_wire_3 = "ECC_OP_WIRE_3";
485 ecc_op_wire_4 = "ECC_OP_WIRE_4";
486 calldata = "CALLDATA";
487 calldata_read_counts = "CALLDATA_READ_COUNTS";
488 calldata_read_tags = "CALLDATA_READ_TAGS";
489 calldata_inverses = "CALLDATA_INVERSES";
490 secondary_calldata = "SECONDARY_CALLDATA";
491 secondary_calldata_read_counts = "SECONDARY_CALLDATA_READ_COUNTS";
492 secondary_calldata_read_tags = "SECONDARY_CALLDATA_READ_TAGS";
493 secondary_calldata_inverses = "SECONDARY_CALLDATA_INVERSES";
494 return_data = "RETURN_DATA";
495 return_data_read_counts = "RETURN_DATA_READ_COUNTS";
496 return_data_read_tags = "RETURN_DATA_READ_TAGS";
497 return_data_inverses = "RETURN_DATA_INVERSES";
498
499 q_c = "Q_C";
500 q_l = "Q_L";
501 q_r = "Q_R";
502 q_o = "Q_O";
503 q_4 = "Q_4";
504 q_m = "Q_M";
505 q_busread = "Q_BUSREAD";
506 q_lookup = "Q_LOOKUP";
507 q_arith = "Q_ARITH";
508 q_delta_range = "Q_SORT";
509 q_elliptic = "Q_ELLIPTIC";
510 q_memory = "Q_MEMORY";
511 q_nnf = "Q_NNF";
512 q_poseidon2_external = "Q_POSEIDON2_EXTERNAL";
513 q_poseidon2_internal = "Q_POSEIDON2_INTERNAL";
514 sigma_1 = "SIGMA_1";
515 sigma_2 = "SIGMA_2";
516 sigma_3 = "SIGMA_3";
517 sigma_4 = "SIGMA_4";
518 id_1 = "ID_1";
519 id_2 = "ID_2";
520 id_3 = "ID_3";
521 id_4 = "ID_4";
522 table_1 = "TABLE_1";
523 table_2 = "TABLE_2";
524 table_3 = "TABLE_3";
525 table_4 = "TABLE_4";
526 lagrange_first = "LAGRANGE_FIRST";
527 lagrange_last = "LAGRANGE_LAST";
528 lagrange_ecc_op = "Q_ECC_OP_QUEUE";
529 };
530 };
531
535 template <typename Commitment, typename VerificationKey, bool HasZK_ = HasZK>
536 class VerifierCommitments_ : public AllEntities_<Commitment, HasZK_> {
537 public:
538 VerifierCommitments_(const std::shared_ptr<VerificationKey>& verification_key,
539 const std::optional<WitnessEntities<Commitment>>& witness_commitments = std::nullopt)
540 {
541 // Copy the precomputed polynomial commitments into this
542 for (auto [precomputed, precomputed_in] : zip_view(this->get_precomputed(), verification_key->get_all())) {
543 precomputed = precomputed_in;
544 }
545
546 // If provided, copy the witness polynomial commitments into this
547 if (witness_commitments.has_value()) {
548 for (auto [witness, witness_in] :
549 zip_view(this->get_witness(), witness_commitments.value().get_all())) {
550 witness = witness_in;
551 }
552
553 // Set shifted commitments
554 this->w_l_shift = witness_commitments->w_l;
555 this->w_r_shift = witness_commitments->w_r;
556 this->w_o_shift = witness_commitments->w_o;
557 this->w_4_shift = witness_commitments->w_4;
558 this->z_perm_shift = witness_commitments->z_perm;
559 }
560 }
561 };
562 // Specialize for Mega (general case used in MegaRecursive).
564};
565
566} // namespace bb
#define BB_BENCH_NAME(name)
Definition bb_bench.hpp:225
Common transcript class for both parties. Stores the data for the current round, as well as the manif...
CommitmentKey object over a pairing group 𝔾₁.
A base class labelling all entities (for instance, all of the polynomials used by the prover during s...
DEFINE_COMPOUND_GET_ALL(MaskingEntities< DataType, HasZK_ >, PrecomputedEntities< DataType >, WitnessEntities_< DataType >, ShiftedEntities< DataType >) auto get_unshifted()
A field element for each entity of the flavor. These entities represent the prover polynomials evalua...
A container for commitment labels.
DEFINE_FLAVOR_MEMBERS(DataType, z_perm, lookup_inverses, lookup_read_counts, lookup_read_tags, ecc_op_wire_1, ecc_op_wire_2, ecc_op_wire_3, ecc_op_wire_4, calldata, calldata_read_counts, calldata_read_tags, calldata_inverses, secondary_calldata, secondary_calldata_read_counts, secondary_calldata_read_tags, secondary_calldata_inverses, return_data, return_data_read_counts, return_data_read_tags, return_data_inverses)
ZK-specific entities (only used when HasZK = true)
A base class labelling precomputed entities and (ordered) subsets of interest.
DEFINE_FLAVOR_MEMBERS(DataType, q_m, q_c, q_l, q_r, q_o, q_4, q_busread, q_lookup, q_arith, q_delta_range, q_elliptic, q_memory, q_nnf, q_poseidon2_external, q_poseidon2_internal, sigma_1, sigma_2, sigma_3, sigma_4, id_1, id_2, id_3, id_4, table_1, table_2, table_3, table_4, lagrange_first, lagrange_last, lagrange_ecc_op, databus_id) auto get_non_gate_selectors()
bool operator==(const PrecomputedEntities &) const =default
A container for the prover polynomials handles.
void increase_polynomials_virtual_size(const size_t size_in)
ProverPolynomials_ & operator=(ProverPolynomials_ &&o) noexcept=default
ProverPolynomials_(size_t circuit_size)
ProverPolynomials_(const ProverPolynomials_ &o)=delete
ProverPolynomials_ & operator=(const ProverPolynomials_ &)=delete
AllValues_< HasZK_ > get_row_for_permutation_arg(size_t row_idx)
AllValues_< HasZK_ > get_row(size_t row_idx) const
ProverPolynomials_(ProverPolynomials_ &&o) noexcept=default
Class for ShiftedEntities, containing the shifted witness polynomials.
VerifierCommitments_(const std::shared_ptr< VerificationKey > &verification_key, const std::optional< WitnessEntities< Commitment > > &witness_commitments=std::nullopt)
DEFINE_FLAVOR_MEMBERS(DataType, w_l, w_r, w_o, w_4)
Container for all witness polynomials used/constructed by the prover.
static constexpr size_t NUM_PRECOMPUTED_ENTITIES
Curve::ScalarField FF
std::tuple< bb::ArithmeticRelation< FF >, bb::UltraPermutationRelation< FF >, bb::LogDerivLookupRelation< FF >, bb::DeltaRangeConstraintRelation< FF >, bb::EllipticRelation< FF >, bb::MemoryRelation< FF >, bb::NonNativeFieldRelation< FF >, bb::EccOpQueueRelation< FF >, bb::DatabusLookupRelation< FF >, bb::Poseidon2ExternalRelation< FF >, bb::Poseidon2InternalRelation< FF > > Relations_
crypto::Poseidon2< crypto::Poseidon2Bn254ScalarFieldParams > HashFunction
static constexpr size_t NUM_SHIFTED_ENTITIES
static constexpr RepeatedCommitmentsData REPEATED_COMMITMENTS
static constexpr size_t NUM_WIRES
static constexpr size_t NUM_SUBRELATIONS
Curve::Element GroupElement
static constexpr size_t num_frs_fr
static constexpr size_t NUM_RELATIONS
static constexpr size_t NUM_ALL_ENTITIES
static constexpr bool HasZK
static constexpr size_t MAX_PARTIAL_RELATION_LENGTH
static constexpr size_t NUM_WITNESS_ENTITIES
static constexpr size_t VIRTUAL_LOG_N
static constexpr size_t FINAL_PCS_MSM_SIZE(size_t log_n=VIRTUAL_LOG_N)
Relations_< FF > Relations
static constexpr bool USE_SHORT_MONOMIALS
Curve::AffineElement Commitment
static constexpr bool USE_PADDING
static constexpr size_t NUM_UNSHIFTED_ENTITIES
static constexpr size_t BATCHED_RELATION_PARTIAL_LENGTH
static constexpr size_t num_frs_comm
Base Native verification key class.
Definition flavor.hpp:172
A container for storing the partially evaluated multivariates produced by sumcheck.
A template class for a reference array. Behaves as if std::array<T&, N> was possible.
Definition ref_array.hpp:22
A wrapper for Relations to expose methods used by the Sumcheck prover or verifier to add the contribu...
Wrapper holding a verification key and its precomputed hash.
Definition flavor.hpp:591
Representation of the Grumpkin Verifier Commitment Key inside a bn254 circuit.
typename Group::element Element
Definition bn254.hpp:21
typename Group::affine_element AffineElement
Definition bn254.hpp:22
bb::fr ScalarField
Definition bn254.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(...)
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
RefArray< T,(Ns+...)> constexpr concatenate(const RefArray< T, Ns > &... ref_arrays)
Concatenates multiple RefArray objects into a single RefArray.
MegaCircuitBuilder_< field< Bn254FrParams > > MegaCircuitBuilder
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::vector< MemoryValue > calldata
The precomputed data needed to compute a Honk VK.
Definition flavor.hpp:121