Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
polynomial.cpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: Complete, auditors: [Nishat], commit: 94f596f8b3bbbc216f9ad7dc33253256141156b2 }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
7#include "polynomial.hpp"
16#include <cstddef>
17#include <fcntl.h>
18#include <list>
19#include <memory>
20#include <mutex>
21#include <span>
22#include <sys/stat.h>
23#include <unordered_map>
24#include <utility>
25
26namespace bb {
27
28// Note: This function is pretty gnarly, but we try to make it the only function that deals
29// with copying polynomials. It should be scrutinized thusly.
30template <typename Fr>
32 size_t right_expansion = 0,
33 size_t left_expansion = 0)
34{
35 size_t expanded_size = array.size() + right_expansion + left_expansion;
36 BackingMemory<Fr> backing_clone = BackingMemory<Fr>::allocate(expanded_size);
37 // zero any left extensions to the array
38 memset(static_cast<void*>(backing_clone.raw_data), 0, sizeof(Fr) * left_expansion);
39 // copy our cloned array over
40 memcpy(static_cast<void*>(backing_clone.raw_data + left_expansion),
41 static_cast<const void*>(array.data()),
42 sizeof(Fr) * array.size());
43 // zero any right extensions to the array
44 memset(static_cast<void*>(backing_clone.raw_data + left_expansion + array.size()), 0, sizeof(Fr) * right_expansion);
45 return {
46 array.start_ - left_expansion, array.end_ + right_expansion, array.virtual_size_, std::move(backing_clone)
47 };
48}
49
50template <typename Fr>
51void Polynomial<Fr>::allocate_backing_memory(size_t size, size_t virtual_size, size_t start_index)
52{
53 BB_BENCH_NAME("Polynomial::allocate_backing_memory");
54 BB_ASSERT_LTE(start_index + size, virtual_size);
56 start_index, /* start index, used for shifted polynomials and offset 'islands' of non-zeroes */
57 size + start_index, /* end index, actual memory used is (end - start) */
58 virtual_size, /* virtual size, i.e. until what size do we conceptually have zeroes */
60 };
61}
62
72template <typename Fr> Polynomial<Fr>::Polynomial(size_t size, size_t virtual_size, size_t start_index)
73{
74 BB_BENCH_NAME("Polynomial::Polynomial(size_t, size_t, size_t)");
75 allocate_backing_memory(size, virtual_size, start_index);
76
77 parallel_for([&](const ThreadChunk& chunk) {
78 auto range = chunk.range(size);
79 if (!range.empty()) {
80 size_t start = *range.begin();
81 size_t range_size = range.size();
82 BB_ASSERT(start < size || size == 0);
83 BB_ASSERT_LTE((start + range_size), size);
84 memset(static_cast<void*>(coefficients_.data() + start), 0, sizeof(Fr) * range_size);
85 }
86 });
87}
88
96template <typename Fr>
97Polynomial<Fr>::Polynomial(size_t size, size_t virtual_size, size_t start_index, [[maybe_unused]] DontZeroMemory flag)
99 allocate_backing_memory(size, virtual_size, start_index);
100}
101
102template <typename Fr>
104 : Polynomial<Fr>(other, other.size())
105{}
106
107// fully copying "expensive" constructor
108template <typename Fr> Polynomial<Fr>::Polynomial(const Polynomial<Fr>& other, const size_t target_size)
109{
110 BB_ASSERT_LTE(other.size(), target_size);
111 coefficients_ = _clone(other.coefficients_, target_size - other.size());
112}
113
114// interpolation constructor
115template <typename Fr>
117 std::span<const Fr> evaluations,
118 size_t virtual_size)
119 : Polynomial(interpolation_points.size(), virtual_size)
120{
121 BB_ASSERT_GT(coefficients_.size(), static_cast<size_t>(0));
122
124 evaluations.data(), coefficients_.data(), interpolation_points.data(), coefficients_.size());
125}
126
127template <typename Fr> Polynomial<Fr>::Polynomial(std::span<const Fr> coefficients, size_t virtual_size)
128{
129 allocate_backing_memory(coefficients.size(), virtual_size, 0);
130
131 memcpy(static_cast<void*>(data()), static_cast<const void*>(coefficients.data()), sizeof(Fr) * coefficients.size());
132}
133
134// Assignments
135
136// full copy "expensive" assignment
137template <typename Fr> Polynomial<Fr>& Polynomial<Fr>::operator=(const Polynomial<Fr>& other)
138{
139 if (this == &other) {
140 return *this;
141 }
142 coefficients_ = _clone(other.coefficients_);
143 return *this;
144}
145
146template <typename Fr> Polynomial<Fr> Polynomial<Fr>::share() const
147{
148 Polynomial p;
149 p.coefficients_ = coefficients_;
150 return p;
151}
152
153template <typename Fr> bool Polynomial<Fr>::operator==(Polynomial const& rhs) const
154{
155 // If either is empty, both must be
156 if (is_empty() || rhs.is_empty()) {
157 return is_empty() && rhs.is_empty();
159 // Size must agree
160 if (virtual_size() != rhs.virtual_size()) {
161 return false;
162 }
163 // Each coefficient must agree
164 for (size_t i = std::min(coefficients_.start_, rhs.coefficients_.start_);
165 i < std::max(coefficients_.end_, rhs.coefficients_.end_);
166 i++) {
167 if (coefficients_.get(i) != rhs.coefficients_.get(i)) {
168 return false;
169 }
170 }
171 return true;
172}
173
175{
176 BB_ASSERT_LTE(start_index(), other.start_index);
177 BB_ASSERT_GTE(end_index(), other.end_index());
178 parallel_for([&](const ThreadChunk& chunk) {
179 for (size_t offset : chunk.range(other.size())) {
180 size_t i = offset + other.start_index;
181 at(i) += other[i];
182 }
183 });
184 return *this;
185}
187template <typename Fr> Fr Polynomial<Fr>::evaluate(const Fr& z) const
188{
189 // Evaluate only the backing data; virtual zeroes beyond backing contribute nothing.
190 // When start_index > 0, multiply by z^start_index to account for the offset.
191 Fr result = polynomial_arithmetic::evaluate(data(), z, size());
192 if (start_index() > 0) {
193 result *= z.pow(start_index());
194 }
195 return result;
196}
197
198template <typename Fr> Fr Polynomial<Fr>::evaluate_mle(std::span<const Fr> evaluation_points, bool shift) const
199{
200 return _evaluate_mle(evaluation_points, coefficients_, shift);
201}
204{
205 BB_ASSERT_LTE(start_index(), other.start_index);
206 BB_ASSERT_GTE(end_index(), other.end_index());
207 parallel_for([&](const ThreadChunk& chunk) {
208 for (size_t offset : chunk.range(other.size())) {
209 size_t i = offset + other.start_index;
210 at(i) -= other[i];
211 }
212 });
213 return *this;
214}
215
216template <typename Fr> Polynomial<Fr>& Polynomial<Fr>::operator*=(const Fr& scaling_factor)
217{
218 parallel_for([scaling_factor, this](const ThreadChunk& chunk) { multiply_chunk(chunk, scaling_factor); });
219 return *this;
220}
221
222template <typename Fr> void Polynomial<Fr>::multiply_chunk(const ThreadChunk& chunk, const Fr& scaling_factor)
223{
224 for (size_t i : chunk.range(size())) {
225 data()[i] *= scaling_factor;
227}
228
229template <typename Fr> Polynomial<Fr> Polynomial<Fr>::create_non_parallel_zero_init(size_t size, size_t virtual_size)
230{
232 memset(static_cast<void*>(p.coefficients_.data()), 0, sizeof(Fr) * size);
233 return p;
234}
235
236template <typename Fr> void Polynomial<Fr>::shrink_end_index(const size_t new_end_index)
237{
238 BB_ASSERT_LTE(new_end_index, end_index());
239 coefficients_.end_ = new_end_index;
241
242template <typename Fr> Polynomial<Fr> Polynomial<Fr>::full() const
243{
244 Polynomial result = *this;
245 // Make 0..virtual_size usable
246 result.coefficients_ = _clone(coefficients_, virtual_size() - end_index(), start_index());
247 return result;
248}
250template <typename Fr> void Polynomial<Fr>::add_scaled(PolynomialSpan<const Fr> other, const Fr& scaling_factor)
251{
252 BB_ASSERT_LTE(start_index(), other.start_index);
253 BB_ASSERT_GTE(end_index(), other.end_index());
255 [&other, scaling_factor, this](const ThreadChunk& chunk) { add_scaled_chunk(chunk, other, scaling_factor); });
256}
257
258template <typename Fr>
261 const Fr& scaling_factor)
262{
263 // Iterate over the chunk of the other polynomial's range
264 for (size_t offset : chunk.range(other.size())) {
265 size_t index = other.start_index + offset;
266 at(index) += scaling_factor * other[index];
267 }
268}
269
270template <typename Fr> Polynomial<Fr> Polynomial<Fr>::shifted() const
271{
272 BB_ASSERT_GTE(coefficients_.start_, static_cast<size_t>(1));
273 Polynomial result;
274 result.coefficients_ = coefficients_;
275 result.coefficients_.start_ -= 1;
276 result.coefficients_.end_ -= 1;
277 return result;
278}
279
280template <typename Fr> Polynomial<Fr> Polynomial<Fr>::reverse() const
281{
282 const size_t end_index = this->end_index();
283 const size_t start_index = this->start_index();
284 const size_t poly_size = this->size();
285 Polynomial reversed(/*size=*/poly_size, /*virtual_size=*/end_index);
286 for (size_t idx = end_index; idx > start_index; --idx) {
287 reversed.at(end_index - idx) = this->at(idx - 1);
288 }
289 return reversed;
290}
291
292template class Polynomial<bb::fr>;
293template class Polynomial<grumpkin::fr>;
294} // namespace bb
#define BB_ASSERT_GTE(left, right,...)
Definition assert.hpp:128
#define BB_ASSERT_GT(left, right,...)
Definition assert.hpp:113
#define BB_ASSERT_LTE(left, right,...)
Definition assert.hpp:158
#define BB_BENCH_NAME(name)
Definition bb_bench.hpp:225
Structured polynomial class that represents the coefficients 'a' of a_0 + a_1 x .....
Polynomial & operator=(Polynomial &&other) noexcept=default
Polynomial shifted() const
Returns a Polynomial the left-shift of self.
Polynomial()=default
SharedShiftedVirtualZeroesArray< Fr > coefficients_
Polynomial & operator*=(const Fr &scaling_factor)
sets this = p(X) to s⋅p(X)
void add_scaled(PolynomialSpan< const Fr > other, const Fr &scaling_factor)
adds the polynomial q(X) 'other', multiplied by a scaling factor.
void add_scaled_chunk(const ThreadChunk &chunk, PolynomialSpan< const Fr > other, const Fr &scaling_factor)
Fr evaluate(const Fr &z) const
Fr evaluate_mle(std::span< const Fr > evaluation_points, bool shift=false) const
evaluate multi-linear extension p(X_0,…,X_{n-1}) = \sum_i a_i*L_i(X_0,…,X_{n-1}) at u = (u_0,...
const Fr & get(size_t i, size_t virtual_padding=0) const
Retrieves the value at the specified index.
Polynomial & operator-=(PolynomialSpan< const Fr > other)
subtracts the polynomial q(X) 'other'.
Polynomial share() const
Polynomial reverse() const
Returns the polynomial equal to the reverse of self.
Fr & at(size_t index)
Our mutable accessor, unlike operator[]. We abuse precedent a bit to differentiate at() and operator[...
bool operator==(Polynomial const &rhs) const
void shrink_end_index(const size_t new_end_index)
The end_index of the polynomial is decreased without any memory de-allocation. This is a very fast wa...
Polynomial & operator+=(PolynomialSpan< const Fr > other)
adds the polynomial q(X) 'other'.
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....
void allocate_backing_memory(size_t size, size_t virtual_size, size_t start_index)
void multiply_chunk(const ThreadChunk &chunk, const Fr &scaling_factor)
Polynomial full() const
Copys the polynomial, but with the whole address space usable. The value of the polynomial remains th...
const std::vector< MemoryValue > data
ssize_t offset
Definition engine.cpp:52
Fr evaluate(const Fr *coeffs, const Fr &z, const size_t n)
void compute_efficient_interpolation(const Fr *src, Fr *dest, const Fr *evaluation_points, const size_t n)
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
SharedShiftedVirtualZeroesArray< Fr > _clone(const SharedShiftedVirtualZeroesArray< Fr > &array, size_t right_expansion=0, size_t left_expansion=0)
Fr_ _evaluate_mle(std::span< const Fr_ > evaluation_points, const SharedShiftedVirtualZeroesArray< Fr_ > &coefficients, bool shift)
Internal implementation to support both native and stdlib circuit field types.
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
static BackingMemory allocate(size_t size)
A shared pointer array template that represents a virtual array filled with zeros up to virtual_size_...
size_t start_
The starting index of the memory-backed range.
T * data()
Returns a pointer to the underlying memory array. NOTE: This should be used with care,...
size_t virtual_size_
The total logical size of the array.
size_t end_
The ending index of the memory-backed range.
size_t size() const
size_t end_index() const
auto range(size_t size, size_t offset=0) const
Definition thread.hpp:152
BB_INLINE constexpr field pow(const uint256_t &exponent) const noexcept