2
Copyright (C) 1999-2007 The Botan Project. All rights reserved.
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:
7
1. Redistributions of source code must retain the above copyright notice, this
8
list of conditions, and the following disclaimer.
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.
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.
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.
27
namespace QCA { // WRAPNS_LINE
28
/*************************************************
29
* BigInt Header File *
30
* (C) 1999-2007 The Botan Project *
31
*************************************************/
33
#ifndef BOTAN_BIGINT_H__
34
#define BOTAN_BIGINT_H__
36
#ifdef BOTAN_MINIMAL_BIGINT
38
# include <botan/secmem.h>
39
namespace QCA { // WRAPNS_LINE
41
# include <botan/exceptn.h>
42
namespace QCA { // WRAPNS_LINE
45
# include <botan/base.h>
46
namespace QCA { // WRAPNS_LINE
50
#include <botan/mp_types.h>
51
namespace QCA { // WRAPNS_LINE
54
namespace QCA { // WRAPNS_LINE
58
/*************************************************
60
*************************************************/
64
enum Base { Octal = 8, Decimal = 10, Hexadecimal = 16, Binary = 256 };
65
enum Sign { Negative = 0, Positive = 1 };
66
enum NumberType { Random, Power2 };
68
struct DivideByZero : public Exception
69
{ DivideByZero() : Exception("BigInt divide by zero") {} };
71
BigInt& operator+=(const BigInt&);
72
BigInt& operator-=(const BigInt&);
74
BigInt& operator*=(const BigInt&);
75
BigInt& operator/=(const BigInt&);
76
BigInt& operator%=(const BigInt&);
77
word operator%=(word);
78
BigInt& operator<<=(u32bit);
79
BigInt& operator>>=(u32bit);
81
BigInt& operator++() { return (*this += 1); }
82
BigInt& operator--() { return (*this -= 1); }
83
BigInt operator++(int) { BigInt x = (*this); ++(*this); return x; }
84
BigInt operator--(int) { BigInt x = (*this); --(*this); return x; }
86
BigInt operator-() const;
87
bool operator !() const { return (!is_nonzero()); }
89
s32bit cmp(const BigInt&, bool = true) const;
90
bool is_even() const { return (get_bit(0) == 0); }
91
bool is_odd() const { return (get_bit(0) == 1); }
92
bool is_nonzero() const { return (!is_zero()); }
96
void clear_bit(u32bit);
97
void mask_bits(u32bit);
99
bool get_bit(u32bit) const;
100
u32bit get_substring(u32bit, u32bit) const;
101
byte byte_at(u32bit) const;
102
word word_at(u32bit n) const
103
{ return ((n < size()) ? reg[n] : 0); }
105
u32bit to_u32bit() const;
107
bool is_negative() const { return (sign() == Negative); }
108
bool is_positive() const { return (sign() == Positive); }
109
Sign sign() const { return (signedness); }
110
Sign reverse_sign() const;
115
u32bit size() const { return reg.size(); }
116
u32bit sig_words() const;
117
u32bit bytes() const;
120
const word* data() const { return reg.begin(); }
121
SecureVector<word>& get_reg() { return reg; }
122
void grow_reg(u32bit) const;
124
word& operator[](u32bit index) { return reg[index]; }
125
word operator[](u32bit index) const { return reg[index]; }
126
void clear() { reg.clear(); }
128
#ifndef BOTAN_MINIMAL_BIGINT
129
void randomize(u32bit = 0);
132
void binary_encode(byte[]) const;
133
void binary_decode(const byte[], u32bit);
134
u32bit encoded_size(Base = Binary) const;
136
static SecureVector<byte> encode(const BigInt&, Base = Binary);
137
static void encode(byte[], const BigInt&, Base = Binary);
138
static BigInt decode(const byte[], u32bit, Base = Binary);
139
static BigInt decode(const MemoryRegion<byte>&, Base = Binary);
140
static SecureVector<byte> encode_1363(const BigInt&, u32bit);
144
BigInt() { signedness = Positive; }
146
BigInt(const BigInt&);
147
BigInt(const std::string&);
148
BigInt(const byte[], u32bit, Base = Binary);
149
BigInt(Sign, u32bit);
150
#ifndef BOTAN_MINIMAL_BIGINT
151
BigInt(NumberType, u32bit);
154
void grow_to(u32bit) const;
155
SecureVector<word> reg;
159
/*************************************************
160
* Arithmetic Operators *
161
*************************************************/
162
BigInt operator+(const BigInt&, const BigInt&);
163
BigInt operator-(const BigInt&, const BigInt&);
164
BigInt operator*(const BigInt&, const BigInt&);
165
BigInt operator/(const BigInt&, const BigInt&);
166
BigInt operator%(const BigInt&, const BigInt&);
167
word operator%(const BigInt&, word);
168
BigInt operator<<(const BigInt&, u32bit);
169
BigInt operator>>(const BigInt&, u32bit);
171
/*************************************************
172
* Comparison Operators *
173
*************************************************/
174
inline bool operator==(const BigInt& a, const BigInt& b)
175
{ return (a.cmp(b) == 0); }
176
inline bool operator!=(const BigInt& a, const BigInt& b)
177
{ return (a.cmp(b) != 0); }
178
inline bool operator<=(const BigInt& a, const BigInt& b)
179
{ return (a.cmp(b) <= 0); }
180
inline bool operator>=(const BigInt& a, const BigInt& b)
181
{ return (a.cmp(b) >= 0); }
182
inline bool operator<(const BigInt& a, const BigInt& b)
183
{ return (a.cmp(b) < 0); }
184
inline bool operator>(const BigInt& a, const BigInt& b)
185
{ return (a.cmp(b) > 0); }
187
/*************************************************
189
*************************************************/
190
#ifndef BOTAN_MINIMAL_BIGINT
191
std::ostream& operator<<(std::ostream&, const BigInt&);
192
std::istream& operator>>(std::istream&, BigInt&);
197
#ifndef BOTAN_MINIMAL_BIGINT
201
inline void swap(Botan::BigInt& a, Botan::BigInt& b) { a.swap(b); }
204
namespace QCA { // WRAPNS_LINE