Skip to main content

crypto_bigint/int/
bit_or.rs

1//! [`Int`] bitwise OR operations.
2
3use core::ops::{BitOr, BitOrAssign};
4
5use crate::{CtOption, Uint};
6
7use super::Int;
8
9impl<const LIMBS: usize> Int<LIMBS> {
10    /// Computes bitwise `a | b`.
11    #[inline(always)]
12    #[must_use]
13    pub const fn bitor(&self, rhs: &Self) -> Self {
14        Self(Uint::bitor(&self.0, &rhs.0))
15    }
16
17    /// Perform wrapping bitwise `OR`.
18    ///
19    /// There's no way wrapping could ever happen.
20    /// This function exists so that all operations are accounted for in the wrapping operations
21    #[must_use]
22    pub const fn wrapping_or(&self, rhs: &Self) -> Self {
23        self.bitor(rhs)
24    }
25
26    /// Perform checked bitwise `OR`, returning a [`CtOption`] which `is_some` always
27    #[must_use]
28    pub const fn checked_or(&self, rhs: &Self) -> CtOption<Self> {
29        CtOption::some(self.bitor(rhs))
30    }
31}
32
33impl<const LIMBS: usize> BitOr for Int<LIMBS> {
34    type Output = Self;
35
36    fn bitor(self, rhs: Self) -> Int<LIMBS> {
37        self.bitor(&rhs)
38    }
39}
40
41impl<const LIMBS: usize> BitOr<&Int<LIMBS>> for Int<LIMBS> {
42    type Output = Int<LIMBS>;
43
44    #[allow(clippy::needless_borrow)]
45    fn bitor(self, rhs: &Int<LIMBS>) -> Int<LIMBS> {
46        (&self).bitor(rhs)
47    }
48}
49
50impl<const LIMBS: usize> BitOr<Int<LIMBS>> for &Int<LIMBS> {
51    type Output = Int<LIMBS>;
52
53    fn bitor(self, rhs: Int<LIMBS>) -> Int<LIMBS> {
54        self.bitor(&rhs)
55    }
56}
57
58impl<const LIMBS: usize> BitOr<&Int<LIMBS>> for &Int<LIMBS> {
59    type Output = Int<LIMBS>;
60
61    fn bitor(self, rhs: &Int<LIMBS>) -> Int<LIMBS> {
62        self.bitor(rhs)
63    }
64}
65
66impl<const LIMBS: usize> BitOrAssign for Int<LIMBS> {
67    fn bitor_assign(&mut self, other: Self) {
68        *self = *self | other;
69    }
70}
71
72impl<const LIMBS: usize> BitOrAssign<&Int<LIMBS>> for Int<LIMBS> {
73    fn bitor_assign(&mut self, other: &Self) {
74        *self = *self | other;
75    }
76}
77
78#[cfg(test)]
79mod tests {
80    use crate::I128;
81
82    #[test]
83    fn checked_or_ok() {
84        assert_eq!(I128::ZERO.checked_or(&I128::ONE).unwrap(), I128::ONE);
85        assert_eq!(I128::ONE.checked_or(&I128::ONE).unwrap(), I128::ONE);
86        assert_eq!(I128::MAX.checked_or(&I128::ONE).unwrap(), I128::MAX);
87    }
88
89    #[test]
90    fn wrapping_or_ok() {
91        assert_eq!(I128::ZERO.wrapping_or(&I128::ONE), I128::ONE);
92        assert_eq!(I128::ONE.wrapping_or(&I128::ONE), I128::ONE);
93        assert_eq!(I128::MAX.wrapping_or(&I128::ONE), I128::MAX);
94    }
95}