From a2a28c682e0c677e553723494e4428fa1e88c651 Mon Sep 17 00:00:00 2001 From: Sam Cappleman-Lynes Date: Wed, 28 Jun 2017 15:38:48 +0100 Subject: [PATCH] Scalar addition of BigDigit to BigUint A BigDigit can be added to 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 | 17 +++++++++++++++++ bigint/src/tests/biguint.rs | 20 ++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/bigint/src/biguint.rs b/bigint/src/biguint.rs index a5b74d0..2f5f462 100644 --- a/bigint/src/biguint.rs +++ b/bigint/src/biguint.rs @@ -394,6 +394,23 @@ impl<'a> Add<&'a BigUint> for BigUint { } } +impl Add for BigUint { + type Output = BigUint; + + #[inline] + fn add(mut self, other: BigDigit) -> BigUint { + if self.data.len() == 0 { + self.data.push(0); + } + + let carry = __add2(&mut self.data, &[other]); + if carry != 0 { + self.data.push(carry); + } + self + } +} + forward_val_val_binop!(impl Sub for BigUint, sub); forward_ref_ref_binop!(impl Sub for BigUint, sub); diff --git a/bigint/src/tests/biguint.rs b/bigint/src/tests/biguint.rs index ac95235..31ac79b 100644 --- a/bigint/src/tests/biguint.rs +++ b/bigint/src/tests/biguint.rs @@ -690,6 +690,26 @@ fn test_add() { } } +#[test] +fn test_scalar_add() { + for elm in SUM_TRIPLES.iter() { + let (a_vec, b_vec, c_vec) = *elm; + let c = BigUint::from_slice(c_vec); + + if a_vec.len() == 1 { + let a = a_vec[0]; + let b = BigUint::from_slice(b_vec); + assert!(b + a == c); + } + + if b_vec.len() == 1 { + let a = BigUint::from_slice(a_vec); + let b = b_vec[0]; + assert!(a + b == c); + } + } +} + #[test] fn test_sub() { for elm in SUM_TRIPLES.iter() {