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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#![no_std]
use core::convert::{From, Into};
use core::ops::{Add, Sub, Not, Mul, Div, Shr, Shl};
use core::cmp::Ordering;
#[repr(C)]
#[derive(Eq, PartialEq, Debug, Copy, Clone)]
pub struct U256([u64; 4]);
impl U256 {
pub fn zero() -> U256 { U256([0; 4]) }
pub fn low_u32(&self) -> u32 {
let &U256(ref arr) = self;
arr[0] as u32
}
pub fn mul_u32(self, other: u32) -> U256 {
let U256(ref arr) = self;
let mut carry = [0u64; 4];
let mut ret = [0u64; 4];
for i in 0..4 {
let upper = other as u64 * (arr[i] >> 32);
let lower = other as u64 * (arr[i] & 0xFFFFFFFF);
if i < 3 {
carry[i + 1] += upper >> 32;
}
ret[i] = lower + (upper << 32);
}
U256(ret) + U256(carry)
}
pub fn bits(&self) -> usize {
let &U256(ref arr) = self;
for i in 1..4 {
if arr[4 - i] > 0 { return (0x40 * (4 - i + 1)) - arr[4 - i].leading_zeros() as usize; }
}
0x40 - arr[0].leading_zeros() as usize
}
}
impl From<u64> for U256 {
fn from(val: u64) -> U256 {
U256([0, 0, 0, val])
}
}
impl Into<u64> for U256 {
fn into(self) -> u64 {
assert!(self.0[0] == 0 && self.0[1] == 0 && self.0[2] == 0);
self.0[3]
}
}
impl From<usize> for U256 {
fn from(val: usize) -> U256 {
(val as u64).into()
}
}
impl Into<usize> for U256 {
fn into(self) -> usize {
let v64: u64 = self.into();
v64 as usize
}
}
impl From<i32> for U256 {
fn from(val: i32) -> U256 {
(val as u64).into()
}
}
impl<'a> From<&'a [u8]> for U256 {
fn from(val: &'a [u8]) -> U256 {
assert!(val.len() <= 256 / 8);
let mut u256 = U256::zero();
for i in 0..val.len() {
let rev = val.len() - 1 - i;
let pos = rev / 8;
u256.0[pos] += (val[i] as u64) << ((rev % 8) * 8);
}
u256
}
}
impl Not for U256 {
type Output = U256;
fn not(self) -> U256 {
let U256(ref arr) = self;
let mut ret = [0u64; 4];
for i in 0..4 {
ret[i] = !arr[i];
}
U256(ret)
}
}
impl Add for U256 {
type Output = U256;
fn add(self, other: U256) -> U256 {
let U256(ref me) = self;
let U256(ref you) = other;
let mut ret = [0u64; 4];
let mut carry = [0u64; 4];
let mut b_carry = false;
for i in 0..4 {
ret[i] = me[i].wrapping_add(you[i]);
if i < 4 - 1 && ret[i] < me[i] {
carry[i + 1] = 1;
b_carry = true;
}
}
if b_carry { U256(ret) + U256(carry) } else { U256(ret) }
}
}
impl Sub for U256 {
type Output = U256;
#[inline]
fn sub(self, other: U256) -> U256 {
self + !other
}
}
impl Mul for U256 {
type Output = U256;
fn mul(self, other: U256) -> U256 {
let mut me = self;
for i in 0..(2 * 4) {
me = (me + me.mul_u32((other >> (32 * i)).low_u32())) << (32 * i);
}
me
}
}
impl Div for U256 {
type Output = U256;
fn div(self, other: U256) -> U256 {
let mut sub_copy = self;
let mut shift_copy = other;
let mut ret = [0u64; 4];
let my_bits = self.bits();
let your_bits = other.bits();
assert!(your_bits != 0);
if my_bits < your_bits {
return U256(ret);
}
let mut shift = my_bits - your_bits;
shift_copy = shift_copy << shift;
loop {
if sub_copy >= shift_copy {
ret[shift / 64] |= 1 << (shift % 64);
sub_copy = sub_copy - shift_copy;
}
shift_copy = shift_copy >> 1;
if shift == 0 { break; }
shift -= 1;
}
U256(ret)
}
}
impl Ord for U256 {
fn cmp(&self, other: &U256) -> Ordering {
let &U256(ref me) = self;
let &U256(ref you) = other;
let mut i = 4;
while i > 0 {
i -= 1;
if me[i] < you[i] { return Ordering::Less; }
if me[i] > you[i] { return Ordering::Greater; }
}
Ordering::Equal
}
}
impl PartialOrd for U256 {
fn partial_cmp(&self, other: &U256) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Shl<usize> for U256 {
type Output = U256;
fn shl(self, shift: usize) -> U256 {
let U256(ref original) = self;
let mut ret = [0u64; 4];
let word_shift = shift / 64;
let bit_shift = shift % 64;
for i in 0..4 {
if bit_shift < 64 && i + word_shift < 4 {
ret[i + word_shift] += original[i] << bit_shift;
}
if bit_shift > 0 && i + word_shift + 1 < 4 {
ret[i + word_shift + 1] += original[i] >> (64 - bit_shift);
}
}
U256(ret)
}
}
impl Shr<usize> for U256 {
type Output = U256;
fn shr(self, shift: usize) -> U256 {
let U256(ref original) = self;
let mut ret = [0u64; 4];
let word_shift = shift / 64;
let bit_shift = shift % 64;
for i in word_shift..4 {
ret[i - word_shift] += original[i] >> bit_shift;
if bit_shift > 0 && i < 4 - 1 {
ret[i - word_shift] += original[i + 1] << (64 - bit_shift);
}
}
U256(ret)
}
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
}
}