From bd0ddd848bf9e88b96efa653888b6486300e5512 Mon Sep 17 00:00:00 2001 From: Adam Wick Date: Tue, 1 May 2018 23:04:06 -0700 Subject: [PATCH] A very slightly faster modexp. --- src/cryptonum/unsigned.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/cryptonum/unsigned.rs b/src/cryptonum/unsigned.rs index 3aeccdf..83b5c36 100644 --- a/src/cryptonum/unsigned.rs +++ b/src/cryptonum/unsigned.rs @@ -165,21 +165,23 @@ impl UCN { pub fn fastmodexp(&self, e: &UCN, mu: &BarrettUCN) -> UCN { let mut b = self.reduce(&mu); - let mut eprime = e.clone(); let mut result = UCN::from(1 as u8); - loop { - if eprime.is_zero() { - return result; - } + for digit in e.contents.iter() { + let mut work = *digit; - if eprime.is_odd() { - result = (result * &b).reduce(&mu); - } + for _ in 0..64 { + if (work & 0x1) == 1 { + result = (result * &b).reduce(&mu); + } - b = (&b * &b).reduce(&mu); - eprime >>= 1; + b = (&b * &b).reduce(&mu); + + work >>= 1; + } } + + result } pub fn to_bytes(&self, len: usize) -> Vec {