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

« back to all changes in this revision

Viewing changes to third-party/qca/qca/src/botantools/botan/big_ops2.cpp

  • 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 Assignment Operators Source File        *
 
30
* (C) 1999-2007 The Botan Project                *
 
31
*************************************************/
 
32
 
 
33
} // WRAPNS_LINE
 
34
#include <botan/bigint.h>
 
35
namespace QCA { // WRAPNS_LINE
 
36
} // WRAPNS_LINE
 
37
#include <botan/numthry.h>
 
38
namespace QCA { // WRAPNS_LINE
 
39
} // WRAPNS_LINE
 
40
#include <botan/mp_core.h>
 
41
namespace QCA { // WRAPNS_LINE
 
42
} // WRAPNS_LINE
 
43
#include <botan/bit_ops.h>
 
44
namespace QCA { // WRAPNS_LINE
 
45
} // WRAPNS_LINE
 
46
#include <botan/util.h>
 
47
namespace QCA { // WRAPNS_LINE
 
48
} // WRAPNS_LINE
 
49
#include <algorithm>
 
50
namespace QCA { // WRAPNS_LINE
 
51
 
 
52
namespace Botan {
 
53
 
 
54
/*************************************************
 
55
* Addition Operator                              *
 
56
*************************************************/
 
57
BigInt& BigInt::operator+=(const BigInt& y)
 
58
   {
 
59
   const u32bit x_sw = sig_words(), y_sw = y.sig_words();
 
60
 
 
61
#ifdef BOTAN_TYPES_QT
 
62
   const u32bit reg_size = qMax(x_sw, y_sw) + 1;
 
63
#else
 
64
   const u32bit reg_size = std::max(x_sw, y_sw) + 1;
 
65
#endif
 
66
   grow_to(reg_size);
 
67
 
 
68
   if((sign() == y.sign()))
 
69
      bigint_add2(get_reg(), reg_size - 1, y.data(), y_sw);
 
70
   else
 
71
      {
 
72
      s32bit relative_size = bigint_cmp(data(), x_sw, y.data(), y_sw);
 
73
 
 
74
      if(relative_size < 0)
 
75
         {
 
76
         SecureVector<word> z(reg_size - 1);
 
77
         bigint_sub3(z, y.data(), reg_size - 1, data(), x_sw);
 
78
         copy_mem(reg.begin(), z.begin(), z.size());
 
79
         set_sign(y.sign());
 
80
         }
 
81
      else if(relative_size == 0)
 
82
         {
 
83
         reg.clear();
 
84
         set_sign(Positive);
 
85
         }
 
86
      else if(relative_size > 0)
 
87
         bigint_sub2(get_reg(), x_sw, y.data(), y_sw);
 
88
      }
 
89
 
 
90
   return (*this);
 
91
   }
 
92
 
 
93
/*************************************************
 
94
* Subtraction Operator                           *
 
95
*************************************************/
 
96
BigInt& BigInt::operator-=(const BigInt& y)
 
97
   {
 
98
   const u32bit x_sw = sig_words(), y_sw = y.sig_words();
 
99
 
 
100
   s32bit relative_size = bigint_cmp(data(), x_sw, y.data(), y_sw);
 
101
 
 
102
#ifdef BOTAN_TYPES_QT
 
103
   const u32bit reg_size = qMax(x_sw, y_sw) + 1;
 
104
#else
 
105
   const u32bit reg_size = std::max(x_sw, y_sw) + 1;
 
106
#endif
 
107
   grow_to(reg_size);
 
108
 
 
109
   if(relative_size < 0)
 
110
      {
 
111
      if(sign() == y.sign())
 
112
         {
 
113
         SecureVector<word> z(reg_size - 1);
 
114
         bigint_sub3(z, y.data(), reg_size - 1, data(), x_sw);
 
115
         copy_mem(reg.begin(), z.begin(), z.size());
 
116
         }
 
117
      else
 
118
         bigint_add2(get_reg(), reg_size - 1, y.data(), y_sw);
 
119
 
 
120
      set_sign(y.reverse_sign());
 
121
      }
 
122
   else if(relative_size == 0)
 
123
      {
 
124
      if(sign() == y.sign())
 
125
         {
 
126
         reg.clear();
 
127
         set_sign(Positive);
 
128
         }
 
129
      else
 
130
         bigint_shl1(get_reg(), x_sw, 0, 1);
 
131
      }
 
132
   else if(relative_size > 0)
 
133
      {
 
134
      if(sign() == y.sign())
 
135
         bigint_sub2(get_reg(), x_sw, y.data(), y_sw);
 
136
      else
 
137
         bigint_add2(get_reg(), reg_size - 1, y.data(), y_sw);
 
138
      }
 
139
 
 
140
   return (*this);
 
141
   }
 
142
 
 
143
/*************************************************
 
144
* Multiplication Operator                        *
 
145
*************************************************/
 
146
BigInt& BigInt::operator*=(const BigInt& y)
 
147
   {
 
148
   const u32bit x_sw = sig_words(), y_sw = y.sig_words();
 
149
   set_sign((sign() == y.sign()) ? Positive : Negative);
 
150
 
 
151
   if(x_sw == 0 || y_sw == 0)
 
152
      {
 
153
      reg.clear();
 
154
      set_sign(Positive);
 
155
      }
 
156
   else if(x_sw == 1 && y_sw)
 
157
      {
 
158
      grow_to(y_sw + 2);
 
159
      bigint_linmul3(get_reg(), y.data(), y_sw, word_at(0));
 
160
      }
 
161
   else if(y_sw == 1 && x_sw)
 
162
      {
 
163
      grow_to(x_sw + 2);
 
164
      bigint_linmul2(get_reg(), x_sw, y.word_at(0));
 
165
      }
 
166
   else
 
167
      {
 
168
      grow_to(size() + y.size());
 
169
 
 
170
      SecureVector<word> z(data(), x_sw);
 
171
      SecureVector<word> workspace(size());
 
172
 
 
173
      bigint_mul(get_reg(), size(), workspace,
 
174
                 z, z.size(), x_sw,
 
175
                 y.data(), y.size(), y_sw);
 
176
      }
 
177
 
 
178
   return (*this);
 
179
   }
 
180
 
 
181
/*************************************************
 
182
* Division Operator                              *
 
183
*************************************************/
 
184
BigInt& BigInt::operator/=(const BigInt& y)
 
185
   {
 
186
   if(y.sig_words() == 1 && power_of_2(y.word_at(0)))
 
187
      (*this) >>= (y.bits() - 1);
 
188
   else
 
189
      (*this) = (*this) / y;
 
190
   return (*this);
 
191
   }
 
192
 
 
193
/*************************************************
 
194
* Modulo Operator                                *
 
195
*************************************************/
 
196
BigInt& BigInt::operator%=(const BigInt& mod)
 
197
   {
 
198
   return (*this = (*this) % mod);
 
199
   }
 
200
 
 
201
/*************************************************
 
202
* Modulo Operator                                *
 
203
*************************************************/
 
204
word BigInt::operator%=(word mod)
 
205
   {
 
206
   if(mod == 0)
 
207
      throw BigInt::DivideByZero();
 
208
   if(power_of_2(mod))
 
209
       {
 
210
       word result = (word_at(0) & (mod - 1));
 
211
       clear();
 
212
       grow_to(2);
 
213
       reg[0] = result;
 
214
       return result;
 
215
       }
 
216
 
 
217
   word remainder = 0;
 
218
 
 
219
   for(u32bit j = sig_words(); j > 0; --j)
 
220
      remainder = bigint_modop(remainder, word_at(j-1), mod);
 
221
   clear();
 
222
   grow_to(2);
 
223
 
 
224
   if(remainder && sign() == BigInt::Negative)
 
225
      reg[0] = mod - remainder;
 
226
   else
 
227
      reg[0] = remainder;
 
228
 
 
229
   set_sign(BigInt::Positive);
 
230
 
 
231
   return word_at(0);
 
232
   }
 
233
 
 
234
/*************************************************
 
235
* Left Shift Operator                            *
 
236
*************************************************/
 
237
BigInt& BigInt::operator<<=(u32bit shift)
 
238
   {
 
239
   if(shift)
 
240
      {
 
241
      const u32bit shift_words = shift / MP_WORD_BITS,
 
242
                   shift_bits  = shift % MP_WORD_BITS,
 
243
                   words = sig_words();
 
244
 
 
245
      grow_to(words + shift_words + (shift_bits ? 1 : 0));
 
246
      bigint_shl1(get_reg(), words, shift_words, shift_bits);
 
247
      }
 
248
 
 
249
   return (*this);
 
250
   }
 
251
 
 
252
/*************************************************
 
253
* Right Shift Operator                           *
 
254
*************************************************/
 
255
BigInt& BigInt::operator>>=(u32bit shift)
 
256
   {
 
257
   if(shift)
 
258
      {
 
259
      const u32bit shift_words = shift / MP_WORD_BITS,
 
260
                   shift_bits  = shift % MP_WORD_BITS;
 
261
 
 
262
      bigint_shr1(get_reg(), sig_words(), shift_words, shift_bits);
 
263
 
 
264
      if(is_zero())
 
265
         set_sign(Positive);
 
266
      }
 
267
 
 
268
   return (*this);
 
269
   }
 
270
 
 
271
}
 
272
} // WRAPNS_LINE