From 7b7799eab7d1ee1888bf7ce1978a7c8675ab228e Mon Sep 17 00:00:00 2001 From: Sam Cappleman-Lynes Date: Wed, 28 Jun 2017 16:24:56 +0100 Subject: [PATCH] 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. --- bigint/src/biguint.rs | 10 ++++++++++ bigint/src/tests/biguint.rs | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/bigint/src/biguint.rs b/bigint/src/biguint.rs index 2f5f462..5d71465 100644 --- a/bigint/src/biguint.rs +++ b/bigint/src/biguint.rs @@ -437,6 +437,16 @@ impl<'a> Sub for &'a BigUint { } } +impl Sub 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); impl<'a, 'b> Mul<&'b BigUint> for &'a BigUint { diff --git a/bigint/src/tests/biguint.rs b/bigint/src/tests/biguint.rs index 31ac79b..f5c226f 100644 --- a/bigint/src/tests/biguint.rs +++ b/bigint/src/tests/biguint.rs @@ -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] #[should_panic] fn test_sub_fail_on_underflow() {