cnfy-uint 0.2.3

Zero-dependency 256-bit unsigned integer arithmetic for cryptographic applications
Documentation
//! Branchless zero check for [`U320`].
use super::U320;

impl U320 {
    /// Returns `true` if all five limbs are zero.
    ///
    /// Uses bitwise OR to combine all limbs into a single comparison,
    /// producing a branchless check that avoids short-circuit evaluation.
    ///
    /// # Examples
    ///
    /// ```
    /// use cnfy_uint::u320::U320;
    ///
    /// assert!(U320::from_be_limbs([0, 0, 0, 0, 0]).is_zero());
    /// assert!(!U320::from_be_limbs([0, 0, 0, 0, 1]).is_zero());
    /// ```
    #[inline]
    pub const fn is_zero(&self) -> bool {
        (self.0[0] | self.0[1] | self.0[2] | self.0[3] | self.0[4]) == 0
    }
}

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

    /// Zero is zero.
    #[test]
    fn zero_is_zero() {
        assert!(U320::ZERO.is_zero());
    }

    /// One is not zero.
    #[test]
    fn one_is_not_zero() {
        assert!(!U320::ONE.is_zero());
    }

    /// Max is not zero.
    #[test]
    fn max_is_not_zero() {
        assert!(!U320::MAX.is_zero());
    }

    /// Only the MSB limb set is not zero.
    #[test]
    fn msb_only() {
        assert!(!U320::from_be_limbs([1, 0, 0, 0, 0]).is_zero());
    }

    /// Default is zero.
    #[test]
    fn default_is_zero() {
        assert!(U320::default().is_zero());
    }
}