1 | /* |
2 | Copyright (C) 1999-2007 The Botan Project. All rights reserved. |
3 | |
4 | Redistribution and use in source and binary forms, for any use, with or without |
5 | modification, is permitted provided that the following conditions are met: |
6 | |
7 | 1. Redistributions of source code must retain the above copyright notice, this |
8 | list of conditions, and the following disclaimer. |
9 | |
10 | 2. Redistributions in binary form must reproduce the above copyright notice, |
11 | this list of conditions, and the following disclaimer in the documentation |
12 | and/or other materials provided with the distribution. |
13 | |
14 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) "AS IS" AND ANY EXPRESS OR IMPLIED |
15 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
16 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED. |
17 | |
18 | IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE FOR ANY DIRECT, |
19 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
20 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
21 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
22 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
23 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
24 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 | */ |
26 | // LICENSEHEADER_END |
27 | namespace QCA { // WRAPNS_LINE |
28 | /************************************************* |
29 | * BigInt Header File * |
30 | * (C) 1999-2007 The Botan Project * |
31 | *************************************************/ |
32 | |
33 | #ifndef BOTAN_BIGINT_H__ |
34 | #define BOTAN_BIGINT_H__ |
35 | |
36 | #ifdef BOTAN_MINIMAL_BIGINT |
37 | } // WRAPNS_LINE |
38 | #include <botan/secmem.h> |
39 | namespace QCA { // WRAPNS_LINE |
40 | } // WRAPNS_LINE |
41 | #include <botan/exceptn.h> |
42 | namespace QCA { // WRAPNS_LINE |
43 | #else |
44 | } // WRAPNS_LINE |
45 | #include <botan/base.h> |
46 | namespace QCA { // WRAPNS_LINE |
47 | #endif |
48 | |
49 | } // WRAPNS_LINE |
50 | #include <botan/mp_types.h> |
51 | namespace QCA { // WRAPNS_LINE |
52 | } // WRAPNS_LINE |
53 | #include <iosfwd> |
54 | namespace QCA { // WRAPNS_LINE |
55 | |
56 | namespace Botan { |
57 | |
58 | /************************************************* |
59 | * BigInt * |
60 | *************************************************/ |
61 | class BigInt // clazy:exclude=rule-of-three TODO Needs checking if a real bug or not |
62 | { |
63 | public: |
64 | enum Base |
65 | { |
66 | Octal = 8, |
67 | Decimal = 10, |
68 | Hexadecimal = 16, |
69 | Binary = 256 |
70 | }; |
71 | enum Sign |
72 | { |
73 | Negative = 0, |
74 | Positive = 1 |
75 | }; |
76 | enum NumberType |
77 | { |
78 | Random, |
79 | Power2 |
80 | }; |
81 | |
82 | struct DivideByZero : public Exception |
83 | { |
84 | DivideByZero() |
85 | : Exception("BigInt divide by zero" ) |
86 | { |
87 | } |
88 | }; |
89 | |
90 | BigInt &operator+=(const BigInt &); |
91 | BigInt &operator-=(const BigInt &); |
92 | |
93 | BigInt &operator*=(const BigInt &); |
94 | BigInt &operator/=(const BigInt &); |
95 | BigInt &operator%=(const BigInt &); |
96 | word operator%=(word); |
97 | BigInt &operator<<=(u32bit); |
98 | BigInt &operator>>=(u32bit); |
99 | |
100 | BigInt &operator++() |
101 | { |
102 | return (*this += 1); |
103 | } |
104 | BigInt &operator--() |
105 | { |
106 | return (*this -= 1); |
107 | } |
108 | BigInt operator++(int) |
109 | { |
110 | BigInt x = (*this); |
111 | ++(*this); |
112 | return x; |
113 | } |
114 | BigInt operator--(int) |
115 | { |
116 | BigInt x = (*this); |
117 | --(*this); |
118 | return x; |
119 | } |
120 | |
121 | BigInt operator-() const; |
122 | bool operator!() const |
123 | { |
124 | return (!is_nonzero()); |
125 | } |
126 | |
127 | s32bit cmp(const BigInt &, bool = true) const; |
128 | bool is_even() const |
129 | { |
130 | return (get_bit(0) == 0); |
131 | } |
132 | bool is_odd() const |
133 | { |
134 | return (get_bit(0) == 1); |
135 | } |
136 | bool is_nonzero() const |
137 | { |
138 | return (!is_zero()); |
139 | } |
140 | bool is_zero() const; |
141 | |
142 | void set_bit(u32bit); |
143 | void clear_bit(u32bit); |
144 | void mask_bits(u32bit); |
145 | |
146 | bool get_bit(u32bit) const; |
147 | u32bit get_substring(u32bit, u32bit) const; |
148 | byte byte_at(u32bit) const; |
149 | word word_at(u32bit n) const |
150 | { |
151 | return ((n < size()) ? reg[n] : 0); |
152 | } |
153 | |
154 | u32bit to_u32bit() const; |
155 | |
156 | bool is_negative() const |
157 | { |
158 | return (sign() == Negative); |
159 | } |
160 | bool is_positive() const |
161 | { |
162 | return (sign() == Positive); |
163 | } |
164 | Sign sign() const |
165 | { |
166 | return (signedness); |
167 | } |
168 | Sign reverse_sign() const; |
169 | void flip_sign(); |
170 | void set_sign(Sign); |
171 | BigInt abs() const; |
172 | |
173 | u32bit size() const |
174 | { |
175 | return reg.size(); |
176 | } |
177 | u32bit sig_words() const; |
178 | u32bit bytes() const; |
179 | u32bit bits() const; |
180 | |
181 | const word *data() const |
182 | { |
183 | return reg.begin(); |
184 | } |
185 | SecureVector<word> &get_reg() |
186 | { |
187 | return reg; |
188 | } |
189 | void grow_reg(u32bit) const; |
190 | |
191 | word &operator[](u32bit index) |
192 | { |
193 | return reg[index]; |
194 | } |
195 | word operator[](u32bit index) const |
196 | { |
197 | return reg[index]; |
198 | } |
199 | void clear() |
200 | { |
201 | reg.clear(); |
202 | } |
203 | |
204 | #ifndef BOTAN_MINIMAL_BIGINT |
205 | void randomize(u32bit = 0); |
206 | #endif |
207 | |
208 | void binary_encode(byte[]) const; |
209 | void binary_decode(const byte[], u32bit); |
210 | u32bit encoded_size(Base = Binary) const; |
211 | |
212 | static SecureVector<byte> encode(const BigInt &, Base = Binary); |
213 | static void encode(byte[], const BigInt &, Base = Binary); |
214 | static BigInt decode(const byte[], u32bit, Base = Binary); |
215 | static BigInt decode(const MemoryRegion<byte> &, Base = Binary); |
216 | static SecureVector<byte> encode_1363(const BigInt &, u32bit); |
217 | |
218 | void swap(BigInt &); |
219 | |
220 | BigInt() |
221 | { |
222 | signedness = Positive; |
223 | } |
224 | BigInt(u64bit); |
225 | BigInt(const BigInt &); |
226 | BigInt(const std::string &); |
227 | BigInt(const byte[], u32bit, Base = Binary); |
228 | BigInt(Sign, u32bit); |
229 | #ifndef BOTAN_MINIMAL_BIGINT |
230 | BigInt(NumberType, u32bit); |
231 | #endif |
232 | BigInt &operator=(const BigInt &); |
233 | |
234 | private: |
235 | void grow_to(u32bit) const; |
236 | SecureVector<word> reg; |
237 | Sign signedness; |
238 | }; |
239 | |
240 | /************************************************* |
241 | * Arithmetic Operators * |
242 | *************************************************/ |
243 | BigInt operator+(const BigInt &, const BigInt &); |
244 | BigInt operator-(const BigInt &, const BigInt &); |
245 | BigInt operator*(const BigInt &, const BigInt &); |
246 | BigInt operator/(const BigInt &, const BigInt &); |
247 | BigInt operator%(const BigInt &, const BigInt &); |
248 | word operator%(const BigInt &, word); |
249 | BigInt operator<<(const BigInt &, u32bit); |
250 | BigInt operator>>(const BigInt &, u32bit); |
251 | |
252 | /************************************************* |
253 | * Comparison Operators * |
254 | *************************************************/ |
255 | inline bool operator==(const BigInt &a, const BigInt &b) |
256 | { |
257 | return (a.cmp(b) == 0); |
258 | } |
259 | inline bool operator!=(const BigInt &a, const BigInt &b) |
260 | { |
261 | return (a.cmp(b) != 0); |
262 | } |
263 | inline bool operator<=(const BigInt &a, const BigInt &b) |
264 | { |
265 | return (a.cmp(b) <= 0); |
266 | } |
267 | inline bool operator>=(const BigInt &a, const BigInt &b) |
268 | { |
269 | return (a.cmp(b) >= 0); |
270 | } |
271 | inline bool operator<(const BigInt &a, const BigInt &b) |
272 | { |
273 | return (a.cmp(b) < 0); |
274 | } |
275 | inline bool operator>(const BigInt &a, const BigInt &b) |
276 | { |
277 | return (a.cmp(b) > 0); |
278 | } |
279 | |
280 | /************************************************* |
281 | * I/O Operators * |
282 | *************************************************/ |
283 | #ifndef BOTAN_MINIMAL_BIGINT |
284 | std::ostream &operator<<(std::ostream &, const BigInt &); |
285 | std::istream &operator>>(std::istream &, BigInt &); |
286 | #endif |
287 | |
288 | } |
289 | |
290 | #ifndef BOTAN_MINIMAL_BIGINT |
291 | } // WRAPNS_LINE |
292 | namespace std { |
293 | |
294 | inline void swap(Botan::BigInt &a, Botan::BigInt &b) |
295 | { |
296 | a.swap(b); |
297 | } |
298 | |
299 | } |
300 | namespace QCA { // WRAPNS_LINE |
301 | #endif |
302 | |
303 | #endif |
304 | } // WRAPNS_LINE |
305 | |