crypto_bigint/int/
bit_and.rs1use core::ops::{BitAnd, BitAndAssign};
4
5use crate::{CtOption, Int, Limb, Uint, Wrapping};
6
7impl<const LIMBS: usize> Int<LIMBS> {
8 #[inline(always)]
10 #[must_use]
11 pub const fn bitand(&self, rhs: &Self) -> Self {
12 Self(Uint::bitand(&self.0, &rhs.0))
13 }
14
15 #[must_use]
18 pub const fn bitand_limb(&self, rhs: Limb) -> Self {
19 Self(Uint::bitand_limb(&self.0, rhs))
20 }
21
22 #[must_use]
27 pub const fn wrapping_and(&self, rhs: &Self) -> Self {
28 self.bitand(rhs)
29 }
30
31 #[must_use]
33 pub const fn checked_and(&self, rhs: &Self) -> CtOption<Self> {
34 CtOption::some(self.bitand(rhs))
35 }
36}
37
38impl<const LIMBS: usize> BitAnd for Int<LIMBS> {
39 type Output = Self;
40
41 fn bitand(self, rhs: Self) -> Int<LIMBS> {
42 self.bitand(&rhs)
43 }
44}
45
46impl<const LIMBS: usize> BitAnd<&Int<LIMBS>> for Int<LIMBS> {
47 type Output = Int<LIMBS>;
48
49 #[allow(clippy::needless_borrow)]
50 fn bitand(self, rhs: &Int<LIMBS>) -> Int<LIMBS> {
51 (&self).bitand(rhs)
52 }
53}
54
55impl<const LIMBS: usize> BitAnd<Int<LIMBS>> for &Int<LIMBS> {
56 type Output = Int<LIMBS>;
57
58 fn bitand(self, rhs: Int<LIMBS>) -> Int<LIMBS> {
59 self.bitand(&rhs)
60 }
61}
62
63impl<const LIMBS: usize> BitAnd<&Int<LIMBS>> for &Int<LIMBS> {
64 type Output = Int<LIMBS>;
65
66 fn bitand(self, rhs: &Int<LIMBS>) -> Int<LIMBS> {
67 self.bitand(rhs)
68 }
69}
70
71impl<const LIMBS: usize> BitAndAssign for Int<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<&Int<LIMBS>> for Int<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<Int<LIMBS>> {
86 type Output = Self;
87
88 fn bitand(self, rhs: Self) -> Wrapping<Int<LIMBS>> {
89 Wrapping(self.0.bitand(&rhs.0))
90 }
91}
92
93impl<const LIMBS: usize> BitAnd<&Wrapping<Int<LIMBS>>> for Wrapping<Int<LIMBS>> {
94 type Output = Wrapping<Int<LIMBS>>;
95
96 fn bitand(self, rhs: &Wrapping<Int<LIMBS>>) -> Wrapping<Int<LIMBS>> {
97 Wrapping(self.0.bitand(&rhs.0))
98 }
99}
100
101impl<const LIMBS: usize> BitAnd<Wrapping<Int<LIMBS>>> for &Wrapping<Int<LIMBS>> {
102 type Output = Wrapping<Int<LIMBS>>;
103
104 fn bitand(self, rhs: Wrapping<Int<LIMBS>>) -> Wrapping<Int<LIMBS>> {
105 Wrapping(self.0.bitand(&rhs.0))
106 }
107}
108
109impl<const LIMBS: usize> BitAnd<&Wrapping<Int<LIMBS>>> for &Wrapping<Int<LIMBS>> {
110 type Output = Wrapping<Int<LIMBS>>;
111
112 fn bitand(self, rhs: &Wrapping<Int<LIMBS>>) -> Wrapping<Int<LIMBS>> {
113 Wrapping(self.0.bitand(&rhs.0))
114 }
115}
116
117impl<const LIMBS: usize> BitAndAssign for Wrapping<Int<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<Int<LIMBS>>> for Wrapping<Int<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::I128;
134
135 #[test]
136 fn checked_and_ok() {
137 assert_eq!(I128::ZERO.checked_and(&I128::ONE).unwrap(), I128::ZERO);
138 assert_eq!(I128::ONE.checked_and(&I128::ONE).unwrap(), I128::ONE);
139 assert_eq!(I128::MAX.checked_and(&I128::ONE).unwrap(), I128::ONE);
140 }
141
142 #[test]
143 fn wrapping_and_ok() {
144 assert_eq!(I128::ZERO.wrapping_and(&I128::ONE), I128::ZERO);
145 assert_eq!(I128::ONE.wrapping_and(&I128::ONE), I128::ONE);
146 assert_eq!(I128::MAX.wrapping_and(&I128::ONE), I128::ONE);
147 }
148}