qca
bigint.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 namespace QCA {
00028
00029
00030
00031
00032
00033 #ifndef BOTAN_BIGINT_H__
00034 #define BOTAN_BIGINT_H__
00035
00036 #ifdef BOTAN_MINIMAL_BIGINT
00037 }
00038 # include <botan/secmem.h>
00039 namespace QCA {
00040 }
00041 # include <botan/exceptn.h>
00042 namespace QCA {
00043 #else
00044 }
00045 # include <botan/base.h>
00046 namespace QCA {
00047 #endif
00048
00049 }
00050 #include <botan/mp_types.h>
00051 namespace QCA {
00052 }
00053 #include <iosfwd>
00054 namespace QCA {
00055
00056 namespace Botan {
00057
00058
00059
00060
00061 class BigInt
00062 {
00063 public:
00064 enum Base { Octal = 8, Decimal = 10, Hexadecimal = 16, Binary = 256 };
00065 enum Sign { Negative = 0, Positive = 1 };
00066 enum NumberType { Random, Power2 };
00067
00068 struct DivideByZero : public Exception
00069 { DivideByZero() : Exception("BigInt divide by zero") {} };
00070
00071 BigInt& operator+=(const BigInt&);
00072 BigInt& operator-=(const BigInt&);
00073
00074 BigInt& operator*=(const BigInt&);
00075 BigInt& operator/=(const BigInt&);
00076 BigInt& operator%=(const BigInt&);
00077 word operator%=(word);
00078 BigInt& operator<<=(u32bit);
00079 BigInt& operator>>=(u32bit);
00080
00081 BigInt& operator++() { return (*this += 1); }
00082 BigInt& operator--() { return (*this -= 1); }
00083 BigInt operator++(int) { BigInt x = (*this); ++(*this); return x; }
00084 BigInt operator--(int) { BigInt x = (*this); --(*this); return x; }
00085
00086 BigInt operator-() const;
00087 bool operator !() const { return (!is_nonzero()); }
00088
00089 s32bit cmp(const BigInt&, bool = true) const;
00090 bool is_even() const { return (get_bit(0) == 0); }
00091 bool is_odd() const { return (get_bit(0) == 1); }
00092 bool is_nonzero() const { return (!is_zero()); }
00093 bool is_zero() const;
00094
00095 void set_bit(u32bit);
00096 void clear_bit(u32bit);
00097 void mask_bits(u32bit);
00098
00099 bool get_bit(u32bit) const;
00100 u32bit get_substring(u32bit, u32bit) const;
00101 byte byte_at(u32bit) const;
00102 word word_at(u32bit n) const
00103 { return ((n < size()) ? reg[n] : 0); }
00104
00105 u32bit to_u32bit() const;
00106
00107 bool is_negative() const { return (sign() == Negative); }
00108 bool is_positive() const { return (sign() == Positive); }
00109 Sign sign() const { return (signedness); }
00110 Sign reverse_sign() const;
00111 void flip_sign();
00112 void set_sign(Sign);
00113 BigInt abs() const;
00114
00115 u32bit size() const { return reg.size(); }
00116 u32bit sig_words() const;
00117 u32bit bytes() const;
00118 u32bit bits() const;
00119
00120 const word* data() const { return reg.begin(); }
00121 SecureVector<word>& get_reg() { return reg; }
00122 void grow_reg(u32bit) const;
00123
00124 word& operator[](u32bit index) { return reg[index]; }
00125 word operator[](u32bit index) const { return reg[index]; }
00126 void clear() { reg.clear(); }
00127
00128 #ifndef BOTAN_MINIMAL_BIGINT
00129 void randomize(u32bit = 0);
00130 #endif
00131
00132 void binary_encode(byte[]) const;
00133 void binary_decode(const byte[], u32bit);
00134 u32bit encoded_size(Base = Binary) const;
00135
00136 static SecureVector<byte> encode(const BigInt&, Base = Binary);
00137 static void encode(byte[], const BigInt&, Base = Binary);
00138 static BigInt decode(const byte[], u32bit, Base = Binary);
00139 static BigInt decode(const MemoryRegion<byte>&, Base = Binary);
00140 static SecureVector<byte> encode_1363(const BigInt&, u32bit);
00141
00142 void swap(BigInt&);
00143
00144 BigInt() { signedness = Positive; }
00145 BigInt(u64bit);
00146 BigInt(const BigInt&);
00147 BigInt(const std::string&);
00148 BigInt(const byte[], u32bit, Base = Binary);
00149 BigInt(Sign, u32bit);
00150 #ifndef BOTAN_MINIMAL_BIGINT
00151 BigInt(NumberType, u32bit);
00152 #endif
00153 private:
00154 void grow_to(u32bit) const;
00155 SecureVector<word> reg;
00156 Sign signedness;
00157 };
00158
00159
00160
00161
00162 BigInt operator+(const BigInt&, const BigInt&);
00163 BigInt operator-(const BigInt&, const BigInt&);
00164 BigInt operator*(const BigInt&, const BigInt&);
00165 BigInt operator/(const BigInt&, const BigInt&);
00166 BigInt operator%(const BigInt&, const BigInt&);
00167 word operator%(const BigInt&, word);
00168 BigInt operator<<(const BigInt&, u32bit);
00169 BigInt operator>>(const BigInt&, u32bit);
00170
00171
00172
00173
00174 inline bool operator==(const BigInt& a, const BigInt& b)
00175 { return (a.cmp(b) == 0); }
00176 inline bool operator!=(const BigInt& a, const BigInt& b)
00177 { return (a.cmp(b) != 0); }
00178 inline bool operator<=(const BigInt& a, const BigInt& b)
00179 { return (a.cmp(b) <= 0); }
00180 inline bool operator>=(const BigInt& a, const BigInt& b)
00181 { return (a.cmp(b) >= 0); }
00182 inline bool operator<(const BigInt& a, const BigInt& b)
00183 { return (a.cmp(b) < 0); }
00184 inline bool operator>(const BigInt& a, const BigInt& b)
00185 { return (a.cmp(b) > 0); }
00186
00187
00188
00189
00190 #ifndef BOTAN_MINIMAL_BIGINT
00191 std::ostream& operator<<(std::ostream&, const BigInt&);
00192 std::istream& operator>>(std::istream&, BigInt&);
00193 #endif
00194
00195 }
00196
00197 #ifndef BOTAN_MINIMAL_BIGINT
00198 }
00199 namespace std {
00200
00201 inline void swap(Botan::BigInt& a, Botan::BigInt& b) { a.swap(b); }
00202
00203 }
00204 namespace QCA {
00205 #endif
00206
00207 #endif
00208 }