cnfy-uint 0.2.3

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

/// Creates a [`U256`] from a `u32` value, placing it in the least
/// significant limb with the upper three limbs set to zero.
///
/// # Examples
///
/// ```
/// use cnfy_uint::u256::U256;
///
/// let v = U256::from(42u32);
/// assert_eq!(v, U256::from_be_limbs([0, 0, 0, 42]));
/// ```
impl From<u32> for U256 {
    #[inline]
    fn from(value: u32) -> Self {
        Self::from_be_limbs([0, 0, 0, value as u64])
    }
}

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

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

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

    /// Converting u32::MAX fills only the low 32 bits of the lowest limb.
    #[test]
    fn max_u32() {
        assert_eq!(
            U256::from(u32::MAX),
            U256::from_be_limbs([0, 0, 0, u32::MAX as u64]),
        );
    }

    /// Matches equivalent u64 conversion.
    #[test]
    fn matches_u64() {
        assert_eq!(U256::from(12345u32), U256::from(12345u64));
    }
}