crypto_bigint/uint/
sub.rs

1//! [`UInt`] addition operations.
2
3use super::UInt;
4use crate::{Checked, CheckedSub, Limb, Wrapping, 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
29        if underflow.0 == 0 {
30            res
31        } else {
32            Self::ZERO
33        }
34    }
35
36    /// Perform wrapping subtraction, discarding underflow and wrapping around
37    /// the boundary of the type.
38    pub const fn wrapping_sub(&self, rhs: &Self) -> Self {
39        self.sbb(rhs, Limb::ZERO).0
40    }
41}
42
43impl<const LIMBS: usize> CheckedSub<&UInt<LIMBS>> for UInt<LIMBS> {
44    type Output = Self;
45
46    fn checked_sub(&self, rhs: &Self) -> CtOption<Self> {
47        let (result, underflow) = self.sbb(rhs, Limb::ZERO);
48        CtOption::new(result, underflow.is_zero())
49    }
50}
51
52impl<const LIMBS: usize> Sub for Wrapping<UInt<LIMBS>> {
53    type Output = Self;
54
55    fn sub(self, rhs: Self) -> Wrapping<UInt<LIMBS>> {
56        Wrapping(self.0.wrapping_sub(&rhs.0))
57    }
58}
59
60impl<const LIMBS: usize> Sub<&Wrapping<UInt<LIMBS>>> for Wrapping<UInt<LIMBS>> {
61    type Output = Wrapping<UInt<LIMBS>>;
62
63    fn sub(self, rhs: &Wrapping<UInt<LIMBS>>) -> Wrapping<UInt<LIMBS>> {
64        Wrapping(self.0.wrapping_sub(&rhs.0))
65    }
66}
67
68impl<const LIMBS: usize> Sub<Wrapping<UInt<LIMBS>>> for &Wrapping<UInt<LIMBS>> {
69    type Output = Wrapping<UInt<LIMBS>>;
70
71    fn sub(self, rhs: Wrapping<UInt<LIMBS>>) -> Wrapping<UInt<LIMBS>> {
72        Wrapping(self.0.wrapping_sub(&rhs.0))
73    }
74}
75
76impl<const LIMBS: usize> Sub<&Wrapping<UInt<LIMBS>>> for &Wrapping<UInt<LIMBS>> {
77    type Output = Wrapping<UInt<LIMBS>>;
78
79    fn sub(self, rhs: &Wrapping<UInt<LIMBS>>) -> Wrapping<UInt<LIMBS>> {
80        Wrapping(self.0.wrapping_sub(&rhs.0))
81    }
82}
83
84impl<const LIMBS: usize> SubAssign for Wrapping<UInt<LIMBS>> {
85    fn sub_assign(&mut self, other: Self) {
86        *self = *self - other;
87    }
88}
89
90impl<const LIMBS: usize> SubAssign<&Wrapping<UInt<LIMBS>>> for Wrapping<UInt<LIMBS>> {
91    fn sub_assign(&mut self, other: &Self) {
92        *self = *self - other;
93    }
94}
95
96impl<const LIMBS: usize> Sub for Checked<UInt<LIMBS>> {
97    type Output = Self;
98
99    fn sub(self, rhs: Self) -> Checked<UInt<LIMBS>> {
100        Checked(
101            self.0
102                .and_then(|lhs| rhs.0.and_then(|rhs| lhs.checked_sub(&rhs))),
103        )
104    }
105}
106
107impl<const LIMBS: usize> Sub<&Checked<UInt<LIMBS>>> for Checked<UInt<LIMBS>> {
108    type Output = Checked<UInt<LIMBS>>;
109
110    fn sub(self, rhs: &Checked<UInt<LIMBS>>) -> Checked<UInt<LIMBS>> {
111        Checked(
112            self.0
113                .and_then(|lhs| rhs.0.and_then(|rhs| lhs.checked_sub(&rhs))),
114        )
115    }
116}
117
118impl<const LIMBS: usize> Sub<Checked<UInt<LIMBS>>> for &Checked<UInt<LIMBS>> {
119    type Output = Checked<UInt<LIMBS>>;
120
121    fn sub(self, rhs: Checked<UInt<LIMBS>>) -> Checked<UInt<LIMBS>> {
122        Checked(
123            self.0
124                .and_then(|lhs| rhs.0.and_then(|rhs| lhs.checked_sub(&rhs))),
125        )
126    }
127}
128
129impl<const LIMBS: usize> Sub<&Checked<UInt<LIMBS>>> for &Checked<UInt<LIMBS>> {
130    type Output = Checked<UInt<LIMBS>>;
131
132    fn sub(self, rhs: &Checked<UInt<LIMBS>>) -> Checked<UInt<LIMBS>> {
133        Checked(
134            self.0
135                .and_then(|lhs| rhs.0.and_then(|rhs| lhs.checked_sub(&rhs))),
136        )
137    }
138}
139
140impl<const LIMBS: usize> SubAssign for Checked<UInt<LIMBS>> {
141    fn sub_assign(&mut self, other: Self) {
142        *self = *self - other;
143    }
144}
145
146impl<const LIMBS: usize> SubAssign<&Checked<UInt<LIMBS>>> for Checked<UInt<LIMBS>> {
147    fn sub_assign(&mut self, other: &Self) {
148        *self = *self - other;
149    }
150}
151
152#[cfg(test)]
153mod tests {
154    use crate::{CheckedSub, Limb, U128};
155
156    #[test]
157    fn sbb_no_borrow() {
158        let (res, borrow) = U128::ONE.sbb(&U128::ONE, Limb::ZERO);
159        assert_eq!(res, U128::ZERO);
160        assert_eq!(borrow, Limb::ZERO);
161    }
162
163    #[test]
164    fn sbb_with_borrow() {
165        let (res, borrow) = U128::ZERO.sbb(&U128::ONE, Limb::ZERO);
166
167        assert_eq!(res, U128::MAX);
168        assert_eq!(borrow, Limb::MAX);
169    }
170
171    #[test]
172    fn wrapping_sub_no_borrow() {
173        assert_eq!(U128::ONE.wrapping_sub(&U128::ONE), U128::ZERO);
174    }
175
176    #[test]
177    fn wrapping_sub_with_borrow() {
178        assert_eq!(U128::ZERO.wrapping_sub(&U128::ONE), U128::MAX);
179    }
180
181    #[test]
182    fn checked_sub_ok() {
183        let result = U128::ONE.checked_sub(&U128::ONE);
184        assert_eq!(result.unwrap(), U128::ZERO);
185    }
186
187    #[test]
188    fn checked_sub_overflow() {
189        let result = U128::ZERO.checked_sub(&U128::ONE);
190        assert!(!bool::from(result.is_some()));
191    }
192}