ark_poly/evaluations/univariate/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//! A univariate polynomial represented in evaluations form.

use crate::{
    univariate::DensePolynomial, DenseUVPolynomial, EvaluationDomain, GeneralEvaluationDomain,
};
use ark_ff::{batch_inversion, FftField};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::{
    ops::{Add, AddAssign, Div, DivAssign, Index, Mul, MulAssign, Sub, SubAssign},
    vec::*,
};

#[cfg(feature = "parallel")]
use rayon::prelude::*;

/// Stores a UV polynomial in evaluation form.
#[derive(Clone, PartialEq, Eq, Hash, Debug, CanonicalSerialize, CanonicalDeserialize)]
pub struct Evaluations<F: FftField, D: EvaluationDomain<F> = GeneralEvaluationDomain<F>> {
    /// The evaluations of a polynomial over the domain `D`
    pub evals: Vec<F>,
    #[doc(hidden)]
    domain: D,
}

impl<F: FftField, D: EvaluationDomain<F>> Evaluations<F, D> {
    /// Evaluations of the zero polynomial over `domain`.
    pub fn zero(domain: D) -> Self {
        Self {
            evals: vec![F::zero(); domain.size()],
            domain,
        }
    }

    /// Construct `Self` from evaluations and a domain.
    pub fn from_vec_and_domain(evals: Vec<F>, domain: D) -> Self {
        Self { evals, domain }
    }

    /// Interpolate a polynomial from a list of evaluations
    pub fn interpolate_by_ref(&self) -> DensePolynomial<F> {
        DensePolynomial::from_coefficients_vec(self.domain.ifft(&self.evals))
    }

    /// Interpolate a polynomial from a list of evaluations
    pub fn interpolate(self) -> DensePolynomial<F> {
        let Self { mut evals, domain } = self;
        domain.ifft_in_place(&mut evals);
        DensePolynomial::from_coefficients_vec(evals)
    }

    /// Return the domain `self` is defined over
    pub fn domain(&self) -> D {
        self.domain
    }
}

impl<F: FftField, D: EvaluationDomain<F>> Index<usize> for Evaluations<F, D> {
    type Output = F;

    fn index(&self, index: usize) -> &F {
        &self.evals[index]
    }
}

impl<'a, 'b, F: FftField, D: EvaluationDomain<F>> Mul<&'a Evaluations<F, D>>
    for &'b Evaluations<F, D>
{
    type Output = Evaluations<F, D>;

    #[inline]
    fn mul(self, other: &'a Evaluations<F, D>) -> Evaluations<F, D> {
        let mut result = self.clone();
        result *= other;
        result
    }
}

impl<'a, F: FftField, D: EvaluationDomain<F>> MulAssign<&'a Evaluations<F, D>>
    for Evaluations<F, D>
{
    #[inline]
    fn mul_assign(&mut self, other: &'a Evaluations<F, D>) {
        assert_eq!(self.domain, other.domain, "domains are unequal");
        ark_std::cfg_iter_mut!(self.evals)
            .zip(&other.evals)
            .for_each(|(a, b)| *a *= b);
    }
}

impl<'a, F: FftField, D: EvaluationDomain<F>> Mul<F> for &'a Evaluations<F, D> {
    type Output = Evaluations<F, D>;

    #[inline]
    fn mul(self, elem: F) -> Evaluations<F, D> {
        let mut result = self.clone();
        ark_std::cfg_iter_mut!(result.evals).for_each(|e| {
            *e *= elem;
        });
        result
    }
}

impl<'a, 'b, F: FftField, D: EvaluationDomain<F>> Add<&'a Evaluations<F, D>>
    for &'b Evaluations<F, D>
{
    type Output = Evaluations<F, D>;

    #[inline]
    fn add(self, other: &'a Evaluations<F, D>) -> Evaluations<F, D> {
        let mut result = self.clone();
        result += other;
        result
    }
}

impl<'a, F: FftField, D: EvaluationDomain<F>> AddAssign<&'a Evaluations<F, D>>
    for Evaluations<F, D>
{
    #[inline]
    fn add_assign(&mut self, other: &'a Evaluations<F, D>) {
        assert_eq!(self.domain, other.domain, "domains are unequal");
        ark_std::cfg_iter_mut!(self.evals)
            .zip(&other.evals)
            .for_each(|(a, b)| *a += b);
    }
}

impl<'a, 'b, F: FftField, D: EvaluationDomain<F>> Sub<&'a Evaluations<F, D>>
    for &'b Evaluations<F, D>
{
    type Output = Evaluations<F, D>;

    #[inline]
    fn sub(self, other: &'a Evaluations<F, D>) -> Evaluations<F, D> {
        let mut result = self.clone();
        result -= other;
        result
    }
}

impl<'a, F: FftField, D: EvaluationDomain<F>> SubAssign<&'a Evaluations<F, D>>
    for Evaluations<F, D>
{
    #[inline]
    fn sub_assign(&mut self, other: &'a Evaluations<F, D>) {
        assert_eq!(self.domain, other.domain, "domains are unequal");
        ark_std::cfg_iter_mut!(self.evals)
            .zip(&other.evals)
            .for_each(|(a, b)| *a -= b);
    }
}

impl<'a, 'b, F: FftField, D: EvaluationDomain<F>> Div<&'a Evaluations<F, D>>
    for &'b Evaluations<F, D>
{
    type Output = Evaluations<F, D>;

    #[inline]
    fn div(self, other: &'a Evaluations<F, D>) -> Evaluations<F, D> {
        let mut result = self.clone();
        result /= other;
        result
    }
}

impl<'a, F: FftField, D: EvaluationDomain<F>> DivAssign<&'a Evaluations<F, D>>
    for Evaluations<F, D>
{
    #[inline]
    fn div_assign(&mut self, other: &'a Evaluations<F, D>) {
        assert_eq!(self.domain, other.domain, "domains are unequal");
        let mut other_copy = other.clone();
        batch_inversion(other_copy.evals.as_mut_slice());
        ark_std::cfg_iter_mut!(self.evals)
            .zip(&other_copy.evals)
            .for_each(|(a, b)| *a *= b);
    }
}