cnfy-uint 0.2.3

Zero-dependency 256-bit unsigned integer arithmetic for cryptographic applications
Documentation
//! Conversion from `u128` into [`U512`].
use super::U512;

/// Creates a [`U512`] from a `u128` value, placing the high 64 bits in
/// limb `w6` and the low 64 bits in limb `w7`, with the upper six limbs
/// set to zero.
///
/// # Examples
///
/// ```
/// use cnfy_uint::u512::U512;
///
/// let v = U512::from(0x1_0000_0000_0000_0000u128);
/// assert_eq!(v, U512::from_be_limbs([0, 0, 0, 0, 0, 0, 1, 0]));
/// ```
impl From<u128> for U512 {
    #[inline]
    fn from(value: u128) -> Self {
        Self::from_be_limbs([0, 0, 0, 0, 0, 0, (value >> 64) as u64, value as u64])
    }
}

#[cfg(test)]
mod ai_tests {
    use super::*;

    /// Converting zero produces U512::ZERO.
    #[test]
    fn zero() {
        assert_eq!(U512::from(0u128), U512::ZERO);
    }

    /// Converting one produces U512::ONE.
    #[test]
    fn one() {
        assert_eq!(U512::from(1u128), U512::ONE);
    }

    /// Converting u128::MAX fills the lower two limbs.
    #[test]
    fn max_u128() {
        assert_eq!(
            U512::from(u128::MAX),
            U512::from_be_limbs([0, 0, 0, 0, 0, 0, u64::MAX, u64::MAX]),
        );
    }

    /// Value spanning both u64 halves of the u128.
    #[test]
    fn cross_limb() {
        let v = U512::from(((5u128) << 64) | 7);
        assert_eq!(v, U512::from_be_limbs([0, 0, 0, 0, 0, 0, 5, 7]));
    }
}