Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
ultra_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
33
34namespace bb {
35
37 public:
43 using PCS = KZG<Curve>;
47 using Codec = FrCodec;
50
51 static constexpr size_t VIRTUAL_LOG_N = CONST_PROOF_SIZE_LOG_N;
52 // indicates when evaluating sumcheck, edges can be left as degree-1 monomials
53 static constexpr bool USE_SHORT_MONOMIALS = true;
54
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 (witness polynomials,
62 // precomputed polynomials and shifts). We often need containers of this size to hold related data, so we choose a
63 // name more agnostic than `NUM_POLYNOMIALS`.
64 static constexpr size_t NUM_ALL_ENTITIES = 41;
65 // The number of polynomials precomputed to describe a circuit and to aid a prover in constructing a satisfying
66 // assignment of witnesses. We again choose a neutral name.
67 static constexpr size_t NUM_PRECOMPUTED_ENTITIES = 28;
68 // The total number of witness entities not including shifts.
69 static constexpr size_t NUM_WITNESS_ENTITIES = 8;
70 // The number of shifted witness entities including derived witness entities
71 static constexpr size_t NUM_SHIFTED_ENTITIES = 5;
72 // The number of unshifted witness entities
74
75 // A container to be fed to ShpleminiVerifier to avoid redundant scalar muls
78
79 // Size of the final PCS MSM after KZG adds quotient commitment:
80 // 1 (Shplonk Q) + NUM_UNSHIFTED + (log_n - 1) Gemini folds + 1 (G1 identity) + 1 (KZG W)
81 // (shifted commitments are removed as duplicates)
82 static constexpr size_t FINAL_PCS_MSM_SIZE(size_t log_n = VIRTUAL_LOG_N)
83 {
84 return NUM_UNSHIFTED_ENTITIES + log_n + 2;
85 }
86
87 // define the tuple of Relations that comprise the Sumcheck relation
88 // Note: made generic for use in MegaRecursive.
89 template <typename FF>
90
91 // List of relations reflecting the Ultra arithmetisation. WARNING: As UltraKeccak flavor inherits from
92 // Ultra flavor any change of ordering in this tuple needs to be reflected in the smart contract, otherwise
93 // relation accumulation will not match.
103
105
106 static constexpr size_t MAX_PARTIAL_RELATION_LENGTH = compute_max_partial_relation_length<Relations>();
107 static_assert(MAX_PARTIAL_RELATION_LENGTH == 7);
108 static constexpr size_t NUM_SUBRELATIONS = compute_number_of_subrelations<Relations>();
109 // A challenge whose powers are used to batch subrelation contributions during Sumcheck
111
112 // BATCHED_RELATION_PARTIAL_LENGTH = algebraic degree of sumcheck relation *after* multiplying by the `pow_zeta`
113 // random polynomial e.g. For \sum(x) [A(x) * B(x) + C(x)] * PowZeta(X), relation length = 2 and random relation
114 // length = 3
117
118 static constexpr size_t num_frs_comm = FrCodec::calc_num_fields<Commitment>();
119 static constexpr size_t num_frs_fr = FrCodec::calc_num_fields<FF>();
120
121 // Proof length formula methods
122 static constexpr size_t OINK_PROOF_LENGTH_WITHOUT_PUB_INPUTS =
123 /* 1. NUM_WITNESS_ENTITIES commitments */ (NUM_WITNESS_ENTITIES * num_frs_comm);
124
125 static constexpr size_t DECIDER_PROOF_LENGTH(size_t virtual_log_n = VIRTUAL_LOG_N)
126 {
127 return /* 2. virtual_log_n sumcheck univariates */
128 (virtual_log_n * BATCHED_RELATION_PARTIAL_LENGTH * num_frs_fr) +
129 /* 3. NUM_ALL_ENTITIES sumcheck evaluations */ (NUM_ALL_ENTITIES * num_frs_fr) +
130 /* 4. virtual_log_n - 1 Gemini Fold commitments */ ((virtual_log_n - 1) * num_frs_comm) +
131 /* 5. virtual_log_n Gemini a evaluations */ (virtual_log_n * num_frs_fr) +
132 /* 6. Shplonk Q commitment */ (num_frs_comm) +
133 /* 7. KZG W commitment */ (num_frs_comm);
134 }
135
136 static constexpr size_t PROOF_LENGTH_WITHOUT_PUB_INPUTS(size_t virtual_log_n = VIRTUAL_LOG_N)
137 {
139 }
140
141 static constexpr bool is_decider = true;
142
147 template <typename DataType_> class PrecomputedEntities {
148 public:
149 bool operator==(const PrecomputedEntities&) const = default;
150 using DataType = DataType_;
152 q_m, // column 0
153 q_c, // column 1
154 q_l, // column 2
155 q_r, // column 3
156 q_o, // column 4
157 q_4, // column 5
158 q_lookup, // column 6
159 q_arith, // column 7
160 q_delta_range, // column 8
161 q_elliptic, // column 9
162 q_memory, // column 10
163 q_nnf, // column 11
164 q_poseidon2_external, // column 12
165 q_poseidon2_internal, // column 13
166 sigma_1, // column 14
167 sigma_2, // column 15
168 sigma_3, // column 16
169 sigma_4, // column 17
170 id_1, // column 18
171 id_2, // column 19
172 id_3, // column 20
173 id_4, // column 21
174 table_1, // column 22
175 table_2, // column 23
176 table_3, // column 24
177 table_4, // column 25
178 lagrange_first, // column 26
179 lagrange_last) // column 27
180
181 auto get_non_gate_selectors() { return RefArray{ q_m, q_c, q_l, q_r, q_o, q_4 }; }
183 {
184 return RefArray{ q_lookup, q_arith, q_delta_range, q_elliptic,
185 q_memory, q_nnf, q_poseidon2_external, q_poseidon2_internal };
186 }
187 auto get_selectors() { return concatenate(get_non_gate_selectors(), get_gate_selectors()); }
188
189 auto get_sigmas() { return RefArray{ sigma_1, sigma_2, sigma_3, sigma_4 }; };
190 auto get_ids() { return RefArray{ id_1, id_2, id_3, id_4 }; };
191 auto get_tables() { return RefArray{ table_1, table_2, table_3, table_4 }; };
192 };
193
198 template <typename DataType, bool HasZK_ = HasZK> class MaskingEntities {
199 public:
200 // When ZK is disabled, this class is empty
201 auto get_all() { return RefArray<DataType, 0>{}; }
202 auto get_all() const { return RefArray<const DataType, 0>{}; }
203 static auto get_labels() { return std::vector<std::string>{}; }
204 };
205
206 // Specialization for when ZK is enabled
207 template <typename DataType> class MaskingEntities<DataType, true> {
208 public:
209 DEFINE_FLAVOR_MEMBERS(DataType, gemini_masking_poly)
210 };
211
215 template <typename DataType> class WitnessEntities {
216 public:
218 w_l, // column 0
219 w_r, // column 1
220 w_o, // column 2
221 w_4, // column 3
222 z_perm, // column 4
223 lookup_inverses, // column 5
224 lookup_read_counts, // column 6
225 lookup_read_tags) // column 7
226
227 auto get_wires() { return RefArray{ w_l, w_r, w_o, w_4 }; };
228 auto get_to_be_shifted() { return RefArray{ w_l, w_r, w_o, w_4, z_perm }; };
229 };
230
234 template <typename DataType> class ShiftedEntities {
235 public:
237 w_l_shift, // column 0
238 w_r_shift, // column 1
239 w_o_shift, // column 2
240 w_4_shift, // column 3
241 z_perm_shift) // column 4
242
243 auto get_shifted() { return RefArray{ w_l_shift, w_r_shift, w_o_shift, w_4_shift, z_perm_shift }; };
244 };
245
255 template <typename DataType, bool HasZK_ = HasZK>
276
277 // Default AllEntities alias (no ZK)
278 template <typename DataType> using AllEntities = AllEntities_<DataType, HasZK>;
279
284 template <bool HasZK_ = HasZK> class AllValues_ : public AllEntities_<FF, HasZK_> {
285 public:
287 using Base::Base;
288 };
289
291
295 // TODO(https://github.com/AztecProtocol/barretenberg/issues/966): use inheritance
296 template <bool HasZK_ = HasZK> class ProverPolynomials_ : public AllEntities_<Polynomial, HasZK_> {
297 public:
298 // Define all operations as default, except copy construction/assignment
300 ProverPolynomials_(size_t circuit_size)
301 {
302
303 BB_BENCH_NAME("creating empty prover polys");
304
305 for (auto& poly : this->get_to_be_shifted()) {
306 poly = Polynomial{ /*memory size*/ circuit_size - 1,
307 /*largest possible index*/ circuit_size,
308 /* offset */ 1 };
309 }
310 for (auto& poly : this->get_unshifted()) {
311 if (poly.is_empty()) {
312 // Not set above
313 poly = Polynomial{ /*fully formed*/ circuit_size };
314 }
315 }
316 set_shifted();
317 }
320 ProverPolynomials_(ProverPolynomials_&& o) noexcept = default;
323 [[nodiscard]] size_t get_polynomial_size() const { return this->q_c.size(); }
324 [[nodiscard]] AllValues_<HasZK_> get_row(const size_t row_idx) const
325 {
326 AllValues_<HasZK_> result;
327 for (auto [result_field, polynomial] : zip_view(result.get_all(), this->get_all())) {
328 result_field = polynomial[row_idx];
329 }
330 return result;
331 }
332
334 {
335 AllValues_<HasZK_> result;
336 for (auto [result_field, polynomial] : zip_view(result.get_sigmas(), this->get_sigmas())) {
337 result_field = polynomial[row_idx];
338 }
339 for (auto [result_field, polynomial] : zip_view(result.get_ids(), this->get_ids())) {
340 result_field = polynomial[row_idx];
341 }
342 for (auto [result_field, polynomial] : zip_view(result.get_wires(), this->get_wires())) {
343 result_field = polynomial[row_idx];
344 }
345 return result;
346 }
347
348 // Set all shifted polynomials based on their to-be-shifted counterpart
350 {
351 for (auto [shifted, to_be_shifted] : zip_view(this->get_shifted(), this->get_to_be_shifted())) {
352 shifted = to_be_shifted.shifted();
353 }
354 }
355
356 void increase_polynomials_virtual_size(const size_t size_in)
357 {
358 for (auto& polynomial : this->get_all()) {
359 polynomial.increase_virtual_size(size_in);
360 }
361 }
362 };
363
365
367
377
379
383 template <bool HasZK_ = HasZK>
386
388
394
399
404
411 class CommitmentLabels : public AllEntities<std::string> {
412 public:
414 {
415 w_l = "W_L";
416 w_r = "W_R";
417 w_o = "W_O";
418 w_4 = "W_4";
419 z_perm = "Z_PERM";
420 lookup_inverses = "LOOKUP_INVERSES";
421 lookup_read_counts = "LOOKUP_READ_COUNTS";
422 lookup_read_tags = "LOOKUP_READ_TAGS";
423
424 q_c = "Q_C";
425 q_l = "Q_L";
426 q_r = "Q_R";
427 q_o = "Q_O";
428 q_4 = "Q_4";
429 q_m = "Q_M";
430 q_lookup = "Q_LOOKUP";
431 q_arith = "Q_ARITH";
432 q_delta_range = "Q_SORT";
433 q_elliptic = "Q_ELLIPTIC";
434 q_memory = "Q_MEMORY";
435 q_nnf = "Q_NNF";
436 q_poseidon2_external = "Q_POSEIDON2_EXTERNAL";
437 q_poseidon2_internal = "Q_POSEIDON2_INTERNAL";
438 sigma_1 = "SIGMA_1";
439 sigma_2 = "SIGMA_2";
440 sigma_3 = "SIGMA_3";
441 sigma_4 = "SIGMA_4";
442 id_1 = "ID_1";
443 id_2 = "ID_2";
444 id_3 = "ID_3";
445 id_4 = "ID_4";
446 table_1 = "TABLE_1";
447 table_2 = "TABLE_2";
448 table_3 = "TABLE_3";
449 table_4 = "TABLE_4";
450 lagrange_first = "LAGRANGE_FIRST";
451 lagrange_last = "LAGRANGE_LAST";
452 };
453 };
454
460 template <typename Commitment, typename VerificationKey, bool HasZK_ = HasZK>
461 class VerifierCommitments_ : public AllEntities_<Commitment, HasZK_> {
462 public:
463 VerifierCommitments_(const std::shared_ptr<VerificationKey>& verification_key,
464 const std::optional<WitnessEntities<Commitment>>& witness_commitments = std::nullopt)
465 {
466 // Copy the precomputed polynomial commitments into this
467 for (auto [precomputed, precomputed_in] : zip_view(this->get_precomputed(), verification_key->get_all())) {
468 precomputed = precomputed_in;
469 }
470
471 // If provided, copy the witness polynomial commitments into this
472 if (witness_commitments.has_value()) {
473 for (auto [witness, witness_in] :
474 zip_view(this->get_witness(), witness_commitments.value().get_all())) {
475 witness = witness_in;
476 }
477
478 // Set shifted commitments
479 this->w_l_shift = witness_commitments->w_l;
480 this->w_r_shift = witness_commitments->w_r;
481 this->w_o_shift = witness_commitments->w_o;
482 this->w_4_shift = witness_commitments->w_4;
483 this->z_perm_shift = witness_commitments->z_perm;
484 }
485 }
486 }; // namespace bb
487 // Specialize for Ultra (general case used in UltraRecursive).
489};
490
491} // 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 𝔾₁.
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...
static constexpr size_t NUM_WIRES
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.
ZK-specific entities (only used when HasZK = true)
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_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) auto get_non_gate_selectors()
A container for polynomials handles.
void increase_polynomials_virtual_size(const size_t size_in)
ProverPolynomials_(const ProverPolynomials_ &o)=delete
AllValues_< HasZK_ > get_row_for_permutation_arg(size_t row_idx)
ProverPolynomials_ & operator=(ProverPolynomials_ &&o) noexcept=default
ProverPolynomials_(ProverPolynomials_ &&o) noexcept=default
AllValues_< HasZK_ > get_row(const size_t row_idx) const
ProverPolynomials_(size_t circuit_size)
ProverPolynomials_ & operator=(const ProverPolynomials_ &)=delete
Class for ShitftedEntities, containing shifted witness polynomials.
DEFINE_FLAVOR_MEMBERS(DataType, w_l_shift, w_r_shift, w_o_shift, w_4_shift, z_perm_shift) auto get_shifted()
A container encapsulating all the commitments that the verifier receives (to precomputed 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, z_perm, lookup_inverses, lookup_read_counts, lookup_read_tags) auto get_wires()
static constexpr RepeatedCommitmentsData REPEATED_COMMITMENTS
crypto::Poseidon2< crypto::Poseidon2Bn254ScalarFieldParams > HashFunction
static constexpr size_t BATCHED_RELATION_PARTIAL_LENGTH
static constexpr bool USE_PADDING
static constexpr size_t NUM_RELATIONS
static constexpr size_t NUM_PRECOMPUTED_ENTITIES
static constexpr bool HasZK
static constexpr bool is_decider
static constexpr bool USE_SHORT_MONOMIALS
static constexpr size_t FINAL_PCS_MSM_SIZE(size_t log_n=VIRTUAL_LOG_N)
static constexpr size_t num_frs_comm
Curve::ScalarField FF
Curve::AffineElement Commitment
static constexpr size_t num_frs_fr
static constexpr size_t MAX_PARTIAL_RELATION_LENGTH
static constexpr size_t NUM_SUBRELATIONS
static constexpr size_t VIRTUAL_LOG_N
std::tuple< bb::ArithmeticRelation< FF >, bb::UltraPermutationRelation< FF >, bb::LogDerivLookupRelation< FF >, bb::DeltaRangeConstraintRelation< FF >, bb::EllipticRelation< FF >, bb::MemoryRelation< FF >, bb::NonNativeFieldRelation< FF >, bb::Poseidon2ExternalRelation< FF >, bb::Poseidon2InternalRelation< FF > > Relations_
Curve::Element GroupElement
Relations_< FF > Relations
static constexpr size_t DECIDER_PROOF_LENGTH(size_t virtual_log_n=VIRTUAL_LOG_N)
static constexpr size_t NUM_ALL_ENTITIES
static constexpr size_t OINK_PROOF_LENGTH_WITHOUT_PUB_INPUTS
static constexpr size_t NUM_UNSHIFTED_ENTITIES
static constexpr size_t NUM_SHIFTED_ENTITIES
static constexpr size_t PROOF_LENGTH_WITHOUT_PUB_INPUTS(size_t virtual_log_n=VIRTUAL_LOG_N)
static constexpr size_t NUM_WIRES
static constexpr size_t NUM_WITNESS_ENTITIES
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 ...
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
UltraCircuitBuilder_< UltraExecutionTraceBlocks > UltraCircuitBuilder
RefArray< T,(Ns+...)> constexpr concatenate(const RefArray< T, Ns > &... ref_arrays)
Concatenates multiple RefArray objects into a single RefArray.
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:121