diff options
author | lloyd <[email protected]> | 2010-03-09 01:29:12 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2010-03-09 01:29:12 +0000 |
commit | 56a19fb01f3b0651359f059b51eef26e408015b4 (patch) | |
tree | 6d7e100abf4792159227d88808e71e0803449a98 /src | |
parent | b98f0aa4ac768fb045e439915a8115434e5a91de (diff) |
Consolidate code for choosing a window size for fixed width window
exponentiation algorithms.
Diffstat (limited to 'src')
-rw-r--r-- | src/math/numbertheory/pow_mod.cpp | 32 | ||||
-rw-r--r-- | src/math/numbertheory/pow_mod.h | 7 | ||||
-rw-r--r-- | src/math/numbertheory/powm_fw.cpp | 40 | ||||
-rw-r--r-- | src/math/numbertheory/powm_mnt.cpp | 34 |
4 files changed, 41 insertions, 72 deletions
diff --git a/src/math/numbertheory/pow_mod.cpp b/src/math/numbertheory/pow_mod.cpp index e98364fea..96c978d68 100644 --- a/src/math/numbertheory/pow_mod.cpp +++ b/src/math/numbertheory/pow_mod.cpp @@ -111,6 +111,38 @@ BigInt Power_Mod::execute() const return core->execute(); } +/* +* Try to choose a good window size +*/ +u32bit Power_Mod::window_bits(u32bit exp_bits, u32bit base_bits, + Power_Mod::Usage_Hints hints) + { + static const u32bit wsize[][2] = { + { 2048, 7 }, { 1024, 6 }, { 256, 5 }, { 128, 4 }, { 64, 3 }, { 0, 0 } + }; + + u32bit window_bits = 1; + + if(exp_bits) + { + for(u32bit j = 0; wsize[j][0]; ++j) + { + if(exp_bits >= wsize[j][0]) + { + window_bits += wsize[j][1]; + break; + } + } + } + + if(hints & Power_Mod::BASE_IS_FIXED) + window_bits += 2; + if(hints & Power_Mod::EXP_IS_LARGE) + ++window_bits; + + return window_bits; + } + namespace { /* diff --git a/src/math/numbertheory/pow_mod.h b/src/math/numbertheory/pow_mod.h index 6952dcd1b..7b92f0ec4 100644 --- a/src/math/numbertheory/pow_mod.h +++ b/src/math/numbertheory/pow_mod.h @@ -31,6 +31,7 @@ class BOTAN_DLL Modular_Exponentiator class BOTAN_DLL Power_Mod { public: + enum Usage_Hints { NO_HINTS = 0x0000, @@ -44,6 +45,12 @@ class BOTAN_DLL Power_Mod EXP_IS_LARGE = 0x0400 }; + /* + * Try to choose a good window size + */ + static u32bit window_bits(u32bit exp_bits, u32bit base_bits, + Power_Mod::Usage_Hints hints); + void set_modulus(const BigInt&, Usage_Hints = NO_HINTS) const; void set_base(const BigInt&) const; void set_exponent(const BigInt&) const; diff --git a/src/math/numbertheory/powm_fw.cpp b/src/math/numbertheory/powm_fw.cpp index 1cfcdcd66..68dabc332 100644 --- a/src/math/numbertheory/powm_fw.cpp +++ b/src/math/numbertheory/powm_fw.cpp @@ -11,44 +11,6 @@ namespace Botan { -namespace { - -/* -* Try to choose a good window size -*/ -u32bit fw_powm_window_bits(u32bit exp_bits, u32bit, - Power_Mod::Usage_Hints hints) - { - static const u32bit wsize[][2] = { - { 2048, 7 }, { 1024, 6 }, { 256, 5 }, { 128, 4 }, { 64, 3 }, { 0, 0 } - }; - - u32bit window_bits = 3; - - if(exp_bits) - { - for(u32bit j = 0; wsize[j][0]; ++j) - { - if(exp_bits >= wsize[j][0]) - { - window_bits += wsize[j][1]; - break; - } - } - } - - if(hints & Power_Mod::EXP_IS_FIXED) - window_bits += 2; - if(hints & Power_Mod::EXP_IS_LARGE) - window_bits += 2; - if(hints & Power_Mod::BASE_IS_FIXED) - ++window_bits; - - return window_bits; - } - -} - /* * Set the exponent */ @@ -62,7 +24,7 @@ void Fixed_Window_Exponentiator::set_exponent(const BigInt& e) */ void Fixed_Window_Exponentiator::set_base(const BigInt& base) { - window_bits = fw_powm_window_bits(exp.bits(), base.bits(), hints); + window_bits = Power_Mod::window_bits(exp.bits(), base.bits(), hints); g.resize((1 << window_bits) - 1); g[0] = base; diff --git a/src/math/numbertheory/powm_mnt.cpp b/src/math/numbertheory/powm_mnt.cpp index 2d18ccdef..cce142020 100644 --- a/src/math/numbertheory/powm_mnt.cpp +++ b/src/math/numbertheory/powm_mnt.cpp @@ -14,38 +14,6 @@ namespace Botan { namespace { /* -* Try to choose a good window size -*/ -u32bit montgomery_powm_window_bits(u32bit exp_bits, u32bit, - Power_Mod::Usage_Hints hints) - { - static const u32bit wsize[][2] = { - { 2048, 4 }, { 1024, 3 }, { 256, 2 }, { 128, 1 }, { 0, 0 } - }; - - u32bit window_bits = 1; - - if(exp_bits) - { - for(u32bit j = 0; wsize[j][0]; ++j) - { - if(exp_bits >= wsize[j][0]) - { - window_bits += wsize[j][1]; - break; - } - } - } - - if(hints & Power_Mod::BASE_IS_FIXED) - window_bits += 2; - if(hints & Power_Mod::EXP_IS_LARGE) - ++window_bits; - - return window_bits; - } - -/* * Montgomery Reduction */ inline void montgomery_reduce(BigInt& out, MemoryRegion<word>& z_buf, @@ -76,7 +44,7 @@ void Montgomery_Exponentiator::set_exponent(const BigInt& exp) */ void Montgomery_Exponentiator::set_base(const BigInt& base) { - window_bits = montgomery_powm_window_bits(exp.bits(), base.bits(), hints); + window_bits = Power_Mod::window_bits(exp.bits(), base.bits(), hints); g.resize((1 << window_bits) - 1); |