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: Planned, auditors: [], commit: }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
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
128template <typename Flavor>
129PermutationMapping<Flavor::NUM_WIRES> compute_permutation_mapping(
130 const typename Flavor::CircuitBuilder& circuit_constructor,
131 const size_t dyadic_size,
132 const std::vector<CyclicPermutation>& wire_copy_cycles)
133{
134
135 // Initialize the table of permutations so that every element points to itself
136 PermutationMapping<Flavor::NUM_WIRES> mapping(dyadic_size);
137
138 // Represents the idx of a variable in circuit_constructor.variables
139 std::span<const uint32_t> real_variable_tags = circuit_constructor.real_variable_tags;
140
141 // Go through each cycle
142 for (size_t cycle_idx = 0; cycle_idx < wire_copy_cycles.size(); ++cycle_idx) {
143 // We go through the cycle and fill-out/modify `mapping`. Following the generalized permutation algorithm, we
144 // take separate care of first/last node handling.
145 const CyclicPermutation& cycle = wire_copy_cycles[cycle_idx];
146 const auto cycle_size = cycle.size();
147 if (cycle_size == 0) {
148 continue;
149 }
150
151 const cycle_node& first_node = cycle[0];
152 const cycle_node& last_node = cycle[cycle_size - 1];
153
154 const auto first_row = static_cast<ptrdiff_t>(first_node.gate_idx);
155 const auto first_col = first_node.wire_idx;
156 const auto last_row = static_cast<ptrdiff_t>(last_node.gate_idx);
157 const auto last_col = last_node.wire_idx;
158
159 // First node: id gets tagged with the cycle's variable tag
160 mapping.ids[first_col].is_tag[first_row] = true;
161 mapping.ids[first_col].row_idx[first_row] = real_variable_tags[cycle_idx];
162
163 // Last node: sigma gets tagged and points to tau(tag) instead of wrapping to first node
164 mapping.sigmas[last_col].is_tag[last_row] = true;
165 mapping.sigmas[last_col].row_idx[last_row] = circuit_constructor.tau().at(real_variable_tags[cycle_idx]);
166
167 // All nodes except the last: sigma points to the next node in the cycle
168 for (size_t node_idx = 0; node_idx + 1 < cycle_size; ++node_idx) {
169 const cycle_node& current_node = cycle[node_idx];
170 const cycle_node& next_node = cycle[node_idx + 1];
171
172 const auto current_row = static_cast<ptrdiff_t>(current_node.gate_idx);
173 const auto current_col = current_node.wire_idx;
174 // Point current node to next node.
175 mapping.sigmas[current_col].row_idx[current_row] = next_node.gate_idx;
176 mapping.sigmas[current_col].col_idx[current_row] = static_cast<uint8_t>(next_node.wire_idx);
177 }
178 }
179
180 // Add information about public inputs so that the cycles can be altered later; See the construction of the
181 // permutation polynomials for details. This _only_ effects sigma_0, the 0th sigma polynomial, as the structure of
182 // the algorithm only requires modifying sigma_0(i) where i is a public input row. (Note that at such a row, the
183 // non-zero wire values are in w_l and w_r, and both of them contain the public input.)
184 const auto num_public_inputs = static_cast<uint32_t>(circuit_constructor.num_public_inputs());
185
186 auto pub_inputs_offset = circuit_constructor.blocks.pub_inputs.trace_offset();
187 for (size_t i = 0; i < num_public_inputs; ++i) {
188 uint32_t idx = static_cast<uint32_t>(i + pub_inputs_offset);
189 mapping.sigmas[0].row_idx[static_cast<ptrdiff_t>(idx)] = idx;
190 mapping.sigmas[0].col_idx[static_cast<ptrdiff_t>(idx)] = 0;
191 mapping.sigmas[0].is_public_input[static_cast<ptrdiff_t>(idx)] = true;
192 if (mapping.sigmas[0].is_tag[static_cast<ptrdiff_t>(idx)]) {
193 std::cerr << "MAPPING IS BOTH A TAG AND A PUBLIC INPUT\n";
194 }
195 }
196 return mapping;
197}
198
208template <typename Flavor>
209void compute_honk_style_permutation_lagrange_polynomials_from_mapping(
210 const RefSpan<typename Flavor::Polynomial>& permutation_polynomials,
211 const std::array<Mapping, Flavor::NUM_WIRES>& permutation_mappings)
212{
213 using FF = typename Flavor::FF;
214
215 size_t domain_size = permutation_polynomials[0].size();
216
217 // SEPARATOR ensures that the evaluations of `id_i` (`sigma_i`) and `id_j`(`sigma_j`) polynomials on the boolean
218 // hypercube do not intersect for i != j.
219 const size_t SEPARATOR = PERMUTATION_ARGUMENT_VALUE_SEPARATOR;
220 BB_ASSERT_LT(permutation_polynomials[0].size(), SEPARATOR);
221
222 const MultithreadData thread_data = calculate_thread_data(domain_size);
223
224 size_t wire_idx = 0;
225 for (auto& current_permutation_poly : permutation_polynomials) {
226 parallel_for(thread_data.num_threads, [&](size_t j) {
227 const size_t start = thread_data.start[j];
228 const size_t end = thread_data.end[j];
229 for (size_t i = start; i < end; ++i) {
230 const size_t poly_idx = i + current_permutation_poly.start_index();
231 const auto idx = static_cast<ptrdiff_t>(poly_idx);
232 const auto& current_row_idx = permutation_mappings[wire_idx].row_idx[idx];
233 const auto& current_col_idx = permutation_mappings[wire_idx].col_idx[idx];
234 const auto& current_is_tag = permutation_mappings[wire_idx].is_tag[idx];
235 const auto& current_is_public_input =
236 permutation_mappings[wire_idx].is_public_input[idx]; // this is only `true` for sigma polynomials,
237 // it is always false for the ID polynomials.
238 if (current_is_public_input) {
239 // We intentionally want to break the cycles of the public input variables as an optimization.
240 // During the witness generation, both the left and right wire polynomials (w_l and w_r
241 // respectively) at row idx i contain the i-th public input. Let n = SEPARATOR. The initial
242 // CyclicPermutation created for these variables copy-constrained to the ith public input therefore
243 // always starts with (i) -> (n+i), followed by the indices of the variables in the "real" gates
244 // (i.e., the gates not merely present to set-up inputs).
245 //
246 // We change this and make i point to -(i+1). This choice "unbalances" the grand product argument,
247 // so that the final result of the grand product is _not_ 1. These indices are chosen so they can
248 // easily be computed by the verifier (just knowing the public inputs), and this algorithm
249 // constitutes a specification of the "permutation argument with public inputs" optimization due to
250 // Gabizon and Williamson. The verifier can expect the final product to be equal to the "public
251 // input delta" that is computed in <honk/library/grand_product_delta.hpp>.
252 current_permutation_poly.at(poly_idx) = -FF(current_row_idx + 1 + SEPARATOR * current_col_idx);
253 } else if (current_is_tag) {
254 // Set evaluations to (arbitrary) values disjoint from non-tag values. This is for the
255 // multiset-equality part of the generalized permutation argument, which requires auxiliary values
256 // which have not been used as indices. In particular, these are the actual tags assigned to the
257 // cycle.
258 current_permutation_poly.at(poly_idx) = SEPARATOR * Flavor::NUM_WIRES + current_row_idx;
259 } else {
260 // For the regular permutation we simply point to the next location by setting the
261 // evaluation to its idx
262 current_permutation_poly.at(poly_idx) = FF(current_row_idx + SEPARATOR * current_col_idx);
263 }
264 }
265 });
266 wire_idx++;
267 }
268}
269} // namespace
270
275template <typename Flavor>
277 typename Flavor::ProverPolynomials& polynomials,
278 const std::vector<CyclicPermutation>& copy_cycles)
279{
280 const size_t polynomial_size = polynomials.get_polynomial_size();
281 auto mapping = compute_permutation_mapping<Flavor>(circuit, polynomial_size, copy_cycles);
282
283 // Compute Honk-style sigma and ID polynomials from the corresponding mappings
284 {
285 BB_BENCH_NAME("compute_honk_style_permutation_lagrange_polynomials_from_mapping");
286 compute_honk_style_permutation_lagrange_polynomials_from_mapping<Flavor>(polynomials.get_sigmas(),
287 mapping.sigmas);
288 }
289 {
290 BB_BENCH_NAME("compute_honk_style_permutation_lagrange_polynomials_from_mapping");
291 compute_honk_style_permutation_lagrange_polynomials_from_mapping<Flavor>(polynomials.get_ids(), mapping.ids);
292 }
293}
294
295} // namespace bb
#define BB_ASSERT_LT(left, right,...)
Definition assert.hpp:143
#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,...