Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
generic_permutation_relation.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: Completed, auditors: [Federico], commit: }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
7#pragma once
8#include <array>
9#include <tuple>
10
15
16namespace bb {
17
49
68template <typename Settings, typename FF_> class GenericPermutationRelationImpl {
69 public:
70 using FF = FF_;
72
73 // The term counts should stay set to 1 unless we want to permute several columns at once as accumulated
74 // sets (not as tuples).
75 static constexpr size_t NUM_LOOKUP_TERMS = 1;
76 static constexpr size_t NUM_TABLE_TERMS = 1;
77
78 // Specialization of the calculation of the length for the generic lookup relation to the permutation relation; note
79 // that the second subrelation degree is one lower than the one in the lookup argument because there is not read
80 // count polynomial
82 Settings::INVERSE_EXISTS_POLYNOMIAL_DEGREE) +
83 1; // inverse polynomial correctness sub-relation
84 static constexpr size_t SECOND_RELATION_PARTIAL_LENGTH =
85 NUM_LOOKUP_TERMS + NUM_TABLE_TERMS + 2; // log-derived terms sub-relation
87
88 // We use the max of the subrelation lengths because the inverses of lookup/table terms must be used in both
89 // subrelations
90 static constexpr std::array<size_t, 2> SUBRELATION_PARTIAL_LENGTHS{
91 LENGTH, // inverse polynomial correctness sub-relation
92 LENGTH // log-derived terms subrelation
93 };
94
102 static constexpr std::array<bool, 2> SUBRELATION_LINEARLY_INDEPENDENT = { true, false };
103
111 template <typename AllValues> static bool operation_exists_at_row(const AllValues& row)
112
113 {
114 return Settings::inverse_polynomial_is_computed_at_row(row);
115 }
116
121 template <typename AllEntities> static auto& get_inverse_polynomial(AllEntities& in)
122 {
123 return std::get<PolynomialStructure::get_inverse_polynomial_index()>(Settings::get_nonconst_entities(in));
124 }
125
134 template <typename Accumulator, typename AllEntities>
135 static Accumulator compute_inverse_exists(const AllEntities& in)
136 {
137 using View = typename Accumulator::View;
138
139 Accumulator const& first_set_enabled = Accumulator(
140 View(std::get<PolynomialStructure::get_lookup_term_predicate_index()>(Settings::get_const_entities(in))));
141
142 Accumulator const& second_set_enabled = Accumulator(
143 View(std::get<PolynomialStructure::get_table_term_predicate_index()>(Settings::get_const_entities(in))));
144
145 // The following expression (assuming the values are boolean) is the algebraic representation of a logical OR
146 return (first_set_enabled + second_set_enabled - (first_set_enabled * second_set_enabled));
147 }
148
158 template <typename Accumulator, size_t lookup_index, typename AllEntities>
159 static Accumulator get_lookup_term_predicate(const AllEntities& in)
160
161 {
162 static_assert(lookup_index < NUM_LOOKUP_TERMS);
163 using View = typename Accumulator::View;
164
165 // The selector/wire value that determines that an element from the first set needs to be included. Can be
166 // different from the wire used in the write part.
167 return Accumulator(
168 View(std::get<PolynomialStructure::get_lookup_term_predicate_index()>(Settings::get_const_entities(in))));
169 }
170
180 template <typename Accumulator, size_t table_index, typename AllEntities>
181 static Accumulator get_table_term_predicate(const AllEntities& in)
182 {
183 static_assert(table_index < NUM_TABLE_TERMS);
184 using View = typename Accumulator::View;
185
186 return Accumulator(
187 View(std::get<PolynomialStructure::get_table_term_predicate_index()>(Settings::get_const_entities(in))));
188 }
189
201 template <typename Accumulator, size_t lookup_index, typename AllEntities, typename Parameters>
202 static Accumulator compute_lookup_term(const AllEntities& in, const Parameters& params)
203 {
204 using View = typename Accumulator::View;
205
206 static_assert(lookup_index < NUM_LOOKUP_TERMS);
207 constexpr size_t start_polynomial_index = PolynomialStructure::compute_lookup_term_polynomial_offset();
208 const FF beta = params.beta;
209 const FF gamma = params.gamma;
210
211 auto result = Accumulator(0);
212
213 // Retrieve all polynomials used
214 const auto all_polynomials = Settings::get_const_entities(in);
215
216 // Iterate over tuple and sum as a polynomial over beta
217 bb::constexpr_for<start_polynomial_index, start_polynomial_index + Settings::COLUMNS_PER_SET, 1>(
218 [&]<size_t i>() { result = result * beta + View(std::get<i>(all_polynomials)); });
219
220 return result + gamma;
221 }
222
234 template <typename Accumulator, size_t table_index, typename AllEntities, typename Parameters>
235 static Accumulator compute_table_term(const AllEntities& in, const Parameters& params)
236 {
237 using View = typename Accumulator::View;
238
239 static_assert(table_index < NUM_TABLE_TERMS);
240 constexpr size_t start_polynomial_index = PolynomialStructure::compute_table_term_polynomial_offset();
241 const FF beta = params.beta;
242 const FF gamma = params.gamma;
243
244 auto result = Accumulator(0);
245
246 // Retrieve all polynomials used
247 const auto all_polynomials = Settings::get_const_entities(in);
248
249 // Iterate over tuple and sum as a polynomial over beta
250 bb::constexpr_for<start_polynomial_index, start_polynomial_index + Settings::COLUMNS_PER_SET, 1>(
251 [&]<size_t i>() { result = result * beta + View(std::get<i>(all_polynomials)); });
252
253 return result + gamma;
254 }
255
274 template <typename ContainerOverSubrelations, typename AllEntities, typename Parameters>
275 static void accumulate(ContainerOverSubrelations& accumulator,
276 const AllEntities& in,
277 const Parameters& params,
278 const FF& scaling_factor)
279 {
282 ContainerOverSubrelations,
283 AllEntities,
284 Parameters,
285 true>(accumulator, in, params, scaling_factor);
286 }
287};
288
289template <typename Settings, typename FF>
291
292} // namespace bb
Implementation of a generic permutation relation.
static Accumulator compute_lookup_term(const AllEntities &in, const Parameters &params)
Compute the value of the lookup term at a given index.
static constexpr std::array< bool, 2 > SUBRELATION_LINEARLY_INDEPENDENT
We apply the power polynomial only to the first subrelation.
static Accumulator compute_table_term(const AllEntities &in, const Parameters &params)
Compute the value of a table term at a given index.
static bool operation_exists_at_row(const AllValues &row)
Check if we need to compute the inverse polynomial element value for this row.
static Accumulator compute_inverse_exists(const AllEntities &in)
Get selector/wire switching on (1) or off (0) inverse computation.
static Accumulator get_table_term_predicate(const AllEntities &in)
Extract predicate enabling looking up a given table term at this row.
static void accumulate(ContainerOverSubrelations &accumulator, const AllEntities &in, const Parameters &params, const FF &scaling_factor)
Compute generic log-derivative set permutation subrelation accumulation.
static constexpr std::array< size_t, 2 > SUBRELATION_PARTIAL_LENGTHS
static Accumulator get_lookup_term_predicate(const AllEntities &in)
Extract predicate enabling looking up a given lookup term at this row.
static auto & get_inverse_polynomial(AllEntities &in)
Get the inverse permutation polynomial (needed to compute its value)
Specialization of the polynomial structure required for the lookup argument to the case of the permut...
static constexpr size_t get_lookup_term_predicate_index()
static constexpr size_t TABLE_TERM_PREDICATE_START_POLYNOMIAL_INDEX
static constexpr size_t LOOKUP_TERM_PREDICATE_START_POLYNOMIAL_INDEX
static constexpr size_t compute_table_term_polynomial_offset()
static constexpr size_t compute_lookup_term_polynomial_offset()
A wrapper for Relations to expose methods used by the Sumcheck prover or verifier to add the contribu...
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
void _accumulate_logderivative_subrelation_contributions(ContainerOverSubrelations &accumulator, const AllEntities &in, const Parameters &params, const FF &scaling_factor)
Unified implementation of log-derivative subrelation accumulation.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13