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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
use crate::core_crypto::prelude::{CastFrom, Numeric};
use std::ops::ShlAssign;

// Little endian order
#[derive(Default, Copy, Clone, Debug, PartialEq, Eq)]
#[repr(transparent)]
pub struct U512(pub(crate) [u64; 8]);

impl U512 {
    pub const BITS: u32 = 512;
    pub const MAX: Self = Self([u64::MAX; 8]);
    pub const MIN: Self = Self([0; 8]);
    pub const ZERO: Self = Self([0; 8]);
    pub const ONE: Self = Self([1, 0, 0, 0, 0, 0, 0, 0]);
    pub const TWO: Self = Self([2, 0, 0, 0, 0, 0, 0, 0]);

    /// Replaces the current value by interpreting the bytes in big endian order
    pub fn copy_from_be_byte_slice(&mut self, bytes: &[u8]) {
        super::algorithms::copy_from_be_byte_slice(self.0.as_mut_slice(), bytes);
    }

    /// Replaces the current value by interpreting the bytes in little endian order
    pub fn copy_from_le_byte_slice(&mut self, bytes: &[u8]) {
        super::algorithms::copy_from_le_byte_slice(self.0.as_mut_slice(), bytes);
    }

    pub fn copy_to_le_byte_slice(&self, bytes: &mut [u8]) {
        super::algorithms::copy_to_le_byte_slice(self.0.as_slice(), bytes);
    }

    pub fn copy_to_be_byte_slice(&self, bytes: &mut [u8]) {
        super::algorithms::copy_to_be_byte_slice(self.0.as_slice(), bytes);
    }
    pub fn is_power_of_two(self) -> bool {
        if self == Self::ZERO {
            return false;
        }
        (self & (self - Self::ONE)) == Self::ZERO
    }

    pub fn leading_zeros(self) -> u32 {
        super::algorithms::leading_zeros(self.0.as_slice())
    }

    pub fn ilog2(self) -> u32 {
        // Rust has the same assert
        assert!(
            self > Self::ZERO,
            "argument of integer logarithm must be positive"
        );
        (self.0.len() as u32 * u64::BITS) - self.leading_zeros() - 1
    }

    pub fn ceil_ilog2(self) -> u32 {
        self.ilog2() + u32::from(!self.is_power_of_two())
    }
}

#[cfg(test)]
impl rand::distributions::Distribution<U512> for rand::distributions::Standard {
    fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> U512 {
        let mut s = U512::ZERO;
        rng.fill(s.0.as_mut_slice());
        s
    }
}

impl std::cmp::Ord for U512 {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        super::algorithms::compare(&self.0, &other.0)
    }
}

impl std::cmp::PartialOrd for U512 {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl std::ops::Add<Self> for U512 {
    type Output = Self;

    fn add(mut self, rhs: Self) -> Self::Output {
        self += rhs;
        self
    }
}

impl std::ops::AddAssign<Self> for U512 {
    fn add_assign(&mut self, rhs: Self) {
        super::algorithms::add_assign_words(self.0.as_mut_slice(), rhs.0.as_slice())
    }
}

impl std::ops::Sub<Self> for U512 {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self::Output {
        let negated = !rhs + Self::from(1u64);
        self + negated
    }
}

impl std::ops::SubAssign<Self> for U512 {
    fn sub_assign(&mut self, rhs: Self) {
        *self = *self - rhs;
    }
}

impl std::ops::Shr<u32> for U512 {
    type Output = Self;

    fn shr(mut self, rhs: u32) -> Self::Output {
        self >>= rhs;
        self
    }
}

impl std::ops::MulAssign<Self> for U512 {
    fn mul_assign(&mut self, rhs: Self) {
        if rhs.is_power_of_two() {
            self.shl_assign(rhs.ilog2());
            return;
        }
        super::algorithms::schoolbook_mul_assign(self.0.as_mut_slice(), rhs.0.as_slice());
    }
}

impl std::ops::Mul<Self> for U512 {
    type Output = Self;

    fn mul(mut self, rhs: Self) -> Self::Output {
        self *= rhs;
        self
    }
}

impl std::ops::DivAssign<Self> for U512 {
    fn div_assign(&mut self, rhs: Self) {
        *self = *self / rhs;
    }
}

impl std::ops::Div<Self> for U512 {
    type Output = Self;

    fn div(self, rhs: Self) -> Self::Output {
        let (q, _) = super::algorithms::slow_div(self, rhs);
        q
    }
}

impl std::ops::RemAssign<Self> for U512 {
    fn rem_assign(&mut self, rhs: Self) {
        *self = *self % rhs;
    }
}

impl std::ops::Rem<Self> for U512 {
    type Output = Self;

    fn rem(self, rhs: Self) -> Self::Output {
        let (_, r) = super::algorithms::slow_div(self, rhs);
        r
    }
}

impl std::ops::ShrAssign<u32> for U512 {
    fn shr_assign(&mut self, shift: u32) {
        super::algorithms::shr_assign(self.0.as_mut_slice(), shift);
    }
}

impl std::ops::Shl<u32> for U512 {
    type Output = Self;

    fn shl(mut self, rhs: u32) -> Self::Output {
        self <<= rhs;
        self
    }
}

impl std::ops::ShlAssign<u32> for U512 {
    fn shl_assign(&mut self, shift: u32) {
        super::algorithms::shl_assign(self.0.as_mut_slice(), shift);
    }
}

impl std::ops::Shl<usize> for U512 {
    type Output = Self;

    fn shl(mut self, rhs: usize) -> Self::Output {
        self <<= rhs;
        self
    }
}

impl std::ops::ShrAssign<usize> for U512 {
    fn shr_assign(&mut self, shift: usize) {
        super::algorithms::shr_assign(self.0.as_mut_slice(), shift as u32);
    }
}

impl std::ops::Shr<usize> for U512 {
    type Output = Self;

    fn shr(mut self, rhs: usize) -> Self::Output {
        self >>= rhs;
        self
    }
}

impl std::ops::ShlAssign<usize> for U512 {
    fn shl_assign(&mut self, shift: usize) {
        super::algorithms::shl_assign(self.0.as_mut_slice(), shift as u32);
    }
}

impl std::ops::Not for U512 {
    type Output = Self;

    fn not(mut self) -> Self::Output {
        super::algorithms::bitnot_assign(self.0.as_mut_slice());
        self
    }
}

impl std::ops::BitAnd<Self> for U512 {
    type Output = Self;

    fn bitand(mut self, rhs: Self) -> Self::Output {
        self &= rhs;
        self
    }
}

impl std::ops::BitAndAssign<Self> for U512 {
    fn bitand_assign(&mut self, rhs: Self) {
        super::algorithms::bitand_assign(self.0.as_mut_slice(), rhs.0.as_slice())
    }
}

impl std::ops::BitOrAssign<Self> for U512 {
    fn bitor_assign(&mut self, rhs: Self) {
        super::algorithms::bitor_assign(self.0.as_mut_slice(), rhs.0.as_slice())
    }
}

impl std::ops::BitOr<Self> for U512 {
    type Output = Self;

    fn bitor(mut self, rhs: Self) -> Self::Output {
        self |= rhs;
        self
    }
}

impl std::ops::BitXorAssign<Self> for U512 {
    fn bitxor_assign(&mut self, rhs: Self) {
        super::algorithms::bitxor_assign(self.0.as_mut_slice(), rhs.0.as_slice())
    }
}

impl std::ops::BitXor<Self> for U512 {
    type Output = Self;

    fn bitxor(mut self, rhs: Self) -> Self::Output {
        self ^= rhs;
        self
    }
}

impl From<u8> for U512 {
    fn from(value: u8) -> Self {
        Self::from(value as u128)
    }
}

impl From<u16> for U512 {
    fn from(value: u16) -> Self {
        Self::from(value as u128)
    }
}

impl From<u32> for U512 {
    fn from(value: u32) -> Self {
        Self::from(value as u128)
    }
}

impl From<u64> for U512 {
    fn from(value: u64) -> Self {
        Self::from(value as u128)
    }
}

impl From<u128> for U512 {
    fn from(value: u128) -> Self {
        Self([
            (value & u128::from(u64::MAX)) as u64,
            (value >> 64) as u64,
            0,
            0,
            0,
            0,
            0,
            0,
        ])
    }
}

impl From<(u128, u128, u128, u128)> for U512 {
    fn from((value0, value1, value2, value3): (u128, u128, u128, u128)) -> Self {
        Self([
            (value0 & u128::from(u64::MAX)) as u64,
            (value0 >> 64) as u64,
            (value1 & u128::from(u64::MAX)) as u64,
            (value1 >> 64) as u64,
            (value2 & u128::from(u64::MAX)) as u64,
            (value2 >> 64) as u64,
            (value3 & u128::from(u64::MAX)) as u64,
            (value3 >> 64) as u64,
        ])
    }
}

impl CastFrom<U512> for u64 {
    fn cast_from(input: U512) -> Self {
        input.0[0]
    }
}

impl CastFrom<U512> for u8 {
    fn cast_from(input: U512) -> Self {
        input.0[0] as u8
    }
}

impl CastFrom<crate::integer::U256> for U512 {
    fn cast_from(input: crate::integer::U256) -> Self {
        Self([input.0[0], input.0[1], input.0[2], input.0[3], 0, 0, 0, 0])
    }
}

impl CastFrom<u32> for U512 {
    fn cast_from(input: u32) -> Self {
        Self::from(input)
    }
}

impl CastFrom<u64> for U512 {
    fn cast_from(input: u64) -> Self {
        Self::from(input)
    }
}

impl CastFrom<u8> for U512 {
    fn cast_from(input: u8) -> Self {
        Self::from(input as u64)
    }
}

impl From<bool> for U512 {
    fn from(input: bool) -> Self {
        Self::from(if input { 1u64 } else { 0u64 })
    }
}

// SAFETY
//
// U512 is allowed to be all zeros
unsafe impl bytemuck::Zeroable for U512 {}

// SAFETY
//
// u64 impl bytemuck::Pod,
// [T; N] impl bytemuck::Pod if T: bytemuck::Pod
//
// https://docs.rs/bytemuck/latest/bytemuck/trait.Pod.html#foreign-impls
//
// Thus U512 can safely be considered Pod
unsafe impl bytemuck::Pod for U512 {}

impl Numeric for U512 {
    const BITS: usize = Self::BITS as usize;

    const ZERO: Self = Self::ZERO;

    const ONE: Self = Self::ONE;

    const TWO: Self = Self::TWO;

    const MAX: Self = Self::MAX;
}

#[cfg(test)]
mod tests {
    use super::super::{u64_with_even_bits_set, u64_with_odd_bits_set};
    use super::*;
    use std::panic::catch_unwind;

    #[test]
    fn test_u64_even_odd_bits() {
        let all_even_bits_set = u64_with_even_bits_set();
        let all_odd_bits_set = u64_with_odd_bits_set();

        assert_ne!(all_odd_bits_set, all_even_bits_set);

        assert_eq!(all_even_bits_set.rotate_right(1), all_odd_bits_set);
        assert_eq!(all_even_bits_set, all_odd_bits_set.rotate_left(1));
    }

    #[test]
    fn test_bitand() {
        let all_even_bits_set = U512([u64_with_even_bits_set(); 8]);
        let all_odd_bits_set = U512([u64_with_odd_bits_set(); 8]);

        assert_ne!(all_odd_bits_set, all_even_bits_set);
        assert_eq!(all_odd_bits_set & all_odd_bits_set, all_odd_bits_set);
        assert_eq!(all_even_bits_set & all_even_bits_set, all_even_bits_set);
        assert_eq!(all_even_bits_set & all_odd_bits_set, U512::ZERO);
    }

    #[test]
    fn test_bitor() {
        let all_even_bits_set = U512([u64_with_even_bits_set(); 8]);
        let all_odd_bits_set = U512([u64_with_odd_bits_set(); 8]);

        assert_ne!(all_odd_bits_set, all_even_bits_set);
        assert_eq!(all_odd_bits_set | all_odd_bits_set, all_odd_bits_set);
        assert_eq!(all_even_bits_set | all_even_bits_set, all_even_bits_set);
        assert_eq!(all_even_bits_set | all_odd_bits_set, U512::MAX);
    }

    #[test]
    fn test_bitxor() {
        let all_even_bits_set = U512([u64_with_even_bits_set(); 8]);
        let all_odd_bits_set = U512([u64_with_odd_bits_set(); 8]);

        assert_ne!(all_odd_bits_set, all_even_bits_set);
        assert_eq!(all_odd_bits_set ^ all_odd_bits_set, U512::ZERO);
        assert_eq!(all_even_bits_set ^ all_even_bits_set, U512::ZERO);
        assert_eq!(all_even_bits_set ^ all_odd_bits_set, U512::MAX);
    }

    #[test]
    fn test_is_power_of_two() {
        assert!(!U512::ZERO.is_power_of_two());
        assert!(!U512::MAX.is_power_of_two());
        assert!(!U512::from(8329842348123u64).is_power_of_two());

        for i in 0..U512::BITS {
            assert!((U512::ONE << i).is_power_of_two())
        }
    }

    #[test]
    fn test_ilog2() {
        assert!(catch_unwind(|| { U512::ZERO.ilog2() }).is_err());

        assert_eq!(U512::MAX.ilog2(), 511);
        assert_eq!(
            U512::from(8329842348123u64).ilog2(),
            8329842348123u64.ilog2()
        );

        assert_eq!(
            U512::from(8320912948329842348123u128).ilog2(),
            8320912948329842348123u128.ilog2()
        );

        assert_eq!(
            U512::from(2323912928329942718123u128).ilog2(),
            2323912928329942718123u128.ilog2()
        );

        for i in 0..U512::BITS {
            assert_eq!((U512::ONE << i).ilog2(), i)
        }
    }

    #[test]
    fn test_add_wrap_around() {
        assert_eq!(U512::MAX + U512::from(1u32), U512::MIN);
    }

    #[test]
    fn test_sub_wrap_around() {
        assert_eq!(U512::MIN - U512::from(1u32), U512::MAX);
    }

    #[test]
    fn test_bitnot() {
        assert_eq!(!U512::MAX, U512::MIN);
        assert_eq!(!U512::MIN, U512::MAX);

        // To prove we are testing the correct thing
        assert_eq!(!u128::MAX, u128::MIN);
        assert_eq!(!u128::MIN, u128::MAX);
    }

    #[test]
    fn test_shl_limits() {
        assert_eq!(U512::ONE << 512u32, U512::ONE << (512u32 % U512::BITS));
        assert_eq!(U512::ONE << 513u32, U512::ONE << (513u32 % U512::BITS));

        // We aim to have same behaviour as rust native types
        assert_eq!(1u128.wrapping_shl(128), 1u128 << (128 % u128::BITS));
        assert_eq!(1u128.wrapping_shl(129), 1u128 << (129 % u128::BITS));
    }

    #[test]
    fn test_shr_limits() {
        assert_eq!(U512::MAX >> 512u32, U512::MAX >> (512u32 % U512::BITS));
        assert_eq!(U512::MAX >> 513u32, U512::MAX >> (513u32 % U512::BITS));

        // We aim to have same behaviour as rust native types
        assert_eq!(u128::MAX.wrapping_shr(128), u128::MAX >> (128 % u128::BITS));
        assert_eq!(u128::MAX.wrapping_shr(129), u128::MAX >> (129 % u128::BITS));
    }

    #[test]
    fn test_div_rem() {
        let a = U512([
            14069555808489703714,
            3908590842307590452,
            9855978718424440147,
            13896218366640697882,
            12201638264960915474,
            18057167307258037924,
            3883391760393286388,
            13564609649884598296,
        ]);

        let b = U512([
            13572348791597489471,
            13066001923991081603,
            15152085515464322039,
            5769296961266547928,
            4345306966403270406,
            14861723594231331435,
            12450980561267212543,
            13551971813301316858,
        ]);

        let expected_d = U512([1, 0, 0, 0, 0, 0, 0, 0]);

        let expected_r = U512([
            497207016892214243,
            9289332992026060465,
            13150637276669669723,
            8126921405374149953,
            7856331298557645068,
            3195443713026706489,
            9879155272835625461,
            12637836583281437,
        ]);

        let d = a / b;
        let r = a % b;
        assert_eq!(d, expected_d);
        assert_eq!(r, expected_r);

        let a = U512([
            3916138088563380184,
            11471505607132531241,
            11764394181449351249,
            3009844506142576707,
            5954421029540908215,
            493654323038934126,
            6030625548640772789,
            12886253569586615920,
        ]);

        let b = U512([
            15734844303166734968,
            18157387122068819778,
            7947001930394566593,
            11842813283329086337,
            13455790396220372987,
            16771555752599506498,
            12734235444062124671,
            984106389135760972,
        ]);

        let expected_d = U512([13, 0, 0, 0, 0, 0, 0, 0]);

        let expected_r = U512([
            2277346958200893376,
            15233145978462045124,
            687089454867743607,
            15073968486250418865,
            15496586615771575535,
            3824358423759969034,
            6506261439219116598,
            92870510821723275,
        ]);

        let d = a / b;
        let r = a % b;
        assert_eq!(d, expected_d);
        assert_eq!(r, expected_r);
    }

    #[test]
    fn test_mul() {
        let a = U512([
            14069555808489703714,
            3908590842307590452,
            9855978718424440147,
            13896218366640697882,
            12201638264960915474,
            18057167307258037924,
            3883391760393286388,
            13564609649884598296,
        ]);

        let b = U512([
            13572348791597489471,
            13066001923991081603,
            15152085515464322039,
            5769296961266547928,
            4345306966403270406,
            14861723594231331435,
            12450980561267212543,
            13551971813301316858,
        ]);

        let expected_result = U512([
            4156026748591446366,
            5494944174831101103,
            18238946655790339985,
            7541952578616659641,
            13348226788602217476,
            12942926403796405442,
            8114377417900506174,
            14085526951817456422,
        ]);

        let result = a * b;
        assert_eq!(result, expected_result);

        let a = U512([
            3916138088563380184,
            11471505607132531241,
            11764394181449351249,
            3009844506142576707,
            5954421029540908215,
            493654323038934126,
            6030625548640772789,
            12886253569586615920,
        ]);

        let b = U512([
            15734844303166734968,
            18157387122068819778,
            7947001930394566593,
            11842813283329086337,
            13455790396220372987,
            16771555752599506498,
            12734235444062124671,
            984106389135760972,
        ]);

        let expected_result = U512([
            959128001500093760,
            16717200888364732243,
            4987629022208480162,
            13025491345955469749,
            3380626290232599789,
            4346647371474752052,
            13274445184588134645,
            9592080887013845353,
        ]);

        let result = a * b;
        assert_eq!(result, expected_result);
    }

    #[test]
    fn test_le_byte_slice() {
        // Create a u128 where each bytes stores its index:
        // u128 as &[u8] = [0u8, 1 , 2, 3, .., 15]
        let v0 = u128::from_le_bytes(core::array::from_fn::<u8, 16, _>(|i| i as u8));
        let v1 = u128::from_le_bytes(core::array::from_fn::<u8, 16, _>(|i| 16 + i as u8));
        let v2 = u128::from_le_bytes(core::array::from_fn::<u8, 16, _>(|i| 32 + i as u8));
        let v3 = u128::from_le_bytes(core::array::from_fn::<u8, 16, _>(|i| 48 + i as u8));

        let mut le_bytes = vec![0u8; 64];
        le_bytes[..16].copy_from_slice(v0.to_le_bytes().as_slice());
        le_bytes[16..32].copy_from_slice(v1.to_le_bytes().as_slice());
        le_bytes[32..48].copy_from_slice(v2.to_le_bytes().as_slice());
        le_bytes[48..].copy_from_slice(v3.to_le_bytes().as_slice());

        let mut b = U512::from(1u128 << 64); // To make sure copy cleans self
        b.copy_from_le_byte_slice(le_bytes.as_slice());

        assert_eq!(b, U512::from((v0, v1, v2, v3)));

        let mut le_bytes_2 = vec![0u8; 64];
        b.copy_to_le_byte_slice(&mut le_bytes_2);

        assert_eq!(le_bytes_2, le_bytes);
    }

    #[test]
    fn test_be_byte_slice() {
        let v0 = u128::from_le_bytes(core::array::from_fn::<u8, 16, _>(|i| i as u8));
        let v1 = u128::from_le_bytes(core::array::from_fn::<u8, 16, _>(|i| 16 + i as u8));
        let v2 = u128::from_le_bytes(core::array::from_fn::<u8, 16, _>(|i| 32 + i as u8));
        let v3 = u128::from_le_bytes(core::array::from_fn::<u8, 16, _>(|i| 48 + i as u8));

        let mut be_bytes = vec![0u8; 64];
        be_bytes[..16].copy_from_slice(v3.to_be_bytes().as_slice());
        be_bytes[16..32].copy_from_slice(v2.to_be_bytes().as_slice());
        be_bytes[32..48].copy_from_slice(v1.to_be_bytes().as_slice());
        be_bytes[48..].copy_from_slice(v0.to_be_bytes().as_slice());

        let mut b = U512::from(1u128 << 64); // To make sure copy cleans self
        b.copy_from_be_byte_slice(be_bytes.as_slice());

        assert_eq!(b, U512::from((v0, v1, v2, v3)));

        let mut be_bytes_2 = vec![0u8; 64];
        b.copy_to_be_byte_slice(&mut be_bytes_2);

        assert_eq!(be_bytes_2, be_bytes);
    }
}