crypto_bigint/uint/boxed/
bit_not.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//! [`BoxedUint`] bitwise NOT operations.

use super::BoxedUint;
use crate::{Limb, Wrapping};
use core::ops::Not;

impl BoxedUint {
    /// Computes bitwise `!a`.
    pub fn not(&self) -> Self {
        let mut limbs = vec![Limb::ZERO; self.nlimbs()];

        for i in 0..self.nlimbs() {
            limbs[i] = self.limbs[i].not();
        }

        limbs.into()
    }
}

impl Not for BoxedUint {
    type Output = Self;

    fn not(self) -> Self {
        BoxedUint::not(&self)
    }
}

impl Not for Wrapping<BoxedUint> {
    type Output = Self;

    fn not(self) -> <Self as Not>::Output {
        Wrapping(self.0.not())
    }
}

#[cfg(test)]
mod tests {
    use crate::BoxedUint;

    #[test]
    fn bitnot_ok() {
        assert_eq!(
            BoxedUint::zero_with_precision(128).not(),
            BoxedUint::max(128)
        );
        assert_eq!(
            BoxedUint::max(128).not(),
            BoxedUint::zero_with_precision(128)
        );
    }
}