Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e520bda
Add scalar multiplication to BigUint, BigInt
feadoor Oct 24, 2016
a2a28c6
Scalar addition of BigDigit to BigUint
feadoor Jun 28, 2017
7b7799e
Scalar subtraction of a BigDigit from a BigUint
feadoor Jun 28, 2017
530e2f6
Fix typo in comment in division algorithm
feadoor Jun 28, 2017
784d26b
Scalar division of a BigUint by a BigDigit
feadoor Jun 28, 2017
e5ed503
Implement all variants of adding BigDigit to BigUint
feadoor Jun 29, 2017
fd2f516
All variants of multiplying BigUint by BigDigit
feadoor Jun 29, 2017
5738141
Distinction for commutative scalar ops
feadoor Jun 29, 2017
51408a9
All variants of subtracting BigDigit from BigUint
feadoor Jun 29, 2017
d0bfb54
All variants of dividing BigUint by BigDigit
feadoor Jun 29, 2017
1e26bdd
Remove unnecessary normalization
feadoor Jun 29, 2017
80feea2
Also implement scalar addition for BigInt
feadoor Jun 29, 2017
79448cb
Add scalar subtraction to BigInt
feadoor Jun 29, 2017
8b1288e
Add scalar multiplication to BigInt
feadoor Jun 29, 2017
9b0392d
Add scalar division to BigInt
feadoor Jun 29, 2017
94d5706
Add operations on i32 to BigInt
feadoor Jun 29, 2017
99873d0
Scalar operations on integer types up to 32 bits
feadoor Jun 29, 2017
fd87d87
Fix normalization in scalar addition
feadoor Jun 29, 2017
2a3cd41
Add scalar ops for all remaining integer types
feadoor Jun 29, 2017
1fb03ca
Make new code work on rustc-1.8.0
feadoor Jun 29, 2017
18cc190
inline i32_abs_as_u32 and i64_abs_as_u64
cuviper Jul 12, 2017
18a5bfc
fix endianness of to/from_doublebigdigit calls
cuviper Jul 12, 2017
6afac82
test and fix more scalar add cases
cuviper Jul 12, 2017
e5434dc
Add assert_scalar_op! for DRYer testing
cuviper Jul 12, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion bigint/src/algorithms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ pub fn mac_with_carry(a: BigDigit, b: BigDigit, c: BigDigit, carry: &mut BigDigi
lo
}

#[inline]
pub fn mul_with_carry(a: BigDigit, b: BigDigit, carry: &mut BigDigit) -> BigDigit {
let (hi, lo) = big_digit::from_doublebigdigit((a as DoubleBigDigit) * (b as DoubleBigDigit) +
(*carry as DoubleBigDigit));

*carry = hi;
lo
}

/// Divide a two digit numerator by a one digit divisor, returns quotient and remainder:
///
/// Note: the caller must ensure that both the quotient and remainder will fit into a single digit.
Expand Down Expand Up @@ -377,6 +386,14 @@ pub fn mul3(x: &[BigDigit], y: &[BigDigit]) -> BigUint {
prod.normalize()
}

pub fn scalar_mul(a: &mut [BigDigit], b: BigDigit) -> BigDigit {
let mut carry = 0;
for a in a.iter_mut() {
*a = mul_with_carry(*a, b, &mut carry);
}
carry
}

pub fn div_rem(u: &BigUint, d: &BigUint) -> (BigUint, BigUint) {
if d.is_zero() {
panic!()
Expand Down Expand Up @@ -416,7 +433,7 @@ pub fn div_rem(u: &BigUint, d: &BigUint) -> (BigUint, BigUint) {
// q0, our guess, is calculated by dividing the last few digits of a by the last digit of b
// - this should give us a guess that is "close" to the actual quotient, but is possibly
// greater than the actual quotient. If q0 * b > a, we simply use iterated subtraction
// until we have a guess such that q0 & b <= a.
// until we have a guess such that q0 * b <= a.
//

let bn = *b.data.last().unwrap();
Expand Down
Loading