crypto_bigint/uint/boxed/
bit_xor.rs1use super::BoxedUint;
4use crate::Wrapping;
5use core::ops::{BitXor, BitXorAssign};
6use subtle::{Choice, CtOption};
7
8impl BoxedUint {
9 #[inline(always)]
11 pub fn bitxor(&self, rhs: &Self) -> Self {
12 Self::map_limbs(self, rhs, |a, b| a.bitxor(b))
13 }
14
15 pub fn wrapping_xor(&self, rhs: &Self) -> Self {
20 self.bitxor(rhs)
21 }
22
23 pub fn checked_xor(&self, rhs: &Self) -> CtOption<Self> {
25 let result = self.bitxor(rhs);
26 CtOption::new(result, Choice::from(1))
27 }
28}
29
30impl BitXor for BoxedUint {
31 type Output = Self;
32
33 fn bitxor(self, rhs: Self) -> BoxedUint {
34 self.bitxor(&rhs)
35 }
36}
37
38impl BitXor<&BoxedUint> for BoxedUint {
39 type Output = BoxedUint;
40
41 fn bitxor(self, rhs: &BoxedUint) -> BoxedUint {
42 Self::bitxor(&self, rhs)
43 }
44}
45
46impl BitXor<BoxedUint> for &BoxedUint {
47 type Output = BoxedUint;
48
49 fn bitxor(self, rhs: BoxedUint) -> BoxedUint {
50 self.bitxor(&rhs)
51 }
52}
53
54impl BitXor<&BoxedUint> for &BoxedUint {
55 type Output = BoxedUint;
56
57 fn bitxor(self, rhs: &BoxedUint) -> BoxedUint {
58 self.bitxor(rhs)
59 }
60}
61
62impl BitXorAssign for BoxedUint {
63 fn bitxor_assign(&mut self, other: Self) {
64 *self = Self::bitxor(self, &other);
65 }
66}
67
68impl BitXorAssign<&BoxedUint> for BoxedUint {
69 fn bitxor_assign(&mut self, other: &Self) {
70 *self = Self::bitxor(self, other);
71 }
72}
73
74impl BitXor for Wrapping<BoxedUint> {
75 type Output = Self;
76
77 fn bitxor(self, rhs: Self) -> Wrapping<BoxedUint> {
78 Wrapping(self.0.bitxor(&rhs.0))
79 }
80}
81
82impl BitXor<&Wrapping<BoxedUint>> for Wrapping<BoxedUint> {
83 type Output = Wrapping<BoxedUint>;
84
85 fn bitxor(self, rhs: &Wrapping<BoxedUint>) -> Wrapping<BoxedUint> {
86 Wrapping(self.0.bitxor(&rhs.0))
87 }
88}
89
90impl BitXor<Wrapping<BoxedUint>> for &Wrapping<BoxedUint> {
91 type Output = Wrapping<BoxedUint>;
92
93 fn bitxor(self, rhs: Wrapping<BoxedUint>) -> Wrapping<BoxedUint> {
94 Wrapping(BoxedUint::bitxor(&self.0, &rhs.0))
95 }
96}
97
98impl BitXor<&Wrapping<BoxedUint>> for &Wrapping<BoxedUint> {
99 type Output = Wrapping<BoxedUint>;
100
101 fn bitxor(self, rhs: &Wrapping<BoxedUint>) -> Wrapping<BoxedUint> {
102 Wrapping(BoxedUint::bitxor(&self.0, &rhs.0))
103 }
104}
105
106impl BitXorAssign for Wrapping<BoxedUint> {
107 fn bitxor_assign(&mut self, other: Self) {
108 *self = Wrapping(BoxedUint::bitxor(&self.0, &other.0));
109 }
110}
111
112impl BitXorAssign<&Wrapping<BoxedUint>> for Wrapping<BoxedUint> {
113 fn bitxor_assign(&mut self, other: &Self) {
114 *self = Wrapping(BoxedUint::bitxor(&self.0, &other.0));
115 }
116}
117
118#[cfg(test)]
119mod tests {
120 use crate::U128;
121
122 #[test]
123 fn checked_xor_ok() {
124 let result = U128::ZERO.checked_xor(&U128::ONE);
125 assert_eq!(result.unwrap(), U128::ONE);
126 }
127
128 #[test]
129 fn overlapping_xor_ok() {
130 let result = U128::ZERO.wrapping_xor(&U128::ONE);
131 assert_eq!(result, U128::ONE);
132 }
133}