crypto_bigint/uint/
sub.rs

1//! [`Uint`] subtraction operations.
2
3use super::Uint;
4use crate::{Checked, CheckedSub, ConstChoice, Limb, Wrapping, WrappingSub, Zero};
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 saturating subtraction, returning `ZERO` on underflow.
26    pub const fn saturating_sub(&self, rhs: &Self) -> Self {
27        let (res, underflow) = self.sbb(rhs, Limb::ZERO);
28        Self::select(&res, &Self::ZERO, ConstChoice::from_word_mask(underflow.0))
29    }
30
31    /// Perform wrapping subtraction, discarding underflow and wrapping around
32    /// the boundary of the type.
33    pub const fn wrapping_sub(&self, rhs: &Self) -> Self {
34        self.sbb(rhs, Limb::ZERO).0
35    }
36}
37
38impl<const LIMBS: usize> CheckedSub for Uint<LIMBS> {
39    fn checked_sub(&self, rhs: &Self) -> CtOption<Self> {
40        let (result, underflow) = self.sbb(rhs, Limb::ZERO);
41        CtOption::new(result, underflow.is_zero())
42    }
43}
44
45impl<const LIMBS: usize> Sub for Uint<LIMBS> {
46    type Output = Self;
47
48    fn sub(self, rhs: Self) -> Self {
49        self.sub(&rhs)
50    }
51}
52
53impl<const LIMBS: usize> Sub<&Uint<LIMBS>> for Uint<LIMBS> {
54    type Output = Self;
55
56    fn sub(self, rhs: &Self) -> Self {
57        self.checked_sub(rhs)
58            .expect("attempted to subtract with underflow")
59    }
60}
61
62impl<const LIMBS: usize> SubAssign<Uint<LIMBS>> for Uint<LIMBS> {
63    fn sub_assign(&mut self, rhs: Uint<LIMBS>) {
64        *self = self.sub(&rhs)
65    }
66}
67
68impl<const LIMBS: usize> SubAssign<&Uint<LIMBS>> for Uint<LIMBS> {
69    fn sub_assign(&mut self, rhs: &Uint<LIMBS>) {
70        *self = self.sub(rhs)
71    }
72}
73
74impl<const LIMBS: usize> SubAssign for Wrapping<Uint<LIMBS>> {
75    fn sub_assign(&mut self, other: Self) {
76        *self = *self - other;
77    }
78}
79
80impl<const LIMBS: usize> SubAssign<&Wrapping<Uint<LIMBS>>> for Wrapping<Uint<LIMBS>> {
81    fn sub_assign(&mut self, other: &Self) {
82        *self = *self - other;
83    }
84}
85
86impl<const LIMBS: usize> SubAssign for Checked<Uint<LIMBS>> {
87    fn sub_assign(&mut self, other: Self) {
88        *self = *self - other;
89    }
90}
91
92impl<const LIMBS: usize> SubAssign<&Checked<Uint<LIMBS>>> for Checked<Uint<LIMBS>> {
93    fn sub_assign(&mut self, other: &Self) {
94        *self = *self - other;
95    }
96}
97
98impl<const LIMBS: usize> WrappingSub for Uint<LIMBS> {
99    fn wrapping_sub(&self, v: &Self) -> Self {
100        self.wrapping_sub(v)
101    }
102}
103
104#[cfg(test)]
105mod tests {
106    use crate::{CheckedSub, Limb, U128};
107
108    #[test]
109    fn sbb_no_borrow() {
110        let (res, borrow) = U128::ONE.sbb(&U128::ONE, Limb::ZERO);
111        assert_eq!(res, U128::ZERO);
112        assert_eq!(borrow, Limb::ZERO);
113    }
114
115    #[test]
116    fn sbb_with_borrow() {
117        let (res, borrow) = U128::ZERO.sbb(&U128::ONE, Limb::ZERO);
118
119        assert_eq!(res, U128::MAX);
120        assert_eq!(borrow, Limb::MAX);
121    }
122
123    #[test]
124    fn saturating_sub_no_borrow() {
125        assert_eq!(
126            U128::from(5u64).saturating_sub(&U128::ONE),
127            U128::from(4u64)
128        );
129    }
130
131    #[test]
132    fn saturating_sub_with_borrow() {
133        assert_eq!(
134            U128::from(4u64).saturating_sub(&U128::from(5u64)),
135            U128::ZERO
136        );
137    }
138
139    #[test]
140    fn wrapping_sub_no_borrow() {
141        assert_eq!(U128::ONE.wrapping_sub(&U128::ONE), U128::ZERO);
142    }
143
144    #[test]
145    fn wrapping_sub_with_borrow() {
146        assert_eq!(U128::ZERO.wrapping_sub(&U128::ONE), U128::MAX);
147    }
148
149    #[test]
150    fn checked_sub_ok() {
151        let result = U128::ONE.checked_sub(&U128::ONE);
152        assert_eq!(result.unwrap(), U128::ZERO);
153    }
154
155    #[test]
156    fn checked_sub_overflow() {
157        let result = U128::ZERO.checked_sub(&U128::ONE);
158        assert!(!bool::from(result.is_some()));
159    }
160}