Skip to main content

crypto_bigint/uint/
bit_and.rs

1//! [`Uint`] bitwise AND operations.
2
3use super::Uint;
4use crate::{BitAnd, BitAndAssign, CtOption, Limb};
5
6impl<const LIMBS: usize> Uint<LIMBS> {
7    /// Computes bitwise `a & b`.
8    #[inline(always)]
9    #[must_use]
10    pub const fn bitand(&self, rhs: &Self) -> Self {
11        let mut limbs = [Limb::ZERO; LIMBS];
12        let mut i = 0;
13
14        while i < LIMBS {
15            limbs[i] = self.limbs[i].bitand(rhs.limbs[i]);
16            i += 1;
17        }
18
19        Self { limbs }
20    }
21
22    /// Perform bitwise `AND` between `self` and the given [`Limb`], performing the `AND` operation
23    /// on every limb of `self`.
24    #[must_use]
25    pub const fn bitand_limb(&self, rhs: Limb) -> Self {
26        let mut limbs = [Limb::ZERO; LIMBS];
27        let mut i = 0;
28
29        while i < LIMBS {
30            limbs[i] = self.limbs[i].bitand(rhs);
31            i += 1;
32        }
33
34        Self { limbs }
35    }
36
37    /// Perform wrapping bitwise `AND`.
38    ///
39    /// There's no way wrapping could ever happen.
40    /// This function exists so that all operations are accounted for in the wrapping operations
41    #[must_use]
42    pub const fn wrapping_and(&self, rhs: &Self) -> Self {
43        self.bitand(rhs)
44    }
45
46    /// Perform checked bitwise `AND`, returning a [`CtOption`] which `is_some` always
47    #[must_use]
48    pub const fn checked_and(&self, rhs: &Self) -> CtOption<Self> {
49        CtOption::some(self.bitand(rhs))
50    }
51}
52
53impl<const LIMBS: usize> BitAnd for Uint<LIMBS> {
54    type Output = Self;
55
56    fn bitand(self, rhs: Self) -> Uint<LIMBS> {
57        self.bitand(&rhs)
58    }
59}
60
61impl<const LIMBS: usize> BitAnd<&Uint<LIMBS>> for Uint<LIMBS> {
62    type Output = Uint<LIMBS>;
63
64    #[allow(clippy::needless_borrow)]
65    fn bitand(self, rhs: &Uint<LIMBS>) -> Uint<LIMBS> {
66        (&self).bitand(rhs)
67    }
68}
69
70impl<const LIMBS: usize> BitAnd<Uint<LIMBS>> for &Uint<LIMBS> {
71    type Output = Uint<LIMBS>;
72
73    fn bitand(self, rhs: Uint<LIMBS>) -> Uint<LIMBS> {
74        self.bitand(&rhs)
75    }
76}
77
78impl<const LIMBS: usize> BitAnd<&Uint<LIMBS>> for &Uint<LIMBS> {
79    type Output = Uint<LIMBS>;
80
81    fn bitand(self, rhs: &Uint<LIMBS>) -> Uint<LIMBS> {
82        self.bitand(rhs)
83    }
84}
85
86impl<const LIMBS: usize> BitAndAssign for Uint<LIMBS> {
87    #[allow(clippy::assign_op_pattern)]
88    fn bitand_assign(&mut self, other: Self) {
89        *self = *self & other;
90    }
91}
92
93impl<const LIMBS: usize> BitAndAssign<&Uint<LIMBS>> for Uint<LIMBS> {
94    #[allow(clippy::assign_op_pattern)]
95    fn bitand_assign(&mut self, other: &Self) {
96        *self = *self & other;
97    }
98}
99
100#[cfg(test)]
101mod tests {
102    use crate::U128;
103
104    #[test]
105    fn checked_and_ok() {
106        let result = U128::ZERO.checked_and(&U128::ONE);
107        assert_eq!(result.unwrap(), U128::ZERO);
108    }
109
110    #[test]
111    fn overlapping_and_ok() {
112        let result = U128::MAX.wrapping_and(&U128::ONE);
113        assert_eq!(result, U128::ONE);
114    }
115}