Skip to main content

crypto_bigint/uint/
sub.rs

1//! [`UInt`] addition operations.
2
3use super::UInt;
4use crate::{Limb, Wrapping};
5use core::ops::{Sub, SubAssign};
6use subtle::CtOption;
7
8impl<const LIMBS: usize> UInt<LIMBS> {
9    /// Computes `a - (b + borrow)`, returning the result along with the new borrow.
10    #[inline(always)]
11    pub const fn sbb(&self, rhs: &Self, mut borrow: Limb) -> (Self, Limb) {
12        let mut limbs = [Limb::ZERO; LIMBS];
13        let mut i = 0;
14
15        while i < LIMBS {
16            let (w, b) = self.limbs[i].sbb(rhs.limbs[i], borrow);
17            limbs[i] = w;
18            borrow = b;
19            i += 1;
20        }
21
22        (Self { limbs }, borrow)
23    }
24
25    /// Perform wrapping subtraction, discarding underflow and wrapping around
26    /// the boundary of the type.
27    pub const fn wrapping_sub(&self, rhs: &Self) -> Self {
28        self.sbb(rhs, Limb::ZERO).0
29    }
30
31    /// Perform checked subtraction, returning a [`CtOption`] which `is_some`
32    /// only if the operation did not overflow.
33    pub fn checked_sub(&self, rhs: &Self) -> CtOption<Self> {
34        let (result, underflow) = self.sbb(rhs, Limb::ZERO);
35        CtOption::new(result, underflow.is_zero())
36    }
37}
38
39impl<const LIMBS: usize> Sub for Wrapping<UInt<LIMBS>> {
40    type Output = Self;
41
42    fn sub(self, rhs: Self) -> Wrapping<UInt<LIMBS>> {
43        Wrapping(self.0.wrapping_sub(&rhs.0))
44    }
45}
46
47impl<const LIMBS: usize> Sub<&Wrapping<UInt<LIMBS>>> for Wrapping<UInt<LIMBS>> {
48    type Output = Wrapping<UInt<LIMBS>>;
49
50    fn sub(self, rhs: &Wrapping<UInt<LIMBS>>) -> Wrapping<UInt<LIMBS>> {
51        Wrapping(self.0.wrapping_sub(&rhs.0))
52    }
53}
54
55impl<const LIMBS: usize> Sub<Wrapping<UInt<LIMBS>>> for &Wrapping<UInt<LIMBS>> {
56    type Output = Wrapping<UInt<LIMBS>>;
57
58    fn sub(self, rhs: Wrapping<UInt<LIMBS>>) -> Wrapping<UInt<LIMBS>> {
59        Wrapping(self.0.wrapping_sub(&rhs.0))
60    }
61}
62
63impl<const LIMBS: usize> Sub<&Wrapping<UInt<LIMBS>>> for &Wrapping<UInt<LIMBS>> {
64    type Output = Wrapping<UInt<LIMBS>>;
65
66    fn sub(self, rhs: &Wrapping<UInt<LIMBS>>) -> Wrapping<UInt<LIMBS>> {
67        Wrapping(self.0.wrapping_sub(&rhs.0))
68    }
69}
70
71impl<const LIMBS: usize> SubAssign for Wrapping<UInt<LIMBS>> {
72    fn sub_assign(&mut self, other: Self) {
73        *self = *self - other;
74    }
75}
76
77impl<const LIMBS: usize> SubAssign<&Wrapping<UInt<LIMBS>>> for Wrapping<UInt<LIMBS>> {
78    fn sub_assign(&mut self, other: &Self) {
79        *self = *self - other;
80    }
81}
82
83#[cfg(test)]
84mod tests {
85    use crate::{Limb, U128};
86
87    #[test]
88    fn sbb_no_borrow() {
89        let (res, borrow) = U128::ONE.sbb(&U128::ONE, Limb::ZERO);
90        assert_eq!(res, U128::ZERO);
91        assert_eq!(borrow, Limb::ZERO);
92    }
93
94    #[test]
95    fn sbb_with_borrow() {
96        let (res, borrow) = U128::ZERO.sbb(&U128::ONE, Limb::ZERO);
97
98        assert_eq!(res, U128::MAX);
99        assert_eq!(borrow, Limb::MAX);
100    }
101
102    #[test]
103    fn wrapping_sub_no_borrow() {
104        assert_eq!(U128::ONE.wrapping_sub(&U128::ONE), U128::ZERO);
105    }
106
107    #[test]
108    fn wrapping_sub_with_borrow() {
109        assert_eq!(U128::ZERO.wrapping_sub(&U128::ONE), U128::MAX);
110    }
111
112    #[test]
113    fn checked_sub_ok() {
114        let result = U128::ONE.checked_sub(&U128::ONE);
115        assert_eq!(result.unwrap(), U128::ZERO);
116    }
117
118    #[test]
119    fn checked_sub_overflow() {
120        let result = U128::ZERO.checked_sub(&U128::ONE);
121        assert!(!bool::from(result.is_some()));
122    }
123}