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: 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
10
16namespace bb {
17
18template <class Flavor>
20 typename Flavor::ProverPolynomials& polynomials,
21 ActiveRegionData& active_region_data)
22{
23
24 PROFILE_THIS_NAME("trace populate");
25
26 auto copy_cycles = populate_wires_and_selectors_and_compute_copy_cycles(builder, polynomials, active_region_data);
27
28 if constexpr (IsMegaFlavor<Flavor>) {
29 PROFILE_THIS_NAME("add_ecc_op_wires_to_proving_key");
30
31 add_ecc_op_wires_to_proving_key(builder, polynomials);
32 }
33
34 // Compute the permutation argument polynomials (sigma/id) and add them to proving key
35 {
36 PROFILE_THIS_NAME("compute_permutation_argument_polynomials");
37
38 compute_permutation_argument_polynomials<Flavor>(builder, polynomials, copy_cycles, active_region_data);
39 }
40}
41
42template <class Flavor>
44 Builder& builder, ProverPolynomials& polynomials, ActiveRegionData& active_region_data)
45{
46
47 PROFILE_THIS_NAME("construct_trace_data");
48
50 copy_cycles.resize(builder.get_num_variables()); // at most one copy cycle per variable
51
52 RefArray<Polynomial, NUM_WIRES> wires = polynomials.get_wires();
53 auto selectors = polynomials.get_selectors();
54
55 // For each block in the trace, populate wire polys, copy cycles and selector polys
56 for (auto& block : builder.blocks.get()) {
57 const uint32_t offset = block.trace_offset();
58 const uint32_t block_size = static_cast<uint32_t>(block.size());
59
60 // Save ranges over which the blocks are "active" for use in structured commitments
61 if (block.size() > 0) {
62 active_region_data.add_range(offset, offset + block.size());
63 }
64
65 // Update wire polynomials and copy cycles
66 // NB: The order of row/column loops is arbitrary but needs to be row/column to match old copy_cycle code
67 {
68 PROFILE_THIS_NAME("populating wires and copy_cycles");
69
70 for (uint32_t block_row_idx = 0; block_row_idx < block_size; ++block_row_idx) {
71 for (uint32_t wire_idx = 0; wire_idx < NUM_WIRES; ++wire_idx) {
72 uint32_t var_idx = block.wires[wire_idx][block_row_idx]; // an index into the variables array
73 uint32_t real_var_idx = builder.real_variable_index[var_idx];
74 uint32_t trace_row_idx = block_row_idx + offset;
75 // Insert the real witness values from this block into the wire polys at the correct offset
76 wires[wire_idx].at(trace_row_idx) = builder.get_variable(var_idx);
77 // Add the address of the witness value to its corresponding copy cycle
78 copy_cycles[real_var_idx].emplace_back(cycle_node{ wire_idx, trace_row_idx });
79 }
80 }
81 }
82
83 RefVector<Selector<FF>> block_selectors = block.get_selectors();
84 // Insert the selector values for this block into the selector polynomials at the correct offset
85 // TODO(https://github.com/AztecProtocol/barretenberg/issues/398): implicit arithmetization/flavor consistency
86 for (size_t selector_idx = 0; selector_idx < block_selectors.size(); selector_idx++) {
87 auto& selector = block_selectors[selector_idx];
88 for (size_t row_idx = 0; row_idx < block_size; ++row_idx) {
89 size_t trace_row_idx = row_idx + offset;
90 selectors[selector_idx].set_if_valid_index(trace_row_idx, selector[row_idx]);
91 }
92 }
93 }
94
95 return copy_cycles;
96}
97
98template <class Flavor>
100 requires IsMegaFlavor<Flavor>
101{
102 auto& ecc_op_selector = polynomials.lagrange_ecc_op;
103 const size_t wire_idx_offset = Flavor::has_zero_row ? 1 : 0;
104
105 // Copy the ecc op data from the conventional wires into the op wires over the range of ecc op gates. The data is
106 // stored in the ecc op wires starting from index 0, whereas the wires contain the data offset by a zero row.
107 const size_t num_ecc_ops = builder.blocks.ecc_op.size();
108 for (auto [ecc_op_wire, wire] : zip_view(polynomials.get_ecc_op_wires(), polynomials.get_wires())) {
109 for (size_t i = 0; i < num_ecc_ops; ++i) {
110 ecc_op_wire.at(i) = wire[i + wire_idx_offset];
111 ecc_op_selector.at(i) = 1; // construct selector as the indicator on the ecc op block
112 }
113 }
114}
115
119#ifdef STARKNET_GARAGA_FLAVORS
122#endif
125template class TraceToPolynomials<MegaFlavor>;
127
128} // namespace bb
A container for the prover polynomials handles.
static constexpr bool has_zero_row
A template class for a reference array. Behaves as if std::array<T&, N> was possible.
Definition ref_array.hpp:22
A template class for a reference vector. Behaves as if std::vector<T&> was possible.
std::size_t size() const
typename Flavor::CircuitBuilder Builder
static void add_ecc_op_wires_to_proving_key(Builder &builder, ProverPolynomials &)
Construct and add the goblin ecc op wires to the proving key.
static void populate(Builder &builder, ProverPolynomials &, ActiveRegionData &)
Given a circuit, populate a proving key with wire polys, selector polys, and sigma/id polys.
static std::vector< CyclicPermutation > populate_wires_and_selectors_and_compute_copy_cycles(Builder &builder, ProverPolynomials &, ActiveRegionData &)
Populate wire polynomials, selector polynomials and copy cycles from raw circuit data.
typename Flavor::ProverPolynomials ProverPolynomials
AluTraceBuilder builder
Definition alu.test.cpp:123
ssize_t offset
Definition engine.cpp:36
Entry point for Barretenberg command-line interface.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
#define PROFILE_THIS_NAME(name)
Definition op_count.hpp:16
void add_range(const size_t start, const size_t end)
Definition flavor.hpp:104
cycle_node represents the idx of a value of the circuit. It will belong to a CyclicPermutation,...