cnfy-uint 0.2.3

Zero-dependency 256-bit unsigned integer arithmetic for cryptographic applications
Documentation
//! Bit-granularity right shift with zero-fill.
use super::U384;

impl U384 {
    /// Shifts the value right by `n` bits, filling the vacated high bits
    /// with zeros.
    ///
    /// For shifts of 384 or more, the result is zero. For a shift of 0,
    /// the value is returned unchanged.
    ///
    /// # Examples
    ///
    /// ```
    /// use cnfy_uint::u384::U384;
    ///
    /// let a = U384::from_be_limbs([0, 0, 0, 0, 0, 8]);
    /// assert_eq!(a.shr_bits(3), U384::from_be_limbs([0, 0, 0, 0, 0, 1]));
    ///
    /// let b = U384::from_be_limbs([1, 0, 0, 0, 0, 0]);
    /// assert_eq!(b.shr_bits(320), U384::from_be_limbs([0, 0, 0, 0, 0, 1]));
    /// ```
    #[inline]
    pub const fn shr_bits(&self, n: u32) -> U384 {
        if n >= 384 {
            return U384([0, 0, 0, 0, 0, 0]);
        }
        if n == 0 {
            return *self;
        }

        let limb_shift = (n / 64) as usize;
        let bit_shift = n % 64;

        // Shift whole limbs (little-endian: shifting right moves data
        // toward lower indices)
        let mut shifted = [0u64; 6];
        let mut i = limb_shift;
        while i < 6 {
            shifted[i - limb_shift] = self.0[i];
            i += 1;
        }

        if bit_shift == 0 {
            return U384(shifted);
        }

        // Shift bits within limbs, carrying from MSB to LSB
        let mut result = [0u64; 6];
        result[5] = shifted[5] >> bit_shift;
        let mut j = 5;
        while j > 0 {
            j -= 1;
            result[j] = (shifted[j] >> bit_shift) | (shifted[j + 1] << (64 - bit_shift));
        }

        U384(result)
    }
}

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

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

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

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

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

    /// Shift by 320 moves from MSB limb to LSB.
    #[test]
    fn five_limbs() {
        let a = U384::from_be_limbs([0x42, 0, 0, 0, 0, 0]);
        assert_eq!(a.shr_bits(320), U384::from_be_limbs([0, 0, 0, 0, 0, 0x42]));
    }

    /// Cross-limb bit carry works.
    #[test]
    fn cross_limb_carry() {
        let a = U384::from_be_limbs([0, 0, 0, 0, 1, 0]);
        assert_eq!(a.shr_bits(1), U384::from_be_limbs([0, 0, 0, 0, 0, 1 << 63]));
    }

    /// Combined limb and bit shift.
    #[test]
    fn combined_shift() {
        let a = U384::from_be_limbs([0, 0, 0, 0, 0x100, 0]);
        assert_eq!(a.shr_bits(68), U384::from_be_limbs([0, 0, 0, 0, 0, 0x10]));
    }

    /// Shifting zero always produces zero.
    #[test]
    fn shift_zero() {
        assert_eq!(U384::ZERO.shr_bits(42), U384::ZERO);
    }

    /// Shifting MAX right by 383 leaves 1.
    #[test]
    fn max_shift_383() {
        assert_eq!(U384::MAX.shr_bits(383), U384::ONE);
    }

    /// Round-trip with shl_bits for small shifts.
    #[test]
    fn round_trip() {
        let a = U384::from_be_limbs([0, 0, 0x1234, 0x5678, 0x9ABC, 0]);
        assert_eq!(a.shl_bits(17).shr_bits(17), a);
    }
}