cnfy-uint 0.2.3

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

/// Computes the bitwise complement of a 384-bit integer, flipping
/// every bit.
///
/// Applied independently to each of the six `u64` limbs via the
/// `!` operator.
///
/// # Examples
///
/// ```
/// use cnfy_uint::u384::U384;
///
/// assert_eq!(!U384::ZERO, U384::MAX);
/// assert_eq!(!U384::MAX, U384::ZERO);
/// ```
impl Not for U384 {
    type Output = U384;

    #[inline]
    fn not(self) -> U384 {
        U384([
            !self.0[0],
            !self.0[1],
            !self.0[2],
            !self.0[3],
            !self.0[4],
            !self.0[5],
        ])
    }
}

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

    /// NOT zero is MAX.
    #[test]
    fn not_zero() {
        assert_eq!(!U384::ZERO, U384::MAX);
    }

    /// NOT MAX is zero.
    #[test]
    fn not_max() {
        assert_eq!(!U384::MAX, U384::ZERO);
    }

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

    /// NOT flips specific bits.
    #[test]
    fn flips_bits() {
        let a = U384::from_be_limbs([0, 0, 0, 0, 0, 0xFF]);
        let expected = U384::from_be_limbs([
            u64::MAX,
            u64::MAX,
            u64::MAX,
            u64::MAX,
            u64::MAX,
            u64::MAX ^ 0xFF,
        ]);
        assert_eq!(!a, expected);
    }
}