arcium-primitives 0.4.2

Arcium primitives
Documentation
// Trait for types which allows transformation to an extended space in which arithmetic operations
// have better performance. E.g. field multiply and accumulate with lazy modulus reduction.
pub trait IntoWide<T = Self> {
    // Projection to extended space
    fn to_wide(&self) -> T;

    // Zero in extended space
    fn zero_wide() -> T;
}

// Reduction from extended space to input space
pub trait ReduceWide<T = Self> {
    fn reduce_mod_order(a: T) -> Self;
}

/// Lazy multiplication and accumulate. Inputs are multiplied and no reduction back to input space
/// is made. The result is in an extended space.
pub trait MulAccReduce<Lhs = Self, Rhs = Self>:
    IntoWide<Self::WideType> + ReduceWide<Self::WideType>
{
    type WideType;

    fn mul_acc(acc: &mut Self::WideType, a: Lhs, b: Rhs);

    #[inline]
    fn to_wide(&self) -> Self::WideType {
        <Self as IntoWide<Self::WideType>>::to_wide(self)
    }

    #[inline]
    fn zero_wide() -> Self::WideType {
        <Self as IntoWide<Self::WideType>>::zero_wide()
    }
}

/// Lazy accumulate. Input is added to accumulator and no reduction back to input space is made. The
/// result is in an extended space.
pub trait AccReduce<T = Self>: ReduceWide<Self::WideType> + IntoWide<Self::WideType> {
    type WideType;

    fn acc(acc: &mut Self::WideType, a: T);

    #[inline]
    fn to_wide(&self) -> Self::WideType {
        <Self as IntoWide<Self::WideType>>::to_wide(self)
    }

    #[inline]
    fn zero_wide() -> Self::WideType {
        <Self as IntoWide<Self::WideType>>::zero_wide()
    }
}