cnfy-uint 0.2.3

Zero-dependency 256-bit unsigned integer arithmetic for cryptographic applications
Documentation
//! Left shift via the [`Shl`] trait.
use super::U512;
use core::ops::Shl;

/// Shifts the value left by `n` bits, zero-filling the low bits and
/// discarding bits shifted past position 511.
///
/// Delegates to [`U512::shl_bits`]. For shifts of 512 or more, the
/// result is zero.
///
/// # Examples
///
/// ```
/// use cnfy_uint::u512::U512;
///
/// let a = U512::from_be_limbs([0, 0, 0, 0, 0, 0, 0, 1]);
/// assert_eq!(a << 3, U512::from_be_limbs([0, 0, 0, 0, 0, 0, 0, 8]));
/// ```
impl Shl<u32> for U512 {
    type Output = U512;

    #[inline]
    fn shl(self, rhs: u32) -> U512 {
        self.shl_bits(rhs)
    }
}

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

    /// Shift by zero is identity.
    #[test]
    fn identity() {
        let a = U512::from_be_limbs([0x1234, 0x5678, 0x9ABC, 0xDEF0, 1, 2, 3, 4]);
        assert_eq!(a << 0, a);
    }

    /// Shift by 1 doubles the value.
    #[test]
    fn shift_one() {
        assert_eq!(
            U512::from_be_limbs([0, 0, 0, 0, 0, 0, 0, 4]) << 1,
            U512::from_be_limbs([0, 0, 0, 0, 0, 0, 0, 8]),
        );
    }

    /// Shift by 64 moves one limb.
    #[test]
    fn one_limb() {
        let a = U512::from_be_limbs([0, 0, 0, 0, 0, 0, 0, 1]);
        assert_eq!(a << 64, U512::from_be_limbs([0, 0, 0, 0, 0, 0, 1, 0]));
    }

    /// Shift by 512 or more produces zero.
    #[test]
    fn full_shift() {
        assert_eq!(U512::MAX << 512, U512::ZERO);
        assert_eq!(U512::MAX << 600, U512::ZERO);
    }

    /// Matches shl_bits behavior.
    #[test]
    fn matches_shl_bits() {
        let a = U512::from_be_limbs([0, 0, 0x1234, 0x5678, 0x9ABC, 0xDEF0, 0, 0]);
        assert_eq!(a << 17, a.shl_bits(17));
    }
}