Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
polynomials.cpp
Go to the documentation of this file.
2
3#include <cstdint>
4
10
11namespace bb::avm2::constraining {
12
14{
16
17 // Polynomials that will be shifted need special care.
18 AVM_TRACK_TIME("proving/init_polys_to_be_shifted", ({
19 auto to_be_shifted = polys.get_to_be_shifted();
20 BB_ASSERT_EQ(to_be_shifted.size(),
22 "To be shifted columns array size mismatch");
23
24 // NOTE: we can't parallelize because Polynomial construction uses parallelism.
25 const size_t num_to_be_shifted = to_be_shifted.size();
26 for (size_t i = 0; i < num_to_be_shifted; i++) {
27 auto& poly = to_be_shifted[i];
28 // WARNING! Column-Polynomials order matters!
29 Column col = static_cast<Column>(TO_BE_SHIFTED_COLUMNS_ARRAY[i]);
30 uint32_t num_rows = trace.get_column_rows(col);
31 // Since we are shifting, we need to allocate one less row.
32 // The first row is always zero.
33 uint32_t allocated_size = num_rows > 0 ? num_rows - 1 : 0;
34
36 /*memory size*/ allocated_size,
37 /*largest possible index*/ MAX_AVM_TRACE_SIZE, // TODO(#16660): use real size?
38 /*make shiftable with offset*/ 1);
39 }
40 }));
41
42 // Catch-all with fully formed polynomials
43 // Note: derived polynomials (i.e., inverses) are not in the trace at this point, because they can only
44 // be computed after committing to the other witnesses. Therefore, they will be initialized as empty
45 // and they will be not set below. The derived polynomials will be reinitialized and set in the prover
46 // itself mid-proving.
47 AVM_TRACK_TIME("proving/init_polys_unshifted", ({
48 auto unshifted = polys.get_unshifted();
49 bb::parallel_for(unshifted.size(), [&](size_t i) {
50 auto& poly = unshifted[i];
51 // Some of the polynomials have been initialized above. Skip those.
52 if (poly.virtual_size() > 0) {
53 // Already initialized above.
54 return;
55 }
56
57 // WARNING! Column-Polynomials order matters!
58 Column col = static_cast<Column>(i);
59 const auto num_rows = trace.get_column_rows(col);
61 });
62 }));
63
64 AVM_TRACK_TIME("proving/set_polys_unshifted", ({
65 auto unshifted = polys.get_unshifted();
66 bb::parallel_for(unshifted.size(), [&](size_t i) {
67 // WARNING! Column-Polynomials order matters!
68 auto& poly = unshifted[i];
69 Column col = static_cast<Column>(i);
70
71 trace.visit_column(col, [&](size_t row, const AvmProver::FF& value) {
72 // We use `at` because we are sure the row exists and the value is non-zero.
73 poly.at(row) = value;
74 });
75 // We free columns as we go.
77 });
78 }));
79
80 AVM_TRACK_TIME("proving/set_polys_shifted", ({
81 for (auto [shifted, to_be_shifted] : zip_view(polys.get_shifted(), polys.get_to_be_shifted())) {
82 shifted = to_be_shifted.shifted();
83 }
84 }));
85
86 return polys;
87}
88
90 Column inverses_col,
91 Column src_selector_col,
92 Column dst_selector_col)
93{
94 auto& inverse_polynomial = prover_polynomials.get(static_cast<ColumnAndShifts>(inverses_col));
95 const auto& src_selector = prover_polynomials.get(static_cast<ColumnAndShifts>(src_selector_col));
96 const auto& dst_selector = prover_polynomials.get(static_cast<ColumnAndShifts>(dst_selector_col));
97
98 if (!inverse_polynomial.is_empty()) {
99 throw std::runtime_error("Inverse polynomial is expected to be empty at this point.");
100 }
101
102 const size_t num_rows = std::max<size_t>(src_selector.end_index(), dst_selector.end_index());
103 inverse_polynomial = AvmProver::Polynomial::create_non_parallel_zero_init(num_rows, MAX_AVM_TRACE_SIZE);
104 BB_ASSERT_EQ(prover_polynomials.get(static_cast<ColumnAndShifts>(inverses_col)).size(),
105 num_rows,
106 "Inverse polynomial size mismatch");
107}
108
110{
111 auto proving_key = std::make_shared<AvmProver::ProvingKey>();
112
113 for (auto [key_poly, prover_poly] : zip_view(proving_key->get_all(), polynomials.get_unshifted())) {
114 BB_ASSERT_EQ(flavor_get_label(*proving_key, key_poly), flavor_get_label(polynomials, prover_poly));
115 key_poly = std::move(prover_poly);
116 }
117
118 return proving_key;
119}
120
121} // namespace bb::avm2::constraining
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:83
static Polynomial create_non_parallel_zero_init(size_t size, size_t virtual_size)
A factory to construct a polynomial where parallel initialization is not possible (e....
DataType & get(ColumnAndShifts c)
Definition flavor.hpp:151
A container for the prover polynomials handles.
Definition flavor.hpp:293
Flavor::Polynomial Polynomial
Definition prover.hpp:20
uint32_t get_column_rows(Column col) const
TestTraceContainer trace
void resize_inverses(AvmFlavor::ProverPolynomials &prover_polynomials, Column inverses_col, Column src_selector_col, Column dst_selector_col)
AvmProver::ProverPolynomials compute_polynomials(tracegen::TraceContainer &trace)
std::shared_ptr< AvmProver::ProvingKey > proving_key_from_polynomials(AvmProver::ProverPolynomials &polynomials)
constexpr std::size_t MAX_AVM_TRACE_SIZE
Definition constants.hpp:13
constexpr auto TO_BE_SHIFTED_COLUMNS_ARRAY
Definition columns.hpp:77
ColumnAndShifts
Definition columns.hpp:34
std::string flavor_get_label(Container &&container, const Element &element)
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
#define AVM_TRACK_TIME(key, body)
Definition stats.hpp:16