cnfy-uint 0.2.3

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

/// Creates a [`U384`] from a [`U256`] value by embedding it in the lower
/// four limbs (`[2..5]`), with the upper two limbs set to zero.
///
/// # Examples
///
/// ```
/// use cnfy_uint::u256::U256;
/// use cnfy_uint::u384::U384;
///
/// let v = U256::from_be_limbs([1, 2, 3, 4]);
/// let wide = U384::from(v);
/// assert_eq!(wide, U384::from_be_limbs([0, 0, 1, 2, 3, 4]));
/// ```
impl From<U256> for U384 {
    #[inline]
    fn from(v: U256) -> Self {
        U384([v.0[0], v.0[1], v.0[2], v.0[3], 0, 0])
    }
}

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

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

    /// One U256 produces one U384.
    #[test]
    fn one() {
        assert_eq!(U384::from(U256::from_be_limbs([0, 0, 0, 1])), U384::ONE);
    }

    /// All limbs are placed in the correct positions.
    #[test]
    fn all_limbs() {
        let v = U256::from_be_limbs([0xA, 0xB, 0xC, 0xD]);
        let wide = U384::from(v);
        assert_eq!(wide.to_be_limbs(), [0, 0, 0xA, 0xB, 0xC, 0xD]);
    }

    /// U256::MAX maps to lower 256 bits of U384.
    #[test]
    fn max_u256() {
        let wide = U384::from(U256::from_be_limbs([u64::MAX; 4]));
        assert_eq!(
            wide,
            U384::from_be_limbs([0, 0, u64::MAX, u64::MAX, u64::MAX, u64::MAX]),
        );
    }
}