lambdaworks_math/unsigned_integer/
element.rs

1use core::cmp::Ordering;
2use core::convert::From;
3use core::ops::{
4    Add, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Mul, Shl, Shr, ShrAssign,
5    Sub,
6};
7
8#[cfg(feature = "proptest")]
9use proptest::{
10    arbitrary::Arbitrary,
11    prelude::any,
12    strategy::{SBoxedStrategy, Strategy},
13};
14
15use crate::errors::ByteConversionError;
16use crate::errors::CreationError;
17#[cfg(feature = "alloc")]
18use crate::traits::AsBytes;
19use crate::traits::ByteConversion;
20use crate::unsigned_integer::traits::IsUnsignedInteger;
21
22use core::fmt::{self, Debug, Display};
23
24pub type U384 = UnsignedInteger<6>;
25pub type U256 = UnsignedInteger<4>;
26pub type U128 = UnsignedInteger<2>;
27pub type U64 = UnsignedInteger<1>;
28
29/// A big unsigned integer in base 2^{64} represented
30/// as fixed-size array `limbs` of `u64` components.
31/// The most significant bit is in the left-most position.
32/// That is, the array `[a_n, ..., a_0]` represents the
33/// integer 2^{64 * n} * a_n + ... + 2^{64} * a_1 + a_0.
34#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
35pub struct UnsignedInteger<const NUM_LIMBS: usize> {
36    pub limbs: [u64; NUM_LIMBS],
37}
38
39impl<const NUM_LIMBS: usize> Default for UnsignedInteger<NUM_LIMBS> {
40    fn default() -> Self {
41        Self {
42            limbs: [0; NUM_LIMBS],
43        }
44    }
45}
46
47// NOTE: manually implementing `PartialOrd` may seem unorthodox, but the
48// derived implementation had terrible performance.
49#[allow(clippy::non_canonical_partial_ord_impl)]
50impl<const NUM_LIMBS: usize> PartialOrd for UnsignedInteger<NUM_LIMBS> {
51    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
52        let mut i = 0;
53        while i < NUM_LIMBS {
54            if self.limbs[i] != other.limbs[i] {
55                return Some(self.limbs[i].cmp(&other.limbs[i]));
56            }
57            i += 1;
58        }
59        Some(Ordering::Equal)
60    }
61}
62
63// NOTE: because we implemented `PartialOrd`, clippy asks us to implement
64// this manually too.
65impl<const NUM_LIMBS: usize> Ord for UnsignedInteger<NUM_LIMBS> {
66    fn cmp(&self, other: &Self) -> Ordering {
67        let mut i = 0;
68        while i < NUM_LIMBS {
69            if self.limbs[i] != other.limbs[i] {
70                return self.limbs[i].cmp(&other.limbs[i]);
71            }
72            i += 1;
73        }
74        Ordering::Equal
75    }
76}
77
78/// impl from u128 for UnSignedInteger
79impl<const NUM_LIMBS: usize> From<u128> for UnsignedInteger<NUM_LIMBS> {
80    fn from(value: u128) -> Self {
81        let mut limbs = [0u64; NUM_LIMBS];
82        limbs[NUM_LIMBS - 1] = value as u64;
83        limbs[NUM_LIMBS - 2] = (value >> 64) as u64;
84        UnsignedInteger { limbs }
85    }
86}
87
88/// impl from u64 for UnSignedInteger
89impl<const NUM_LIMBS: usize> From<u64> for UnsignedInteger<NUM_LIMBS> {
90    fn from(value: u64) -> Self {
91        Self::from_u64(value)
92    }
93}
94
95/// impl from u16 for UnSignedInteger
96impl<const NUM_LIMBS: usize> From<u16> for UnsignedInteger<NUM_LIMBS> {
97    fn from(value: u16) -> Self {
98        let mut limbs = [0u64; NUM_LIMBS];
99        limbs[NUM_LIMBS - 1] = value as u64;
100        UnsignedInteger { limbs }
101    }
102}
103
104impl<const NUM_LIMBS: usize> From<&str> for UnsignedInteger<NUM_LIMBS> {
105    fn from(hex_str: &str) -> Self {
106        Self::from_hex_unchecked(hex_str)
107    }
108}
109
110impl<const NUM_LIMBS: usize> Display for UnsignedInteger<NUM_LIMBS> {
111    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
112        let mut limbs_iterator = self.limbs.iter().skip_while(|limb| **limb == 0).peekable();
113
114        if limbs_iterator.peek().is_none() {
115            write!(f, "0x0")?;
116        } else {
117            write!(f, "0x")?;
118            if let Some(most_significant_limb) = limbs_iterator.next() {
119                write!(f, "{most_significant_limb:x}")?;
120            }
121
122            for limb in limbs_iterator {
123                write!(f, "{limb:016x}")?;
124            }
125        }
126
127        Ok(())
128    }
129}
130
131// impl Add for both references and variables
132
133impl<const NUM_LIMBS: usize> Add<&UnsignedInteger<NUM_LIMBS>> for &UnsignedInteger<NUM_LIMBS> {
134    type Output = UnsignedInteger<NUM_LIMBS>;
135
136    fn add(self, other: &UnsignedInteger<NUM_LIMBS>) -> UnsignedInteger<NUM_LIMBS> {
137        let (result, overflow) = UnsignedInteger::add(self, other);
138        debug_assert!(!overflow, "UnsignedInteger addition overflow.");
139        result
140    }
141}
142
143impl<const NUM_LIMBS: usize> Add<UnsignedInteger<NUM_LIMBS>> for UnsignedInteger<NUM_LIMBS> {
144    type Output = UnsignedInteger<NUM_LIMBS>;
145
146    fn add(self, other: UnsignedInteger<NUM_LIMBS>) -> UnsignedInteger<NUM_LIMBS> {
147        &self + &other
148    }
149}
150
151impl<const NUM_LIMBS: usize> Add<&UnsignedInteger<NUM_LIMBS>> for UnsignedInteger<NUM_LIMBS> {
152    type Output = UnsignedInteger<NUM_LIMBS>;
153
154    fn add(self, other: &Self) -> Self {
155        &self + other
156    }
157}
158
159impl<const NUM_LIMBS: usize> Add<UnsignedInteger<NUM_LIMBS>> for &UnsignedInteger<NUM_LIMBS> {
160    type Output = UnsignedInteger<NUM_LIMBS>;
161
162    fn add(self, other: UnsignedInteger<NUM_LIMBS>) -> UnsignedInteger<NUM_LIMBS> {
163        self + &other
164    }
165}
166
167// impl Sub
168
169impl<const NUM_LIMBS: usize> Sub<&UnsignedInteger<NUM_LIMBS>> for &UnsignedInteger<NUM_LIMBS> {
170    type Output = UnsignedInteger<NUM_LIMBS>;
171
172    fn sub(self, other: &UnsignedInteger<NUM_LIMBS>) -> UnsignedInteger<NUM_LIMBS> {
173        let (result, overflow) = UnsignedInteger::sub(self, other);
174        debug_assert!(!overflow, "UnsignedInteger subtraction overflow.");
175        result
176    }
177}
178
179impl<const NUM_LIMBS: usize> Sub<UnsignedInteger<NUM_LIMBS>> for UnsignedInteger<NUM_LIMBS> {
180    type Output = UnsignedInteger<NUM_LIMBS>;
181
182    fn sub(self, other: UnsignedInteger<NUM_LIMBS>) -> UnsignedInteger<NUM_LIMBS> {
183        &self - &other
184    }
185}
186
187impl<const NUM_LIMBS: usize> Sub<&UnsignedInteger<NUM_LIMBS>> for UnsignedInteger<NUM_LIMBS> {
188    type Output = UnsignedInteger<NUM_LIMBS>;
189
190    fn sub(self, other: &Self) -> Self {
191        &self - other
192    }
193}
194
195impl<const NUM_LIMBS: usize> Sub<UnsignedInteger<NUM_LIMBS>> for &UnsignedInteger<NUM_LIMBS> {
196    type Output = UnsignedInteger<NUM_LIMBS>;
197    #[inline(always)]
198    fn sub(self, other: UnsignedInteger<NUM_LIMBS>) -> UnsignedInteger<NUM_LIMBS> {
199        self - &other
200    }
201}
202
203/// Multi-precision multiplication.
204/// Algorithm 14.12 of "Handbook of Applied Cryptography" (https://cacr.uwaterloo.ca/hac/)
205impl<const NUM_LIMBS: usize> Mul<&UnsignedInteger<NUM_LIMBS>> for &UnsignedInteger<NUM_LIMBS> {
206    type Output = UnsignedInteger<NUM_LIMBS>;
207
208    #[inline(always)]
209    fn mul(self, other: &UnsignedInteger<NUM_LIMBS>) -> UnsignedInteger<NUM_LIMBS> {
210        let (mut n, mut t) = (0, 0);
211        for i in (0..NUM_LIMBS).rev() {
212            if self.limbs[i] != 0u64 {
213                n = NUM_LIMBS - 1 - i;
214            }
215            if other.limbs[i] != 0u64 {
216                t = NUM_LIMBS - 1 - i;
217            }
218        }
219        debug_assert!(
220            n + t < NUM_LIMBS,
221            "UnsignedInteger multiplication overflow."
222        );
223
224        // 1.
225        let mut limbs = [0u64; NUM_LIMBS];
226        // 2.
227        let mut carry = 0u128;
228        for i in 0..=t {
229            // 2.2
230            for j in 0..=n {
231                let uv = (limbs[NUM_LIMBS - 1 - (i + j)] as u128)
232                    + (self.limbs[NUM_LIMBS - 1 - j] as u128)
233                        * (other.limbs[NUM_LIMBS - 1 - i] as u128)
234                    + carry;
235                carry = uv >> 64;
236                limbs[NUM_LIMBS - 1 - (i + j)] = uv as u64;
237            }
238            if i + n + 1 < NUM_LIMBS {
239                // 2.3
240                limbs[NUM_LIMBS - 1 - (i + n + 1)] = carry as u64;
241                carry = 0;
242            }
243        }
244        assert_eq!(carry, 0, "UnsignedInteger multiplication overflow.");
245        // 3.
246        Self::Output { limbs }
247    }
248}
249
250impl<const NUM_LIMBS: usize> Mul<UnsignedInteger<NUM_LIMBS>> for UnsignedInteger<NUM_LIMBS> {
251    type Output = UnsignedInteger<NUM_LIMBS>;
252    #[inline(always)]
253    fn mul(self, other: UnsignedInteger<NUM_LIMBS>) -> UnsignedInteger<NUM_LIMBS> {
254        &self * &other
255    }
256}
257
258impl<const NUM_LIMBS: usize> Mul<&UnsignedInteger<NUM_LIMBS>> for UnsignedInteger<NUM_LIMBS> {
259    type Output = UnsignedInteger<NUM_LIMBS>;
260    #[inline(always)]
261    fn mul(self, other: &Self) -> Self {
262        &self * other
263    }
264}
265
266impl<const NUM_LIMBS: usize> Mul<UnsignedInteger<NUM_LIMBS>> for &UnsignedInteger<NUM_LIMBS> {
267    type Output = UnsignedInteger<NUM_LIMBS>;
268    #[inline(always)]
269    fn mul(self, other: UnsignedInteger<NUM_LIMBS>) -> UnsignedInteger<NUM_LIMBS> {
270        self * &other
271    }
272}
273
274impl<const NUM_LIMBS: usize> Shl<usize> for &UnsignedInteger<NUM_LIMBS> {
275    type Output = UnsignedInteger<NUM_LIMBS>;
276    #[inline(always)]
277    fn shl(self, times: usize) -> UnsignedInteger<NUM_LIMBS> {
278        self.const_shl(times)
279    }
280}
281
282impl<const NUM_LIMBS: usize> Shl<usize> for UnsignedInteger<NUM_LIMBS> {
283    type Output = UnsignedInteger<NUM_LIMBS>;
284    #[inline(always)]
285    fn shl(self, times: usize) -> UnsignedInteger<NUM_LIMBS> {
286        &self << times
287    }
288}
289
290// impl Shr
291
292impl<const NUM_LIMBS: usize> Shr<usize> for &UnsignedInteger<NUM_LIMBS> {
293    type Output = UnsignedInteger<NUM_LIMBS>;
294    #[inline(always)]
295    fn shr(self, times: usize) -> UnsignedInteger<NUM_LIMBS> {
296        self.const_shr(times)
297    }
298}
299
300impl<const NUM_LIMBS: usize> Shr<usize> for UnsignedInteger<NUM_LIMBS> {
301    type Output = UnsignedInteger<NUM_LIMBS>;
302    #[inline(always)]
303    fn shr(self, times: usize) -> UnsignedInteger<NUM_LIMBS> {
304        &self >> times
305    }
306}
307
308impl<const NUM_LIMBS: usize> ShrAssign<usize> for UnsignedInteger<NUM_LIMBS> {
309    fn shr_assign(&mut self, times: usize) {
310        debug_assert!(
311            times < 64 * NUM_LIMBS,
312            "UnsignedInteger shift left overflows."
313        );
314
315        let (a, b) = (times / 64, times % 64);
316
317        if b == 0 {
318            self.limbs.copy_within(..NUM_LIMBS - a, a);
319        } else {
320            for i in (a + 1..NUM_LIMBS).rev() {
321                self.limbs[i] = (self.limbs[i - a] >> b) | (self.limbs[i - a - 1] << (64 - b));
322            }
323            self.limbs[a] = self.limbs[0] >> b;
324        }
325
326        for limb in self.limbs.iter_mut().take(a) {
327            *limb = 0;
328        }
329    }
330}
331
332/// Impl BitAnd
333impl<const NUM_LIMBS: usize> BitAnd for UnsignedInteger<NUM_LIMBS> {
334    type Output = Self;
335
336    #[inline(always)]
337    fn bitand(self, rhs: Self) -> Self::Output {
338        let mut result = self;
339        result &= rhs;
340        result
341    }
342}
343
344impl<const NUM_LIMBS: usize> BitAndAssign for UnsignedInteger<NUM_LIMBS> {
345    fn bitand_assign(&mut self, rhs: Self) {
346        for (a_i, b_i) in self.limbs.iter_mut().zip(rhs.limbs.iter()) {
347            *a_i &= b_i;
348        }
349    }
350}
351
352/// Impl BitOr
353impl<const NUM_LIMBS: usize> BitOr for UnsignedInteger<NUM_LIMBS> {
354    type Output = Self;
355
356    #[inline(always)]
357    fn bitor(self, rhs: Self) -> Self::Output {
358        let mut result = self;
359        result |= rhs;
360        result
361    }
362}
363
364impl<const NUM_LIMBS: usize> BitOrAssign for UnsignedInteger<NUM_LIMBS> {
365    #[inline(always)]
366    fn bitor_assign(&mut self, rhs: Self) {
367        for (a_i, b_i) in self.limbs.iter_mut().zip(rhs.limbs.iter()) {
368            *a_i |= b_i;
369        }
370    }
371}
372
373/// Impl BitXor
374impl<const NUM_LIMBS: usize> BitXor for UnsignedInteger<NUM_LIMBS> {
375    type Output = Self;
376
377    #[inline(always)]
378    fn bitxor(self, rhs: Self) -> Self::Output {
379        let mut result = self;
380        result ^= rhs;
381        result
382    }
383}
384
385impl<const NUM_LIMBS: usize> BitXorAssign for UnsignedInteger<NUM_LIMBS> {
386    #[inline(always)]
387    fn bitxor_assign(&mut self, rhs: Self) {
388        for (a_i, b_i) in self.limbs.iter_mut().zip(rhs.limbs.iter()) {
389            *a_i ^= b_i;
390        }
391    }
392}
393
394impl<const NUM_LIMBS: usize> UnsignedInteger<NUM_LIMBS> {
395    pub const fn from_limbs(limbs: [u64; NUM_LIMBS]) -> Self {
396        Self { limbs }
397    }
398
399    #[inline(always)]
400    pub const fn from_u64(value: u64) -> Self {
401        let mut limbs = [0u64; NUM_LIMBS];
402        limbs[NUM_LIMBS - 1] = value;
403        UnsignedInteger { limbs }
404    }
405
406    #[inline(always)]
407    pub const fn from_u128(value: u128) -> Self {
408        let mut limbs = [0u64; NUM_LIMBS];
409        limbs[NUM_LIMBS - 1] = value as u64;
410        limbs[NUM_LIMBS - 2] = (value >> 64) as u64;
411        UnsignedInteger { limbs }
412    }
413
414    #[inline(always)]
415    const fn is_hex_string(string: &str) -> bool {
416        let len: usize = string.len();
417        let bytes = string.as_bytes();
418        let mut i = 0;
419
420        while i < len {
421            match bytes[i] {
422                b'0'..=b'9' => (),
423                b'a'..=b'f' => (),
424                b'A'..=b'F' => (),
425                _ => return false,
426            }
427            i += 1;
428        }
429
430        true
431    }
432
433    /// Creates an `UnsignedInteger` from a hexstring. It can contain `0x` or not.
434    /// Returns an `CreationError::InvalidHexString`if the value is not a hexstring.
435    /// Returns a `CreationError::EmptyString` if the input string is empty.
436    /// Returns a `CreationError::HexStringIsTooBig` if the the input hex string is bigger
437    /// than the maximum amount of characters for this element.
438    pub fn from_hex(value: &str) -> Result<Self, CreationError> {
439        let mut string = value;
440        let mut char_iterator = value.chars();
441        if string.len() > 2
442            && char_iterator.next().unwrap() == '0'
443            && char_iterator.next().unwrap() == 'x'
444        {
445            string = &string[2..];
446        }
447        if string.is_empty() {
448            return Err(CreationError::EmptyString);
449        }
450        if !Self::is_hex_string(string) {
451            return Err(CreationError::InvalidHexString);
452        }
453
454        // Limbs are of 64 bits - 8 bytes
455        // We have 16 nibbles per bytes
456        let max_amount_of_hex_chars = NUM_LIMBS * 16;
457        if string.len() > max_amount_of_hex_chars {
458            return Err(CreationError::HexStringIsTooBig);
459        }
460
461        Ok(Self::from_hex_unchecked(string))
462    }
463
464    /// Creates an `UnsignedInteger` from a hexstring
465    /// # Panics
466    /// Panics if value is not a hexstring. It can contain `0x` or not.
467    pub const fn from_hex_unchecked(value: &str) -> Self {
468        let mut result = [0u64; NUM_LIMBS];
469        let mut limb = 0;
470        let mut limb_index = NUM_LIMBS - 1;
471        let mut shift = 0;
472
473        let value_bytes = value.as_bytes();
474
475        // Remove "0x" if it's at the beginning of the string
476        let mut i = 0;
477        if value_bytes.len() > 2 && value_bytes[0] == b'0' && value_bytes[1] == b'x' {
478            i = 2;
479        }
480
481        let mut j = value_bytes.len();
482        while j > i {
483            j -= 1;
484            limb |= match value_bytes[j] {
485                c @ b'0'..=b'9' => (c as u64 - b'0' as u64) << shift,
486                c @ b'a'..=b'f' => (c as u64 - b'a' as u64 + 10) << shift,
487                c @ b'A'..=b'F' => (c as u64 - b'A' as u64 + 10) << shift,
488                _ => panic!("Malformed hex expression."),
489            };
490            shift += 4;
491            if shift == 64 && limb_index > 0 {
492                result[limb_index] = limb;
493                limb = 0;
494                limb_index -= 1;
495                shift = 0;
496            }
497        }
498
499        result[limb_index] = limb;
500        UnsignedInteger { limbs: result }
501    }
502
503    /// Creates a hexstring from a `FieldElement` without `0x`.
504    #[cfg(feature = "std")]
505    pub fn to_hex(&self) -> String {
506        let mut hex_string = String::new();
507        for &limb in self.limbs.iter() {
508            hex_string.push_str(&format!("{limb:016X}"));
509        }
510        hex_string.trim_start_matches('0').to_string()
511    }
512
513    pub const fn const_ne(a: &UnsignedInteger<NUM_LIMBS>, b: &UnsignedInteger<NUM_LIMBS>) -> bool {
514        let mut i = 0;
515        while i < NUM_LIMBS {
516            if a.limbs[i] != b.limbs[i] {
517                return true;
518            }
519            i += 1;
520        }
521        false
522    }
523
524    pub const fn const_le(a: &UnsignedInteger<NUM_LIMBS>, b: &UnsignedInteger<NUM_LIMBS>) -> bool {
525        let mut i = 0;
526        while i < NUM_LIMBS {
527            if a.limbs[i] < b.limbs[i] {
528                return true;
529            } else if a.limbs[i] > b.limbs[i] {
530                return false;
531            }
532            i += 1;
533        }
534        true
535    }
536
537    pub const fn const_shl(self, times: usize) -> Self {
538        debug_assert!(
539            times < 64 * NUM_LIMBS,
540            "UnsignedInteger shift left overflows."
541        );
542        let mut limbs = [0u64; NUM_LIMBS];
543        let (a, b) = (times / 64, times % 64);
544
545        if b == 0 {
546            let mut i = 0;
547            while i < NUM_LIMBS - a {
548                limbs[i] = self.limbs[a + i];
549                i += 1;
550            }
551            Self { limbs }
552        } else {
553            limbs[NUM_LIMBS - 1 - a] = self.limbs[NUM_LIMBS - 1] << b;
554            let mut i = a + 1;
555            while i < NUM_LIMBS {
556                limbs[NUM_LIMBS - 1 - i] = (self.limbs[NUM_LIMBS - 1 - i + a] << b)
557                    | (self.limbs[NUM_LIMBS - i + a] >> (64 - b));
558                i += 1;
559            }
560            Self { limbs }
561        }
562    }
563
564    pub const fn const_shr(self, times: usize) -> UnsignedInteger<NUM_LIMBS> {
565        debug_assert!(
566            times < 64 * NUM_LIMBS,
567            "UnsignedInteger shift right overflows."
568        );
569
570        let mut limbs = [0u64; NUM_LIMBS];
571        let (a, b) = (times / 64, times % 64);
572
573        if b == 0 {
574            let mut i = 0;
575            while i < NUM_LIMBS - a {
576                limbs[a + i] = self.limbs[i];
577                i += 1;
578            }
579            Self { limbs }
580        } else {
581            limbs[a] = self.limbs[0] >> b;
582            let mut i = a + 1;
583            while i < NUM_LIMBS {
584                limbs[i] = (self.limbs[i - a - 1] << (64 - b)) | (self.limbs[i - a] >> b);
585                i += 1;
586            }
587            Self { limbs }
588        }
589    }
590
591    pub const fn add(
592        a: &UnsignedInteger<NUM_LIMBS>,
593        b: &UnsignedInteger<NUM_LIMBS>,
594    ) -> (UnsignedInteger<NUM_LIMBS>, bool) {
595        let mut limbs = [0u64; NUM_LIMBS];
596        let mut carry = 0u64;
597        let mut i = NUM_LIMBS;
598        while i > 0 {
599            let (x, cb) = a.limbs[i - 1].overflowing_add(b.limbs[i - 1]);
600            let (x, cc) = x.overflowing_add(carry);
601            limbs[i - 1] = x;
602            carry = (cb | cc) as u64;
603            i -= 1;
604        }
605        (UnsignedInteger { limbs }, carry > 0)
606    }
607
608    pub fn double(a: &UnsignedInteger<NUM_LIMBS>) -> (UnsignedInteger<NUM_LIMBS>, bool) {
609        let mut cloned = *a;
610        let overflow = cloned.double_in_place();
611        (cloned, overflow)
612    }
613
614    pub fn double_in_place(&mut self) -> bool {
615        let mut msb_of_previous_limb = 0;
616        for i in (0..NUM_LIMBS).rev() {
617            let limb_ref = &mut self.limbs[i];
618            let msb_of_current_limb = *limb_ref >> 63;
619            *limb_ref <<= 1;
620            *limb_ref |= msb_of_previous_limb;
621            msb_of_previous_limb = msb_of_current_limb;
622        }
623        msb_of_previous_limb != 0
624    }
625
626    /// Multi-precision subtraction.
627    /// Adapted from Algorithm 14.9 of "Handbook of Applied Cryptography" (https://cacr.uwaterloo.ca/hac/)
628    /// Returns the results and a flag that is set if the substraction underflowed
629    #[inline(always)]
630    pub const fn sub(
631        a: &UnsignedInteger<NUM_LIMBS>,
632        b: &UnsignedInteger<NUM_LIMBS>,
633    ) -> (UnsignedInteger<NUM_LIMBS>, bool) {
634        let mut limbs = [0u64; NUM_LIMBS];
635        // 1.
636        let mut carry = false;
637        // 2.
638        let mut i: usize = NUM_LIMBS;
639        while i > 0 {
640            i -= 1;
641            let (x, cb) = a.limbs[i].overflowing_sub(b.limbs[i]);
642            let (x, cc) = x.overflowing_sub(carry as u64);
643            // Casting i128 to u64 drops the most significant bits of i128,
644            // which effectively computes residue modulo 2^{64}
645            // 2.1
646            limbs[i] = x;
647            // 2.2
648            carry = cb | cc;
649        }
650        // 3.
651        (Self { limbs }, carry)
652    }
653
654    /// Multi-precision multiplication.
655    /// Adapted from Algorithm 14.12 of "Handbook of Applied Cryptography" (https://cacr.uwaterloo.ca/hac/)
656    pub const fn mul(
657        a: &UnsignedInteger<NUM_LIMBS>,
658        b: &UnsignedInteger<NUM_LIMBS>,
659    ) -> (UnsignedInteger<NUM_LIMBS>, UnsignedInteger<NUM_LIMBS>) {
660        // 1.
661        let mut hi = [0u64; NUM_LIMBS];
662        let mut lo = [0u64; NUM_LIMBS];
663        // Const functions don't support for loops so we use whiles
664        // this is equivalent to:
665        // for i in (0..NUM_LIMBS).rev()
666        // 2.
667        let mut i = NUM_LIMBS;
668        while i > 0 {
669            i -= 1;
670            // 2.1
671            let mut carry = 0u128;
672            let mut j = NUM_LIMBS;
673            // 2.2
674            while j > 0 {
675                j -= 1;
676                let mut k = i + j;
677                if k >= NUM_LIMBS - 1 {
678                    k -= NUM_LIMBS - 1;
679                    let uv = (lo[k] as u128) + (a.limbs[j] as u128) * (b.limbs[i] as u128) + carry;
680                    carry = uv >> 64;
681                    // Casting u128 to u64 takes modulo 2^{64}
682                    lo[k] = uv as u64;
683                } else {
684                    let uv =
685                        (hi[k + 1] as u128) + (a.limbs[j] as u128) * (b.limbs[i] as u128) + carry;
686                    carry = uv >> 64;
687                    // Casting u128 to u64 takes modulo 2^{64}
688                    hi[k + 1] = uv as u64;
689                }
690            }
691            // 2.3
692            hi[i] = carry as u64;
693        }
694        // 3.
695        (Self { limbs: hi }, Self { limbs: lo })
696    }
697
698    #[inline(always)]
699    pub fn square(
700        a: &UnsignedInteger<NUM_LIMBS>,
701    ) -> (UnsignedInteger<NUM_LIMBS>, UnsignedInteger<NUM_LIMBS>) {
702        // NOTE: we use explicit `while` loops in this function because profiling pointed
703        // at iterators of the form `(<x>..<y>).rev()` as the main performance bottleneck.
704
705        let mut hi = Self {
706            limbs: [0u64; NUM_LIMBS],
707        };
708        let mut lo = Self {
709            limbs: [0u64; NUM_LIMBS],
710        };
711
712        // Compute products between a[i] and a[j] when i != j.
713        // The variable `index` below is the index of `lo` or
714        // `hi` to update
715        let mut i = NUM_LIMBS;
716        while i > 1 {
717            i -= 1;
718            let mut c: u128 = 0;
719            let mut j = i;
720            while j > 0 {
721                j -= 1;
722                let k = i + j;
723                if k >= NUM_LIMBS - 1 {
724                    let index = k + 1 - NUM_LIMBS;
725                    let cs = lo.limbs[index] as u128 + a.limbs[i] as u128 * a.limbs[j] as u128 + c;
726                    c = cs >> 64;
727                    lo.limbs[index] = cs as u64;
728                } else {
729                    let index = k + 1;
730                    let cs = hi.limbs[index] as u128 + a.limbs[i] as u128 * a.limbs[j] as u128 + c;
731                    c = cs >> 64;
732                    hi.limbs[index] = cs as u64;
733                }
734            }
735            hi.limbs[i] = c as u64;
736        }
737
738        // All these terms should appear twice each,
739        // so we have to multiply what we got so far by two.
740        let carry = lo.limbs[0] >> 63;
741        lo = lo << 1;
742        hi = hi << 1;
743        hi.limbs[NUM_LIMBS - 1] |= carry;
744
745        // Add the only remaning terms, which are the squares a[i] * a[i].
746        // The variable `index` below is the index of `lo` or
747        // `hi` to update
748        let mut c = 0;
749        let mut i = NUM_LIMBS;
750        while i > 0 {
751            i -= 1;
752            if NUM_LIMBS - 1 <= i * 2 {
753                let index = 2 * i + 1 - NUM_LIMBS;
754                let cs = lo.limbs[index] as u128 + a.limbs[i] as u128 * a.limbs[i] as u128 + c;
755                c = cs >> 64;
756                lo.limbs[index] = cs as u64;
757            } else {
758                let index = 2 * i + 1;
759                let cs = hi.limbs[index] as u128 + a.limbs[i] as u128 * a.limbs[i] as u128 + c;
760                c = cs >> 64;
761                hi.limbs[index] = cs as u64;
762            }
763            if NUM_LIMBS - 1 < i * 2 {
764                let index = 2 * i - NUM_LIMBS;
765                let cs = lo.limbs[index] as u128 + c;
766                c = cs >> 64;
767                lo.limbs[index] = cs as u64;
768            } else {
769                let index = 2 * i;
770                let cs = hi.limbs[index] as u128 + c;
771                c = cs >> 64;
772                hi.limbs[index] = cs as u64;
773            }
774        }
775        debug_assert_eq!(c, 0);
776        (hi, lo)
777    }
778
779    #[inline(always)]
780    /// Returns the number of bits needed to represent the number (0 for zero).
781    /// If nonzero, this is equivalent to one plus the floored log2 of the number.
782    pub const fn bits(&self) -> u32 {
783        let mut i = NUM_LIMBS;
784        while i > 0 {
785            if self.limbs[i - 1] != 0 {
786                return i as u32 * u64::BITS - self.limbs[i - 1].leading_zeros();
787            }
788            i -= 1;
789        }
790        0
791    }
792
793    /// Returns the truthy value if `self != 0` and the falsy value otherwise.
794    #[inline]
795    const fn ct_is_nonzero(ct: u64) -> u64 {
796        Self::ct_from_lsb((ct | ct.wrapping_neg()) >> (u64::BITS - 1))
797    }
798
799    /// Returns the truthy value if `value == 1`, and the falsy value if `value == 0`.
800    /// Panics for other values.
801    const fn ct_from_lsb(value: u64) -> u64 {
802        debug_assert!(value == 0 || value == 1);
803        value.wrapping_neg()
804    }
805
806    /// Return `b` if `c` is truthy, otherwise return `a`.
807    #[inline]
808    const fn ct_select_limb(a: u64, b: u64, ct: u64) -> u64 {
809        a ^ (ct & (a ^ b))
810    }
811
812    /// Return `b` if `c` is truthy, otherwise return `a`.
813    #[inline]
814    const fn ct_select(a: &Self, b: &Self, c: u64) -> Self {
815        let mut limbs = [0_u64; NUM_LIMBS];
816
817        let mut i = 0;
818        while i < NUM_LIMBS {
819            limbs[i] = Self::ct_select_limb(a.limbs[i], b.limbs[i], c);
820            i += 1;
821        }
822
823        Self { limbs }
824    }
825
826    /// Computes `self - (rhs + borrow)`, returning the result along with the new borrow.
827    #[inline(always)]
828    const fn sbb_limbs(lhs: u64, rhs: u64, borrow: u64) -> (u64, u64) {
829        let a = lhs as u128;
830        let b = rhs as u128;
831        let borrow = (borrow >> (u64::BITS - 1)) as u128;
832        let ret = a.wrapping_sub(b + borrow);
833        (ret as u64, (ret >> u64::BITS) as u64)
834    }
835
836    #[inline(always)]
837    /// Computes `a - (b + borrow)`, returning the result along with the new borrow.
838    pub fn sbb(&self, rhs: &Self, mut borrow: u64) -> (Self, u64) {
839        let mut limbs = [0; NUM_LIMBS];
840
841        for i in (0..NUM_LIMBS).rev() {
842            let (w, b) = Self::sbb_limbs(self.limbs[i], rhs.limbs[i], borrow);
843            limbs[i] = w;
844            borrow = b;
845        }
846
847        (Self { limbs }, borrow)
848    }
849
850    #[inline(always)]
851    /// Returns the number of bits needed to represent the number as little endian
852    pub const fn bits_le(&self) -> usize {
853        let mut i = 0;
854        while i < NUM_LIMBS {
855            if self.limbs[i] != 0 {
856                return u64::BITS as usize * (NUM_LIMBS - i)
857                    - self.limbs[i].leading_zeros() as usize;
858            }
859            i += 1;
860        }
861        0
862    }
863
864    /// Computes self / rhs, returns the quotient, remainder.
865    pub fn div_rem(&self, rhs: &Self) -> (Self, Self) {
866        debug_assert!(
867            *rhs != UnsignedInteger::from_u64(0),
868            "Attempted to divide by zero"
869        );
870        let mb = rhs.bits_le();
871        let mut bd = (NUM_LIMBS * u64::BITS as usize) - mb;
872        let mut rem = *self;
873        let mut quo = Self::from_u64(0);
874        let mut c = rhs.shl(bd);
875
876        loop {
877            let (mut r, borrow) = rem.sbb(&c, 0);
878            debug_assert!(borrow == 0 || borrow == u64::MAX);
879            rem = Self::ct_select(&r, &rem, borrow);
880            r = quo.bitor(Self::from_u64(1));
881            quo = Self::ct_select(&r, &quo, borrow);
882            if bd == 0 {
883                break;
884            }
885            bd -= 1;
886            c = c.shr(1);
887            quo = quo.shl(1);
888        }
889
890        let is_some = Self::ct_is_nonzero(mb as u64);
891        quo = Self::ct_select(&Self::from_u64(0), &quo, is_some);
892        (quo, rem)
893    }
894
895    /// Convert from a decimal string.
896    pub fn from_dec_str(value: &str) -> Result<Self, CreationError> {
897        if value.is_empty() {
898            return Err(CreationError::InvalidDecString);
899        }
900        let mut res = Self::from_u64(0);
901        for b in value.bytes().map(|b| b.wrapping_sub(b'0')) {
902            if b > 9 {
903                return Err(CreationError::InvalidDecString);
904            }
905            let (high, low) = Self::mul(&res, &Self::from(10_u64));
906            if high > Self::from_u64(0) {
907                return Err(CreationError::InvalidDecString);
908            }
909            res = low + Self::from(b as u64);
910        }
911        Ok(res)
912    }
913
914    #[cfg(feature = "proptest")]
915    pub fn nonzero_uint() -> impl Strategy<Value = UnsignedInteger<NUM_LIMBS>> {
916        any_uint::<NUM_LIMBS>().prop_filter("is_zero", |&x| x != UnsignedInteger::from_u64(0))
917    }
918}
919
920impl<const NUM_LIMBS: usize> IsUnsignedInteger for UnsignedInteger<NUM_LIMBS> {}
921
922impl<const NUM_LIMBS: usize> ByteConversion for UnsignedInteger<NUM_LIMBS> {
923    #[cfg(feature = "alloc")]
924    fn to_bytes_be(&self) -> alloc::vec::Vec<u8> {
925        self.limbs
926            .iter()
927            .flat_map(|limb| limb.to_be_bytes())
928            .collect()
929    }
930
931    #[cfg(feature = "alloc")]
932    fn to_bytes_le(&self) -> alloc::vec::Vec<u8> {
933        self.limbs
934            .iter()
935            .rev()
936            .flat_map(|limb| limb.to_le_bytes())
937            .collect()
938    }
939
940    fn from_bytes_be(bytes: &[u8]) -> Result<Self, ByteConversionError> {
941        // We cut off extra bytes, this is useful when you use this function to generate the element from randomness
942        // In the future with the right algorithm this shouldn't be needed
943
944        let needed_bytes = bytes
945            .get(0..NUM_LIMBS * 8)
946            .ok_or(ByteConversionError::FromBEBytesError)?;
947
948        let mut limbs: [u64; NUM_LIMBS] = [0; NUM_LIMBS];
949
950        needed_bytes
951            .chunks_exact(8)
952            .enumerate()
953            .try_for_each(|(i, chunk)| {
954                let limb = u64::from_be_bytes(
955                    chunk
956                        .try_into()
957                        .map_err(|_| ByteConversionError::FromBEBytesError)?,
958                );
959                limbs[i] = limb;
960                Ok::<_, ByteConversionError>(())
961            })?;
962
963        Ok(Self { limbs })
964    }
965
966    fn from_bytes_le(bytes: &[u8]) -> Result<Self, ByteConversionError> {
967        let needed_bytes = bytes
968            .get(0..NUM_LIMBS * 8)
969            .ok_or(ByteConversionError::FromBEBytesError)?;
970
971        let mut limbs: [u64; NUM_LIMBS] = [0; NUM_LIMBS];
972
973        needed_bytes
974            .chunks_exact(8)
975            .rev()
976            .enumerate()
977            .try_for_each(|(i, chunk)| {
978                let limb = u64::from_le_bytes(
979                    chunk
980                        .try_into()
981                        .map_err(|_| ByteConversionError::FromLEBytesError)?,
982                );
983                limbs[i] = limb;
984                Ok::<_, ByteConversionError>(())
985            })?;
986
987        Ok(Self { limbs })
988    }
989}
990
991impl<const NUM_LIMBS: usize> From<UnsignedInteger<NUM_LIMBS>> for u16 {
992    fn from(value: UnsignedInteger<NUM_LIMBS>) -> Self {
993        value.limbs[NUM_LIMBS - 1] as u16
994    }
995}
996
997#[cfg(feature = "alloc")]
998impl<const NUM_LIMBS: usize> AsBytes for UnsignedInteger<NUM_LIMBS> {
999    fn as_bytes(&self) -> alloc::vec::Vec<u8> {
1000        self.limbs
1001            .into_iter()
1002            .fold(alloc::vec::Vec::new(), |mut acc, limb| {
1003                acc.extend_from_slice(&limb.as_bytes());
1004                acc
1005            })
1006    }
1007}
1008
1009#[cfg(feature = "alloc")]
1010impl<const NUM_LIMBS: usize> From<UnsignedInteger<NUM_LIMBS>> for alloc::vec::Vec<u8> {
1011    fn from(val: UnsignedInteger<NUM_LIMBS>) -> Self {
1012        val.as_bytes()
1013    }
1014}
1015
1016#[cfg(feature = "proptest")]
1017fn any_uint<const NUM_LIMBS: usize>() -> impl Strategy<Value = UnsignedInteger<NUM_LIMBS>> {
1018    any::<[u64; NUM_LIMBS]>().prop_map(UnsignedInteger::from_limbs)
1019}
1020
1021#[cfg(feature = "proptest")]
1022impl<const NUM_LIMBS: usize> Arbitrary for UnsignedInteger<NUM_LIMBS> {
1023    type Parameters = ();
1024
1025    fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
1026        any_uint::<NUM_LIMBS>().sboxed()
1027    }
1028
1029    type Strategy = SBoxedStrategy<Self>;
1030}
1031
1032#[cfg(test)]
1033mod tests_u384 {
1034    use crate::traits::ByteConversion;
1035    use crate::unsigned_integer::element::{UnsignedInteger, U256, U384};
1036    #[cfg(feature = "proptest")]
1037    use proptest::prelude::*;
1038    #[cfg(feature = "proptest")]
1039    use std::ops::Shr;
1040
1041    #[cfg(feature = "proptest")]
1042    const N_LIMBS: usize = 8;
1043    #[cfg(feature = "proptest")]
1044    type Uint = UnsignedInteger<N_LIMBS>;
1045
1046    #[cfg(feature = "proptest")]
1047    proptest! {
1048        #[test]
1049        fn bitand(a in any::<Uint>(), b in any::<Uint>()) {
1050            let result = a & b;
1051
1052            for i in 0..N_LIMBS {
1053                assert_eq!(result.limbs[i], a.limbs[i] & b.limbs[i]);
1054            }
1055        }
1056
1057        #[test]
1058        fn bitand_assign(a in any::<Uint>(), b in any::<Uint>()) {
1059            let mut result = a;
1060            result &= b;
1061
1062            for i in 0..N_LIMBS {
1063                assert_eq!(result.limbs[i], a.limbs[i] & b.limbs[i]);
1064            }
1065        }
1066
1067        #[test]
1068        fn bitor(a in any::<Uint>(), b in any::<Uint>()) {
1069            let result = a | b;
1070
1071            for i in 0..N_LIMBS {
1072                assert_eq!(result.limbs[i], a.limbs[i] | b.limbs[i]);
1073            }
1074        }
1075
1076        #[test]
1077        fn bitor_assign(a in any::<Uint>(), b in any::<Uint>()) {
1078            let mut result = a;
1079            result |= b;
1080
1081            for i in 0..N_LIMBS {
1082                assert_eq!(result.limbs[i], a.limbs[i] | b.limbs[i]);
1083            }
1084        }
1085
1086        #[test]
1087        fn bitxor(a in any::<Uint>(), b in any::<Uint>()) {
1088            let result = a ^ b;
1089
1090            for i in 0..N_LIMBS {
1091                assert_eq!(result.limbs[i], a.limbs[i] ^ b.limbs[i]);
1092            }
1093        }
1094
1095        #[test]
1096        fn bitxor_assign(a in any::<Uint>(), b in any::<Uint>()) {
1097            let mut result = a;
1098            result ^= b;
1099
1100            for i in 0..N_LIMBS {
1101                assert_eq!(result.limbs[i], a.limbs[i] ^ b.limbs[i]);
1102            }
1103        }
1104
1105        #[test]
1106        fn div_rem(a in any::<Uint>(), b in any::<Uint>()) {
1107            let a = a.shr(256);
1108            let b = b.shr(256);
1109            assert_eq!((a * b).div_rem(&b), (a, Uint::from_u64(0)));
1110        }
1111    }
1112
1113    #[test]
1114    fn construct_new_integer_from_limbs() {
1115        let a: U384 = UnsignedInteger {
1116            limbs: [0, 1, 2, 3, 4, 5],
1117        };
1118        assert_eq!(U384::from_limbs([0, 1, 2, 3, 4, 5]), a);
1119    }
1120
1121    #[test]
1122    fn construct_new_integer_from_u64_1() {
1123        let a = U384::from_u64(1_u64);
1124        assert_eq!(a.limbs, [0, 0, 0, 0, 0, 1]);
1125    }
1126
1127    #[test]
1128    fn construct_new_integer_from_u54_2() {
1129        let a = U384::from_u64(u64::MAX);
1130        assert_eq!(a.limbs, [0, 0, 0, 0, 0, u64::MAX]);
1131    }
1132
1133    #[test]
1134    fn construct_new_integer_from_u128_1() {
1135        let a = U384::from_u128(u128::MAX);
1136        assert_eq!(a.limbs, [0, 0, 0, 0, u64::MAX, u64::MAX]);
1137    }
1138
1139    #[test]
1140    fn construct_new_integer_from_u128_2() {
1141        let a = U384::from_u128(276371540478856090688472252609570374439);
1142        assert_eq!(
1143            a.limbs,
1144            [0, 0, 0, 0, 14982131230017065096, 14596400355126379303]
1145        );
1146    }
1147
1148    #[test]
1149    fn construct_new_integer_from_hex_1() {
1150        let a = U384::from_hex_unchecked("1");
1151        assert_eq!(a.limbs, [0, 0, 0, 0, 0, 1]);
1152    }
1153
1154    #[test]
1155    fn construct_new_integer_from_zero_x_1() {
1156        let a = U384::from_hex_unchecked("0x1");
1157        assert_eq!(a.limbs, [0, 0, 0, 0, 0, 1]);
1158    }
1159
1160    #[test]
1161    fn construct_new_integer_from_hex_2() {
1162        let a = U384::from_hex_unchecked("f");
1163        assert_eq!(a.limbs, [0, 0, 0, 0, 0, 15]);
1164    }
1165
1166    #[test]
1167    fn construct_new_integer_from_hex_3() {
1168        let a = U384::from_hex_unchecked("10000000000000000");
1169        assert_eq!(a.limbs, [0, 0, 0, 0, 1, 0]);
1170    }
1171
1172    #[test]
1173    fn construct_new_integer_from_hex_4() {
1174        let a = U384::from_hex_unchecked("a0000000000000000");
1175        assert_eq!(a.limbs, [0, 0, 0, 0, 10, 0]);
1176    }
1177
1178    #[test]
1179    fn construct_new_integer_from_hex_5() {
1180        let a = U384::from_hex_unchecked("ffffffffffffffffff");
1181        assert_eq!(a.limbs, [0, 0, 0, 0, 255, u64::MAX]);
1182    }
1183
1184    #[test]
1185    fn construct_new_integer_from_hex_6() {
1186        let a = U384::from_hex_unchecked("eb235f6144d9e91f4b14");
1187        assert_eq!(a.limbs, [0, 0, 0, 0, 60195, 6872850209053821716]);
1188    }
1189
1190    #[test]
1191    fn construct_new_integer_from_hex_7() {
1192        let a = U384::from_hex_unchecked("2b20aaa5cf482b239e2897a787faf4660cc95597854beb2");
1193        assert_eq!(
1194            a.limbs,
1195            [
1196                0,
1197                0,
1198                0,
1199                194229460750598834,
1200                4171047363999149894,
1201                6975114134393503410
1202            ]
1203        );
1204    }
1205
1206    #[test]
1207    fn construct_new_integer_from_hex_checked_7() {
1208        let a = U384::from_hex("2b20aaa5cf482b239e2897a787faf4660cc95597854beb2").unwrap();
1209        assert_eq!(
1210            a.limbs,
1211            [
1212                0,
1213                0,
1214                0,
1215                194229460750598834,
1216                4171047363999149894,
1217                6975114134393503410
1218            ]
1219        );
1220    }
1221
1222    #[test]
1223    fn construct_new_integer_from_hex_checked_7_with_zero_x() {
1224        let a = U384::from_hex("0x2b20aaa5cf482b239e2897a787faf4660cc95597854beb2").unwrap();
1225        assert_eq!(
1226            a.limbs,
1227            [
1228                0,
1229                0,
1230                0,
1231                194229460750598834,
1232                4171047363999149894,
1233                6975114134393503410
1234            ]
1235        );
1236    }
1237
1238    #[test]
1239    fn construct_new_integer_from_non_hex_errs() {
1240        assert!(U384::from_hex("0xTEST").is_err());
1241    }
1242
1243    #[test]
1244    fn construct_new_integer_from_empty_string_errs() {
1245        assert!(U384::from_hex("").is_err());
1246    }
1247
1248    #[test]
1249    fn construct_new_integer_from_hex_checked_8() {
1250        let a = U384::from_hex("140f5177b90b4f96b61bb8ccb4f298ad2b20aaa5cf482b239e2897a787faf4660cc95597854beb235f6144d9e91f4b14").unwrap();
1251        assert_eq!(
1252            a.limbs,
1253            [
1254                1445463580056702870,
1255                13122285128622708909,
1256                3107671372009581347,
1257                11396525602857743462,
1258                921361708038744867,
1259                6872850209053821716
1260            ]
1261        );
1262    }
1263
1264    #[test]
1265    fn construct_new_integer_from_hex_8() {
1266        let a = U384::from_hex_unchecked("140f5177b90b4f96b61bb8ccb4f298ad2b20aaa5cf482b239e2897a787faf4660cc95597854beb235f6144d9e91f4b14");
1267        assert_eq!(
1268            a.limbs,
1269            [
1270                1445463580056702870,
1271                13122285128622708909,
1272                3107671372009581347,
1273                11396525602857743462,
1274                921361708038744867,
1275                6872850209053821716
1276            ]
1277        );
1278    }
1279
1280    #[test]
1281    fn from_hex_with_overflowing_hexstring_should_error() {
1282        let u256_from_big_string = U256::from_hex(&"f".repeat(65));
1283        assert!(u256_from_big_string.is_err());
1284        assert!(
1285            u256_from_big_string
1286                == Err(crate::unsigned_integer::element::CreationError::HexStringIsTooBig)
1287        );
1288    }
1289
1290    #[test]
1291    fn from_hex_with_non_overflowing_hexstring_should_work() {
1292        assert_eq!(U256::from_hex(&"0".repeat(64)).unwrap().limbs, [0, 0, 0, 0])
1293    }
1294
1295    #[test]
1296    fn construct_new_integer_from_dec_1() {
1297        let a = U384::from_dec_str("1").unwrap();
1298        assert_eq!(a.limbs, [0, 0, 0, 0, 0, 1]);
1299    }
1300
1301    #[test]
1302    fn construct_new_integer_from_dec_2() {
1303        let a = U384::from_dec_str("15").unwrap();
1304        assert_eq!(a.limbs, [0, 0, 0, 0, 0, 15]);
1305    }
1306
1307    #[test]
1308    fn construct_new_integer_from_dec_3() {
1309        let a = U384::from_dec_str("18446744073709551616").unwrap();
1310        assert_eq!(a.limbs, [0, 0, 0, 0, 1, 0]);
1311    }
1312
1313    #[test]
1314    fn construct_new_integer_from_dec_4() {
1315        let a = U384::from_dec_str("184467440737095516160").unwrap();
1316        assert_eq!(a.limbs, [0, 0, 0, 0, 10, 0]);
1317    }
1318
1319    #[test]
1320    fn construct_new_integer_from_dec_5() {
1321        let a = U384::from_dec_str("4722366482869645213695").unwrap();
1322        assert_eq!(a.limbs, [0, 0, 0, 0, 255, u64::MAX]);
1323    }
1324
1325    #[test]
1326    fn construct_new_integer_from_dec_6() {
1327        let a = U384::from_dec_str("1110408632367155513346836").unwrap();
1328        assert_eq!(a.limbs, [0, 0, 0, 0, 60195, 6872850209053821716]);
1329    }
1330
1331    #[test]
1332    fn construct_new_integer_from_dec_7() {
1333        let a =
1334            U384::from_dec_str("66092860629991288370279803883558073888453977263446474418").unwrap();
1335        assert_eq!(
1336            a.limbs,
1337            [
1338                0,
1339                0,
1340                0,
1341                194229460750598834,
1342                4171047363999149894,
1343                6975114134393503410
1344            ]
1345        );
1346    }
1347
1348    #[test]
1349    fn construct_new_integer_from_dec_8() {
1350        let a = U384::from_dec_str("3087491467896943881295768554872271030441880044814691421073017731442549147034464936390742057449079000462340371991316").unwrap();
1351        assert_eq!(
1352            a.limbs,
1353            [
1354                1445463580056702870,
1355                13122285128622708909,
1356                3107671372009581347,
1357                11396525602857743462,
1358                921361708038744867,
1359                6872850209053821716
1360            ]
1361        );
1362    }
1363
1364    #[test]
1365    fn construct_new_integer_from_dec_empty() {
1366        assert!(U384::from_dec_str("").is_err());
1367    }
1368
1369    #[test]
1370    fn construct_new_integer_from_dec_invalid() {
1371        assert!(U384::from_dec_str("0xff").is_err());
1372    }
1373
1374    #[test]
1375    fn equality_works_1() {
1376        let a = U384::from_hex_unchecked("1");
1377        let b = U384 {
1378            limbs: [0, 0, 0, 0, 0, 1],
1379        };
1380        assert_eq!(a, b);
1381    }
1382    #[test]
1383    fn equality_works_2() {
1384        let a = U384::from_hex_unchecked("f");
1385        let b = U384 {
1386            limbs: [0, 0, 0, 0, 0, 15],
1387        };
1388        assert_eq!(a, b);
1389    }
1390
1391    #[test]
1392    fn equality_works_3() {
1393        let a = U384::from_hex_unchecked("10000000000000000");
1394        let b = U384 {
1395            limbs: [0, 0, 0, 0, 1, 0],
1396        };
1397        assert_eq!(a, b);
1398    }
1399
1400    #[test]
1401    fn equality_works_4() {
1402        let a = U384::from_hex_unchecked("a0000000000000000");
1403        let b = U384 {
1404            limbs: [0, 0, 0, 0, 10, 0],
1405        };
1406        assert_eq!(a, b);
1407    }
1408
1409    #[test]
1410    fn equality_works_5() {
1411        let a = U384::from_hex_unchecked("ffffffffffffffffff");
1412        let b = U384 {
1413            limbs: [0, 0, 0, 0, u8::MAX as u64, u64::MAX],
1414        };
1415        assert_eq!(a, b);
1416    }
1417
1418    #[test]
1419    fn equality_works_6() {
1420        let a = U384::from_hex_unchecked("eb235f6144d9e91f4b14");
1421        let b = U384 {
1422            limbs: [0, 0, 0, 0, 60195, 6872850209053821716],
1423        };
1424        assert_eq!(a, b);
1425    }
1426
1427    #[test]
1428    fn equality_works_7() {
1429        let a = U384::from_hex_unchecked("2b20aaa5cf482b239e2897a787faf4660cc95597854beb2");
1430        let b = U384 {
1431            limbs: [
1432                0,
1433                0,
1434                0,
1435                194229460750598834,
1436                4171047363999149894,
1437                6975114134393503410,
1438            ],
1439        };
1440        assert_eq!(a, b);
1441    }
1442
1443    #[test]
1444    fn equality_works_8() {
1445        let a = U384::from_hex_unchecked("140f5177b90b4f96b61bb8ccb4f298ad2b20aaa5cf482b239e2897a787faf4660cc95597854beb235f6144d9e91f4b14");
1446        let b = U384 {
1447            limbs: [
1448                1445463580056702870,
1449                13122285128622708909,
1450                3107671372009581347,
1451                11396525602857743462,
1452                921361708038744867,
1453                6872850209053821716,
1454            ],
1455        };
1456        assert_eq!(a, b);
1457    }
1458
1459    #[test]
1460    fn equality_works_9() {
1461        let a = U384::from_hex_unchecked("fffffff");
1462        let b = U384::from_hex_unchecked("fefffff");
1463        assert_ne!(a, b);
1464    }
1465
1466    #[test]
1467    fn equality_works_10() {
1468        let a = U384::from_hex_unchecked("ffff000000000000");
1469        let b = U384::from_hex_unchecked("ffff000000100000");
1470        assert_ne!(a, b);
1471    }
1472
1473    #[test]
1474    fn const_ne_works_1() {
1475        let a = U384::from_hex_unchecked("ffff000000000000");
1476        let b = U384::from_hex_unchecked("ffff000000100000");
1477        assert!(U384::const_ne(&a, &b));
1478    }
1479
1480    #[test]
1481    fn const_ne_works_2() {
1482        let a = U384::from_hex_unchecked("140f5177b90b4f96b61bb8ccb4f298ad2b20aaa5cf482b239e2897a787faf4660cc95597854beb235f6144d9e91f4b14");
1483        let b = U384 {
1484            limbs: [
1485                1445463580056702870,
1486                13122285128622708909,
1487                3107671372009581347,
1488                11396525602857743462,
1489                921361708038744867,
1490                6872850209053821716,
1491            ],
1492        };
1493        assert!(!U384::const_ne(&a, &b));
1494    }
1495
1496    #[test]
1497    fn double_two_384_bit_integers() {
1498        let a = U384::from_u64(2);
1499        let b = U384::from_u64(5);
1500        let c = U384::from_u64(7);
1501        assert_eq!(U384::double(&a).0, a + a);
1502        assert_eq!(U384::double(&b).0, b + b);
1503        assert_eq!(U384::double(&c).0, c + c);
1504    }
1505
1506    #[test]
1507    fn add_two_384_bit_integers_1() {
1508        let a = U384::from_u64(2);
1509        let b = U384::from_u64(5);
1510        let c = U384::from_u64(7);
1511        assert_eq!(a + b, c);
1512    }
1513
1514    #[test]
1515    fn add_two_384_bit_integers_2() {
1516        let a = U384::from_u64(334);
1517        let b = U384::from_u64(666);
1518        let c = U384::from_u64(1000);
1519        assert_eq!(a + b, c);
1520    }
1521
1522    #[test]
1523    fn add_two_384_bit_integers_3() {
1524        let a = U384::from_hex_unchecked("ffffffffffffffff");
1525        let b = U384::from_hex_unchecked("1");
1526        let c = U384::from_hex_unchecked("10000000000000000");
1527        assert_eq!(a + b, c);
1528    }
1529
1530    #[test]
1531    fn add_two_384_bit_integers_4() {
1532        let a = U384::from_hex_unchecked("b58e1e0b66");
1533        let b = U384::from_hex_unchecked("55469d9619");
1534        let c = U384::from_hex_unchecked("10ad4bba17f");
1535        assert_eq!(a + b, c);
1536    }
1537
1538    #[test]
1539    fn add_two_384_bit_integers_5() {
1540        let a = U384::from_hex_unchecked("e8dff25cb6160f7705221da6f");
1541        let b = U384::from_hex_unchecked("ab879169b5f80dc8a7969f0b0");
1542        let c = U384::from_hex_unchecked("1946783c66c0e1d3facb8bcb1f");
1543        assert_eq!(a + b, c);
1544    }
1545
1546    #[test]
1547    fn add_two_384_bit_integers_6() {
1548        let a = U384::from_hex_unchecked("9adf291af3a64d59e14e7b440c850508014c551ed5");
1549        let b = U384::from_hex_unchecked("e7948474bce907f0feaf7e5d741a8cd2f6d1fb9448");
1550        let c = U384::from_hex_unchecked("18273ad8fb08f554adffdf9a1809f91daf81e50b31d");
1551        assert_eq!(a + b, c);
1552    }
1553
1554    #[test]
1555    fn add_two_384_bit_integers_7() {
1556        let a = U384::from_hex_unchecked(
1557            "f866aef803c92bf02e85c7fad0eccb4881c59825e499fa22f98e1a8fefed4cd9a03647cd3cc84",
1558        );
1559        let b = U384::from_hex_unchecked(
1560            "9b4000dccf01a010e196154a1b998408f949d734389626ba97cb3331ee87e01dd5badc58f41b2",
1561        );
1562        let c = U384::from_hex_unchecked(
1563            "193a6afd4d2cacc01101bdd44ec864f517b0f6f5a1d3020dd91594dc1de752cf775f1242630e36",
1564        );
1565        assert_eq!(a + b, c);
1566    }
1567
1568    #[test]
1569    fn add_two_384_bit_integers_8() {
1570        let a = U384::from_hex_unchecked("07df9c74fa9d5aafa74a87dbbf93215659d8a3e1706d4b06de9512284802580eb36ae12ea59f90db5b1799d0970a42e");
1571        let b = U384::from_hex_unchecked("d515e54973f0643a6a9957579c1f84020a6a91d5d5f27b75401c7538d2c9ea9cafff44a2c606877d46c49a3433cc85e");
1572        let c = U384::from_hex_unchecked("dcf581be6e8dbeea11e3df335bb2a558644335b7465fc67c1eb187611acc42ab636a25d16ba61858a1dc3404cad6c8c");
1573        assert_eq!(a + b, c);
1574    }
1575
1576    #[test]
1577    fn add_two_384_bit_integers_9() {
1578        let a = U384::from_hex_unchecked("92977527a0f8ba00d18c1b2f1900d965d4a70e5f5f54468ffb2d4d41519385f24b078a0e7d0281d5ad0c36724dc4233");
1579        let b = U384::from_hex_unchecked("46facf9953a9494822bf18836ffd7e55c48b30aa81e17fa1ace0b473015307e4622b8bd6fa68ef654796a183abde842");
1580        let c = U384::from_hex_unchecked("d99244c0f4a20348f44b33b288fe57bb99323f09e135c631a80e01b452e68dd6ad3315e5776b713af4a2d7f5f9a2a75");
1581        assert_eq!(a + b, c);
1582    }
1583
1584    #[test]
1585    fn add_two_384_bit_integers_10() {
1586        let a = U384::from_hex_unchecked("07df9c74fa9d5aafa74a87dbbf93215659d8a3e1706d4b06de9512284802580eb36ae12ea59f90db5b1799d0970a42e");
1587        let b = U384::from_hex_unchecked("d515e54973f0643a6a9957579c1f84020a6a91d5d5f27b75401c7538d2c9ea9cafff44a2c606877d46c49a3433cc85e");
1588        let c_expected = U384::from_hex_unchecked("dcf581be6e8dbeea11e3df335bb2a558644335b7465fc67c1eb187611acc42ab636a25d16ba61858a1dc3404cad6c8c");
1589        let (c, overflow) = U384::add(&a, &b);
1590        assert_eq!(c, c_expected);
1591        assert!(!overflow);
1592    }
1593
1594    #[test]
1595    fn add_two_384_bit_integers_11() {
1596        let a = U384::from_hex_unchecked("92977527a0f8ba00d18c1b2f1900d965d4a70e5f5f54468ffb2d4d41519385f24b078a0e7d0281d5ad0c36724dc4233");
1597        let b = U384::from_hex_unchecked("46facf9953a9494822bf18836ffd7e55c48b30aa81e17fa1ace0b473015307e4622b8bd6fa68ef654796a183abde842");
1598        let c_expected = U384::from_hex_unchecked("d99244c0f4a20348f44b33b288fe57bb99323f09e135c631a80e01b452e68dd6ad3315e5776b713af4a2d7f5f9a2a75");
1599        let (c, overflow) = U384::add(&a, &b);
1600        assert_eq!(c, c_expected);
1601        assert!(!overflow);
1602    }
1603
1604    #[test]
1605    fn add_two_384_bit_integers_12_with_overflow() {
1606        let a = U384::from_hex_unchecked("b07bc844363dd56467d9ebdd5929e9bb34a8e2577db77df6cf8f2ac45bd3d0bc2fc3078d265fe761af51d6aec5b59428");
1607        let b = U384::from_hex_unchecked("cbbc474761bb7995ff54e25fa5d30295604fe3545d0cde405e72d8c0acebb119e9158131679b6c34483a3dafb49deeea");
1608        let c_expected = U384::from_hex_unchecked("7c380f8b97f94efa672ece3cfefcec5094f8c5abdac45c372e02038508bf81d618d888be8dfb5395f78c145e7a538312");
1609        let (c, overflow) = U384::add(&a, &b);
1610        assert_eq!(c, c_expected);
1611        assert!(overflow);
1612    }
1613
1614    #[test]
1615    fn double_384_bit_integer_12_with_overflow() {
1616        let a = U384::from_hex_unchecked("b07bc844363dd56467d9ebdd5929e9bb34a8e2577db77df6cf8f2ac45bd3d0bc2fc3078d265fe761af51d6aec5b59428");
1617        assert_eq!(U384::double(&a), U384::add(&a, &a));
1618    }
1619
1620    #[test]
1621    fn sub_two_384_bit_integers_1() {
1622        let a = U384::from_u64(2);
1623        let b = U384::from_u64(5);
1624        let c = U384::from_u64(7);
1625        assert_eq!(c - a, b);
1626    }
1627
1628    #[test]
1629    fn sub_two_384_bit_integers_2() {
1630        let a = U384::from_u64(334);
1631        let b = U384::from_u64(666);
1632        let c = U384::from_u64(1000);
1633        assert_eq!(c - a, b);
1634    }
1635
1636    #[test]
1637    fn sub_two_384_bit_integers_3() {
1638        let a = U384::from_hex_unchecked("ffffffffffffffff");
1639        let b = U384::from_hex_unchecked("1");
1640        let c = U384::from_hex_unchecked("10000000000000000");
1641        assert_eq!(c - a, b);
1642    }
1643
1644    #[test]
1645    fn sub_two_384_bit_integers_4() {
1646        let a = U384::from_hex_unchecked("b58e1e0b66");
1647        let b = U384::from_hex_unchecked("55469d9619");
1648        let c = U384::from_hex_unchecked("10ad4bba17f");
1649        assert_eq!(c - a, b);
1650    }
1651
1652    #[test]
1653    fn sub_two_384_bit_integers_5() {
1654        let a = U384::from_hex_unchecked("e8dff25cb6160f7705221da6f");
1655        let b = U384::from_hex_unchecked("ab879169b5f80dc8a7969f0b0");
1656        let c = U384::from_hex_unchecked("1946783c66c0e1d3facb8bcb1f");
1657        assert_eq!(c - a, b);
1658    }
1659
1660    #[test]
1661    fn sub_two_384_bit_integers_6() {
1662        let a = U384::from_hex_unchecked("9adf291af3a64d59e14e7b440c850508014c551ed5");
1663        let b = U384::from_hex_unchecked("e7948474bce907f0feaf7e5d741a8cd2f6d1fb9448");
1664        let c = U384::from_hex_unchecked("18273ad8fb08f554adffdf9a1809f91daf81e50b31d");
1665        assert_eq!(c - a, b);
1666    }
1667
1668    #[test]
1669    fn sub_two_384_bit_integers_7() {
1670        let a = U384::from_hex_unchecked(
1671            "f866aef803c92bf02e85c7fad0eccb4881c59825e499fa22f98e1a8fefed4cd9a03647cd3cc84",
1672        );
1673        let b = U384::from_hex_unchecked(
1674            "9b4000dccf01a010e196154a1b998408f949d734389626ba97cb3331ee87e01dd5badc58f41b2",
1675        );
1676        let c = U384::from_hex_unchecked(
1677            "193a6afd4d2cacc01101bdd44ec864f517b0f6f5a1d3020dd91594dc1de752cf775f1242630e36",
1678        );
1679        assert_eq!(c - a, b);
1680    }
1681
1682    #[test]
1683    fn sub_two_384_bit_integers_8() {
1684        let a = U384::from_hex_unchecked("07df9c74fa9d5aafa74a87dbbf93215659d8a3e1706d4b06de9512284802580eb36ae12ea59f90db5b1799d0970a42e");
1685        let b = U384::from_hex_unchecked("d515e54973f0643a6a9957579c1f84020a6a91d5d5f27b75401c7538d2c9ea9cafff44a2c606877d46c49a3433cc85e");
1686        let c = U384::from_hex_unchecked("dcf581be6e8dbeea11e3df335bb2a558644335b7465fc67c1eb187611acc42ab636a25d16ba61858a1dc3404cad6c8c");
1687        assert_eq!(c - a, b);
1688    }
1689
1690    #[test]
1691    fn sub_two_384_bit_integers_9() {
1692        let a = U384::from_hex_unchecked("92977527a0f8ba00d18c1b2f1900d965d4a70e5f5f54468ffb2d4d41519385f24b078a0e7d0281d5ad0c36724dc4233");
1693        let b = U384::from_hex_unchecked("46facf9953a9494822bf18836ffd7e55c48b30aa81e17fa1ace0b473015307e4622b8bd6fa68ef654796a183abde842");
1694        let c = U384::from_hex_unchecked("d99244c0f4a20348f44b33b288fe57bb99323f09e135c631a80e01b452e68dd6ad3315e5776b713af4a2d7f5f9a2a75");
1695        assert_eq!(c - a, b);
1696    }
1697
1698    #[test]
1699    fn sub_two_384_bit_integers_11_without_overflow() {
1700        let a = U384::from_u64(334);
1701        let b_expected = U384::from_u64(666);
1702        let c = U384::from_u64(1000);
1703        let (b, underflow) = U384::sub(&c, &a);
1704        assert!(!underflow);
1705        assert_eq!(b_expected, b);
1706    }
1707
1708    #[test]
1709    fn sub_two_384_bit_integers_11_with_overflow() {
1710        let a = U384::from_u64(334);
1711        let b_expected = U384::from_hex_unchecked("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd66");
1712        let c = U384::from_u64(1000);
1713        let (b, underflow) = U384::sub(&a, &c);
1714        assert!(underflow);
1715        assert_eq!(b_expected, b);
1716    }
1717
1718    #[test]
1719    fn partial_order_works() {
1720        assert!(U384::from_u64(10) <= U384::from_u64(10));
1721        assert!(U384::from_u64(1) < U384::from_u64(2));
1722        assert!(U384::from_u64(2) >= U384::from_u64(1));
1723
1724        assert!(U384::from_u64(10) >= U384::from_u64(10));
1725        assert!(U384::from_u64(2) > U384::from_u64(1));
1726        assert!(U384::from_u64(1) <= U384::from_u64(2));
1727
1728        let a = U384::from_hex_unchecked("92977527a0f8ba00d18c1b2f1900d965d4a70e5f5f54468ffb2d4d41519385f24b078a0e7d0281d5ad0c36724dc4233");
1729        let c = U384::from_hex_unchecked("d99244c0f4a20348f44b33b288fe57bb99323f09e135c631a80e01b452e68dd6ad3315e5776b713af4a2d7f5f9a2a75");
1730
1731        assert!(&a <= &a);
1732        assert!(&a >= &a);
1733        assert!(&a >= &a);
1734        assert!(&a <= &a);
1735        assert!(&a < &(&a + U384::from_u64(1)));
1736        assert!(&a <= &(&a + U384::from_u64(1)));
1737        assert!(&a + U384::from_u64(1) > a);
1738        assert!((&a + U384::from_u64(1) >= a));
1739        assert!(&a <= &c);
1740        assert!(&a < &c);
1741        assert!(&a < &c);
1742        assert!(&a <= &c);
1743        assert!(&c > &a);
1744        assert!(&c >= &a);
1745        assert!(&c >= &a);
1746        assert!(&c > &a);
1747        assert!(a < c);
1748    }
1749
1750    #[test]
1751    fn mul_two_384_bit_integers_works_1() {
1752        let a = U384::from_u64(3);
1753        let b = U384::from_u64(8);
1754        let c = U384::from_u64(3 * 8);
1755        assert_eq!(a * b, c);
1756    }
1757
1758    #[test]
1759    fn mul_two_384_bit_integers_works_2() {
1760        let a = U384::from_hex_unchecked("6131d99f840b3b0");
1761        let b = U384::from_hex_unchecked("6f5c466db398f43");
1762        let c = U384::from_hex_unchecked("2a47a603a77f871dfbb937af7e5710");
1763        assert_eq!(a * b, c);
1764    }
1765
1766    #[test]
1767    fn mul_two_384_bit_integers_works_3() {
1768        let a = U384::from_hex_unchecked("84a6add5db9e095b2e0f6b40eff8ee");
1769        let b = U384::from_hex_unchecked("2347db918f725461bec2d5c57");
1770        let c = U384::from_hex_unchecked("124805c476c9462adc0df6c88495d4253f5c38033afc18d78d920e2");
1771        assert_eq!(a * b, c);
1772    }
1773
1774    #[test]
1775    fn mul_two_384_bit_integers_works_4() {
1776        let a = U384::from_hex_unchecked("04050753dd7c0b06c404633016f87040");
1777        let b = U384::from_hex_unchecked("dc3830be041b3b4476445fcad3dac0f6f3a53e4ba12da");
1778        let c = U384::from_hex_unchecked(
1779            "375342999dab7f52f4010c4abc2e18b55218015931a55d6053ac39e86e2a47d6b1cb95f41680",
1780        );
1781        assert_eq!(a * b, c);
1782    }
1783
1784    #[test]
1785    fn mul_two_384_bit_integers_works_5() {
1786        let a = U384::from_hex_unchecked("7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8");
1787        let b = U384::from_hex_unchecked("2");
1788        let c_expected = U384::from_hex_unchecked(
1789            "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0",
1790        );
1791        assert_eq!(a * b, c_expected);
1792    }
1793
1794    #[test]
1795    #[should_panic]
1796    fn mul_two_384_bit_integers_works_6() {
1797        let a = U384::from_hex_unchecked("800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
1798        let b = U384::from_hex_unchecked("2");
1799        let _c = a * b;
1800    }
1801
1802    #[test]
1803    fn mul_two_384_bit_integers_works_7_hi_lo() {
1804        let a = U384::from_hex_unchecked("04050753dd7c0b06c404633016f87040");
1805        let b = U384::from_hex_unchecked("dc3830be041b3b4476445fcad3dac0f6f3a53e4ba12da");
1806        let hi_expected = U384::from_hex_unchecked("0");
1807        let lo_expected = U384::from_hex_unchecked(
1808            "375342999dab7f52f4010c4abc2e18b55218015931a55d6053ac39e86e2a47d6b1cb95f41680",
1809        );
1810        let (hi, lo) = U384::mul(&a, &b);
1811        assert_eq!(hi, hi_expected);
1812        assert_eq!(lo, lo_expected);
1813    }
1814
1815    #[test]
1816    fn mul_two_384_bit_integers_works_8_hi_lo() {
1817        let a = U384::from_hex_unchecked("5e2d939b602a50911232731d04fe6f40c05f97da0602307099fb991f9b414e2d52bef130349ec18db1a0215ea6caf76");
1818        let b = U384::from_hex_unchecked("3f3ad1611ab58212f92a2484e9560935b9ac4615fe61cfed1a4861e193a74d20c94f9f88d8b2cc089543c3f699969d9");
1819        let hi_expected = U384::from_hex_unchecked(
1820            "1742daad9c7861dd3499e7ece65467e337937b27e20d641b225bfe00323d33ed62715654eadc092b057a5f19f2ad6c",
1821        );
1822        let lo_expected = U384::from_hex_unchecked("9969c0417b9304d9c16b046c860447d3533999e16710d2e90a44959a168816c015ffb44b987e8cbb82bd46b08d9e2106");
1823        let (hi, lo) = U384::mul(&a, &b);
1824        assert_eq!(hi, hi_expected);
1825        assert_eq!(lo, lo_expected);
1826    }
1827
1828    #[test]
1829    fn mul_two_384_bit_integers_works_9_hi_lo() {
1830        let a = U384::from_hex_unchecked("800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
1831        let b = U384::from_hex_unchecked("2");
1832        let hi_expected = U384::from_hex_unchecked("1");
1833        let lo_expected = U384::from_hex_unchecked("0");
1834        let (hi, lo) = U384::mul(&a, &b);
1835        assert_eq!(hi, hi_expected);
1836        assert_eq!(lo, lo_expected);
1837    }
1838
1839    #[test]
1840    fn shift_left_on_384_bit_integer_works_1() {
1841        let a = U384::from_hex_unchecked("1");
1842        let b = U384::from_hex_unchecked("10");
1843        assert_eq!(a << 4, b);
1844    }
1845
1846    #[test]
1847    fn shift_left_on_384_bit_integer_works_2() {
1848        let a = U384::from_u64(1);
1849        let b = U384::from_u128(1_u128 << 64);
1850        assert_eq!(a << 64, b);
1851    }
1852
1853    #[test]
1854    fn shift_left_on_384_bit_integer_works_3() {
1855        let a = U384::from_hex_unchecked("10");
1856        let b = U384::from_hex_unchecked("1000");
1857        assert_eq!(&a << 8, b);
1858    }
1859
1860    #[test]
1861    fn shift_left_on_384_bit_integer_works_4() {
1862        let a = U384::from_hex_unchecked("e45542992b6844553f3cb1c5ac33e7fa5");
1863        let b = U384::from_hex_unchecked("391550a64ada11154fcf2c716b0cf9fe940");
1864        assert_eq!(a << 6, b);
1865    }
1866
1867    #[test]
1868    fn shift_left_on_384_bit_integer_works_5() {
1869        let a = U384::from_hex_unchecked(
1870            "03303f4d6c2d1caf0c24a6b0239b679a8390aa99bead76bc0093b1bc1a8101f5ce",
1871        );
1872        let b = U384::from_hex_unchecked("6607e9ad85a395e18494d604736cf35072155337d5aed7801276378350203eb9c0000000000000000000000000000000");
1873        assert_eq!(&a << 125, b);
1874    }
1875
1876    #[test]
1877    fn shift_left_on_384_bit_integer_works_6() {
1878        let a = U384::from_hex_unchecked("762e8968bc392ed786ab132f0b5b0cacd385dd51de3a");
1879        let b = U384::from_hex_unchecked(
1880            "762e8968bc392ed786ab132f0b5b0cacd385dd51de3a00000000000000000000000000000000",
1881        );
1882        assert_eq!(&a << (64 * 2), b);
1883    }
1884
1885    #[test]
1886    fn shift_left_on_384_bit_integer_works_7() {
1887        let a = U384::from_hex_unchecked("90823e0bd707f");
1888        let b = U384::from_hex_unchecked(
1889            "90823e0bd707f000000000000000000000000000000000000000000000000",
1890        );
1891        assert_eq!(&a << (64 * 3), b);
1892    }
1893
1894    #[test]
1895    fn shift_right_on_384_bit_integer_works_1() {
1896        let a = U384::from_hex_unchecked("1");
1897        let b = U384::from_hex_unchecked("10");
1898        assert_eq!(b >> 4, a);
1899    }
1900
1901    #[test]
1902    fn shift_right_on_384_bit_integer_works_2() {
1903        let a = U384::from_hex_unchecked("10");
1904        let b = U384::from_hex_unchecked("1000");
1905        assert_eq!(&b >> 8, a);
1906    }
1907
1908    #[test]
1909    fn shift_right_on_384_bit_integer_works_3() {
1910        let a = U384::from_hex_unchecked("e45542992b6844553f3cb1c5ac33e7fa5");
1911        let b = U384::from_hex_unchecked("391550a64ada11154fcf2c716b0cf9fe940");
1912        assert_eq!(b >> 6, a);
1913    }
1914
1915    #[test]
1916    fn shift_right_on_384_bit_integer_works_4() {
1917        let a = U384::from_hex_unchecked(
1918            "03303f4d6c2d1caf0c24a6b0239b679a8390aa99bead76bc0093b1bc1a8101f5ce",
1919        );
1920        let b = U384::from_hex_unchecked("6607e9ad85a395e18494d604736cf35072155337d5aed7801276378350203eb9c0000000000000000000000000000000");
1921        assert_eq!(&b >> 125, a);
1922    }
1923
1924    #[test]
1925    fn shift_right_on_384_bit_integer_works_5() {
1926        let a = U384::from_hex_unchecked("ba6ab46f9a9a2f20e4061b67ce4d8c3da98091cf990d7b14ef47ffe27370abbdeb6a3ce9f9cbf5df1b2430114c8558eb");
1927        let b =
1928            U384::from_hex_unchecked("174d568df35345e41c80c36cf9c9b187b5301239f321af629de8fffc4e6");
1929        assert_eq!(a >> 151, b);
1930    }
1931
1932    #[test]
1933    fn shift_right_on_384_bit_integer_works_6() {
1934        let a = U384::from_hex_unchecked(
1935            "076c075d2f65e39b9ecdde8bf6f8c94241962ce0f557b7739673200c777152eb7e772ad35",
1936        );
1937        let b = U384::from_hex_unchecked("ed80eba5ecbc7373d9bbd17edf19284832c59c1eaaf6ee7");
1938        assert_eq!(&a >> 99, b);
1939    }
1940
1941    #[test]
1942    fn shift_right_on_384_bit_integer_works_7() {
1943        let a = U384::from_hex_unchecked("6a9ce35d8940a5ebd29604ce9a182ade76f03f7e9965760b84a8cfd1d3dd2e612669fe000e58b2af688fd90");
1944        let b = U384::from_hex_unchecked("6a9ce35d8940a5ebd29604ce9a182ade76f03f7");
1945        assert_eq!(&a >> (64 * 3), b);
1946    }
1947
1948    #[test]
1949    fn shift_right_on_384_bit_integer_works_8() {
1950        let a = U384::from_hex_unchecked("5322c128ec84081b6c376c108ebd7fd36bbd44f71ee5e6ad6bcb3dd1c5265bd7db75c90b2665a0826d17600f0e9");
1951        let b =
1952            U384::from_hex_unchecked("5322c128ec84081b6c376c108ebd7fd36bbd44f71ee5e6ad6bcb3dd1c52");
1953        assert_eq!(&a >> (64 * 2), b);
1954    }
1955
1956    #[test]
1957    #[cfg(feature = "alloc")]
1958    fn to_be_bytes_works() {
1959        let number = U384::from_u64(1);
1960        let expected_bytes = [
1961            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1962            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1963        ];
1964
1965        assert_eq!(number.to_bytes_be(), expected_bytes);
1966    }
1967
1968    #[test]
1969    #[cfg(feature = "alloc")]
1970    fn to_le_bytes_works() {
1971        let number = U384::from_u64(1);
1972        let expected_bytes = [
1973            1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1974            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1975        ];
1976
1977        assert_eq!(number.to_bytes_le(), expected_bytes);
1978    }
1979
1980    #[test]
1981    fn from_bytes_be_works() {
1982        let bytes = [
1983            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1984            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1985        ];
1986        let expected_number = U384::from_u64(1);
1987
1988        assert_eq!(U384::from_bytes_be(&bytes).unwrap(), expected_number);
1989    }
1990
1991    #[test]
1992    fn from_bytes_le_works() {
1993        let bytes = [
1994            1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1995            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1996        ];
1997        let expected_number = U384::from_u64(1);
1998
1999        assert_eq!(U384::from_bytes_le(&bytes).unwrap(), expected_number);
2000    }
2001
2002    #[test]
2003    fn from_bytes_be_works_with_extra_data() {
2004        let bytes = [
2005            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2006            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2007        ];
2008        let expected_number = U384::from_u64(0);
2009
2010        assert_eq!(U384::from_bytes_be(&bytes).unwrap(), expected_number);
2011    }
2012
2013    #[test]
2014    #[should_panic]
2015    fn from_bytes_be_errs_with_less_data() {
2016        let bytes = [0, 0, 0, 0, 0];
2017        U384::from_bytes_be(&bytes).unwrap();
2018    }
2019
2020    #[test]
2021    #[should_panic]
2022    fn from_bytes_le_errs_with_less_data() {
2023        let bytes = [0, 0, 0, 0, 0];
2024        U384::from_bytes_le(&bytes).unwrap();
2025    }
2026
2027    #[test]
2028    fn from_bytes_le_works_with_extra_data() {
2029        let bytes = [
2030            1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2031            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1,
2032        ];
2033        let expected_number = U384::from_u64(1);
2034
2035        assert_eq!(U384::from_bytes_le(&bytes).unwrap(), expected_number);
2036    }
2037
2038    #[test]
2039    fn test_square_0() {
2040        let a = U384::from_hex_unchecked("362e35606447fb568704026c25da7a304bc7bd0aea36a61d77d4151395078cfa332b9d4928a60721eece725bbc81e158");
2041        let (hi, lo) = U384::square(&a);
2042        assert_eq!(lo, U384::from_hex_unchecked("11724caeb10c4bce5319097d74aed2246e2942b56b7365b5b2f8ceb3bb847db4828862043299d798577996e210bce40"));
2043        assert_eq!(hi, U384::from_hex_unchecked("b7786dbe41375b7ff64dbdc65152ef7d3fdbf499485e26486201cdbfb71b5673c77eb355a1274d08cbfbc1a4cdfdfad"));
2044    }
2045
2046    #[test]
2047    fn test_square_1() {
2048        let a = U384::from_limbs([0, 0, 0, 0, 0, u64::MAX]);
2049        let (hi, lo) = U384::square(&a);
2050        assert_eq!(
2051            lo,
2052            U384::from_hex_unchecked("fffffffffffffffe0000000000000001")
2053        );
2054        assert_eq!(hi, U384::from_hex_unchecked("0"));
2055    }
2056
2057    #[test]
2058    fn test_square_2() {
2059        let a = U384::from_limbs([0, 0, 0, 0, u64::MAX, 0]);
2060        let (hi, lo) = U384::square(&a);
2061        assert_eq!(
2062            lo,
2063            U384::from_hex_unchecked(
2064                "fffffffffffffffe000000000000000100000000000000000000000000000000"
2065            )
2066        );
2067        assert_eq!(hi, U384::from_hex_unchecked("0"));
2068    }
2069
2070    #[test]
2071    fn test_square_3() {
2072        let a = U384::from_limbs([0, 0, 0, u64::MAX, 0, 0]);
2073        let (hi, lo) = U384::square(&a);
2074        assert_eq!(lo, U384::from_hex_unchecked("fffffffffffffffe00000000000000010000000000000000000000000000000000000000000000000000000000000000"));
2075        assert_eq!(hi, U384::from_hex_unchecked("0"));
2076    }
2077
2078    #[test]
2079    fn test_square_4() {
2080        let a = U384::from_limbs([0, 0, u64::MAX, 0, 0, 0]);
2081        let (hi, lo) = U384::square(&a);
2082        assert_eq!(lo, U384::from_hex_unchecked("0"));
2083        assert_eq!(
2084            hi,
2085            U384::from_hex_unchecked("fffffffffffffffe0000000000000001")
2086        );
2087    }
2088
2089    #[test]
2090    fn test_square_5() {
2091        let a = U384::from_limbs([0, 0, u64::MAX, u64::MAX, u64::MAX, u64::MAX]);
2092        let (hi, lo) = U384::square(&a);
2093        assert_eq!(lo, U384::from_hex_unchecked("fffffffffffffffffffffffffffffffe0000000000000000000000000000000000000000000000000000000000000001"));
2094        assert_eq!(
2095            hi,
2096            U384::from_hex_unchecked("ffffffffffffffffffffffffffffffff")
2097        );
2098    }
2099
2100    #[test]
2101    fn test_square_6() {
2102        let a = U384::from_limbs([0, u64::MAX, u64::MAX, u64::MAX, u64::MAX, u64::MAX]);
2103        let (hi, lo) = U384::square(&a);
2104        assert_eq!(lo, U384::from_hex_unchecked("fffffffffffffffe00000000000000000000000000000000000000000000000000000000000000000000000000000001"));
2105        assert_eq!(
2106            hi,
2107            U384::from_hex_unchecked(
2108                "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
2109            )
2110        );
2111    }
2112
2113    #[test]
2114    fn test_square_7() {
2115        let a = U384::from_limbs([u64::MAX, u64::MAX, u64::MAX, u64::MAX, u64::MAX, u64::MAX]);
2116        let (hi, lo) = U384::square(&a);
2117        assert_eq!(lo, U384::from_hex_unchecked("1"));
2118        assert_eq!(hi, U384::from_hex_unchecked("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"));
2119    }
2120}
2121
2122#[cfg(test)]
2123mod tests_u256 {
2124    use crate::unsigned_integer::element::ByteConversion;
2125    use crate::unsigned_integer::element::{UnsignedInteger, U256};
2126    #[cfg(feature = "proptest")]
2127    use proptest::prelude::*;
2128    #[cfg(feature = "proptest")]
2129    use std::ops::Shr;
2130
2131    #[cfg(feature = "proptest")]
2132    const N_LIMBS: usize = 4;
2133    #[cfg(feature = "proptest")]
2134    type Uint = UnsignedInteger<N_LIMBS>;
2135
2136    #[cfg(feature = "proptest")]
2137    proptest! {
2138        #[test]
2139        fn bitand(a in any::<Uint>(), b in any::<Uint>()) {
2140            let result = a & b;
2141
2142            for i in 0..N_LIMBS {
2143                assert_eq!(result.limbs[i], a.limbs[i] & b.limbs[i]);
2144            }
2145        }
2146
2147        #[test]
2148        fn bitand_assign(a in any::<Uint>(), b in any::<Uint>()) {
2149            let mut result = a;
2150            result &= b;
2151
2152            for i in 0..N_LIMBS {
2153                assert_eq!(result.limbs[i], a.limbs[i] & b.limbs[i]);
2154            }
2155        }
2156
2157        #[test]
2158        fn bitor(a in any::<Uint>(), b in any::<Uint>()) {
2159            let result = a | b;
2160
2161            for i in 0..N_LIMBS {
2162                assert_eq!(result.limbs[i], a.limbs[i] | b.limbs[i]);
2163            }
2164        }
2165
2166        #[test]
2167        fn bitor_assign(a in any::<Uint>(), b in any::<Uint>()) {
2168            let mut result = a;
2169            result |= b;
2170
2171            for i in 0..N_LIMBS {
2172                assert_eq!(result.limbs[i], a.limbs[i] | b.limbs[i]);
2173            }
2174        }
2175
2176        #[test]
2177        fn bitxor(a in any::<Uint>(), b in any::<Uint>()) {
2178            let result = a ^ b;
2179
2180            for i in 0..N_LIMBS {
2181                assert_eq!(result.limbs[i], a.limbs[i] ^ b.limbs[i]);
2182            }
2183        }
2184
2185        #[test]
2186        fn bitxor_assign(a in any::<Uint>(), b in any::<Uint>()) {
2187            let mut result = a;
2188            result ^= b;
2189
2190            for i in 0..N_LIMBS {
2191                assert_eq!(result.limbs[i], a.limbs[i] ^ b.limbs[i]);
2192            }
2193        }
2194
2195        #[test]
2196        fn div_rem(a in any::<Uint>(), b in any::<Uint>()) {
2197            let a = a.shr(128);
2198            let b = b.shr(128);
2199            assert_eq!((a * b).div_rem(&b), (a, Uint::from_u64(0)));
2200        }
2201    }
2202
2203    #[test]
2204    fn construct_new_integer_from_limbs() {
2205        let a: U256 = UnsignedInteger {
2206            limbs: [0, 1, 2, 3],
2207        };
2208        assert_eq!(U256::from_limbs([0, 1, 2, 3]), a);
2209    }
2210
2211    #[test]
2212    fn construct_new_integer_from_u64_1() {
2213        let a = U256::from_u64(1_u64);
2214        assert_eq!(a.limbs, [0, 0, 0, 1]);
2215    }
2216
2217    #[test]
2218    fn construct_new_integer_from_u64_2() {
2219        let a = U256::from_u64(u64::MAX);
2220        assert_eq!(a.limbs, [0, 0, 0, u64::MAX]);
2221    }
2222
2223    #[test]
2224    fn construct_new_integer_from_u128_1() {
2225        let a = U256::from_u128(u128::MAX);
2226        assert_eq!(a.limbs, [0, 0, u64::MAX, u64::MAX]);
2227    }
2228
2229    #[test]
2230    fn construct_new_integer_from_u128_4() {
2231        let a = U256::from_u128(276371540478856090688472252609570374439);
2232        assert_eq!(a.limbs, [0, 0, 14982131230017065096, 14596400355126379303]);
2233    }
2234
2235    #[test]
2236    fn construct_new_integer_from_hex_1() {
2237        let a = U256::from_hex_unchecked("1");
2238        assert_eq!(a.limbs, [0, 0, 0, 1]);
2239    }
2240
2241    #[test]
2242    fn construct_new_integer_from_hex_2() {
2243        let a = U256::from_hex_unchecked("f");
2244        assert_eq!(a.limbs, [0, 0, 0, 15]);
2245    }
2246
2247    #[test]
2248    fn construct_new_integer_from_hex_3() {
2249        let a = U256::from_hex_unchecked("10000000000000000");
2250        assert_eq!(a.limbs, [0, 0, 1, 0]);
2251    }
2252
2253    #[test]
2254    fn construct_new_integer_from_hex_4() {
2255        let a = U256::from_hex_unchecked("a0000000000000000");
2256        assert_eq!(a.limbs, [0, 0, 10, 0]);
2257    }
2258
2259    #[test]
2260    fn construct_new_integer_from_hex_5() {
2261        let a = U256::from_hex_unchecked("ffffffffffffffffff");
2262        assert_eq!(a.limbs, [0, 0, 255, u64::MAX]);
2263    }
2264
2265    #[test]
2266    fn construct_new_integer_from_hex_6() {
2267        let a = U256::from_hex_unchecked("eb235f6144d9e91f4b14");
2268        assert_eq!(a.limbs, [0, 0, 60195, 6872850209053821716]);
2269    }
2270
2271    #[test]
2272    fn construct_new_integer_from_hex_7() {
2273        let a = U256::from_hex_unchecked("2b20aaa5cf482b239e2897a787faf4660cc95597854beb2");
2274        assert_eq!(
2275            a.limbs,
2276            [
2277                0,
2278                194229460750598834,
2279                4171047363999149894,
2280                6975114134393503410
2281            ]
2282        );
2283    }
2284
2285    #[test]
2286    fn construct_new_integer_from_hex_8() {
2287        let a = U256::from_hex_unchecked(
2288            "2B20AAA5CF482B239E2897A787FAF4660CC95597854BEB235F6144D9E91F4B14",
2289        );
2290        assert_eq!(
2291            a.limbs,
2292            [
2293                3107671372009581347,
2294                11396525602857743462,
2295                921361708038744867,
2296                6872850209053821716
2297            ]
2298        );
2299    }
2300
2301    #[test]
2302    fn construct_new_integer_from_dec_1() {
2303        let a = U256::from_dec_str("1").unwrap();
2304        assert_eq!(a.limbs, [0, 0, 0, 1]);
2305    }
2306
2307    #[test]
2308    fn construct_integer_from_invalid_hex_returns_error() {
2309        use crate::unsigned_integer::element::CreationError;
2310        assert_eq!(U256::from_hex("0xaO"), Err(CreationError::InvalidHexString));
2311        assert_eq!(U256::from_hex("0xOa"), Err(CreationError::InvalidHexString));
2312        assert_eq!(U256::from_hex("0xm"), Err(CreationError::InvalidHexString));
2313    }
2314
2315    #[test]
2316    fn construct_new_integer_from_dec_2() {
2317        let a = U256::from_dec_str("15").unwrap();
2318        assert_eq!(a.limbs, [0, 0, 0, 15]);
2319    }
2320
2321    #[test]
2322    fn construct_new_integer_from_dec_3() {
2323        let a = U256::from_dec_str("18446744073709551616").unwrap();
2324        assert_eq!(a.limbs, [0, 0, 1, 0]);
2325    }
2326
2327    #[test]
2328    fn construct_new_integer_from_dec_4() {
2329        let a = U256::from_dec_str("184467440737095516160").unwrap();
2330        assert_eq!(a.limbs, [0, 0, 10, 0]);
2331    }
2332
2333    #[test]
2334    fn construct_new_integer_from_dec_5() {
2335        let a = U256::from_dec_str("4722366482869645213695").unwrap();
2336        assert_eq!(a.limbs, [0, 0, 255, u64::MAX]);
2337    }
2338
2339    #[test]
2340    fn construct_new_integer_from_dec_6() {
2341        let a = U256::from_dec_str("1110408632367155513346836").unwrap();
2342        assert_eq!(a.limbs, [0, 0, 60195, 6872850209053821716]);
2343    }
2344
2345    #[test]
2346    fn construct_new_integer_from_dec_7() {
2347        let a =
2348            U256::from_dec_str("66092860629991288370279803883558073888453977263446474418").unwrap();
2349        assert_eq!(
2350            a.limbs,
2351            [
2352                0,
2353                194229460750598834,
2354                4171047363999149894,
2355                6975114134393503410
2356            ]
2357        );
2358    }
2359
2360    #[test]
2361    fn construct_new_integer_from_dec_8() {
2362        let a = U256::from_dec_str(
2363            "19507169362252850253634654373914901165934018806002526957372506333098895428372",
2364        )
2365        .unwrap();
2366        assert_eq!(
2367            a.limbs,
2368            [
2369                3107671372009581347,
2370                11396525602857743462,
2371                921361708038744867,
2372                6872850209053821716
2373            ]
2374        );
2375    }
2376
2377    #[test]
2378    fn construct_new_integer_from_dec_empty() {
2379        assert!(U256::from_dec_str("").is_err());
2380    }
2381
2382    #[test]
2383    fn construct_new_integer_from_dec_invalid() {
2384        assert!(U256::from_dec_str("0xff").is_err());
2385    }
2386
2387    #[test]
2388    fn equality_works_1() {
2389        let a = U256::from_hex_unchecked("1");
2390        let b = U256 {
2391            limbs: [0, 0, 0, 1],
2392        };
2393        assert_eq!(a, b);
2394    }
2395    #[test]
2396    fn equality_works_2() {
2397        let a = U256::from_hex_unchecked("f");
2398        let b = U256 {
2399            limbs: [0, 0, 0, 15],
2400        };
2401        assert_eq!(a, b);
2402    }
2403
2404    #[test]
2405    fn equality_works_3() {
2406        let a = U256::from_hex_unchecked("10000000000000000");
2407        let b = U256 {
2408            limbs: [0, 0, 1, 0],
2409        };
2410        assert_eq!(a, b);
2411    }
2412
2413    #[test]
2414    fn equality_works_4() {
2415        let a = U256::from_hex_unchecked("a0000000000000000");
2416        let b = U256 {
2417            limbs: [0, 0, 10, 0],
2418        };
2419        assert_eq!(a, b);
2420    }
2421
2422    #[test]
2423    fn equality_works_5() {
2424        let a = U256::from_hex_unchecked("ffffffffffffffffff");
2425        let b = U256 {
2426            limbs: [0, 0, u8::MAX as u64, u64::MAX],
2427        };
2428        assert_eq!(a, b);
2429    }
2430
2431    #[test]
2432    fn equality_works_6() {
2433        let a = U256::from_hex_unchecked("eb235f6144d9e91f4b14");
2434        let b = U256 {
2435            limbs: [0, 0, 60195, 6872850209053821716],
2436        };
2437        assert_eq!(a, b);
2438    }
2439
2440    #[test]
2441    fn equality_works_7() {
2442        let a = U256::from_hex_unchecked("2b20aaa5cf482b239e2897a787faf4660cc95597854beb2");
2443        let b = U256 {
2444            limbs: [
2445                0,
2446                194229460750598834,
2447                4171047363999149894,
2448                6975114134393503410,
2449            ],
2450        };
2451        assert_eq!(a, b);
2452    }
2453
2454    #[test]
2455    fn equality_works_8() {
2456        let a = U256::from_hex_unchecked(
2457            "2B20AAA5CF482B239E2897A787FAF4660CC95597854BEB235F6144D9E91F4B14",
2458        );
2459        let b = U256 {
2460            limbs: [
2461                3107671372009581347,
2462                11396525602857743462,
2463                921361708038744867,
2464                6872850209053821716,
2465            ],
2466        };
2467        assert_eq!(a, b);
2468    }
2469
2470    #[test]
2471    fn equality_works_9() {
2472        let a = U256::from_hex_unchecked("fffffff");
2473        let b = U256::from_hex_unchecked("fefffff");
2474        assert_ne!(a, b);
2475    }
2476
2477    #[test]
2478    fn equality_works_10() {
2479        let a = U256::from_hex_unchecked("ffff000000000000");
2480        let b = U256::from_hex_unchecked("ffff000000100000");
2481        assert_ne!(a, b);
2482    }
2483
2484    #[test]
2485    fn double_256_bit_integer_1() {
2486        let a = U256::from_u64(2);
2487        let b = U256::from_u64(5);
2488        let c = U256::from_u64(7);
2489        assert_eq!(U256::double(&a).0, a + a);
2490        assert_eq!(U256::double(&b).0, b + b);
2491        assert_eq!(U256::double(&c).0, c + c);
2492    }
2493
2494    #[test]
2495    fn add_two_256_bit_integers_1() {
2496        let a = U256::from_u64(2);
2497        let b = U256::from_u64(5);
2498        let c = U256::from_u64(7);
2499        assert_eq!(a + b, c);
2500    }
2501
2502    #[test]
2503    fn add_two_256_bit_integers_2() {
2504        let a = U256::from_u64(334);
2505        let b = U256::from_u64(666);
2506        let c = U256::from_u64(1000);
2507        assert_eq!(a + b, c);
2508    }
2509
2510    #[test]
2511    fn add_two_256_bit_integers_3() {
2512        let a = U256::from_hex_unchecked("ffffffffffffffff");
2513        let b = U256::from_hex_unchecked("1");
2514        let c = U256::from_hex_unchecked("10000000000000000");
2515        assert_eq!(a + b, c);
2516    }
2517
2518    #[test]
2519    fn add_two_256_bit_integers_4() {
2520        let a = U256::from_hex_unchecked("b58e1e0b66");
2521        let b = U256::from_hex_unchecked("55469d9619");
2522        let c = U256::from_hex_unchecked("10ad4bba17f");
2523        assert_eq!(a + b, c);
2524    }
2525
2526    #[test]
2527    fn add_two_256_bit_integers_5() {
2528        let a = U256::from_hex_unchecked("e8dff25cb6160f7705221da6f");
2529        let b = U256::from_hex_unchecked("ab879169b5f80dc8a7969f0b0");
2530        let c = U256::from_hex_unchecked("1946783c66c0e1d3facb8bcb1f");
2531        assert_eq!(a + b, c);
2532    }
2533
2534    #[test]
2535    fn add_two_256_bit_integers_6() {
2536        let a = U256::from_hex_unchecked("9adf291af3a64d59e14e7b440c850508014c551ed5");
2537        let b = U256::from_hex_unchecked("e7948474bce907f0feaf7e5d741a8cd2f6d1fb9448");
2538        let c = U256::from_hex_unchecked("18273ad8fb08f554adffdf9a1809f91daf81e50b31d");
2539        assert_eq!(a + b, c);
2540    }
2541
2542    #[test]
2543    fn add_two_256_bit_integers_7() {
2544        let a = U256::from_hex_unchecked(
2545            "10d3bc05496380cfe27bf5d97ddb99ac95eb5ecfbd3907eadf877a4c2dfa05f6",
2546        );
2547        let b = U256::from_hex_unchecked(
2548            "0866aef803c92bf02e85c7fad0eccb4881c59825e499fa22f98e1a8fefed4cd9",
2549        );
2550        let c = U256::from_hex_unchecked(
2551            "193a6afd4d2cacc01101bdd44ec864f517b0f6f5a1d3020dd91594dc1de752cf",
2552        );
2553        assert_eq!(a + b, c);
2554    }
2555
2556    #[test]
2557    fn add_two_256_bit_integers_8() {
2558        let a = U256::from_hex_unchecked(
2559            "07df9c74fa9d5aafa74a87dbbf93215659d8a3e1706d4b06de9512284802580f",
2560        );
2561        let b = U256::from_hex_unchecked(
2562            "d515e54973f0643a6a9957579c1f84020a6a91d5d5f27b75401c7538d2c9ea9c",
2563        );
2564        let c = U256::from_hex_unchecked(
2565            "dcf581be6e8dbeea11e3df335bb2a558644335b7465fc67c1eb187611acc42ab",
2566        );
2567        assert_eq!(a + b, c);
2568    }
2569
2570    #[test]
2571    fn add_two_256_bit_integers_9() {
2572        let a = U256::from_hex_unchecked(
2573            "92977527a0f8ba00d18c1b2f1900d965d4a70e5f5f54468ffb2d4d41519385f2",
2574        );
2575        let b = U256::from_hex_unchecked(
2576            "46facf9953a9494822bf18836ffd7e55c48b30aa81e17fa1ace0b473015307e4",
2577        );
2578        let c = U256::from_hex_unchecked(
2579            "d99244c0f4a20348f44b33b288fe57bb99323f09e135c631a80e01b452e68dd6",
2580        );
2581        assert_eq!(a + b, c);
2582    }
2583
2584    #[test]
2585    fn add_two_256_bit_integers_10() {
2586        let a = U256::from_hex_unchecked(
2587            "07df9c74fa9d5aafa74a87dbbf93215659d8a3e1706d4b06de9512284802580f",
2588        );
2589        let b = U256::from_hex_unchecked(
2590            "d515e54973f0643a6a9957579c1f84020a6a91d5d5f27b75401c7538d2c9ea9c",
2591        );
2592        let c_expected = U256::from_hex_unchecked(
2593            "dcf581be6e8dbeea11e3df335bb2a558644335b7465fc67c1eb187611acc42ab",
2594        );
2595        let (c, overflow) = U256::add(&a, &b);
2596        assert_eq!(c, c_expected);
2597        assert!(!overflow);
2598    }
2599
2600    #[test]
2601    fn add_two_256_bit_integers_11() {
2602        let a = U256::from_hex_unchecked(
2603            "92977527a0f8ba00d18c1b2f1900d965d4a70e5f5f54468ffb2d4d41519385f2",
2604        );
2605        let b = U256::from_hex_unchecked(
2606            "46facf9953a9494822bf18836ffd7e55c48b30aa81e17fa1ace0b473015307e4",
2607        );
2608        let c_expected = U256::from_hex_unchecked(
2609            "d99244c0f4a20348f44b33b288fe57bb99323f09e135c631a80e01b452e68dd6",
2610        );
2611        let (c, overflow) = U256::add(&a, &b);
2612        assert_eq!(c, c_expected);
2613        assert!(!overflow);
2614    }
2615
2616    #[test]
2617    fn add_two_256_bit_integers_12_with_overflow() {
2618        let a = U256::from_hex_unchecked(
2619            "b07bc844363dd56467d9ebdd5929e9bb34a8e2577db77df6cf8f2ac45bd3d0bc",
2620        );
2621        let b = U256::from_hex_unchecked(
2622            "cbbc474761bb7995ff54e25fa5d30295604fe3545d0cde405e72d8c0acebb119",
2623        );
2624        let c_expected = U256::from_hex_unchecked(
2625            "7c380f8b97f94efa672ece3cfefcec5094f8c5abdac45c372e02038508bf81d5",
2626        );
2627        let (c, overflow) = U256::add(&a, &b);
2628        assert_eq!(c, c_expected);
2629        assert!(overflow);
2630    }
2631
2632    #[test]
2633    fn double_256_bit_integer_12_with_overflow() {
2634        let a = U256::from_hex_unchecked(
2635            "b07bc844363dd56467d9ebdd5929e9bb34a8e2577db77df6cf8f2ac45bd3d0bc",
2636        );
2637        let b = U256::from_hex_unchecked(
2638            "cbbc474761bb7995ff54e25fa5d30295604fe3545d0cde405e72d8c0acebb119",
2639        );
2640        assert_eq!(U256::double(&a), U256::add(&a, &a));
2641        assert_eq!(U256::double(&b), U256::add(&b, &b));
2642    }
2643
2644    #[test]
2645    fn sub_two_256_bit_integers_1() {
2646        let a = U256::from_u64(2);
2647        let b = U256::from_u64(5);
2648        let c = U256::from_u64(7);
2649        assert_eq!(c - a, b);
2650    }
2651
2652    #[test]
2653    fn sub_two_256_bit_integers_2() {
2654        let a = U256::from_u64(334);
2655        let b = U256::from_u64(666);
2656        let c = U256::from_u64(1000);
2657        assert_eq!(c - a, b);
2658    }
2659
2660    #[test]
2661    fn sub_two_256_bit_integers_3() {
2662        let a = U256::from_hex_unchecked("ffffffffffffffff");
2663        let b = U256::from_hex_unchecked("1");
2664        let c = U256::from_hex_unchecked("10000000000000000");
2665        assert_eq!(c - a, b);
2666    }
2667
2668    #[test]
2669    fn sub_two_256_bit_integers_4() {
2670        let a = U256::from_hex_unchecked("b58e1e0b66");
2671        let b = U256::from_hex_unchecked("55469d9619");
2672        let c = U256::from_hex_unchecked("10ad4bba17f");
2673        assert_eq!(c - a, b);
2674    }
2675
2676    #[test]
2677    fn sub_two_256_bit_integers_5() {
2678        let a = U256::from_hex_unchecked("e8dff25cb6160f7705221da6f");
2679        let b = U256::from_hex_unchecked("ab879169b5f80dc8a7969f0b0");
2680        let c = U256::from_hex_unchecked("1946783c66c0e1d3facb8bcb1f");
2681        assert_eq!(c - a, b);
2682    }
2683
2684    #[test]
2685    fn sub_two_256_bit_integers_6() {
2686        let a = U256::from_hex_unchecked("9adf291af3a64d59e14e7b440c850508014c551ed5");
2687        let b = U256::from_hex_unchecked("e7948474bce907f0feaf7e5d741a8cd2f6d1fb9448");
2688        let c = U256::from_hex_unchecked("18273ad8fb08f554adffdf9a1809f91daf81e50b31d");
2689        assert_eq!(c - a, b);
2690    }
2691
2692    #[test]
2693    fn sub_two_256_bit_integers_7() {
2694        let a = U256::from_hex_unchecked(
2695            "9b4000dccf01a010e196154a1b998408f949d734389626ba97cb3331ee87e01d",
2696        );
2697        let b = U256::from_hex_unchecked(
2698            "5d26ae1b34c78bdf4cefb2b0b553473f887bc0f1ac03d36861c2e75e01656cbc",
2699        );
2700        let c = U256::from_hex_unchecked(
2701            "f866aef803c92bf02e85c7fad0eccb4881c59825e499fa22f98e1a8fefed4cd9",
2702        );
2703        assert_eq!(c - a, b);
2704    }
2705
2706    #[test]
2707    fn sub_two_256_bit_integers_8() {
2708        let a = U256::from_hex_unchecked(
2709            "07df9c74fa9d5aafa74a87dbbf93215659d8a3e1706d4b06de9512284802580e",
2710        );
2711        let b = U256::from_hex_unchecked(
2712            "d515e54973f0643a6a9957579c1f84020a6a91d5d5f27b75401c7538d2c9ea9d",
2713        );
2714        let c = U256::from_hex_unchecked(
2715            "dcf581be6e8dbeea11e3df335bb2a558644335b7465fc67c1eb187611acc42ab",
2716        );
2717        assert_eq!(c - a, b);
2718    }
2719
2720    #[test]
2721    fn sub_two_256_bit_integers_9() {
2722        let a = U256::from_hex_unchecked(
2723            "92977527a0f8ba00d18c1b2f1900d965d4a70e5f5f54468ffb2d4d41519385f2",
2724        );
2725        let b = U256::from_hex_unchecked(
2726            "46facf9953a9494822bf18836ffd7e55c48b30aa81e17fa1ace0b473015307e4",
2727        );
2728        let c = U256::from_hex_unchecked(
2729            "d99244c0f4a20348f44b33b288fe57bb99323f09e135c631a80e01b452e68dd6",
2730        );
2731        assert_eq!(c - a, b);
2732    }
2733
2734    #[test]
2735    fn sub_two_256_bit_integers_11_without_overflow() {
2736        let a = U256::from_u64(334);
2737        let b_expected = U256::from_u64(666);
2738        let c = U256::from_u64(1000);
2739        let (b, overflow) = U256::sub(&c, &a);
2740        assert!(!overflow);
2741        assert_eq!(b_expected, b);
2742    }
2743
2744    #[test]
2745    fn sub_two_256_bit_integers_11_with_overflow() {
2746        let a = U256::from_u64(334);
2747        let b_expected = U256::from_hex_unchecked(
2748            "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd66",
2749        );
2750        let c = U256::from_u64(1000);
2751        let (b, overflow) = U256::sub(&a, &c);
2752        assert!(overflow);
2753        assert_eq!(b_expected, b);
2754    }
2755
2756    #[test]
2757    fn const_le_works() {
2758        let a = U256::from_u64(334);
2759        let b = U256::from_u128(333);
2760        assert!(U256::const_le(&b, &a));
2761        assert!(U256::const_le(&a, &a));
2762        assert!(!U256::const_le(&a, &b));
2763    }
2764
2765    #[test]
2766    fn partial_order_works() {
2767        assert!(U256::from_u64(10) <= U256::from_u64(10));
2768        assert!(U256::from_u64(1) < U256::from_u64(2));
2769        assert!(U256::from_u64(2) >= U256::from_u64(1));
2770
2771        assert!(U256::from_u64(10) >= U256::from_u64(10));
2772        assert!(U256::from_u64(2) > U256::from_u64(1));
2773        assert!(U256::from_u64(1) <= U256::from_u64(2));
2774
2775        let a = U256::from_hex_unchecked(
2776            "5d4a70e5f5f54468ffb2d4d41519385f24b078a0e7d0281d5ad0c36724dc4233",
2777        );
2778        let c = U256::from_hex_unchecked(
2779            "b99323f09e135c631a80e01b452e68dd6ad3315e5776b713af4a2d7f5f9a2a75",
2780        );
2781
2782        assert!(&a <= &a);
2783        assert!(&a >= &a);
2784        assert!(&a >= &a);
2785        assert!(&a <= &a);
2786        assert!(&a < &(&a + U256::from_u64(1)));
2787        assert!(&a <= &(&a + U256::from_u64(1)));
2788        assert!(&a + U256::from_u64(1) > a);
2789        assert!((&a + U256::from_u64(1) >= a));
2790        assert!(&a <= &c);
2791        assert!(&a < &c);
2792        assert!(&a < &c);
2793        assert!(&a <= &c);
2794        assert!(&c > &a);
2795        assert!(&c >= &a);
2796        assert!(&c >= &a);
2797        assert!(&c > &a);
2798        assert!(a < c);
2799    }
2800
2801    #[test]
2802    fn mul_two_256_bit_integers_works_1() {
2803        let a = U256::from_u64(3);
2804        let b = U256::from_u64(8);
2805        let c = U256::from_u64(3 * 8);
2806        assert_eq!(a * b, c);
2807    }
2808
2809    #[test]
2810    fn mul_two_256_bit_integers_works_2() {
2811        let a = U256::from_hex_unchecked("6131d99f840b3b0");
2812        let b = U256::from_hex_unchecked("6f5c466db398f43");
2813        let c = U256::from_hex_unchecked("2a47a603a77f871dfbb937af7e5710");
2814        assert_eq!(a * b, c);
2815    }
2816
2817    #[test]
2818    fn mul_two_256_bit_integers_works_3() {
2819        let a = U256::from_hex_unchecked("84a6add5db9e095b2e0f6b40eff8ee");
2820        let b = U256::from_hex_unchecked("2347db918f725461bec2d5c57");
2821        let c = U256::from_hex_unchecked("124805c476c9462adc0df6c88495d4253f5c38033afc18d78d920e2");
2822        assert_eq!(a * b, c);
2823    }
2824
2825    #[test]
2826    fn mul_two_256_bit_integers_works_4() {
2827        let a = U256::from_hex_unchecked("15bf61fcf53a3f0ae1e8e555d");
2828        let b = U256::from_hex_unchecked("cbbc474761bb7995ff54e25fa5d5d0cde405e9f");
2829        let c_expected = U256::from_hex_unchecked(
2830            "114ec14db0c80d30b7dcb9c45948ef04cc149e612cb544f447b146553aff2ac3",
2831        );
2832        assert_eq!(a * b, c_expected);
2833    }
2834
2835    #[test]
2836    fn mul_two_256_bit_integers_works_5_hi_lo() {
2837        let a = U256::from_hex_unchecked(
2838            "8e2d939b602a50911232731d04fe6f40c05f97da0602307099fb991f9b414e2d",
2839        );
2840        let b = U256::from_hex_unchecked(
2841            "7f3ad1611ab58212f92a2484e9560935b9ac4615fe61cfed1a4861e193a74d20",
2842        );
2843        let hi_expected = U256::from_hex_unchecked(
2844            "46A946D6A984FE6507DE6B8D1354256D7A7BAE4283404733BDC876A264BCE5EE",
2845        );
2846        let lo_expected = U256::from_hex_unchecked(
2847            "43F24263F10930EBE3EA0307466C19B13B9C7DBA6B3F7604B7F32FB0E3084EA0",
2848        );
2849        let (hi, lo) = U256::mul(&a, &b);
2850        assert_eq!(hi, hi_expected);
2851        assert_eq!(lo, lo_expected);
2852    }
2853
2854    #[test]
2855    fn shift_left_on_256_bit_integer_works_1() {
2856        let a = U256::from_hex_unchecked("1");
2857        let b = U256::from_hex_unchecked("10");
2858        assert_eq!(a << 4, b);
2859    }
2860
2861    #[test]
2862    fn shift_left_on_256_bit_integer_works_2() {
2863        let a = U256::from_u64(1);
2864        let b = U256::from_u128(1_u128 << 64);
2865        assert_eq!(a << 64, b);
2866    }
2867
2868    #[test]
2869    fn shift_left_on_256_bit_integer_works_3() {
2870        let a = U256::from_hex_unchecked("10");
2871        let b = U256::from_hex_unchecked("1000");
2872        assert_eq!(&a << 8, b);
2873    }
2874
2875    #[test]
2876    fn shift_left_on_256_bit_integer_works_4() {
2877        let a = U256::from_hex_unchecked("e45542992b6844553f3cb1c5ac33e7fa5");
2878        let b = U256::from_hex_unchecked("391550a64ada11154fcf2c716b0cf9fe940");
2879        assert_eq!(a << 6, b);
2880    }
2881
2882    #[test]
2883    fn shift_left_on_256_bit_integer_works_5() {
2884        let a = U256::from_hex_unchecked("a8390aa99bead76bc0093b1bc1a8101f5ce");
2885        let b = U256::from_hex_unchecked(
2886            "72155337d5aed7801276378350203eb9c0000000000000000000000000000000",
2887        );
2888        assert_eq!(&a << 125, b);
2889    }
2890
2891    #[test]
2892    fn shift_left_on_256_bit_integer_works_6() {
2893        let a = U256::from_hex_unchecked("2ed786ab132f0b5b0cacd385dd51de3a");
2894        let b = U256::from_hex_unchecked(
2895            "2ed786ab132f0b5b0cacd385dd51de3a00000000000000000000000000000000",
2896        );
2897        assert_eq!(&a << (64 * 2), b);
2898    }
2899
2900    #[test]
2901    fn shift_left_on_256_bit_integer_works_7() {
2902        let a = U256::from_hex_unchecked("90823e0bd707f");
2903        let b = U256::from_hex_unchecked(
2904            "90823e0bd707f000000000000000000000000000000000000000000000000",
2905        );
2906        assert_eq!(&a << (64 * 3), b);
2907    }
2908
2909    #[test]
2910    fn shift_right_on_256_bit_integer_works_1() {
2911        let a = U256::from_hex_unchecked("1");
2912        let b = U256::from_hex_unchecked("10");
2913        assert_eq!(b >> 4, a);
2914    }
2915
2916    #[test]
2917    fn shift_right_on_256_bit_integer_works_2() {
2918        let a = U256::from_hex_unchecked("10");
2919        let b = U256::from_hex_unchecked("1000");
2920        assert_eq!(&b >> 8, a);
2921    }
2922
2923    #[test]
2924    fn shift_right_on_256_bit_integer_works_3() {
2925        let a = U256::from_hex_unchecked("e45542992b6844553f3cb1c5ac33e7fa5");
2926        let b = U256::from_hex_unchecked("391550a64ada11154fcf2c716b0cf9fe940");
2927        assert_eq!(b >> 6, a);
2928    }
2929
2930    #[test]
2931    fn shift_right_on_256_bit_integer_works_4() {
2932        let a = U256::from_hex_unchecked("390aa99bead76bc0093b1bc1a8101f5ce");
2933        let b = U256::from_hex_unchecked(
2934            "72155337d5aed7801276378350203eb9c0000000000000000000000000000000",
2935        );
2936        assert_eq!(&b >> 125, a);
2937    }
2938
2939    #[test]
2940    fn shift_right_on_256_bit_integer_works_5() {
2941        let a = U256::from_hex_unchecked(
2942            "ba6ab46f9a9a2f20e4061b67ce4d8c3da98091cf990d7b14ef47ffe27370abbd",
2943        );
2944        let b = U256::from_hex_unchecked("174d568df35345e41c80c36cf9c");
2945        assert_eq!(a >> 151, b);
2946    }
2947
2948    #[test]
2949    fn shift_right_on_256_bit_integer_works_6() {
2950        let a = U256::from_hex_unchecked(
2951            "076c075d2f65e39b9ecdde8bf6f8c94241962ce0f557b7739673200c777152eb",
2952        );
2953        let b = U256::from_hex_unchecked("ed80eba5ecbc7373d9bbd17edf19284832c59c");
2954        assert_eq!(&a >> 99, b);
2955    }
2956
2957    #[test]
2958    fn shift_right_on_256_bit_integer_works_7() {
2959        let a = U256::from_hex_unchecked(
2960            "6a9ce35d8940a5ebd29604ce9a182ade76f03f7e9965760b84a8cfd1d3dd2e61",
2961        );
2962        let b = U256::from_hex_unchecked("6a9ce35d8940a5eb");
2963        assert_eq!(&a >> (64 * 3), b);
2964    }
2965
2966    #[test]
2967    fn shift_right_on_256_bit_integer_works_8() {
2968        let a = U256::from_hex_unchecked(
2969            "5322c128ec84081b6c376c108ebd7fd36bbd44f71ee5e6ad6bcb3dd1c5265bd7",
2970        );
2971        let b = U256::from_hex_unchecked("5322c128ec84081b6c376c108ebd7fd3");
2972        assert_eq!(&a >> (64 * 2), b);
2973    }
2974
2975    #[test]
2976    #[cfg(feature = "alloc")]
2977    fn to_be_bytes_works() {
2978        let number = U256::from_u64(1);
2979        let expected_bytes = [
2980            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2981            0, 0, 1,
2982        ];
2983
2984        assert_eq!(number.to_bytes_be(), expected_bytes);
2985    }
2986
2987    #[test]
2988    #[cfg(feature = "alloc")]
2989    fn to_le_bytes_works() {
2990        let number = U256::from_u64(1);
2991        let expected_bytes = [
2992            1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2993            0, 0, 0,
2994        ];
2995
2996        assert_eq!(number.to_bytes_le(), expected_bytes);
2997    }
2998
2999    #[test]
3000    fn from_bytes_be_works() {
3001        let bytes = [
3002            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3003            0, 0, 1,
3004        ];
3005        let expected_number = U256::from_u64(1);
3006        assert_eq!(U256::from_bytes_be(&bytes).unwrap(), expected_number);
3007    }
3008
3009    #[test]
3010    fn from_bytes_le_works() {
3011        let bytes = [
3012            1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3013            0, 0, 0,
3014        ];
3015        let expected_number = U256::from_u64(1);
3016        assert_eq!(U256::from_bytes_le(&bytes).unwrap(), expected_number);
3017    }
3018
3019    #[test]
3020    fn shr_inplace_works_1() {
3021        let mut n = UnsignedInteger::<3>::from(4u64);
3022        n >>= 1;
3023
3024        assert_eq!(n, UnsignedInteger::<3>::from(2u64));
3025    }
3026
3027    #[test]
3028    fn shr_inplace_on_256_bit_integer_works_1() {
3029        let a = U256::from_hex_unchecked("e45542992b6844553f3cb1c5ac33e7fa5");
3030        let mut b = U256::from_hex_unchecked("391550a64ada11154fcf2c716b0cf9fe940");
3031        b >>= 6;
3032        assert_eq!(a, b);
3033    }
3034
3035    #[test]
3036    fn shr_inplace_on_254_bit_integer_works_2() {
3037        let a = U256::from_hex_unchecked("390aa99bead76bc0093b1bc1a8101f5ce");
3038        let mut b = U256::from_hex_unchecked(
3039            "72155337d5aed7801276378350203eb9c0000000000000000000000000000000",
3040        );
3041        b >>= 125;
3042        assert_eq!(a, b);
3043    }
3044
3045    #[test]
3046    fn shr_inplace_on_256_bit_integer_works_3() {
3047        let a = U256::from_hex_unchecked("2ed786ab132f0b5b0cacd385dd51de3a");
3048        let mut b = U256::from_hex_unchecked(
3049            "2ed786ab132f0b5b0cacd385dd51de3a00000000000000000000000000000000",
3050        );
3051        b >>= 64 * 2;
3052        assert_eq!(a, b);
3053    }
3054
3055    #[test]
3056    fn shr_inplace_on_256_bit_integer_works_4() {
3057        let a = U256::from_hex_unchecked("90823e0bd707f");
3058        let mut b = U256::from_hex_unchecked(
3059            "90823e0bd707f000000000000000000000000000000000000000000000000",
3060        );
3061        b >>= 64 * 3;
3062        assert_eq!(a, b);
3063    }
3064
3065    #[test]
3066    fn shr_inplace_on_256_bit_integer_works_5() {
3067        let a = U256::from_hex_unchecked("24208f");
3068        let mut b = U256::from_hex_unchecked(
3069            "90823e0bd707f000000000000000000000000000000000000000000000000",
3070        );
3071        b >>= 222;
3072        assert_eq!(a, b);
3073    }
3074
3075    #[test]
3076    fn multiplying_and_dividing_for_number_is_number_with_remainder_0() {
3077        let a = U256::from_u128(12678920202929299999999999282828);
3078        let b = U256::from_u128(9000000000000);
3079        assert_eq!((a * b).div_rem(&b), (a, U256::from_u64(0)));
3080    }
3081
3082    #[test]
3083    fn unsigned_int_8_div_rem_3_is_2_2() {
3084        let a: UnsignedInteger<4> = U256::from_u64(8);
3085        let b = U256::from_u64(3);
3086        assert_eq!(a.div_rem(&b), (U256::from_u64(2), U256::from_u64(2)));
3087    }
3088
3089    #[test]
3090    fn unsigned_int_500721_div_rem_5_is_100144_1() {
3091        let a = U256::from_u64(500721);
3092        let b = U256::from_u64(5);
3093        assert_eq!(a.div_rem(&b), (U256::from_u64(100144), U256::from_u64(1)));
3094    }
3095
3096    #[test]
3097    fn div_rem_works_with_big_numbers() {
3098        let a = U256::from_u128(4758402376589578934275873583589345);
3099        let b = U256::from_u128(43950384634609);
3100        assert_eq!(
3101            a.div_rem(&b),
3102            (
3103                U256::from_u128(108267593472721187331),
3104                U256::from_u128(12368508650766)
3105            )
3106        );
3107    }
3108
3109    #[cfg(feature = "std")]
3110    #[test]
3111    fn to_hex_test() {
3112        let a = U256::from_hex_unchecked("390aa99bead76bc0093b1bc1a8101f5ce");
3113        assert_eq!(U256::to_hex(&a), "390AA99BEAD76BC0093B1BC1A8101F5CE")
3114    }
3115}