bigint::monty: deduplicate mr.n and mr.p

This commit is contained in:
Josh Stone 2017-10-22 15:45:01 -07:00
parent b380880ed3
commit bb0c9324b2
1 changed files with 10 additions and 12 deletions

View File

@ -4,8 +4,7 @@ use traits::Zero;
use biguint::BigUint; use biguint::BigUint;
struct MontyReducer<'a> { struct MontyReducer<'a> {
p: &'a BigUint, n: &'a BigUint,
n: Vec<u32>,
n0inv: u32 n0inv: u32
} }
@ -46,10 +45,9 @@ fn inv_mod_u32(num: u32) -> u32 {
} }
impl<'a> MontyReducer<'a> { impl<'a> MontyReducer<'a> {
fn new(p: &'a BigUint) -> Self { fn new(n: &'a BigUint) -> Self {
let n : Vec<u32> = p.data.clone(); let n0inv = inv_mod_u32(n.data[0]);
let n0inv = inv_mod_u32(n[0]); MontyReducer { n: n, n0inv: n0inv }
MontyReducer { p: p, n: n, n0inv: n0inv }
} }
} }
@ -59,7 +57,7 @@ impl<'a> MontyReducer<'a> {
// Brent & Zimmermann, Modern Computer Arithmetic, v0.5.9, Algorithm 2.6 // Brent & Zimmermann, Modern Computer Arithmetic, v0.5.9, Algorithm 2.6
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.data;
let n_size = n.len(); let n_size = n.len();
// Allocate sufficient work space // Allocate sufficient work space
@ -84,10 +82,10 @@ fn monty_redc(a: BigUint, mr: &MontyReducer) -> BigUint {
let ret = BigUint::new(c[n_size..].to_vec()); let ret = BigUint::new(c[n_size..].to_vec());
// 5: if R >= β^n then return R-N else return R. // 5: if R >= β^n then return R-N else return R.
if &ret < mr.p { if &ret < mr.n {
ret ret
} else { } else {
ret - mr.p ret - mr.n
} }
} }
@ -106,15 +104,15 @@ pub fn monty_modpow(a: &BigUint, exp: &BigUint, modulus: &BigUint) -> BigUint{
let mr = MontyReducer::new(modulus); let mr = MontyReducer::new(modulus);
// Calculate the Montgomery parameter // Calculate the Montgomery parameter
let mut v = vec![0; mr.p.data.len()]; let mut v = vec![0; modulus.data.len()];
v.push(1); v.push(1);
let r = BigUint::new(v); let r = BigUint::new(v);
// Map the base to the Montgomery domain // Map the base to the Montgomery domain
let mut apri = a * &r % mr.p; let mut apri = a * &r % modulus;
// Binary exponentiation // Binary exponentiation
let mut ans = &r % mr.p; let mut ans = &r % modulus;
let mut e = exp.clone(); let mut e = exp.clone();
while !e.is_zero() { while !e.is_zero() {
if e.is_odd() { if e.is_odd() {