Botan  2.11.0
Crypto and TLS for C++11
pow_mod.h
Go to the documentation of this file.
1 /*
2 * Modular Exponentiator
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #ifndef BOTAN_POWER_MOD_H_
9 #define BOTAN_POWER_MOD_H_
10 
11 #include <botan/bigint.h>
12 
13 namespace Botan {
14 
15 /**
16 * Modular Exponentiator Interface
17 */
18 class BOTAN_PUBLIC_API(2,0) Modular_Exponentiator
19  {
20  public:
21  virtual void set_base(const BigInt&) = 0;
22  virtual void set_exponent(const BigInt&) = 0;
23  virtual BigInt execute() const = 0;
24  virtual Modular_Exponentiator* copy() const = 0;
25 
26  Modular_Exponentiator() = default;
27  Modular_Exponentiator(const Modular_Exponentiator&) = default;
28  Modular_Exponentiator & operator=(const Modular_Exponentiator&) = default;
29  virtual ~Modular_Exponentiator() = default;
30  };
31 
32 /**
33 * Modular Exponentiator Proxy
34 */
35 class BOTAN_PUBLIC_API(2,0) Power_Mod
36  {
37  public:
38 
39  enum Usage_Hints {
40  NO_HINTS = 0x0000,
41 
42  BASE_IS_FIXED = 0x0001,
43  BASE_IS_SMALL = 0x0002,
44  BASE_IS_LARGE = 0x0004,
45  BASE_IS_2 = 0x0008,
46 
47  EXP_IS_FIXED = 0x0100,
48  EXP_IS_SMALL = 0x0200,
49  EXP_IS_LARGE = 0x0400
50  };
51 
52  /*
53  * Try to choose a good window size
54  */
55  static size_t window_bits(size_t exp_bits, size_t base_bits,
56  Power_Mod::Usage_Hints hints);
57 
58  /**
59  * @param modulus the modulus
60  * @param hints Passed to set_modulus if modulus > 0
61  * @param disable_montgomery_arith Disables use of Montgomery
62  * representation. Likely only useful for testing.
63  */
64  void set_modulus(const BigInt& modulus,
65  Usage_Hints hints = NO_HINTS,
66  bool disable_montgomery_arith = false) const;
67 
68  /**
69  * Set the base
70  */
71  void set_base(const BigInt& base) const;
72 
73  /**
74  * Set the exponent
75  */
76  void set_exponent(const BigInt& exponent) const;
77 
78  /**
79  * All three of the above functions must have already been called.
80  * @return result of g^x%p
81  */
82  BigInt execute() const;
83 
84  Power_Mod& operator=(const Power_Mod&);
85 
86  /**
87  * @param modulus Optionally call set_modulus
88  * @param hints Passed to set_modulus if modulus > 0
89  * @param disable_montgomery_arith Disables use of Montgomery
90  * representation. Likely only useful for testing.
91  */
92  Power_Mod(const BigInt& modulus = 0,
93  Usage_Hints hints = NO_HINTS,
94  bool disable_montgomery_arith = false);
95  Power_Mod(const Power_Mod&);
96  virtual ~Power_Mod() = default;
97  private:
98  mutable std::unique_ptr<Modular_Exponentiator> m_core;
99  };
100 
101 /**
102 * Fixed Exponent Modular Exponentiator Proxy
103 */
104 class BOTAN_PUBLIC_API(2,0) Fixed_Exponent_Power_Mod final : public Power_Mod
105  {
106  public:
107  BigInt operator()(const BigInt& b) const
108  { set_base(b); return execute(); }
109 
110  Fixed_Exponent_Power_Mod() = default;
111 
112  Fixed_Exponent_Power_Mod(const BigInt& exponent,
113  const BigInt& modulus,
114  Usage_Hints hints = NO_HINTS);
115  };
116 
117 /**
118 * Fixed Base Modular Exponentiator Proxy
119 */
120 class BOTAN_PUBLIC_API(2,0) Fixed_Base_Power_Mod final : public Power_Mod
121  {
122  public:
123  BigInt operator()(const BigInt& e) const
124  { set_exponent(e); return execute(); }
125 
126  Fixed_Base_Power_Mod() = default;
127 
128  Fixed_Base_Power_Mod(const BigInt& base,
129  const BigInt& modulus,
130  Usage_Hints hints = NO_HINTS);
131  };
132 
133 }
134 
135 #endif
const botan_mp_t const botan_mp_t exponent
Definition: ffi.h:857
bool const OID & b
Definition: asn1_oid.h:109
int(* final)(unsigned char *, CTX *)
Definition: alg_id.cpp:13
class BOTAN_PUBLIC_API(2, 0) AlgorithmIdentifier final bool BOTAN_PUBLIC_API(2, 0) operator
Name Constraints.
Definition: asn1_obj.h:66
uint8_t base
Definition: ffi.h:749
botan_mp_t botan_mp_t botan_mp_t e
Definition: ffi.h:1116
BigInt const BigInt & modulus
Definition: numthry.h:87