cnfy-uint 0.2.3

Zero-dependency 256-bit unsigned integer arithmetic for cryptographic applications
Documentation
//! Construction of a [`U256`] from four little-endian `u64` limbs.
use super::U256;

impl U256 {
    /// Creates a new [`U256`] from a four-element array of `u64` limbs in
    /// little-endian order: `[w0, w1, w2, w3]` where `w0` is the least
    /// significant and `w3` is the most significant.
    ///
    /// Since the internal layout is already little-endian, the array is
    /// stored directly without reversal. This is the inverse of
    /// [`U256::to_le_limbs`].
    ///
    /// # Examples
    ///
    /// ```
    /// use cnfy_uint::u256::U256;
    ///
    /// let value = U256::from_le_limbs([1, 0, 0, 0]);
    /// assert_eq!(value, U256::from_be_limbs([0, 0, 0, 1]));
    /// ```
    #[inline]
    pub const fn from_le_limbs(value: [u64; 4]) -> Self {
        Self(value)
    }
}

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

    /// Constructing from four zeros yields a zero value.
    #[test]
    fn zero() {
        assert_eq!(U256::from_le_limbs([0; 4]), U256::ZERO);
    }

    /// LE limb order is the reverse of BE limb order.
    #[test]
    fn reverse_of_be() {
        let le = U256::from_le_limbs([4, 3, 2, 1]);
        let be = U256::from_be_limbs([1, 2, 3, 4]);
        assert_eq!(le, be);
    }

    /// Maximum value fills all four limbs.
    #[test]
    fn max_value() {
        assert_eq!(U256::from_le_limbs([u64::MAX; 4]), U256::MAX);
    }

    /// Round-trip with to_le_limbs.
    #[test]
    fn round_trip() {
        let limbs = [0xDEAD, 0xBEEF, 0xCAFE, 0xBABE];
        assert_eq!(U256::from_le_limbs(limbs).to_le_limbs(), limbs);
    }
}