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
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>
{ }