-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathsub.rs
271 lines (220 loc) · 7.09 KB
/
sub.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//! [`BoxedUint`] subtraction operations.
use crate::{BoxedUint, CheckedSub, Limb, Uint, Wrapping, WrappingSub, Zero, U128, U64};
use core::ops::{Sub, SubAssign};
use subtle::{Choice, ConditionallySelectable, CtOption};
impl BoxedUint {
/// Computes `a - (b + borrow)`, returning the result along with the new borrow.
#[inline(always)]
pub fn sbb(&self, rhs: &Self, borrow: Limb) -> (Self, Limb) {
Self::fold_limbs(self, rhs, borrow, |a, b, c| a.sbb(b, c))
}
/// Computes `a - (b + borrow)` in-place, returning the new borrow.
///
/// Panics if `rhs` has a larger precision than `self`.
#[inline(always)]
pub fn sbb_assign(&mut self, rhs: impl AsRef<[Limb]>, mut borrow: Limb) -> Limb {
debug_assert!(self.bits_precision() <= (rhs.as_ref().len() as u32 * Limb::BITS));
for i in 0..self.nlimbs() {
let (limb, b) = self.limbs[i].sbb(*rhs.as_ref().get(i).unwrap_or(&Limb::ZERO), borrow);
self.limbs[i] = limb;
borrow = b;
}
borrow
}
/// Perform wrapping subtraction, discarding overflow.
pub fn wrapping_sub(&self, rhs: &Self) -> Self {
self.sbb(rhs, Limb::ZERO).0
}
/// Perform in-place wrapping subtraction, returning the truthy value as the second element of
/// the tuple if an underflow has occurred.
pub(crate) fn conditional_sbb_assign(&mut self, rhs: &Self, choice: Choice) -> Choice {
debug_assert!(self.bits_precision() <= rhs.bits_precision());
let mask = Limb::conditional_select(&Limb::ZERO, &Limb::MAX, choice);
let mut borrow = Limb::ZERO;
for i in 0..self.nlimbs() {
let masked_rhs = *rhs.limbs.get(i).unwrap_or(&Limb::ZERO) & mask;
let (limb, b) = self.limbs[i].sbb(masked_rhs, borrow);
self.limbs[i] = limb;
borrow = b;
}
Choice::from((borrow.0 & 1) as u8)
}
}
impl CheckedSub for BoxedUint {
fn checked_sub(&self, rhs: &Self) -> CtOption<Self> {
let (result, carry) = self.sbb(rhs, Limb::ZERO);
CtOption::new(result, carry.is_zero())
}
}
impl Sub for BoxedUint {
type Output = Self;
fn sub(self, rhs: Self) -> Self {
self.sub(&rhs)
}
}
impl Sub<&BoxedUint> for BoxedUint {
type Output = Self;
fn sub(self, rhs: &Self) -> Self {
Sub::sub(&self, rhs)
}
}
impl Sub<BoxedUint> for &BoxedUint {
type Output = BoxedUint;
fn sub(self, rhs: BoxedUint) -> BoxedUint {
Sub::sub(self, &rhs)
}
}
impl Sub<&BoxedUint> for &BoxedUint {
type Output = BoxedUint;
fn sub(self, rhs: &BoxedUint) -> BoxedUint {
self.checked_sub(rhs)
.expect("attempted to subtract with underflow")
}
}
impl SubAssign<BoxedUint> for BoxedUint {
fn sub_assign(&mut self, rhs: BoxedUint) {
self.sub_assign(&rhs)
}
}
impl SubAssign<&BoxedUint> for BoxedUint {
fn sub_assign(&mut self, rhs: &BoxedUint) {
let carry = self.sbb_assign(rhs, Limb::ZERO);
assert_eq!(
carry.is_zero().unwrap_u8(),
1,
"attempted to subtract with underflow"
);
}
}
impl<const LIMBS: usize> Sub<Uint<LIMBS>> for BoxedUint {
type Output = BoxedUint;
fn sub(self, rhs: Uint<LIMBS>) -> BoxedUint {
self.sub(&rhs)
}
}
impl<const LIMBS: usize> Sub<&Uint<LIMBS>> for BoxedUint {
type Output = BoxedUint;
fn sub(mut self, rhs: &Uint<LIMBS>) -> BoxedUint {
self -= rhs;
self
}
}
impl<const LIMBS: usize> Sub<Uint<LIMBS>> for &BoxedUint {
type Output = BoxedUint;
fn sub(self, rhs: Uint<LIMBS>) -> BoxedUint {
self.clone().sub(rhs)
}
}
impl<const LIMBS: usize> Sub<&Uint<LIMBS>> for &BoxedUint {
type Output = BoxedUint;
fn sub(self, rhs: &Uint<LIMBS>) -> BoxedUint {
self.clone().sub(rhs)
}
}
impl<const LIMBS: usize> SubAssign<Uint<LIMBS>> for BoxedUint {
fn sub_assign(&mut self, rhs: Uint<LIMBS>) {
*self -= &rhs;
}
}
impl<const LIMBS: usize> SubAssign<&Uint<LIMBS>> for BoxedUint {
fn sub_assign(&mut self, rhs: &Uint<LIMBS>) {
let carry = self.sbb_assign(rhs.as_limbs(), Limb::ZERO);
assert_eq!(carry.0, 0, "attempted to sub with overflow");
}
}
impl SubAssign<Wrapping<BoxedUint>> for Wrapping<BoxedUint> {
fn sub_assign(&mut self, other: Wrapping<BoxedUint>) {
self.0.sbb_assign(&other.0, Limb::ZERO);
}
}
impl SubAssign<&Wrapping<BoxedUint>> for Wrapping<BoxedUint> {
fn sub_assign(&mut self, other: &Wrapping<BoxedUint>) {
self.0.sbb_assign(&other.0, Limb::ZERO);
}
}
impl WrappingSub for BoxedUint {
fn wrapping_sub(&self, v: &Self) -> Self {
self.wrapping_sub(v)
}
}
macro_rules! impl_sub_primitive {
($($primitive:ty),+) => {
$(
impl Sub<$primitive> for BoxedUint {
type Output = BoxedUint;
#[inline]
fn sub(self, rhs: $primitive) -> BoxedUint {
self - U64::from(rhs)
}
}
impl Sub<$primitive> for &BoxedUint {
type Output = BoxedUint;
#[inline]
fn sub(self, rhs: $primitive) -> BoxedUint {
self - U64::from(rhs)
}
}
impl SubAssign<$primitive> for BoxedUint {
fn sub_assign(&mut self, rhs: $primitive) {
*self -= U64::from(rhs);
}
}
)+
};
}
impl_sub_primitive!(u8, u16, u32, u64);
impl Sub<u128> for BoxedUint {
type Output = BoxedUint;
#[inline]
fn sub(self, rhs: u128) -> BoxedUint {
self - U128::from(rhs)
}
}
impl Sub<u128> for &BoxedUint {
type Output = BoxedUint;
#[inline]
fn sub(self, rhs: u128) -> BoxedUint {
self - U128::from(rhs)
}
}
impl SubAssign<u128> for BoxedUint {
fn sub_assign(&mut self, rhs: u128) {
*self -= U128::from(rhs);
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::{BoxedUint, CheckedSub, Limb};
#[test]
fn sbb_no_borrow() {
let (res, carry) = BoxedUint::one().sbb(&BoxedUint::one(), Limb::ZERO);
assert_eq!(res, BoxedUint::zero());
assert_eq!(carry, Limb::ZERO);
let (res, carry) = BoxedUint::one().sbb(&BoxedUint::zero(), Limb::ZERO);
assert_eq!(res, BoxedUint::one());
assert_eq!(carry, Limb::ZERO);
}
#[test]
fn sbb_with_borrow() {
let (res, borrow) = BoxedUint::zero().sbb(&BoxedUint::one(), Limb::ZERO);
assert_eq!(res, BoxedUint::max(Limb::BITS));
assert_eq!(borrow, Limb::MAX);
}
#[test]
fn sub_with_u32_rhs() {
let a = BoxedUint::from(0x100000000u64);
let b = a - u32::MAX;
assert_eq!(b, BoxedUint::one());
}
#[test]
fn checked_sub_ok() {
let result = BoxedUint::one().checked_sub(&BoxedUint::one());
assert_eq!(result.unwrap(), BoxedUint::zero());
}
#[test]
fn checked_sub_overflow() {
let result = BoxedUint::zero().checked_sub(&BoxedUint::one());
assert!(!bool::from(result.is_some()));
}
}