Botan  2.7.0
Crypto and TLS for C++11
Public Types | Public Member Functions | Static Public Member Functions | List of all members
Botan::Power_Mod Class Reference

#include <pow_mod.h>

Inheritance diagram for Botan::Power_Mod:
Botan::Fixed_Base_Power_Mod Botan::Fixed_Exponent_Power_Mod

Public Types

enum  Usage_Hints {
  NO_HINTS = 0x0000, BASE_IS_FIXED = 0x0001, BASE_IS_SMALL = 0x0002, BASE_IS_LARGE = 0x0004,
  BASE_IS_2 = 0x0008, EXP_IS_FIXED = 0x0100, EXP_IS_SMALL = 0x0200, EXP_IS_LARGE = 0x0400
}
 

Public Member Functions

BigInt execute () const
 
Power_Modoperator= (const Power_Mod &)
 
 Power_Mod (const BigInt &modulus=0, Usage_Hints hints=NO_HINTS, bool disable_montgomery_arith=false)
 
 Power_Mod (const Power_Mod &)
 
void set_base (const BigInt &base) const
 
void set_exponent (const BigInt &exponent) const
 
void set_modulus (const BigInt &modulus, Usage_Hints hints=NO_HINTS, bool disable_montgomery_arith=false) const
 
virtual ~Power_Mod ()=default
 

Static Public Member Functions

static size_t window_bits (size_t exp_bits, size_t base_bits, Power_Mod::Usage_Hints hints)
 

Detailed Description

Modular Exponentiator Proxy

Definition at line 35 of file pow_mod.h.

Member Enumeration Documentation

◆ Usage_Hints

Enumerator
NO_HINTS 
BASE_IS_FIXED 
BASE_IS_SMALL 
BASE_IS_LARGE 
BASE_IS_2 
EXP_IS_FIXED 
EXP_IS_SMALL 
EXP_IS_LARGE 

Definition at line 39 of file pow_mod.h.

Constructor & Destructor Documentation

◆ Power_Mod() [1/2]

Botan::Power_Mod::Power_Mod ( const BigInt modulus = 0,
Usage_Hints  hints = NO_HINTS,
bool  disable_montgomery_arith = false 
)
Parameters
modulusOptionally call set_modulus
hintsPassed to set_modulus if modulus > 0
disable_montgomery_arithDisables use of Montgomery representation. Likely only useful for testing.

Definition at line 16 of file pow_mod.cpp.

References set_modulus().

17  {
18  set_modulus(n, hints, disable_monty);
19  }
void set_modulus(const BigInt &modulus, Usage_Hints hints=NO_HINTS, bool disable_montgomery_arith=false) const
Definition: pow_mod.cpp:48

◆ Power_Mod() [2/2]

Botan::Power_Mod::Power_Mod ( const Power_Mod other)

Definition at line 24 of file pow_mod.cpp.

25  {
26  if(other.m_core.get())
27  m_core.reset(other.m_core->copy());
28  }

◆ ~Power_Mod()

virtual Botan::Power_Mod::~Power_Mod ( )
virtualdefault

Member Function Documentation

◆ execute()

BigInt Botan::Power_Mod::execute ( ) const

All three of the above functions must have already been called.

Returns
result of g^xp

Definition at line 92 of file pow_mod.cpp.

Referenced by Botan::power_mod().

93  {
94  if(!m_core)
95  throw Internal_Error("Power_Mod::execute: m_core was NULL");
96  return m_core->execute();
97  }

◆ operator=()

Power_Mod & Botan::Power_Mod::operator= ( const Power_Mod other)

Definition at line 33 of file pow_mod.cpp.

34  {
35  if(this != &other)
36  {
37  if(other.m_core)
38  m_core.reset(other.m_core->copy());
39  else
40  m_core.reset();
41  }
42  return (*this);
43  }

◆ set_base()

void Botan::Power_Mod::set_base ( const BigInt base) const

Set the base

Definition at line 66 of file pow_mod.cpp.

References Botan::BigInt::is_negative(), and Botan::BigInt::is_zero().

Referenced by Botan::Fixed_Base_Power_Mod::Fixed_Base_Power_Mod(), and Botan::power_mod().

67  {
68  if(b.is_zero() || b.is_negative())
69  throw Invalid_Argument("Power_Mod::set_base: arg must be > 0");
70 
71  if(!m_core)
72  throw Internal_Error("Power_Mod::set_base: m_core was NULL");
73  m_core->set_base(b);
74  }

◆ set_exponent()

void Botan::Power_Mod::set_exponent ( const BigInt exponent) const

Set the exponent

Definition at line 79 of file pow_mod.cpp.

References Botan::BigInt::is_negative().

Referenced by Botan::Fixed_Exponent_Power_Mod::Fixed_Exponent_Power_Mod(), and Botan::power_mod().

80  {
81  if(e.is_negative())
82  throw Invalid_Argument("Power_Mod::set_exponent: arg must be > 0");
83 
84  if(!m_core)
85  throw Internal_Error("Power_Mod::set_exponent: m_core was NULL");
86  m_core->set_exponent(e);
87  }

◆ set_modulus()

void Botan::Power_Mod::set_modulus ( const BigInt modulus,
Usage_Hints  hints = NO_HINTS,
bool  disable_montgomery_arith = false 
) const
Parameters
modulusthe modulus
hintsPassed to set_modulus if modulus > 0
disable_montgomery_arithDisables use of Montgomery representation. Likely only useful for testing.

Definition at line 48 of file pow_mod.cpp.

References Botan::BigInt::is_odd().

Referenced by Power_Mod().

49  {
50  // Allow set_modulus(0) to mean "drop old state"
51 
52  m_core.reset();
53 
54  if(n != 0)
55  {
56  if(n.is_odd() && disable_monty == false)
57  m_core.reset(new Montgomery_Exponentiator(n, hints));
58  else
59  m_core.reset(new Fixed_Window_Exponentiator(n, hints));
60  }
61  }

◆ window_bits()

size_t Botan::Power_Mod::window_bits ( size_t  exp_bits,
size_t  base_bits,
Power_Mod::Usage_Hints  hints 
)
static

Definition at line 102 of file pow_mod.cpp.

References BASE_IS_FIXED, and EXP_IS_LARGE.

Referenced by Botan::Fixed_Window_Exponentiator::set_base(), and Botan::Montgomery_Exponentiator::set_base().

104  {
105  static const size_t wsize[][2] = {
106  { 1434, 7 },
107  { 539, 6 },
108  { 197, 4 },
109  { 70, 3 },
110  { 17, 2 },
111  { 0, 0 }
112  };
113 
114  size_t window_bits = 1;
115 
116  if(exp_bits)
117  {
118  for(size_t j = 0; wsize[j][0]; ++j)
119  {
120  if(exp_bits >= wsize[j][0])
121  {
122  window_bits += wsize[j][1];
123  break;
124  }
125  }
126  }
127 
128  if(hints & Power_Mod::BASE_IS_FIXED)
129  window_bits += 2;
130  if(hints & Power_Mod::EXP_IS_LARGE)
131  ++window_bits;
132 
133  return window_bits;
134  }
static size_t window_bits(size_t exp_bits, size_t base_bits, Power_Mod::Usage_Hints hints)
Definition: pow_mod.cpp:102

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