crypto_bigint/uint/boxed/
bit_and.rs1use super::BoxedUint;
4use crate::{BitAnd, BitAndAssign, CtOption, Limb};
5
6impl BoxedUint {
7 #[inline(always)]
9 #[must_use]
10 pub fn bitand(&self, rhs: &Self) -> Self {
11 Self::map_limbs(self, rhs, Limb::bitand)
12 }
13
14 #[must_use]
17 pub fn bitand_limb(&self, rhs: Limb) -> Self {
18 Self {
19 limbs: self.limbs.iter().map(|limb| limb.bitand(rhs)).collect(),
20 }
21 }
22
23 #[must_use]
28 pub fn wrapping_and(&self, rhs: &Self) -> Self {
29 self.bitand(rhs)
30 }
31
32 #[must_use]
34 pub fn checked_and(&self, rhs: &Self) -> CtOption<Self> {
35 CtOption::some(self.bitand(rhs))
36 }
37}
38
39impl BitAnd for BoxedUint {
40 type Output = Self;
41
42 fn bitand(self, rhs: Self) -> BoxedUint {
43 self.bitand(&rhs)
44 }
45}
46
47impl BitAnd<&BoxedUint> for BoxedUint {
48 type Output = BoxedUint;
49
50 #[allow(clippy::needless_borrow)]
51 fn bitand(self, rhs: &BoxedUint) -> BoxedUint {
52 (&self).bitand(rhs)
53 }
54}
55
56impl BitAnd<BoxedUint> for &BoxedUint {
57 type Output = BoxedUint;
58
59 fn bitand(self, rhs: BoxedUint) -> BoxedUint {
60 self.bitand(&rhs)
61 }
62}
63
64impl BitAnd<&BoxedUint> for &BoxedUint {
65 type Output = BoxedUint;
66
67 fn bitand(self, rhs: &BoxedUint) -> BoxedUint {
68 self.bitand(rhs)
69 }
70}
71
72impl BitAndAssign for BoxedUint {
73 #[allow(clippy::assign_op_pattern)]
74 fn bitand_assign(&mut self, other: Self) {
75 *self = BoxedUint::bitand(self, &other);
76 }
77}
78
79impl BitAndAssign<&BoxedUint> for BoxedUint {
80 #[allow(clippy::assign_op_pattern)]
81 fn bitand_assign(&mut self, other: &Self) {
82 *self = BoxedUint::bitand(self, other);
83 }
84}
85
86#[cfg(test)]
87mod tests {
88 use crate::BoxedUint;
89
90 #[test]
91 fn checked_and_ok() {
92 let result = BoxedUint::zero().checked_and(&BoxedUint::one());
93 assert_eq!(result.unwrap(), BoxedUint::zero());
94 }
95
96 #[test]
97 fn overlapping_and_ok() {
98 let result = BoxedUint::max(128).wrapping_and(&BoxedUint::one());
99 assert_eq!(result, BoxedUint::one());
100 }
101}