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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
use crate::backends::ntt::private::math::mod_q::ModQ;
use crate::commons::math::polynomial::Polynomial;
use crate::commons::math::tensor::{
ck_dim_eq, tensor_traits, AsMutSlice, AsMutTensor, AsRefSlice, AsRefTensor, IntoTensor, Tensor,
};
use crate::commons::numeric::UnsignedInteger;
use crate::commons::utils::{zip, zip_args};
use crate::prelude::PolynomialSize;
/// A polynomial in the NTT domain.
///
/// This structure represents a polynomial, which was put in the NTT domain.
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct ModQPolynomial<Cont> {
tensor: Tensor<Cont>,
}
tensor_traits!(ModQPolynomial);
impl<N: UnsignedInteger> ModQPolynomial<Vec<ModQ<N>>> {
/// Allocates a new polynomial over Z_q.
///
/// # Example
///
/// ```
/// use concrete_core::backends::ntt::private::math::mod_q::ModQ;
/// use concrete_core::backends::ntt::private::math::polynomial::ModQPolynomial;
/// use concrete_core::prelude::PolynomialSize;
/// let modq_poly = ModQPolynomial::allocate(ModQ::new(0, 257u64), PolynomialSize(128));
/// assert_eq!(modq_poly.polynomial_size(), PolynomialSize(128));
/// ```
pub fn allocate(value: ModQ<N>, coef_count: PolynomialSize) -> Self {
let tensor = Tensor::from_container(vec![value; coef_count.0]);
ModQPolynomial { tensor }
}
}
impl<Cont> ModQPolynomial<Cont> {
/// Creates a polynomial over Z_q from an existing container of values.
///
/// # Example
///
/// ```rust
/// use concrete_core::backends::ntt::private::math::mod_q::ModQ;
/// use concrete_core::backends::ntt::private::math::polynomial::ModQPolynomial;
/// use concrete_core::prelude::PolynomialSize;
/// let mut vec: Vec<ModQ<u64>> = vec![ModQ::empty(); 128];
/// let modq_poly = ModQPolynomial::from_container(vec.as_mut_slice());
/// assert_eq!(modq_poly.polynomial_size(), PolynomialSize(128));
/// ```
pub fn from_container(cont: Cont) -> Self {
ModQPolynomial {
tensor: Tensor::from_container(cont),
}
}
pub(crate) fn from_tensor(tensor: Tensor<Cont>) -> Self {
ModQPolynomial { tensor }
}
/// Returns the number of coefficients in the polynomial.
///
/// # Example
///
/// ```
/// use concrete_core::backends::ntt::private::math::mod_q::ModQ;
/// use concrete_core::backends::ntt::private::math::polynomial::ModQPolynomial;
/// use concrete_core::prelude::PolynomialSize;
/// let modq_poly = ModQPolynomial::allocate(ModQ::new(0, 257u64), PolynomialSize(128));
/// assert_eq!(modq_poly.polynomial_size(), PolynomialSize(128));
/// ```
pub fn polynomial_size(&self) -> PolynomialSize
where
Self: AsRefTensor,
{
PolynomialSize(self.as_tensor().len())
}
/// Returns an iterator over borrowed polynomial coefficients.
///
/// # Note
///
/// We do not give any guarantee on the order of the coefficients.
///
/// # Example
///
/// ```
/// use concrete_core::backends::ntt::private::math::mod_q::ModQ;
/// use concrete_core::backends::ntt::private::math::polynomial::ModQPolynomial;
/// use concrete_core::prelude::PolynomialSize;
/// let modq_poly = ModQPolynomial::allocate(ModQ::new(0, 257u64), PolynomialSize(128));
/// for coef in modq_poly.coefficient_iter() {
/// assert_eq!(*coef, ModQ::new(0, 257u64));
/// }
/// assert_eq!(modq_poly.coefficient_iter().count(), 128);
/// ```
pub fn coefficient_iter<N: UnsignedInteger>(&self) -> impl Iterator<Item = &ModQ<N>>
where
Self: AsRefTensor<Element = ModQ<N>>,
{
self.as_tensor().iter()
}
/// Returns an iterator over mutably borrowed polynomial coefficients.
///
/// # Note
///
/// We do not give any guarantee on the order of the coefficients.
///
/// # Example
///
/// ```
/// use concrete_core::backends::ntt::private::math::mod_q::ModQ;
/// use concrete_core::backends::ntt::private::math::polynomial::ModQPolynomial;
/// use concrete_core::commons::math::tensor::AsRefTensor;
/// use concrete_core::prelude::PolynomialSize;
/// let mut modq_poly = ModQPolynomial::allocate(ModQ::new(0, 257u64), PolynomialSize(128));
/// for mut coef in modq_poly.coefficient_iter_mut() {
/// coef.set(259u64);
/// }
/// assert!(modq_poly.as_tensor().iter().all(|a| a.get() == 2u64));
/// assert_eq!(modq_poly.coefficient_iter_mut().count(), 128);
/// ```
pub fn coefficient_iter_mut<N: UnsignedInteger>(&mut self) -> impl Iterator<Item = &mut ModQ<N>>
where
Self: AsMutTensor<Element = ModQ<N>>,
{
self.as_mut_tensor().iter_mut()
}
}
impl<N, Cont> ModQPolynomial<Cont>
where
N: UnsignedInteger,
Cont: AsMutSlice<Element = ModQ<N>>,
{
pub fn from_polynomial<InCont>(&mut self, poly: &Polynomial<InCont>)
where
InCont: AsRefSlice<Element = N>,
{
for (modq, n) in self.coefficient_iter_mut().zip(poly.coefficient_iter()) {
modq.set(*n);
}
}
}
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct NttPolynomial<Cont>(ModQPolynomial<Cont>);
impl<N: UnsignedInteger> NttPolynomial<Vec<ModQ<N>>> {
/// Allocates a new empty NTT polynomial.
///
/// # Example
///
/// ```
/// use concrete_core::backends::ntt::private::math::mod_q::ModQ;
/// use concrete_core::backends::ntt::private::math::polynomial::NttPolynomial;
/// use concrete_core::prelude::PolynomialSize;
/// let ntt_poly = NttPolynomial::allocate(ModQ::new(0, 257u64), PolynomialSize(128));
/// assert_eq!(ntt_poly.polynomial_size(), PolynomialSize(128));
/// ```
pub fn allocate(value: ModQ<N>, coef_count: PolynomialSize) -> Self {
let modq_poly = ModQPolynomial::allocate(value, coef_count);
NttPolynomial(modq_poly)
}
}
impl<Cont> NttPolynomial<Cont> {
/// Creates an NTT polynomial from an existing container of values.
///
/// # Example
///
/// ```rust
/// use concrete_core::backends::ntt::private::math::mod_q::ModQ;
/// use concrete_core::backends::ntt::private::math::polynomial::NttPolynomial;
/// use concrete_core::prelude::PolynomialSize;
/// let mut vec: Vec<ModQ<u64>> = vec![ModQ::new(0, 257u64); 128];
/// let ntt_poly = NttPolynomial::from_container(vec.as_mut_slice());
/// assert_eq!(ntt_poly.polynomial_size(), PolynomialSize(128));
/// ```
pub fn from_container(cont: Cont) -> Self {
NttPolynomial(ModQPolynomial::from_container(cont))
}
pub(crate) fn from_tensor(tensor: Tensor<Cont>) -> Self {
NttPolynomial(ModQPolynomial::from_tensor(tensor))
}
/// Returns the number of coefficients in the polynomial.
///
/// # Example
///
/// ```
/// use concrete_core::backends::ntt::private::math::mod_q::ModQ;
/// use concrete_core::backends::ntt::private::math::polynomial::NttPolynomial;
/// use concrete_core::prelude::PolynomialSize;
/// let ntt_poly = NttPolynomial::allocate(ModQ::new(0, 257u64), PolynomialSize(128));
/// assert_eq!(ntt_poly.polynomial_size(), PolynomialSize(128));
/// ```
pub fn polynomial_size(&self) -> PolynomialSize
where
Self: AsRefTensor,
{
PolynomialSize(self.as_tensor().len())
}
/// Returns an iterator over borrowed polynomial coefficients.
///
/// # Note
///
/// We do not give any guarantee on the order of the coefficients.
///
/// # Example
///
/// ```
/// use concrete_core::backends::ntt::private::math::mod_q::ModQ;
/// use concrete_core::backends::ntt::private::math::polynomial::NttPolynomial;
/// use concrete_core::prelude::PolynomialSize;
/// let ntt_poly = NttPolynomial::allocate(ModQ::new(0, 257u64), PolynomialSize(128));
/// for coef in ntt_poly.coefficient_iter() {
/// assert_eq!(*coef, ModQ::new(0, 257u64));
/// }
/// assert_eq!(ntt_poly.coefficient_iter().count(), 128);
/// ```
pub fn coefficient_iter<N: UnsignedInteger>(&self) -> impl Iterator<Item = &ModQ<N>>
where
Self: AsRefTensor<Element = ModQ<N>>,
{
self.as_tensor().iter()
}
/// Returns an iterator over mutably borrowed polynomial coefficients.
///
/// # Note
///
/// We do not give any guarantee on the order of the coefficients.
///
/// # Example
///
/// ```
/// use concrete_core::backends::ntt::private::math::mod_q::ModQ;
/// use concrete_core::backends::ntt::private::math::polynomial::NttPolynomial;
/// use concrete_core::commons::math::tensor::AsRefTensor;
/// use concrete_core::prelude::PolynomialSize;
/// let mut ntt_poly = NttPolynomial::allocate(ModQ::new(0, 257u64), PolynomialSize(128));
/// for mut coef in ntt_poly.coefficient_iter_mut() {
/// coef.set(259u64);
/// }
/// assert!(ntt_poly.as_tensor().iter().all(|a| a.get() == 2u64));
/// assert_eq!(ntt_poly.coefficient_iter_mut().count(), 128);
/// ```
pub fn coefficient_iter_mut<N: UnsignedInteger>(&mut self) -> impl Iterator<Item = &mut ModQ<N>>
where
Self: AsMutTensor<Element = ModQ<N>>,
{
self.as_mut_tensor().iter_mut()
}
/// Adds the result of the element-wise product of two polynomials to the current polynomial:
/// $$
/// self\[i\] = self\[i\] + poly_1\[i\] * poly_2\[i\]
/// $$
///
/// # Example
///
/// ```rust
/// use concrete_core::backends::ntt::private::math::mod_q::ModQ;
/// use concrete_core::backends::ntt::private::math::polynomial::NttPolynomial;
/// use concrete_core::prelude::PolynomialSize;
/// let q = 257u64;
/// let mut npoly1 = NttPolynomial::allocate(ModQ::new(250, q), PolynomialSize(128));
/// let npoly2 = NttPolynomial::allocate(ModQ::new(3, q), PolynomialSize(128));
/// let npoly3 = NttPolynomial::allocate(ModQ::new(5, q), PolynomialSize(128));
/// npoly1.update_with_multiply_accumulate(&npoly2, &npoly3);
/// assert!(npoly1.coefficient_iter().all(|a| a.get() == 8u64));
/// ```
pub fn update_with_multiply_accumulate<PolyCont1, PolyCont2, N>(
&mut self,
poly_1: &NttPolynomial<PolyCont1>,
poly_2: &NttPolynomial<PolyCont2>,
) where
Self: AsMutTensor<Element = ModQ<N>>,
NttPolynomial<PolyCont1>: AsRefTensor<Element = ModQ<N>>,
NttPolynomial<PolyCont2>: AsRefTensor<Element = ModQ<N>>,
N: UnsignedInteger,
{
ck_dim_eq!(self.polynomial_size().0 => poly_1.polynomial_size().0, poly_2.polynomial_size().0);
for zip_args!(res, coef_1, coef_2) in zip!(
self.as_mut_tensor().iter_mut(),
poly_1.as_tensor().iter(),
poly_2.as_tensor().iter()
) {
*res += *coef_1 * *coef_2;
}
}
}
impl<Element, Cont> AsRefTensor for NttPolynomial<Cont>
where
Cont: AsRefSlice<Element = Element>,
{
type Element = Element;
type Container = Cont;
fn as_tensor(&self) -> &Tensor<Self::Container> {
&self.0.tensor
}
}
impl<Element, Cont> AsMutTensor for NttPolynomial<Cont>
where
Cont: AsMutSlice<Element = Element>,
{
type Element = Element;
type Container = Cont;
fn as_mut_tensor(&mut self) -> &mut Tensor<<Self as AsMutTensor>::Container> {
&mut self.0.tensor
}
}
impl<Cont> IntoTensor for NttPolynomial<Cont>
where
Cont: AsRefSlice,
{
type Element = <Cont as AsRefSlice>::Element;
type Container = Cont;
fn into_tensor(self) -> Tensor<Self::Container> {
self.0.tensor
}
}