diff --git a/bigint/src/biguint.rs b/bigint/src/biguint.rs index 82e1483..0bfc158 100644 --- a/bigint/src/biguint.rs +++ b/bigint/src/biguint.rs @@ -30,7 +30,7 @@ use self::algorithms::{mac_with_carry, mul3, scalar_mul, div_rem, div_rem_digit} use self::algorithms::{__add2, add2, sub2, sub2rev}; use self::algorithms::{biguint_shl, biguint_shr}; use self::algorithms::{cmp_slice, fls, ilog2}; -use self::monty::{MontyReducer, monty_modpow}; +use self::monty::monty_modpow; use UsizePromotion; @@ -1625,8 +1625,7 @@ impl BigUint { /// Returns `(self ^ exponent) % modulus`. pub fn modpow(&self, exponent: &Self, modulus: &Self) -> Self { - let mr = MontyReducer::new(modulus); - monty_modpow(self, exponent, &mr) + monty_modpow(self, exponent, modulus) } } diff --git a/bigint/src/monty.rs b/bigint/src/monty.rs index 422201e..a9ef305 100644 --- a/bigint/src/monty.rs +++ b/bigint/src/monty.rs @@ -4,7 +4,7 @@ use traits::{Zero, One}; use biguint::BigUint; -pub struct MontyReducer<'a> { +struct MontyReducer<'a> { p: &'a BigUint, n: Vec, n0inv: u64 @@ -52,7 +52,7 @@ fn inv_mod_u32(num: u32) -> u64 { } impl<'a> MontyReducer<'a> { - pub fn new(p: &'a BigUint) -> Self { + fn new(p: &'a BigUint) -> Self { let n : Vec = p.data.clone(); let n0inv = inv_mod_u32(n[0]); MontyReducer { p: p, n: n, n0inv: n0inv } @@ -63,7 +63,7 @@ impl<'a> MontyReducer<'a> { // // Reference: // Brent & Zimmermann, Modern Computer Arithmetic, v0.5.9, Algorithm 2.6 -pub fn monty_redc(a: BigUint, mr: &MontyReducer) -> BigUint { +fn monty_redc(a: BigUint, mr: &MontyReducer) -> BigUint { let mut c = a.data; let n = &mr.n; let n_size = n.len(); @@ -128,7 +128,9 @@ fn monty_sqr(a: BigUint, mr: &MontyReducer) -> BigUint { monty_redc(&a * &a, mr) } -pub fn monty_modpow(a: &BigUint, exp: &BigUint, mr: &MontyReducer) -> BigUint{ +pub fn monty_modpow(a: &BigUint, exp: &BigUint, modulus: &BigUint) -> BigUint{ + let mr = MontyReducer::new(modulus); + // Calculate the Montgomery parameter let mut r : BigUint = One::one(); while &r < mr.p { @@ -144,12 +146,12 @@ pub fn monty_modpow(a: &BigUint, exp: &BigUint, mr: &MontyReducer) -> BigUint{ let zero = Zero::zero(); while e > zero { if e.is_odd() { - ans = monty_mult(ans, &apri, mr); + ans = monty_mult(ans, &apri, &mr); } - apri = monty_sqr(apri, mr); + apri = monty_sqr(apri, &mr); e = e >> 1; } // Map the result back to the residues domain - monty_redc(ans, mr) + monty_redc(ans, &mr) }