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
32
33namespace bb {
34
36 public:
42 using PCS = KZG<Curve>;
46 using Codec = FrCodec;
49
50 static constexpr size_t VIRTUAL_LOG_N = CONST_PROOF_SIZE_LOG_N;
51 // indicates when evaluating sumcheck, edges can be left as degree-1 monomials
52 static constexpr bool USE_SHORT_MONOMIALS = true;
53
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 (witness polynomials,
61 // precomputed polynomials and shifts). We often need containers of this size to hold related data, so we choose a
62 // name more agnostic than `NUM_POLYNOMIALS`.
63 static constexpr size_t NUM_ALL_ENTITIES = 41;
64 // The number of polynomials precomputed to describe a circuit and to aid a prover in constructing a satisfying
65 // assignment of witnesses. We again choose a neutral name.
66 static constexpr size_t NUM_PRECOMPUTED_ENTITIES = 28;
67 // The total number of witness entities not including shifts.
68 static constexpr size_t NUM_WITNESS_ENTITIES = 8;
69 // The number of shifted witness entities including derived witness entities
70 static constexpr size_t NUM_SHIFTED_ENTITIES = 5;
71 // The number of unshifted witness entities
73
74 // A container to be fed to ShpleminiVerifier to avoid redundant scalar muls
77
78 // Size of the final PCS MSM after KZG adds quotient commitment:
79 // 1 (Shplonk Q) + NUM_UNSHIFTED + (log_n - 1) Gemini folds + 1 (G1 identity) + 1 (KZG W)
80 // (shifted commitments are removed as duplicates)
81 static constexpr size_t FINAL_PCS_MSM_SIZE(size_t log_n = VIRTUAL_LOG_N)
82 {
83 return NUM_UNSHIFTED_ENTITIES + log_n + 2;
84 }
85
86 // define the tuple of Relations that comprise the Sumcheck relation
87 // Note: made generic for use in MegaRecursive.
88 template <typename FF>
89
90 // List of relations reflecting the Ultra arithmetisation. WARNING: As UltraKeccak flavor inherits from
91 // Ultra flavor any change of ordering in this tuple needs to be reflected in the smart contract, otherwise
92 // relation accumulation will not match.
102
104
105 static constexpr size_t MAX_PARTIAL_RELATION_LENGTH = compute_max_partial_relation_length<Relations>();
106 static_assert(MAX_PARTIAL_RELATION_LENGTH == 7);
107 static constexpr size_t NUM_SUBRELATIONS = compute_number_of_subrelations<Relations>();
108 // A challenge whose powers are used to batch subrelation contributions during Sumcheck
110
111 // BATCHED_RELATION_PARTIAL_LENGTH = algebraic degree of sumcheck relation *after* multiplying by the `pow_zeta`
112 // random polynomial e.g. For \sum(x) [A(x) * B(x) + C(x)] * PowZeta(X), relation length = 2 and random relation
113 // length = 3
116
117 static constexpr size_t num_frs_comm = FrCodec::calc_num_fields<Commitment>();
118 static constexpr size_t num_frs_fr = FrCodec::calc_num_fields<FF>();
119
120 // Proof length formula methods
121 static constexpr size_t OINK_PROOF_LENGTH_WITHOUT_PUB_INPUTS =
122 /* 1. NUM_WITNESS_ENTITIES commitments */ (NUM_WITNESS_ENTITIES * num_frs_comm);
123
124 static constexpr size_t DECIDER_PROOF_LENGTH(size_t virtual_log_n = VIRTUAL_LOG_N)
125 {
126 return /* 2. virtual_log_n sumcheck univariates */
127 (virtual_log_n * BATCHED_RELATION_PARTIAL_LENGTH * num_frs_fr) +
128 /* 3. NUM_ALL_ENTITIES sumcheck evaluations */ (NUM_ALL_ENTITIES * num_frs_fr) +
129 /* 4. virtual_log_n - 1 Gemini Fold commitments */ ((virtual_log_n - 1) * num_frs_comm) +
130 /* 5. virtual_log_n Gemini a evaluations */ (virtual_log_n * num_frs_fr) +
131 /* 6. Shplonk Q commitment */ (num_frs_comm) +
132 /* 7. KZG W commitment */ (num_frs_comm);
133 }
134
135 static constexpr size_t PROOF_LENGTH_WITHOUT_PUB_INPUTS(size_t virtual_log_n = VIRTUAL_LOG_N)
136 {
138 }
139
140 // Whether or not the first row of the execution trace is reserved for 0s to enable shifts
141 static constexpr bool has_zero_row = true;
142
143 static constexpr bool is_decider = true;
144
149 template <typename DataType_> class PrecomputedEntities {
150 public:
151 bool operator==(const PrecomputedEntities&) const = default;
152 using DataType = DataType_;
154 q_m, // column 0
155 q_c, // column 1
156 q_l, // column 2
157 q_r, // column 3
158 q_o, // column 4
159 q_4, // column 5
160 q_lookup, // column 6
161 q_arith, // column 7
162 q_delta_range, // column 8
163 q_elliptic, // column 9
164 q_memory, // column 10
165 q_nnf, // column 11
166 q_poseidon2_external, // column 12
167 q_poseidon2_internal, // column 13
168 sigma_1, // column 14
169 sigma_2, // column 15
170 sigma_3, // column 16
171 sigma_4, // column 17
172 id_1, // column 18
173 id_2, // column 19
174 id_3, // column 20
175 id_4, // column 21
176 table_1, // column 22
177 table_2, // column 23
178 table_3, // column 24
179 table_4, // column 25
180 lagrange_first, // column 26
181 lagrange_last) // column 27
182
183 auto get_non_gate_selectors() { return RefArray{ q_m, q_c, q_l, q_r, q_o, q_4 }; }
185 {
186 return RefArray{ q_lookup, q_arith, q_delta_range, q_elliptic,
187 q_memory, q_nnf, q_poseidon2_external, q_poseidon2_internal };
188 }
189 auto get_selectors() { return concatenate(get_non_gate_selectors(), get_gate_selectors()); }
190
191 auto get_sigmas() { return RefArray{ sigma_1, sigma_2, sigma_3, sigma_4 }; };
192 auto get_ids() { return RefArray{ id_1, id_2, id_3, id_4 }; };
193 auto get_tables() { return RefArray{ table_1, table_2, table_3, table_4 }; };
194 };
195
200 template <typename DataType, bool HasZK_ = HasZK> class MaskingEntities {
201 public:
202 // When ZK is disabled, this class is empty
203 auto get_all() { return RefArray<DataType, 0>{}; }
204 auto get_all() const { return RefArray<const DataType, 0>{}; }
205 static auto get_labels() { return std::vector<std::string>{}; }
206 };
207
208 // Specialization for when ZK is enabled
209 template <typename DataType> class MaskingEntities<DataType, true> {
210 public:
211 DEFINE_FLAVOR_MEMBERS(DataType, gemini_masking_poly)
212 };
213
217 template <typename DataType> class WitnessEntities {
218 public:
220 w_l, // column 0
221 w_r, // column 1
222 w_o, // column 2
223 w_4, // column 3
224 z_perm, // column 4
225 lookup_inverses, // column 5
226 lookup_read_counts, // column 6
227 lookup_read_tags) // column 7
228
229 auto get_wires() { return RefArray{ w_l, w_r, w_o, w_4 }; };
230 auto get_to_be_shifted() { return RefArray{ w_l, w_r, w_o, w_4, z_perm }; };
231 };
232
236 template <typename DataType> class ShiftedEntities {
237 public:
239 w_l_shift, // column 0
240 w_r_shift, // column 1
241 w_o_shift, // column 2
242 w_4_shift, // column 3
243 z_perm_shift) // column 4
244
245 auto get_shifted() { return RefArray{ w_l_shift, w_r_shift, w_o_shift, w_4_shift, z_perm_shift }; };
246 };
247
257 template <typename DataType, bool HasZK_ = HasZK>
278
279 // Default AllEntities alias (no ZK)
280 template <typename DataType> using AllEntities = AllEntities_<DataType, HasZK>;
281
286 template <bool HasZK_ = HasZK> class AllValues_ : public AllEntities_<FF, HasZK_> {
287 public:
289 using Base::Base;
290 };
291
293
297 // TODO(https://github.com/AztecProtocol/barretenberg/issues/966): use inheritance
298 template <bool HasZK_ = HasZK> class ProverPolynomials_ : public AllEntities_<Polynomial, HasZK_> {
299 public:
300 // Define all operations as default, except copy construction/assignment
302 ProverPolynomials_(size_t circuit_size)
303 {
304
305 BB_BENCH_NAME("creating empty prover polys");
306
307 for (auto& poly : this->get_to_be_shifted()) {
308 poly = Polynomial{ /*memory size*/ circuit_size - 1,
309 /*largest possible index*/ circuit_size,
310 /* offset */ 1 };
311 }
312 for (auto& poly : this->get_unshifted()) {
313 if (poly.is_empty()) {
314 // Not set above
315 poly = Polynomial{ /*fully formed*/ circuit_size };
316 }
317 }
318 set_shifted();
319 }
322 ProverPolynomials_(ProverPolynomials_&& o) noexcept = default;
325 [[nodiscard]] size_t get_polynomial_size() const { return this->q_c.size(); }
326 [[nodiscard]] AllValues_<HasZK_> get_row(const size_t row_idx) const
327 {
328 AllValues_<HasZK_> result;
329 for (auto [result_field, polynomial] : zip_view(result.get_all(), this->get_all())) {
330 result_field = polynomial[row_idx];
331 }
332 return result;
333 }
334
336 {
337 AllValues_<HasZK_> result;
338 for (auto [result_field, polynomial] : zip_view(result.get_sigmas(), this->get_sigmas())) {
339 result_field = polynomial[row_idx];
340 }
341 for (auto [result_field, polynomial] : zip_view(result.get_ids(), this->get_ids())) {
342 result_field = polynomial[row_idx];
343 }
344 for (auto [result_field, polynomial] : zip_view(result.get_wires(), this->get_wires())) {
345 result_field = polynomial[row_idx];
346 }
347 return result;
348 }
349
350 // Set all shifted polynomials based on their to-be-shifted counterpart
352 {
353 for (auto [shifted, to_be_shifted] : zip_view(this->get_shifted(), this->get_to_be_shifted())) {
354 shifted = to_be_shifted.shifted();
355 }
356 }
357
358 void increase_polynomials_virtual_size(const size_t size_in)
359 {
360 for (auto& polynomial : this->get_all()) {
361 polynomial.increase_virtual_size(size_in);
362 }
363 }
364 };
365
367
369
379
381
385 template <bool HasZK_ = HasZK> class PartiallyEvaluatedMultivariates_ : public AllEntities_<Polynomial, HasZK_> {
386 public:
388 PartiallyEvaluatedMultivariates_(const size_t circuit_size)
389 {
390 BB_BENCH_NAME("PartiallyEvaluatedMultivariates constructor");
391
392 // Storage is only needed after the first partial evaluation, hence polynomials of
393 // size (n / 2)
394 for (auto& poly : this->get_all()) {
395 poly = Polynomial(circuit_size / 2);
396 }
397 }
398 PartiallyEvaluatedMultivariates_(const ProverPolynomials_<HasZK_>& full_polynomials, size_t circuit_size)
399 {
400 BB_BENCH_NAME("PartiallyEvaluatedMultivariates constructor");
401 for (auto [poly, full_poly] : zip_view(this->get_all(), full_polynomials.get_all())) {
402 // After the initial sumcheck round, the new size is CEIL(size/2).
403 size_t desired_size = full_poly.end_index() / 2 + full_poly.end_index() % 2;
404 poly = Polynomial(desired_size, circuit_size / 2);
405 }
406 }
407 };
408
410
416
421
426
433 class CommitmentLabels : public AllEntities<std::string> {
434 public:
436 {
437 w_l = "W_L";
438 w_r = "W_R";
439 w_o = "W_O";
440 w_4 = "W_4";
441 z_perm = "Z_PERM";
442 lookup_inverses = "LOOKUP_INVERSES";
443 lookup_read_counts = "LOOKUP_READ_COUNTS";
444 lookup_read_tags = "LOOKUP_READ_TAGS";
445
446 q_c = "Q_C";
447 q_l = "Q_L";
448 q_r = "Q_R";
449 q_o = "Q_O";
450 q_4 = "Q_4";
451 q_m = "Q_M";
452 q_lookup = "Q_LOOKUP";
453 q_arith = "Q_ARITH";
454 q_delta_range = "Q_SORT";
455 q_elliptic = "Q_ELLIPTIC";
456 q_memory = "Q_MEMORY";
457 q_nnf = "Q_NNF";
458 q_poseidon2_external = "Q_POSEIDON2_EXTERNAL";
459 q_poseidon2_internal = "Q_POSEIDON2_INTERNAL";
460 sigma_1 = "SIGMA_1";
461 sigma_2 = "SIGMA_2";
462 sigma_3 = "SIGMA_3";
463 sigma_4 = "SIGMA_4";
464 id_1 = "ID_1";
465 id_2 = "ID_2";
466 id_3 = "ID_3";
467 id_4 = "ID_4";
468 table_1 = "TABLE_1";
469 table_2 = "TABLE_2";
470 table_3 = "TABLE_3";
471 table_4 = "TABLE_4";
472 lagrange_first = "LAGRANGE_FIRST";
473 lagrange_last = "LAGRANGE_LAST";
474 };
475 };
476
482 template <typename Commitment, typename VerificationKey, bool HasZK_ = HasZK>
483 class VerifierCommitments_ : public AllEntities_<Commitment, HasZK_> {
484 public:
485 VerifierCommitments_(const std::shared_ptr<VerificationKey>& verification_key,
486 const std::optional<WitnessEntities<Commitment>>& witness_commitments = std::nullopt)
487 {
488 // Copy the precomputed polynomial commitments into this
489 for (auto [precomputed, precomputed_in] : zip_view(this->get_precomputed(), verification_key->get_all())) {
490 precomputed = precomputed_in;
491 }
492
493 // If provided, copy the witness polynomial commitments into this
494 if (witness_commitments.has_value()) {
495 for (auto [witness, witness_in] :
496 zip_view(this->get_witness(), witness_commitments.value().get_all())) {
497 witness = witness_in;
498 }
499
500 // Set shifted commitments
501 this->w_l_shift = witness_commitments->w_l;
502 this->w_r_shift = witness_commitments->w_r;
503 this->w_o_shift = witness_commitments->w_o;
504 this->w_4_shift = witness_commitments->w_4;
505 this->z_perm_shift = witness_commitments->z_perm;
506 }
507 }
508 }; // namespace bb
509 // Specialize for Ultra (general case used in UltraRecursive).
511};
512
513} // 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: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...
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 container for storing the partially evaluated multivariates produced by sumcheck.
PartiallyEvaluatedMultivariates_(const ProverPolynomials_< HasZK_ > &full_polynomials, size_t circuit_size)
PartiallyEvaluatedMultivariates_(const size_t circuit_size)
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 bool has_zero_row
bb::Polynomial< FF > Polynomial
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: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 ...
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