crypto_bigint/uint/boxed/
bit_and.rs1use super::BoxedUint;
4use crate::{BitAnd, BitAndAssign, CtOption, Limb, Wrapping};
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
86impl BitAnd for Wrapping<BoxedUint> {
87 type Output = Self;
88
89 fn bitand(self, rhs: Self) -> Wrapping<BoxedUint> {
90 Wrapping(self.0.bitand(&rhs.0))
91 }
92}
93
94impl BitAnd<&Wrapping<BoxedUint>> for Wrapping<BoxedUint> {
95 type Output = Wrapping<BoxedUint>;
96
97 fn bitand(self, rhs: &Wrapping<BoxedUint>) -> Wrapping<BoxedUint> {
98 Wrapping(self.0.bitand(&rhs.0))
99 }
100}
101
102impl BitAnd<Wrapping<BoxedUint>> for &Wrapping<BoxedUint> {
103 type Output = Wrapping<BoxedUint>;
104
105 fn bitand(self, rhs: Wrapping<BoxedUint>) -> Wrapping<BoxedUint> {
106 Wrapping(BoxedUint::bitand(&self.0, &rhs.0))
107 }
108}
109
110impl BitAnd<&Wrapping<BoxedUint>> for &Wrapping<BoxedUint> {
111 type Output = Wrapping<BoxedUint>;
112
113 fn bitand(self, rhs: &Wrapping<BoxedUint>) -> Wrapping<BoxedUint> {
114 Wrapping(BoxedUint::bitand(&self.0, &rhs.0))
115 }
116}
117
118impl BitAndAssign for Wrapping<BoxedUint> {
119 #[allow(clippy::assign_op_pattern)]
120 fn bitand_assign(&mut self, other: Self) {
121 *self = Wrapping(BoxedUint::bitand(&self.0, &other.0));
122 }
123}
124
125impl BitAndAssign<&Wrapping<BoxedUint>> for Wrapping<BoxedUint> {
126 #[allow(clippy::assign_op_pattern)]
127 fn bitand_assign(&mut self, other: &Self) {
128 *self = Wrapping(BoxedUint::bitand(&self.0, &other.0));
129 }
130}
131
132#[cfg(test)]
133mod tests {
134 use crate::BoxedUint;
135
136 #[test]
137 fn checked_and_ok() {
138 let result = BoxedUint::zero().checked_and(&BoxedUint::one());
139 assert_eq!(result.unwrap(), BoxedUint::zero());
140 }
141
142 #[test]
143 fn overlapping_and_ok() {
144 let result = BoxedUint::max(128).wrapping_and(&BoxedUint::one());
145 assert_eq!(result, BoxedUint::one());
146 }
147}