cnfy-uint 0.2.3

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

/// Shifts the value right by `n` bits, zero-filling the high bits.
///
/// Delegates to [`U384::shr_bits`]. For shifts of 384 or more, the
/// result is zero.
///
/// # Examples
///
/// ```
/// use cnfy_uint::u384::U384;
///
/// let a = U384::from_be_limbs([0, 0, 0, 0, 0, 8]);
/// assert_eq!(a >> 3, U384::from_be_limbs([0, 0, 0, 0, 0, 1]));
/// ```
impl Shr<u32> for U384 {
    type Output = U384;

    #[inline]
    fn shr(self, rhs: u32) -> U384 {
        self.shr_bits(rhs)
    }
}

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

    /// Shift by zero is identity.
    #[test]
    fn identity() {
        let a = U384::from_be_limbs([0x1234, 0x5678, 0x9ABC, 0xDEF0, 0x1111, 0x2222]);
        assert_eq!(a >> 0, a);
    }

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

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

    /// Shift by 384 or more produces zero.
    #[test]
    fn full_shift() {
        assert_eq!(U384::MAX >> 384, U384::ZERO);
        assert_eq!(U384::MAX >> 500, U384::ZERO);
    }

    /// Matches shr_bits behavior.
    #[test]
    fn matches_shr_bits() {
        let a = U384::from_be_limbs([0, 0, 0x1234, 0x5678, 0x9ABC, 0xDEF0]);
        assert_eq!(a >> 17, a.shr_bits(17));
    }
}