24#include <unordered_map>
33 size_t right_expansion = 0,
34 size_t left_expansion = 0)
36 size_t expanded_size = array.
size() + right_expansion + left_expansion;
39 memset(
static_cast<void*
>(backing_clone.
raw_data), 0,
sizeof(
Fr) * left_expansion);
41 memcpy(
static_cast<void*
>(backing_clone.
raw_data + left_expansion),
42 static_cast<const void*
>(array.
data()),
43 sizeof(
Fr) * array.
size());
45 memset(
static_cast<void*
>(backing_clone.
raw_data + left_expansion + array.
size()), 0,
sizeof(
Fr) * right_expansion);
75 BB_BENCH_NAME(
"Polynomial::Polynomial(size_t, size_t, size_t)");
76 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();
83 BB_ASSERT(start < size || size == 0);
84 BB_ASSERT_LTE((start + range_size), 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>
112 coefficients_ =
_clone(other.coefficients_, target_size - other.size());
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)) {
182 if (start_index() > 0) {
196 memset(
static_cast<void*
>(p.
coefficients_.data()), 0,
sizeof(
Fr) * size);
205 coefficients_.end_ = new_end_index;
212 result.coefficients_ =
_clone(coefficients_, virtual_size() - end_index(), start_index());
225 result.coefficients_ = coefficients_;
226 result.coefficients_.start_ -= 1;
227 result.coefficients_.end_ -= 1;
233 const size_t end_index = this->end_index();
234 const size_t start_index = this->start_index();
235 const size_t poly_size = this->size();
237 for (
size_t idx = end_index; idx > start_index; --idx) {
238 reversed.
at(end_index - idx) = this->at(idx - 1);
243template <
typename Fr>
249 BB_ASSERT_EQ(sources.size(), scalars.size(),
"sources and scalars must have the same length");
250 if (sources.empty()) {
254 size_t min_start = sources[0].start_index;
255 size_t max_end = sources[0].end_index();
256 for (
size_t i = 1; i < sources.size(); ++i) {
257 min_start = std::min(min_start, sources[i].start_index);
258 max_end =
std::max(max_end, sources[i].end_index());
263 const size_t union_size = max_end - min_start;
266 auto chunk_indices = chunk.
range(union_size, min_start);
267 if (chunk_indices.empty()) {
270 auto chunk_start = chunk_indices.front();
271 auto chunk_end = chunk_indices.back();
273 for (
size_t k = 0; k < sources.size(); ++k) {
274 const auto& src = sources[k];
275 const Fr& c = scalars[k];
276 const size_t src_start = src.start_index;
277 const size_t src_end = src.end_index();
279 const size_t idx_start =
std::max(chunk_start, src_start);
280 const size_t idx_end = std::min(chunk_end + 1, src_end);
282 for (
size_t i = idx_start; i < idx_end; ++i) {
283 dst.
at(i) += c * src[i];
289template class Polynomial<bb::fr>;
290template class Polynomial<grumpkin::fr>;
#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)
Polynomial shifted() const
Returns a Polynomial the left-shift of self.
size_t start_index() const
SharedShiftedVirtualZeroesArray< Fr > coefficients_
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 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...
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
Polynomial full() const
Copys the polynomial, but with the whole address space usable. The value of the polynomial remains th...
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].
constexpr ScalarIndex shift(ScalarIndex ctx, size_t d)
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
bb::VectorAffineElementPushSpan< BaseParams > rhs
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