1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
/*
* Modular Exponentiation Proxy
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#include <botan/pow_mod.h>
#include <botan/libstate.h>
#include <botan/engine.h>
namespace Botan {
/*
* Power_Mod Constructor
*/
Power_Mod::Power_Mod(const BigInt& n, Usage_Hints hints)
{
core = 0;
set_modulus(n, hints);
}
/*
* Power_Mod Copy Constructor
*/
Power_Mod::Power_Mod(const Power_Mod& other)
{
core = 0;
if(other.core)
core = other.core->copy();
}
/*
* Power_Mod Assignment Operator
*/
Power_Mod& Power_Mod::operator=(const Power_Mod& other)
{
delete core;
core = 0;
if(other.core)
core = other.core->copy();
return (*this);
}
/*
* Power_Mod Destructor
*/
Power_Mod::~Power_Mod()
{
delete core;
}
/*
* Set the modulus
*/
void Power_Mod::set_modulus(const BigInt& n, Usage_Hints hints) const
{
delete core;
core = 0;
if(n != 0)
{
Algorithm_Factory::Engine_Iterator i(global_state().algorithm_factory());
while(const Engine* engine = i.next())
{
core = engine->mod_exp(n, hints);
if(core)
break;
}
if(!core)
throw Lookup_Error("Power_Mod: Unable to find a working engine");
}
}
/*
* Set the base
*/
void Power_Mod::set_base(const BigInt& b) const
{
if(b.is_zero() || b.is_negative())
throw Invalid_Argument("Power_Mod::set_base: arg must be > 0");
if(!core)
throw Internal_Error("Power_Mod::set_base: core was NULL");
core->set_base(b);
}
/*
* Set the exponent
*/
void Power_Mod::set_exponent(const BigInt& e) const
{
if(e.is_negative())
throw Invalid_Argument("Power_Mod::set_exponent: arg must be > 0");
if(!core)
throw Internal_Error("Power_Mod::set_exponent: core was NULL");
core->set_exponent(e);
}
/*
* Compute the result
*/
BigInt Power_Mod::execute() const
{
if(!core)
throw Internal_Error("Power_Mod::execute: core was NULL");
return core->execute();
}
/*
* Try to choose a good window size
*/
u32bit Power_Mod::window_bits(u32bit exp_bits, u32bit,
Power_Mod::Usage_Hints hints)
{
static const u32bit wsize[][2] = {
{ 2048, 7 }, { 1024, 6 }, { 256, 5 }, { 128, 4 }, { 64, 3 }, { 0, 0 }
};
u32bit window_bits = 1;
if(exp_bits)
{
for(u32bit j = 0; wsize[j][0]; ++j)
{
if(exp_bits >= wsize[j][0])
{
window_bits += wsize[j][1];
break;
}
}
}
if(hints & Power_Mod::BASE_IS_FIXED)
window_bits += 2;
if(hints & Power_Mod::EXP_IS_LARGE)
++window_bits;
return window_bits;
}
namespace {
/*
* Choose potentially useful hints
*/
Power_Mod::Usage_Hints choose_base_hints(const BigInt& b, const BigInt& n)
{
if(b == 2)
return Power_Mod::Usage_Hints(Power_Mod::BASE_IS_2 |
Power_Mod::BASE_IS_SMALL);
const u32bit b_bits = b.bits();
const u32bit n_bits = n.bits();
if(b_bits < n_bits / 32)
return Power_Mod::BASE_IS_SMALL;
if(b_bits > n_bits / 4)
return Power_Mod::BASE_IS_LARGE;
return Power_Mod::NO_HINTS;
}
/*
* Choose potentially useful hints
*/
Power_Mod::Usage_Hints choose_exp_hints(const BigInt& e, const BigInt& n)
{
const u32bit e_bits = e.bits();
const u32bit n_bits = n.bits();
if(e_bits < n_bits / 32)
return Power_Mod::BASE_IS_SMALL;
if(e_bits > n_bits / 4)
return Power_Mod::BASE_IS_LARGE;
return Power_Mod::NO_HINTS;
}
}
/*
* Fixed_Exponent_Power_Mod Constructor
*/
Fixed_Exponent_Power_Mod::Fixed_Exponent_Power_Mod(const BigInt& e,
const BigInt& n,
Usage_Hints hints) :
Power_Mod(n, Usage_Hints(hints | EXP_IS_FIXED | choose_exp_hints(e, n)))
{
set_exponent(e);
}
/*
* Fixed_Base_Power_Mod Constructor
*/
Fixed_Base_Power_Mod::Fixed_Base_Power_Mod(const BigInt& b, const BigInt& n,
Usage_Hints hints) :
Power_Mod(n, Usage_Hints(hints | BASE_IS_FIXED | choose_base_hints(b, n)))
{
set_base(b);
}
}
|