~ubuntu-branches/ubuntu/quantal/psi/quantal

« back to all changes in this revision

Viewing changes to third-party/qca/qca/src/botantools/botan/botan/bigint.h

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-04-14 18:57:30 UTC
  • mfrom: (2.1.9 hardy)
  • Revision ID: james.westby@ubuntu.com-20080414185730-528re3zp0m2hdlhi
Tags: 0.11-8
* added CONFIG -= link_prl to .pro files and removed dependencies
  which are made unnecessary by this change
* Fix segfault when closing last chat tab with qt4.4
  (This is from upstream svn, rev. 1101) (Closes: Bug#476122)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
62
   {
 
63
   public:
 
64
      enum Base { Octal = 8, Decimal = 10, Hexadecimal = 16, Binary = 256 };
 
65
      enum Sign { Negative = 0, Positive = 1 };
 
66
      enum NumberType { Random, Power2 };
 
67
 
 
68
      struct DivideByZero : public Exception
 
69
         { DivideByZero() : Exception("BigInt divide by zero") {} };
 
70
 
 
71
      BigInt& operator+=(const BigInt&);
 
72
      BigInt& operator-=(const BigInt&);
 
73
 
 
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);
 
80
 
 
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; }
 
85
 
 
86
      BigInt operator-() const;
 
87
      bool operator !() const { return (!is_nonzero()); }
 
88
 
 
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()); }
 
93
      bool is_zero() const;
 
94
 
 
95
      void set_bit(u32bit);
 
96
      void clear_bit(u32bit);
 
97
      void mask_bits(u32bit);
 
98
 
 
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); }
 
104
 
 
105
      u32bit to_u32bit() const;
 
106
 
 
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;
 
111
      void flip_sign();
 
112
      void set_sign(Sign);
 
113
      BigInt abs() const;
 
114
 
 
115
      u32bit size() const { return reg.size(); }
 
116
      u32bit sig_words() const;
 
117
      u32bit bytes() const;
 
118
      u32bit bits() const;
 
119
 
 
120
      const word* data() const { return reg.begin(); }
 
121
      SecureVector<word>& get_reg() { return reg; }
 
122
      void grow_reg(u32bit) const;
 
123
 
 
124
      word& operator[](u32bit index) { return reg[index]; }
 
125
      word operator[](u32bit index) const { return reg[index]; }
 
126
      void clear() { reg.clear(); }
 
127
 
 
128
#ifndef BOTAN_MINIMAL_BIGINT
 
129
      void randomize(u32bit = 0);
 
130
#endif
 
131
 
 
132
      void binary_encode(byte[]) const;
 
133
      void binary_decode(const byte[], u32bit);
 
134
      u32bit encoded_size(Base = Binary) const;
 
135
 
 
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);
 
141
 
 
142
      void swap(BigInt&);
 
143
 
 
144
      BigInt() { signedness = Positive; }
 
145
      BigInt(u64bit);
 
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);
 
152
#endif
 
153
   private:
 
154
      void grow_to(u32bit) const;
 
155
      SecureVector<word> reg;
 
156
      Sign signedness;
 
157
   };
 
158
 
 
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);
 
170
 
 
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); }
 
186
 
 
187
/*************************************************
 
188
* I/O Operators                                  *
 
189
*************************************************/
 
190
#ifndef BOTAN_MINIMAL_BIGINT
 
191
std::ostream& operator<<(std::ostream&, const BigInt&);
 
192
std::istream& operator>>(std::istream&, BigInt&);
 
193
#endif
 
194
 
 
195
}
 
196
 
 
197
#ifndef BOTAN_MINIMAL_BIGINT
 
198
} // WRAPNS_LINE
 
199
namespace std {
 
200
 
 
201
inline void swap(Botan::BigInt& a, Botan::BigInt& b) { a.swap(b); }
 
202
 
 
203
}
 
204
namespace QCA { // WRAPNS_LINE
 
205
#endif
 
206
 
 
207
#endif
 
208
} // WRAPNS_LINE