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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//! This crate provides abstration of prime fields

#![cfg_attr(not(feature = "std"), no_std)]
#![allow(incomplete_features)]
#![feature(generic_const_exprs)]
#![feature(core_intrinsics)]

use core::{marker::PhantomData, fmt::Debug, ops::Rem};

pub mod group;
pub mod ring;
pub mod field;

/// An element in mod n ring, the type parameter I should be an integer
/// the modular p is embedded in the type parameter M, it's typically a ZST
pub struct Element<I, M: Modular<I>>(pub I, PhantomData<M>);

/// Be careful when implementing this trait. The modular **must be** a prime, or all mathmatical
/// assumptions in this crate will be broken.
/// 
/// Implement this trait to embed the modular into a type
pub trait Modular<E> {
    /// this modular P must be a prime
    const P: E;
}

/// # Safety
/// 
/// This trait is safe to implement only when `<Self as Modular>::P` is indeed a prime
/// 
/// this trait indicates if the modular is prime
pub trait PrimeModular<E>: Modular<E> { }

/// # Safety
/// 
/// This trait is safe to implement only when `<Self as Modular>::P` is indeed an odd number
/// 
/// this trait indicates if the modular is odd
pub trait OddModular<E>: Modular<E> { }

impl<I: Copy, M: Modular<I>> Clone for Element<I, M> {
    fn clone(&self) -> Self {
        Element(self.0, PhantomData)
    }
}

impl<I: Copy, M: Modular<I>> Copy for Element<I, M> { }

impl<I, M: Modular<I>> From<I> for Element<I, M> 
where
    I: Rem<Output = I>
{
    /// convert a integer to a mod p field element, this value will be converted 
    /// into the canonical representative
    fn from(value: I) -> Self {
        Element::new(value)
    }
}

impl<I, M: Modular<I>> Element<I, M> 
where
    I: Rem<Output = I>
{
    pub fn new(value: I) -> Self {
        // TODO: add a debug assertion that checks if P is prime
        Element(value % M::P, PhantomData)
    }
}

impl<I, M: Modular<I>> Element<I, M> {
    /// # Safety
    /// 
    /// This function is designed to be used in const environment to directly convert
    /// a canonical representative into mod p field element. So we made an assumption
    /// that the parameter `value` must lie between 0 to p - 1. Creating a field element 
    /// with non-canonical representative can lead to unexpected result when computing
    pub const fn new_unchecked(value: I) -> Self {
        // TODO: add a debug assertion that checks if P is prime and 
        // value is indeed a canonical representative
        Element(value, PhantomData)
    }
}

impl<I, M: Modular<I>> Element<I, M> {
    /// value of the representative
    pub const fn repr(&self) -> &I {
        &self.0
    }

    pub const fn modular() -> I {
        M::P
    }
}

impl<I: Debug, M: Modular<I>> Debug for Element<I, M> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        self.0.fmt(f)
    }
}

impl<I: PartialEq, M: Modular<I>> PartialEq for Element<I, M> {
    fn eq(&self, other: &Self) -> bool {
        // we only compare the value here since is guaranteed to be in group
        self.0.eq(&other.0)
    }
}

impl<I: Eq, M: Modular<I>> Eq for Element<I, M> { }

impl<I: PartialOrd, M: Modular<I>> PartialOrd for Element<I, M> {
    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
        self.0.partial_cmp(&other.0)
    }
}

impl<I: Ord, M: Modular<I>> Ord for Element<I, M> {
    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
        self.0.cmp(&other.0)
    }
}