fullcodec_plonk/fft/
evaluations.rs1use super::domain::EvaluationDomain;
10use super::polynomial::Polynomial;
11use crate::error::Error;
12use core::ops::{
13 Add, AddAssign, DivAssign, Index, Mul, MulAssign, Sub, SubAssign,
14};
15use dusk_bls12_381::BlsScalar;
16use dusk_bytes::{DeserializableSlice, Serializable};
17use sp_std::vec::Vec;
18
19#[derive(PartialEq, Eq, Debug, Clone)]
21pub(crate) struct Evaluations {
22 pub(crate) evals: Vec<BlsScalar>,
24 #[doc(hidden)]
25 domain: EvaluationDomain,
27}
28
29impl Evaluations {
30 pub fn to_var_bytes(&self) -> Vec<u8> {
32 let mut bytes: Vec<u8> = self.domain.to_bytes().to_vec();
33 bytes.extend(
34 self.evals
35 .iter()
36 .map(|scalar| scalar.to_bytes().to_vec())
37 .flatten(),
38 );
39
40 bytes
41 }
42
43 pub fn from_slice(bytes: &[u8]) -> Result<Evaluations, Error> {
45 let mut buffer = bytes;
46 let domain = EvaluationDomain::from_reader(&mut buffer)?;
47 let evals = buffer
48 .chunks(BlsScalar::SIZE)
49 .map(|chunk| BlsScalar::from_slice(chunk))
50 .collect::<Result<Vec<BlsScalar>, dusk_bytes::Error>>()?;
51 Ok(Evaluations::from_vec_and_domain(evals, domain))
52 }
53
54 pub(crate) const fn from_vec_and_domain(
56 evals: Vec<BlsScalar>,
57 domain: EvaluationDomain,
58 ) -> Self {
59 Self { evals, domain }
60 }
61
62 pub(crate) fn interpolate(self) -> Polynomial {
64 let Self { mut evals, domain } = self;
65 domain.ifft_in_place(&mut evals);
66 Polynomial::from_coefficients_vec(evals)
67 }
68}
69
70impl Index<usize> for Evaluations {
71 type Output = BlsScalar;
72
73 fn index(&self, index: usize) -> &BlsScalar {
74 &self.evals[index]
75 }
76}
77
78impl<'a, 'b> Mul<&'a Evaluations> for &'b Evaluations {
79 type Output = Evaluations;
80
81 #[inline]
82 fn mul(self, other: &'a Evaluations) -> Evaluations {
83 let mut result = self.clone();
84 result *= other;
85 result
86 }
87}
88
89impl<'a> MulAssign<&'a Evaluations> for Evaluations {
90 #[inline]
91 fn mul_assign(&mut self, other: &'a Evaluations) {
92 assert_eq!(self.domain, other.domain, "domains are unequal");
93 self.evals
94 .iter_mut()
95 .zip(&other.evals)
96 .for_each(|(a, b)| *a *= b);
97 }
98}
99
100impl<'a, 'b> Add<&'a Evaluations> for &'b Evaluations {
101 type Output = Evaluations;
102
103 #[inline]
104 fn add(self, other: &'a Evaluations) -> Evaluations {
105 let mut result = self.clone();
106 result += other;
107 result
108 }
109}
110
111impl<'a> AddAssign<&'a Evaluations> for Evaluations {
112 #[inline]
113 fn add_assign(&mut self, other: &'a Evaluations) {
114 assert_eq!(self.domain, other.domain, "domains are unequal");
115 self.evals
116 .iter_mut()
117 .zip(&other.evals)
118 .for_each(|(a, b)| *a += b);
119 }
120}
121
122impl<'a, 'b> Sub<&'a Evaluations> for &'b Evaluations {
123 type Output = Evaluations;
124
125 #[inline]
126 fn sub(self, other: &'a Evaluations) -> Evaluations {
127 let mut result = self.clone();
128 result -= other;
129 result
130 }
131}
132
133impl<'a> SubAssign<&'a Evaluations> for Evaluations {
134 #[inline]
135 fn sub_assign(&mut self, other: &'a Evaluations) {
136 assert_eq!(self.domain, other.domain, "domains are unequal");
137 self.evals
138 .iter_mut()
139 .zip(&other.evals)
140 .for_each(|(a, b)| *a -= b);
141 }
142}
143
144impl<'a> DivAssign<&'a Evaluations> for Evaluations {
145 #[inline]
146 fn div_assign(&mut self, other: &'a Evaluations) {
147 assert_eq!(self.domain, other.domain, "domains are unequal");
148 self.evals
149 .iter_mut()
150 .zip(&other.evals)
151 .for_each(|(a, b)| *a *= b.invert().unwrap());
152 }
153}