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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign, Sub, SubAssign};
use ic_stable_structures::{storable::Bound, Storable};
use num_bigint::BigUint;
use num_traits::{CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, One, ToPrimitive, Zero};
use serde::{Deserialize, Serialize};
use std::fmt::Display;

#[derive(Debug, Clone, Default, Hash, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
#[serde(transparent)]
pub struct Ex3Uint(pub BigUint);
mod add;
mod compare;
mod convert;
mod div;
mod mul;
mod pow;
mod rem;
mod sub;

impl Zero for Ex3Uint {
    fn zero() -> Self {
        Ex3Uint(BigUint::zero())
    }

    fn is_zero(&self) -> bool {
        self.0.is_zero()
    }
}

impl One for Ex3Uint {
    fn one() -> Self {
        Ex3Uint(BigUint::one())
    }
    fn set_one(&mut self) {
        *self = One::one();
    }

    fn is_one(&self) -> bool
    where
        Self: PartialEq,
    {
        *self == Self::one()
    }
}

impl Add for Ex3Uint {
    type Output = Ex3Uint;

    fn add(self, rhs: Self) -> Self::Output {
        Ex3Uint(self.0 + rhs.0)
    }
}

impl CheckedAdd for Ex3Uint {
    fn checked_add(&self, rhs: &Self) -> Option<Self> {
        self.0.checked_add(&rhs.0).map(Self)
    }
}

impl AddAssign for Ex3Uint {
    fn add_assign(&mut self, rhs: Self) {
        self.0 += rhs.0;
    }
}

impl Sub for Ex3Uint {
    type Output = Ex3Uint;

    fn sub(self, rhs: Self) -> Self::Output {
        Ex3Uint(self.0 - rhs.0)
    }
}

impl CheckedSub for Ex3Uint {
    fn checked_sub(&self, rhs: &Self) -> Option<Self> {
        self.0.checked_sub(&rhs.0).map(Self)
    }
}

impl SubAssign for Ex3Uint {
    fn sub_assign(&mut self, rhs: Self) {
        self.0 -= rhs.0;
    }
}

impl Div for Ex3Uint {
    type Output = Ex3Uint;

    fn div(self, rhs: Self) -> Self::Output {
        Ex3Uint(self.0 / rhs.0)
    }
}

impl CheckedDiv for Ex3Uint {
    fn checked_div(&self, rhs: &Self) -> Option<Self> {
        self.0.checked_div(&rhs.0).map(Self)
    }
}

impl DivAssign for Ex3Uint {
    fn div_assign(&mut self, rhs: Self) {
        self.0 /= rhs.0;
    }
}

impl Mul for Ex3Uint {
    type Output = Ex3Uint;

    fn mul(self, rhs: Self) -> Self::Output {
        Ex3Uint(self.0 * rhs.0)
    }
}

impl CheckedMul for Ex3Uint {
    fn checked_mul(&self, rhs: &Self) -> Option<Self> {
        self.0.checked_mul(&rhs.0).map(Self)
    }
}

impl MulAssign for Ex3Uint {
    fn mul_assign(&mut self, rhs: Self) {
        self.0 *= rhs.0;
    }
}

impl Rem for Ex3Uint {
    type Output = Ex3Uint;

    fn rem(self, rhs: Self) -> Self::Output {
        Ex3Uint(self.0 % rhs.0)
    }
}

impl RemAssign for Ex3Uint {
    fn rem_assign(&mut self, rhs: Self) {
        self.0 %= rhs.0;
    }
}

impl Display for Ex3Uint {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl Storable for Ex3Uint {
    fn to_bytes(&self) -> std::borrow::Cow<[u8]> {
        self.0.to_bytes_le().into()
    }

    fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self {
        Self(BigUint::from_bytes_le(&bytes))
    }

    const BOUND: Bound = Bound::Bounded {
        max_size: 32,
        is_fixed_size: false,
    };
}

impl std::iter::Step for Ex3Uint {
    fn steps_between(start: &Self, end: &Self) -> Option<usize> {
        end.0.checked_sub(&start.0).map(|x| x.to_usize().unwrap())
    }

    fn forward_checked(start: Self, count: usize) -> Option<Self> {
        start.0.checked_add(&BigUint::from(count)).map(|x| x.into())
    }

    fn backward_checked(start: Self, count: usize) -> Option<Self> {
        start.0.checked_sub(&BigUint::from(count)).map(|x| x.into())
    }
}

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

    #[test]
    fn test_to_bytes() {
        let num = Ex3Uint(BigUint::from(u128::MAX));
        let bytes = num.to_bytes();
        assert!(bytes.len() <= 32);

        let num_decode = Ex3Uint::from_bytes(bytes.into());
        assert_eq!(num, num_decode);
    }

    #[test]
    fn test_add() {
        let num1 = Ex3Uint(BigUint::from(10u128));
        let num2 = Ex3Uint(BigUint::from(20u128));
        let sum = num1 + num2;
        assert_eq!(sum.0, BigUint::from(30u128));
    }

    #[test]
    fn test_sub() {
        let num1 = Ex3Uint(BigUint::from(20u128));
        let num2 = Ex3Uint(BigUint::from(10u128));
        let diff = num1 - num2;
        assert_eq!(diff.0, BigUint::from(10u128));
    }

    #[test]
    fn test_checked_sub() {
        let num1 = Ex3Uint(BigUint::from(20u128));
        let num2 = Ex3Uint(BigUint::from(10u128));
        let diff = num1.checked_sub(&num2).unwrap();
        assert_ne!(diff, num1);
        assert_eq!(diff.0, BigUint::from(10u128));
    }
}