aboutsummaryrefslogtreecommitdiff
path: root/src/passwordmaker/base_conversion/iterative_conversion_impl.rs
blob: ea67dd1b5827a3d127a4b2e9069ba7db754e4e54 (plain) (blame)
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
//! Implementation of iterative conversion support for the types we need it for: u128 and [u32;N].

//Reminder for myself: The traits needed are:
//    where V: for<'a> From<&'a B> +                    //could be replaced by num::traits::identities::One.
//          for<'a> DivAssign<&'a B> +                  //used between steps to go to next-lower current_base_potency
//          RemAssignWithQuotient+                      //used to get the result of each step.
//          TryInto<B>,                                 //used to convert the result of each step. We _know_ this cannot fail, but requiring Into would be wrong.
//      for<'a> &'a V : Mul<&'a B, Output = Option<V>>  //used to get the first current_base_potency.

//let's start with the simple case: u128
//we do need a NewType here, because actual u128 already has a Mul<&usize> implementation that does not match the version we want.

use std::{ops::{DivAssign, Mul}, convert::{TryFrom, TryInto}, fmt::Display, error::Error, cmp::Ordering, iter::once};

use super::iterative_conversion::RemAssignWithQuotient;

//Type to be used as V, with usize as B.
pub(crate) struct SixteenBytes(u128);

impl SixteenBytes{
    pub(super) fn new(value : u128) -> Self {
        SixteenBytes(value)
    }
}

//just for convenience
impl From<u128> for SixteenBytes{
    fn from(x: u128) -> Self {
        SixteenBytes(x)
    }
}
impl From<&usize> for SixteenBytes{
    fn from(x: &usize) -> Self {
        SixteenBytes(*x as u128)
    }
}
impl DivAssign<&usize> for SixteenBytes{
    fn div_assign(&mut self, rhs: &usize) {
        self.0 /= *rhs as u128
    }
}
impl RemAssignWithQuotient for SixteenBytes{
    fn rem_assign_with_quotient(&mut self, divisor : &Self) -> Self {
        let quotient = self.0 / divisor.0;
        self.0 %= divisor.0;
        Self(quotient)
    }
}
impl TryFrom<SixteenBytes> for usize{
    type Error = std::num::TryFromIntError;
    fn try_from(value: SixteenBytes) -> Result<Self, Self::Error> {
        value.0.try_into()
    }
}
impl Mul<&usize> for &SixteenBytes{
    type Output = Option<SixteenBytes>;
    fn mul(self, rhs: &usize) -> Self::Output {
        self.0.checked_mul(*rhs as u128).map(Into::into)
    }
}

impl Mul<&SixteenBytes> for &SixteenBytes{
    type Output = Option<SixteenBytes>;

    fn mul(self, rhs: &SixteenBytes) -> Self::Output {
        self.0.checked_mul(rhs.0).map(Into::into)
    }
}

//--------------------------------------------------------------------------------------------------------------------------------------
//and now the hard part: The same for [u32;N].
//We cannot directly implement all the Foreign traits on arrays directly. So, newtypes again.

#[derive(PartialEq, PartialOrd, Ord, Eq, Clone)]
pub(crate) struct ArbitraryBytes<const N : usize>([u32;N]);

//Const generics are still a bit limited -> let's just implement From for the exact types we need.
impl From<&usize> for ArbitraryBytes<5>{
    fn from(x: &usize) -> Self {
        Self([
            0,//(*x >> 32*4) as u32, //zero on all target platforms
            0,//(*x >> 32*3) as u32, //zero on all target platforms
            0,//(*x >> 32*2) as u32, //zero on all target platforms
            x.checked_shr(32).map(|x| x as u32).unwrap_or_default(),
            *x as u32,
        ])
    }
}

impl From<&usize> for ArbitraryBytes<8>{
    fn from(x: &usize) -> Self {
        Self([
            0,//(*x >> 32*7) as u32, //zero on all target platforms
            0,//(*x >> 32*6) as u32, //zero on all target platforms
            0,//(*x >> 32*5) as u32, //zero on all target platforms
            0,//(*x >> 32*4) as u32, //zero on all target platforms
            0,//(*x >> 32*3) as u32, //zero on all target platforms
            0,//(*x >> 32*2) as u32, //zero on all target platforms
            x.checked_shr(32).map(|x| x as u32).unwrap_or_default(),
            *x as u32,
        ])
    }
}

impl From<&u32> for ArbitraryBytes<5>{
    fn from(x: &u32) -> Self {
        Self([
            0,
            0,
            0,
            0,
            *x,
        ])
    }
}

impl From<&u32> for ArbitraryBytes<8>{
    fn from(x: &u32) -> Self {
        Self([
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            *x,
        ])
    }
}

//workaround for lack of proper const-generic support.
pub(crate) trait PadWithAZero{
    type Output;
    fn pad_with_a_zero(&self) -> Self::Output;
}

impl PadWithAZero for ArbitraryBytes<5>{
    type Output = ArbitraryBytes<6>;
    fn pad_with_a_zero(&self) -> Self::Output {
        ArbitraryBytes::<6>([
            0,
            self.0[0],
            self.0[1],
            self.0[2],
            self.0[3],
            self.0[4],
        ])
    }
}

impl PadWithAZero for ArbitraryBytes<8>{
    type Output = ArbitraryBytes<9>;
    fn pad_with_a_zero(&self) -> Self::Output {
        ArbitraryBytes::<9>([
            0,
            self.0[0],
            self.0[1],
            self.0[2],
            self.0[3],
            self.0[4],
            self.0[5],
            self.0[6],
            self.0[7],
        ])
    }
}

impl<const N : usize> DivAssign<&usize> for ArbitraryBytes<N>{
    //just do long division.
    fn div_assign(&mut self, rhs: &usize) {
        self.div_assign_with_remainder_usize(rhs);
    }
}

#[derive(Debug, Clone, Copy)]
pub(crate) struct ArbitraryBytesToUsizeError;
impl Display for ArbitraryBytesToUsizeError{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "conversion from arbitrary sized int-array to usize failed")
    }
}
impl Error for ArbitraryBytesToUsizeError{}

impl<const N : usize> TryFrom<ArbitraryBytes<N>> for usize{
    type Error = ArbitraryBytesToUsizeError;

    fn try_from(value: ArbitraryBytes<N>) -> Result<Self, Self::Error> {
        usize::try_from(&value)
    }
}

impl<const N : usize> TryFrom<&ArbitraryBytes<N>> for usize{
    type Error = ArbitraryBytesToUsizeError;
    #[cfg(target_pointer_width = "64")]
    fn try_from(value: &ArbitraryBytes<N>) -> Result<Self, Self::Error> {
        //64 bits.
        if value.0[0..N.saturating_sub(2)].iter().any(|x| *x != 0) {
            Err(ArbitraryBytesToUsizeError)
        } else {
            //failing to get last_bit is an actual error.
            let last_bit = value.0.get(N-1).ok_or(ArbitraryBytesToUsizeError).map(|x| *x as usize);
            //second-last is not an error though.
            let second_last_bit = value.0.get(N-2).map(|u| (*u as usize) << 32).unwrap_or_default();
            last_bit.map(|last_bit| last_bit | second_last_bit)
        }
    }
    #[cfg(not(target_pointer_width = "64"))]
    fn try_from(value: &ArbitraryBytes<N>) -> Result<Self, Self::Error> {
        //16 or 32 bits.
        if value.0[0..N.saturating_sub(1)].iter().any(|x| *x != 0) {
            Err(ArbitraryBytesToUsizeError)
        } else {
            value.0.get(N-1).and_then(|x| (*x).try_into().ok()).ok_or(ArbitraryBytesToUsizeError)
        }
    }
}

#[derive(Debug, Clone, Copy)]
pub(crate) struct ArbitraryBytesToU32Error;
impl Display for ArbitraryBytesToU32Error{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "conversion from arbitrary sized int-array to u32 failed")
    }
}
impl Error for ArbitraryBytesToU32Error{}

impl<const N : usize> TryFrom<&ArbitraryBytes<N>> for u32{
    type Error = ArbitraryBytesToU32Error;

    fn try_from(value: &ArbitraryBytes<N>) -> Result<Self, Self::Error> {
        if value.0[0..N.saturating_sub(1)].iter().any(|x| *x != 0) {
            Err(ArbitraryBytesToU32Error)
        } else {
            value.0.get(N-1).copied().ok_or(ArbitraryBytesToU32Error)
        }
    }
}

macro_rules! make_mul {
    ($t:ty, $long_t:ty) => {
        impl<const N : usize> Mul<$t> for ArbitraryBytes<N>{
            type Output = Option<ArbitraryBytes<N>>;
            fn mul(mut self, rhs: $t) -> Self::Output {
                let carry = self.0.iter_mut().rev().fold(<$long_t>::default(), |carry, digit|{
                    debug_assert_eq!(carry, carry & (<$t>::MAX as $long_t)); //carry always has to fit in usize, otherwise something is terribly wrong.
                    let res = (*digit as $long_t) * (rhs as $long_t) + carry;
                    *digit = res as u32;
                    res >> 32
                });
                if carry != 0 { //if there's still carry after we hit the last digit, well, didn't fit obviously.
                    None
                } else {
                    Some(self)
                }
            }
        }
    };
}
make_mul!(u32,u64);
#[cfg(target_pointer_width = "64")]
make_mul!(usize, u128);
#[cfg(not(target_pointer_width = "64"))]
make_mul!(usize, u64);

impl<const N : usize> Mul<&usize> for &ArbitraryBytes<N>{
    type Output = Option<ArbitraryBytes<N>>;
    fn mul(self, rhs: &usize) -> Self::Output {
        (*self).clone() * (*rhs)
    }
}

impl<const N : usize> Mul<&ArbitraryBytes<N>> for &ArbitraryBytes<N> where ArbitraryBytes<N> : for<'a> From<&'a usize> {
    type Output = Option<ArbitraryBytes<N>>;
    ///School method. I haven't tried Karatsuba, but rule of thumb is that it only gets faster at about 32 digits. We have 8 digits max.
    fn mul(self, rhs: &ArbitraryBytes<N>) -> Self::Output {
        let mut result : ArbitraryBytes<N> = (&0_usize).into();
        let no_overflow = rhs.0.iter().enumerate().filter(|(_,b)| **b != 0).try_for_each(|(i,b)|{
            let p : Option<ArbitraryBytes<N>> = self.clone() * *b;
            let p = p.filter(|p| p.0[0..(N-1-i)].iter().all(|&i| i == 0));
            let carry = p.map(|p|{
                //for some reason it's faster to use slices than iterators here.
                slice_add_assign(&mut result.0[0..(i+1)], &p.0[(N-1-i)..])
            });
            carry.filter(|x| !x).map(|_|())
        });
        no_overflow.map(|_| result)
    }
}

impl<const N : usize, const M : usize> RemAssignWithQuotient for ArbitraryBytes<N> 
    where Self : for<'a> From<&'a usize> + for<'a> From<&'a u32> + PadWithAZero<Output = ArbitraryBytes<M>>
{
    fn rem_assign_with_quotient(&mut self, divisor : &Self) -> Self{

        //This is based on Knuth, TAOCP vol 2 section 4.3, algorithm D. However, at least for now, a 
        //non-performing restoring version of the algorithm is used, because I'm too tired right now
        //to properly implement the performing one (which would with near certainty be faster a bit).
        
        //First, check if we can get away without doing a division.
        match Ord::cmp(self, divisor){
            std::cmp::Ordering::Less => Self::from(&0_usize), //leave self unchanged, it's the remainder.
            std::cmp::Ordering::Equal => { *self = Self::from(&0_usize); Self::from(&1_usize) },
            std::cmp::Ordering::Greater => {
                //If a single digit division suffices, do a single digit division.
                if let Ok(divisor_as_u32) = divisor.try_into() {
                    self.rem_assign_with_quotient_u32(&divisor_as_u32)
                } else {
                    self.rem_assign_with_quotient_knuth(divisor)
                }
            },
        }
    }
}

macro_rules! make_div_assign_with_remainder {
    ($name:ident, $t_divisor:ty, $t_long:ty) => {
        /// Replaces self with Quotient and returns Remainder
        fn $name(&mut self, rhs: &$t_divisor) -> $t_divisor {
            debug_assert!((<$t_long>::MAX >> 32) as u128 >= <$t_divisor>::MAX as u128);

            let divisor = *rhs as $t_long;
            let remainder = self.0.iter_mut().fold(0 as $t_long,|carry, current| {
                debug_assert_eq!(carry, carry & (<$t_divisor>::MAX as $t_long)); //carry has to be lower than divisor, and divisor is $t_divisor.
                let carry_shifted = carry << 32;
                let dividend = (carry_shifted) | (*current as $t_long);
                let remainder = dividend % divisor;
                let ratio = dividend / divisor;
                debug_assert_eq!(ratio, ratio & 0xffff_ffff); //this is fine. The first digit after re-adding the carry is alwys zero.
                *current = (ratio) as u32; 
                remainder
            });
            debug_assert_eq!(remainder, remainder & (<$t_divisor>::MAX as $t_long));
            remainder as $t_divisor
        }
    };
}

impl<const N : usize> ArbitraryBytes<N>{
    pub(super) fn new(data : [u32;N]) -> Self {
        ArbitraryBytes(data)
    }

    #[cfg(target_pointer_width = "64")]
    make_div_assign_with_remainder!(div_assign_with_remainder_usize, usize, u128);

    #[cfg(not(target_pointer_width = "64"))]
    make_div_assign_with_remainder!(div_assign_with_remainder_usize, usize, u64);

    make_div_assign_with_remainder!(div_assign_with_remainder_u32, u32, u64);

    fn rem_assign_with_quotient_u32(&mut self, divisor: &u32) -> Self where Self : for<'a> From<&'a u32> {
        let remainder = self.div_assign_with_remainder_u32(divisor);
        std::mem::replace(self, Self::from(&remainder))
    }
    

    fn rem_assign_with_quotient_knuth<const M : usize>(&mut self, divisor : &Self) -> Self
        where Self : PadWithAZero<Output = ArbitraryBytes<M>> +
                     for<'a> From<&'a usize>
    {
        debug_assert!(M == N+1);
        //first we need to find n (number of digits in divisor)
        let n_digits_divisor= N - divisor.find_first_nonzero_digit();
        debug_assert!(n_digits_divisor > 1);
        //and same in the non-normalized dividend
        let m_plus_n_digits_dividend = N - self.find_first_nonzero_digit();
        let m_extra_digits_dividend = m_plus_n_digits_dividend - n_digits_divisor;

        //step D1: Normalize. This brings the maximum error for each digit down to no more than 2.
        let normalize_shift = divisor.get_digit_from_right(n_digits_divisor - 1).leading_zeros() as usize;
        //again, missing const generics ruin all the fun.
        let mut dividend = self.shift_left(normalize_shift);
        let divisor = divisor.shift_left(normalize_shift);
        debug_assert_eq!(divisor.get_digit_from_right(n_digits_divisor - 1).leading_zeros(),0);

        let mut quotient : Self = (&0_usize).into();

        //needed for Step D3.
        let guess_divisor = divisor.get_digit_from_right(n_digits_divisor - 1) as u64;
        let divisor_second_significant_digit = divisor.get_digit_from_right(n_digits_divisor-2) as u64;

        //step D2, D7: the loop.
        for j in (0..=m_extra_digits_dividend).rev() {
            //Step D3: Guess a digit
            let guess_dividend = ((dividend.get_digit_from_right(j+n_digits_divisor) as u64)<<32) + (dividend.get_digit_from_right(j + n_digits_divisor - 1) as u64);
            let mut guesstimate = guess_dividend/guess_divisor;
            let mut guess_reminder = guess_dividend % guess_divisor;
            //refine this result (still step D3)
            while guess_reminder <= u32::MAX as u64
                && (guesstimate > u32::MAX as u64
                    || divisor_second_significant_digit * guesstimate
                        > (guess_reminder << 32) + (dividend.get_digit_from_right(j + n_digits_divisor - 2) as u64)
                ) {
                guesstimate -= 1;
                guess_reminder += guess_divisor;
            }
            //I'm too tired to do this by the book. If this thing is gonna blow, we can just as well increase our guesstimate by one and call it a day.
            //In any case, this does only happen in _very_ rare cases. Soo:
            //Steps D4-D6.
            debug_assert!(guesstimate & (u32::MAX as u64) == guesstimate); //Knuth says this is a one-place number, and I trust him.
            let mut guesstimate = guesstimate as u32;
            let mut s = (divisor.clone() * guesstimate).expect("Multipliation by a digit cannot overflow for a padded type.");
            let will_overflow = 
                std::cmp::PartialOrd::lt(&dividend.0[(M - 1 - (j+n_digits_divisor))..=(M - 1 - j)], &s.0[(M - 1 - n_digits_divisor)..=(M - 1)]);
            if will_overflow {
                guesstimate -= 1;
                slice_sub_assign(&mut s.0, &divisor.0);
                debug_assert!(std::cmp::Ord::cmp(&dividend.0[(M - 1 - (j+n_digits_divisor))..=(M - 1 - j)], &s.0[(M - 1 - n_digits_divisor)..=(M - 1)]) != Ordering::Less)
            }
            slice_sub_assign(&mut dividend.0[(M - 1 - (j+n_digits_divisor))..=(M - 1 - j)], &s.0[(M - 1 - n_digits_divisor)..=(M - 1)]);
            quotient.set_digit_from_right(guesstimate, j);
        }

        //Steop D8: Compute Remainder.
        self.0 = dividend.shift_right(normalize_shift).0[1..].try_into()
            .expect("Conversion of what should have been an N-element slice into an N-element array failed.");
        quotient
        
    }

    fn find_first_nonzero_digit(&self) -> usize{
        self.0.iter().enumerate().skip_while(|(_,v)| **v == 0).next().map(|(x,_)| x).unwrap_or(N)
    }

    fn get_digit_from_right(&self, i : usize) -> u32{
        self.0[N-i-1]
    }
    fn set_digit_from_right(&mut self, val: u32, i : usize){
        self.0[N-i-1] = val;
    }

    fn shift_left<const M : usize>(&self, s : usize) -> <Self as PadWithAZero>::Output
        where Self : PadWithAZero<Output = ArbitraryBytes<M>>
    {
        debug_assert!(s < 32);
        let mut res = self.pad_with_a_zero();
        if s != 0{
            res.0.iter_mut().zip(self.0.iter().chain(once(&0))).for_each(|(current, next)| *current = (*current << s) | (*next >> (32-s)));
        }
        res
    }

    fn shift_right(mut self, s : usize) -> Self {
        debug_assert!(s < 32);
        if s != 0 {
            let _ = self.0.iter_mut().fold(0u32, |carry, val| {
                let c = *val << (32-s);
                *val >>= s;
                debug_assert!(*val & carry == 0);
                *val |= carry;
                c
            });
        }
        self
    }
}

fn slice_sub_assign(lhs : &mut [u32], rhs: &[u32]) -> bool{
    debug_assert_eq!(lhs.len(), rhs.len());
    lhs.iter_mut().zip(rhs.iter()).rev().fold(false,|carry,(i,s)| {
        //don't ask me why, but this branching monstrosity seems faster than checked_add(), overflowing_sub()...
        let (s, overflow) = s.overflowing_add(carry as u32);
        if !overflow && *i >= s {
            *i -= s as u32;
            false
        } else {
            *i = i.wrapping_sub(s as u32);
            true
        }
    })
}

fn slice_add_assign(lhs : &mut [u32], rhs : &[u32]) -> bool {
    debug_assert_eq!(lhs.len(), rhs.len());
    lhs.iter_mut().zip(rhs.iter()).rev().fold(false, |carry, (a, b)| {
        let r = a.overflowing_add(*b);
        let s = r.0.overflowing_add(carry as u32);
        *a = s.0;
        r.1 || s.1
    })
}

#[cfg(test)]
mod iterative_conversion_impl_tests{
    use super::*;
    use rand::RngCore;
    use rand_xoshiro::rand_core::SeedableRng;
    use rand_xoshiro::Xoshiro256Plus;

    /// Tests specifically the case that will_overflow is true.
    #[test]
    fn knuth_add_back_test(){
        let mut dividend = ArbitraryBytes::new([
            //m = 3, n=5
            u32::MAX,
            u32::MAX,
            u32::MAX-1,
            u32::MAX,
            u32::MAX,
            0,
            0,
            3
        ]);
        let divisor = ArbitraryBytes::new([
            0,
            0,
            0,
            0,
            0,
            u32::MAX,
            u32::MAX,
            u32::MAX,
        ]);
        let result = dividend.rem_assign_with_quotient(&divisor);
        assert_eq!(dividend.0, [0,0,0,0,0,0,0,2]);
        assert_eq!(result.0, [0,0,0,u32::MAX,u32::MAX, u32::MAX, u32::MAX, u32::MAX]);
    }


    fn prepare_many_numbers() -> Vec<(ArbitraryBytes<5>,ArbitraryBytes<5>, u128, u128)>{
        let mut rng = Xoshiro256Plus::seed_from_u64(0);
        let mut res = Vec::new();
        for _i in 0..1000000 {
            let dx = rng.next_u32() % 3 + 2; //at least 2 digits, at max 4 (u128)
            let dy = rng.next_u32() % 3 + 2;
            let ds = dx.min(dy);
            let dl = dx.max(dy);
            let dividendx = [
                0,
                if dl == 4 { rng.next_u32() } else { 0 },
                if dl >=3 { rng.next_u32() } else {0},
                rng.next_u32(),
                rng.next_u32(),
            ];
            let divisorx = [
                0,
                if ds == 4 { rng.next_u32() } else { 0 },
                if ds >=3 { rng.next_u32() } else {0},
                rng.next_u32(),
                rng.next_u32(),
            ];
            let needs_swap = ds == dl && dividendx[5-ds as usize] < divisorx[5-ds as usize];
            let dividend = ArbitraryBytes::new(if needs_swap { divisorx } else {dividendx});
            let divisor = ArbitraryBytes::new(if needs_swap {dividendx} else {divisorx});
            assert!(dividend.ge(&divisor));

            let td = 
                ((dividend.0[1] as u128)<<96)
              + ((dividend.0[2] as u128)<<64)
              + ((dividend.0[3] as u128)<<32)
              + (dividend.0[4] as u128);
            let tn = 
                ((divisor.0[1] as u128)<<96)
              + ((divisor.0[2] as u128)<<64)
              + ((divisor.0[3] as u128)<<32)
              + (divisor.0[4] as u128);


            res.push((dividend, divisor, td/tn, td%tn));
        }
        res
    }

    /// Just tests a bunch of procedurally generated numbers (all within u128 for easy comparison.)
    #[test]
    fn knuth_many_numbers_test() {
        let input = prepare_many_numbers();
        for (mut dividend, divisor, expected_quotient, expexted_remainder) in input {
            let quotient = dividend.rem_assign_with_quotient_knuth(&divisor);
            let remainder = dividend;
            let quotient = ((quotient.0[1] as u128)<<(96)) + ((quotient.0[2] as u128)<<64) + ((quotient.0[3] as u128)<<32) + (quotient.0[4] as u128);
            let remainder = ((remainder.0[1] as u128)<<(96)) + ((remainder.0[2] as u128)<<64) + ((remainder.0[3] as u128)<<32) + (remainder.0[4] as u128);
            assert_eq!(quotient, expected_quotient);
            assert_eq!(remainder, expexted_remainder);
        }
    }

    #[test]
    fn rem_assign_with_quotient_u32_test(){
        let mut a = ArbitraryBytes::new([0xaf4a816a,0xb414f734,0x7a2167c7,0x47ea7314,0xfba75574]);
        let quotient = a.rem_assign_with_quotient_u32(&0x12345);
        assert_eq!(quotient.0, [0x9A10,0xB282B7BA,0xE4948E98,0x2AE63D74,0xE6FDFF4A]);
        assert_eq!(a.0, [0,0,0,0,0x6882]);
    }

    #[test]
    fn sub_assign_test() {
        let mut a = ArbitraryBytes::new([0xaf4a816a,0xb414f734,0x7a2167c7,0x47ea7314,0xfba75574]);
        let b = ArbitraryBytes::new([0x42a7bf02,0xffffffff,0xc7138bd5,0x12345678,0xabcde012]);
        let carry = slice_sub_assign(&mut a.0,&b.0);
        assert!(!carry);
        assert_eq!(a.0, [0x6CA2C267,0xb414f734,0xb30ddbf2,0x35b61c9c,0x4fd97562]);
    }

    #[test]
    fn sub_assign_test2() {
        let mut a = ArbitraryBytes::new([0x42a7bf02,0xffffffff,0xc7138bd5,0x12345678,0xabcde012]);
        let b = ArbitraryBytes::new([0xaf4a816a,0xb414f734,0x7a2167c7,0x47ea7314,0xfba75574]);
        let carry = slice_sub_assign(&mut a.0,&b.0);
        assert!(carry);
        assert_eq!(a.0, [0x935D3D98,0x4BEB08CB,0x4CF2240D,0xCA49E363,0xB0268A9E]);
    }

    #[test]
    fn add_assign_test() {
        let mut a = ArbitraryBytes::new([0x42a7bf02,0xffffffff,0xc7138bd5,0x12345678,0xabcde012]);
        let b = ArbitraryBytes::new([0xaf4a816a,0xb414f734,0x7a2167c7,0x47ea7314,0xfba75574]);
        let carry = slice_add_assign(&mut a.0,&b.0);
        assert!(!carry);
        assert_eq!(a.0, [0xF1F2406D,0xB414F734,0x4134F39C,0x5A1EC98D,0xA7753586]);
    }
    #[test]
    fn add_assign_test2() {
        let mut a = ArbitraryBytes::new([0x42a7bf02,0xffffffff,0xc7138bd5,0x12345678,0xabcde012]);
        let b = ArbitraryBytes::new([0xbf4a816a,0xb414f734,0x7a2167c7,0x47ea7314,0xfba75574]);
        let carry = slice_add_assign(&mut a.0,&b.0);
        assert!(carry);
        assert_eq!(a.0, [0x01F2406D,0xB414F734,0x4134F39C,0x5A1EC98D,0xA7753586]);
    }

    #[test]
    fn shift_left_test() {
        let a = ArbitraryBytes::new([0x42a7bf02,0xffffffff,0xc7138bd5,0x12345678,0xabcde012]);
        let b = a.shift_left(7);
        assert_eq!(b.0,[0x21, 0x53DF817F,0xFFFFFFE3, 0x89C5EA89, 0x1A2B3C55, 0xE6F00900]);
    }
    
    #[test]
    fn shift_right_test() {
        let a = ArbitraryBytes::new([0x21, 0x53DF817F,0xFFFFFFE3, 0x89C5EA89, 0x1A2B3C55, 0xE6F00900]);
        let b = a.shift_right(7);
        assert_eq!(b.0,[0, 0x42a7bf02,0xffffffff,0xc7138bd5,0x12345678,0xabcde012]);
    }

    #[test]
    fn get_digit_from_right_test(){
        let a = ArbitraryBytes::new([0x42a7bf02,0xffffffff,0xc7138bd5,0x12345678,0xabcde012]);
        assert_eq!(a.get_digit_from_right(3), 0xffffffff);
    }

    #[test]
    fn set_digit_from_right_test(){
        let mut a = ArbitraryBytes::new([0x42a7bf02,0xffffffff,0xc7138bd5,0x12345678,0xabcde012]);
        a.set_digit_from_right(0xdeadbeef, 4);
        assert_eq!(a.0[0], 0xdeadbeef);
    }

    #[test]
    fn find_first_nonzero_digit_test() {
        let a = ArbitraryBytes::new([0,0,0,0x12345678,0xabcde012]);
        assert_eq!(a.find_first_nonzero_digit(),3);
    }

    #[test]
    fn mul_arbitrary_test(){
        let a = ArbitraryBytes::new([0,0,0,0x47ea7314,0xfba75574]);
        let b = ArbitraryBytes::new([0,0,0,0x12345678,0xabcde012]);
        let a_big = (0x47ea7314_u128 << 32) | 0xfba75574u128;
        let b_big = (0x12345678_u128 << 32) | 0xabcde012u128;
        let c_big = a_big*b_big;
        let c = (&a * &b).unwrap();
        assert_eq!(c_big & 0xffff_ffff, c.0[4] as u128 );
        assert_eq!((c_big >> 32 ) & 0xffff_ffff, c.0[3] as u128);
        assert_eq!((c_big >> 64 ) & 0xffff_ffff, c.0[2] as u128);
        assert_eq!((c_big >> 96 ) & 0xffff_ffff, c.0[1] as u128);
        assert_eq!(0, c.0[0]);
    }
    #[test]
    fn mul_arbitrary_test_2(){
        let a = ArbitraryBytes::new([0x2763ac9f,0xd1ae1f38,0x1753a5c7,0x47ea7314,0xfba75574]);
        let b = ArbitraryBytes::new([0,0,0,0,2]);
        let c = (&a * &b).unwrap();
        assert_eq!(0x4EC7593F, c.0[0]);
        assert_eq!(0xA35C3E70, c.0[1]);
        assert_eq!(2*0x1753a5c7, c.0[2]);
        assert_eq!(0x8fd4e629, c.0[3]);
        assert_eq!(0xf74eaae8, c.0[4]);
    }
    #[test]
    fn mul_arbitrary_test_3(){
        let a = ArbitraryBytes::new([0,0,0,0,2]);
        let b = ArbitraryBytes::new([0x2763ac9f,0xd1ae1f38,0x1753a5c7,0x47ea7314,0xfba75574]);
        let c = (&a * &b).unwrap();
        assert_eq!(0x4EC7593F, c.0[0]);
        assert_eq!(0xA35C3E70, c.0[1]);
        assert_eq!(2*0x1753a5c7, c.0[2]);
        assert_eq!(0x8fd4e629, c.0[3]);
        assert_eq!(0xf74eaae8, c.0[4]);
    }
    #[test]
    fn mul_arbitrary_test_4(){
        let a = ArbitraryBytes::new([0,0,0,0,8]);
        let b = ArbitraryBytes::new([0x2763ac9f,0xd1ae1f38,0x1753a5c7,0x47ea7314,0xfba75574]);
        let c = &a * &b;
        assert!(c.is_none())
    }
    #[test]
    fn mul_arbitrary_test_5(){
        let a = ArbitraryBytes::new([0,0,0,1,0]);
        let b = ArbitraryBytes::new([0x2763ac9f,0xd1ae1f38,0x1753a5c7,0x47ea7314,0xfba75574]);
        let c = &a * &b;
        assert!(c.is_none())
    }
    #[test]
    fn mul_arbitrary_test_6(){
        let a = ArbitraryBytes::new([0,0,0,1,1]);
        let b = ArbitraryBytes::new([0,0xffffffff,0x1753a5c7,0x47ea7314,0xfba75574]);
        let c = &a * &b;
        assert!(c.is_none())
    }
}