Eliminate some additions of zero

This commit is contained in:
Eirik Ogilvie-Wigley 2018-09-05 17:06:42 -06:00
parent 4fd3ca135b
commit 7ff7477e12
1 changed files with 25 additions and 11 deletions

View File

@ -80,6 +80,20 @@ fn sbb(a: u64, b: u64, borrow: &mut u128) -> u64 {
*borrow as u64
}
/// Compute (b * c), returning the result and the carry over.
#[inline(always)]
fn overflowing_mul(b: u64, c: u64) -> (u64, u64) {
let mac = u128::from(b) * u128::from(c);
(mac as u64, (mac >> 64) as u64)
}
/// Compute a + (b * c), returning the result and the carry over.
#[inline(always)]
fn mac(a: u64, b: u64, c: u64) -> (u64, u64) {
let mac = u128::from(a) + (u128::from(b) * u128::from(c));
(mac as u64, (mac >> 64) as u64)
}
/// Compute a + (b * c) + carry, returning the result and the new carry over.
#[inline(always)]
fn mac_with_carry(a: u64, b: u64, c: u64, carry: u64) -> (u64, u64) {
@ -143,22 +157,22 @@ impl<'b> MulAssign<&'b Fq> for Fq {
fn mul_assign(&mut self, rhs: &'b Fq) {
// Schoolbook multiplication
let (r0, carry) = mac_with_carry(0, self.0[0], rhs.0[0], 0);
let (r1, carry) = mac_with_carry(0, self.0[0], rhs.0[1], carry);
let (r2, carry) = mac_with_carry(0, self.0[0], rhs.0[2], carry);
let (r3, r4) = mac_with_carry(0, self.0[0], rhs.0[3], carry);
let (r0, carry) = overflowing_mul(self.0[0], rhs.0[0]);
let (r1, carry) = mac(carry, self.0[0], rhs.0[1]);
let (r2, carry) = mac(carry, self.0[0], rhs.0[2]);
let (r3, r4) = mac(carry, self.0[0], rhs.0[3]);
let (r1, carry) = mac_with_carry(r1, self.0[1], rhs.0[0], 0);
let (r1, carry) = mac(r1, self.0[1], rhs.0[0]);
let (r2, carry) = mac_with_carry(r2, self.0[1], rhs.0[1], carry);
let (r3, carry) = mac_with_carry(r3, self.0[1], rhs.0[2], carry);
let (r4, r5) = mac_with_carry(r4, self.0[1], rhs.0[3], carry);
let (r2, carry) = mac_with_carry(r2, self.0[2], rhs.0[0], 0);
let (r2, carry) = mac(r2, self.0[2], rhs.0[0]);
let (r3, carry) = mac_with_carry(r3, self.0[2], rhs.0[1], carry);
let (r4, carry) = mac_with_carry(r4, self.0[2], rhs.0[2], carry);
let (r5, r6) = mac_with_carry(r5, self.0[2], rhs.0[3], carry);
let (r3, carry) = mac_with_carry(r3, self.0[3], rhs.0[0], 0);
let (r3, carry) = mac(r3, self.0[3], rhs.0[0]);
let (r4, carry) = mac_with_carry(r4, self.0[3], rhs.0[1], carry);
let (r5, carry) = mac_with_carry(r5, self.0[3], rhs.0[2], carry);
let (r6, r7) = mac_with_carry(r6, self.0[3], rhs.0[3], carry);
@ -299,28 +313,28 @@ impl Fq {
// <http://cacr.uwaterloo.ca/hac/about/chap14.pdf>.
let k = r0.wrapping_mul(INV);
let (_, carry) = mac_with_carry(r0, k, MODULUS.0[0], 0);
let (_, carry) = mac(r0, k, MODULUS.0[0]);
let (r1, carry) = mac_with_carry(r1, k, MODULUS.0[1], carry);
let (r2, carry) = mac_with_carry(r2, k, MODULUS.0[2], carry);
let (r3, carry) = mac_with_carry(r3, k, MODULUS.0[3], carry);
let (r4, carry2) = adc2(r4, 0, carry);
let k = r1.wrapping_mul(INV);
let (_, carry) = mac_with_carry(r1, k, MODULUS.0[0], 0);
let (_, carry) = mac(r1, k, MODULUS.0[0]);
let (r2, carry) = mac_with_carry(r2, k, MODULUS.0[1], carry);
let (r3, carry) = mac_with_carry(r3, k, MODULUS.0[2], carry);
let (r4, carry) = mac_with_carry(r4, k, MODULUS.0[3], carry);
let (r5, carry2) = adc2(r5, carry2, carry);
let k = r2.wrapping_mul(INV);
let (_, carry) = mac_with_carry(r2, k, MODULUS.0[0], 0);
let (_, carry) = mac(r2, k, MODULUS.0[0]);
let (r3, carry) = mac_with_carry(r3, k, MODULUS.0[1], carry);
let (r4, carry) = mac_with_carry(r4, k, MODULUS.0[2], carry);
let (r5, carry) = mac_with_carry(r5, k, MODULUS.0[3], carry);
let (r6, carry2) = adc2(r6, carry2, carry);
let k = r3.wrapping_mul(INV);
let (_, carry) = mac_with_carry(r3, k, MODULUS.0[0], 0);
let (_, carry) = mac(r3, k, MODULUS.0[0]);
let (r4, carry) = mac_with_carry(r4, k, MODULUS.0[1], carry);
let (r5, carry) = mac_with_carry(r5, k, MODULUS.0[2], carry);
let (r6, carry) = mac_with_carry(r6, k, MODULUS.0[3], carry);