#[cfg(not(feature = "std"))]
use alloc::boxed::Box;
use core::{
fmt::Debug,
ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign},
};
use generic_array::{
ArrayLength, GenericArray,
typenum::{Prod, U3},
};
pub(crate) mod large_fields;
pub(crate) mod large_fields_constants;
pub(crate) mod small_fields;
#[cfg(all(feature = "opt-simd", any(target_arch = "x86", target_arch = "x86_64")))]
#[allow(unused_unsafe)]
pub(crate) mod x86_simd_large_fields;
pub(crate) use large_fields::{
BigGaloisField, ByteCombine, ByteCombineConstants, ByteCombineSquaredConstants, FromBit,
Sigmas, SumPoly,
};
#[cfg(not(all(
feature = "opt-simd",
target_feature = "avx2",
target_feature = "pclmulqdq"
)))]
pub(crate) use large_fields::{GF128, GF192, GF256};
pub(crate) use small_fields::{GF8, GF64};
#[cfg(all(
feature = "opt-simd",
target_feature = "avx2",
target_feature = "pclmulqdq"
))]
pub(crate) use x86_simd_large_fields::{GF128, GF192, GF256};
pub(crate) trait Field:
Sized
+ Default
+ Add<Self, Output = Self>
+ AddAssign
+ Sub<Self, Output = Self>
+ SubAssign
+ Neg<Output = Self>
+ Mul<Self, Output = Self>
+ MulAssign
+ PartialEq
+ Eq
{
const ZERO: Self;
const ONE: Self;
type Length: ArrayLength;
fn as_bytes(&self) -> GenericArray<u8, Self::Length>;
}
pub(crate) trait BaseField: Field<Length: Mul<U3, Output: ArrayLength>> {
type ExtenionField: ExtensionField<BaseField = Self, Length = Prod<Self::Length, U3>>;
}
pub(crate) trait Double {
type Output;
fn double(self) -> Self::Output;
}
pub(crate) trait Square {
type Output;
fn square(self) -> Self::Output;
}
impl<F, L> Square for GenericArray<F, L>
where
F: Square,
L: ArrayLength,
{
type Output = GenericArray<F::Output, L>;
fn square(self) -> Self::Output {
self.into_iter().map(|x| x.square()).collect()
}
}
impl<F, L> Square for &GenericArray<F, L>
where
F: Square + Clone,
L: ArrayLength,
{
type Output = GenericArray<F::Output, L>;
fn square(self) -> Self::Output {
self.iter().cloned().map(|x| x.square()).collect()
}
}
impl<F, L> Square for &Box<GenericArray<F, L>>
where
F: Square + Clone,
L: ArrayLength,
{
type Output = Box<GenericArray<F::Output, L>>;
fn square(self) -> Self::Output {
self.iter().cloned().map(|x| x.square()).collect()
}
}
pub(crate) trait ExtensionField:
Sized
+ Default
+ Add<Self, Output = Self>
+ AddAssign
+ Sub<Self, Output = Self>
+ SubAssign
+ Neg<Output = Self>
+ Mul<Self::BaseField, Output = Self>
+ for<'a> Mul<&'a Self::BaseField, Output = Self>
+ for<'a> Add<&'a Self, Output = Self>
+ for<'a> From<&'a [u8]>
+ PartialEq
+ Eq
+ Debug
{
type Length: ArrayLength;
type BaseField: Field;
fn as_bytes(&self) -> GenericArray<u8, Self::Length>;
}