crypto_bigint/uint/
bit_and.rs
1use super::UInt;
4use crate::{Limb, Wrapping};
5use core::ops::{BitAnd, BitAndAssign};
6use subtle::{Choice, CtOption};
7
8impl<const LIMBS: usize> UInt<LIMBS> {
9 #[inline(always)]
11 pub const fn bitand(&self, rhs: &Self) -> Self {
12 let mut limbs = [Limb::ZERO; LIMBS];
13 let mut i = 0;
14
15 while i < LIMBS {
16 limbs[i] = self.limbs[i].bitand(rhs.limbs[i]);
17 i += 1;
18 }
19
20 Self { limbs }
21 }
22
23 pub const fn wrapping_and(&self, rhs: &Self) -> Self {
28 self.bitand(rhs)
29 }
30
31 pub fn checked_and(&self, rhs: &Self) -> CtOption<Self> {
33 let result = self.bitand(rhs);
34 CtOption::new(result, Choice::from(1))
35 }
36}
37
38impl<const LIMBS: usize> BitAnd for UInt<LIMBS> {
39 type Output = Self;
40
41 fn bitand(self, rhs: Self) -> UInt<LIMBS> {
42 self.bitand(&rhs)
43 }
44}
45
46impl<const LIMBS: usize> BitAnd<&UInt<LIMBS>> for UInt<LIMBS> {
47 type Output = UInt<LIMBS>;
48
49 fn bitand(self, rhs: &UInt<LIMBS>) -> UInt<LIMBS> {
50 (&self).bitand(rhs)
51 }
52}
53
54impl<const LIMBS: usize> BitAnd<UInt<LIMBS>> for &UInt<LIMBS> {
55 type Output = UInt<LIMBS>;
56
57 fn bitand(self, rhs: UInt<LIMBS>) -> UInt<LIMBS> {
58 self.bitand(&rhs)
59 }
60}
61
62impl<const LIMBS: usize> BitAnd<&UInt<LIMBS>> for &UInt<LIMBS> {
63 type Output = UInt<LIMBS>;
64
65 fn bitand(self, rhs: &UInt<LIMBS>) -> UInt<LIMBS> {
66 self.bitand(rhs)
67 }
68}
69
70impl<const LIMBS: usize> BitAndAssign for UInt<LIMBS> {
71 #[allow(clippy::assign_op_pattern)]
72 fn bitand_assign(&mut self, other: Self) {
73 *self = *self & other;
74 }
75}
76
77impl<const LIMBS: usize> BitAndAssign<&UInt<LIMBS>> for UInt<LIMBS> {
78 #[allow(clippy::assign_op_pattern)]
79 fn bitand_assign(&mut self, other: &Self) {
80 *self = *self & other;
81 }
82}
83
84impl<const LIMBS: usize> BitAnd for Wrapping<UInt<LIMBS>> {
85 type Output = Self;
86
87 fn bitand(self, rhs: Self) -> Wrapping<UInt<LIMBS>> {
88 Wrapping(self.0.bitand(&rhs.0))
89 }
90}
91
92impl<const LIMBS: usize> BitAnd<&Wrapping<UInt<LIMBS>>> for Wrapping<UInt<LIMBS>> {
93 type Output = Wrapping<UInt<LIMBS>>;
94
95 fn bitand(self, rhs: &Wrapping<UInt<LIMBS>>) -> Wrapping<UInt<LIMBS>> {
96 Wrapping(self.0.bitand(&rhs.0))
97 }
98}
99
100impl<const LIMBS: usize> BitAnd<Wrapping<UInt<LIMBS>>> for &Wrapping<UInt<LIMBS>> {
101 type Output = Wrapping<UInt<LIMBS>>;
102
103 fn bitand(self, rhs: Wrapping<UInt<LIMBS>>) -> Wrapping<UInt<LIMBS>> {
104 Wrapping(self.0.bitand(&rhs.0))
105 }
106}
107
108impl<const LIMBS: usize> BitAnd<&Wrapping<UInt<LIMBS>>> for &Wrapping<UInt<LIMBS>> {
109 type Output = Wrapping<UInt<LIMBS>>;
110
111 fn bitand(self, rhs: &Wrapping<UInt<LIMBS>>) -> Wrapping<UInt<LIMBS>> {
112 Wrapping(self.0.bitand(&rhs.0))
113 }
114}
115
116impl<const LIMBS: usize> BitAndAssign for Wrapping<UInt<LIMBS>> {
117 #[allow(clippy::assign_op_pattern)]
118 fn bitand_assign(&mut self, other: Self) {
119 *self = *self & other;
120 }
121}
122
123impl<const LIMBS: usize> BitAndAssign<&Wrapping<UInt<LIMBS>>> for Wrapping<UInt<LIMBS>> {
124 #[allow(clippy::assign_op_pattern)]
125 fn bitand_assign(&mut self, other: &Self) {
126 *self = *self & other;
127 }
128}
129
130#[cfg(test)]
131mod tests {
132 use crate::U128;
133
134 #[test]
135 fn checked_and_ok() {
136 let result = U128::ZERO.checked_and(&U128::ONE);
137 assert_eq!(result.unwrap(), U128::ZERO);
138 }
139
140 #[test]
141 fn overlapping_and_ok() {
142 let result = U128::MAX.wrapping_and(&U128::ONE);
143 assert_eq!(result, U128::ONE);
144 }
145}