Botan  2.4.0
Crypto and TLS for C++11
Public Member Functions | List of all members
Botan::Montgomery_Exponentiator Class Referencefinal

#include <def_powm.h>

Inheritance diagram for Botan::Montgomery_Exponentiator:
Botan::Modular_Exponentiator

Public Member Functions

Modular_Exponentiatorcopy () const override
 
BigInt execute () const override
 
 Montgomery_Exponentiator (const BigInt &, Power_Mod::Usage_Hints)
 
void set_base (const BigInt &) override
 
void set_exponent (const BigInt &) override
 

Detailed Description

Montgomery Exponentiator

Definition at line 42 of file def_powm.h.

Constructor & Destructor Documentation

◆ Montgomery_Exponentiator()

Botan::Montgomery_Exponentiator::Montgomery_Exponentiator ( const BigInt mod,
Power_Mod::Usage_Hints  hints 
)

Definition at line 119 of file powm_mnt.cpp.

References Botan::BigInt::is_even(), Botan::BigInt::is_positive(), Botan::monty_inverse(), Botan::BigInt::power_of_2(), Botan::Modular_Reducer::reduce(), Botan::Modular_Reducer::square(), and Botan::BigInt::word_at().

120  :
121  m_modulus(mod),
122  m_reducer(m_modulus),
123  m_mod_words(m_modulus.sig_words()),
124  m_window_bits(1),
125  m_hints(hints)
126  {
127  // Montgomery reduction only works for positive odd moduli
128  if(!m_modulus.is_positive() || m_modulus.is_even())
129  throw Invalid_Argument("Montgomery_Exponentiator: invalid modulus");
130 
131  m_mod_prime = monty_inverse(mod.word_at(0));
132 
133  const BigInt r = BigInt::power_of_2(m_mod_words * BOTAN_MP_WORD_BITS);
134  m_R_mod = m_reducer.reduce(r);
135  m_R2_mod = m_reducer.square(m_R_mod);
136  m_exp_bits = 0;
137  }
bool is_even() const
Definition: bigint.h:237
static BigInt power_of_2(size_t n)
Definition: bigint.h:499
size_t sig_words() const
Definition: bigint.h:398
BigInt reduce(const BigInt &x) const
Definition: reducer.cpp:32
BigInt square(const BigInt &x) const
Definition: reducer.h:39
word monty_inverse(word input)
Definition: numthry.cpp:326
bool is_positive() const
Definition: bigint.h:359

Member Function Documentation

◆ copy()

Modular_Exponentiator* Botan::Montgomery_Exponentiator::copy ( ) const
inlineoverridevirtual

Implements Botan::Modular_Exponentiator.

Definition at line 49 of file def_powm.h.

50  { return new Montgomery_Exponentiator(*this); }
Montgomery_Exponentiator(const BigInt &, Power_Mod::Usage_Hints)
Definition: powm_mnt.cpp:119

◆ execute()

BigInt Botan::Montgomery_Exponentiator::execute ( ) const
overridevirtual

Implements Botan::Modular_Exponentiator.

Definition at line 69 of file powm_mnt.cpp.

References Botan::bigint_monty_redc(), Botan::bigint_monty_sqr(), Botan::bigint_mul(), Botan::BigInt::const_time_lookup(), Botan::BigInt::data(), Botan::BigInt::get_substring(), Botan::BigInt::grow_to(), Botan::BigInt::mutable_data(), Botan::BigInt::Positive, Botan::BigInt::sig_words(), and Botan::BigInt::size().

70  {
71  const size_t exp_nibbles = (m_exp_bits + m_window_bits - 1) / m_window_bits;
72 
73  BigInt x = m_R_mod;
74 
75  const size_t z_size = 2*(m_mod_words + 1);
76 
77  BigInt z(BigInt::Positive, z_size);
78  secure_vector<word> workspace(z.size());
79  secure_vector<word> e(m_mod_words);
80 
81  for(size_t i = exp_nibbles; i > 0; --i)
82  {
83  for(size_t k = 0; k != m_window_bits; ++k)
84  {
85  bigint_monty_sqr(z, x, m_modulus.data(), m_mod_words, m_mod_prime,
86  workspace.data());
87 
88  x = z;
89  }
90 
91  const uint32_t nibble = m_exp.get_substring(m_window_bits*(i-1), m_window_bits);
92 
93  BigInt::const_time_lookup(e, m_g, nibble);
94 
95  bigint_mul(z.mutable_data(), z.size(),
96  x.data(), x.size(), x.sig_words(),
97  e.data(), m_mod_words, m_mod_words,
98  workspace.data());
99 
100  bigint_monty_redc(z.mutable_data(),
101  m_modulus.data(), m_mod_words, m_mod_prime,
102  workspace.data());
103 
104  x = z;
105  }
106 
107  x.grow_to(2*m_mod_words + 1);
108 
109  bigint_monty_redc(x.mutable_data(),
110  m_modulus.data(), m_mod_words, m_mod_prime,
111  workspace.data());
112 
113  return x;
114  }
static void const_time_lookup(secure_vector< word > &output, const std::vector< BigInt > &vec, size_t idx)
Definition: bigint.cpp:302
uint32_t get_substring(size_t offset, size_t length) const
Definition: bigint.cpp:119
const word * data() const
Definition: bigint.h:430
void bigint_monty_sqr(BigInt &z, const BigInt &x, const word p[], size_t p_size, word p_dash, word workspace[])
Definition: mp_monty.cpp:108
void bigint_mul(BigInt &z, const BigInt &x, const BigInt &y, word workspace[])
Definition: mp_karat.cpp:253
void bigint_monty_redc(word z[], const word p[], size_t p_size, word p_dash, word workspace[])
Definition: mp_monty.cpp:22

◆ set_base()

void Botan::Montgomery_Exponentiator::set_base ( const BigInt base)
overridevirtual

Implements Botan::Modular_Exponentiator.

Definition at line 27 of file powm_mnt.cpp.

References Botan::bigint_monty_mul(), Botan::BigInt::bits(), Botan::BigInt::data(), Botan::BigInt::Positive, Botan::Modular_Reducer::reduce(), Botan::BigInt::shrink_to_fit(), Botan::BigInt::size(), and Botan::Power_Mod::window_bits().

28  {
29  m_window_bits = Power_Mod::window_bits(m_exp.bits(), base.bits(), m_hints);
30 
31  m_g.resize(1U << m_window_bits);
32 
33  BigInt z(BigInt::Positive, 2 * (m_mod_words + 1));
34  secure_vector<word> workspace(z.size());
35 
36  m_g[0] = 1;
37 
38  bigint_monty_mul(z, m_g[0], m_R2_mod,
39  m_modulus.data(), m_mod_words, m_mod_prime,
40  workspace.data());
41  m_g[0] = z;
42 
43  m_g[1] = m_reducer.reduce(base);
44 
45  bigint_monty_mul(z, m_g[1], m_R2_mod,
46  m_modulus.data(), m_mod_words, m_mod_prime,
47  workspace.data());
48 
49  m_g[1] = z;
50 
51  const BigInt& x = m_g[1];
52 
53  for(size_t i = 2; i != m_g.size(); ++i)
54  {
55  const BigInt& y = m_g[i-1];
56 
57  bigint_monty_mul(z, x, y, m_modulus.data(), m_mod_words, m_mod_prime,
58  workspace.data());
59 
60  m_g[i] = z;
61  m_g[i].shrink_to_fit();
62  m_g[i].grow_to(m_mod_words);
63  }
64  }
size_t bits() const
Definition: bigint.cpp:183
const word * data() const
Definition: bigint.h:430
void bigint_monty_mul(BigInt &z, const BigInt &x, const BigInt &y, const word p[], size_t p_size, word p_dash, word workspace[])
Definition: mp_monty.cpp:97
static size_t window_bits(size_t exp_bits, size_t base_bits, Power_Mod::Usage_Hints hints)
Definition: pow_mod.cpp:102
BigInt reduce(const BigInt &x) const
Definition: reducer.cpp:32

◆ set_exponent()

void Botan::Montgomery_Exponentiator::set_exponent ( const BigInt exp)
overridevirtual

Implements Botan::Modular_Exponentiator.

Definition at line 18 of file powm_mnt.cpp.

References Botan::BigInt::bits().

19  {
20  m_exp = exp;
21  m_exp_bits = exp.bits();
22  }

The documentation for this class was generated from the following files: