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
use crate::{
field::polynomial::Polynomial,
generic_array_length::AnyArrayLength,
ring::{FiniteRing, IsSubRingOf},
};
use generic_array::{ArrayLength, GenericArray};
use std::ops::{Div, DivAssign};
pub trait FiniteField: FiniteRing + DivAssign<Self> + Div<Self, Output = Self> {
type PrimeField: PrimeFiniteField + IsSubFieldOf<Self>;
fn polynomial_modulus() -> Polynomial<Self::PrimeField>;
const GENERATOR: Self;
type NumberOfBitsInBitDecomposition: ArrayLength<bool> + ArrayLength<F2>;
fn bit_decomposition(&self) -> GenericArray<bool, Self::NumberOfBitsInBitDecomposition>;
fn inverse(&self) -> Self;
#[inline]
fn decompose<T: FiniteField + IsSubFieldOf<Self>>(
&self,
) -> GenericArray<T, DegreeModulo<T, Self>> {
T::decompose_superfield(self)
}
#[inline]
fn from_subfield<T: FiniteField + IsSubFieldOf<Self>>(
arr: &GenericArray<T, DegreeModulo<T, Self>>,
) -> Self {
T::form_superfield(arr)
}
}
pub type Degree<FE> = DegreeModulo<<FE as FiniteField>::PrimeField, FE>;
pub type DegreeModulo<A, B> = <A as IsSubFieldOf<B>>::DegreeModulo;
pub trait IsSubFieldOf<FE: FiniteField>: FiniteField + IsSubRingOf<FE> {
type DegreeModulo: ArrayLength<Self> + AnyArrayLength;
fn decompose_superfield(fe: &FE) -> GenericArray<Self, Self::DegreeModulo>;
fn form_superfield(components: &GenericArray<Self, Self::DegreeModulo>) -> FE;
}
impl<FE: FiniteField> IsSubFieldOf<FE> for FE {
type DegreeModulo = generic_array::typenum::U1;
#[inline]
fn decompose_superfield(fe: &FE) -> GenericArray<Self, Self::DegreeModulo> {
GenericArray::from([*fe])
}
#[inline]
fn form_superfield(components: &GenericArray<Self, Self::DegreeModulo>) -> FE {
components[0]
}
}
pub trait PrimeFiniteField:
FiniteField<PrimeField = Self>
+ IsSubFieldOf<Self, DegreeModulo = generic_array::typenum::U1>
+ std::convert::TryFrom<u128>
{
}
#[cfg(test)]
#[macro_use]
mod test_utils;
#[cfg(test)]
macro_rules! call_with_big_finite_fields {
($f:ident $(, $arg:expr)* $(,)?) => {{
$f::<$crate::field::F61p>($($arg),*);
$f::<$crate::field::F64b>($($arg),*);
$f::<$crate::field::F128b>($($arg),*);
$f::<$crate::field::F40b>($($arg),*);
$f::<$crate::field::F45b>($($arg),*);
$f::<$crate::field::F56b>($($arg),*);
$f::<$crate::field::F63b>($($arg),*);
#[cfg(feature = "ff")]
$f::<$crate::field::F128p>($($arg),*);
#[cfg(feature = "ff")]
$f::<$crate::field::F384p>($($arg),*);
#[cfg(feature = "ff")]
$f::<$crate::field::F384q>($($arg),*);
}};
}
#[cfg(test)]
macro_rules! call_with_finite_field {
($f:ident $(, $arg:expr)* $(,)?) => {{
call_with_big_finite_fields!($f$(,$arg)*);
$f::<$crate::field::F2>($($arg),*);
}};
}
macro_rules! field_ops {
($f:ident $($tt:tt)*) => {
crate::ring::ring_ops!($f $($tt)*);
$crate::ops::binop!(Div, div, std::ops::DivAssign::div_assign, $f);
$crate::ops::assign_op!(DivAssign, div_assign, $f);
impl<'a> std::ops::DivAssign<&'a $f> for $f {
fn div_assign(&mut self, rhs: &Self) {
*self *= rhs.inverse();
}
}
};
}
pub(crate) fn standard_bit_decomposition<L: ArrayLength<bool>>(
bits: u128,
) -> GenericArray<bool, L> {
let mut out: GenericArray<bool, L> = Default::default();
for (i, dst) in out.iter_mut().enumerate() {
*dst = (bits & (1 << (i as u128))) != 0;
}
out
}
mod f2;
pub use f2::F2;
mod f128b;
pub use f128b::F128b;
mod f64b;
pub use f64b::F64b;
mod small_binary_fields;
pub use small_binary_fields::{F40b, F45b, F56b, F63b, SmallBinaryField};
mod f61p;
pub use f61p::F61p;
#[cfg(feature = "ff")]
mod prime_field_using_ff;
#[cfg(feature = "ff")]
pub use prime_field_using_ff::{F128p, F256p, F384p, F384q, F400p, Fbls12381, Fbn254};
#[cfg(feature = "ff")]
mod f2e19x3e26;
#[cfg(feature = "ff")]
pub use f2e19x3e26::F2e19x3e26;
pub mod polynomial;
pub mod fft;