cnfy-uint 0.2.3

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

impl U512 {
    /// Returns `true` if all eight 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::u512::U512;
    ///
    /// assert!(U512::from_be_limbs([0; 8]).is_zero());
    /// assert!(!U512::from_be_limbs([0, 0, 0, 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]
            | self.0[5]
            | self.0[6]
            | self.0[7])
            == 0
    }
}

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

    /// Zero is zero.
    #[test]
    fn zero_is_zero() {
        assert!(U512::from_be_limbs([0; 8]).is_zero());
    }

    /// One is not zero.
    #[test]
    fn one_is_not_zero() {
        assert!(!U512::from_be_limbs([0, 0, 0, 0, 0, 0, 0, 1]).is_zero());
    }

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

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