cnfy-uint 0.2.3

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

impl U256 {
    /// Creates a new [`U256`] from a four-element array of `u64` limbs in
    /// big-endian order: `[w0, w1, w2, w3]` where `w0` is the most
    /// significant and `w3` is the least significant.
    ///
    /// # Examples
    ///
    /// ```
    /// use cnfy_uint::u256::U256;
    ///
    /// let value = U256::from_be_limbs([0, 0, 0, 1]);
    /// ```
    #[inline]
    pub const fn from_be_limbs(value: [u64; 4]) -> Self {
        Self([value[3], value[2], value[1], value[0]])
    }
}

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

    /// Constructing from four zeros yields a zero value.
    #[test]
    fn zero() {
        let z = U256::from_be_limbs([0, 0, 0, 0]);
        assert_eq!(z.high_u128(), 0);
        assert_eq!(z.low_u128(), 0);
    }

    /// High and low u128 values are reconstructed correctly from limbs.
    #[test]
    fn high_and_low() {
        let v = U256::from_be_limbs([0, 1, 0, 2]);
        assert_eq!(v.high_u128(), 1);
        assert_eq!(v.low_u128(), 2);
    }

    /// Maximum value fills all four limbs.
    #[test]
    fn max_value() {
        let v = U256::from_be_limbs([u64::MAX, u64::MAX, u64::MAX, u64::MAX]);
        assert_eq!(v.high_u128(), u128::MAX);
        assert_eq!(v.low_u128(), u128::MAX);
    }
}