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 static constexpr bool is_decider = true;
122
127 template <typename DataType_> class PrecomputedEntities {
128 public:
129 bool operator==(const PrecomputedEntities&) const = default;
130 using DataType = DataType_;
132 q_m, // column 0
133 q_c, // column 1
134 q_l, // column 2
135 q_r, // column 3
136 q_o, // column 4
137 q_4, // column 5
138 q_lookup, // column 6
139 q_arith, // column 7
140 q_delta_range, // column 8
141 q_elliptic, // column 9
142 q_memory, // column 10
143 q_nnf, // column 11
144 q_poseidon2_external, // column 12
145 q_poseidon2_internal, // column 13
146 sigma_1, // column 14
147 sigma_2, // column 15
148 sigma_3, // column 16
149 sigma_4, // column 17
150 id_1, // column 18
151 id_2, // column 19
152 id_3, // column 20
153 id_4, // column 21
154 table_1, // column 22
155 table_2, // column 23
156 table_3, // column 24
157 table_4, // column 25
158 lagrange_first, // column 26
159 lagrange_last) // column 27
160
161 auto get_non_gate_selectors() { return RefArray{ q_m, q_c, q_l, q_r, q_o, q_4 }; }
163 {
164 return RefArray{ q_lookup, q_arith, q_delta_range, q_elliptic,
165 q_memory, q_nnf, q_poseidon2_external, q_poseidon2_internal };
166 }
167 auto get_selectors() { return concatenate(get_non_gate_selectors(), get_gate_selectors()); }
168
169 auto get_sigmas() { return RefArray{ sigma_1, sigma_2, sigma_3, sigma_4 }; };
170 auto get_ids() { return RefArray{ id_1, id_2, id_3, id_4 }; };
171 auto get_tables() { return RefArray{ table_1, table_2, table_3, table_4 }; };
172 };
173
178 template <typename DataType, bool HasZK_ = HasZK> class MaskingEntities {
179 public:
180 // When ZK is disabled, this class is empty
181 auto get_all() { return RefArray<DataType, 0>{}; }
182 auto get_all() const { return RefArray<const DataType, 0>{}; }
183 static auto get_labels() { return std::vector<std::string>{}; }
184 };
185
186 // Specialization for when ZK is enabled
187 template <typename DataType> class MaskingEntities<DataType, true> {
188 public:
189 DEFINE_FLAVOR_MEMBERS(DataType, gemini_masking_poly)
190 };
191
195 template <typename DataType> class WitnessEntities {
196 public:
198 w_l, // column 0
199 w_r, // column 1
200 w_o, // column 2
201 w_4, // column 3
202 z_perm, // column 4
203 lookup_inverses, // column 5
204 lookup_read_counts, // column 6
205 lookup_read_tags) // column 7
206
207 auto get_wires() { return RefArray{ w_l, w_r, w_o, w_4 }; };
208 auto get_to_be_shifted() { return RefArray{ w_l, w_r, w_o, w_4, z_perm }; };
209 };
210
214 template <typename DataType> class ShiftedEntities {
215 public:
217 w_l_shift, // column 0
218 w_r_shift, // column 1
219 w_o_shift, // column 2
220 w_4_shift, // column 3
221 z_perm_shift) // column 4
222
223 auto get_shifted() { return RefArray{ w_l_shift, w_r_shift, w_o_shift, w_4_shift, z_perm_shift }; };
224 };
225
235 template <typename DataType, bool HasZK_ = HasZK>
256
257 // Default AllEntities alias (no ZK)
258 template <typename DataType> using AllEntities = AllEntities_<DataType, HasZK>;
259
264 template <bool HasZK_ = HasZK> class AllValues_ : public AllEntities_<FF, HasZK_> {
265 public:
267 using Base::Base;
268 };
269
271
275 template <bool HasZK_ = HasZK> class ProverPolynomials_ : public AllEntities_<Polynomial, HasZK_> {
276 public:
277 // Define all operations as default, except copy construction/assignment
279 ProverPolynomials_(size_t circuit_size)
280 {
281
282 BB_BENCH_NAME("creating empty prover polys");
283
284 for (auto& poly : this->get_to_be_shifted()) {
285 poly = Polynomial{ /*memory size*/ circuit_size - 1,
286 /*largest possible index*/ circuit_size,
287 /* offset */ 1 };
288 }
289 for (auto& poly : this->get_unshifted()) {
290 if (poly.is_empty()) {
291 // Not set above
292 poly = Polynomial{ /*fully formed*/ circuit_size };
293 }
294 }
295 set_shifted();
296 }
299 ProverPolynomials_(ProverPolynomials_&& o) noexcept = default;
302 [[nodiscard]] size_t get_polynomial_size() const { return this->q_c.size(); }
303 [[nodiscard]] AllValues_<HasZK_> get_row(const size_t row_idx) const
304 {
305 AllValues_<HasZK_> result;
306 for (auto [result_field, polynomial] : zip_view(result.get_all(), this->get_all())) {
307 result_field = polynomial[row_idx];
308 }
309 return result;
310 }
311
313 {
314 AllValues_<HasZK_> result;
315 for (auto [result_field, polynomial] : zip_view(result.get_sigmas(), this->get_sigmas())) {
316 result_field = polynomial[row_idx];
317 }
318 for (auto [result_field, polynomial] : zip_view(result.get_ids(), this->get_ids())) {
319 result_field = polynomial[row_idx];
320 }
321 for (auto [result_field, polynomial] : zip_view(result.get_wires(), this->get_wires())) {
322 result_field = polynomial[row_idx];
323 }
324 return result;
325 }
326
327 // Set all shifted polynomials based on their to-be-shifted counterpart
329 {
330 for (auto [shifted, to_be_shifted] : zip_view(this->get_shifted(), this->get_to_be_shifted())) {
331 shifted = to_be_shifted.shifted();
332 }
333 }
334
335 void increase_polynomials_virtual_size(const size_t size_in)
336 {
337 for (auto& polynomial : this->get_all()) {
338 polynomial.increase_virtual_size(size_in);
339 }
340 }
341 };
342
344
346
356
358
362 template <bool HasZK_ = HasZK>
365
367
373
378
383
390 class CommitmentLabels : public AllEntities<std::string> {
391 public:
393 {
394 w_l = "W_L";
395 w_r = "W_R";
396 w_o = "W_O";
397 w_4 = "W_4";
398 z_perm = "Z_PERM";
399 lookup_inverses = "LOOKUP_INVERSES";
400 lookup_read_counts = "LOOKUP_READ_COUNTS";
401 lookup_read_tags = "LOOKUP_READ_TAGS";
402
403 q_c = "Q_C";
404 q_l = "Q_L";
405 q_r = "Q_R";
406 q_o = "Q_O";
407 q_4 = "Q_4";
408 q_m = "Q_M";
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 };
432 };
433
439 template <typename Commitment, typename VerificationKey, bool HasZK_ = HasZK>
440 class VerifierCommitments_ : public AllEntities_<Commitment, HasZK_> {
441 public:
442 VerifierCommitments_(const std::shared_ptr<VerificationKey>& verification_key,
443 const std::optional<WitnessEntities<Commitment>>& witness_commitments = std::nullopt)
444 {
445 // Copy the precomputed polynomial commitments into this
446 for (auto [precomputed, precomputed_in] : zip_view(this->get_precomputed(), verification_key->get_all())) {
447 precomputed = precomputed_in;
448 }
449
450 // If provided, copy the witness polynomial commitments into this
451 if (witness_commitments.has_value()) {
452 for (auto [witness, witness_in] :
453 zip_view(this->get_witness(), witness_commitments.value().get_all())) {
454 witness = witness_in;
455 }
456
457 // Set shifted commitments
458 this->w_l_shift = witness_commitments->w_l;
459 this->w_r_shift = witness_commitments->w_r;
460 this->w_o_shift = witness_commitments->w_o;
461 this->w_4_shift = witness_commitments->w_4;
462 this->z_perm_shift = witness_commitments->z_perm;
463 }
464 }
465 }; // namespace bb
466 // Specialize for Ultra (general case used in UltraRecursive).
468};
469
470} // namespace bb
#define BB_BENCH_NAME(name)
Definition bb_bench.hpp:225
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 NUM_ALL_ENTITIES
static constexpr size_t NUM_UNSHIFTED_ENTITIES
static constexpr size_t NUM_SHIFTED_ENTITIES
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:591
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