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
use std::rc::Rc;
use crate::Exceptions;
use super::ModulusGF;
#[derive(Clone, Debug)]
pub struct ModulusPoly {
field: Rc<&'static ModulusGF>,
coefficients: Vec<u32>,
}
impl ModulusPoly {
pub fn new(
field: Rc<&'static ModulusGF>,
coefficients: Vec<u32>,
) -> Result<ModulusPoly, Exceptions> {
if coefficients.is_empty() {
return Err(Exceptions::IllegalArgumentException("".to_owned()));
}
let orig_coefs = coefficients.clone();
let mut coefficients = coefficients;
let coefficientsLength = coefficients.len();
if coefficientsLength > 1 && coefficients[0] == 0 {
let mut firstNonZero = 1;
while firstNonZero < coefficientsLength && coefficients[firstNonZero] == 0 {
firstNonZero += 1;
}
if firstNonZero == coefficientsLength {
coefficients = vec![0];
} else {
coefficients = vec![0u32; coefficientsLength - firstNonZero];
coefficients[..].copy_from_slice(&&orig_coefs[firstNonZero..]);
}
} else {
coefficients = coefficients;
}
Ok(ModulusPoly {
field: field.clone(),
coefficients: coefficients,
})
}
pub fn getCoefficients(&self) -> &[u32] {
&self.coefficients
}
pub fn getDegree(&self) -> u32 {
self.coefficients.len() as u32 - 1
}
pub fn isZero(&self) -> bool {
self.coefficients[0] == 0
}
pub fn getCoefficient(&self, degree: usize) -> u32 {
self.coefficients[self.coefficients.len() - 1 - degree]
}
pub fn evaluateAt(&self, a: u32) -> u32 {
if a == 0 {
return self.getCoefficient(0);
}
if a == 1 {
let mut result = 0;
for coefficient in self.coefficients.iter() {
result = self.field.add(result, *coefficient);
}
return result;
}
let mut result = self.coefficients[0];
let size = self.coefficients.len();
for i in 1..size {
result = self
.field
.add(self.field.multiply(a, result), self.coefficients[i]);
}
return result;
}
pub fn add(&self, other: Rc<ModulusPoly>) -> Result<Rc<ModulusPoly>, Exceptions> {
if self.field != other.field {
return Err(Exceptions::IllegalArgumentException(
"ModulusPolys do not have same ModulusGF field".to_owned(),
));
}
if self.isZero() {
return Ok(other);
}
if other.isZero() {
return Ok(Rc::new(self.clone()));
}
let mut smallerCoefficients = &self.coefficients;
let mut largerCoefficients = &other.coefficients;
if smallerCoefficients.len() > largerCoefficients.len() {
std::mem::swap(&mut smallerCoefficients, &mut largerCoefficients);
}
let mut sumDiff = vec![0032; largerCoefficients.len()];
let lengthDiff = largerCoefficients.len() - smallerCoefficients.len();
sumDiff[..lengthDiff].copy_from_slice(&largerCoefficients[..lengthDiff]);
for i in lengthDiff..largerCoefficients.len() {
sumDiff[i] = self
.field
.add(smallerCoefficients[i - lengthDiff], largerCoefficients[i]);
}
Ok(Rc::new(
ModulusPoly::new(self.field.clone(), sumDiff)
.expect("should always generate with known goods"),
))
}
pub fn subtract(&self, other: Rc<ModulusPoly>) -> Result<Rc<ModulusPoly>, Exceptions> {
if self.field != other.field {
return Err(Exceptions::IllegalArgumentException(
"ModulusPolys do not have same ModulusGF field".to_owned(),
));
}
if other.isZero() {
return Ok(Rc::new(self.clone()));
};
self.add(other.negative().clone())
}
pub fn multiply(&self, other: Rc<ModulusPoly>) -> Result<Rc<ModulusPoly>, Exceptions> {
if !(self.field == other.field) {
return Err(Exceptions::IllegalArgumentException(
"ModulusPolys do not have same ModulusGF field".to_owned(),
));
}
if self.isZero() || other.isZero() {
return Ok(Self::getZero(self.field.clone()).clone());
}
let aCoefficients = &self.coefficients;
let aLength = aCoefficients.len();
let bCoefficients = &other.coefficients;
let bLength = bCoefficients.len();
let mut product = vec![0u32; aLength + bLength - 1];
for i in 0..aLength {
let aCoeff = aCoefficients[i];
for j in 0..bLength {
product[i + j] = self.field.add(
product[i + j],
self.field.multiply(aCoeff, bCoefficients[j]),
);
}
}
Ok(Rc::new(
ModulusPoly::new(self.field.clone(), product)
.expect("should always generate with known goods"),
))
}
pub fn negative(&self) -> Rc<ModulusPoly> {
let size = self.coefficients.len();
let mut negativeCoefficients = vec![0u32; size];
for i in 0..size {
negativeCoefficients[i] = self.field.subtract(0, self.coefficients[i]);
}
Rc::new(
ModulusPoly::new(self.field.clone(), negativeCoefficients)
.expect("should always generate with known goods"),
)
}
pub fn multiplyByScaler(&self, scalar: u32) -> Rc<ModulusPoly> {
if scalar == 0 {
return Self::getZero(self.field.clone()).clone();
}
if scalar == 1 {
return Rc::new(self.clone());
}
let size = self.coefficients.len();
let mut product = vec![0u32; size];
for i in 0..size {
product[i] = self.field.multiply(self.coefficients[i], scalar);
}
Rc::new(
ModulusPoly::new(self.field.clone(), product)
.expect("should always generate with known goods"),
)
}
pub fn multiplyByMonomial(&self, degree: usize, coefficient: u32) -> Rc<ModulusPoly> {
if coefficient == 0 {
return Self::getZero(self.field.clone()).clone();
}
let size = self.coefficients.len();
let mut product = vec![0u32; size + degree];
for i in 0..size {
product[i] = self.field.multiply(self.coefficients[i], coefficient);
}
Rc::new(
ModulusPoly::new(self.field.clone(), product)
.expect("should always generate with known goods"),
)
}
pub fn getZero(field: Rc<&'static ModulusGF>) -> Rc<ModulusPoly> {
Rc::new(
ModulusPoly::new(field.clone(), vec![0])
.expect("should always generate with known goods"),
)
}
pub fn getOne(field: Rc<&'static ModulusGF>) -> Rc<ModulusPoly> {
Rc::new(
ModulusPoly::new(field.clone(), vec![1])
.expect("should always generate with known goods"),
)
}
pub fn buildMonomial(
field: Rc<&'static ModulusGF>,
degree: usize,
coefficient: u32,
) -> Rc<ModulusPoly> {
if coefficient == 0 {
return Self::getZero(field);
}
let mut coefficients = vec![0_u32; degree + 1];
coefficients[0] = coefficient;
Rc::new(
ModulusPoly::new(field.clone(), coefficients)
.expect("should always generate with known goods"),
)
}
}