primitives/algebra/ops/
wide_ops.rs

1// Trait for types which allows transformation to an extended space in which arithmetic operations
2// have better performance. E.g. field multiply and accumulate with lazy modulus reduction.
3pub trait IntoWide<T = Self> {
4    // Projection to extended space
5    fn to_wide(&self) -> T;
6
7    // Zero in extended space
8    fn zero_wide() -> T;
9}
10
11// Reduction from extended space to input space
12pub trait ReduceWide<T = Self> {
13    fn reduce_mod_order(a: T) -> Self;
14}
15
16/// Lazy multiplication and accumulate. Inputs are multiplied and no reduction back to input space
17/// is made. The result is in an extended space.
18pub trait MulAccReduce<Lhs = Self, Rhs = Self>:
19    IntoWide<Self::WideType> + ReduceWide<Self::WideType>
20{
21    type WideType;
22
23    fn mul_acc(acc: &mut Self::WideType, a: Lhs, b: Rhs);
24
25    #[inline]
26    fn to_wide(&self) -> Self::WideType {
27        <Self as IntoWide<Self::WideType>>::to_wide(self)
28    }
29
30    #[inline]
31    fn zero_wide() -> Self::WideType {
32        <Self as IntoWide<Self::WideType>>::zero_wide()
33    }
34}
35
36/// Lazy accumulate. Input is added to accumulator and no reduction back to input space is made. The
37/// result is in an extended space.
38pub trait AccReduce<T = Self>: ReduceWide<Self::WideType> + IntoWide<Self::WideType> {
39    type WideType;
40
41    fn acc(acc: &mut Self::WideType, a: T);
42
43    #[inline]
44    fn to_wide(&self) -> Self::WideType {
45        <Self as IntoWide<Self::WideType>>::to_wide(self)
46    }
47
48    #[inline]
49    fn zero_wide() -> Self::WideType {
50        <Self as IntoWide<Self::WideType>>::zero_wide()
51    }
52}