1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//! Support for the [`num-bigint`](https://crates.io/crates/num-bigint) crate.
#![cfg(feature = "num-bigint")]
#![cfg_attr(has_doc_cfg, doc(cfg(feature = "num-bigint")))]

use crate::{from::ToUintError, Uint};
use num_bigint::{BigInt, BigUint, Sign};

impl<const BITS: usize, const LIMBS: usize> TryFrom<BigUint> for Uint<BITS, LIMBS> {
    type Error = ToUintError;

    #[allow(clippy::only_used_in_recursion)] // False positive
    fn try_from(value: BigUint) -> Result<Self, Self::Error> {
        Self::try_from(&value)
    }
}

impl<const BITS: usize, const LIMBS: usize> TryFrom<&BigUint> for Uint<BITS, LIMBS> {
    type Error = ToUintError;

    fn try_from(value: &BigUint) -> Result<Self, Self::Error> {
        Self::checked_from_limbs_slice(value.to_u64_digits().as_slice())
            .ok_or(ToUintError::ValueTooLarge(BITS))
    }
}

impl<const BITS: usize, const LIMBS: usize> From<Uint<BITS, LIMBS>> for BigUint {
    fn from(value: Uint<BITS, LIMBS>) -> Self {
        Self::from(&value)
    }
}

impl<const BITS: usize, const LIMBS: usize> From<&Uint<BITS, LIMBS>> for BigUint {
    fn from(value: &Uint<BITS, LIMBS>) -> Self {
        Self::from_bytes_le(&value.as_le_bytes())
    }
}

impl<const BITS: usize, const LIMBS: usize> TryFrom<BigInt> for Uint<BITS, LIMBS> {
    type Error = ToUintError;

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

impl<const BITS: usize, const LIMBS: usize> TryFrom<&BigInt> for Uint<BITS, LIMBS> {
    type Error = ToUintError;

    fn try_from(value: &BigInt) -> Result<Self, Self::Error> {
        let (sign, digits) = value.to_u64_digits();
        if sign == Sign::Minus {
            return Err(ToUintError::ValueNegative(BITS));
        }
        Self::checked_from_limbs_slice(digits.as_slice()).ok_or(ToUintError::ValueTooLarge(BITS))
    }
}

impl<const BITS: usize, const LIMBS: usize> From<Uint<BITS, LIMBS>> for BigInt {
    fn from(value: Uint<BITS, LIMBS>) -> Self {
        Self::from(&value)
    }
}

impl<const BITS: usize, const LIMBS: usize> From<&Uint<BITS, LIMBS>> for BigInt {
    fn from(value: &Uint<BITS, LIMBS>) -> Self {
        Self::from_bytes_le(Sign::Plus, &value.as_le_bytes())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{const_for, nlimbs};
    use proptest::proptest;

    #[test]
    #[allow(clippy::unreadable_literal)]
    fn test_roundtrip_biguint() {
        const_for!(BITS in SIZES {
            const LIMBS: usize = nlimbs(BITS);
            type U = Uint<BITS, LIMBS>;
            proptest!(|(value: U)| {
                let big: BigUint = value.into();
                let back: U = big.try_into().unwrap();
                assert_eq!(back, value);
            });
        });
    }

    #[test]
    #[allow(clippy::unreadable_literal)]
    fn test_roundtrip_bigint() {
        const_for!(BITS in SIZES {
            const LIMBS: usize = nlimbs(BITS);
            type U = Uint<BITS, LIMBS>;
            proptest!(|(value: U)| {
                let big: BigInt = value.into();
                let back: U = big.try_into().unwrap();
                assert_eq!(back, value);
            });
        });
    }
}