barretenberg
Loading...
Searching...
No Matches
generic_permutation_relation.hpp
Go to the documentation of this file.
1
8#pragma once
9#include <array>
10#include <tuple>
11
12#include "barretenberg/common/constexpr_utils.hpp"
13#include "barretenberg/polynomials/polynomial.hpp"
14#include "barretenberg/polynomials/univariate.hpp"
15#include "barretenberg/relations/relation_types.hpp"
16
17namespace proof_system::honk::sumcheck {
23 INVERSE_POLYNOMIAL_INDEX, /* The index of the inverse polynomial*/
24 ENABLE_INVERSE_CORRECTNESS_CHECK_POLYNOMIAL_INDEX, /* The index of the polynomial enabling first subrelation*/
25 FIRST_PERMUTATION_SET_ENABLE_POLYNOMIAL_INDEX, /* The index of the polynomial that adds an element from the first
26 set to the sum*/
27 SECOND_PERMUTATION_SET_ENABLE_POLYNOMIAL_INDEX, /* The index of the polynomial that adds an element from the second
28 set to the sum*/
29
30 PERMUTATION_SETS_START_POLYNOMIAL_INDEX, /* The starting index of the polynomials that are used in the permutation
31 sets*/
32};
33
34template <typename Settings, typename FF_> class GenericPermutationRelationImpl {
35 public:
36 using FF = FF_;
37 // Read and write terms counts should stay set to 1 unless we want to permute several columns at once as accumulated
38 // sets (not as tuples).
39 static constexpr size_t READ_TERMS = 1;
40 static constexpr size_t WRITE_TERMS = 1;
41 // 1 + polynomial degree of this relation
42 static constexpr size_t LENGTH = READ_TERMS + WRITE_TERMS + 3; // 5
43
44 static constexpr std::array<size_t, 2> SUBRELATION_PARTIAL_LENGTHS{
45 LENGTH, // inverse polynomial correctness sub-relation
46 LENGTH // log-derived terms subrelation
47 };
48
56 static constexpr std::array<bool, 2> SUBRELATION_LINEARLY_INDEPENDENT = { true, false };
57
64 template <typename AllValues> static bool operation_exists_at_row(const AllValues& row)
65
66 {
67 return Settings::inverse_polynomial_is_computed_at_row(row);
68 }
69
74 template <typename AllEntities> static auto& get_inverse_polynomial(AllEntities& in)
75 {
76 // WIRE containing the inverse of the product of terms at this row. Used to reconstruct individual inversed
77 // terms
78 return std::get<INVERSE_POLYNOMIAL_INDEX>(Settings::get_nonconst_entities(in));
79 }
80
85 template <typename Accumulator, typename AllEntities>
86 static Accumulator compute_inverse_exists(const AllEntities& in)
87 {
88 using View = typename Accumulator::View;
89
90 // WIRE/SELECTOR enabling the permutation used in the sumcheck computation. This affects the first subrelation
91 return Accumulator(
92 View(std::get<ENABLE_INVERSE_CORRECTNESS_CHECK_POLYNOMIAL_INDEX>(Settings::get_const_entities(in))));
93 }
94
100 template <typename Accumulator, size_t read_index, typename AllEntities>
101 static Accumulator compute_read_term_predicate(const AllEntities& in)
102
103 {
104 static_assert(read_index < WRITE_TERMS);
105 using View = typename Accumulator::View;
106
107 // The selector/wire value that determines that an element from the first set needs to be included. Can be
108 // different from the wire used in the write part.
109 return Accumulator(
110 View(std::get<FIRST_PERMUTATION_SET_ENABLE_POLYNOMIAL_INDEX>(Settings::get_const_entities(in))));
111 }
112
118 template <typename Accumulator, size_t write_index, typename AllEntities>
119 static Accumulator compute_write_term_predicate(const AllEntities& in)
120 {
121 static_assert(write_index < WRITE_TERMS);
122 using View = typename Accumulator::View;
123
124 // The selector/wire value that determines that an element from the second set needs to be included. Can be
125 // different from the wire used in the read part.
126 return Accumulator(
127 View(std::get<SECOND_PERMUTATION_SET_ENABLE_POLYNOMIAL_INDEX>(Settings::get_const_entities(in))));
128 }
129
140 template <typename Accumulator, size_t read_index, typename AllEntities, typename Parameters>
141 static Accumulator compute_read_term(const AllEntities& in, const Parameters& params)
142 {
143 using View = typename Accumulator::View;
144
145 static_assert(read_index < READ_TERMS);
146
147 // Retrieve all polynomials used
148 const auto all_polynomials = Settings::get_const_entities(in);
149
150 auto result = Accumulator(0);
151
152 // Iterate over tuple and sum as a polynomial over beta
153 barretenberg::constexpr_for<PERMUTATION_SETS_START_POLYNOMIAL_INDEX,
154 PERMUTATION_SETS_START_POLYNOMIAL_INDEX + Settings::COLUMNS_PER_SET,
155 1>(
156 [&]<size_t i>() { result = result * params.beta + View(std::get<i>(all_polynomials)); });
157
158 const auto& gamma = params.gamma;
159 return result + gamma;
160 }
161
172 template <typename Accumulator, size_t write_index, typename AllEntities, typename Parameters>
173 static Accumulator compute_write_term(const AllEntities& in, const Parameters& params)
174 {
175 using View = typename Accumulator::View;
176
177 static_assert(write_index < WRITE_TERMS);
178
179 // Get all used entities
180 const auto& used_entities = Settings::get_const_entities(in);
181
182 auto result = Accumulator(0);
183 // Iterate over tuple and sum as a polynomial over beta
184 barretenberg::constexpr_for<PERMUTATION_SETS_START_POLYNOMIAL_INDEX + Settings::COLUMNS_PER_SET,
185 PERMUTATION_SETS_START_POLYNOMIAL_INDEX + 2 * Settings::COLUMNS_PER_SET,
186 1>(
187 [&]<size_t i>() { result = result * params.beta + View(std::get<i>(used_entities)); });
188
189 const auto& gamma = params.gamma;
190 return result + gamma;
191 }
192
200 template <typename ContainerOverSubrelations, typename AllEntities, typename Parameters>
201 static void accumulate(ContainerOverSubrelations& accumulator,
202 const AllEntities& in,
203 const Parameters& params,
204 const FF& scaling_factor);
205};
206
207template <typename Settings, typename FF>
208using GenericPermutationRelation = Relation<GenericPermutationRelationImpl<Settings, FF>>;
209
210} // namespace proof_system::honk::sumcheck
The templates defined herein facilitate sharing the relation arithmetic between the prover and the ve...
Definition: relation_types.hpp:121
Definition: generic_permutation_relation.hpp:34
static Accumulator compute_read_term(const AllEntities &in, const Parameters &params)
Compute the value of a single item in the set.
Definition: generic_permutation_relation.hpp:141
static auto & get_inverse_polynomial(AllEntities &in)
Get the inverse permutation polynomial (needed to compute its value)
Definition: generic_permutation_relation.hpp:74
static constexpr std::array< bool, 2 > SUBRELATION_LINEARLY_INDEPENDENT
We apply the power polynomial only to the first subrelation.
Definition: generic_permutation_relation.hpp:56
static Accumulator compute_read_term_predicate(const AllEntities &in)
Compute if the value from the first set exists in this row.
Definition: generic_permutation_relation.hpp:101
static Accumulator compute_write_term(const AllEntities &in, const Parameters &params)
Compute the value of a single item in the set.
Definition: generic_permutation_relation.hpp:173
static void accumulate(ContainerOverSubrelations &accumulator, const AllEntities &in, const Parameters &params, const FF &scaling_factor)
Expression for generic log-derivative-based set permutation.
Definition: generic_permutation_relation.cpp:18
static Accumulator compute_inverse_exists(const AllEntities &in)
Get selector/wire switching on(1) or off(0) inverse computation.
Definition: generic_permutation_relation.hpp:86
static bool operation_exists_at_row(const AllValues &row)
Check if we need to compute the inverse polynomial element value for this row.
Definition: generic_permutation_relation.hpp:64
static Accumulator compute_write_term_predicate(const AllEntities &in)
Compute if the value from the second set exists in this row.
Definition: generic_permutation_relation.hpp:119
GenericPermutationSettingIndices
Specifies positions of elements in the tuple of entities received from methods in the Settings class.
Definition: generic_permutation_relation.hpp:22
constexpr void constexpr_for(F &&f)
Implements a loop using a compile-time iterator. Requires c++20. Implementation (and description) fro...
Definition: constexpr_utils.hpp:65