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: Completed, auditors: [Sergei], 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
35
36namespace bb {
37
39 public:
45 using PCS = KZG<Curve>;
48 using Codec = FrCodec;
51
52 // An upper bound on the size of the Mega-circuits. `CONST_FOLDING_LOG_N` bounds the log circuit sizes in the Chonk
53 // context.
54 static constexpr size_t VIRTUAL_LOG_N = CONST_FOLDING_LOG_N;
55 // indicates when evaluating sumcheck, edges can be left as degree-1 monomials
56 static constexpr bool USE_SHORT_MONOMIALS = true;
57 // Indicates that this flavor runs with non-ZK Sumcheck.
58 static constexpr bool HasZK = false;
59 // To achieve fixed proof size and that the recursive verifier circuit is constant, we are using padding in Sumcheck
60 // and Shplemini
61 static constexpr bool USE_PADDING = true;
62 static constexpr size_t NUM_WIRES = CircuitBuilder::NUM_WIRES;
63
64 // define the tuple of Relations that comprise the Sumcheck relation
65 // Note: made generic for use in MegaRecursive.
66 template <typename FF>
82
83 static constexpr size_t MAX_PARTIAL_RELATION_LENGTH = compute_max_partial_relation_length<Relations>();
84 // BATCHED_RELATION_PARTIAL_LENGTH = algebraic degree of sumcheck relation *after* multiplying by the `pow_zeta`
85 // random polynomial e.g. For \sum(x) [A(x) * B(x) + C(x)] * PowZeta(X), relation length = 2 and random relation
86 // length = 3
89
90 static constexpr size_t num_frs_comm = FrCodec::calc_num_fields<Commitment>();
91 static constexpr size_t num_frs_fr = FrCodec::calc_num_fields<FF>();
92
93 // A challenge whose powers are used to batch subrelation contributions during Sumcheck
94 static constexpr size_t NUM_SUBRELATIONS = compute_number_of_subrelations<Relations>();
96
115 template <typename DataType_> class PrecomputedEntities {
116 public:
117 bool operator==(const PrecomputedEntities&) const = default;
118 using DataType = DataType_;
120 q_m, // column 0
121 q_c, // column 1
122 q_l, // column 2
123 q_r, // column 3
124 q_o, // column 4
125 q_4, // column 5
126 q_5, // column 6
127 q_busread, // column 7
128 q_lookup, // column 8
129 q_arith, // column 9
130 q_delta_range, // column 10
131 q_elliptic, // column 11
132 q_memory, // column 12
133 q_nnf, // column 13
134 q_poseidon2_external, // column 14
135 q_poseidon2_external_initial, // column 15
136 q_poseidon2_quad_internal, // column 16
137 q_poseidon2_quad_internal_terminal, // column 17
138 q_poseidon2_transition_entry, // column 18
139 sigma_1, // column 19
140 sigma_2, // column 20
141 sigma_3, // column 21
142 sigma_4, // column 22
143 id_1, // column 23
144 id_2, // column 24
145 id_3, // column 25
146 id_4, // column 26
147 table_1, // column 27
148 table_2, // column 28
149 table_3, // column 29
150 table_4, // column 30
151 lagrange_first, // column 31
152 lagrange_last, // column 32
153 lagrange_ecc_op, // column 33 // indicator poly for ecc op gates
154 databus_id // column 34 // id polynomial, i.e. id_i = i
155 )
156
157 auto get_non_gate_selectors() { return RefArray{ q_m, q_c, q_l, q_r, q_o, q_4, q_5 }; };
159 {
160 return RefArray{
161 q_busread,
162 q_lookup,
163 q_arith,
164 q_delta_range,
165 q_elliptic,
166 q_memory,
167 q_nnf,
168 q_poseidon2_external,
169 q_poseidon2_external_initial,
170 q_poseidon2_quad_internal,
171 q_poseidon2_quad_internal_terminal,
172 q_poseidon2_transition_entry,
173 };
174 }
175 auto get_selectors() { return concatenate(get_non_gate_selectors(), get_gate_selectors()); }
176
177 auto get_sigmas() { return RefArray{ sigma_1, sigma_2, sigma_3, sigma_4 }; };
178 auto get_ids() { return RefArray{ id_1, id_2, id_3, id_4 }; };
179 auto get_tables() { return RefArray{ table_1, table_2, table_3, table_4 }; };
180 };
181
182 // WireEntities for basic witness entities
183 template <typename DataType> class WireEntities {
184 public:
186 w_l, // column 0
187 w_r, // column 1
188 w_o, // column 2
189 w_4); // column 3
190 };
191
192 // DerivedEntities for derived witness entities
193 template <typename DataType> class DerivedEntities {
194 public:
196 z_perm, // column 4
197 lookup_inverses, // column 5
198 lookup_read_counts, // column 6
199 lookup_read_tags, // column 7
200 ecc_op_wire_1, // column 8
201 ecc_op_wire_2, // column 9
202 ecc_op_wire_3, // column 10
203 ecc_op_wire_4, // column 11
204 kernel_calldata, // column 12
205 kernel_calldata_read_counts, // column 13
206 kernel_calldata_inverses, // column 14
207 first_app_calldata, // column 15
208 first_app_calldata_read_counts, // column 16
209 first_app_calldata_inverses, // column 17
210 second_app_calldata, // column 18
211 second_app_calldata_read_counts, // column 19
212 second_app_calldata_inverses, // column 20
213 third_app_calldata, // column 21
214 third_app_calldata_read_counts, // column 22
215 third_app_calldata_inverses, // column 23
216 return_data, // column 24
217 return_data_read_counts, // column 25
218 return_data_inverses); // column 26
219 auto get_to_be_shifted() { return RefArray{ z_perm }; };
220 };
221
227 template <typename DataType>
228 class WitnessEntities_ : public WireEntities<DataType>, public DerivedEntities<DataType> {
229 public:
231
234 {
235 return RefArray{ this->ecc_op_wire_1, this->ecc_op_wire_2, this->ecc_op_wire_3, this->ecc_op_wire_4 };
236 }
237
238 // Per-bus entity groups. Keeps the mapping from bus_idx to the named DerivedEntities members
239 // in one place; the indexed getters below build their RefArrays by unpacking over 0..NUM_BUS_COLUMNS-1.
240 template <size_t bus_idx> auto databus_entities_for_bus()
241 {
242 if constexpr (bus_idx == 0) {
243 return RefArray{ this->kernel_calldata, this->kernel_calldata_read_counts };
244 } else if constexpr (bus_idx == 1) {
245 return RefArray{ this->first_app_calldata, this->first_app_calldata_read_counts };
246 } else if constexpr (bus_idx == 2) {
247 return RefArray{ this->second_app_calldata, this->second_app_calldata_read_counts };
248 } else if constexpr (bus_idx == 3) {
249 return RefArray{ this->third_app_calldata, this->third_app_calldata_read_counts };
250 } else {
251 static_assert(bus_idx == 4);
252 return RefArray{ this->return_data, this->return_data_read_counts };
253 }
254 }
255 template <size_t bus_idx> auto databus_inverse_for_bus()
256 {
257 if constexpr (bus_idx == 0) {
258 return RefArray{ this->kernel_calldata_inverses };
259 } else if constexpr (bus_idx == 1) {
260 return RefArray{ this->first_app_calldata_inverses };
261 } else if constexpr (bus_idx == 2) {
262 return RefArray{ this->second_app_calldata_inverses };
263 } else if constexpr (bus_idx == 3) {
264 return RefArray{ this->third_app_calldata_inverses };
265 } else {
266 static_assert(bus_idx == 4);
267 return RefArray{ this->return_data_inverses };
268 }
269 }
270
271 auto get_databus_entities() // Excludes the derived inverse polynomials
272 {
273 return [this]<size_t... Is>(std::index_sequence<Is...>) {
274 return concatenate(this->template databus_entities_for_bus<Is>()...);
276 }
277
279 {
280 return [this]<size_t... Is>(std::index_sequence<Is...>) {
281 return concatenate(this->template databus_inverse_for_bus<Is>()...);
283 }
288 };
289
290 // Default WitnessEntities alias
291 template <typename DataType> using WitnessEntities = WitnessEntities_<DataType>;
292
296 template <typename DataType> class ShiftedEntities {
297 public:
298 DEFINE_FLAVOR_MEMBERS(DataType,
299 w_l_shift, // column 0
300 w_r_shift, // column 1
301 w_o_shift, // column 2
302 w_4_shift, // column 3
303 z_perm_shift) // column 4
304 };
305
315 template <typename DataType>
332
333 template <typename DataType> using AllEntities = AllEntities_<DataType>;
334
335 // Derive entity counts from the actual struct definitions
341
342 // Rows reserved at the top of the trace for row-disabling / ZK masking.
343 static constexpr size_t TRACE_OFFSET = 0;
344
347
348 // Size of the final PCS MSM after KZG adds quotient commitment:
349 // 1 (Shplonk Q) + NUM_UNSHIFTED + (log_n - 1) Gemini folds + 1 (G1 identity) + 1 (KZG W)
350 // (shifted commitments are removed as duplicates)
351 static constexpr size_t FINAL_PCS_MSM_SIZE(size_t log_n = VIRTUAL_LOG_N)
352 {
353 return NUM_UNSHIFTED_ENTITIES + log_n + 2;
354 }
355
361
366
368
374
376
382
388
393
398
405 class CommitmentLabels : public AllEntities<std::string> {
406 public:
408 {
409 w_l = "W_L";
410 w_r = "W_R";
411 w_o = "W_O";
412 w_4 = "W_4";
413 z_perm = "Z_PERM";
414 lookup_inverses = "LOOKUP_INVERSES";
415 lookup_read_counts = "LOOKUP_READ_COUNTS";
416 lookup_read_tags = "LOOKUP_READ_TAGS";
417 ecc_op_wire_1 = "ECC_OP_WIRE_1";
418 ecc_op_wire_2 = "ECC_OP_WIRE_2";
419 ecc_op_wire_3 = "ECC_OP_WIRE_3";
420 ecc_op_wire_4 = "ECC_OP_WIRE_4";
421 kernel_calldata = "KERNEL_CALLDATA";
422 kernel_calldata_read_counts = "KERNEL_CALLDATA_READ_COUNTS";
423 kernel_calldata_inverses = "KERNEL_CALLDATA_INVERSES";
424 first_app_calldata = "FIRST_APP_CALLDATA";
425 first_app_calldata_read_counts = "FIRST_APP_CALLDATA_READ_COUNTS";
426 first_app_calldata_inverses = "FIRST_APP_CALLDATA_INVERSES";
427 second_app_calldata = "SECOND_APP_CALLDATA";
428 second_app_calldata_read_counts = "SECOND_APP_CALLDATA_READ_COUNTS";
429 second_app_calldata_inverses = "SECOND_APP_CALLDATA_INVERSES";
430 third_app_calldata = "THIRD_APP_CALLDATA";
431 third_app_calldata_read_counts = "THIRD_APP_CALLDATA_READ_COUNTS";
432 third_app_calldata_inverses = "THIRD_APP_CALLDATA_INVERSES";
433 return_data = "RETURN_DATA";
434 return_data_read_counts = "RETURN_DATA_READ_COUNTS";
435 return_data_inverses = "RETURN_DATA_INVERSES";
436
437 q_c = "Q_C";
438 q_l = "Q_L";
439 q_r = "Q_R";
440 q_o = "Q_O";
441 q_4 = "Q_4";
442 q_5 = "Q_5";
443 q_m = "Q_M";
444 q_busread = "Q_BUSREAD";
445 q_lookup = "Q_LOOKUP";
446 q_arith = "Q_ARITH";
447 q_delta_range = "Q_SORT";
448 q_elliptic = "Q_ELLIPTIC";
449 q_memory = "Q_MEMORY";
450 q_nnf = "Q_NNF";
451 q_poseidon2_external = "Q_POSEIDON2_EXTERNAL";
452 q_poseidon2_external_initial = "Q_POSEIDON2_EXTERNAL_INITIAL";
453 q_poseidon2_quad_internal = "Q_POSEIDON2_QUAD_INTERNAL";
454 q_poseidon2_quad_internal_terminal = "Q_POSEIDON2_QUAD_INTERNAL_TERMINAL";
455 q_poseidon2_transition_entry = "Q_POSEIDON2_TRANSITION_ENTRY";
456 sigma_1 = "SIGMA_1";
457 sigma_2 = "SIGMA_2";
458 sigma_3 = "SIGMA_3";
459 sigma_4 = "SIGMA_4";
460 id_1 = "ID_1";
461 id_2 = "ID_2";
462 id_3 = "ID_3";
463 id_4 = "ID_4";
464 table_1 = "TABLE_1";
465 table_2 = "TABLE_2";
466 table_3 = "TABLE_3";
467 table_4 = "TABLE_4";
468 lagrange_first = "LAGRANGE_FIRST";
469 lagrange_last = "LAGRANGE_LAST";
470 lagrange_ecc_op = "Q_ECC_OP_QUEUE";
471 };
472 };
473
477 template <typename Commitment, typename VerificationKey>
478 class VerifierCommitments_ : public AllEntities_<Commitment> {
479 public:
480 VerifierCommitments_(const std::shared_ptr<VerificationKey>& verification_key,
481 const std::optional<WitnessEntities<Commitment>>& witness_commitments = std::nullopt)
482 {
483 // Copy the precomputed polynomial commitments into this
484 for (auto [precomputed, precomputed_in] : zip_view(this->get_precomputed(), verification_key->get_all())) {
485 precomputed = precomputed_in;
486 }
487
488 // If provided, copy the witness polynomial commitments into this
489 if (witness_commitments.has_value()) {
490 for (auto [witness, witness_in] :
491 zip_view(this->get_witness(), witness_commitments.value().get_all())) {
492 witness = witness_in;
493 }
494
495 // Set shifted commitments
496 this->w_l_shift = witness_commitments->w_l;
497 this->w_r_shift = witness_commitments->w_r;
498 this->w_o_shift = witness_commitments->w_o;
499 this->w_4_shift = witness_commitments->w_4;
500 this->z_perm_shift = witness_commitments->z_perm;
501 }
502 }
503 };
504 // Specialize for Mega (general case used in MegaRecursive).
506};
507
508} // namespace bb
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...
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, kernel_calldata, kernel_calldata_read_counts, kernel_calldata_inverses, first_app_calldata, first_app_calldata_read_counts, first_app_calldata_inverses, second_app_calldata, second_app_calldata_read_counts, second_app_calldata_inverses, third_app_calldata, third_app_calldata_read_counts, third_app_calldata_inverses, return_data, return_data_read_counts, return_data_inverses)
A base class labelling precomputed entities and (ordered) subsets of interest.
bool operator==(const PrecomputedEntities &) const =default
DEFINE_FLAVOR_MEMBERS(DataType, q_m, q_c, q_l, q_r, q_o, q_4, q_5, q_busread, q_lookup, q_arith, q_delta_range, q_elliptic, q_memory, q_nnf, q_poseidon2_external, q_poseidon2_external_initial, q_poseidon2_quad_internal, q_poseidon2_quad_internal_terminal, q_poseidon2_transition_entry, 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()
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
ProverPolynomialsBase< AllEntities_< Polynomial >, AllValues, Polynomial > ProverPolynomials
A container for the prover polynomials handles.
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
AllEntities_< FF > AllValues
A field element for each entity of the flavor. These entities represent the prover polynomials evalua...
static constexpr size_t NUM_RELATIONS
static constexpr size_t NUM_ALL_ENTITIES
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::Poseidon2InitialExternalRelation< FF >, bb::Poseidon2QuadInternalRelation< FF >, bb::Poseidon2QuadInternalTerminalRelation< FF >, bb::Poseidon2TransitionEntryRelation< FF > > Relations_
static constexpr bool HasZK
static constexpr size_t TRACE_OFFSET
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:135
A container for storing the partially evaluated multivariates produced by sumcheck.
A container for polynomials handles used by the prover.
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:541
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 shared across Honk flavors.
#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
The precomputed data needed to compute a Honk VK.
Definition flavor.hpp:84