cnfy-uint 0.2.3

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

/// Creates a [`U256`] from a `u64` value, placing it in the least
/// significant limb with the upper three limbs set to zero.
///
/// This provides ergonomic construction of small values without
/// needing the full `from_be_limbs([0, 0, 0, n])` syntax.
///
/// # Examples
///
/// ```
/// use cnfy_uint::u256::U256;
///
/// let v = U256::from(42u64);
/// assert_eq!(v, U256::from_be_limbs([0, 0, 0, 42]));
/// ```
impl From<u64> for U256 {
    #[inline]
    fn from(value: u64) -> Self {
        Self::from_be_limbs([0, 0, 0, value])
    }
}

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

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

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

    /// Converting u64::MAX fills only the lowest limb.
    #[test]
    fn max_u64() {
        assert_eq!(
            U256::from(u64::MAX),
            U256::from_be_limbs([0, 0, 0, u64::MAX]),
        );
    }

    /// Arbitrary value round-trips through to_be_limbs.
    #[test]
    fn round_trip() {
        let v = U256::from(0xDEADBEEFu64);
        assert_eq!(v.to_be_limbs(), [0, 0, 0, 0xDEADBEEF]);
    }
}