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
30
31namespace bb {
32
34 public:
40 using PCS = KZG<Curve>;
45 using Codec = FrCodec;
48
49 // An upper bound on the size of the Mega-circuits. `CONST_FOLDING_LOG_N` bounds the log circuit sizes in the Chonk
50 // context.
51 static constexpr size_t VIRTUAL_LOG_N = CONST_FOLDING_LOG_N;
52 // indicates when evaluating sumcheck, edges can be left as degree-1 monomials
53 static constexpr bool USE_SHORT_MONOMIALS = true;
54 // Indicates that this flavor runs with non-ZK Sumcheck.
55 static constexpr bool HasZK = false;
56 // To achieve fixed proof size and that the recursive verifier circuit is constant, we are using padding in Sumcheck
57 // and Shplemini
58 static constexpr bool USE_PADDING = true;
59 static constexpr size_t NUM_WIRES = CircuitBuilder::NUM_WIRES;
60 // The number of multivariate polynomials on which a sumcheck prover sumcheck operates (including shifts). We often
61 // need containers of this size to hold related data, so we choose a name more agnostic than `NUM_POLYNOMIALS`.
62 static constexpr size_t NUM_ALL_ENTITIES = 60;
63 // The number of polynomials precomputed to describe a circuit and to aid a prover in constructing a satisfying
64 // assignment of witnesses. We again choose a neutral name.
65 static constexpr size_t NUM_PRECOMPUTED_ENTITIES = 31;
66 // The total number of witness entities not including shifts.
67 static constexpr size_t NUM_WITNESS_ENTITIES = 24;
68 // The number of shifted witness entities including derived witness entities
69 static constexpr size_t NUM_SHIFTED_ENTITIES = 5;
70 // The number of unshifted witness entities
72
75
76 // Size of the final PCS MSM after KZG adds quotient commitment:
77 // 1 (Shplonk Q) + NUM_UNSHIFTED + (log_n - 1) Gemini folds + 1 (G1 identity) + 1 (KZG W)
78 // (shifted commitments are removed as duplicates)
79 static constexpr size_t FINAL_PCS_MSM_SIZE(size_t log_n = VIRTUAL_LOG_N)
80 {
81 return NUM_UNSHIFTED_ENTITIES + log_n + 2;
82 }
83
84 // define the tuple of Relations that comprise the Sumcheck relation
85 // Note: made generic for use in MegaRecursive.
86 template <typename FF>
99
100 static constexpr size_t MAX_PARTIAL_RELATION_LENGTH = compute_max_partial_relation_length<Relations>();
101 // BATCHED_RELATION_PARTIAL_LENGTH = algebraic degree of sumcheck relation *after* multiplying by the `pow_zeta`
102 // random polynomial e.g. For \sum(x) [A(x) * B(x) + C(x)] * PowZeta(X), relation length = 2 and random relation
103 // length = 3
106
107 static constexpr size_t num_frs_comm = FrCodec::calc_num_fields<Commitment>();
108 static constexpr size_t num_frs_fr = FrCodec::calc_num_fields<FF>();
109
110 // Proof length formula methods
111 static constexpr size_t OINK_PROOF_LENGTH_WITHOUT_PUB_INPUTS =
112 /* 1. NUM_WITNESS_ENTITIES commitments */ (NUM_WITNESS_ENTITIES * num_frs_comm);
113
114 static constexpr size_t DECIDER_PROOF_LENGTH(size_t virtual_log_n = VIRTUAL_LOG_N)
115 {
116 return /* 2. virtual_log_n sumcheck univariates */
117 (virtual_log_n * BATCHED_RELATION_PARTIAL_LENGTH * num_frs_fr) +
118 /* 3. NUM_ALL_ENTITIES sumcheck evaluations */ (NUM_ALL_ENTITIES * num_frs_fr) +
119 /* 4. virtual_log_n - 1 Gemini Fold commitments */ ((virtual_log_n - 1) * num_frs_comm) +
120 /* 5. virtual_log_n Gemini a evaluations */ (virtual_log_n * num_frs_fr) +
121 /* 6. Shplonk Q commitment */ (num_frs_comm) +
122 /* 7. KZG W commitment */ (num_frs_comm);
123 }
124
125 static constexpr size_t PROOF_LENGTH_WITHOUT_PUB_INPUTS(size_t virtual_log_n = VIRTUAL_LOG_N)
126 {
128 }
129
130 // A challenge whose powers are used to batch subrelation contributions during Sumcheck
131 static constexpr size_t NUM_SUBRELATIONS = compute_number_of_subrelations<Relations>();
133
134 // Whether or not the first row of the execution trace is reserved for 0s to enable shifts
135 static constexpr bool has_zero_row = true;
152 template <typename DataType_> class PrecomputedEntities {
153 public:
154 bool operator==(const PrecomputedEntities&) const = default;
155 using DataType = DataType_;
157 q_m, // column 0
158 q_c, // column 1
159 q_l, // column 2
160 q_r, // column 3
161 q_o, // column 4
162 q_4, // column 5
163 q_busread, // column 6
164 q_lookup, // column 7
165 q_arith, // column 8
166 q_delta_range, // column 9
167 q_elliptic, // column 10
168 q_memory, // column 11
169 q_nnf, // column 12
170 q_poseidon2_external, // column 13
171 q_poseidon2_internal, // column 14
172 sigma_1, // column 15
173 sigma_2, // column 16
174 sigma_3, // column 17
175 sigma_4, // column 18
176 id_1, // column 19
177 id_2, // column 20
178 id_3, // column 21
179 id_4, // column 22
180 table_1, // column 23
181 table_2, // column 24
182 table_3, // column 25
183 table_4, // column 26
184 lagrange_first, // column 27
185 lagrange_last, // column 28
186 lagrange_ecc_op, // column 29 // indicator poly for ecc op gates
187 databus_id // column 30 // id polynomial, i.e. id_i = i
188 )
189
190 auto get_non_gate_selectors() { return RefArray{ q_m, q_c, q_l, q_r, q_o, q_4 }; };
192 {
193 return RefArray{
194 q_busread,
195 q_lookup,
196 q_arith,
197 q_delta_range,
198 q_elliptic,
199 q_memory,
200 q_nnf,
201 q_poseidon2_external,
202 q_poseidon2_internal,
203 };
204 }
205 auto get_selectors() { return concatenate(get_non_gate_selectors(), get_gate_selectors()); }
206
207 auto get_sigmas() { return RefArray{ sigma_1, sigma_2, sigma_3, sigma_4 }; };
208 auto get_ids() { return RefArray{ id_1, id_2, id_3, id_4 }; };
209 auto get_tables() { return RefArray{ table_1, table_2, table_3, table_4 }; };
210 };
211
212 // Mega needs to expose more public classes than most flavors due to MegaRecursive reuse, but these
213 // are internal:
214
215 // WireEntities for basic witness entities
216 template <typename DataType> class WireEntities {
217 public:
219 w_l, // column 0
220 w_r, // column 1
221 w_o, // column 2
222 w_4); // column 3
223 };
224
225 // DerivedEntities for derived witness entities
226 template <typename DataType> class DerivedEntities {
227 public:
229 z_perm, // column 4
230 lookup_inverses, // column 5
231 lookup_read_counts, // column 6
232 lookup_read_tags, // column 7
233 ecc_op_wire_1, // column 8
234 ecc_op_wire_2, // column 9
235 ecc_op_wire_3, // column 10
236 ecc_op_wire_4, // column 11
237 calldata, // column 12
238 calldata_read_counts, // column 13
239 calldata_read_tags, // column 14
240 calldata_inverses, // column 15
241 secondary_calldata, // column 16
242 secondary_calldata_read_counts, // column 17
243 secondary_calldata_read_tags, // column 18
244 secondary_calldata_inverses, // column 19
245 return_data, // column 20
246 return_data_read_counts, // column 21
247 return_data_read_tags, // column 22
248 return_data_inverses); // column 23
249 auto get_to_be_shifted() { return RefArray{ z_perm }; };
250 };
251
256 template <typename DataType, bool HasZK_ = false> class MaskingEntities {
257 public:
258 // When ZK is disabled, this class is empty
259 auto get_all() { return RefArray<DataType, 0>{}; }
260 auto get_all() const { return RefArray<const DataType, 0>{}; }
261 static auto get_labels() { return std::vector<std::string>{}; }
262 };
263
264 // Specialization for when ZK is enabled
265 template <typename DataType> class MaskingEntities<DataType, true> {
266 public:
267 DEFINE_FLAVOR_MEMBERS(DataType, gemini_masking_poly)
268 };
269
275 template <typename DataType>
276 class WitnessEntities_ : public WireEntities<DataType>, public DerivedEntities<DataType> {
277 public:
279
282 {
283 return RefArray{ this->ecc_op_wire_1, this->ecc_op_wire_2, this->ecc_op_wire_3, this->ecc_op_wire_4 };
284 }
285 auto get_databus_entities() // Excludes the derived inverse polynomials
286 {
287 return RefArray{
288 this->calldata, this->calldata_read_counts, this->calldata_read_tags,
289 this->secondary_calldata, this->secondary_calldata_read_counts, this->secondary_calldata_read_tags,
290 this->return_data, this->return_data_read_counts, this->return_data_read_tags
291 };
292 }
293
295 {
296 return RefArray{
297 this->calldata_inverses,
298 this->secondary_calldata_inverses,
299 this->return_data_inverses,
300 };
301 }
306 };
307
308 // Default WitnessEntities alias
309 template <typename DataType> using WitnessEntities = WitnessEntities_<DataType>;
310
314 template <typename DataType> class ShiftedEntities {
315 public:
316 DEFINE_FLAVOR_MEMBERS(DataType,
317 w_l_shift, // column 0
318 w_r_shift, // column 1
319 w_o_shift, // column 2
320 w_4_shift, // column 3
321 z_perm_shift) // column 4
322 };
323
324 public:
334 template <typename DataType, bool HasZK_ = HasZK>
356
357 // Default AllEntities alias (no ZK)
358 template <typename DataType> using AllEntities = AllEntities_<DataType, HasZK>;
359
364 template <bool HasZK_ = HasZK> class AllValues_ : public AllEntities_<FF, HasZK_> {
365 public:
367 using Base::Base;
368 };
369
371
375 template <bool HasZK_ = HasZK> class ProverPolynomials_ : public AllEntities_<Polynomial, HasZK_> {
376 public:
377 // Define all operations as default, except copy construction/assignment
379 // fully-formed constructor
380 ProverPolynomials_(size_t circuit_size)
381 {
382 BB_BENCH_NAME("ProverPolynomials(size_t)");
383
384 for (auto& poly : this->get_to_be_shifted()) {
385 poly = Polynomial{ /*memory size*/ circuit_size - 1,
386 /*largest possible index*/ circuit_size,
387 /* offset */ 1 };
388 }
389 // catch-all with fully formed polynomials
390 for (auto& poly : this->get_unshifted()) {
391 if (poly.is_empty()) {
392 // Not set above
393 poly = Polynomial{ /*memory size*/ circuit_size, /*largest possible index*/ circuit_size };
394 }
395 }
396 set_shifted();
397 }
400 ProverPolynomials_(ProverPolynomials_&& o) noexcept = default;
403 [[nodiscard]] size_t get_polynomial_size() const { return this->q_c.size(); }
404 [[nodiscard]] AllValues_<HasZK_> get_row(size_t row_idx) const
405 {
406 AllValues_<HasZK_> result;
407 for (auto [result_field, polynomial] : zip_view(result.get_all(), this->get_all())) {
408 result_field = polynomial[row_idx];
409 }
410 return result;
411 }
412
414 {
415 AllValues_<HasZK_> result;
416 for (auto [result_field, polynomial] : zip_view(result.get_sigmas(), this->get_sigmas())) {
417 result_field = polynomial[row_idx];
418 }
419 for (auto [result_field, polynomial] : zip_view(result.get_ids(), this->get_ids())) {
420 result_field = polynomial[row_idx];
421 }
422 for (auto [result_field, polynomial] : zip_view(result.get_wires(), this->get_wires())) {
423 result_field = polynomial[row_idx];
424 }
425 return result;
426 }
427
429 {
430 for (auto [shifted, to_be_shifted] : zip_view(this->get_shifted(), this->get_to_be_shifted())) {
431 shifted = to_be_shifted.shifted();
432 }
433 }
434
435 void increase_polynomials_virtual_size(const size_t size_in)
436 {
437 for (auto& polynomial : this->get_all()) {
438 polynomial.increase_virtual_size(size_in);
439 }
440 }
441 };
442
444
446
457
459
463 template <bool HasZK_ = HasZK> class PartiallyEvaluatedMultivariates_ : public AllEntities_<Polynomial, HasZK_> {
464
465 public:
467 PartiallyEvaluatedMultivariates_(const size_t circuit_size)
468 {
469 // Storage is only needed after the first partial evaluation, hence polynomials of size (n / 2)
470 for (auto& poly : this->get_all()) {
471 poly = Polynomial(circuit_size / 2);
472 }
473 }
474 PartiallyEvaluatedMultivariates_(const ProverPolynomials_<HasZK_>& full_polynomials, size_t circuit_size)
475 {
476 for (auto [poly, full_poly] : zip_view(this->get_all(), full_polynomials.get_all())) {
477 // After the initial sumcheck round, the new size is CEIL(size/2).
478 size_t desired_size = full_poly.end_index() / 2 + full_poly.end_index() % 2;
479 poly = Polynomial(desired_size, circuit_size / 2);
480 }
481 }
482 };
483
485
491
496
501
508 class CommitmentLabels : public AllEntities<std::string> {
509 public:
511 {
512 w_l = "W_L";
513 w_r = "W_R";
514 w_o = "W_O";
515 w_4 = "W_4";
516 z_perm = "Z_PERM";
517 lookup_inverses = "LOOKUP_INVERSES";
518 lookup_read_counts = "LOOKUP_READ_COUNTS";
519 lookup_read_tags = "LOOKUP_READ_TAGS";
520 ecc_op_wire_1 = "ECC_OP_WIRE_1";
521 ecc_op_wire_2 = "ECC_OP_WIRE_2";
522 ecc_op_wire_3 = "ECC_OP_WIRE_3";
523 ecc_op_wire_4 = "ECC_OP_WIRE_4";
524 calldata = "CALLDATA";
525 calldata_read_counts = "CALLDATA_READ_COUNTS";
526 calldata_read_tags = "CALLDATA_READ_TAGS";
527 calldata_inverses = "CALLDATA_INVERSES";
528 secondary_calldata = "SECONDARY_CALLDATA";
529 secondary_calldata_read_counts = "SECONDARY_CALLDATA_READ_COUNTS";
530 secondary_calldata_read_tags = "SECONDARY_CALLDATA_READ_TAGS";
531 secondary_calldata_inverses = "SECONDARY_CALLDATA_INVERSES";
532 return_data = "RETURN_DATA";
533 return_data_read_counts = "RETURN_DATA_READ_COUNTS";
534 return_data_read_tags = "RETURN_DATA_READ_TAGS";
535 return_data_inverses = "RETURN_DATA_INVERSES";
536
537 q_c = "Q_C";
538 q_l = "Q_L";
539 q_r = "Q_R";
540 q_o = "Q_O";
541 q_4 = "Q_4";
542 q_m = "Q_M";
543 q_busread = "Q_BUSREAD";
544 q_lookup = "Q_LOOKUP";
545 q_arith = "Q_ARITH";
546 q_delta_range = "Q_SORT";
547 q_elliptic = "Q_ELLIPTIC";
548 q_memory = "Q_MEMORY";
549 q_nnf = "Q_NNF";
550 q_poseidon2_external = "Q_POSEIDON2_EXTERNAL";
551 q_poseidon2_internal = "Q_POSEIDON2_INTERNAL";
552 sigma_1 = "SIGMA_1";
553 sigma_2 = "SIGMA_2";
554 sigma_3 = "SIGMA_3";
555 sigma_4 = "SIGMA_4";
556 id_1 = "ID_1";
557 id_2 = "ID_2";
558 id_3 = "ID_3";
559 id_4 = "ID_4";
560 table_1 = "TABLE_1";
561 table_2 = "TABLE_2";
562 table_3 = "TABLE_3";
563 table_4 = "TABLE_4";
564 lagrange_first = "LAGRANGE_FIRST";
565 lagrange_last = "LAGRANGE_LAST";
566 lagrange_ecc_op = "Q_ECC_OP_QUEUE";
567 };
568 };
569
573 template <typename Commitment, typename VerificationKey, bool HasZK_ = HasZK>
574 class VerifierCommitments_ : public AllEntities_<Commitment, HasZK_> {
575 public:
576 VerifierCommitments_(const std::shared_ptr<VerificationKey>& verification_key,
577 const std::optional<WitnessEntities<Commitment>>& witness_commitments = std::nullopt)
578 {
579 // Copy the precomputed polynomial commitments into this
580 for (auto [precomputed, precomputed_in] : zip_view(this->get_precomputed(), verification_key->get_all())) {
581 precomputed = precomputed_in;
582 }
583
584 // If provided, copy the witness polynomial commitments into this
585 if (witness_commitments.has_value()) {
586 for (auto [witness, witness_in] :
587 zip_view(this->get_witness(), witness_commitments.value().get_all())) {
588 witness = witness_in;
589 }
590
591 // Set shifted commitments
592 this->w_l_shift = witness_commitments->w_l;
593 this->w_r_shift = witness_commitments->w_r;
594 this->w_o_shift = witness_commitments->w_o;
595 this->w_4_shift = witness_commitments->w_4;
596 this->z_perm_shift = witness_commitments->z_perm;
597 }
598 }
599 };
600 // Specialize for Mega (general case used in MegaRecursive).
602};
603
604} // 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 container for storing the partially evaluated multivariates produced by sumcheck.
PartiallyEvaluatedMultivariates_(const size_t circuit_size)
PartiallyEvaluatedMultivariates_(const ProverPolynomials_< HasZK_ > &full_polynomials, size_t circuit_size)
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)
bb::Polynomial< FF > Polynomial
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 bool has_zero_row
static constexpr size_t num_frs_comm
Base Native verification key class.
Definition flavor.hpp:141
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:521
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