monotone

monotone Mtn Source Tree

Root/botan/mod_exp.cpp

1/*************************************************
2* Modular Exponentiation Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/mod_exp.h>
7#include <botan/numthry.h>
8
9namespace Botan {
10
11/*************************************************
12* FixedExponent_Exp Constructor *
13*************************************************/
14FixedExponent_Exp::FixedExponent_Exp(const BigInt& exp, const BigInt& mod) :
15 reducer(get_reducer(mod)), exponent(exp)
16 {
17 if(mod <= 0)
18 throw Invalid_Argument("FixedExponent_Exp: Invalid modulus");
19 if(exp < 0)
20 throw Invalid_Argument("FixedExponent_Exp: Invalid exponent");
21 }
22
23/*************************************************
24* FixedExponent_Exp Copy Constructor *
25*************************************************/
26FixedExponent_Exp::FixedExponent_Exp(const FixedExponent_Exp& exp)
27 {
28 exponent = 0;
29 reducer = 0;
30
31 if(exp.initialized())
32 {
33 exponent = exp.get_exponent();
34 reducer = get_reducer(exp.get_modulus());
35 }
36 }
37
38/*************************************************
39* FixedExponent_Exp Assignment Operator *
40*************************************************/
41FixedExponent_Exp& FixedExponent_Exp::operator=(const FixedExponent_Exp& exp)
42 {
43 delete reducer;
44 exponent = 0;
45 reducer = 0;
46
47 if(exp.initialized())
48 {
49 reducer = get_reducer(exp.get_modulus());
50 exponent = exp.get_exponent();
51 }
52 return (*this);
53 }
54
55/*************************************************
56* Fixed Exponent Exponentiation *
57*************************************************/
58BigInt FixedExponent_Exp::power_mod(const BigInt& base) const
59 {
60 init_check();
61 return Botan::power_mod(reducer->reduce(base), exponent, reducer);
62 }
63
64/*************************************************
65* Calculate n modulo the fixed modulus *
66*************************************************/
67BigInt FixedExponent_Exp::reduce(const BigInt& n) const
68 {
69 init_check();
70 return reducer->reduce(n);
71 }
72
73/*************************************************
74* Return the exponent being used *
75*************************************************/
76const BigInt& FixedExponent_Exp::get_exponent() const
77 {
78 init_check();
79 return exponent;
80 }
81
82/*************************************************
83* Return the modulus being used *
84*************************************************/
85const BigInt& FixedExponent_Exp::get_modulus() const
86 {
87 init_check();
88 return reducer->get_modulus();
89 }
90
91/*************************************************
92* Ensure the object has been initialized *
93*************************************************/
94void FixedExponent_Exp::init_check() const
95 {
96 if(!initialized())
97 throw Invalid_State("FixedExponent_Exp: Uninitialized access");
98 }
99
100/*************************************************
101* FixedBase_Exp Constructor *
102*************************************************/
103FixedBase_Exp::FixedBase_Exp(const BigInt& base, const BigInt& mod) :
104 reducer(get_reducer(mod)), g(255)
105 {
106 if(mod <= 0)
107 throw Invalid_Argument("FixedBase_Exp: Invalid modulus");
108 if(base < 0)
109 throw Invalid_Argument("FixedBase_Exp: Invalid base");
110
111 g[0] = base;
112 for(u32bit j = 1; j != g.size(); j++)
113 g[j] = reducer->multiply(g[j-1], g[0]);
114 }
115
116/*************************************************
117* FixedBase_Exp Copy Constructor *
118*************************************************/
119FixedBase_Exp::FixedBase_Exp(const FixedBase_Exp& exp)
120 {
121 reducer = 0;
122
123 if(exp.initialized())
124 {
125 reducer = get_reducer(exp.get_modulus());
126 g = exp.g;
127 }
128 }
129
130/*************************************************
131* FixedBase_Exp Assignment Operator *
132*************************************************/
133FixedBase_Exp& FixedBase_Exp::operator=(const FixedBase_Exp& exp)
134 {
135 delete reducer;
136 reducer = 0;
137
138 if(exp.initialized())
139 {
140 reducer = get_reducer(exp.get_modulus());
141 g = exp.g;
142 }
143 return (*this);
144 }
145
146/*************************************************
147* Fixed Base Exponentiation *
148*************************************************/
149BigInt FixedBase_Exp::power_mod(const BigInt& exp) const
150 {
151 init_check();
152 if(exp.is_negative())
153 throw Invalid_Argument("power_mod: exponent must be positive");
154 if(exp.is_zero())
155 return 1;
156
157 const u32bit exp_bytes = (exp.bits() + 7) / 8;
158
159 BigInt x = 1;
160 for(u32bit j = exp_bytes; j > 0; j--)
161 {
162 for(u32bit k = 0; k != 8; k++)
163 x = reducer->square(x);
164 u32bit nibble = exp.byte_at(j-1);
165 if(nibble)
166 x = reducer->multiply(x, g[nibble-1]);
167 }
168 return x;
169 }
170
171/*************************************************
172* Calculate n modulo the fixed modulus *
173*************************************************/
174BigInt FixedBase_Exp::reduce(const BigInt& n) const
175 {
176 init_check();
177 return reducer->reduce(n);
178 }
179
180/*************************************************
181* Return the generator being used *
182*************************************************/
183const BigInt& FixedBase_Exp::get_base() const
184 {
185 init_check();
186 return g[0];
187 }
188
189/*************************************************
190* Return the modulus being used *
191*************************************************/
192const BigInt& FixedBase_Exp::get_modulus() const
193 {
194 init_check();
195 return reducer->get_modulus();
196 }
197
198/*************************************************
199* Ensure the object has been initialized *
200*************************************************/
201void FixedBase_Exp::init_check() const
202 {
203 if(!initialized())
204 throw Invalid_State("FixedBase_Exp: Uninitialized access");
205 }
206
207
208}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status