ark_poly/evaluations/univariate/
mod.rs1use crate::{
4 univariate::DensePolynomial, DenseUVPolynomial, EvaluationDomain, GeneralEvaluationDomain,
5};
6use ark_ff::{batch_inversion, FftField};
7use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
8use ark_std::{
9 ops::{Add, AddAssign, Div, DivAssign, Index, Mul, MulAssign, Sub, SubAssign},
10 vec::*,
11};
12
13#[cfg(feature = "parallel")]
14use rayon::prelude::*;
15
16#[derive(Clone, PartialEq, Eq, Hash, Debug, CanonicalSerialize, CanonicalDeserialize)]
18pub struct Evaluations<F: FftField, D: EvaluationDomain<F> = GeneralEvaluationDomain<F>> {
19 pub evals: Vec<F>,
21 #[doc(hidden)]
22 domain: D,
23}
24
25impl<F: FftField, D: EvaluationDomain<F>> Evaluations<F, D> {
26 pub fn zero(domain: D) -> Self {
28 Self {
29 evals: vec![F::zero(); domain.size()],
30 domain,
31 }
32 }
33
34 pub fn from_vec_and_domain(evals: Vec<F>, domain: D) -> Self {
36 Self { evals, domain }
37 }
38
39 pub fn interpolate_by_ref(&self) -> DensePolynomial<F> {
41 DensePolynomial::from_coefficients_vec(self.domain.ifft(&self.evals))
42 }
43
44 pub fn interpolate(self) -> DensePolynomial<F> {
46 let Self { mut evals, domain } = self;
47 domain.ifft_in_place(&mut evals);
48 DensePolynomial::from_coefficients_vec(evals)
49 }
50
51 pub fn domain(&self) -> D {
53 self.domain
54 }
55}
56
57impl<F: FftField, D: EvaluationDomain<F>> Index<usize> for Evaluations<F, D> {
58 type Output = F;
59
60 fn index(&self, index: usize) -> &F {
61 &self.evals[index]
62 }
63}
64
65impl<'a, 'b, F: FftField, D: EvaluationDomain<F>> Mul<&'a Evaluations<F, D>>
66 for &'b Evaluations<F, D>
67{
68 type Output = Evaluations<F, D>;
69
70 #[inline]
71 fn mul(self, other: &'a Evaluations<F, D>) -> Evaluations<F, D> {
72 let mut result = self.clone();
73 result *= other;
74 result
75 }
76}
77
78impl<'a, F: FftField, D: EvaluationDomain<F>> MulAssign<&'a Evaluations<F, D>>
79 for Evaluations<F, D>
80{
81 #[inline]
82 fn mul_assign(&mut self, other: &'a Evaluations<F, D>) {
83 assert_eq!(self.domain, other.domain, "domains are unequal");
84 ark_std::cfg_iter_mut!(self.evals)
85 .zip(&other.evals)
86 .for_each(|(a, b)| *a *= b);
87 }
88}
89
90impl<'a, F: FftField, D: EvaluationDomain<F>> Mul<F> for &'a Evaluations<F, D> {
91 type Output = Evaluations<F, D>;
92
93 #[inline]
94 fn mul(self, elem: F) -> Evaluations<F, D> {
95 let mut result = self.clone();
96 ark_std::cfg_iter_mut!(result.evals).for_each(|e| {
97 *e *= elem;
98 });
99 result
100 }
101}
102
103impl<'a, 'b, F: FftField, D: EvaluationDomain<F>> Add<&'a Evaluations<F, D>>
104 for &'b Evaluations<F, D>
105{
106 type Output = Evaluations<F, D>;
107
108 #[inline]
109 fn add(self, other: &'a Evaluations<F, D>) -> Evaluations<F, D> {
110 let mut result = self.clone();
111 result += other;
112 result
113 }
114}
115
116impl<'a, F: FftField, D: EvaluationDomain<F>> AddAssign<&'a Evaluations<F, D>>
117 for Evaluations<F, D>
118{
119 #[inline]
120 fn add_assign(&mut self, other: &'a Evaluations<F, D>) {
121 assert_eq!(self.domain, other.domain, "domains are unequal");
122 ark_std::cfg_iter_mut!(self.evals)
123 .zip(&other.evals)
124 .for_each(|(a, b)| *a += b);
125 }
126}
127
128impl<'a, 'b, F: FftField, D: EvaluationDomain<F>> Sub<&'a Evaluations<F, D>>
129 for &'b Evaluations<F, D>
130{
131 type Output = Evaluations<F, D>;
132
133 #[inline]
134 fn sub(self, other: &'a Evaluations<F, D>) -> Evaluations<F, D> {
135 let mut result = self.clone();
136 result -= other;
137 result
138 }
139}
140
141impl<'a, F: FftField, D: EvaluationDomain<F>> SubAssign<&'a Evaluations<F, D>>
142 for Evaluations<F, D>
143{
144 #[inline]
145 fn sub_assign(&mut self, other: &'a Evaluations<F, D>) {
146 assert_eq!(self.domain, other.domain, "domains are unequal");
147 ark_std::cfg_iter_mut!(self.evals)
148 .zip(&other.evals)
149 .for_each(|(a, b)| *a -= b);
150 }
151}
152
153impl<'a, 'b, F: FftField, D: EvaluationDomain<F>> Div<&'a Evaluations<F, D>>
154 for &'b Evaluations<F, D>
155{
156 type Output = Evaluations<F, D>;
157
158 #[inline]
159 fn div(self, other: &'a Evaluations<F, D>) -> Evaluations<F, D> {
160 let mut result = self.clone();
161 result /= other;
162 result
163 }
164}
165
166impl<'a, F: FftField, D: EvaluationDomain<F>> DivAssign<&'a Evaluations<F, D>>
167 for Evaluations<F, D>
168{
169 #[inline]
170 fn div_assign(&mut self, other: &'a Evaluations<F, D>) {
171 assert_eq!(self.domain, other.domain, "domains are unequal");
172 let mut other_copy = other.clone();
173 batch_inversion(other_copy.evals.as_mut_slice());
174 ark_std::cfg_iter_mut!(self.evals)
175 .zip(&other_copy.evals)
176 .for_each(|(a, b)| *a *= b);
177 }
178}