From 5a0de140c961f2f96b830f0600e5a3d69a1d0a12 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Sun, 22 Oct 2017 15:30:17 -0700 Subject: [PATCH] bigint::monty: use mac_digit --- bigint/src/algorithms.rs | 2 +- bigint/src/monty.rs | 18 +----------------- 2 files changed, 2 insertions(+), 18 deletions(-) diff --git a/bigint/src/algorithms.rs b/bigint/src/algorithms.rs index de99ccc..5b74539 100644 --- a/bigint/src/algorithms.rs +++ b/bigint/src/algorithms.rs @@ -220,7 +220,7 @@ pub fn sub_sign(a: &[BigDigit], b: &[BigDigit]) -> (Sign, BigUint) { /// Three argument multiply accumulate: /// acc += b * c -fn mac_digit(acc: &mut [BigDigit], b: &[BigDigit], c: BigDigit) { +pub fn mac_digit(acc: &mut [BigDigit], b: &[BigDigit], c: BigDigit) { if c == 0 { return; } diff --git a/bigint/src/monty.rs b/bigint/src/monty.rs index da352ed..d522df5 100644 --- a/bigint/src/monty.rs +++ b/bigint/src/monty.rs @@ -73,27 +73,11 @@ fn monty_redc(a: BigUint, mr: &MontyReducer) -> BigUint { // 1: for i = 0 to (n-1) for i in 0..n_size { - // Carry storage - let mut carry = 0; - // 2: q_i <- mu*c_i mod β let q_i = ((c[i] as u64) * mu) & beta_mask; // 3: C <- C + q_i * N * β^i - // When iterating over each word, this becomes: - for j in 0..n_size { - // c_(i+j) <- c_(i+j) + q_i * n_j - let x = (c[i+j] as u64) + q_i * (n[j] as u64) + carry; - c[i+j] = (x & beta_mask) as u32; - carry = x >> 32; - } - - // Apply the remaining carry to the rest of the work space - for j in n_size..2*n_size-i+2 { - let x = (c[i+j] as u64) + carry; - c[i+j] = (x & beta_mask) as u32; - carry = x >> 32; - } + super::algorithms::mac_digit(&mut c[i..], n, q_i as u32); } // 4: R <- C * β^(-n)