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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use super::*;

#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub struct Z251 {
    pub inner: u8,
}

impl Add for Z251 {
    type Output = Z251;

    fn add(self, rhs: Z251) -> Self::Output {
        let sum: u16 = self.inner as u16 + rhs.inner as u16;

        Z251 {
            inner: (sum % 251) as u8,
        }
    }
}

impl Neg for Z251 {
    type Output = Z251;

    fn neg(self) -> Self::Output {
        Z251 {
            inner: 251 - self.inner,
        }
    }
}

impl Sub for Z251 {
    type Output = Z251;

    fn sub(self, rhs: Z251) -> Self::Output {
        self + -rhs
    }
}

impl Mul for Z251 {
    type Output = Z251;

    fn mul(self, rhs: Z251) -> Self::Output {
        let product = (self.inner as u16) * (rhs.inner as u16);

        Z251 {
            inner: (product % 251) as u8,
        }
    }
}

impl Div for Z251 {
    type Output = Z251;

    fn div(self, rhs: Z251) -> Self::Output {
        let (_, mut inv, _) = ext_euc_alg(rhs.inner as isize, 251);
        while inv < 0 {
            inv += 251
        }

        self * Z251 { inner: inv as u8 }
    }
}

impl FieldIdentity for Z251 {
    fn zero() -> Self {
        Z251 { inner: 0 }
    }
    fn one() -> Self {
        Z251 { inner: 1 }
    }
}

impl Field for Z251 {
    fn mul_inv(self) -> Self {
        Z251::one().div(self)
    }
}

impl From<usize> for Z251 {
    fn from(n: usize) -> Self {
        assert!(n < 251);
        Z251 { inner: (n) as u8 }
    }
}

impl Into<usize> for Z251 {
    fn into(self) -> usize {
        self.inner as usize
    }
}

impl FromStr for Z251 {
    type Err = ::std::num::ParseIntError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Z251::from(usize::from_str(s)?))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn z251_add() {
        for i in 0_u16..251_u16 {
            for j in 0_u16..251_u16 {
                let lhs = Z251 { inner: i as u8 };
                let rhs = Z251 { inner: j as u8 };

                assert_eq!((lhs + rhs).inner, ((i + j) % 251) as u8);
            }
        }
    }

    #[test]
    fn z251_neg() {
        for i in 1..251 {
            let lhs = Z251 { inner: i };
            let rhs = -Z251 { inner: i };
            assert_eq!(lhs + rhs, Z251::zero());
        }
    }

    #[test]
    fn z251_mul_inv() {
        for i in 1..251 {
            let lhs = Z251 { inner: i };
            let rhs = Z251 { inner: i }.mul_inv();
            assert_eq!(lhs * rhs, Z251::one());
        }
    }

    #[test]
    fn crt() {
        let rems = [0, 3, 4];
        let moduli = [3, 4, 5];
        let mut ret = chinese_remainder(&rems[..], &moduli[..]);
        while ret < 0 {
            ret += moduli.iter().product::<isize>();
        }
        assert_eq!(ret, 39);

        let rems = [1, 2, 3, 4];
        let moduli = [2, 3, 5, 7];
        let mut ret = chinese_remainder(&rems[..], &moduli[..]);
        while ret < 0 {
            ret += moduli.iter().product::<isize>();
        }
        assert_eq!(ret, 53);
    }
}