crypto_bigint/int/
bit_xor.rs1use core::ops::{BitXor, BitXorAssign};
4
5use crate::{CtOption, Uint, Wrapping};
6
7use super::Int;
8
9impl<const LIMBS: usize> Int<LIMBS> {
10 #[inline(always)]
12 #[must_use]
13 pub const fn bitxor(&self, rhs: &Self) -> Self {
14 Self(Uint::bitxor(&self.0, &rhs.0))
15 }
16
17 #[must_use]
22 pub const fn wrapping_xor(&self, rhs: &Self) -> Self {
23 self.bitxor(rhs)
24 }
25
26 #[must_use]
28 pub fn checked_xor(&self, rhs: &Self) -> CtOption<Self> {
29 CtOption::some(self.bitxor(rhs))
30 }
31}
32
33impl<const LIMBS: usize> BitXor for Int<LIMBS> {
34 type Output = Self;
35
36 fn bitxor(self, rhs: Self) -> Int<LIMBS> {
37 self.bitxor(&rhs)
38 }
39}
40
41impl<const LIMBS: usize> BitXor<&Int<LIMBS>> for Int<LIMBS> {
42 type Output = Int<LIMBS>;
43
44 #[allow(clippy::needless_borrow)]
45 fn bitxor(self, rhs: &Int<LIMBS>) -> Int<LIMBS> {
46 (&self).bitxor(rhs)
47 }
48}
49
50impl<const LIMBS: usize> BitXor<Int<LIMBS>> for &Int<LIMBS> {
51 type Output = Int<LIMBS>;
52
53 fn bitxor(self, rhs: Int<LIMBS>) -> Int<LIMBS> {
54 self.bitxor(&rhs)
55 }
56}
57
58impl<const LIMBS: usize> BitXor<&Int<LIMBS>> for &Int<LIMBS> {
59 type Output = Int<LIMBS>;
60
61 fn bitxor(self, rhs: &Int<LIMBS>) -> Int<LIMBS> {
62 self.bitxor(rhs)
63 }
64}
65
66impl<const LIMBS: usize> BitXorAssign for Int<LIMBS> {
67 fn bitxor_assign(&mut self, other: Self) {
68 *self = *self ^ other;
69 }
70}
71
72impl<const LIMBS: usize> BitXorAssign<&Int<LIMBS>> for Int<LIMBS> {
73 fn bitxor_assign(&mut self, other: &Self) {
74 *self = *self ^ other;
75 }
76}
77
78impl<const LIMBS: usize> BitXor for Wrapping<Int<LIMBS>> {
79 type Output = Self;
80
81 fn bitxor(self, rhs: Self) -> Wrapping<Int<LIMBS>> {
82 Wrapping(self.0.bitxor(&rhs.0))
83 }
84}
85
86impl<const LIMBS: usize> BitXor<&Wrapping<Int<LIMBS>>> for Wrapping<Int<LIMBS>> {
87 type Output = Wrapping<Int<LIMBS>>;
88
89 fn bitxor(self, rhs: &Wrapping<Int<LIMBS>>) -> Wrapping<Int<LIMBS>> {
90 Wrapping(self.0.bitxor(&rhs.0))
91 }
92}
93
94impl<const LIMBS: usize> BitXor<Wrapping<Int<LIMBS>>> for &Wrapping<Int<LIMBS>> {
95 type Output = Wrapping<Int<LIMBS>>;
96
97 fn bitxor(self, rhs: Wrapping<Int<LIMBS>>) -> Wrapping<Int<LIMBS>> {
98 Wrapping(self.0.bitxor(&rhs.0))
99 }
100}
101
102impl<const LIMBS: usize> BitXor<&Wrapping<Int<LIMBS>>> for &Wrapping<Int<LIMBS>> {
103 type Output = Wrapping<Int<LIMBS>>;
104
105 fn bitxor(self, rhs: &Wrapping<Int<LIMBS>>) -> Wrapping<Int<LIMBS>> {
106 Wrapping(self.0.bitxor(&rhs.0))
107 }
108}
109
110impl<const LIMBS: usize> BitXorAssign for Wrapping<Int<LIMBS>> {
111 fn bitxor_assign(&mut self, other: Self) {
112 *self = *self ^ other;
113 }
114}
115
116impl<const LIMBS: usize> BitXorAssign<&Wrapping<Int<LIMBS>>> for Wrapping<Int<LIMBS>> {
117 fn bitxor_assign(&mut self, other: &Self) {
118 *self = *self ^ other;
119 }
120}
121
122#[cfg(test)]
123mod tests {
124 use crate::I128;
125
126 #[test]
127 fn checked_xor_ok() {
128 assert_eq!(I128::ZERO.checked_xor(&I128::ONE).unwrap(), I128::ONE);
129 assert_eq!(I128::ONE.checked_xor(&I128::ONE).unwrap(), I128::ZERO);
130 assert_eq!(
131 I128::MAX.checked_xor(&I128::ONE).unwrap(),
132 I128::MAX - I128::ONE
133 );
134 }
135
136 #[test]
137 fn wrapping_xor_ok() {
138 assert_eq!(I128::ZERO.wrapping_xor(&I128::ONE), I128::ONE);
139 assert_eq!(I128::ONE.wrapping_xor(&I128::ONE), I128::ZERO);
140 assert_eq!(I128::MAX.wrapping_xor(&I128::ONE), I128::MAX - I128::ONE);
141 }
142}