reweb3-num 0.2.4

Arbitrary precision, fixed-size signed and unsigned integer types for ethereum, this a fork of bnum crate.
Documentation
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
use crate::digit;
use crate::doc;
use crate::errors::div_zero;
use crate::int::checked::tuple_to_option;
use crate::ExpType;

macro_rules! checked {
    ($BUint: ident, $BInt: ident, $Digit: ident) => {
        #[doc = doc::checked::impl_desc!()]
        impl<const N: usize> $BUint<N> {
            #[doc = doc::checked::checked_add!(U)]
            #[must_use = doc::must_use_op!()]
            #[inline]
            pub const fn checked_add(self, rhs: Self) -> Option<Self> {
                tuple_to_option(self.overflowing_add(rhs))
            }

            #[doc = doc::checked::checked_add_signed!(U)]
            #[must_use = doc::must_use_op!()]
            #[inline]
            pub const fn checked_add_signed(self, rhs: $BInt<N>) -> Option<Self> {
                tuple_to_option(self.overflowing_add_signed(rhs))
            }

            #[doc = doc::checked::checked_sub!(U)]
            #[must_use = doc::must_use_op!()]
            #[inline]
            pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
                tuple_to_option(self.overflowing_sub(rhs))
            }

            #[doc = doc::checked::checked_mul!(U)]
            #[must_use = doc::must_use_op!()]
            #[inline]
            pub const fn checked_mul(self, rhs: Self) -> Option<Self> {
                tuple_to_option(self.overflowing_mul(rhs))
            }

            pub(crate) const fn div_rem_digit(self, rhs: $Digit) -> (Self, $Digit) {
                let mut out = Self::ZERO;
                let mut rem: $Digit = 0;
                let mut i = N;
                while i > 0 {
                    i -= 1;
                    let (q, r) = digit::$Digit::div_rem_wide(self.digits[i], rem, rhs);
                    rem = r;
                    out.digits[i] = q;
                }
                (out, rem)
            }
            const fn basecase_div_rem(self, mut v: Self, n: usize) -> (Self, Self) {
                // The Art of Computer Programming Volume 2 by Donald Knuth, Section 4.3.1, Algorithm D

                let mut q = Self::ZERO;
                let m = self.last_digit_index() + 1 - n;
                let shift = v.digits[n - 1].leading_zeros() as ExpType;

                v = unsafe { Self::unchecked_shl_internal(v, shift) }; // D1

                struct Remainder<const M: usize> {
                    first: $Digit,
                    rest: [$Digit; M],
                }
                impl<const M: usize> Remainder<M> {
                    const fn digit(&self, index: usize) -> $Digit {
                        if index == 0 {
                            self.first
                        } else {
                            self.rest[index - 1]
                        }
                    }
                    const fn shr(self, shift: ExpType) -> $BUint<M> {
                        let mut out = $BUint::ZERO;
                        let mut i = 0;
                        while i < M {
                            out.digits[i] = self.digit(i) >> shift;
                            i += 1;
                        }
                        if shift > 0 {
                            i = 0;
                            while i < M {
                                out.digits[i] |=
                                    self.rest[i] << (digit::$Digit::BITS as ExpType - shift);
                                i += 1;
                            }
                        }
                        out
                    }
                    const fn new(uint: $BUint<M>, shift: ExpType) -> Self {
                        let first = uint.digits[0] << shift;
                        let rest = uint.wrapping_shr(digit::$Digit::BITS - shift);
                        Self {
                            first,
                            rest: rest.digits,
                        }
                    }
                    /*crate::nightly::const_fns! {
                        const fn set_digit(&mut self, index: usize, digit: $Digit) -> () {
                            if index == 0 {
                                self.first = digit;
                            } else {
                                self.rest[index - 1] = digit;
                            }
                        }
                        const fn sub(&mut self, rhs: Mul<M>, start: usize, range: usize) -> bool {
                            let mut borrow = false;
                            let mut i = 0;
                            while i <= range {
                                let (sub, overflow) = digit::$Digit::borrowing_sub(self.digit(i + start), rhs.digit(i), borrow);
                                self.set_digit(i + start, sub);
                                borrow = overflow;
                                i += 1;
                            }
                            borrow
                        }
                        const fn add(&mut self, rhs: $BUint<M>, start: usize, range: usize) -> () {
                            let mut carry = false;
                            let mut i = 0;
                            while i < range {
                                let (sum, overflow) = digit::$Digit::carrying_add(self.digit(i + start), rhs.digits[i], carry);
                                self.set_digit(i + start, sum);
                                carry = overflow;
                                i += 1;
                            }
                            if carry {
                                self.set_digit(range + start, self.digit(range + start).wrapping_add(1)); // we use wrapping_add here, not regular addition as a carry will always occur to the left of self.digit(range + start)
                            }
                        }
                    }*/
                    const fn sub(
                        mut self,
                        rhs: Mul<M>,
                        start: usize,
                        range: usize,
                    ) -> (Self, bool) {
                        let mut borrow = false;
                        let mut i = 0;
                        while i <= range {
                            let (sub, overflow) = digit::$Digit::borrowing_sub(
                                self.digit(i + start),
                                rhs.digit(i),
                                borrow,
                            );
                            if start == 0 && i == 0 {
                                self.first = sub;
                            } else {
                                self.rest[i + start - 1] = sub;
                            }
                            borrow = overflow;
                            i += 1;
                        }
                        (self, borrow)
                    }
                    const fn add(mut self, rhs: $BUint<M>, start: usize, range: usize) -> Self {
                        let mut carry = false;
                        let mut i = 0;
                        while i < range {
                            let (sum, overflow) = digit::$Digit::carrying_add(
                                self.digit(i + start),
                                rhs.digits[i],
                                carry,
                            );
                            if start == 0 && i == 0 {
                                self.first = sum;
                            } else {
                                self.rest[i + start - 1] = sum;
                            }
                            carry = overflow;
                            i += 1;
                        }
                        if carry {
                            if start == 0 && range == 0 {
                                self.first = self.first.wrapping_add(1);
                            } else {
                                self.rest[range + start - 1] =
                                    self.rest[range + start - 1].wrapping_add(1);
                            }
                        }
                        self
                    }
                }

                #[derive(Clone, Copy)]
                struct Mul<const M: usize> {
                    last: $Digit,
                    rest: [$Digit; M],
                }
                impl<const M: usize> Mul<M> {
                    const fn new(uint: $BUint<M>, rhs: $Digit) -> Self {
                        let mut rest = [0; M];
                        let mut carry: $Digit = 0;
                        let mut i = 0;
                        while i < M {
                            let (prod, c) =
                                digit::$Digit::carrying_mul(uint.digits[i], rhs, carry, 0);
                            carry = c;
                            rest[i] = prod;
                            i += 1;
                        }
                        Self { last: carry, rest }
                    }
                    const fn digit(&self, index: usize) -> $Digit {
                        if index == M {
                            self.last
                        } else {
                            self.rest[index]
                        }
                    }
                }

                let v_n_m1 = v.digits[n - 1];
                let v_n_m2 = v.digits[n - 2];

                let mut u = Remainder::new(self, shift);

                let mut j = m + 1; // D2
                while j > 0 {
                    j -= 1; // D7

                    let u_jn = u.digit(j + n);

                    #[inline]
                    const fn tuple_gt(a: ($Digit, $Digit), b: ($Digit, $Digit)) -> bool {
                        a.1 > b.1 || a.1 == b.1 && a.0 > b.0
                    }

                    // q_hat will be either `q` or `q + 1`
                    let mut q_hat = if u_jn < v_n_m1 {
                        let (mut q_hat, r_hat) =
                            digit::$Digit::div_rem_wide(u.digit(j + n - 1), u_jn, v_n_m1); // D3

                        if tuple_gt(
                            digit::$Digit::widening_mul(q_hat, v_n_m2),
                            (u.digit(j + n - 2), r_hat as $Digit),
                        ) {
                            q_hat -= 1;

                            if let Some(r_hat) = r_hat.checked_add(v_n_m1) {
                                // this checks if `r_hat <= b`, where `b` is the digit base
                                if tuple_gt(
                                    digit::$Digit::widening_mul(q_hat, v_n_m2),
                                    (u.digit(j + n - 2), r_hat as $Digit),
                                ) {
                                    q_hat -= 1;
                                }
                            }
                        }
                        q_hat
                    } else {
                        // `u[j + n - 1] >= v[n - 1]` so we know that estimate for q_hat would be larger than `Digit::MAX`. This is either equal to `q` or `q + 1` (very unlikely to be `q + 1`).
                        $Digit::MAX
                    };
                    let (u_new, overflow) = u.sub(Mul::new(v, q_hat), j, n); // D4
                    u = u_new;

                    if overflow {
                        // D5 - unlikely, probability of this being true is ~ 2 / b where b is the digit base (i.e. `Digit::MAX + 1`)
                        q_hat -= 1;
                        u = u.add(v, j, n);
                    }
                    q.digits[j] = q_hat;
                }
                (q, u.shr(shift))
            }

            #[inline]
            pub(crate) const fn div_rem_unchecked(self, rhs: Self) -> (Self, Self) {
                use core::cmp::Ordering;

                if self.is_zero() {
                    return (Self::ZERO, Self::ZERO);
                }

                match self.cmp(&rhs) {
                    Ordering::Less => (Self::ZERO, self),
                    Ordering::Equal => (Self::ONE, Self::ZERO),
                    Ordering::Greater => {
                        let ldi = rhs.last_digit_index();
                        if ldi == 0 {
                            let (div, rem) = self.div_rem_digit(rhs.digits[0]);
                            (div, Self::from_digit(rem))
                        } else {
                            self.basecase_div_rem(rhs, ldi + 1)
                        }
                    }
                }
            }

            #[inline]
            pub(crate) const fn div_rem(self, rhs: Self) -> (Self, Self) {
                if rhs.is_zero() {
                    div_zero!()
                } else {
                    self.div_rem_unchecked(rhs)
                }
            }

            #[doc = doc::checked::checked_div!(U)]
            #[must_use = doc::must_use_op!()]
            #[inline]
            pub const fn checked_div(self, rhs: Self) -> Option<Self> {
                if rhs.is_zero() {
                    None
                } else {
                    Some(self.div_rem_unchecked(rhs).0)
                }
            }

            #[doc = doc::checked::checked_div_euclid!(U)]
            #[must_use = doc::must_use_op!()]
            #[inline]
            pub const fn checked_div_euclid(self, rhs: Self) -> Option<Self> {
                self.checked_div(rhs)
            }

            #[doc = doc::checked::checked_rem!(U)]
            #[must_use = doc::must_use_op!()]
            #[inline]
            pub const fn checked_rem(self, rhs: Self) -> Option<Self> {
                if rhs.is_zero() {
                    None
                } else {
                    Some(self.div_rem_unchecked(rhs).1)
                }
            }

            #[doc = doc::checked::checked_rem_euclid!(U)]
            #[must_use = doc::must_use_op!()]
            #[inline]
            pub const fn checked_rem_euclid(self, rhs: Self) -> Option<Self> {
                self.checked_rem(rhs)
            }

            #[doc = doc::checked::checked_neg!(U)]
            #[must_use = doc::must_use_op!()]
            #[inline]
            pub const fn checked_neg(self) -> Option<Self> {
                if self.is_zero() {
                    Some(self)
                } else {
                    None
                }
            }

            #[doc = doc::checked::checked_shl!(U)]
            #[must_use = doc::must_use_op!()]
            #[inline]
            pub const fn checked_shl(self, rhs: ExpType) -> Option<Self> {
                if rhs >= Self::BITS {
                    None
                } else {
                    unsafe { Some(Self::unchecked_shl_internal(self, rhs)) }
                }
            }

            #[doc = doc::checked::checked_shr!(U)]
            #[must_use = doc::must_use_op!()]
            #[inline]
            pub const fn checked_shr(self, rhs: ExpType) -> Option<Self> {
                if rhs >= Self::BITS {
                    None
                } else {
                    unsafe { Some(Self::unchecked_shr_internal(self, rhs)) }
                }
            }

            #[doc = doc::checked::checked_pow!(U)]
            #[must_use = doc::must_use_op!()]
            #[inline]
            pub const fn checked_pow(mut self, mut pow: ExpType) -> Option<Self> {
                // https://en.wikipedia.org/wiki/Exponentiation_by_squaring#Basic_method
                if pow == 0 {
                    return Some(Self::ONE);
                }
                let mut y = Self::ONE;
                while pow > 1 {
                    if pow & 1 == 1 {
                        y = match self.checked_mul(y) {
                            Some(m) => m,
                            None => return None,
                        };
                    }
                    self = match self.checked_mul(self) {
                        Some(m) => m,
                        None => return None,
                    };
                    pow >>= 1;
                }
                self.checked_mul(y)
            }

            #[doc = doc::checked::checked_next_multiple_of!(U)]
            #[must_use = doc::must_use_op!()]
            #[inline]
            pub const fn checked_next_multiple_of(self, rhs: Self) -> Option<Self> {
                match self.checked_rem(rhs) {
                    Some(rem) => {
                        if rem.is_zero() {
                            // `rhs` divides `self` exactly so just return `self`
                            Some(self)
                        } else {
                            // `next_multiple = floor(self / rhs) * rhs + rhs = (self - rem) + rhs`
                            self.checked_add(rhs.sub(rem))
                        }
                    }
                    None => None,
                }
            }

            #[doc = doc::checked::checked_ilog2!(U)]
            #[must_use = doc::must_use_op!()]
            #[inline]
            pub const fn checked_ilog2(self) -> Option<ExpType> {
                self.bits().checked_sub(1)
            }

            #[inline]
            const fn iilog(m: ExpType, b: Self, k: Self) -> (ExpType, Self) {
                // https://people.csail.mit.edu/jaffer/III/iilog.pdf
                if b.gt(&k) {
                    (m, k)
                } else {
                    let (new, q) = Self::iilog(m << 1, b.mul(b), k.div_rem_unchecked(b).0);
                    if b.gt(&q) {
                        (new, q)
                    } else {
                        (new + m, q.div(b))
                    }
                }
            }

            #[doc = doc::checked::checked_ilog10!(U)]
            #[must_use = doc::must_use_op!()]
            #[inline]
            pub const fn checked_ilog10(self) -> Option<ExpType> {
                if self.is_zero() {
                    return None;
                }
                if Self::TEN.gt(&self) {
                    return Some(0);
                }
                Some(Self::iilog(1, Self::TEN, self.div_rem_digit(10).0).0)
            }

            #[doc = doc::checked::checked_ilog!(U)]
            #[must_use = doc::must_use_op!()]
            #[inline]
            pub const fn checked_ilog(self, base: Self) -> Option<ExpType> {
                use core::cmp::Ordering;
                match base.cmp(&Self::TWO) {
                    Ordering::Less => None,
                    Ordering::Equal => self.checked_ilog2(),
                    Ordering::Greater => {
                        if self.is_zero() {
                            return None;
                        }
                        if base.gt(&self) {
                            return Some(0);
                        }
                        Some(Self::iilog(1, base, self.div(base)).0)
                    }
                }
            }

            #[doc = doc::checked::checked_next_power_of_two!(U 256)]
            #[must_use = doc::must_use_op!()]
            #[inline]
            pub const fn checked_next_power_of_two(self) -> Option<Self> {
                if self.is_power_of_two() {
                    return Some(self);
                }
                let bits = self.bits();
                if bits == Self::BITS {
                    return None;
                }
                Some(Self::power_of_two(bits))
            }
        }
    };
}

crate::macro_impl!(checked);