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() {