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: Complete, auditors: [Raju], commit: 21a7e3670e6 }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
14#pragma once
15
20
21#include <cstddef>
22#include <cstdint>
23#include <vector>
24
25namespace bb {
26
33struct cycle_node {
34 uint32_t wire_idx;
35 uint32_t gate_idx;
36};
37
42struct Mapping {
43 std::shared_ptr<uint32_t[]> row_idx; // row idx of next entry in copy cycle
44 std::shared_ptr<uint8_t[]> col_idx; // column idx of next entry in copy cycle
45 std::shared_ptr<bool[]> is_public_input; // if we are a sigma polynomial, is the current row a public input row?
46 // (always false for id polynomials.)
47 std::shared_ptr<bool[]>
48 is_tag; // is this element a tag, (N.B. For each permutation polynomial (i.e., id_i or
49 // sigma_j), only one element per cycle is a tag. This follows the generalized permutation argument.)
50 size_t _size = 0;
51
52 Mapping() = default;
53
54 size_t size() const { return _size; }
55
56 Mapping(size_t n)
57 : row_idx(_allocate_aligned_memory<uint32_t>(n))
61 , _size(n)
62 {}
63};
64
65template <size_t NUM_WIRES> struct PermutationMapping {
68
73 PermutationMapping(size_t circuit_size)
74 {
75 BB_BENCH_NAME("PermutationMapping constructor");
76
77 for (size_t wire_idx = 0; wire_idx < NUM_WIRES; ++wire_idx) {
78 sigmas[wire_idx] = Mapping(circuit_size);
79 ids[wire_idx] = Mapping(circuit_size);
80 }
81
82 parallel_for([&](const ThreadChunk& chunk) {
83 // Initialize every element to point to itself
84 for (uint8_t col_idx = 0; col_idx < NUM_WIRES; ++col_idx) {
85 for (size_t i : chunk.range(circuit_size)) {
86 auto row_idx = static_cast<uint32_t>(i);
87 auto idx = static_cast<ptrdiff_t>(row_idx);
88 // sigma polynomials
89 sigmas[col_idx].row_idx[idx] = row_idx;
90 sigmas[col_idx].col_idx[idx] = col_idx;
91 sigmas[col_idx].is_public_input[idx] = false;
92 sigmas[col_idx].is_tag[idx] = false;
93 // id polynomials
94 ids[col_idx].row_idx[idx] = row_idx;
95 ids[col_idx].col_idx[idx] = col_idx;
96 ids[col_idx].is_public_input[idx] = false; // always false.
97 ids[col_idx].is_tag[idx] = false;
98 }
99 }
100 });
101 }
102};
103
105
106namespace {
107
121template <typename Flavor>
122PermutationMapping<Flavor::NUM_WIRES> compute_permutation_mapping(
123 const typename Flavor::CircuitBuilder& circuit_constructor,
124 const size_t dyadic_size,
125 const std::vector<CyclicPermutation>& wire_copy_cycles)
126{
127
128 // Initialize the table of permutations so that every element points to itself
129 PermutationMapping<Flavor::NUM_WIRES> mapping(dyadic_size);
130
131 // Represents the idx of a variable in circuit_constructor.variables
132 std::span<const uint32_t> real_variable_tags = circuit_constructor.real_variable_tags;
133
134 // Go through each cycle
135 for (size_t cycle_idx = 0; cycle_idx < wire_copy_cycles.size(); ++cycle_idx) {
136 // We go through the cycle and fill-out/modify `mapping`. Following the generalized permutation algorithm, we
137 // take separate care of first/last node handling.
138 const CyclicPermutation& cycle = wire_copy_cycles[cycle_idx];
139 const auto cycle_size = cycle.size();
140 if (cycle_size == 0) {
141 continue;
142 }
143
144 const cycle_node& first_node = cycle[0];
145 const cycle_node& last_node = cycle[cycle_size - 1];
146
147 const auto first_row = static_cast<ptrdiff_t>(first_node.gate_idx);
148 const auto first_col = first_node.wire_idx;
149 const auto last_row = static_cast<ptrdiff_t>(last_node.gate_idx);
150 const auto last_col = last_node.wire_idx;
151
152 // First node: id gets tagged with the cycle's variable tag
153 mapping.ids[first_col].is_tag[first_row] = true;
154 mapping.ids[first_col].row_idx[first_row] = real_variable_tags[cycle_idx];
155
156 // Last node: sigma gets tagged and points to tau(tag) instead of wrapping to first node
157 mapping.sigmas[last_col].is_tag[last_row] = true;
158 mapping.sigmas[last_col].row_idx[last_row] = circuit_constructor.tau().at(real_variable_tags[cycle_idx]);
159
160 // All nodes except the last: sigma points to the next node in the cycle
161 for (size_t node_idx = 0; node_idx + 1 < cycle_size; ++node_idx) {
162 const cycle_node& current_node = cycle[node_idx];
163 const cycle_node& next_node = cycle[node_idx + 1];
164
165 const auto current_row = static_cast<ptrdiff_t>(current_node.gate_idx);
166 const auto current_col = current_node.wire_idx;
167 // Point current node to next node.
168 mapping.sigmas[current_col].row_idx[current_row] = next_node.gate_idx;
169 mapping.sigmas[current_col].col_idx[current_row] = static_cast<uint8_t>(next_node.wire_idx);
170 }
171 }
172
173 // Add information about public inputs so that the cycles can be altered later; See the construction of the
174 // permutation polynomials for details. This _only_ effects sigma_0, the 0th sigma polynomial, as the structure of
175 // the algorithm only requires modifying sigma_0(i) where i is a public input row. (Note that at such a row, the
176 // non-zero wire values are in w_l and w_r, and both of them contain the public input.)
177 const auto num_public_inputs = static_cast<uint32_t>(circuit_constructor.num_public_inputs());
178
179 auto pub_inputs_offset = circuit_constructor.blocks.pub_inputs.trace_offset();
180 for (size_t i = 0; i < num_public_inputs; ++i) {
181 uint32_t idx = static_cast<uint32_t>(i + pub_inputs_offset);
182 mapping.sigmas[0].row_idx[static_cast<ptrdiff_t>(idx)] = idx;
183 mapping.sigmas[0].col_idx[static_cast<ptrdiff_t>(idx)] = 0;
184 mapping.sigmas[0].is_public_input[static_cast<ptrdiff_t>(idx)] = true;
185 BB_ASSERT(!mapping.sigmas[0].is_tag[static_cast<ptrdiff_t>(idx)], "MAPPING IS BOTH A TAG AND A PUBLIC INPUT");
186 }
187 return mapping;
188}
189
199template <typename Flavor>
200void compute_honk_style_permutation_lagrange_polynomials_from_mapping(
201 const RefSpan<typename Flavor::Polynomial>& permutation_polynomials,
202 const std::array<Mapping, Flavor::NUM_WIRES>& permutation_mappings)
203{
204 using FF = typename Flavor::FF;
205
206 size_t domain_size = permutation_polynomials[0].size();
207
208 // SEPARATOR ensures that the evaluations of `id_i` (`sigma_i`) and `id_j`(`sigma_j`) polynomials on the boolean
209 // hypercube do not intersect for i != j.
210 const size_t SEPARATOR = PERMUTATION_ARGUMENT_VALUE_SEPARATOR;
211 BB_ASSERT_LT(permutation_polynomials[0].size(), SEPARATOR);
212
213 const MultithreadData thread_data = calculate_thread_data(domain_size);
214
215 size_t wire_idx = 0;
216 for (auto& current_permutation_poly : permutation_polynomials) {
217 parallel_for(thread_data.num_threads, [&](size_t j) {
218 const size_t start = thread_data.start[j];
219 const size_t end = thread_data.end[j];
220 for (size_t i = start; i < end; ++i) {
221 const size_t poly_idx = i + current_permutation_poly.start_index();
222 const auto idx = static_cast<ptrdiff_t>(poly_idx);
223 const auto& current_row_idx = permutation_mappings[wire_idx].row_idx[idx];
224 const auto& current_col_idx = permutation_mappings[wire_idx].col_idx[idx];
225 const auto& current_is_tag = permutation_mappings[wire_idx].is_tag[idx];
226 const auto& current_is_public_input =
227 permutation_mappings[wire_idx].is_public_input[idx]; // this is only `true` for sigma polynomials,
228 // it is always false for the ID polynomials.
229 if (current_is_public_input) {
230 // We intentionally want to break the cycles of the public input variables as an optimization.
231 // During the witness generation, both the left and right wire polynomials (w_l and w_r
232 // respectively) at row idx i contain the i-th public input. Let n = SEPARATOR. The initial
233 // CyclicPermutation created for these variables copy-constrained to the ith public input therefore
234 // always starts with (i) -> (n+i), followed by the indices of the variables in the "real" gates
235 // (i.e., the gates not merely present to set-up inputs).
236 //
237 // We change this and make i point to -(i+1). This choice "unbalances" the grand product argument,
238 // so that the final result of the grand product is _not_ 1. These indices are chosen so they can
239 // easily be computed by the verifier (just knowing the public inputs), and this algorithm
240 // constitutes a specification of the "permutation argument with public inputs" optimization due to
241 // Gabizon and Williamson. The verifier can expect the final product to be equal to the "public
242 // input delta" that is computed in <honk/library/grand_product_delta.hpp>.
243 current_permutation_poly.at(poly_idx) = -FF(current_row_idx + 1 + SEPARATOR * current_col_idx);
244 } else if (current_is_tag) {
245 // Set evaluations to (arbitrary) values disjoint from non-tag values. This is for the
246 // multiset-equality part of the generalized permutation argument, which requires auxiliary values
247 // which have not been used as indices. In particular, these are the actual tags assigned to the
248 // cycle.
249 current_permutation_poly.at(poly_idx) = SEPARATOR * Flavor::NUM_WIRES + current_row_idx;
250 } else {
251 // For the regular permutation we simply point to the next location by setting the
252 // evaluation to its idx
253 current_permutation_poly.at(poly_idx) = FF(current_row_idx + SEPARATOR * current_col_idx);
254 }
255 }
256 });
257 wire_idx++;
258 }
259}
260} // namespace
261
266template <typename Flavor>
268 typename Flavor::ProverPolynomials& polynomials,
269 const std::vector<CyclicPermutation>& copy_cycles)
270{
271 const size_t polynomial_size = polynomials.get_polynomial_size();
272 auto mapping = compute_permutation_mapping<Flavor>(circuit, polynomial_size, copy_cycles);
273
274 // Compute Honk-style sigma and ID polynomials from the corresponding mappings
275 {
276 BB_BENCH_NAME("compute_honk_style_permutation_lagrange_polynomials_from_mapping");
277 compute_honk_style_permutation_lagrange_polynomials_from_mapping<Flavor>(polynomials.get_sigmas(),
278 mapping.sigmas);
279 }
280 {
281 BB_BENCH_NAME("compute_honk_style_permutation_lagrange_polynomials_from_mapping");
282 compute_honk_style_permutation_lagrange_polynomials_from_mapping<Flavor>(polynomials.get_ids(), mapping.ids);
283 }
284}
285
286} // namespace bb
#define BB_ASSERT(expression,...)
Definition assert.hpp:70
#define BB_ASSERT_LT(left, right,...)
Definition assert.hpp:143
#define BB_BENCH_NAME(name)
Definition bb_bench.hpp:225
A container for the prover polynomials.
typename Curve::ScalarField FF
constexpr std::size_t size() const
Definition ref_span.hpp:58
Base class templates shared across Honk flavors.
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:208
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:152
cycle_node represents the idx of a value of the circuit. It will belong to a CyclicPermutation,...