bigint: less pub in monty

This commit is contained in:
Josh Stone 2017-10-22 15:05:16 -07:00
parent 2a1fe6e7ef
commit c2fba06787
2 changed files with 11 additions and 10 deletions

View File

@ -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::{__add2, add2, sub2, sub2rev};
use self::algorithms::{biguint_shl, biguint_shr}; use self::algorithms::{biguint_shl, biguint_shr};
use self::algorithms::{cmp_slice, fls, ilog2}; use self::algorithms::{cmp_slice, fls, ilog2};
use self::monty::{MontyReducer, monty_modpow}; use self::monty::monty_modpow;
use UsizePromotion; use UsizePromotion;
@ -1625,8 +1625,7 @@ impl BigUint {
/// Returns `(self ^ exponent) % modulus`. /// Returns `(self ^ exponent) % modulus`.
pub fn modpow(&self, exponent: &Self, modulus: &Self) -> Self { pub fn modpow(&self, exponent: &Self, modulus: &Self) -> Self {
let mr = MontyReducer::new(modulus); monty_modpow(self, exponent, modulus)
monty_modpow(self, exponent, &mr)
} }
} }

View File

@ -4,7 +4,7 @@ use traits::{Zero, One};
use biguint::BigUint; use biguint::BigUint;
pub struct MontyReducer<'a> { struct MontyReducer<'a> {
p: &'a BigUint, p: &'a BigUint,
n: Vec<u32>, n: Vec<u32>,
n0inv: u64 n0inv: u64
@ -52,7 +52,7 @@ fn inv_mod_u32(num: u32) -> u64 {
} }
impl<'a> MontyReducer<'a> { impl<'a> MontyReducer<'a> {
pub fn new(p: &'a BigUint) -> Self { fn new(p: &'a BigUint) -> Self {
let n : Vec<u32> = p.data.clone(); let n : Vec<u32> = p.data.clone();
let n0inv = inv_mod_u32(n[0]); let n0inv = inv_mod_u32(n[0]);
MontyReducer { p: p, n: n, n0inv: n0inv } MontyReducer { p: p, n: n, n0inv: n0inv }
@ -63,7 +63,7 @@ impl<'a> MontyReducer<'a> {
// //
// Reference: // Reference:
// Brent & Zimmermann, Modern Computer Arithmetic, v0.5.9, Algorithm 2.6 // 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 mut c = a.data;
let n = &mr.n; let n = &mr.n;
let n_size = n.len(); let n_size = n.len();
@ -128,7 +128,9 @@ fn monty_sqr(a: BigUint, mr: &MontyReducer) -> BigUint {
monty_redc(&a * &a, mr) 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 // Calculate the Montgomery parameter
let mut r : BigUint = One::one(); let mut r : BigUint = One::one();
while &r < mr.p { while &r < mr.p {
@ -144,12 +146,12 @@ pub fn monty_modpow(a: &BigUint, exp: &BigUint, mr: &MontyReducer) -> BigUint{
let zero = Zero::zero(); let zero = Zero::zero();
while e > zero { while e > zero {
if e.is_odd() { 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; e = e >> 1;
} }
// Map the result back to the residues domain // Map the result back to the residues domain
monty_redc(ans, mr) monty_redc(ans, &mr)
} }