Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
utils.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
7#pragma once
8
9#include <tuple>
10#include <type_traits>
11#include <utility>
12
19
20namespace bb {
21
22template <typename Flavor> class RelationUtils {
23 public:
24 using FF = typename Flavor::FF;
25 using Relations = typename Flavor::Relations;
27 using RelationEvaluations = decltype(create_tuple_of_arrays_of_values<typename Flavor::Relations>());
29
30 static constexpr size_t NUM_RELATIONS = Flavor::NUM_RELATIONS;
31 static constexpr size_t NUM_SUBRELATIONS = Flavor::NUM_SUBRELATIONS;
32
43 template <class Operation> static void apply_to_tuple_of_tuples(auto& tuple, Operation&& operation)
44 {
45 constexpr size_t outer_tuple_size = std::tuple_size_v<std::decay_t<decltype(tuple)>>;
46 constexpr_for<0, outer_tuple_size, 1>([&]<size_t outer_idx>() {
47 auto& inner_tuple = std::get<outer_idx>(tuple);
48 constexpr size_t inner_tuple_size = std::tuple_size_v<std::decay_t<decltype(inner_tuple)>>;
49 constexpr_for<0, inner_tuple_size, 1>([&]<size_t inner_idx>() {
50 std::forward<Operation>(operation).template operator()<outer_idx, inner_idx>(
51 std::get<inner_idx>(inner_tuple));
52 });
53 });
54 }
55
61 static void zero_univariates(auto& tuple)
62 {
63 auto set_to_zero = [](auto&&... elements) {
64 (std::fill(elements.evaluations.begin(), elements.evaluations.end(), FF(0)), ...);
65 };
66 flat_tuple::apply([&](auto&&... args) { (flat_tuple::apply(set_to_zero, args), ...); }, tuple);
67 }
68
76 static void scale_univariates(auto& tuple, const SubrelationSeparators& subrelation_separators)
77 {
78 size_t idx = 0;
79 auto scale_by_challenges = [&]<size_t outer_idx, size_t inner_idx>(auto& element) {
80 // Don't need to scale first univariate
81 if constexpr (!(outer_idx == 0 && inner_idx == 0)) {
82 element *= subrelation_separators[idx++];
83 }
84 };
85 apply_to_tuple_of_tuples(tuple, scale_by_challenges);
86 }
87
97 template <typename Tuple> static constexpr void add_tuples(Tuple& tuple_1, const Tuple& tuple_2)
98 {
99 auto add_tuples_helper = [&]<std::size_t... I>(std::index_sequence<I...>) {
100 ((std::get<I>(tuple_1) += std::get<I>(tuple_2)), ...);
101 };
102
104 }
105
117 template <typename Tuple> static constexpr void add_nested_tuples(Tuple& tuple_1, const Tuple& tuple_2)
118 {
119 constexpr_for<0, std::tuple_size_v<Tuple>, 1>(
120 [&]<size_t Index>() { add_tuples(std::get<Index>(tuple_1), std::get<Index>(tuple_2)); });
121 }
122
132 template <typename Parameters>
134 RelationEvaluations& relation_evaluations,
135 const Parameters& relation_parameters,
136 const FF& partial_evaluation_result)
137 {
138 constexpr_for<0, NUM_RELATIONS, 1>([&]<size_t rel_index>() {
139 // FIXME: You wan't /*consider_skipping=*/false here, but tests need to be fixed.
140 accumulate_single_relation<Parameters, rel_index, /*consider_skipping=*/false>(
141 evaluations, relation_evaluations, relation_parameters, partial_evaluation_result);
142 });
143 }
144
158 template <typename Parameters>
160 const Parameters& relation_parameters,
161 const FF& scaling_factor)
162 {
163 RelationEvaluations result{};
164 constexpr_for<0, NUM_RELATIONS, 1>([&]<size_t rel_index>() {
165 accumulate_single_relation<Parameters, rel_index>(evaluations, result, relation_parameters, scaling_factor);
166 });
167 return result;
168 }
169
170 template <typename Parameters, size_t relation_idx, bool consider_skipping = true>
171 inline static void accumulate_single_relation(const PolynomialEvaluations& evaluations,
172 RelationEvaluations& relation_evaluations,
173 const Parameters& relation_parameters,
174 const FF& scaling_factor)
175 {
177
178 // Check if the relation is skippable to speed up accumulation
179 if constexpr (!consider_skipping || !isSkippable<Relation, decltype(evaluations)> ||
181 // If not, accumulate normally
182 Relation::accumulate(
183 std::get<relation_idx>(relation_evaluations), evaluations, relation_parameters, scaling_factor);
184 } else {
185 // If so, only compute the contribution if the relation is active
186 if (!Relation::skip(evaluations)) {
187 Relation::accumulate(
188 std::get<relation_idx>(relation_evaluations), evaluations, relation_parameters, scaling_factor);
189 }
190 }
191 }
192
202 static void zero_elements(auto& tuple)
203 {
204 auto set_to_zero = [](auto& element) { std::fill(element.begin(), element.end(), FF(0)); };
205 apply_to_tuple_of_arrays(set_to_zero, tuple);
206 };
207
214 static FF scale_and_batch_elements(auto& tuple, const SubrelationSeparators& subrelation_separators)
215 {
216 // Initialize result with the contribution from the first subrelation
217 FF result = std::get<0>(tuple)[0];
218
219 size_t idx = 0;
220
221 auto scale_by_challenges_and_accumulate = [&]<size_t outer_idx, size_t inner_idx>(auto& element) {
222 if constexpr (!(outer_idx == 0 && inner_idx == 0)) {
223 // Accumulate scaled subrelation contribution
224 result += element * subrelation_separators[idx++];
225 }
226 };
227 apply_to_tuple_of_arrays_elements(scale_by_challenges_and_accumulate, tuple);
228 return result;
229 }
230
237 template <typename Operation> static void apply_to_tuple_of_arrays(Operation&& operation, auto& tuple)
238 {
240 [&operation](auto&... elements_ref) {
241 // The comma operator ensures sequential application of the operation to each element.
242 // (void) cast is used to discard the result of std::invoke if it's not void,
243 // to prevent issues with overloaded comma operators.
244 ((void)std::invoke(std::forward<Operation>(operation), elements_ref), ...);
245 },
246 tuple);
247 }
248
257 template <typename Operation, typename tuple_type>
258 static void apply_to_tuple_of_arrays_elements(Operation&& operation, const tuple_type& tuple)
259 {
260 // Iterate over each array in the outer tuple.
261 // OuterIdx is the compile-time index of the current array in the tuple.
262 constexpr_for<0, std::tuple_size_v<tuple_type>, 1>([&]<size_t OuterIdx>() {
263 // Get a const reference to the current array from the tuple.
264 const auto& current_array = std::get<OuterIdx>(tuple);
265 constexpr size_t num_elements_in_current_array = std::tuple_size_v<std::decay_t<decltype(current_array)>>;
266
267 // Iterate over each element within the current_array.
268 // InnerIdx is the compile-time index of the element within this specific array.
269 constexpr_for<0, num_elements_in_current_array, 1>([&]<size_t InnerIdx>() {
270 // Invoke the operation.
271 // The operation is called with OuterIdx (array index in the tuple) and
272 // InnerIdx (element index in the current array) as template arguments.
273 // The current element (e.g., an FF value) is passed as an argument.
274 std::forward<Operation>(operation).template operator()<OuterIdx, InnerIdx>(current_array[InnerIdx]);
275 });
276 });
277 }
278};
279
280} // namespace bb
A field element for each entity of the flavor. These entities represent the prover polynomials evalua...
Curve::ScalarField FF
std::array< FF, NUM_SUBRELATIONS - 1 > SubrelationSeparators
static constexpr size_t NUM_SUBRELATIONS
static constexpr size_t NUM_RELATIONS
Relations_< FF > Relations
A wrapper for Relations to expose methods used by the Sumcheck prover or verifier to add the contribu...
static constexpr size_t NUM_RELATIONS
Definition utils.hpp:30
typename Flavor::SubrelationSeparators SubrelationSeparators
Definition utils.hpp:28
static void apply_to_tuple_of_arrays_elements(Operation &&operation, const tuple_type &tuple)
Recursive template function to apply a specific operation on each element of several arrays in a tupl...
Definition utils.hpp:258
typename Flavor::Relations Relations
Definition utils.hpp:25
static void scale_univariates(auto &tuple, const SubrelationSeparators &subrelation_separators)
Scale Univariates, each representing a subrelation, by different challenges.
Definition utils.hpp:76
static void zero_elements(auto &tuple)
Set each element in a tuple of arrays to zero.
Definition utils.hpp:202
static void zero_univariates(auto &tuple)
Set all coefficients of Univariates to zero.
Definition utils.hpp:61
static constexpr void add_nested_tuples(Tuple &tuple_1, const Tuple &tuple_2)
Componentwise addition of nested tuples (tuples of tuples)
Definition utils.hpp:117
typename Flavor::FF FF
Definition utils.hpp:24
static constexpr void add_tuples(Tuple &tuple_1, const Tuple &tuple_2)
Componentwise addition of two tuples.
Definition utils.hpp:97
static RelationEvaluations accumulate_relation_evaluations(const PolynomialEvaluations &evaluations, const Parameters &relation_parameters, const FF &scaling_factor)
Calculate the contribution of each relation to the expected value of the full Honk relation.
Definition utils.hpp:159
static void apply_to_tuple_of_tuples(auto &tuple, Operation &&operation)
General purpose method for applying an operation to a tuple of tuples of Univariates.
Definition utils.hpp:43
static void accumulate_single_relation(const PolynomialEvaluations &evaluations, RelationEvaluations &relation_evaluations, const Parameters &relation_parameters, const FF &scaling_factor)
Definition utils.hpp:171
static FF scale_and_batch_elements(auto &tuple, const SubrelationSeparators &subrelation_separators)
Scale elements, representing evaluations of subrelations, by separate challenges then sum them.
Definition utils.hpp:214
decltype(create_tuple_of_arrays_of_values< typename Flavor::Relations >()) RelationEvaluations
Definition utils.hpp:27
static void accumulate_relation_evaluations_without_skipping(const PolynomialEvaluations &evaluations, RelationEvaluations &relation_evaluations, const Parameters &relation_parameters, const FF &partial_evaluation_result)
Calculate the contribution of each relation to the expected value of the full Honk relation.
Definition utils.hpp:133
static constexpr size_t NUM_SUBRELATIONS
Definition utils.hpp:31
typename Flavor::AllValues PolynomialEvaluations
Definition utils.hpp:26
static void apply_to_tuple_of_arrays(Operation &&operation, auto &tuple)
General purpose method for applying a tuple of arrays (of FFs)
Definition utils.hpp:237
The templates defined herein facilitate sharing the relation arithmetic between the prover and the ve...
Base class templates for structures that contain data parameterized by the fundamental polynomials of...
Entry point for Barretenberg command-line interface.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
TUPLET_INLINE constexpr decltype(auto) apply(F &&func, Tup &&tup)
Definition tuplet.hpp:1032