aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlloyd <[email protected]>2012-06-15 20:05:04 +0000
committerlloyd <[email protected]>2012-06-15 20:05:04 +0000
commit105d9add3baa1d69a6331de8a91c7ebe904a6c0e (patch)
treeb420f97ff2a688863a87930690317f99147bd6c5
parenteaca7a7c4e57b697f14c021254bc2787c1f6b500 (diff)
Computing the Montgomery parameter can be done much cheaper because we
only need the low word of the result. Credits to HAC, somehow I missed that this was possible. This helps especially when a program does a lot of setups, so the improvement is only minor on the benchmark but fairly huge for asio_tls_server.
-rw-r--r--src/math/numbertheory/powm_mnt.cpp7
1 files changed, 4 insertions, 3 deletions
diff --git a/src/math/numbertheory/powm_mnt.cpp b/src/math/numbertheory/powm_mnt.cpp
index 0db5455a7..62df84da5 100644
--- a/src/math/numbertheory/powm_mnt.cpp
+++ b/src/math/numbertheory/powm_mnt.cpp
@@ -116,7 +116,7 @@ BigInt Montgomery_Exponentiator::execute() const
* Montgomery_Exponentiator Constructor
*/
Montgomery_Exponentiator::Montgomery_Exponentiator(const BigInt& mod,
- Power_Mod::Usage_Hints hints)
+ Power_Mod::Usage_Hints hints)
{
// Montgomery reduction only works for positive odd moduli
if(!mod.is_positive() || mod.is_even())
@@ -128,9 +128,10 @@ Montgomery_Exponentiator::Montgomery_Exponentiator(const BigInt& mod,
mod_words = modulus.sig_words();
- BigInt r(BigInt::Power2, mod_words * BOTAN_MP_WORD_BITS);
- mod_prime = (((r * inverse_mod(r, mod)) - 1) / mod).word_at(0);
+ const BigInt b = BigInt(1) << BOTAN_MP_WORD_BITS;
+ mod_prime = (b - inverse_mod(modulus.word_at(0), b)).word_at(0);
+ const BigInt r(BigInt::Power2, mod_words * BOTAN_MP_WORD_BITS);
R_mod = r % modulus;
R2 = (R_mod * R_mod) % modulus;