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
32
33namespace bb {
34
36 public:
42 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
61 // define the tuple of Relations that comprise the Sumcheck relation
62 // Note: made generic for use in MegaRecursive.
63 template <typename FF>
76
77 static constexpr size_t MAX_PARTIAL_RELATION_LENGTH = compute_max_partial_relation_length<Relations>();
78 // BATCHED_RELATION_PARTIAL_LENGTH = algebraic degree of sumcheck relation *after* multiplying by the `pow_zeta`
79 // random polynomial e.g. For \sum(x) [A(x) * B(x) + C(x)] * PowZeta(X), relation length = 2 and random relation
80 // length = 3
83
84 static constexpr size_t num_frs_comm = FrCodec::calc_num_fields<Commitment>();
85 static constexpr size_t num_frs_fr = FrCodec::calc_num_fields<FF>();
86
87 // A challenge whose powers are used to batch subrelation contributions during Sumcheck
88 static constexpr size_t NUM_SUBRELATIONS = compute_number_of_subrelations<Relations>();
90
107 template <typename DataType_> class PrecomputedEntities {
108 public:
109 bool operator==(const PrecomputedEntities&) const = default;
110 using DataType = DataType_;
112 q_m, // column 0
113 q_c, // column 1
114 q_l, // column 2
115 q_r, // column 3
116 q_o, // column 4
117 q_4, // column 5
118 q_busread, // column 6
119 q_lookup, // column 7
120 q_arith, // column 8
121 q_delta_range, // column 9
122 q_elliptic, // column 10
123 q_memory, // column 11
124 q_nnf, // column 12
125 q_poseidon2_external, // column 13
126 q_poseidon2_internal, // column 14
127 sigma_1, // column 15
128 sigma_2, // column 16
129 sigma_3, // column 17
130 sigma_4, // column 18
131 id_1, // column 19
132 id_2, // column 20
133 id_3, // column 21
134 id_4, // column 22
135 table_1, // column 23
136 table_2, // column 24
137 table_3, // column 25
138 table_4, // column 26
139 lagrange_first, // column 27
140 lagrange_last, // column 28
141 lagrange_ecc_op, // column 29 // indicator poly for ecc op gates
142 databus_id // column 30 // id polynomial, i.e. id_i = i
143 )
144
145 auto get_non_gate_selectors() { return RefArray{ q_m, q_c, q_l, q_r, q_o, q_4 }; };
147 {
148 return RefArray{
149 q_busread,
150 q_lookup,
151 q_arith,
152 q_delta_range,
153 q_elliptic,
154 q_memory,
155 q_nnf,
156 q_poseidon2_external,
157 q_poseidon2_internal,
158 };
159 }
160 auto get_selectors() { return concatenate(get_non_gate_selectors(), get_gate_selectors()); }
161
162 auto get_sigmas() { return RefArray{ sigma_1, sigma_2, sigma_3, sigma_4 }; };
163 auto get_ids() { return RefArray{ id_1, id_2, id_3, id_4 }; };
164 auto get_tables() { return RefArray{ table_1, table_2, table_3, table_4 }; };
165 };
166
167 // WireEntities for basic witness entities
168 template <typename DataType> class WireEntities {
169 public:
171 w_l, // column 0
172 w_r, // column 1
173 w_o, // column 2
174 w_4); // column 3
175 };
176
177 // DerivedEntities for derived witness entities
178 template <typename DataType> class DerivedEntities {
179 public:
181 z_perm, // column 4
182 lookup_inverses, // column 5
183 lookup_read_counts, // column 6
184 lookup_read_tags, // column 7
185 ecc_op_wire_1, // column 8
186 ecc_op_wire_2, // column 9
187 ecc_op_wire_3, // column 10
188 ecc_op_wire_4, // column 11
189 calldata, // column 12
190 calldata_read_counts, // column 13
191 calldata_inverses, // column 14
192 secondary_calldata, // column 15
193 secondary_calldata_read_counts, // column 16
194 secondary_calldata_inverses, // column 17
195 return_data, // column 18
196 return_data_read_counts, // column 19
197 return_data_inverses); // column 20
198 auto get_to_be_shifted() { return RefArray{ z_perm }; };
199 };
200
206 template <typename DataType>
207 class WitnessEntities_ : public WireEntities<DataType>, public DerivedEntities<DataType> {
208 public:
210
213 {
214 return RefArray{ this->ecc_op_wire_1, this->ecc_op_wire_2, this->ecc_op_wire_3, this->ecc_op_wire_4 };
215 }
216
217 // Per-bus entity groups. Keeps the mapping from bus_idx to the named DerivedEntities members
218 // in one place; the indexed getters below build their RefArrays by unpacking over 0..NUM_BUS_COLUMNS-1.
219 template <size_t bus_idx> auto databus_entities_for_bus()
220 {
221 if constexpr (bus_idx == 0) {
222 return RefArray{ this->calldata, this->calldata_read_counts };
223 } else if constexpr (bus_idx == 1) {
224 return RefArray{ this->secondary_calldata, this->secondary_calldata_read_counts };
225 } else {
226 static_assert(bus_idx == 2);
227 return RefArray{ this->return_data, this->return_data_read_counts };
228 }
229 }
230 template <size_t bus_idx> auto databus_inverse_for_bus()
231 {
232 if constexpr (bus_idx == 0) {
233 return RefArray{ this->calldata_inverses };
234 } else if constexpr (bus_idx == 1) {
235 return RefArray{ this->secondary_calldata_inverses };
236 } else {
237 static_assert(bus_idx == 2);
238 return RefArray{ this->return_data_inverses };
239 }
240 }
241
242 auto get_databus_entities() // Excludes the derived inverse polynomials
243 {
244 return [this]<size_t... Is>(std::index_sequence<Is...>) {
245 return concatenate(this->template databus_entities_for_bus<Is>()...);
247 }
248
250 {
251 return [this]<size_t... Is>(std::index_sequence<Is...>) {
252 return concatenate(this->template databus_inverse_for_bus<Is>()...);
254 }
259 };
260
261 // Default WitnessEntities alias
262 template <typename DataType> using WitnessEntities = WitnessEntities_<DataType>;
263
267 template <typename DataType> class ShiftedEntities {
268 public:
269 DEFINE_FLAVOR_MEMBERS(DataType,
270 w_l_shift, // column 0
271 w_r_shift, // column 1
272 w_o_shift, // column 2
273 w_4_shift, // column 3
274 z_perm_shift) // column 4
275 };
276
286 template <typename DataType>
303
304 template <typename DataType> using AllEntities = AllEntities_<DataType>;
305
306 // Derive entity counts from the actual struct definitions
312
313 // Rows reserved at the top of the trace for row-disabling / ZK masking.
314 static constexpr size_t TRACE_OFFSET = 0;
315
318
319 // Size of the final PCS MSM after KZG adds quotient commitment:
320 // 1 (Shplonk Q) + NUM_UNSHIFTED + (log_n - 1) Gemini folds + 1 (G1 identity) + 1 (KZG W)
321 // (shifted commitments are removed as duplicates)
322 static constexpr size_t FINAL_PCS_MSM_SIZE(size_t log_n = VIRTUAL_LOG_N)
323 {
324 return NUM_UNSHIFTED_ENTITIES + log_n + 2;
325 }
326
332
337
339
345
347
353
359
364
369
376 class CommitmentLabels : public AllEntities<std::string> {
377 public:
379 {
380 w_l = "W_L";
381 w_r = "W_R";
382 w_o = "W_O";
383 w_4 = "W_4";
384 z_perm = "Z_PERM";
385 lookup_inverses = "LOOKUP_INVERSES";
386 lookup_read_counts = "LOOKUP_READ_COUNTS";
387 lookup_read_tags = "LOOKUP_READ_TAGS";
388 ecc_op_wire_1 = "ECC_OP_WIRE_1";
389 ecc_op_wire_2 = "ECC_OP_WIRE_2";
390 ecc_op_wire_3 = "ECC_OP_WIRE_3";
391 ecc_op_wire_4 = "ECC_OP_WIRE_4";
392 calldata = "CALLDATA";
393 calldata_read_counts = "CALLDATA_READ_COUNTS";
394 calldata_inverses = "CALLDATA_INVERSES";
395 secondary_calldata = "SECONDARY_CALLDATA";
396 secondary_calldata_read_counts = "SECONDARY_CALLDATA_READ_COUNTS";
397 secondary_calldata_inverses = "SECONDARY_CALLDATA_INVERSES";
398 return_data = "RETURN_DATA";
399 return_data_read_counts = "RETURN_DATA_READ_COUNTS";
400 return_data_inverses = "RETURN_DATA_INVERSES";
401
402 q_c = "Q_C";
403 q_l = "Q_L";
404 q_r = "Q_R";
405 q_o = "Q_O";
406 q_4 = "Q_4";
407 q_m = "Q_M";
408 q_busread = "Q_BUSREAD";
409 q_lookup = "Q_LOOKUP";
410 q_arith = "Q_ARITH";
411 q_delta_range = "Q_SORT";
412 q_elliptic = "Q_ELLIPTIC";
413 q_memory = "Q_MEMORY";
414 q_nnf = "Q_NNF";
415 q_poseidon2_external = "Q_POSEIDON2_EXTERNAL";
416 q_poseidon2_internal = "Q_POSEIDON2_INTERNAL";
417 sigma_1 = "SIGMA_1";
418 sigma_2 = "SIGMA_2";
419 sigma_3 = "SIGMA_3";
420 sigma_4 = "SIGMA_4";
421 id_1 = "ID_1";
422 id_2 = "ID_2";
423 id_3 = "ID_3";
424 id_4 = "ID_4";
425 table_1 = "TABLE_1";
426 table_2 = "TABLE_2";
427 table_3 = "TABLE_3";
428 table_4 = "TABLE_4";
429 lagrange_first = "LAGRANGE_FIRST";
430 lagrange_last = "LAGRANGE_LAST";
431 lagrange_ecc_op = "Q_ECC_OP_QUEUE";
432 };
433 };
434
438 template <typename Commitment, typename VerificationKey>
439 class VerifierCommitments_ : public AllEntities_<Commitment> {
440 public:
441 VerifierCommitments_(const std::shared_ptr<VerificationKey>& verification_key,
442 const std::optional<WitnessEntities<Commitment>>& witness_commitments = std::nullopt)
443 {
444 // Copy the precomputed polynomial commitments into this
445 for (auto [precomputed, precomputed_in] : zip_view(this->get_precomputed(), verification_key->get_all())) {
446 precomputed = precomputed_in;
447 }
448
449 // If provided, copy the witness polynomial commitments into this
450 if (witness_commitments.has_value()) {
451 for (auto [witness, witness_in] :
452 zip_view(this->get_witness(), witness_commitments.value().get_all())) {
453 witness = witness_in;
454 }
455
456 // Set shifted commitments
457 this->w_l_shift = witness_commitments->w_l;
458 this->w_r_shift = witness_commitments->w_r;
459 this->w_o_shift = witness_commitments->w_o;
460 this->w_4_shift = witness_commitments->w_4;
461 this->z_perm_shift = witness_commitments->z_perm;
462 }
463 }
464 };
465 // Specialize for Mega (general case used in MegaRecursive).
467};
468
469} // 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, calldata, calldata_read_counts, calldata_inverses, secondary_calldata, secondary_calldata_read_counts, secondary_calldata_inverses, return_data, return_data_read_counts, return_data_inverses)
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
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.
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
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
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
std::vector< MemoryValue > calldata
The precomputed data needed to compute a Honk VK.
Definition flavor.hpp:84