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
use crate::field::{polynomial::Polynomial, FiniteField, PrimeFiniteField};
use crate::ring::FiniteRing;
use crate::serialization::{BiggerThanModulus, CanonicalSerialize};
use generic_array::GenericArray;
use rand_core::RngCore;
use std::ops::{AddAssign, MulAssign, SubAssign};
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq};
#[derive(Clone, Copy, Eq, Debug, Hash)]
pub struct F61p(u64);
const MODULUS: u64 = (1 << 61) - 1;
impl ConstantTimeEq for F61p {
#[inline]
fn ct_eq(&self, other: &Self) -> Choice {
self.0.ct_eq(&other.0)
}
}
impl ConditionallySelectable for F61p {
#[inline]
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
F61p(u64::conditional_select(&a.0, &b.0, choice))
}
}
impl FiniteRing for F61p {
#[inline]
fn from_uniform_bytes(x: &[u8; 16]) -> Self {
F61p(reduce(
u64::from_le_bytes(<[u8; 8]>::try_from(&x[0..8]).unwrap()) as u128,
))
}
#[inline]
fn random<R: RngCore + ?Sized>(rng: &mut R) -> Self {
F61p(reduce(rng.next_u64() as u128))
}
const ZERO: Self = F61p(0);
const ONE: Self = F61p(1);
}
impl CanonicalSerialize for F61p {
type Serializer = crate::serialization::ByteElementSerializer<Self>;
type Deserializer = crate::serialization::ByteElementDeserializer<Self>;
type ByteReprLen = generic_array::typenum::U8;
type FromBytesError = BiggerThanModulus;
#[inline]
fn from_bytes(
bytes: &GenericArray<u8, Self::ByteReprLen>,
) -> Result<Self, Self::FromBytesError> {
let buf = <[u8; 8]>::from(*bytes);
let raw = u64::from_le_bytes(buf);
if raw < MODULUS {
Ok(F61p(raw))
} else {
Err(BiggerThanModulus)
}
}
#[inline]
fn to_bytes(&self) -> GenericArray<u8, Self::ByteReprLen> {
self.0.to_le_bytes().into()
}
}
impl FiniteField for F61p {
type PrimeField = Self;
const GENERATOR: Self = F61p(37);
fn polynomial_modulus() -> Polynomial<Self::PrimeField> {
Polynomial::x()
}
type NumberOfBitsInBitDecomposition = generic_array::typenum::U61;
fn bit_decomposition(&self) -> GenericArray<bool, Self::NumberOfBitsInBitDecomposition> {
super::standard_bit_decomposition(u128::from(self.0))
}
fn inverse(&self) -> Self {
if *self == Self::ZERO {
panic!("Zero cannot be inverted");
}
self.pow_var_time(u128::from(MODULUS) - 2)
}
}
#[inline]
fn reduce(k: u128) -> u64 {
let i = (k & u128::from(MODULUS)) + (k >> 61);
let flag = (i < u128::from(MODULUS)) as u128;
let operand = flag.wrapping_sub(1) & u128::from(MODULUS);
(i - operand) as u64
}
impl AddAssign<&F61p> for F61p {
#[inline]
fn add_assign(&mut self, rhs: &Self) {
let a = self.0 as u128;
let b = rhs.0 as u128;
self.0 = reduce(a + b);
}
}
impl SubAssign<&F61p> for F61p {
#[inline]
fn sub_assign(&mut self, rhs: &Self) {
let a = u128::from(self.0) + u128::from(MODULUS);
let b = u128::from(rhs.0);
self.0 = reduce(a - b);
}
}
impl MulAssign<&F61p> for F61p {
#[inline]
fn mul_assign(&mut self, rhs: &Self) {
self.0 = reduce(u128::from(self.0) * u128::from(rhs.0));
}
}
impl std::iter::Sum for F61p {
#[inline]
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
let mut out: u128 = 0;
for e in iter {
out += u128::from(e.0);
}
return F61p(reduce(out));
}
}
impl TryFrom<u128> for F61p {
type Error = BiggerThanModulus;
fn try_from(value: u128) -> Result<Self, Self::Error> {
if value < MODULUS.into() {
Ok(F61p(value.try_into().unwrap()))
} else {
Err(BiggerThanModulus)
}
}
}
impl PrimeFiniteField for F61p {}
field_ops!(F61p, SUM_ALREADY_DEFINED);
#[cfg(test)]
mod tests {
use super::*;
use proptest::prelude::*;
test_field!(test_field, crate::field::F61p);
#[cfg(test)]
proptest! {
#[test]
fn test_reduce(x in 0u128..((1 << (2 * 61))-1)) {
assert_eq!(u128::from(reduce(x)), x % u128::from(MODULUS));
}
}
#[test]
fn test_sum_overflow() {
let neg1 = F61p::ZERO - F61p::ONE;
let x = [neg1; 2];
assert_eq!(x.iter().map(|x| *x).sum::<F61p>(), neg1 + neg1);
}
}