monotone

monotone Mtn Source Tree

Root/botan/pow_mod.cpp

1/*************************************************
2* Modular Exponentiation Proxy Source File *
3* (C) 1999-2006 The Botan Project *
4*************************************************/
5
6#include <botan/pow_mod.h>
7#include <botan/engine.h>
8
9namespace Botan {
10
11/*************************************************
12* Power_Mod Constructor *
13*************************************************/
14Power_Mod::Power_Mod(const BigInt& n, Usage_Hints hints)
15 {
16 core = 0;
17 set_modulus(n, hints);
18 }
19
20/*************************************************
21* Power_Mod Copy Constructor *
22*************************************************/
23Power_Mod::Power_Mod(const Power_Mod& other)
24 {
25 core = 0;
26 if(other.core)
27 core = other.core->copy();
28 }
29
30/*************************************************
31* Power_Mod Assignment Operator *
32*************************************************/
33Power_Mod& Power_Mod::operator=(const Power_Mod& other)
34 {
35 delete core;
36 core = 0;
37 if(other.core)
38 core = other.core->copy();
39 return (*this);
40 }
41
42/*************************************************
43* Power_Mod Destructor *
44*************************************************/
45Power_Mod::~Power_Mod()
46 {
47 delete core;
48 }
49
50/*************************************************
51* Set the modulus *
52*************************************************/
53void Power_Mod::set_modulus(const BigInt& n, Usage_Hints hints) const
54 {
55 delete core;
56 core = ((n == 0) ? 0 : Engine_Core::mod_exp(n, hints));
57 }
58
59/*************************************************
60* Set the base *
61*************************************************/
62void Power_Mod::set_base(const BigInt& b) const
63 {
64 if(b.is_zero() || b.is_negative())
65 throw Invalid_Argument("Power_Mod::set_base: arg must be > 0");
66
67 if(!core)
68 throw Internal_Error("Power_Mod::set_base: core was NULL");
69 core->set_base(b);
70 }
71
72/*************************************************
73* Set the exponent *
74*************************************************/
75void Power_Mod::set_exponent(const BigInt& e) const
76 {
77 if(e.is_negative())
78 throw Invalid_Argument("Power_Mod::set_exponent: arg must be > 0");
79
80 if(!core)
81 throw Internal_Error("Power_Mod::set_exponent: core was NULL");
82 core->set_exponent(e);
83 }
84
85/*************************************************
86* Compute the result *
87*************************************************/
88BigInt Power_Mod::execute() const
89 {
90 if(!core)
91 throw Internal_Error("Power_Mod::execute: core was NULL");
92 return core->execute();
93 }
94
95namespace {
96
97/*************************************************
98* Choose potentially useful hints *
99*************************************************/
100Power_Mod::Usage_Hints choose_base_hints(const BigInt& b, const BigInt& n)
101 {
102 if(b == 2)
103 return Power_Mod::Usage_Hints(Power_Mod::BASE_IS_2 |
104 Power_Mod::BASE_IS_SMALL);
105
106 const u32bit b_bits = b.bits();
107 const u32bit n_bits = n.bits();
108
109 if(b_bits < n_bits / 32)
110 return Power_Mod::BASE_IS_SMALL;
111 if(b_bits > n_bits / 4)
112 return Power_Mod::BASE_IS_LARGE;
113
114 return Power_Mod::NO_HINTS;
115 }
116
117/*************************************************
118* Choose potentially useful hints *
119*************************************************/
120Power_Mod::Usage_Hints choose_exp_hints(const BigInt& e, const BigInt& n)
121 {
122 const u32bit e_bits = e.bits();
123 const u32bit n_bits = n.bits();
124
125 if(e_bits < n_bits / 32)
126 return Power_Mod::BASE_IS_SMALL;
127 if(e_bits > n_bits / 4)
128 return Power_Mod::BASE_IS_LARGE;
129 return Power_Mod::NO_HINTS;
130 }
131
132}
133
134/*************************************************
135* Fixed_Exponent_Power_Mod Constructor *
136*************************************************/
137Fixed_Exponent_Power_Mod::Fixed_Exponent_Power_Mod(const BigInt& e,
138 const BigInt& n,
139 Usage_Hints hints) :
140 Power_Mod(n, Usage_Hints(hints | EXP_IS_FIXED | choose_exp_hints(e, n)))
141 {
142 set_exponent(e);
143 }
144
145/*************************************************
146* Fixed_Base_Power_Mod Constructor *
147*************************************************/
148Fixed_Base_Power_Mod::Fixed_Base_Power_Mod(const BigInt& b, const BigInt& n,
149 Usage_Hints hints) :
150 Power_Mod(n, Usage_Hints(hints | BASE_IS_FIXED | choose_base_hints(b, n)))
151 {
152 set_base(b);
153 }
154
155}

Archive Download this file

Branches

Tags

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