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: not started, auditors: [], date: YYYY-MM-DD }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
5// =====================
6
14#pragma once
15#include <array>
16#include <tuple>
17
22
23namespace bb {
29 INVERSE_POLYNOMIAL_INDEX, /* The index of the inverse polynomial*/
30 ENABLE_INVERSE_CORRECTNESS_CHECK_POLYNOMIAL_INDEX, /* The index of the polynomial enabling first subrelation*/
31 FIRST_PERMUTATION_SET_ENABLE_POLYNOMIAL_INDEX, /* The index of the polynomial that adds an element from the first
32 set to the sum*/
33 SECOND_PERMUTATION_SET_ENABLE_POLYNOMIAL_INDEX, /* The index of the polynomial that adds an element from the second
34 set to the sum*/
35
36 PERMUTATION_SETS_START_POLYNOMIAL_INDEX, /* The starting index of the polynomials that are used in the permutation
37 sets*/
38};
39
40template <typename Settings, typename FF_> class GenericPermutationRelationImpl {
41 public:
42 using FF = FF_;
43 // Read and write terms counts should stay set to 1 unless we want to permute several columns at once as accumulated
44 // sets (not as tuples).
45 static constexpr size_t READ_TERMS = 1;
46 static constexpr size_t WRITE_TERMS = 1;
47 // 1 + polynomial degree of this relation
48 static constexpr size_t LENGTH = READ_TERMS + WRITE_TERMS + 3; // 5
49
50 static constexpr std::array<size_t, 2> SUBRELATION_PARTIAL_LENGTHS{
51 LENGTH, // inverse polynomial correctness sub-relation
52 LENGTH // log-derived terms subrelation
53 };
54
62 static constexpr std::array<bool, 2> SUBRELATION_LINEARLY_INDEPENDENT = { true, false };
63
70 template <typename AllValues> static bool operation_exists_at_row(const AllValues& row)
71
72 {
73 return Settings::inverse_polynomial_is_computed_at_row(row);
74 }
75
80 template <typename AllEntities> static auto& get_inverse_polynomial(AllEntities& in)
81 {
82 // WIRE containing the inverse of the product of terms at this row. Used to reconstruct individual inversed
83 // terms
84 return std::get<INVERSE_POLYNOMIAL_INDEX>(Settings::get_nonconst_entities(in));
85 }
86
92 template <typename Accumulator, typename AllEntities>
93 static Accumulator compute_inverse_exists(const AllEntities& in)
94 {
95 using View = typename Accumulator::View;
96
97 // WIRE/SELECTOR enabling the permutation used in the sumcheck computation. This affects the first
98 // subrelation
99 Accumulator const& first_set_enabled = Accumulator(
100 View(std::get<FIRST_PERMUTATION_SET_ENABLE_POLYNOMIAL_INDEX>(Settings::get_const_entities(in))));
101
102 Accumulator const& second_set_enabled = Accumulator(
103 View(std::get<SECOND_PERMUTATION_SET_ENABLE_POLYNOMIAL_INDEX>(Settings::get_const_entities(in))));
104
105 // This has the truth table of a logical OR
106 return (first_set_enabled + second_set_enabled - (first_set_enabled * second_set_enabled));
107 }
108
114 template <typename Accumulator, size_t read_index, typename AllEntities>
115 static Accumulator compute_read_term_predicate(const AllEntities& in)
116
117 {
118 static_assert(read_index < READ_TERMS);
119 using View = typename Accumulator::View;
120
121 // The selector/wire value that determines that an element from the first set needs to be included. Can be
122 // different from the wire used in the write part.
123 return Accumulator(
124 View(std::get<FIRST_PERMUTATION_SET_ENABLE_POLYNOMIAL_INDEX>(Settings::get_const_entities(in))));
125 }
126
132 template <typename Accumulator, size_t write_index, typename AllEntities>
133 static Accumulator compute_write_term_predicate(const AllEntities& in)
134 {
135 static_assert(write_index < WRITE_TERMS);
136 using View = typename Accumulator::View;
137
138 // The selector/wire value that determines that an element from the second set needs to be included. Can be
139 // different from the wire used in the read part.
140 return Accumulator(
141 View(std::get<SECOND_PERMUTATION_SET_ENABLE_POLYNOMIAL_INDEX>(Settings::get_const_entities(in))));
142 }
143
154 template <typename Accumulator, size_t read_index, typename AllEntities, typename Parameters>
155 static Accumulator compute_read_term(const AllEntities& in, const Parameters& params)
156 {
157 using View = typename Accumulator::View;
158
159 static_assert(read_index < READ_TERMS);
160
161 // Retrieve all polynomials used
162 const auto all_polynomials = Settings::get_const_entities(in);
163
164 auto result = Accumulator(0);
165
166 // Iterate over tuple and sum as a polynomial over beta
168 PERMUTATION_SETS_START_POLYNOMIAL_INDEX + Settings::COLUMNS_PER_SET,
169 1>([&]<size_t i>() { result = result * params.beta + View(std::get<i>(all_polynomials)); });
170
171 const auto& gamma = params.gamma;
172 return result + gamma;
173 }
174
185 template <typename Accumulator, size_t write_index, typename AllEntities, typename Parameters>
186 static Accumulator compute_write_term(const AllEntities& in, const Parameters& params)
187 {
188 using View = typename Accumulator::View;
189
190 static_assert(write_index < WRITE_TERMS);
191
192 // Get all used entities
193 const auto& used_entities = Settings::get_const_entities(in);
194
195 auto result = Accumulator(0);
196 // Iterate over tuple and sum as a polynomial over beta
198 PERMUTATION_SETS_START_POLYNOMIAL_INDEX + 2 * Settings::COLUMNS_PER_SET,
199 1>([&]<size_t i>() { result = result * params.beta + View(std::get<i>(used_entities)); });
200
201 const auto& gamma = params.gamma;
202 return result + gamma;
203 }
204
212 template <typename ContainerOverSubrelations, typename AllEntities, typename Parameters>
213 static void accumulate(ContainerOverSubrelations& accumulator,
214 const AllEntities& in,
215 const Parameters& params,
216 const FF& scaling_factor)
217 {
220 accumulator, in, params, scaling_factor);
221 }
222};
223
224template <typename Settings, typename FF>
226
227template <typename Settings, typename FF> using GenericPermutation = GenericPermutationRelationImpl<Settings, FF>;
228
229} // namespace bb
static constexpr std::array< bool, 2 > SUBRELATION_LINEARLY_INDEPENDENT
We apply the power polynomial only to the first subrelation.
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_read_term_predicate(const AllEntities &in)
Compute if the value from the first set exists in this row.
static Accumulator compute_write_term_predicate(const AllEntities &in)
Compute if the value from the second set exists in this row.
static Accumulator compute_inverse_exists(const AllEntities &in)
Get selector/wire switching on(1) or off(0) inverse computation We turn it on if either of the permut...
static Accumulator compute_read_term(const AllEntities &in, const Parameters &params)
Compute the value of a single item in the set.
static void accumulate(ContainerOverSubrelations &accumulator, const AllEntities &in, const Parameters &params, const FF &scaling_factor)
Expression for generic log-derivative-based set permutation.
static constexpr std::array< size_t, 2 > SUBRELATION_PARTIAL_LENGTHS
static auto & get_inverse_polynomial(AllEntities &in)
Get the inverse permutation polynomial (needed to compute its value)
static Accumulator compute_write_term(const AllEntities &in, const Parameters &params)
Compute the value of a single item in the set.
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.
GenericPermutationSettingIndices
Specifies positions of elements in the tuple of entities received from methods in the Settings class.
@ PERMUTATION_SETS_START_POLYNOMIAL_INDEX
@ FIRST_PERMUTATION_SET_ENABLE_POLYNOMIAL_INDEX
@ ENABLE_INVERSE_CORRECTNESS_CHECK_POLYNOMIAL_INDEX
@ SECOND_PERMUTATION_SET_ENABLE_POLYNOMIAL_INDEX
void accumulate_logderivative_permutation_subrelation_contributions(ContainerOverSubrelations &accumulator, const AllEntities &in, const Parameters &params, const FF &scaling_factor)
Compute generic log-derivative set permutation subrelation accumulation.
constexpr void constexpr_for(F &&f)
Implements a loop using a compile-time iterator. Requires c++20. Implementation (and description) fro...
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13