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 // Proof length formula methods
112 static constexpr size_t OINK_PROOF_LENGTH_WITHOUT_PUB_INPUTS =
113 /* 1. NUM_WITNESS_ENTITIES commitments */ (NUM_WITNESS_ENTITIES * num_frs_comm);
114
115 static constexpr size_t DECIDER_PROOF_LENGTH(size_t virtual_log_n = VIRTUAL_LOG_N)
116 {
117 return /* 2. virtual_log_n sumcheck univariates */
118 (virtual_log_n * BATCHED_RELATION_PARTIAL_LENGTH * num_frs_fr) +
119 /* 3. NUM_ALL_ENTITIES sumcheck evaluations */ (NUM_ALL_ENTITIES * num_frs_fr) +
120 /* 4. virtual_log_n - 1 Gemini Fold commitments */ ((virtual_log_n - 1) * num_frs_comm) +
121 /* 5. virtual_log_n Gemini a evaluations */ (virtual_log_n * num_frs_fr) +
122 /* 6. Shplonk Q commitment */ (num_frs_comm) +
123 /* 7. KZG W commitment */ (num_frs_comm);
124 }
125
126 static constexpr size_t PROOF_LENGTH_WITHOUT_PUB_INPUTS(size_t virtual_log_n = VIRTUAL_LOG_N)
127 {
129 }
130
131 // A challenge whose powers are used to batch subrelation contributions during Sumcheck
132 static constexpr size_t NUM_SUBRELATIONS = compute_number_of_subrelations<Relations>();
134
151 template <typename DataType_> class PrecomputedEntities {
152 public:
153 bool operator==(const PrecomputedEntities&) const = default;
154 using DataType = DataType_;
156 q_m, // column 0
157 q_c, // column 1
158 q_l, // column 2
159 q_r, // column 3
160 q_o, // column 4
161 q_4, // column 5
162 q_busread, // column 6
163 q_lookup, // column 7
164 q_arith, // column 8
165 q_delta_range, // column 9
166 q_elliptic, // column 10
167 q_memory, // column 11
168 q_nnf, // column 12
169 q_poseidon2_external, // column 13
170 q_poseidon2_internal, // column 14
171 sigma_1, // column 15
172 sigma_2, // column 16
173 sigma_3, // column 17
174 sigma_4, // column 18
175 id_1, // column 19
176 id_2, // column 20
177 id_3, // column 21
178 id_4, // column 22
179 table_1, // column 23
180 table_2, // column 24
181 table_3, // column 25
182 table_4, // column 26
183 lagrange_first, // column 27
184 lagrange_last, // column 28
185 lagrange_ecc_op, // column 29 // indicator poly for ecc op gates
186 databus_id // column 30 // id polynomial, i.e. id_i = i
187 )
188
189 auto get_non_gate_selectors() { return RefArray{ q_m, q_c, q_l, q_r, q_o, q_4 }; };
191 {
192 return RefArray{
193 q_busread,
194 q_lookup,
195 q_arith,
196 q_delta_range,
197 q_elliptic,
198 q_memory,
199 q_nnf,
200 q_poseidon2_external,
201 q_poseidon2_internal,
202 };
203 }
204 auto get_selectors() { return concatenate(get_non_gate_selectors(), get_gate_selectors()); }
205
206 auto get_sigmas() { return RefArray{ sigma_1, sigma_2, sigma_3, sigma_4 }; };
207 auto get_ids() { return RefArray{ id_1, id_2, id_3, id_4 }; };
208 auto get_tables() { return RefArray{ table_1, table_2, table_3, table_4 }; };
209 };
210
211 // Mega needs to expose more public classes than most flavors due to MegaRecursive reuse, but these
212 // are internal:
213
214 // WireEntities for basic witness entities
215 template <typename DataType> class WireEntities {
216 public:
218 w_l, // column 0
219 w_r, // column 1
220 w_o, // column 2
221 w_4); // column 3
222 };
223
224 // DerivedEntities for derived witness entities
225 template <typename DataType> class DerivedEntities {
226 public:
228 z_perm, // column 4
229 lookup_inverses, // column 5
230 lookup_read_counts, // column 6
231 lookup_read_tags, // column 7
232 ecc_op_wire_1, // column 8
233 ecc_op_wire_2, // column 9
234 ecc_op_wire_3, // column 10
235 ecc_op_wire_4, // column 11
236 calldata, // column 12
237 calldata_read_counts, // column 13
238 calldata_read_tags, // column 14
239 calldata_inverses, // column 15
240 secondary_calldata, // column 16
241 secondary_calldata_read_counts, // column 17
242 secondary_calldata_read_tags, // column 18
243 secondary_calldata_inverses, // column 19
244 return_data, // column 20
245 return_data_read_counts, // column 21
246 return_data_read_tags, // column 22
247 return_data_inverses); // column 23
248 auto get_to_be_shifted() { return RefArray{ z_perm }; };
249 };
250
255 template <typename DataType, bool HasZK_ = false> class MaskingEntities {
256 public:
257 // When ZK is disabled, this class is empty
258 auto get_all() { return RefArray<DataType, 0>{}; }
259 auto get_all() const { return RefArray<const DataType, 0>{}; }
260 static auto get_labels() { return std::vector<std::string>{}; }
261 };
262
263 // Specialization for when ZK is enabled
264 template <typename DataType> class MaskingEntities<DataType, true> {
265 public:
266 DEFINE_FLAVOR_MEMBERS(DataType, gemini_masking_poly)
267 };
268
274 template <typename DataType>
275 class WitnessEntities_ : public WireEntities<DataType>, public DerivedEntities<DataType> {
276 public:
278
281 {
282 return RefArray{ this->ecc_op_wire_1, this->ecc_op_wire_2, this->ecc_op_wire_3, this->ecc_op_wire_4 };
283 }
284 auto get_databus_entities() // Excludes the derived inverse polynomials
285 {
286 return RefArray{
287 this->calldata, this->calldata_read_counts, this->calldata_read_tags,
288 this->secondary_calldata, this->secondary_calldata_read_counts, this->secondary_calldata_read_tags,
289 this->return_data, this->return_data_read_counts, this->return_data_read_tags
290 };
291 }
292
294 {
295 return RefArray{
296 this->calldata_inverses,
297 this->secondary_calldata_inverses,
298 this->return_data_inverses,
299 };
300 }
305 };
306
307 // Default WitnessEntities alias
308 template <typename DataType> using WitnessEntities = WitnessEntities_<DataType>;
309
313 template <typename DataType> class ShiftedEntities {
314 public:
315 DEFINE_FLAVOR_MEMBERS(DataType,
316 w_l_shift, // column 0
317 w_r_shift, // column 1
318 w_o_shift, // column 2
319 w_4_shift, // column 3
320 z_perm_shift) // column 4
321 };
322
323 public:
333 template <typename DataType, bool HasZK_ = HasZK>
355
356 // Default AllEntities alias (no ZK)
357 template <typename DataType> using AllEntities = AllEntities_<DataType, HasZK>;
358
363 template <bool HasZK_ = HasZK> class AllValues_ : public AllEntities_<FF, HasZK_> {
364 public:
366 using Base::Base;
367 };
368
370
374 template <bool HasZK_ = HasZK> class ProverPolynomials_ : public AllEntities_<Polynomial, HasZK_> {
375 public:
376 // Define all operations as default, except copy construction/assignment
378 // fully-formed constructor
379 ProverPolynomials_(size_t circuit_size)
380 {
381 BB_BENCH_NAME("ProverPolynomials(size_t)");
382
383 for (auto& poly : this->get_to_be_shifted()) {
384 poly = Polynomial{ /*memory size*/ circuit_size - 1,
385 /*largest possible index*/ circuit_size,
386 /* offset */ 1 };
387 }
388 // catch-all with fully formed polynomials
389 for (auto& poly : this->get_unshifted()) {
390 if (poly.is_empty()) {
391 // Not set above
392 poly = Polynomial{ /*memory size*/ circuit_size, /*largest possible index*/ circuit_size };
393 }
394 }
395 set_shifted();
396 }
399 ProverPolynomials_(ProverPolynomials_&& o) noexcept = default;
402 [[nodiscard]] size_t get_polynomial_size() const { return this->q_c.size(); }
403 [[nodiscard]] AllValues_<HasZK_> get_row(size_t row_idx) const
404 {
405 AllValues_<HasZK_> result;
406 for (auto [result_field, polynomial] : zip_view(result.get_all(), this->get_all())) {
407 result_field = polynomial[row_idx];
408 }
409 return result;
410 }
411
413 {
414 AllValues_<HasZK_> result;
415 for (auto [result_field, polynomial] : zip_view(result.get_sigmas(), this->get_sigmas())) {
416 result_field = polynomial[row_idx];
417 }
418 for (auto [result_field, polynomial] : zip_view(result.get_ids(), this->get_ids())) {
419 result_field = polynomial[row_idx];
420 }
421 for (auto [result_field, polynomial] : zip_view(result.get_wires(), this->get_wires())) {
422 result_field = polynomial[row_idx];
423 }
424 return result;
425 }
426
428 {
429 for (auto [shifted, to_be_shifted] : zip_view(this->get_shifted(), this->get_to_be_shifted())) {
430 shifted = to_be_shifted.shifted();
431 }
432 }
433
434 void increase_polynomials_virtual_size(const size_t size_in)
435 {
436 for (auto& polynomial : this->get_all()) {
437 polynomial.increase_virtual_size(size_in);
438 }
439 }
440 };
441
443
445
456
458
462 template <bool HasZK_ = HasZK>
465
467
473
478
483
490 class CommitmentLabels : public AllEntities<std::string> {
491 public:
493 {
494 w_l = "W_L";
495 w_r = "W_R";
496 w_o = "W_O";
497 w_4 = "W_4";
498 z_perm = "Z_PERM";
499 lookup_inverses = "LOOKUP_INVERSES";
500 lookup_read_counts = "LOOKUP_READ_COUNTS";
501 lookup_read_tags = "LOOKUP_READ_TAGS";
502 ecc_op_wire_1 = "ECC_OP_WIRE_1";
503 ecc_op_wire_2 = "ECC_OP_WIRE_2";
504 ecc_op_wire_3 = "ECC_OP_WIRE_3";
505 ecc_op_wire_4 = "ECC_OP_WIRE_4";
506 calldata = "CALLDATA";
507 calldata_read_counts = "CALLDATA_READ_COUNTS";
508 calldata_read_tags = "CALLDATA_READ_TAGS";
509 calldata_inverses = "CALLDATA_INVERSES";
510 secondary_calldata = "SECONDARY_CALLDATA";
511 secondary_calldata_read_counts = "SECONDARY_CALLDATA_READ_COUNTS";
512 secondary_calldata_read_tags = "SECONDARY_CALLDATA_READ_TAGS";
513 secondary_calldata_inverses = "SECONDARY_CALLDATA_INVERSES";
514 return_data = "RETURN_DATA";
515 return_data_read_counts = "RETURN_DATA_READ_COUNTS";
516 return_data_read_tags = "RETURN_DATA_READ_TAGS";
517 return_data_inverses = "RETURN_DATA_INVERSES";
518
519 q_c = "Q_C";
520 q_l = "Q_L";
521 q_r = "Q_R";
522 q_o = "Q_O";
523 q_4 = "Q_4";
524 q_m = "Q_M";
525 q_busread = "Q_BUSREAD";
526 q_lookup = "Q_LOOKUP";
527 q_arith = "Q_ARITH";
528 q_delta_range = "Q_SORT";
529 q_elliptic = "Q_ELLIPTIC";
530 q_memory = "Q_MEMORY";
531 q_nnf = "Q_NNF";
532 q_poseidon2_external = "Q_POSEIDON2_EXTERNAL";
533 q_poseidon2_internal = "Q_POSEIDON2_INTERNAL";
534 sigma_1 = "SIGMA_1";
535 sigma_2 = "SIGMA_2";
536 sigma_3 = "SIGMA_3";
537 sigma_4 = "SIGMA_4";
538 id_1 = "ID_1";
539 id_2 = "ID_2";
540 id_3 = "ID_3";
541 id_4 = "ID_4";
542 table_1 = "TABLE_1";
543 table_2 = "TABLE_2";
544 table_3 = "TABLE_3";
545 table_4 = "TABLE_4";
546 lagrange_first = "LAGRANGE_FIRST";
547 lagrange_last = "LAGRANGE_LAST";
548 lagrange_ecc_op = "Q_ECC_OP_QUEUE";
549 };
550 };
551
555 template <typename Commitment, typename VerificationKey, bool HasZK_ = HasZK>
556 class VerifierCommitments_ : public AllEntities_<Commitment, HasZK_> {
557 public:
558 VerifierCommitments_(const std::shared_ptr<VerificationKey>& verification_key,
559 const std::optional<WitnessEntities<Commitment>>& witness_commitments = std::nullopt)
560 {
561 // Copy the precomputed polynomial commitments into this
562 for (auto [precomputed, precomputed_in] : zip_view(this->get_precomputed(), verification_key->get_all())) {
563 precomputed = precomputed_in;
564 }
565
566 // If provided, copy the witness polynomial commitments into this
567 if (witness_commitments.has_value()) {
568 for (auto [witness, witness_in] :
569 zip_view(this->get_witness(), witness_commitments.value().get_all())) {
570 witness = witness_in;
571 }
572
573 // Set shifted commitments
574 this->w_l_shift = witness_commitments->w_l;
575 this->w_r_shift = witness_commitments->w_r;
576 this->w_o_shift = witness_commitments->w_o;
577 this->w_4_shift = witness_commitments->w_4;
578 this->z_perm_shift = witness_commitments->z_perm;
579 }
580 }
581 };
582 // Specialize for Mega (general case used in MegaRecursive).
584};
585
586} // namespace bb
#define BB_BENCH_NAME(name)
Definition bb_bench.hpp:219
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 DECIDER_PROOF_LENGTH(size_t virtual_log_n=VIRTUAL_LOG_N)
static constexpr size_t NUM_SHIFTED_ENTITIES
static constexpr RepeatedCommitmentsData REPEATED_COMMITMENTS
static constexpr size_t NUM_WIRES
static constexpr size_t PROOF_LENGTH_WITHOUT_PUB_INPUTS(size_t virtual_log_n=VIRTUAL_LOG_N)
static constexpr size_t NUM_SUBRELATIONS
Curve::Element GroupElement
static constexpr size_t OINK_PROOF_LENGTH_WITHOUT_PUB_INPUTS
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:593
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