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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
use crate::util;
use crate::util::{OverflowingAdd, OverflowingSub};
use std::{cmp, fmt};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[allow(non_camel_case_types)]
pub struct w256 {
low: u128,
high: u128,
}
impl w256 {
pub const MIN: w256 = w256::new(0u128, 0u128);
pub const MAX: w256 = w256::new(u128::MAX, u128::MAX);
pub const ZERO: w256 = w256::from(0);
pub const ONE: w256 = w256::from(1);
pub const TWO: w256 = w256::from(2);
pub const THREE: w256 = w256::from(3);
pub const FOUR: w256 = w256::from(4);
pub const fn new(low: u128, high: u128) -> Self {
w256 { low, high }
}
pub const fn from(item: u128) -> Self {
w256 { low: item, high: 0 }
}
pub fn from_be_bytes(bytes: &[u8]) -> Self {
let n = bytes.len();
assert!(n <= 32);
let lw;
let hw;
if n >= 16 {
let m = n - 16;
lw = util::from_be_bytes(&bytes[m..]);
hw = util::from_be_bytes(&bytes[..m]);
} else {
lw = util::from_be_bytes(bytes);
hw = 0;
}
w256 { low: lw, high: hw }
}
}
impl util::Max for w256 {
const MAX: Self = w256::MAX;
}
impl util::Min for w256 {
const MIN: Self = w256::MIN;
}
impl<T: Into<u128>> From<T> for w256 {
fn from(val: T) -> w256 {
w256 {
low: val.into(),
high: 0,
}
}
}
impl Into<u16> for w256 {
fn into(self) -> u16 {
self.low as u16
}
}
impl Into<u16> for &w256 {
fn into(self) -> u16 {
self.low as u16
}
}
impl Into<usize> for w256 {
fn into(self) -> usize {
self.low as usize
}
}
impl Into<usize> for &w256 {
fn into(self) -> usize {
self.low as usize
}
}
impl Ord for w256 {
fn cmp(&self, other: &Self) -> cmp::Ordering {
let h = u128::cmp(&self.high, &other.high);
if h == cmp::Ordering::Equal {
u128::cmp(&self.low, &other.low)
} else {
h
}
}
}
impl PartialOrd for w256 {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}
impl std::ops::Add for w256 {
type Output = Self;
fn add(self, rhs: Self) -> Self {
let (lw, c0) = self.low.overflowing_add(rhs.low);
let (hw, _) = carrying_add(self.high, rhs.high, c0);
w256 { low: lw, high: hw }
}
}
impl std::ops::Add<usize> for w256 {
type Output = Self;
fn add(self, rhs: usize) -> Self {
let r: w256 = (rhs as u128).into();
self + r
}
}
impl OverflowingAdd for w256 {
fn overflowing_add(self, rhs: w256) -> (Self, bool) {
let (lw, c0) = self.low.overflowing_add(rhs.low);
let (hw, c1) = carrying_add(self.high, rhs.high, c0);
(w256 { low: lw, high: hw }, c1)
}
}
impl std::ops::Sub for w256 {
type Output = Self;
fn sub(self, rhs: Self) -> Self {
let (lw, b1) = self.low.overflowing_sub(rhs.low);
let (hw, _) = borrowing_sub(self.high, rhs.high, b1);
w256 { low: lw, high: hw }
}
}
impl fmt::Display for w256 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{},{}", self.low, self.high)
}
}
impl fmt::LowerHex for w256 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut s = if self.high == 0 {
format!("{:x}",self.low)
} else {
format!("{:x}{:x}",self.high,self.low)
};
if f.alternate() { write!(f,"0x")?; }
match f.width() {
Some(w) => {
for i in s.len() .. w { write!(f,"0")?; }
}
None => {}
};
write!(f,"{}",s)
}
}
fn carrying_add(lhs: u128, rhs: u128, carry: bool) -> (u128, bool) {
let (r0, c0) = if carry {
lhs.overflowing_add(1)
} else {
(lhs, false)
};
let (r1, c1) = r0.overflowing_add(rhs);
(r1, c0 | c1)
}
fn borrowing_sub(lhs: u128, rhs: u128, borrow: bool) -> (u128, bool) {
let (r0, b0) = if borrow {
lhs.overflowing_sub(1)
} else {
(lhs, false)
};
let (r1, b1) = r0.overflowing_sub(rhs);
(r1, b0 | b1)
}
#[cfg(test)]
mod tests {
use crate::util::w256;
const ZERO: w256 = w256::new(0, 0);
const ONE: w256 = w256::new(1, 0);
const TWO: w256 = w256::new(2, 0);
const THREE: w256 = w256::new(3, 0);
const FOUR: w256 = w256::new(4, 0);
const MAX128: w256 = w256::new(u128::MAX, 0);
const MAX128M1: w256 = w256::new(u128::MAX - 1, 0);
const MAX128P1: w256 = w256::new(0, 1);
const MAX256: w256 = w256::MAX;
const MAX256M1: w256 = w256::new(u128::MAX - 1, u128::MAX);
#[test]
fn test_hex_01() {
assert_eq!(format!("{:x}",w256::new(15, 0)),"f");
}
#[test]
fn test_hex_02() {
assert_eq!(format!("{:#01x}",ONE),"0x1");
}
#[test]
fn test_hex_03() {
assert_eq!(format!("{:#04x}",ONE),"0x0001");
}
#[test]
fn test_hex_04() {
assert_eq!(format!("{:2x}",w256::new(255, 0)),"ff");
}
#[test]
fn test_hex_05() {
assert_eq!(format!("{:2x}",w256::new(256, 0)),"100");
}
macro_rules! assert_add {
($l:expr,$r:expr,$e:expr) => {
assert_eq!($l + $r, $e);
assert_eq!($r + $l, $e);
};
}
#[test]
fn test_add_01() {
assert_add!(ZERO, ZERO, ZERO);
}
#[test]
fn test_add_02() {
assert_add!(ZERO, ONE, ONE);
}
#[test]
fn test_add_03() {
assert_add!(ONE, ONE, TWO);
}
#[test]
fn test_add_04() {
assert_add!(ONE, TWO, THREE);
}
#[test]
fn test_add_05() {
assert_add!(TWO, TWO, FOUR);
}
#[test]
fn test_add_06() {
assert_add!(ZERO, MAX128, MAX128);
}
#[test]
fn test_add_07() {
assert_add!(ONE, MAX128, MAX128P1);
}
#[test]
fn test_add_08() {
assert_add!(ONE, MAX128M1, MAX128);
}
#[test]
fn test_add_09() {
assert_add!(ONE, MAX256, ZERO);
}
#[test]
fn test_add_10() {
assert_add!(ONE, MAX256M1, MAX256);
}
macro_rules! assert_sub {
($l:expr,$r:expr,$e1:expr,$e2:expr) => {
assert_eq!($l - $r, $e1);
assert_eq!($r - $l, $e2);
};
}
#[test]
fn test_sub_01() {
assert_sub!(ZERO, ZERO, ZERO, ZERO);
}
#[test]
fn test_sub_02() {
assert_sub!(ONE, ONE, ZERO, ZERO);
}
#[test]
fn test_sub_03() {
assert_sub!(ZERO, ONE, MAX256, ONE);
}
#[test]
fn test_sub_04() {
assert_sub!(ZERO, TWO, MAX256M1, TWO);
}
#[test]
fn test_sub_05() {
assert_sub!(ONE, TWO, MAX256, ONE);
}
}