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 for (size_t i = 0; i < to_be_shifted.size(); i++) {
26 auto& poly = to_be_shifted[i];
27 // WARNING! Column-Polynomials order matters!
28 Column col = static_cast<Column>(TO_BE_SHIFTED_COLUMNS_ARRAY.at(i));
29 uint32_t num_rows = trace.get_column_rows(col);
30 // Since we are shifting, we need to allocate one less row.
31 // The first row is always zero.
32 uint32_t allocated_size = num_rows > 0 ? num_rows - 1 : 0;
33
35 /*memory size*/ allocated_size,
36 /*largest possible index*/ MAX_AVM_TRACE_SIZE, // TODO(#16660): use real size?
37 /*make shiftable with offset*/ 1);
38 }
39 }));
40
41 // Catch-all with fully formed polynomials
42 // Note: derived polynomials (i.e., inverses) are not in the trace at this point, because they can only
43 // be computed after committing to the other witnesses. Therefore, they will be initialized as empty
44 // and they will be not set below. The derived polynomials will be reinitialized and set in the prover
45 // itself mid-proving.
46 AVM_TRACK_TIME("proving/init_polys_unshifted", ({
47 auto unshifted = polys.get_unshifted();
48 bb::parallel_for(unshifted.size(), [&](size_t i) {
49 auto& poly = unshifted[i];
50 // Some of the polynomials have been initialized above. Skip those.
51 if (poly.virtual_size() > 0) {
52 // Already initialized above.
53 return;
54 }
55
56 // WARNING! Column-Polynomials order matters!
57 Column col = static_cast<Column>(i);
58 const auto num_rows = trace.get_column_rows(col);
60 });
61 }));
62
63 AVM_TRACK_TIME("proving/set_polys_unshifted", ({
64 auto unshifted = polys.get_unshifted();
65 bb::parallel_for(unshifted.size(), [&](size_t i) {
66 // WARNING! Column-Polynomials order matters!
67 auto& poly = unshifted[i];
68 Column col = static_cast<Column>(i);
69
70 trace.visit_column(col, [&](size_t row, const AvmProver::FF& value) {
71 // We use `at` because we are sure the row exists and the value is non-zero.
72 poly.at(row) = value;
73 });
74 // We free columns as we go.
76 });
77 }));
78
79 AVM_TRACK_TIME("proving/set_polys_shifted", ({
80 for (auto [shifted, to_be_shifted] : zip_view(polys.get_shifted(), polys.get_to_be_shifted())) {
81 shifted = to_be_shifted.shifted();
82 }
83 }));
84
85 return polys;
86}
87
89 Column inverses_col,
90 Column src_selector_col,
91 Column dst_selector_col)
92{
93 auto& inverse_polynomial = prover_polynomials.get(static_cast<ColumnAndShifts>(inverses_col));
94 const auto& src_selector = prover_polynomials.get(static_cast<ColumnAndShifts>(src_selector_col));
95 const auto& dst_selector = prover_polynomials.get(static_cast<ColumnAndShifts>(dst_selector_col));
96
97 if (!inverse_polynomial.is_empty()) {
98 throw std::runtime_error("Inverse polynomial is expected to be empty at this point.");
99 }
100
101 const size_t num_rows = std::max<size_t>(src_selector.end_index(), dst_selector.end_index());
102 inverse_polynomial = AvmProver::Polynomial::create_non_parallel_zero_init(num_rows, MAX_AVM_TRACE_SIZE);
103 BB_ASSERT_EQ(prover_polynomials.get(static_cast<ColumnAndShifts>(inverses_col)).size(),
104 num_rows,
105 "Inverse polynomial size mismatch");
106}
107
109{
110 auto proving_key = std::make_shared<AvmProver::ProvingKey>();
111
112 for (auto [key_poly, prover_poly] : zip_view(proving_key->get_all(), polynomials.get_unshifted())) {
113 BB_ASSERT_EQ(flavor_get_label(*proving_key, key_poly), flavor_get_label(polynomials, prover_poly));
114 key_poly = std::move(prover_poly);
115 }
116
117 return proving_key;
118}
119
120} // namespace bb::avm2::constraining
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:93
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:153
A container for the prover polynomials handles.
Definition flavor.hpp:295
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