cryptix-field 0.1.0

A library for group, ring and field arithmetics based on cryptix-bigint
Documentation
use core::ops::{Add, Sub};

use cryptix_bigint::{
    ops::BigIntOps, 
    property::IsBigInt
};

use crate::{Modular, Element};

use super::*;

impl<I, M> AbelianGroup for Element<I, M> 
where
    M: Modular<I>,
    Self: Group + CommunicativeAdd
{ }

impl<I, M> Group for Element<I, M> 
where 
    M: Modular<I>,
    I: BigIntOps
{ }

impl<I, M> Add for Element<I, M> 
where
    M: Modular<I>,
    I: BigIntOps
{
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        let (res, _) = self.0 + rhs.0;
        /* # Safety
         * 
         * This is safe because 0 <= self < P and 0 <= rhs < P, so 0 <= self + rhs < 2P
         */
        Self::new_unchecked(if res >= M::P { (res - M::P).0 } else { res })
    }
}

impl<I, M> Sub for Element<I, M>
where
    M: Modular<I>,
    I: BigIntOps
{
    type Output = Self;

    fn sub(self, rhs: Self) -> Self::Output {
        let (res, _) = self.0 - rhs.0;
        /* # Safety
         * 
         * This is safe because 0 <= self < P and 0 <= rhs < P, so -P < self - rhs < P.
         * 
         * When -P < self - rhs < 0, since the result is wrapped, the comparation res >= M::P will be true
         */
        Self::new_unchecked(if res >= M::P { (res + M::P).0 } else { res })
    }
}

impl<I, M> AddIdentity for Element<I, M> 
where
    M: Modular<I>,
    I: BigIntOps + IsBigInt
{
    const ADD_IDENTITY: Self = Self(I::ZERO, core::marker::PhantomData);
}

impl<I, M> Neg for Element<I, M> 
where
    M: Modular<I>,
    Self: Sub<Output = Self> + AddIdentity 
{ 
    type Output = Self;
    
    fn neg(self) -> Self::Output {
        Self::ADD_IDENTITY - self
    }
 }

/// # Safety
/// 
/// Element is backed by biguint, which is associative under addition
impl<I, M> AssociativeAdd for Element<I, M> 
where 
    M: Modular<I>,
    Self: Add<Output = Self> 
{ }

/// # Safety
/// 
/// Element is backed by biguint, which is communicative under addition
impl<I, M> CommunicativeAdd for Element<I, M> 
where
    M: Modular<I>,
    Self: Add<Output = Self>
{ }