Scalar subtraction of a BigDigit from a BigUint

A BigDigit can be subtracted from a BigUint - this is one of several
operations being implemented to allow scalar operations on BigInt and
BigUint across the board.
This commit is contained in:
Sam Cappleman-Lynes 2017-06-28 16:24:56 +01:00
parent a2a28c682e
commit 7b7799eab7
2 changed files with 31 additions and 0 deletions

View File

@ -437,6 +437,16 @@ impl<'a> Sub<BigUint> for &'a BigUint {
} }
} }
impl Sub<BigDigit> for BigUint {
type Output = BigUint;
#[inline]
fn sub(mut self, other: BigDigit) -> BigUint {
sub2(&mut self.data[..], &[other]);
self.normalize()
}
}
forward_all_binop_to_ref_ref!(impl Mul for BigUint, mul); forward_all_binop_to_ref_ref!(impl Mul for BigUint, mul);
impl<'a, 'b> Mul<&'b BigUint> for &'a BigUint { impl<'a, 'b> Mul<&'b BigUint> for &'a BigUint {

View File

@ -723,6 +723,27 @@ fn test_sub() {
} }
} }
#[test]
fn test_scalar_sub() {
for elm in SUM_TRIPLES.iter() {
let (a_vec, b_vec, c_vec) = *elm;
if a_vec.len() == 1 {
let a = a_vec[0];
let b = BigUint::from_slice(b_vec);
let c = BigUint::from_slice(c_vec);
assert!(c - a == b);
}
if b_vec.len() == 1 {
let a = BigUint::from_slice(a_vec);
let b = b_vec[0];
let c = BigUint::from_slice(c_vec);
assert!(c - b == a);
}
}
}
#[test] #[test]
#[should_panic] #[should_panic]
fn test_sub_fail_on_underflow() { fn test_sub_fail_on_underflow() {