Skip to main content

crypto_bigint/uint/boxed/
bit_not.rs

1//! [`BoxedUint`] bitwise NOT operations.
2
3use super::BoxedUint;
4use crate::{Limb, Wrapping};
5use core::ops::Not;
6
7impl BoxedUint {
8    /// Computes bitwise `!a`.
9    #[must_use]
10    pub fn not(&self) -> Self {
11        let mut limbs = vec![Limb::ZERO; self.nlimbs()];
12
13        for i in 0..self.nlimbs() {
14            limbs[i] = self.limbs[i].not();
15        }
16
17        limbs.into()
18    }
19}
20
21impl Not for BoxedUint {
22    type Output = Self;
23
24    fn not(self) -> Self {
25        BoxedUint::not(&self)
26    }
27}
28
29impl Not for Wrapping<BoxedUint> {
30    type Output = Self;
31
32    fn not(self) -> <Self as Not>::Output {
33        Wrapping(self.0.not())
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use crate::BoxedUint;
40
41    #[test]
42    fn bitnot_ok() {
43        assert_eq!(
44            BoxedUint::zero_with_precision(128).not(),
45            BoxedUint::max(128)
46        );
47        assert_eq!(
48            BoxedUint::max(128).not(),
49            BoxedUint::zero_with_precision(128)
50        );
51    }
52}