Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
permutation_lib.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
21
23
24#include <algorithm>
25#include <cstddef>
26#include <cstdint>
27#include <initializer_list>
28#include <string>
29#include <utility>
30#include <vector>
31
32namespace bb {
33
40struct cycle_node {
41 uint32_t wire_idx;
42 uint32_t gate_idx;
43};
44
49struct Mapping {
50 std::shared_ptr<uint32_t[]> row_idx; // row idx of next entry in copy cycle
51 std::shared_ptr<uint8_t[]> col_idx; // column idx of next entry in copy cycle
52 std::shared_ptr<bool[]> is_public_input; // if we are a sigma polynomial, is the current row a public input row?
53 // (always false for id polynomials.)
54 std::shared_ptr<bool[]>
55 is_tag; // is this element a tag, (N.B. For each permutation polynomial (i.e., id_i or
56 // sigma_j), only one element per cycle is a tag. This follows the generalized permutation argument.)
57 size_t _size = 0;
58
59 Mapping() = default;
60
61 size_t size() const { return _size; }
62
63 Mapping(size_t n)
64 : row_idx(_allocate_aligned_memory<uint32_t>(n))
68 , _size(n)
69 {}
70};
71
72template <size_t NUM_WIRES> struct PermutationMapping {
75
80 PermutationMapping(size_t circuit_size)
81 {
82 BB_BENCH_NAME("PermutationMapping constructor");
83
84 for (size_t wire_idx = 0; wire_idx < NUM_WIRES; ++wire_idx) {
85 sigmas[wire_idx] = Mapping(circuit_size);
86 ids[wire_idx] = Mapping(circuit_size);
87 }
88
89 parallel_for([&](const ThreadChunk& chunk) {
90 // Initialize every element to point to itself
91 for (uint8_t col_idx = 0; col_idx < NUM_WIRES; ++col_idx) {
92 for (size_t i : chunk.range(circuit_size)) {
93 auto row_idx = static_cast<uint32_t>(i);
94 auto idx = static_cast<ptrdiff_t>(row_idx);
95 // sigma polynomials
96 sigmas[col_idx].row_idx[idx] = row_idx;
97 sigmas[col_idx].col_idx[idx] = col_idx;
98 sigmas[col_idx].is_public_input[idx] = false;
99 sigmas[col_idx].is_tag[idx] = false;
100 // id polynomials
101 ids[col_idx].row_idx[idx] = row_idx;
102 ids[col_idx].col_idx[idx] = col_idx;
103 ids[col_idx].is_public_input[idx] = false; // always false.
104 ids[col_idx].is_tag[idx] = false;
105 }
106 }
107 });
108 }
109};
110
112
113namespace {
114
115constexpr size_t PERMUTATION_POLY_START_INDEX =
116 1; // start_index of the Sigma and ID polynomials, which are shiftable. (Note that they are never shifted.)
117
131template <typename Flavor>
132PermutationMapping<Flavor::NUM_WIRES> compute_permutation_mapping(
133 const typename Flavor::CircuitBuilder& circuit_constructor,
134 const size_t dyadic_size,
135 const std::vector<CyclicPermutation>& wire_copy_cycles)
136{
137
138 // Initialize the table of permutations so that every element points to itself
139 PermutationMapping<Flavor::NUM_WIRES> mapping(dyadic_size);
140
141 // Represents the idx of a variable in circuit_constructor.variables
142 std::span<const uint32_t> real_variable_tags = circuit_constructor.real_variable_tags;
143
144 // Go through each cycle
145 for (size_t cycle_idx = 0; cycle_idx < wire_copy_cycles.size(); ++cycle_idx) {
146 // We go through the cycle and fill-out/modify `mapping`. Following the generalized permutation algorithm, we
147 // take separate care of first/last node handling.
148 const CyclicPermutation& cycle = wire_copy_cycles[cycle_idx];
149 const auto cycle_size = cycle.size();
150 if (cycle_size == 0) {
151 continue;
152 }
153
154 const cycle_node& first_node = cycle[0];
155 const cycle_node& last_node = cycle[cycle_size - 1];
156
157 const auto first_row = static_cast<ptrdiff_t>(first_node.gate_idx);
158 const auto first_col = first_node.wire_idx;
159 const auto last_row = static_cast<ptrdiff_t>(last_node.gate_idx);
160 const auto last_col = last_node.wire_idx;
161
162 // First node: id gets tagged with the cycle's variable tag
163 mapping.ids[first_col].is_tag[first_row] = true;
164 mapping.ids[first_col].row_idx[first_row] = real_variable_tags[cycle_idx];
165
166 // Last node: sigma gets tagged and points to tau(tag) instead of wrapping to first node
167 mapping.sigmas[last_col].is_tag[last_row] = true;
168 mapping.sigmas[last_col].row_idx[last_row] = circuit_constructor.tau().at(real_variable_tags[cycle_idx]);
169
170 // All nodes except the last: sigma points to the next node in the cycle
171 for (size_t node_idx = 0; node_idx + 1 < cycle_size; ++node_idx) {
172 const cycle_node& current_node = cycle[node_idx];
173 const cycle_node& next_node = cycle[node_idx + 1];
174
175 const auto current_row = static_cast<ptrdiff_t>(current_node.gate_idx);
176 const auto current_col = current_node.wire_idx;
177 // Point current node to next node.
178 mapping.sigmas[current_col].row_idx[current_row] = next_node.gate_idx;
179 mapping.sigmas[current_col].col_idx[current_row] = static_cast<uint8_t>(next_node.wire_idx);
180 }
181 }
182
183 // Add information about public inputs so that the cycles can be altered later; See the construction of the
184 // permutation polynomials for details. This _only_ effects sigma_0, the 0th sigma polynomial, as the structure of
185 // the algorithm only requires modifying sigma_0(i) where i is a public input row. (Note that at such a row, the
186 // non-zero wire values are in w_l and w_r, and both of them contain the public input.)
187 const auto num_public_inputs = static_cast<uint32_t>(circuit_constructor.num_public_inputs());
188
189 auto pub_inputs_offset = circuit_constructor.blocks.pub_inputs.trace_offset();
190 for (size_t i = 0; i < num_public_inputs; ++i) {
191 uint32_t idx = static_cast<uint32_t>(i + pub_inputs_offset);
192 mapping.sigmas[0].row_idx[static_cast<ptrdiff_t>(idx)] = idx;
193 mapping.sigmas[0].col_idx[static_cast<ptrdiff_t>(idx)] = 0;
194 mapping.sigmas[0].is_public_input[static_cast<ptrdiff_t>(idx)] = true;
195 if (mapping.sigmas[0].is_tag[static_cast<ptrdiff_t>(idx)]) {
196 std::cerr << "MAPPING IS BOTH A TAG AND A PUBLIC INPUT\n";
197 }
198 }
199 return mapping;
200}
201
211template <typename Flavor>
212void compute_honk_style_permutation_lagrange_polynomials_from_mapping(
213 const RefSpan<typename Flavor::Polynomial>& permutation_polynomials,
214 const std::array<Mapping, Flavor::NUM_WIRES>& permutation_mappings)
215{
216 using FF = typename Flavor::FF;
217
218 size_t domain_size = permutation_polynomials[0].size();
219
220 // SEPARATOR ensures that the evaluations of `id_i` (`sigma_i`) and `id_j`(`sigma_j`) polynomials on the boolean
221 // hypercube do not intersect for i != j.
222 const size_t SEPARATOR = PERMUTATION_ARGUMENT_VALUE_SEPARATOR;
223 BB_ASSERT_LT(permutation_polynomials[0].size(), SEPARATOR);
224
225 const MultithreadData thread_data = calculate_thread_data(domain_size);
226
227 size_t wire_idx = 0;
228 for (auto& current_permutation_poly : permutation_polynomials) {
229 parallel_for(thread_data.num_threads, [&](size_t j) {
230 const size_t start = thread_data.start[j];
231 const size_t end = thread_data.end[j];
232 for (size_t i = start; i < end; ++i) {
233 const size_t poly_idx =
234 i +
235 PERMUTATION_POLY_START_INDEX; // Permutation polynomials (sigma and ID) are shiftable, hence
236 // allocated starting at index `PERMUTATION_POLY_START_INDEX == 1`.
237 const auto idx = static_cast<ptrdiff_t>(poly_idx);
238 const auto& current_row_idx = permutation_mappings[wire_idx].row_idx[idx];
239 const auto& current_col_idx = permutation_mappings[wire_idx].col_idx[idx];
240 const auto& current_is_tag = permutation_mappings[wire_idx].is_tag[idx];
241 const auto& current_is_public_input =
242 permutation_mappings[wire_idx].is_public_input[idx]; // this is only `true` for sigma polynomials,
243 // it is always false for the ID polynomials.
244 if (current_is_public_input) {
245 // We intentionally want to break the cycles of the public input variables as an optimization.
246 // During the witness generation, both the left and right wire polynomials (w_l and w_r
247 // respectively) at row idx i contain the i-th public input. Let n = SEPARATOR. The initial
248 // CyclicPermutation created for these variables copy-constrained to the ith public input therefore
249 // always starts with (i) -> (n+i), followed by the indices of the variables in the "real" gates
250 // (i.e., the gates not merely present to set-up inputs).
251 //
252 // We change this and make i point to -(i+1). This choice "unbalances" the grand product argument,
253 // so that the final result of the grand product is _not_ 1. These indices are chosen so they can
254 // easily be computed by the verifier (just knowing the public inputs), and this algorithm
255 // constitutes a specification of the "permutation argument with public inputs" optimization due to
256 // Gabizon and Williamson. The verifier can expect the final product to be equal to the "public
257 // input delta" that is computed in <honk/library/grand_product_delta.hpp>.
258 current_permutation_poly.at(poly_idx) = -FF(current_row_idx + 1 + SEPARATOR * current_col_idx);
259 } else if (current_is_tag) {
260 // Set evaluations to (arbitrary) values disjoint from non-tag values. This is for the
261 // multiset-equality part of the generalized permutation argument, which requires auxiliary values
262 // which have not been used as indices. In particular, these are the actual tags assigned to the
263 // cycle.
264 current_permutation_poly.at(poly_idx) = SEPARATOR * Flavor::NUM_WIRES + current_row_idx;
265 } else {
266 // For the regular permutation we simply point to the next location by setting the
267 // evaluation to its idx
268 current_permutation_poly.at(poly_idx) = FF(current_row_idx + SEPARATOR * current_col_idx);
269 }
270 }
271 });
272 wire_idx++;
273 }
274}
275} // namespace
276
281template <typename Flavor>
283 typename Flavor::ProverPolynomials& polynomials,
284 const std::vector<CyclicPermutation>& copy_cycles)
285{
286 const size_t polynomial_size = polynomials.get_polynomial_size();
287 auto mapping = compute_permutation_mapping<Flavor>(circuit, polynomial_size, copy_cycles);
288
289 // Compute Honk-style sigma and ID polynomials from the corresponding mappings
290 {
291 BB_BENCH_NAME("compute_honk_style_permutation_lagrange_polynomials_from_mapping");
292 compute_honk_style_permutation_lagrange_polynomials_from_mapping<Flavor>(polynomials.get_sigmas(),
293 mapping.sigmas);
294 }
295 {
296 BB_BENCH_NAME("compute_honk_style_permutation_lagrange_polynomials_from_mapping");
297 compute_honk_style_permutation_lagrange_polynomials_from_mapping<Flavor>(polynomials.get_ids(), mapping.ids);
298 }
299}
300
301} // namespace bb
#define BB_ASSERT_LT(left, right,...)
Definition assert.hpp:137
#define BB_BENCH_NAME(name)
Definition bb_bench.hpp:219
A container for the prover polynomials.
typename Curve::ScalarField FF
constexpr std::size_t size() const
Definition ref_span.hpp:58
Base class templates for structures that contain data parameterized by the fundamental polynomials of...
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
MultithreadData calculate_thread_data(size_t num_iterations, size_t min_iterations_per_thread)
Calculates number of threads and index bounds for each thread.
Definition thread.cpp:212
std::shared_ptr< Fr[]> _allocate_aligned_memory(size_t n_elements)
void compute_permutation_argument_polynomials(const typename Flavor::CircuitBuilder &circuit, typename Flavor::ProverPolynomials &polynomials, const std::vector< CyclicPermutation > &copy_cycles)
Compute Honk-style permutation sigma/id polynomials and add to prover_instance, where the copy_cycles...
constexpr uint32_t PERMUTATION_ARGUMENT_VALUE_SEPARATOR
Definition constants.hpp:9
std::vector< cycle_node > CyclicPermutation
void parallel_for(size_t num_iterations, const std::function< void(size_t)> &func)
Definition thread.cpp:111
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
Stores permutation mapping data for a single wire column.
std::shared_ptr< bool[]> is_public_input
Mapping(size_t n)
std::shared_ptr< uint32_t[]> row_idx
std::shared_ptr< bool[]> is_tag
size_t size() const
Mapping()=default
std::shared_ptr< uint8_t[]> col_idx
PermutationMapping(size_t circuit_size)
Construct a permutation mapping default initialized so every element is in a cycle by itself.
std::array< Mapping, NUM_WIRES > ids
std::array< Mapping, NUM_WIRES > sigmas
auto range(size_t size, size_t offset=0) const
Definition thread.hpp:161
cycle_node represents the idx of a value of the circuit. It will belong to a CyclicPermutation,...