crypto_bigint/uint/
bit_and.rs1use 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 #[allow(clippy::needless_borrow)]
50 fn bitand(self, rhs: &Uint<LIMBS>) -> Uint<LIMBS> {
51 (&self).bitand(rhs)
52 }
53}
54
55impl<const LIMBS: usize> BitAnd<Uint<LIMBS>> for &Uint<LIMBS> {
56 type Output = Uint<LIMBS>;
57
58 fn bitand(self, rhs: Uint<LIMBS>) -> Uint<LIMBS> {
59 self.bitand(&rhs)
60 }
61}
62
63impl<const LIMBS: usize> BitAnd<&Uint<LIMBS>> for &Uint<LIMBS> {
64 type Output = Uint<LIMBS>;
65
66 fn bitand(self, rhs: &Uint<LIMBS>) -> Uint<LIMBS> {
67 self.bitand(rhs)
68 }
69}
70
71impl<const LIMBS: usize> BitAndAssign for Uint<LIMBS> {
72 #[allow(clippy::assign_op_pattern)]
73 fn bitand_assign(&mut self, other: Self) {
74 *self = *self & other;
75 }
76}
77
78impl<const LIMBS: usize> BitAndAssign<&Uint<LIMBS>> for Uint<LIMBS> {
79 #[allow(clippy::assign_op_pattern)]
80 fn bitand_assign(&mut self, other: &Self) {
81 *self = *self & other;
82 }
83}
84
85impl<const LIMBS: usize> BitAnd for Wrapping<Uint<LIMBS>> {
86 type Output = Self;
87
88 fn bitand(self, rhs: Self) -> Wrapping<Uint<LIMBS>> {
89 Wrapping(self.0.bitand(&rhs.0))
90 }
91}
92
93impl<const LIMBS: usize> BitAnd<&Wrapping<Uint<LIMBS>>> for Wrapping<Uint<LIMBS>> {
94 type Output = Wrapping<Uint<LIMBS>>;
95
96 fn bitand(self, rhs: &Wrapping<Uint<LIMBS>>) -> Wrapping<Uint<LIMBS>> {
97 Wrapping(self.0.bitand(&rhs.0))
98 }
99}
100
101impl<const LIMBS: usize> BitAnd<Wrapping<Uint<LIMBS>>> for &Wrapping<Uint<LIMBS>> {
102 type Output = Wrapping<Uint<LIMBS>>;
103
104 fn bitand(self, rhs: Wrapping<Uint<LIMBS>>) -> Wrapping<Uint<LIMBS>> {
105 Wrapping(self.0.bitand(&rhs.0))
106 }
107}
108
109impl<const LIMBS: usize> BitAnd<&Wrapping<Uint<LIMBS>>> for &Wrapping<Uint<LIMBS>> {
110 type Output = Wrapping<Uint<LIMBS>>;
111
112 fn bitand(self, rhs: &Wrapping<Uint<LIMBS>>) -> Wrapping<Uint<LIMBS>> {
113 Wrapping(self.0.bitand(&rhs.0))
114 }
115}
116
117impl<const LIMBS: usize> BitAndAssign for Wrapping<Uint<LIMBS>> {
118 #[allow(clippy::assign_op_pattern)]
119 fn bitand_assign(&mut self, other: Self) {
120 *self = *self & other;
121 }
122}
123
124impl<const LIMBS: usize> BitAndAssign<&Wrapping<Uint<LIMBS>>> for Wrapping<Uint<LIMBS>> {
125 #[allow(clippy::assign_op_pattern)]
126 fn bitand_assign(&mut self, other: &Self) {
127 *self = *self & other;
128 }
129}
130
131#[cfg(test)]
132mod tests {
133 use crate::U128;
134
135 #[test]
136 fn checked_and_ok() {
137 let result = U128::ZERO.checked_and(&U128::ONE);
138 assert_eq!(result.unwrap(), U128::ZERO);
139 }
140
141 #[test]
142 fn overlapping_and_ok() {
143 let result = U128::MAX.wrapping_and(&U128::ONE);
144 assert_eq!(result, U128::ONE);
145 }
146}