23#include <unordered_map>
32 size_t right_expansion = 0,
33 size_t left_expansion = 0)
35 size_t expanded_size = array.
size() + right_expansion + left_expansion;
38 memset(
static_cast<void*
>(backing_clone.
raw_data), 0,
sizeof(
Fr) * left_expansion);
40 memcpy(
static_cast<void*
>(backing_clone.
raw_data + left_expansion),
41 static_cast<const void*
>(array.
data()),
42 sizeof(
Fr) * array.
size());
44 memset(
static_cast<void*
>(backing_clone.
raw_data + left_expansion + array.
size()), 0,
sizeof(
Fr) * right_expansion);
74 BB_BENCH_NAME(
"Polynomial::Polynomial(size_t, size_t, size_t)");
75 allocate_backing_memory(size, virtual_size, start_index);
79 auto range = chunk.
range(size);
81 size_t start = *range.begin();
82 size_t range_size = range.size();
85 memset(
static_cast<void*
>(coefficients_.data() + start), 0,
sizeof(
Fr) * range_size);
100 allocate_backing_memory(size, virtual_size, start_index);
103template <
typename Fr>
116template <
typename Fr>
120 :
Polynomial(interpolation_points.size(), virtual_size)
132 allocate_backing_memory(coefficients.size(), virtual_size, 0);
134 memcpy(
static_cast<void*
>(
data()),
static_cast<const void*
>(coefficients.data()),
sizeof(
Fr) * coefficients.size());
142 if (
this == &other) {
159 if (is_empty() || rhs.is_empty()) {
160 return is_empty() && rhs.is_empty();
163 if (virtual_size() != rhs.virtual_size()) {
167 for (
size_t i = std::min(coefficients_.start_, rhs.coefficients_.start_);
168 i <
std::max(coefficients_.end_, rhs.coefficients_.end_);
170 if (coefficients_.get(i) != rhs.coefficients_.get(i)) {
197 if (start_index() > 0) {
198 result *= z.
pow(start_index());
205 return _evaluate_mle(evaluation_points, coefficients_, shift);
228 multiply_chunk(chunk, scaling_factor);
235 for (
size_t i : chunk.
range(size())) {
236 data()[i] *= scaling_factor;
243 memset(
static_cast<void*
>(p.
coefficients_.data()), 0,
sizeof(
Fr) * size);
253 coefficients_.end_ = new_end_index;
271 add_scaled_chunk(chunk, other, scaling_factor);
275template <
typename Fr>
278 const Fr& scaling_factor)
287template <
typename Fr>
293 BB_ASSERT_EQ(sources.size(), scalars.size(),
"sources and scalars must have the same length");
294 if (sources.empty()) {
298 size_t min_start = sources[0].start_index;
299 size_t max_end = sources[0].end_index();
300 for (
size_t i = 1; i < sources.size(); ++i) {
301 min_start = std::min(min_start, sources[i].start_index);
302 max_end =
std::max(max_end, sources[i].end_index());
307 const size_t union_size = max_end - min_start;
310 auto chunk_indices = chunk.
range(union_size, min_start);
311 if (chunk_indices.empty()) {
314 auto chunk_start = chunk_indices.front();
315 auto chunk_end = chunk_indices.back();
317 for (
size_t k = 0; k < sources.size(); ++k) {
318 const auto& src = sources[k];
319 const Fr& c = scalars[k];
320 const size_t src_start = src.start_index;
321 const size_t src_end = src.end_index();
323 const size_t idx_start =
std::max(chunk_start, src_start);
324 const size_t idx_end = std::min(chunk_end + 1, src_end);
326 for (
size_t i = idx_start; i < idx_end; ++i) {
327 dst.
at(i) += c * src[i];
345 const size_t end_index = this->end_index();
346 const size_t start_index = this->start_index();
347 const size_t poly_size = this->size();
349 for (
size_t idx = end_index; idx > start_index; --idx) {
350 reversed.
at(end_index - idx) = this->at(idx - 1);
355template class Polynomial<bb::fr>;
356template class Polynomial<grumpkin::fr>;
#define BB_ASSERT(expression,...)
#define BB_ASSERT_GTE(left, right,...)
#define BB_ASSERT_GT(left, right,...)
#define BB_ASSERT_EQ(actual, expected,...)
#define BB_ASSERT_LTE(left, right,...)
#define BB_BENCH_NAME(name)
#define BB_BENCH_TRACY_NAME(name)
Structured polynomial class that represents the coefficients 'a' of a_0 + a_1 x .....
Polynomial shifted() const
Returns a Polynomial the left-shift of self.
size_t start_index() const
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 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)
Polynomial & operator=(Polynomial &&other) noexcept
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
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.
template void add_scaled_batch< bb::fr >(Polynomial< bb::fr > &dst, std::span< const PolynomialSpan< const bb::fr > > sources, std::span< const bb::fr > scalars)
template void add_scaled_batch< grumpkin::fr >(Polynomial< grumpkin::fr > &dst, std::span< const PolynomialSpan< const grumpkin::fr > > sources, std::span< const grumpkin::fr > scalars)
SharedShiftedVirtualZeroesArray< Fr > _clone(const SharedShiftedVirtualZeroesArray< Fr > &array, size_t right_expansion=0, size_t left_expansion=0)
void add_scaled_batch(Polynomial< Fr > &dst, std::span< const PolynomialSpan< const Fr > > sources, std::span< const Fr > scalars)
Fused parallel batched add: dst += sum_i scalars[i] * sources[i].
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)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
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.
auto range(size_t size, size_t offset=0) const
BB_INLINE constexpr field pow(const uint256_t &exponent) const noexcept