cnfy-uint 0.2.3

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

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

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

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

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

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

    /// U320 with max limbs maps correctly.
    #[test]
    fn max_u320() {
        let wide = U384::from(U320::from_be_limbs([u64::MAX; 5]));
        assert_eq!(
            wide,
            U384::from_be_limbs([0, u64::MAX, u64::MAX, u64::MAX, u64::MAX, u64::MAX]),
        );
    }
}