Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
trace_to_polynomials.cpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: Completed, auditors: [Sergei], commit: }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
13
19namespace bb {
20
21template <class Flavor>
23{
24
25 BB_BENCH_NAME("trace populate");
26
27 auto copy_cycles = populate_wires_and_selectors_and_compute_copy_cycles(builder, polynomials);
28
29 if constexpr (IsMegaFlavor<Flavor>) {
30 BB_BENCH_NAME("add_ecc_op_wires_to_prover_instance");
31
32 add_ecc_op_wires_to_prover_instance(builder, polynomials);
33 }
34
35 // Compute the permutation argument polynomials (sigma/id) and add them to proving key
36 {
37 BB_BENCH_NAME("compute_permutation_argument_polynomials");
38
39 compute_permutation_argument_polynomials<Flavor>(builder, polynomials, copy_cycles);
40 }
41}
42
43template <class Flavor>
45 Builder& builder, ProverPolynomials& polynomials)
46{
47
48 BB_BENCH_NAME("construct_trace_data");
49
51 copy_cycles.resize(builder.get_num_variables()); // at most one copy cycle per variable
52
53 RefArray<Polynomial, NUM_WIRES> wires = polynomials.get_wires();
54 auto selectors = polynomials.get_selectors();
55
56 // Two-phase parallelisation. Phase 1 fans out over blocks to populate wires and emit copy-cycle
57 // nodes; phase 2 fans out over a flattened (block, selector) task list to fill selectors.
58 auto blocks_array = builder.blocks.get();
59 const size_t num_blocks = blocks_array.size();
60
61 // Phase 1: per-block parallel pass over wires and emit copy-cycle nodes.
63 {
64 BB_BENCH_NAME("populate_wires_and_emit_cycles");
65 parallel_for(num_blocks, [&](size_t block_idx) {
66 auto& block = blocks_array[block_idx];
67 const uint32_t offset = block.trace_offset();
68 const uint32_t block_size = static_cast<uint32_t>(block.size());
69 auto& local_nodes = per_block_nodes[block_idx];
70 local_nodes.reserve(static_cast<size_t>(block_size) * NUM_WIRES);
71
72 // NB: The order of row/column loops is arbitrary but needs to be row/column to match old copy_cycle code.
73 for (uint32_t block_row_idx = 0; block_row_idx < block_size; ++block_row_idx) {
74 for (uint32_t wire_idx = 0; wire_idx < NUM_WIRES; ++wire_idx) {
75 uint32_t var_idx = block.wires[wire_idx][block_row_idx]; // an index into the variables array
76 // Use .at() so out-of-range var_idx is caught instead of producing a silent OOB read.
77 uint32_t real_var_idx = builder.real_variable_index.at(var_idx);
78 uint32_t trace_row_idx = block_row_idx + offset;
79 // Insert the real witness values from this block into the wire polys at the correct offset
80 wires[wire_idx].at(trace_row_idx) = builder.get_variable(var_idx);
81 local_nodes.emplace_back(real_var_idx, cycle_node{ wire_idx, trace_row_idx });
82 }
83 }
84 });
85 }
86
87 // Phase 1.5: Serial concat in block order to preserve cycle-node ordering within each variable's cycle list.
88 {
89 BB_BENCH_NAME("fill_copy_cycles");
90 for (const auto& block_nodes : per_block_nodes) {
91 for (const auto& [real_var_idx, node] : block_nodes) {
92 copy_cycles.at(real_var_idx).emplace_back(node);
93 }
94 }
95 }
96
97 // Phase 2: parallel selector filling across a flattened (block_idx, selector_idx) task list.
98 {
99 BB_BENCH_NAME("populate_selectors");
101 for (size_t block_idx = 0; block_idx < num_blocks; ++block_idx) {
102 const size_t num_selectors = blocks_array[block_idx].get_selectors().size();
103 for (size_t selector_idx = 0; selector_idx < num_selectors; ++selector_idx) {
104 selector_tasks.emplace_back(block_idx, selector_idx);
105 }
106 }
107 parallel_for(selector_tasks.size(), [&](size_t task_idx) {
108 const auto [block_idx, selector_idx] = selector_tasks[task_idx];
109 auto& block = blocks_array[block_idx];
110 const size_t offset = block.trace_offset();
111 const size_t block_size = block.size();
112 RefVector<Selector<FF>> block_selectors = block.get_selectors();
113 auto& selector = block_selectors[selector_idx];
114 for (size_t row_idx = 0; row_idx < block_size; ++row_idx) {
115 selectors[selector_idx].set_if_valid_index(row_idx + offset, selector[row_idx]);
116 }
117 });
118 }
119
120 return copy_cycles;
121}
122
123template <class Flavor>
125 requires IsMegaFlavor<Flavor>
126{
127 auto& ecc_op_selector = polynomials.lagrange_ecc_op;
128
129 // The EccOpQueueRelation constrains ecc_op_wire[row] == w_shift[row] where lagrange_ecc_op == 1;
130 // equivalently, ecc_op_wire[row] == w[row + NUM_ZERO_ROWS], so we write ecc_op_wire starting at
131 // (ecc_op_block.trace_offset() - NUM_ZERO_ROWS).
132 const auto& ecc_op_block = builder.blocks.ecc_op;
133 const size_t wire_start = ecc_op_block.trace_offset();
134 BB_ASSERT_GTE(wire_start, NUM_ZERO_ROWS, "ecc_op block must start beyond the zero row");
135 const size_t op_wire_start = wire_start - NUM_ZERO_ROWS;
136 for (auto [ecc_op_wire, wire] : zip_view(polynomials.get_ecc_op_wires(), polynomials.get_wires())) {
137 for (size_t i = 0; i < ecc_op_block.size(); ++i) {
138 ecc_op_wire.at(op_wire_start + i) = wire[wire_start + i];
139 ecc_op_selector.at(op_wire_start + i) = 1;
140 }
141 }
142}
143
147#ifdef STARKNET_GARAGA_FLAVORS
150#endif
152template class TraceToPolynomials<MegaFlavor>;
155
156} // namespace bb
#define BB_ASSERT_GTE(left, right,...)
Definition assert.hpp:128
#define BB_BENCH_NAME(name)
Definition bb_bench.hpp:264
A container for the prover polynomials.
A template class for a reference array. Behaves as if std::array<T&, N> was possible.
Definition ref_array.hpp:22
constexpr std::size_t size() const
typename Flavor::CircuitBuilder Builder
static std::vector< CyclicPermutation > populate_wires_and_selectors_and_compute_copy_cycles(Builder &builder, ProverPolynomials &)
Populate wire polynomials, selector polynomials and copy cycles from raw circuit data.
static void populate(Builder &builder, ProverPolynomials &)
Given a circuit, populate a proving key with wire polys, selector polys, and sigma/id polys.
typename Flavor::ProverPolynomials ProverPolynomials
AluTraceBuilder builder
Definition alu.test.cpp:124
ssize_t offset
Definition engine.cpp:62
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
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
cycle_node represents the idx of a value of the circuit. It will belong to a CyclicPermutation,...