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
30
31namespace bb {
32
34 public:
40 using PCS = KZG<Curve>;
43 using Codec = FrCodec;
46
47 // An upper bound on the size of the Mega-circuits. `CONST_FOLDING_LOG_N` bounds the log circuit sizes in the Chonk
48 // context.
49 static constexpr size_t VIRTUAL_LOG_N = CONST_FOLDING_LOG_N;
50 // indicates when evaluating sumcheck, edges can be left as degree-1 monomials
51 static constexpr bool USE_SHORT_MONOMIALS = true;
52 // Indicates that this flavor runs with non-ZK Sumcheck.
53 static constexpr bool HasZK = false;
54 // To achieve fixed proof size and that the recursive verifier circuit is constant, we are using padding in Sumcheck
55 // and Shplemini
56 static constexpr bool USE_PADDING = true;
57 static constexpr size_t NUM_WIRES = CircuitBuilder::NUM_WIRES;
58
59 // define the tuple of Relations that comprise the Sumcheck relation
60 // Note: made generic for use in MegaRecursive.
61 template <typename FF>
74
75 static constexpr size_t MAX_PARTIAL_RELATION_LENGTH = compute_max_partial_relation_length<Relations>();
76 // BATCHED_RELATION_PARTIAL_LENGTH = algebraic degree of sumcheck relation *after* multiplying by the `pow_zeta`
77 // random polynomial e.g. For \sum(x) [A(x) * B(x) + C(x)] * PowZeta(X), relation length = 2 and random relation
78 // length = 3
81
82 static constexpr size_t num_frs_comm = FrCodec::calc_num_fields<Commitment>();
83 static constexpr size_t num_frs_fr = FrCodec::calc_num_fields<FF>();
84
85 // A challenge whose powers are used to batch subrelation contributions during Sumcheck
86 static constexpr size_t NUM_SUBRELATIONS = compute_number_of_subrelations<Relations>();
88
105 template <typename DataType_> class PrecomputedEntities {
106 public:
107 bool operator==(const PrecomputedEntities&) const = default;
108 using DataType = DataType_;
110 q_m, // column 0
111 q_c, // column 1
112 q_l, // column 2
113 q_r, // column 3
114 q_o, // column 4
115 q_4, // column 5
116 q_busread, // column 6
117 q_lookup, // column 7
118 q_arith, // column 8
119 q_delta_range, // column 9
120 q_elliptic, // column 10
121 q_memory, // column 11
122 q_nnf, // column 12
123 q_poseidon2_external, // column 13
124 q_poseidon2_internal, // column 14
125 sigma_1, // column 15
126 sigma_2, // column 16
127 sigma_3, // column 17
128 sigma_4, // column 18
129 id_1, // column 19
130 id_2, // column 20
131 id_3, // column 21
132 id_4, // column 22
133 table_1, // column 23
134 table_2, // column 24
135 table_3, // column 25
136 table_4, // column 26
137 lagrange_first, // column 27
138 lagrange_last, // column 28
139 lagrange_ecc_op, // column 29 // indicator poly for ecc op gates
140 databus_id // column 30 // id polynomial, i.e. id_i = i
141 )
142
143 auto get_non_gate_selectors() { return RefArray{ q_m, q_c, q_l, q_r, q_o, q_4 }; };
145 {
146 return RefArray{
147 q_busread,
148 q_lookup,
149 q_arith,
150 q_delta_range,
151 q_elliptic,
152 q_memory,
153 q_nnf,
154 q_poseidon2_external,
155 q_poseidon2_internal,
156 };
157 }
158 auto get_selectors() { return concatenate(get_non_gate_selectors(), get_gate_selectors()); }
159
160 auto get_sigmas() { return RefArray{ sigma_1, sigma_2, sigma_3, sigma_4 }; };
161 auto get_ids() { return RefArray{ id_1, id_2, id_3, id_4 }; };
162 auto get_tables() { return RefArray{ table_1, table_2, table_3, table_4 }; };
163 };
164
165 // WireEntities for basic witness entities
166 template <typename DataType> class WireEntities {
167 public:
169 w_l, // column 0
170 w_r, // column 1
171 w_o, // column 2
172 w_4); // column 3
173 };
174
175 // DerivedEntities for derived witness entities
176 template <typename DataType> class DerivedEntities {
177 public:
179 z_perm, // column 4
180 lookup_inverses, // column 5
181 lookup_read_counts, // column 6
182 lookup_read_tags, // column 7
183 ecc_op_wire_1, // column 8
184 ecc_op_wire_2, // column 9
185 ecc_op_wire_3, // column 10
186 ecc_op_wire_4, // column 11
187 calldata, // column 12
188 calldata_read_counts, // column 13
189 calldata_read_tags, // column 14
190 calldata_inverses, // column 15
191 secondary_calldata, // column 16
192 secondary_calldata_read_counts, // column 17
193 secondary_calldata_read_tags, // column 18
194 secondary_calldata_inverses, // column 19
195 return_data, // column 20
196 return_data_read_counts, // column 21
197 return_data_read_tags, // column 22
198 return_data_inverses); // column 23
199 auto get_to_be_shifted() { return RefArray{ z_perm }; };
200 };
201
206 template <typename DataType, bool HasZK_ = false> class MaskingEntities {
207 public:
208 // When ZK is disabled, this class is empty
209 auto get_all() { return RefArray<DataType, 0>{}; }
210 auto get_all() const { return RefArray<const DataType, 0>{}; }
211 static auto get_labels() { return std::vector<std::string>{}; }
212 };
213
214 // Specialization for when ZK is enabled
215 template <typename DataType> class MaskingEntities<DataType, true> {
216 public:
217 DEFINE_FLAVOR_MEMBERS(DataType, gemini_masking_poly)
218 };
219
225 template <typename DataType>
226 class WitnessEntities_ : public WireEntities<DataType>, public DerivedEntities<DataType> {
227 public:
229
232 {
233 return RefArray{ this->ecc_op_wire_1, this->ecc_op_wire_2, this->ecc_op_wire_3, this->ecc_op_wire_4 };
234 }
235 auto get_databus_entities() // Excludes the derived inverse polynomials
236 {
237 return RefArray{
238 this->calldata, this->calldata_read_counts, this->calldata_read_tags,
239 this->secondary_calldata, this->secondary_calldata_read_counts, this->secondary_calldata_read_tags,
240 this->return_data, this->return_data_read_counts, this->return_data_read_tags
241 };
242 }
243
245 {
246 return RefArray{
247 this->calldata_inverses,
248 this->secondary_calldata_inverses,
249 this->return_data_inverses,
250 };
251 }
256 };
257
258 // Default WitnessEntities alias
259 template <typename DataType> using WitnessEntities = WitnessEntities_<DataType>;
260
264 template <typename DataType> class ShiftedEntities {
265 public:
266 DEFINE_FLAVOR_MEMBERS(DataType,
267 w_l_shift, // column 0
268 w_r_shift, // column 1
269 w_o_shift, // column 2
270 w_4_shift, // column 3
271 z_perm_shift) // column 4
272 };
273
282 template <typename DataType, bool HasZK_ = HasZK>
304
305 // Default AllEntities alias (no ZK)
306 template <typename DataType> using AllEntities = AllEntities_<DataType, HasZK>;
307
308 // Derive entity counts from the actual struct definitions
314
317
318 // Size of the final PCS MSM after KZG adds quotient commitment:
319 // 1 (Shplonk Q) + NUM_UNSHIFTED + (log_n - 1) Gemini folds + 1 (G1 identity) + 1 (KZG W)
320 // (shifted commitments are removed as duplicates)
321 static constexpr size_t FINAL_PCS_MSM_SIZE(size_t log_n = VIRTUAL_LOG_N)
322 {
323 return NUM_UNSHIFTED_ENTITIES + log_n + 2;
324 }
325
330 template <bool HasZK_ = HasZK> class AllValues_ : public AllEntities_<FF, HasZK_> {
331 public:
333 using Base::Base;
334 };
335
337
341 template <bool HasZK_ = HasZK>
343
345
347
353
355
359 template <bool HasZK_ = HasZK>
362
364
370
375
380
387 class CommitmentLabels : public AllEntities<std::string> {
388 public:
390 {
391 w_l = "W_L";
392 w_r = "W_R";
393 w_o = "W_O";
394 w_4 = "W_4";
395 z_perm = "Z_PERM";
396 lookup_inverses = "LOOKUP_INVERSES";
397 lookup_read_counts = "LOOKUP_READ_COUNTS";
398 lookup_read_tags = "LOOKUP_READ_TAGS";
399 ecc_op_wire_1 = "ECC_OP_WIRE_1";
400 ecc_op_wire_2 = "ECC_OP_WIRE_2";
401 ecc_op_wire_3 = "ECC_OP_WIRE_3";
402 ecc_op_wire_4 = "ECC_OP_WIRE_4";
403 calldata = "CALLDATA";
404 calldata_read_counts = "CALLDATA_READ_COUNTS";
405 calldata_read_tags = "CALLDATA_READ_TAGS";
406 calldata_inverses = "CALLDATA_INVERSES";
407 secondary_calldata = "SECONDARY_CALLDATA";
408 secondary_calldata_read_counts = "SECONDARY_CALLDATA_READ_COUNTS";
409 secondary_calldata_read_tags = "SECONDARY_CALLDATA_READ_TAGS";
410 secondary_calldata_inverses = "SECONDARY_CALLDATA_INVERSES";
411 return_data = "RETURN_DATA";
412 return_data_read_counts = "RETURN_DATA_READ_COUNTS";
413 return_data_read_tags = "RETURN_DATA_READ_TAGS";
414 return_data_inverses = "RETURN_DATA_INVERSES";
415
416 q_c = "Q_C";
417 q_l = "Q_L";
418 q_r = "Q_R";
419 q_o = "Q_O";
420 q_4 = "Q_4";
421 q_m = "Q_M";
422 q_busread = "Q_BUSREAD";
423 q_lookup = "Q_LOOKUP";
424 q_arith = "Q_ARITH";
425 q_delta_range = "Q_SORT";
426 q_elliptic = "Q_ELLIPTIC";
427 q_memory = "Q_MEMORY";
428 q_nnf = "Q_NNF";
429 q_poseidon2_external = "Q_POSEIDON2_EXTERNAL";
430 q_poseidon2_internal = "Q_POSEIDON2_INTERNAL";
431 sigma_1 = "SIGMA_1";
432 sigma_2 = "SIGMA_2";
433 sigma_3 = "SIGMA_3";
434 sigma_4 = "SIGMA_4";
435 id_1 = "ID_1";
436 id_2 = "ID_2";
437 id_3 = "ID_3";
438 id_4 = "ID_4";
439 table_1 = "TABLE_1";
440 table_2 = "TABLE_2";
441 table_3 = "TABLE_3";
442 table_4 = "TABLE_4";
443 lagrange_first = "LAGRANGE_FIRST";
444 lagrange_last = "LAGRANGE_LAST";
445 lagrange_ecc_op = "Q_ECC_OP_QUEUE";
446 };
447 };
448
452 template <typename Commitment, typename VerificationKey, bool HasZK_ = HasZK>
453 class VerifierCommitments_ : public AllEntities_<Commitment, HasZK_> {
454 public:
455 VerifierCommitments_(const std::shared_ptr<VerificationKey>& verification_key,
456 const std::optional<WitnessEntities<Commitment>>& witness_commitments = std::nullopt)
457 {
458 // Copy the precomputed polynomial commitments into this
459 for (auto [precomputed, precomputed_in] : zip_view(this->get_precomputed(), verification_key->get_all())) {
460 precomputed = precomputed_in;
461 }
462
463 // If provided, copy the witness polynomial commitments into this
464 if (witness_commitments.has_value()) {
465 for (auto [witness, witness_in] :
466 zip_view(this->get_witness(), witness_commitments.value().get_all())) {
467 witness = witness_in;
468 }
469
470 // Set shifted commitments
471 this->w_l_shift = witness_commitments->w_l;
472 this->w_r_shift = witness_commitments->w_r;
473 this->w_o_shift = witness_commitments->w_o;
474 this->w_4_shift = witness_commitments->w_4;
475 this->z_perm_shift = witness_commitments->z_perm;
476 }
477 }
478 };
479 // Specialize for Mega (general case used in MegaRecursive).
481};
482
483} // 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...
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
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: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