Botan 1.10.17

powm_fw.cpp
Go to the documentation of this file.
1 /*
2 * Fixed Window Exponentiation
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/internal/def_powm.h>
9 #include <botan/numthry.h>
10 #include <vector>
11
12 namespace Botan {
13
14 /*
15 * Set the exponent
16 */
17 void Fixed_Window_Exponentiator::set_exponent(const BigInt& e)
18 {
19 exp = e;
20 }
21
22 /*
23 * Set the base
24 */
25 void Fixed_Window_Exponentiator::set_base(const BigInt& base)
26 {
27 window_bits = Power_Mod::window_bits(exp.bits(), base.bits(), hints);
28
29 g.resize((1 << window_bits));
30 g[0] = 1;
31 g[1] = base;
32
33 for(size_t i = 2; i != g.size(); ++i)
34 g[i] = reducer.multiply(g[i-1], g[1]);
35 }
36
37 /*
38 * Compute the result
39 */
40 BigInt Fixed_Window_Exponentiator::execute() const
41 {
42 const size_t exp_nibbles = (exp.bits() + window_bits - 1) / window_bits;
43
44 BigInt x = 1;
45
46 for(size_t i = exp_nibbles; i > 0; --i)
47 {
48 for(size_t j = 0; j != window_bits; ++j)
49 x = reducer.square(x);
50
51 const u32bit nibble = exp.get_substring(window_bits*(i-1), window_bits);
52
53 x = reducer.multiply(x, g[nibble]);
54 }
55 return x;
56 }
57
58 /*
59 * Fixed_Window_Exponentiator Constructor
60 */
61 Fixed_Window_Exponentiator::Fixed_Window_Exponentiator(const BigInt& n,
62 Power_Mod::Usage_Hints hints)
63 {
64 reducer = Modular_Reducer(n);
65 this->hints = hints;
66 window_bits = 0;
67 }
68
69 }
Botan::Fixed_Window_Exponentiator::Fixed_Window_Exponentiator
Fixed_Window_Exponentiator(const BigInt &, Power_Mod::Usage_Hints)
Definition: powm_fw.cpp:61
Botan::Fixed_Window_Exponentiator::execute
BigInt execute() const
Definition: powm_fw.cpp:40
Botan::BigInt
Definition: bigint.h:22
Botan::BigInt::bits
size_t bits() const
Definition: bigint.cpp:254
Botan::BigInt::get_substring
u32bit get_substring(size_t offset, size_t length) const
Definition: bigint.cpp:169
Botan
Definition: algo_base.h:14
Botan::Power_Mod::window_bits
static size_t window_bits(size_t exp_bits, size_t base_bits, Power_Mod::Usage_Hints hints)
Definition: pow_mod.cpp:119
Botan::Power_Mod::Usage_Hints
Usage_Hints
Definition: pow_mod.h:35
Botan::Fixed_Window_Exponentiator::set_base
void set_base(const BigInt &)
Definition: powm_fw.cpp:25
Botan::Fixed_Window_Exponentiator::set_exponent
void set_exponent(const BigInt &)
Definition: powm_fw.cpp:17
Botan::Modular_Reducer::square
BigInt square(const BigInt &x) const
Definition: reducer.h:39
Botan::Modular_Reducer
Definition: reducer.h:18
Botan::Modular_Reducer::multiply
BigInt multiply(const BigInt &x, const BigInt &y) const
Definition: reducer.h:31
Botan::u32bit
unsigned int u32bit
Definition: types.h:32