Botan  2.7.0
Crypto and TLS for C++11
powm_fw.cpp
Go to the documentation of this file.
1 /*
2 * Fixed Window Exponentiation
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/internal/def_powm.h>
9 #include <vector>
10 
11 namespace Botan {
12 
13 /*
14 * Set the exponent
15 */
17  {
18  m_exp = e;
19  }
20 
21 /*
22 * Set the base
23 */
25  {
26  m_window_bits = Power_Mod::window_bits(m_exp.bits(), base.bits(), m_hints);
27 
28  m_g.resize(1U << m_window_bits);
29  m_g[0] = 1;
30  m_g[1] = base;
31 
32  for(size_t i = 2; i != m_g.size(); ++i)
33  m_g[i] = m_reducer.multiply(m_g[i-1], m_g[1]);
34  }
35 
36 /*
37 * Compute the result
38 */
40  {
41  const size_t exp_nibbles = (m_exp.bits() + m_window_bits - 1) / m_window_bits;
42 
43  BigInt x = 1;
44 
45  for(size_t i = exp_nibbles; i > 0; --i)
46  {
47  for(size_t j = 0; j != m_window_bits; ++j)
48  x = m_reducer.square(x);
49 
50  const uint32_t nibble = m_exp.get_substring(m_window_bits*(i-1), m_window_bits);
51 
52  x = m_reducer.multiply(x, m_g[nibble]);
53  }
54  return x;
55  }
56 
57 /*
58 * Fixed_Window_Exponentiator Constructor
59 */
62  : m_reducer{Modular_Reducer(n)}, m_exp{}, m_window_bits{}, m_g{}, m_hints{hints}
63  {}
64 
65 }
void set_base(const BigInt &) override
Definition: powm_fw.cpp:24
Fixed_Window_Exponentiator(const BigInt &, Power_Mod::Usage_Hints)
Definition: powm_fw.cpp:60
size_t bits() const
Definition: bigint.cpp:228
void set_exponent(const BigInt &) override
Definition: powm_fw.cpp:16
uint32_t get_substring(size_t offset, size_t length) const
Definition: bigint.cpp:164
Definition: alg_id.cpp:13
static size_t window_bits(size_t exp_bits, size_t base_bits, Power_Mod::Usage_Hints hints)
Definition: pow_mod.cpp:102
BigInt execute() const override
Definition: powm_fw.cpp:39
BigInt square(const BigInt &x) const
Definition: reducer.h:39
BigInt multiply(const BigInt &x, const BigInt &y) const
Definition: reducer.h:31