diff options
author | lloyd <[email protected]> | 2014-01-01 21:20:55 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2014-01-01 21:20:55 +0000 |
commit | 197dc467dec28a04c3b2f30da7cef122dfbb13e9 (patch) | |
tree | cdbd3ddaec051c72f0a757db461973d90c37b97a /lib/math/numbertheory/powm_fw.cpp | |
parent | 62faac373c07cfe10bc8c309e89ebdd30d8e5eaa (diff) |
Shuffle things around. Add NIST X.509 test to build.
Diffstat (limited to 'lib/math/numbertheory/powm_fw.cpp')
-rw-r--r-- | lib/math/numbertheory/powm_fw.cpp | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/lib/math/numbertheory/powm_fw.cpp b/lib/math/numbertheory/powm_fw.cpp new file mode 100644 index 000000000..16a48a5b0 --- /dev/null +++ b/lib/math/numbertheory/powm_fw.cpp @@ -0,0 +1,69 @@ +/* +* Fixed Window Exponentiation +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/internal/def_powm.h> +#include <botan/numthry.h> +#include <vector> + +namespace Botan { + +/* +* Set the exponent +*/ +void Fixed_Window_Exponentiator::set_exponent(const BigInt& e) + { + exp = e; + } + +/* +* Set the base +*/ +void Fixed_Window_Exponentiator::set_base(const BigInt& base) + { + window_bits = Power_Mod::window_bits(exp.bits(), base.bits(), hints); + + g.resize((1 << window_bits)); + g[0] = 1; + g[1] = base; + + for(size_t i = 2; i != g.size(); ++i) + g[i] = reducer.multiply(g[i-1], g[0]); + } + +/* +* Compute the result +*/ +BigInt Fixed_Window_Exponentiator::execute() const + { + const size_t exp_nibbles = (exp.bits() + window_bits - 1) / window_bits; + + BigInt x = 1; + + for(size_t i = exp_nibbles; i > 0; --i) + { + for(size_t j = 0; j != window_bits; ++j) + x = reducer.square(x); + + const u32bit nibble = exp.get_substring(window_bits*(i-1), window_bits); + + x = reducer.multiply(x, g[nibble]); + } + return x; + } + +/* +* Fixed_Window_Exponentiator Constructor +*/ +Fixed_Window_Exponentiator::Fixed_Window_Exponentiator(const BigInt& n, + Power_Mod::Usage_Hints hints) + { + reducer = Modular_Reducer(n); + this->hints = hints; + window_bits = 0; + } + +} |