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
#[derive(Debug, thiserror::Error)]
pub enum FiniteFieldError {
#[error("input sizes do not match")]
InputSizeMismatch,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Field(u32);
pub const MODULUS: u32 = 4293918721;
pub(crate) const GENERATOR: u32 = 3925978153;
pub(crate) const N_ROOTS: u32 = 1 << 20;
impl std::ops::Add for Field {
type Output = Field;
fn add(self, rhs: Self) -> Self {
self - Field(MODULUS - rhs.0)
}
}
impl std::ops::AddAssign for Field {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs;
}
}
impl std::ops::Sub for Field {
type Output = Field;
fn sub(self, rhs: Self) -> Self {
let l = self.0;
let r = rhs.0;
if l >= r {
Field(l - r)
} else {
Field(MODULUS - r + l)
}
}
}
impl std::ops::SubAssign for Field {
fn sub_assign(&mut self, rhs: Self) {
*self = *self - rhs;
}
}
impl std::ops::Mul for Field {
type Output = Field;
#[allow(clippy::suspicious_arithmetic_impl)]
fn mul(self, rhs: Self) -> Self {
let l = self.0 as u64;
let r = rhs.0 as u64;
let mul = l * r;
Field((mul % (MODULUS as u64)) as u32)
}
}
impl std::ops::MulAssign for Field {
fn mul_assign(&mut self, rhs: Self) {
*self = *self * rhs;
}
}
impl std::ops::Div for Field {
type Output = Field;
#[allow(clippy::suspicious_arithmetic_impl)]
fn div(self, rhs: Self) -> Self {
self * rhs.inv()
}
}
impl std::ops::DivAssign for Field {
fn div_assign(&mut self, rhs: Self) {
*self = *self / rhs;
}
}
impl Field {
pub fn pow(self, exp: Self) -> Self {
let mut base = self;
let mut exp = exp.0;
let mut result: Field = Field(1);
while exp > 0 {
while (exp & 1) == 0 {
exp /= 2;
base *= base;
}
exp -= 1;
result *= base;
}
result
}
pub fn inv(self) -> Self {
let mut x1: i32 = 1;
let mut a1: u32 = self.0;
let mut x0: i32 = 0;
let mut a2: u32 = MODULUS;
let mut q: u32 = 0;
while a2 != 0 {
let x2 = x0 - (q as i32) * x1;
x0 = x1;
let a0 = a1;
x1 = x2;
a1 = a2;
q = a0 / a1;
a2 = a0 - q * a1;
}
if x1 < 0 {
let (r, _) = MODULUS.overflowing_add(x1 as u32);
Field(r)
} else {
Field(x1 as u32)
}
}
}
impl From<u32> for Field {
fn from(x: u32) -> Self {
Field(x % MODULUS)
}
}
impl From<Field> for u32 {
fn from(x: Field) -> Self {
x.0
}
}
impl PartialEq<u32> for Field {
fn eq(&self, rhs: &u32) -> bool {
self.0 == *rhs
}
}
impl std::fmt::Display for Field {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
#[test]
fn test_arithmetic() {
use rand::prelude::*;
assert_eq!(Field(MODULUS - 1) + Field(1), 0);
assert_eq!(Field(MODULUS - 2) + Field(2), 0);
assert_eq!(Field(MODULUS - 2) + Field(3), 1);
assert_eq!(Field(1) + Field(1), 2);
assert_eq!(Field(2) + Field(MODULUS), 2);
assert_eq!(Field(3) + Field(MODULUS - 1), 2);
assert_eq!(Field(0) - Field(1), MODULUS - 1);
assert_eq!(Field(1) - Field(2), MODULUS - 1);
assert_eq!(Field(15) - Field(3), 12);
assert_eq!(Field(1) - Field(1), 0);
assert_eq!(Field(2) - Field(MODULUS), 2);
assert_eq!(Field(3) - Field(MODULUS - 1), 4);
for _ in 0..100 {
let f = Field::from(random::<u32>());
let g = Field::from(random::<u32>());
assert_eq!(f + g - f - g, 0);
assert_eq!(f + g - g, f);
assert_eq!(f + g - f, g);
}
assert_eq!(Field(35) * Field(123), 4305);
assert_eq!(Field(1) * Field(MODULUS), 0);
assert_eq!(Field(0) * Field(123), 0);
assert_eq!(Field(123) * Field(0), 0);
assert_eq!(Field(123123123) * Field(123123123), 1237630077);
assert_eq!(Field(35) / Field(5), 7);
assert_eq!(Field(35) / Field(0), 0);
assert_eq!(Field(0) / Field(5), 0);
assert_eq!(Field(1237630077) / Field(123123123), 123123123);
assert_eq!(Field(0).inv(), 0);
let uniform = rand::distributions::Uniform::from(1..MODULUS);
let mut rng = thread_rng();
for _ in 0..100 {
let f = Field(uniform.sample(&mut rng));
assert_eq!(f * f.inv(), 1);
assert_eq!(f.inv() * f, 1);
}
assert_eq!(Field(2).pow(3.into()), 8);
assert_eq!(Field(3).pow(9.into()), 19683);
assert_eq!(Field(51).pow(27.into()), 3760729523);
assert_eq!(Field(432).pow(0.into()), 1);
assert_eq!(Field(0).pow(123.into()), 0);
}
pub fn merge_vector(
accumulator: &mut [Field],
other_vector: &[Field],
) -> Result<(), FiniteFieldError> {
if accumulator.len() != other_vector.len() {
return Err(FiniteFieldError::InputSizeMismatch);
}
for (a, o) in accumulator.iter_mut().zip(other_vector.iter()) {
*a += *o;
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::util::vector_with_length;
use assert_matches::assert_matches;
#[test]
fn test_accumulate() {
let mut lhs = vector_with_length(10);
lhs.iter_mut().for_each(|f| *f = Field(1));
let mut rhs = vector_with_length(10);
rhs.iter_mut().for_each(|f| *f = Field(2));
merge_vector(&mut lhs, &rhs).unwrap();
lhs.iter().for_each(|f| assert_eq!(*f, Field(3)));
rhs.iter().for_each(|f| assert_eq!(*f, Field(2)));
let wrong_len = vector_with_length(9);
let result = merge_vector(&mut lhs, &wrong_len);
assert_matches!(result, Err(FiniteFieldError::InputSizeMismatch));
}
}