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    pub fn not(&self) -> Self {
10        let mut limbs = vec![Limb::ZERO; self.nlimbs()];
11
12        for i in 0..self.nlimbs() {
13            limbs[i] = self.limbs[i].not();
14        }
15
16        limbs.into()
17    }
18}
19
20impl Not for BoxedUint {
21    type Output = Self;
22
23    fn not(self) -> Self {
24        BoxedUint::not(&self)
25    }
26}
27
28impl Not for Wrapping<BoxedUint> {
29    type Output = Self;
30
31    fn not(self) -> <Self as Not>::Output {
32        Wrapping(self.0.not())
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use crate::BoxedUint;
39
40    #[test]
41    fn bitnot_ok() {
42        assert_eq!(
43            BoxedUint::zero_with_precision(128).not(),
44            BoxedUint::max(128)
45        );
46        assert_eq!(
47            BoxedUint::max(128).not(),
48            BoxedUint::zero_with_precision(128)
49        );
50    }
51}