sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 2003-2010 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
* This component and the accompanying materials are made available
|
sl@0
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
sl@0
|
6 |
* which accompanies this distribution, and is available
|
sl@0
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* Initial Contributors:
|
sl@0
|
10 |
* Nokia Corporation - initial contribution.
|
sl@0
|
11 |
*
|
sl@0
|
12 |
* Contributors:
|
sl@0
|
13 |
*
|
sl@0
|
14 |
* Description:
|
sl@0
|
15 |
*
|
sl@0
|
16 |
*/
|
sl@0
|
17 |
|
sl@0
|
18 |
|
sl@0
|
19 |
#include <random.h>
|
sl@0
|
20 |
#include <bigint.h>
|
sl@0
|
21 |
#include <e32std.h>
|
sl@0
|
22 |
#include <euserext.h>
|
sl@0
|
23 |
#include <securityerr.h>
|
sl@0
|
24 |
#include "words.h"
|
sl@0
|
25 |
#include "algorithms.h"
|
sl@0
|
26 |
#include "windowslider.h"
|
sl@0
|
27 |
#include "stackinteger.h"
|
sl@0
|
28 |
#include "mont.h"
|
sl@0
|
29 |
|
sl@0
|
30 |
|
sl@0
|
31 |
/**
|
sl@0
|
32 |
* Creates a new buffer containing the big-endian binary representation of this
|
sl@0
|
33 |
* integer.
|
sl@0
|
34 |
*
|
sl@0
|
35 |
* Note that it does not support the exporting of negative integers.
|
sl@0
|
36 |
*
|
sl@0
|
37 |
* @return The new buffer.
|
sl@0
|
38 |
*
|
sl@0
|
39 |
* @leave KErrNegativeExportNotSupported If this instance is a negative integer.
|
sl@0
|
40 |
*
|
sl@0
|
41 |
*/
|
sl@0
|
42 |
EXPORT_C HBufC8* TInteger::BufferLC() const
|
sl@0
|
43 |
{
|
sl@0
|
44 |
if(IsNegative())
|
sl@0
|
45 |
{
|
sl@0
|
46 |
User::Leave(KErrNegativeExportNotSupported);
|
sl@0
|
47 |
}
|
sl@0
|
48 |
TUint bytes = ByteCount();
|
sl@0
|
49 |
HBufC8* buf = HBufC8::NewMaxLC(bytes);
|
sl@0
|
50 |
TUint8* bufPtr = (TUint8*)(buf->Ptr());
|
sl@0
|
51 |
TUint8* regPtr = (TUint8*)Ptr();
|
sl@0
|
52 |
|
sl@0
|
53 |
// we internally store the number little endian, as a string we want it big
|
sl@0
|
54 |
// endian
|
sl@0
|
55 |
for(TUint i=0,j=bytes-1; i<bytes; )
|
sl@0
|
56 |
{
|
sl@0
|
57 |
bufPtr[i++] = regPtr[j--];
|
sl@0
|
58 |
}
|
sl@0
|
59 |
return buf;
|
sl@0
|
60 |
}
|
sl@0
|
61 |
|
sl@0
|
62 |
EXPORT_C HBufC8* TInteger::BufferWithNoTruncationLC() const
|
sl@0
|
63 |
{
|
sl@0
|
64 |
if(IsNegative())
|
sl@0
|
65 |
{
|
sl@0
|
66 |
User::Leave(KErrNegativeExportNotSupported);
|
sl@0
|
67 |
}
|
sl@0
|
68 |
|
sl@0
|
69 |
TUint wordCount = Size();
|
sl@0
|
70 |
TUint bytes = (wordCount)*WORD_SIZE;
|
sl@0
|
71 |
|
sl@0
|
72 |
HBufC8* buf = HBufC8::NewMaxLC(bytes);
|
sl@0
|
73 |
TUint8* bufPtr = (TUint8*)(buf->Ptr());
|
sl@0
|
74 |
TUint8* regPtr = (TUint8*)Ptr();
|
sl@0
|
75 |
for(TUint i=0,j=bytes-1; i<bytes; )
|
sl@0
|
76 |
{
|
sl@0
|
77 |
bufPtr[i++] = regPtr[j--];
|
sl@0
|
78 |
}
|
sl@0
|
79 |
|
sl@0
|
80 |
return buf;
|
sl@0
|
81 |
}
|
sl@0
|
82 |
|
sl@0
|
83 |
/**
|
sl@0
|
84 |
* Gets the number of words required to represent this RInteger.
|
sl@0
|
85 |
*
|
sl@0
|
86 |
* @return The size of the integer in words.
|
sl@0
|
87 |
*
|
sl@0
|
88 |
*/
|
sl@0
|
89 |
EXPORT_C TUint TInteger::WordCount() const
|
sl@0
|
90 |
{
|
sl@0
|
91 |
return CountWords(Ptr(), Size());
|
sl@0
|
92 |
}
|
sl@0
|
93 |
|
sl@0
|
94 |
/**
|
sl@0
|
95 |
* Gets the number of bytes required to represent this RInteger.
|
sl@0
|
96 |
*
|
sl@0
|
97 |
* @return The size of the integer in bytes.
|
sl@0
|
98 |
*
|
sl@0
|
99 |
*/
|
sl@0
|
100 |
EXPORT_C TUint TInteger::ByteCount() const
|
sl@0
|
101 |
{
|
sl@0
|
102 |
TUint wordCount = WordCount();
|
sl@0
|
103 |
if(wordCount)
|
sl@0
|
104 |
{
|
sl@0
|
105 |
return (wordCount-1)*WORD_SIZE + BytePrecision((Ptr())[wordCount-1]);
|
sl@0
|
106 |
}
|
sl@0
|
107 |
else
|
sl@0
|
108 |
{
|
sl@0
|
109 |
return 0;
|
sl@0
|
110 |
}
|
sl@0
|
111 |
}
|
sl@0
|
112 |
|
sl@0
|
113 |
/**
|
sl@0
|
114 |
* Get the number of bits required to represent this RInteger.
|
sl@0
|
115 |
*
|
sl@0
|
116 |
* @return The size of the integer in bits.
|
sl@0
|
117 |
*
|
sl@0
|
118 |
*/
|
sl@0
|
119 |
EXPORT_C TUint TInteger::BitCount() const
|
sl@0
|
120 |
{
|
sl@0
|
121 |
TUint wordCount = WordCount();
|
sl@0
|
122 |
if(wordCount)
|
sl@0
|
123 |
{
|
sl@0
|
124 |
return (wordCount-1)*WORD_BITS + BitPrecision(Ptr()[wordCount-1]);
|
sl@0
|
125 |
}
|
sl@0
|
126 |
else
|
sl@0
|
127 |
{
|
sl@0
|
128 |
return 0;
|
sl@0
|
129 |
}
|
sl@0
|
130 |
}
|
sl@0
|
131 |
|
sl@0
|
132 |
|
sl@0
|
133 |
//These 3 declarations instantiate a constant 0, 1, 2 for ease of use and
|
sl@0
|
134 |
//quick construction elsewhere in the code. Note that the functions
|
sl@0
|
135 |
//returning references to this static data return const references as you can't
|
sl@0
|
136 |
//modify the ROM ;)
|
sl@0
|
137 |
//word 0: Size of storage in words
|
sl@0
|
138 |
//word 1: Pointer to storage
|
sl@0
|
139 |
//word 2: LSW of storage
|
sl@0
|
140 |
//word 3: MSW of storage
|
sl@0
|
141 |
//Note that the flag bits in word 1 (Ptr()) are zero in the case of a positive
|
sl@0
|
142 |
//stack based integer (SignBit == 0, IsHeapBasedBit == 0)
|
sl@0
|
143 |
const TUint KBigintZero[4] = {2, (TUint)(KBigintZero+2), 0, 0};
|
sl@0
|
144 |
const TUint KBigintOne[4] = {2, (TUint)(KBigintOne+2), 1, 0};
|
sl@0
|
145 |
const TUint KBigintTwo[4] = {2, (TUint)(KBigintTwo+2), 2, 0};
|
sl@0
|
146 |
|
sl@0
|
147 |
/**
|
sl@0
|
148 |
* Gets the TInteger that represents zero
|
sl@0
|
149 |
*
|
sl@0
|
150 |
* @return The TInteger representing zero
|
sl@0
|
151 |
*/
|
sl@0
|
152 |
EXPORT_C const TInteger& TInteger::Zero(void)
|
sl@0
|
153 |
{
|
sl@0
|
154 |
return *reinterpret_cast<const TStackInteger64*>(KBigintZero);
|
sl@0
|
155 |
}
|
sl@0
|
156 |
|
sl@0
|
157 |
/**
|
sl@0
|
158 |
* Gets the TInteger that represents one
|
sl@0
|
159 |
*
|
sl@0
|
160 |
* @return The TInteger representing one
|
sl@0
|
161 |
*/
|
sl@0
|
162 |
EXPORT_C const TInteger& TInteger::One(void)
|
sl@0
|
163 |
{
|
sl@0
|
164 |
return *reinterpret_cast<const TStackInteger64*>(KBigintOne);
|
sl@0
|
165 |
}
|
sl@0
|
166 |
|
sl@0
|
167 |
/**
|
sl@0
|
168 |
* Gets the TInteger that represents two
|
sl@0
|
169 |
*
|
sl@0
|
170 |
* @return The TInteger representing two
|
sl@0
|
171 |
*/
|
sl@0
|
172 |
EXPORT_C const TInteger& TInteger::Two(void)
|
sl@0
|
173 |
{
|
sl@0
|
174 |
return *reinterpret_cast<const TStackInteger64*>(KBigintTwo);
|
sl@0
|
175 |
}
|
sl@0
|
176 |
|
sl@0
|
177 |
EXPORT_C RInteger TInteger::PlusL(const TInteger& aOperand) const
|
sl@0
|
178 |
{
|
sl@0
|
179 |
RInteger sum;
|
sl@0
|
180 |
if (NotNegative())
|
sl@0
|
181 |
{
|
sl@0
|
182 |
if (aOperand.NotNegative())
|
sl@0
|
183 |
sum = PositiveAddL(*this, aOperand);
|
sl@0
|
184 |
else
|
sl@0
|
185 |
sum = PositiveSubtractL(*this, aOperand);
|
sl@0
|
186 |
}
|
sl@0
|
187 |
else
|
sl@0
|
188 |
{
|
sl@0
|
189 |
if (aOperand.NotNegative())
|
sl@0
|
190 |
sum = PositiveSubtractL(aOperand, *this);
|
sl@0
|
191 |
else
|
sl@0
|
192 |
{
|
sl@0
|
193 |
sum = PositiveAddL(*this, aOperand);
|
sl@0
|
194 |
sum.SetSign(TInteger::ENegative);
|
sl@0
|
195 |
}
|
sl@0
|
196 |
}
|
sl@0
|
197 |
return sum;
|
sl@0
|
198 |
}
|
sl@0
|
199 |
|
sl@0
|
200 |
EXPORT_C RInteger TInteger::MinusL(const TInteger& aOperand) const
|
sl@0
|
201 |
{
|
sl@0
|
202 |
RInteger diff;
|
sl@0
|
203 |
if (NotNegative())
|
sl@0
|
204 |
{
|
sl@0
|
205 |
if (aOperand.NotNegative())
|
sl@0
|
206 |
diff = PositiveSubtractL(*this, aOperand);
|
sl@0
|
207 |
else
|
sl@0
|
208 |
diff = PositiveAddL(*this, aOperand);
|
sl@0
|
209 |
}
|
sl@0
|
210 |
else
|
sl@0
|
211 |
{
|
sl@0
|
212 |
if (aOperand.NotNegative())
|
sl@0
|
213 |
{
|
sl@0
|
214 |
diff = PositiveAddL(*this, aOperand);
|
sl@0
|
215 |
diff.SetSign(TInteger::ENegative);
|
sl@0
|
216 |
}
|
sl@0
|
217 |
else
|
sl@0
|
218 |
diff = PositiveSubtractL(aOperand, *this);
|
sl@0
|
219 |
}
|
sl@0
|
220 |
return diff;
|
sl@0
|
221 |
}
|
sl@0
|
222 |
|
sl@0
|
223 |
EXPORT_C RInteger TInteger::TimesL(const TInteger& aOperand) const
|
sl@0
|
224 |
{
|
sl@0
|
225 |
RInteger product = PositiveMultiplyL(*this, aOperand);
|
sl@0
|
226 |
|
sl@0
|
227 |
if (NotNegative() != aOperand.NotNegative())
|
sl@0
|
228 |
{
|
sl@0
|
229 |
product.Negate();
|
sl@0
|
230 |
}
|
sl@0
|
231 |
return product;
|
sl@0
|
232 |
}
|
sl@0
|
233 |
|
sl@0
|
234 |
EXPORT_C RInteger TInteger::DividedByL(const TInteger& aOperand) const
|
sl@0
|
235 |
{
|
sl@0
|
236 |
RInteger quotient;
|
sl@0
|
237 |
RInteger remainder;
|
sl@0
|
238 |
DivideL(remainder, quotient, *this, aOperand);
|
sl@0
|
239 |
remainder.Close();
|
sl@0
|
240 |
return quotient;
|
sl@0
|
241 |
}
|
sl@0
|
242 |
|
sl@0
|
243 |
EXPORT_C RInteger TInteger::ModuloL(const TInteger& aOperand) const
|
sl@0
|
244 |
{
|
sl@0
|
245 |
RInteger remainder;
|
sl@0
|
246 |
RInteger quotient;
|
sl@0
|
247 |
DivideL(remainder, quotient, *this, aOperand);
|
sl@0
|
248 |
quotient.Close();
|
sl@0
|
249 |
return remainder;
|
sl@0
|
250 |
}
|
sl@0
|
251 |
|
sl@0
|
252 |
EXPORT_C TUint TInteger::ModuloL(TUint aOperand) const
|
sl@0
|
253 |
{
|
sl@0
|
254 |
if(!aOperand)
|
sl@0
|
255 |
{
|
sl@0
|
256 |
User::Leave(KErrDivideByZero);
|
sl@0
|
257 |
}
|
sl@0
|
258 |
return Modulo(*this, aOperand);
|
sl@0
|
259 |
}
|
sl@0
|
260 |
|
sl@0
|
261 |
EXPORT_C RInteger TInteger::ModularMultiplyL(const TInteger& aA, const TInteger& aB,
|
sl@0
|
262 |
const TInteger& aMod)
|
sl@0
|
263 |
{
|
sl@0
|
264 |
RInteger product = aA.TimesL(aB);
|
sl@0
|
265 |
CleanupStack::PushL(product);
|
sl@0
|
266 |
RInteger reduced = product.ModuloL(aMod);
|
sl@0
|
267 |
CleanupStack::PopAndDestroy(&product);
|
sl@0
|
268 |
return reduced;
|
sl@0
|
269 |
}
|
sl@0
|
270 |
|
sl@0
|
271 |
EXPORT_C RInteger TInteger::ModularExponentiateL(const TInteger& aBase,
|
sl@0
|
272 |
const TInteger& aExp, const TInteger& aMod)
|
sl@0
|
273 |
{
|
sl@0
|
274 |
CMontgomeryStructure* mont = CMontgomeryStructure::NewLC(aMod);
|
sl@0
|
275 |
RInteger result = RInteger::NewL(mont->ExponentiateL(aBase, aExp));
|
sl@0
|
276 |
CleanupStack::PopAndDestroy(mont);
|
sl@0
|
277 |
return result;
|
sl@0
|
278 |
}
|
sl@0
|
279 |
|
sl@0
|
280 |
EXPORT_C RInteger TInteger::GCDL(const TInteger& aOperand) const
|
sl@0
|
281 |
{
|
sl@0
|
282 |
//Binary GCD algorithm -- see HAC 14.4.1
|
sl@0
|
283 |
//with a slight variation -- our g counts shifts rather than actually
|
sl@0
|
284 |
//shifting. We then do one shift at the end.
|
sl@0
|
285 |
assert(NotNegative());
|
sl@0
|
286 |
assert(aOperand.NotNegative());
|
sl@0
|
287 |
|
sl@0
|
288 |
RInteger x = RInteger::NewL(*this);
|
sl@0
|
289 |
CleanupStack::PushL(x);
|
sl@0
|
290 |
RInteger y = RInteger::NewL(aOperand);
|
sl@0
|
291 |
CleanupStack::PushL(y);
|
sl@0
|
292 |
|
sl@0
|
293 |
// 1 Ensure x >= y
|
sl@0
|
294 |
if( x < y )
|
sl@0
|
295 |
{
|
sl@0
|
296 |
TClassSwap(x, y);
|
sl@0
|
297 |
}
|
sl@0
|
298 |
|
sl@0
|
299 |
TUint g = 0;
|
sl@0
|
300 |
// 2 while x and y even x <- x/2, y <- y/2
|
sl@0
|
301 |
while( x.IsEven() && y.IsEven() )
|
sl@0
|
302 |
{
|
sl@0
|
303 |
x >>= 1;
|
sl@0
|
304 |
y >>= 1;
|
sl@0
|
305 |
++g;
|
sl@0
|
306 |
}
|
sl@0
|
307 |
// 3 while x != 0
|
sl@0
|
308 |
while( x.NotZero() )
|
sl@0
|
309 |
{
|
sl@0
|
310 |
// 3.1 while x even x <- x/2
|
sl@0
|
311 |
while( x.IsEven() )
|
sl@0
|
312 |
{
|
sl@0
|
313 |
x >>= 1;
|
sl@0
|
314 |
}
|
sl@0
|
315 |
// 3.2 while y even y <- y/2
|
sl@0
|
316 |
while( y.IsEven() )
|
sl@0
|
317 |
{
|
sl@0
|
318 |
y >>= 1;
|
sl@0
|
319 |
}
|
sl@0
|
320 |
// 3.3 t <- abs(x-y)/2
|
sl@0
|
321 |
RInteger t = x.MinusL(y);
|
sl@0
|
322 |
t >>= 1;
|
sl@0
|
323 |
t.SetSign(TInteger::EPositive);
|
sl@0
|
324 |
|
sl@0
|
325 |
// 3.4 If x>=y then x <- t else y <- t
|
sl@0
|
326 |
if( x >= y )
|
sl@0
|
327 |
{
|
sl@0
|
328 |
x.Set(t);
|
sl@0
|
329 |
}
|
sl@0
|
330 |
else
|
sl@0
|
331 |
{
|
sl@0
|
332 |
y.Set(t);
|
sl@0
|
333 |
}
|
sl@0
|
334 |
}
|
sl@0
|
335 |
|
sl@0
|
336 |
// 4 Return (g*y) (equiv to y<<=g as our g was counting shifts not actually
|
sl@0
|
337 |
//shifting)
|
sl@0
|
338 |
y <<= g;
|
sl@0
|
339 |
CleanupStack::Pop(&y);
|
sl@0
|
340 |
CleanupStack::PopAndDestroy(&x);
|
sl@0
|
341 |
return y;
|
sl@0
|
342 |
}
|
sl@0
|
343 |
|
sl@0
|
344 |
EXPORT_C RInteger TInteger::InverseModL(const TInteger& aMod) const
|
sl@0
|
345 |
{
|
sl@0
|
346 |
assert(aMod.NotNegative());
|
sl@0
|
347 |
|
sl@0
|
348 |
RInteger result;
|
sl@0
|
349 |
if(IsNegative() || *this>=aMod)
|
sl@0
|
350 |
{
|
sl@0
|
351 |
RInteger temp = ModuloL(aMod);
|
sl@0
|
352 |
CleanupClosePushL(temp);
|
sl@0
|
353 |
result = temp.InverseModL(aMod);
|
sl@0
|
354 |
CleanupStack::PopAndDestroy(&temp);
|
sl@0
|
355 |
return result;
|
sl@0
|
356 |
}
|
sl@0
|
357 |
|
sl@0
|
358 |
if(aMod.IsEven())
|
sl@0
|
359 |
{
|
sl@0
|
360 |
if( !aMod || IsEven() )
|
sl@0
|
361 |
{
|
sl@0
|
362 |
return RInteger::NewL(Zero());
|
sl@0
|
363 |
}
|
sl@0
|
364 |
if( *this == One() )
|
sl@0
|
365 |
{
|
sl@0
|
366 |
return RInteger::NewL(One());
|
sl@0
|
367 |
}
|
sl@0
|
368 |
RInteger u = aMod.InverseModL(*this);
|
sl@0
|
369 |
CleanupClosePushL(u);
|
sl@0
|
370 |
if(!u)
|
sl@0
|
371 |
{
|
sl@0
|
372 |
result = RInteger::NewL(Zero());
|
sl@0
|
373 |
}
|
sl@0
|
374 |
else
|
sl@0
|
375 |
{
|
sl@0
|
376 |
//calculates (aMod*(*this-u)+1)/(*this)
|
sl@0
|
377 |
result = MinusL(u);
|
sl@0
|
378 |
CleanupClosePushL(result);
|
sl@0
|
379 |
result *= aMod;
|
sl@0
|
380 |
++result;
|
sl@0
|
381 |
result /= *this;
|
sl@0
|
382 |
CleanupStack::Pop(&result);
|
sl@0
|
383 |
}
|
sl@0
|
384 |
CleanupStack::PopAndDestroy(&u);
|
sl@0
|
385 |
return result;
|
sl@0
|
386 |
}
|
sl@0
|
387 |
|
sl@0
|
388 |
result = RInteger::NewEmptyL(aMod.Size());
|
sl@0
|
389 |
CleanupClosePushL(result);
|
sl@0
|
390 |
RInteger workspace = RInteger::NewEmptyL(aMod.Size() * 4);
|
sl@0
|
391 |
TUint k = AlmostInverse(result.Ptr(), workspace.Ptr(), Ptr(), Size(),
|
sl@0
|
392 |
aMod.Ptr(), aMod.Size());
|
sl@0
|
393 |
DivideByPower2Mod(result.Ptr(), result.Ptr(), k, aMod.Ptr(), aMod.Size());
|
sl@0
|
394 |
workspace.Close();
|
sl@0
|
395 |
CleanupStack::Pop(&result);
|
sl@0
|
396 |
|
sl@0
|
397 |
return result;
|
sl@0
|
398 |
}
|
sl@0
|
399 |
|
sl@0
|
400 |
EXPORT_C TInteger& TInteger::operator+=(const TInteger& aOperand)
|
sl@0
|
401 |
{
|
sl@0
|
402 |
this->Set(PlusL(aOperand));
|
sl@0
|
403 |
return *this;
|
sl@0
|
404 |
}
|
sl@0
|
405 |
|
sl@0
|
406 |
EXPORT_C TInteger& TInteger::operator-=(const TInteger& aOperand)
|
sl@0
|
407 |
{
|
sl@0
|
408 |
this->Set(MinusL(aOperand));
|
sl@0
|
409 |
return *this;
|
sl@0
|
410 |
}
|
sl@0
|
411 |
|
sl@0
|
412 |
EXPORT_C TInteger& TInteger::operator*=(const TInteger& aOperand)
|
sl@0
|
413 |
{
|
sl@0
|
414 |
this->Set(TimesL(aOperand));
|
sl@0
|
415 |
return *this;
|
sl@0
|
416 |
}
|
sl@0
|
417 |
|
sl@0
|
418 |
EXPORT_C TInteger& TInteger::operator/=(const TInteger& aOperand)
|
sl@0
|
419 |
{
|
sl@0
|
420 |
this->Set(DividedByL(aOperand));
|
sl@0
|
421 |
return *this;
|
sl@0
|
422 |
}
|
sl@0
|
423 |
|
sl@0
|
424 |
EXPORT_C TInteger& TInteger::operator%=(const TInteger& aOperand)
|
sl@0
|
425 |
{
|
sl@0
|
426 |
this->Set(ModuloL(aOperand));
|
sl@0
|
427 |
return *this;
|
sl@0
|
428 |
}
|
sl@0
|
429 |
|
sl@0
|
430 |
EXPORT_C TInteger& TInteger::operator+=(TInt aOperand)
|
sl@0
|
431 |
{
|
sl@0
|
432 |
TStackInteger64 operand(aOperand);
|
sl@0
|
433 |
*this += operand;
|
sl@0
|
434 |
return *this;
|
sl@0
|
435 |
}
|
sl@0
|
436 |
|
sl@0
|
437 |
EXPORT_C TInteger& TInteger::operator-=(TInt aOperand)
|
sl@0
|
438 |
{
|
sl@0
|
439 |
TStackInteger64 operand(aOperand);
|
sl@0
|
440 |
*this -= operand;
|
sl@0
|
441 |
return *this;
|
sl@0
|
442 |
}
|
sl@0
|
443 |
|
sl@0
|
444 |
EXPORT_C TInteger& TInteger::operator*=(TInt aOperand)
|
sl@0
|
445 |
{
|
sl@0
|
446 |
TStackInteger64 operand(aOperand);
|
sl@0
|
447 |
*this *= operand;
|
sl@0
|
448 |
return *this;
|
sl@0
|
449 |
}
|
sl@0
|
450 |
|
sl@0
|
451 |
EXPORT_C TInteger& TInteger::operator--()
|
sl@0
|
452 |
{
|
sl@0
|
453 |
if (IsNegative())
|
sl@0
|
454 |
{
|
sl@0
|
455 |
if (Increment(Ptr(), Size()))
|
sl@0
|
456 |
{
|
sl@0
|
457 |
CleanGrowL(2*Size());
|
sl@0
|
458 |
(Ptr())[Size()/2]=1;
|
sl@0
|
459 |
}
|
sl@0
|
460 |
}
|
sl@0
|
461 |
else
|
sl@0
|
462 |
{
|
sl@0
|
463 |
if (Decrement(Ptr(), Size()))
|
sl@0
|
464 |
{
|
sl@0
|
465 |
this->CopyL(-1);
|
sl@0
|
466 |
}
|
sl@0
|
467 |
}
|
sl@0
|
468 |
return *this;
|
sl@0
|
469 |
}
|
sl@0
|
470 |
|
sl@0
|
471 |
EXPORT_C TInteger& TInteger::operator++()
|
sl@0
|
472 |
{
|
sl@0
|
473 |
if(NotNegative())
|
sl@0
|
474 |
{
|
sl@0
|
475 |
if(Increment(Ptr(), Size()))
|
sl@0
|
476 |
{
|
sl@0
|
477 |
CleanGrowL(2*Size());
|
sl@0
|
478 |
(Ptr())[Size()/2]=1;
|
sl@0
|
479 |
}
|
sl@0
|
480 |
}
|
sl@0
|
481 |
else
|
sl@0
|
482 |
{
|
sl@0
|
483 |
DecrementNoCarry(Ptr(), Size());
|
sl@0
|
484 |
if(WordCount()==0)
|
sl@0
|
485 |
{
|
sl@0
|
486 |
this->CopyL(Zero());
|
sl@0
|
487 |
}
|
sl@0
|
488 |
}
|
sl@0
|
489 |
return *this;
|
sl@0
|
490 |
}
|
sl@0
|
491 |
|
sl@0
|
492 |
EXPORT_C TInteger& TInteger::operator <<=(TUint aBits)
|
sl@0
|
493 |
{
|
sl@0
|
494 |
const TUint wordCount = WordCount();
|
sl@0
|
495 |
const TUint shiftWords = aBits / WORD_BITS;
|
sl@0
|
496 |
const TUint shiftBits = aBits % WORD_BITS;
|
sl@0
|
497 |
|
sl@0
|
498 |
CleanGrowL(wordCount+BitsToWords(aBits));
|
sl@0
|
499 |
ShiftWordsLeftByWords(Ptr(), wordCount + shiftWords, shiftWords);
|
sl@0
|
500 |
ShiftWordsLeftByBits(Ptr()+shiftWords, wordCount + BitsToWords(shiftBits),
|
sl@0
|
501 |
shiftBits);
|
sl@0
|
502 |
return *this;
|
sl@0
|
503 |
}
|
sl@0
|
504 |
|
sl@0
|
505 |
EXPORT_C TInteger& TInteger::operator >>=(TUint aBits)
|
sl@0
|
506 |
{
|
sl@0
|
507 |
const TUint wordCount = WordCount();
|
sl@0
|
508 |
const TUint shiftWords = aBits / WORD_BITS;
|
sl@0
|
509 |
const TUint shiftBits = aBits % WORD_BITS;
|
sl@0
|
510 |
|
sl@0
|
511 |
ShiftWordsRightByWords(Ptr(), wordCount, shiftWords);
|
sl@0
|
512 |
if(wordCount > shiftWords)
|
sl@0
|
513 |
{
|
sl@0
|
514 |
ShiftWordsRightByBits(Ptr(), wordCount - shiftWords, shiftBits);
|
sl@0
|
515 |
}
|
sl@0
|
516 |
if(IsNegative() && WordCount()==0) // avoid negative 0
|
sl@0
|
517 |
{
|
sl@0
|
518 |
SetSign(EPositive);
|
sl@0
|
519 |
}
|
sl@0
|
520 |
return *this;
|
sl@0
|
521 |
}
|
sl@0
|
522 |
|
sl@0
|
523 |
EXPORT_C TInt TInteger::UnsignedCompare(const TInteger& aThat) const
|
sl@0
|
524 |
{
|
sl@0
|
525 |
TUint size = WordCount();
|
sl@0
|
526 |
TUint thatSize = aThat.WordCount();
|
sl@0
|
527 |
|
sl@0
|
528 |
if( size == thatSize )
|
sl@0
|
529 |
return Compare(Ptr(), aThat.Ptr(), size);
|
sl@0
|
530 |
else
|
sl@0
|
531 |
return size > thatSize ? 1 : -1;
|
sl@0
|
532 |
}
|
sl@0
|
533 |
|
sl@0
|
534 |
EXPORT_C TInt TInteger::SignedCompare(const TInteger& aThat) const
|
sl@0
|
535 |
{
|
sl@0
|
536 |
if (NotNegative())
|
sl@0
|
537 |
{
|
sl@0
|
538 |
if (aThat.NotNegative())
|
sl@0
|
539 |
return UnsignedCompare(aThat);
|
sl@0
|
540 |
else
|
sl@0
|
541 |
return 1;
|
sl@0
|
542 |
}
|
sl@0
|
543 |
else
|
sl@0
|
544 |
{
|
sl@0
|
545 |
if (aThat.NotNegative())
|
sl@0
|
546 |
return -1;
|
sl@0
|
547 |
else
|
sl@0
|
548 |
return -UnsignedCompare(aThat);
|
sl@0
|
549 |
}
|
sl@0
|
550 |
}
|
sl@0
|
551 |
|
sl@0
|
552 |
EXPORT_C TBool TInteger::operator!() const
|
sl@0
|
553 |
{
|
sl@0
|
554 |
//Ptr()[0] is just a quick way of weeding out non-zero numbers without
|
sl@0
|
555 |
//doing a full WordCount() == 0. Very good odds that a non-zero number
|
sl@0
|
556 |
//will have a bit set in the least significant word
|
sl@0
|
557 |
return IsNegative() ? EFalse : (Ptr()[0]==0 && WordCount()==0);
|
sl@0
|
558 |
}
|
sl@0
|
559 |
|
sl@0
|
560 |
EXPORT_C TInt TInteger::SignedCompare(TInt aInteger) const
|
sl@0
|
561 |
{
|
sl@0
|
562 |
TStackInteger64 temp(aInteger);
|
sl@0
|
563 |
return SignedCompare(temp);
|
sl@0
|
564 |
}
|
sl@0
|
565 |
|
sl@0
|
566 |
/* TBool IsPrimeL(void) const
|
sl@0
|
567 |
* and all primality related functions are implemented in primes.cpp */
|
sl@0
|
568 |
|
sl@0
|
569 |
EXPORT_C TBool TInteger::Bit(TUint aBitPos) const
|
sl@0
|
570 |
{
|
sl@0
|
571 |
if( aBitPos/WORD_BITS >= Size() )
|
sl@0
|
572 |
{
|
sl@0
|
573 |
return 0;
|
sl@0
|
574 |
}
|
sl@0
|
575 |
else
|
sl@0
|
576 |
{
|
sl@0
|
577 |
return (((Ptr())[aBitPos/WORD_BITS] >> (aBitPos % WORD_BITS)) & 1);
|
sl@0
|
578 |
}
|
sl@0
|
579 |
}
|
sl@0
|
580 |
|
sl@0
|
581 |
EXPORT_C void TInteger::SetBit(TUint aBitPos)
|
sl@0
|
582 |
{
|
sl@0
|
583 |
if( aBitPos/WORD_BITS < Size() )
|
sl@0
|
584 |
{
|
sl@0
|
585 |
ArraySetBit(Ptr(), aBitPos);
|
sl@0
|
586 |
}
|
sl@0
|
587 |
}
|
sl@0
|
588 |
|
sl@0
|
589 |
EXPORT_C void TInteger::Negate()
|
sl@0
|
590 |
{
|
sl@0
|
591 |
if(!!(*this)) //don't flip sign if *this==0
|
sl@0
|
592 |
{
|
sl@0
|
593 |
SetSign(TSign((~Sign())&KSignMask));
|
sl@0
|
594 |
}
|
sl@0
|
595 |
}
|
sl@0
|
596 |
|
sl@0
|
597 |
EXPORT_C void TInteger::CopyL(const TInteger& aInteger, TBool aAllowShrink)
|
sl@0
|
598 |
{
|
sl@0
|
599 |
if(aAllowShrink)
|
sl@0
|
600 |
{
|
sl@0
|
601 |
CleanResizeL(aInteger.Size());
|
sl@0
|
602 |
}
|
sl@0
|
603 |
else
|
sl@0
|
604 |
{
|
sl@0
|
605 |
CleanGrowL(aInteger.Size());
|
sl@0
|
606 |
}
|
sl@0
|
607 |
Construct(aInteger);
|
sl@0
|
608 |
}
|
sl@0
|
609 |
|
sl@0
|
610 |
EXPORT_C void TInteger::CopyL(TInt aInteger, TBool aAllowShrink)
|
sl@0
|
611 |
{
|
sl@0
|
612 |
if(aAllowShrink)
|
sl@0
|
613 |
{
|
sl@0
|
614 |
CleanResizeL(2);
|
sl@0
|
615 |
}
|
sl@0
|
616 |
else
|
sl@0
|
617 |
{
|
sl@0
|
618 |
CleanGrowL(2);
|
sl@0
|
619 |
}
|
sl@0
|
620 |
Construct(aInteger);
|
sl@0
|
621 |
}
|
sl@0
|
622 |
|
sl@0
|
623 |
EXPORT_C void TInteger::Set(const RInteger& aInteger)
|
sl@0
|
624 |
{
|
sl@0
|
625 |
assert(IsHeapBased());
|
sl@0
|
626 |
Mem::FillZ(Ptr(), WordsToBytes(Size()));
|
sl@0
|
627 |
User::Free(Ptr());
|
sl@0
|
628 |
iPtr = aInteger.iPtr;
|
sl@0
|
629 |
iSize = aInteger.iSize;
|
sl@0
|
630 |
}
|
sl@0
|
631 |
|
sl@0
|
632 |
RInteger TInteger::PositiveAddL(const TInteger &aA, const TInteger& aB) const
|
sl@0
|
633 |
{
|
sl@0
|
634 |
RInteger sum = RInteger::NewEmptyL(CryptoMax(aA.Size(), aB.Size()));
|
sl@0
|
635 |
const word aSize = aA.Size();
|
sl@0
|
636 |
const word bSize = aB.Size();
|
sl@0
|
637 |
const word* const aReg = aA.Ptr();
|
sl@0
|
638 |
const word* const bReg = aB.Ptr();
|
sl@0
|
639 |
word* const sumReg = sum.Ptr();
|
sl@0
|
640 |
|
sl@0
|
641 |
word carry;
|
sl@0
|
642 |
if (aSize == bSize)
|
sl@0
|
643 |
carry = Add(sumReg, aReg, bReg, aSize);
|
sl@0
|
644 |
else if (aSize > bSize)
|
sl@0
|
645 |
{
|
sl@0
|
646 |
carry = Add(sumReg, aReg, bReg, bSize);
|
sl@0
|
647 |
CopyWords(sumReg+bSize, aReg+bSize, aSize-bSize);
|
sl@0
|
648 |
carry = Increment(sumReg+bSize, aSize-bSize, carry);
|
sl@0
|
649 |
}
|
sl@0
|
650 |
else
|
sl@0
|
651 |
{
|
sl@0
|
652 |
carry = Add(sumReg, aReg, bReg, aSize);
|
sl@0
|
653 |
CopyWords(sumReg+aSize, bReg+aSize, bSize-aSize);
|
sl@0
|
654 |
carry = Increment(sumReg+aSize, bSize-aSize, carry);
|
sl@0
|
655 |
}
|
sl@0
|
656 |
|
sl@0
|
657 |
if (carry)
|
sl@0
|
658 |
{
|
sl@0
|
659 |
CleanupStack::PushL(sum);
|
sl@0
|
660 |
sum.CleanGrowL(2*sum.Size());
|
sl@0
|
661 |
CleanupStack::Pop(&sum);
|
sl@0
|
662 |
sum.Ptr()[sum.Size()/2] = 1;
|
sl@0
|
663 |
}
|
sl@0
|
664 |
sum.SetSign(TInteger::EPositive);
|
sl@0
|
665 |
return sum;
|
sl@0
|
666 |
}
|
sl@0
|
667 |
|
sl@0
|
668 |
RInteger TInteger::PositiveSubtractL(const TInteger &aA, const TInteger& aB) const
|
sl@0
|
669 |
{
|
sl@0
|
670 |
RInteger diff = RInteger::NewEmptyL(CryptoMax(aA.Size(), aB.Size()));
|
sl@0
|
671 |
unsigned aSize = aA.WordCount();
|
sl@0
|
672 |
aSize += aSize%2;
|
sl@0
|
673 |
unsigned bSize = aB.WordCount();
|
sl@0
|
674 |
bSize += bSize%2;
|
sl@0
|
675 |
const word* const aReg = aA.Ptr();
|
sl@0
|
676 |
const word* const bReg = aB.Ptr();
|
sl@0
|
677 |
word* const diffReg = diff.Ptr();
|
sl@0
|
678 |
|
sl@0
|
679 |
if (aSize == bSize)
|
sl@0
|
680 |
{
|
sl@0
|
681 |
if (Compare(aReg, bReg, aSize) >= 0)
|
sl@0
|
682 |
{
|
sl@0
|
683 |
Subtract(diffReg, aReg, bReg, aSize);
|
sl@0
|
684 |
diff.SetSign(TInteger::EPositive);
|
sl@0
|
685 |
}
|
sl@0
|
686 |
else
|
sl@0
|
687 |
{
|
sl@0
|
688 |
Subtract(diffReg, bReg, aReg, aSize);
|
sl@0
|
689 |
diff.SetSign(TInteger::ENegative);
|
sl@0
|
690 |
}
|
sl@0
|
691 |
}
|
sl@0
|
692 |
else if (aSize > bSize)
|
sl@0
|
693 |
{
|
sl@0
|
694 |
word borrow = Subtract(diffReg, aReg, bReg, bSize);
|
sl@0
|
695 |
CopyWords(diffReg+bSize, aReg+bSize, aSize-bSize);
|
sl@0
|
696 |
borrow = Decrement(diffReg+bSize, aSize-bSize, borrow);
|
sl@0
|
697 |
assert(!borrow);
|
sl@0
|
698 |
diff.SetSign(TInteger::EPositive);
|
sl@0
|
699 |
}
|
sl@0
|
700 |
else
|
sl@0
|
701 |
{
|
sl@0
|
702 |
word borrow = Subtract(diffReg, bReg, aReg, aSize);
|
sl@0
|
703 |
CopyWords(diffReg+aSize, bReg+aSize, bSize-aSize);
|
sl@0
|
704 |
borrow = Decrement(diffReg+aSize, bSize-aSize, borrow);
|
sl@0
|
705 |
assert(!borrow);
|
sl@0
|
706 |
diff.SetSign(TInteger::ENegative);
|
sl@0
|
707 |
}
|
sl@0
|
708 |
return diff;
|
sl@0
|
709 |
}
|
sl@0
|
710 |
|
sl@0
|
711 |
RInteger TInteger::PositiveMultiplyL(const TInteger &aA, const TInteger &aB) const
|
sl@0
|
712 |
{
|
sl@0
|
713 |
unsigned aSize = RoundupSize(aA.WordCount());
|
sl@0
|
714 |
unsigned bSize = RoundupSize(aB.WordCount());
|
sl@0
|
715 |
|
sl@0
|
716 |
RInteger product = RInteger::NewEmptyL(aSize+bSize);
|
sl@0
|
717 |
CleanupClosePushL(product);
|
sl@0
|
718 |
|
sl@0
|
719 |
RInteger workspace = RInteger::NewEmptyL(aSize + bSize);
|
sl@0
|
720 |
AsymmetricMultiply(product.Ptr(), workspace.Ptr(), aA.Ptr(), aSize, aB.Ptr(),
|
sl@0
|
721 |
bSize);
|
sl@0
|
722 |
workspace.Close();
|
sl@0
|
723 |
CleanupStack::Pop(&product);
|
sl@0
|
724 |
return product;
|
sl@0
|
725 |
}
|
sl@0
|
726 |
|
sl@0
|
727 |
TUint TInteger::Modulo(const TInteger& aDividend, TUint aDivisor) const
|
sl@0
|
728 |
{
|
sl@0
|
729 |
assert(aDivisor);
|
sl@0
|
730 |
TUint i = aDividend.WordCount();
|
sl@0
|
731 |
TUint remainder = 0;
|
sl@0
|
732 |
while(i--)
|
sl@0
|
733 |
{
|
sl@0
|
734 |
remainder = TUint(MAKE_DWORD(aDividend.Ptr()[i], remainder) % aDivisor);
|
sl@0
|
735 |
}
|
sl@0
|
736 |
return remainder;
|
sl@0
|
737 |
}
|
sl@0
|
738 |
|
sl@0
|
739 |
void TInteger::PositiveDivideL(RInteger &aRemainder, RInteger &aQuotient,
|
sl@0
|
740 |
const TInteger &aDividend, const TInteger &aDivisor) const
|
sl@0
|
741 |
{
|
sl@0
|
742 |
unsigned dividendSize = aDividend.WordCount();
|
sl@0
|
743 |
unsigned divisorSize = aDivisor.WordCount();
|
sl@0
|
744 |
|
sl@0
|
745 |
if (!divisorSize)
|
sl@0
|
746 |
{
|
sl@0
|
747 |
User::Leave(KErrDivideByZero);
|
sl@0
|
748 |
}
|
sl@0
|
749 |
|
sl@0
|
750 |
if (aDividend.UnsignedCompare(aDivisor) == -1)
|
sl@0
|
751 |
{
|
sl@0
|
752 |
aRemainder.CreateNewL(aDividend.Size());
|
sl@0
|
753 |
CleanupStack::PushL(aRemainder);
|
sl@0
|
754 |
aRemainder.CopyL(aDividend); //set remainder to a
|
sl@0
|
755 |
aRemainder.SetSign(TInteger::EPositive);
|
sl@0
|
756 |
aQuotient.CleanNewL(2); //Set quotient to zero
|
sl@0
|
757 |
CleanupStack::Pop(&aRemainder);
|
sl@0
|
758 |
return;
|
sl@0
|
759 |
}
|
sl@0
|
760 |
|
sl@0
|
761 |
dividendSize += dividendSize%2; // round up to next even number
|
sl@0
|
762 |
divisorSize += divisorSize%2;
|
sl@0
|
763 |
|
sl@0
|
764 |
aRemainder.CleanNewL(divisorSize);
|
sl@0
|
765 |
CleanupStack::PushL(aRemainder);
|
sl@0
|
766 |
aQuotient.CleanNewL(dividendSize-divisorSize+2);
|
sl@0
|
767 |
CleanupStack::PushL(aQuotient);
|
sl@0
|
768 |
|
sl@0
|
769 |
RInteger T = RInteger::NewEmptyL(dividendSize+2*divisorSize+4);
|
sl@0
|
770 |
Divide(aRemainder.Ptr(), aQuotient.Ptr(), T.Ptr(), aDividend.Ptr(),
|
sl@0
|
771 |
dividendSize, aDivisor.Ptr(), divisorSize);
|
sl@0
|
772 |
T.Close();
|
sl@0
|
773 |
CleanupStack::Pop(2, &aRemainder); //aQuotient, aRemainder
|
sl@0
|
774 |
}
|
sl@0
|
775 |
|
sl@0
|
776 |
void TInteger::DivideL(RInteger& aRemainder, RInteger& aQuotient,
|
sl@0
|
777 |
const TInteger& aDividend, const TInteger& aDivisor) const
|
sl@0
|
778 |
{
|
sl@0
|
779 |
PositiveDivideL(aRemainder, aQuotient, aDividend, aDivisor);
|
sl@0
|
780 |
|
sl@0
|
781 |
if (aDividend.IsNegative())
|
sl@0
|
782 |
{
|
sl@0
|
783 |
aQuotient.Negate();
|
sl@0
|
784 |
if (aRemainder.NotZero())
|
sl@0
|
785 |
{
|
sl@0
|
786 |
--aQuotient;
|
sl@0
|
787 |
assert(aRemainder.Size() <= aDivisor.Size());
|
sl@0
|
788 |
Subtract(aRemainder.Ptr(), aDivisor.Ptr(), aRemainder.Ptr(),
|
sl@0
|
789 |
aRemainder.Size());
|
sl@0
|
790 |
}
|
sl@0
|
791 |
}
|
sl@0
|
792 |
|
sl@0
|
793 |
if (aDivisor.IsNegative())
|
sl@0
|
794 |
aQuotient.Negate();
|
sl@0
|
795 |
}
|
sl@0
|
796 |
|
sl@0
|
797 |
void TInteger::RandomizeL(TUint aBits, TRandomAttribute aAttr)
|
sl@0
|
798 |
{
|
sl@0
|
799 |
if(!aBits)
|
sl@0
|
800 |
{
|
sl@0
|
801 |
return;
|
sl@0
|
802 |
}
|
sl@0
|
803 |
const TUint bytes = BitsToBytes(aBits);
|
sl@0
|
804 |
const TUint words = BitsToWords(aBits);
|
sl@0
|
805 |
CleanGrowL(words);
|
sl@0
|
806 |
TPtr8 buf((TUint8*)(Ptr()), bytes, WordsToBytes(Size()));
|
sl@0
|
807 |
TUint bitpos = aBits % BYTE_BITS;
|
sl@0
|
808 |
TRAPD(err, GenerateRandomBytesL(buf));
|
sl@0
|
809 |
if((err != KErrNone) && (err != KErrNotSecure))
|
sl@0
|
810 |
User::Leave(err);
|
sl@0
|
811 |
//mask with 0 all bits above the num requested in the most significant byte
|
sl@0
|
812 |
if(bitpos)
|
sl@0
|
813 |
{
|
sl@0
|
814 |
buf[bytes-1] = TUint8( buf[bytes-1] & ((1L << bitpos) - 1) );
|
sl@0
|
815 |
}
|
sl@0
|
816 |
//set most significant (top) bit
|
sl@0
|
817 |
if(aAttr == ETopBitSet || aAttr == ETop2BitsSet)
|
sl@0
|
818 |
{
|
sl@0
|
819 |
SetBit(aBits-1); //Set bit counts from 0
|
sl@0
|
820 |
assert(BitCount() == aBits);
|
sl@0
|
821 |
assert(Bit(aBits-1));
|
sl@0
|
822 |
}
|
sl@0
|
823 |
//set 2nd bit from top
|
sl@0
|
824 |
if(aAttr == ETop2BitsSet)
|
sl@0
|
825 |
{
|
sl@0
|
826 |
SetBit(aBits-2); //Set bit counts from 0
|
sl@0
|
827 |
assert(BitCount() == aBits);
|
sl@0
|
828 |
assert(Bit(aBits-1));
|
sl@0
|
829 |
assert(Bit(aBits-2));
|
sl@0
|
830 |
}
|
sl@0
|
831 |
}
|
sl@0
|
832 |
|
sl@0
|
833 |
void TInteger::RandomizeL(const TInteger& aMin, const TInteger& aMax)
|
sl@0
|
834 |
{
|
sl@0
|
835 |
assert(aMax > aMin);
|
sl@0
|
836 |
assert(aMin.NotNegative());
|
sl@0
|
837 |
RInteger range = RInteger::NewL(aMax);
|
sl@0
|
838 |
CleanupStack::PushL(range);
|
sl@0
|
839 |
range -= aMin;
|
sl@0
|
840 |
const TUint bits = range.BitCount();
|
sl@0
|
841 |
|
sl@0
|
842 |
//if we find a number < range then aMin+range < aMax
|
sl@0
|
843 |
do
|
sl@0
|
844 |
{
|
sl@0
|
845 |
RandomizeL(bits, EAllBitsRandom);
|
sl@0
|
846 |
}
|
sl@0
|
847 |
while(*this > range);
|
sl@0
|
848 |
|
sl@0
|
849 |
*this += aMin;
|
sl@0
|
850 |
CleanupStack::PopAndDestroy(&range);
|
sl@0
|
851 |
}
|
sl@0
|
852 |
|
sl@0
|
853 |
/* void PrimeRandomizeL(TUint aBits, TRandomAttribute aAttr)
|
sl@0
|
854 |
* and all primality related functions are implemented in primes.cpp */
|
sl@0
|
855 |
|
sl@0
|
856 |
void TInteger::CreateNewL(TUint aNewSize)
|
sl@0
|
857 |
{
|
sl@0
|
858 |
//should only be called on construction
|
sl@0
|
859 |
assert(!iPtr);
|
sl@0
|
860 |
|
sl@0
|
861 |
TUint newSize = RoundupSize(aNewSize);
|
sl@0
|
862 |
SetPtr((TUint*)User::AllocL(WordsToBytes(newSize)));
|
sl@0
|
863 |
SetSize(newSize);
|
sl@0
|
864 |
SetHeapBased();
|
sl@0
|
865 |
}
|
sl@0
|
866 |
|
sl@0
|
867 |
void TInteger::CleanNewL(TUint aNewSize)
|
sl@0
|
868 |
{
|
sl@0
|
869 |
CreateNewL(aNewSize);
|
sl@0
|
870 |
Mem::FillZ(Ptr(), WordsToBytes(Size())); //clear integer storage
|
sl@0
|
871 |
}
|
sl@0
|
872 |
|
sl@0
|
873 |
void TInteger::CleanGrowL(TUint aNewSize)
|
sl@0
|
874 |
{
|
sl@0
|
875 |
assert(IsHeapBased());
|
sl@0
|
876 |
TUint newSize = RoundupSize(aNewSize);
|
sl@0
|
877 |
TUint oldSize = Size();
|
sl@0
|
878 |
if(newSize > oldSize)
|
sl@0
|
879 |
{
|
sl@0
|
880 |
TUint* oldPtr = Ptr();
|
sl@0
|
881 |
//1) allocate new memory and set ptr and size
|
sl@0
|
882 |
SetPtr((TUint*)User::AllocL(WordsToBytes(newSize)));
|
sl@0
|
883 |
SetSize(newSize);
|
sl@0
|
884 |
//2) copy old mem to new mem
|
sl@0
|
885 |
Mem::Copy(Ptr(), oldPtr, WordsToBytes(oldSize));
|
sl@0
|
886 |
//3) zero all old memory
|
sl@0
|
887 |
Mem::FillZ(oldPtr, WordsToBytes(oldSize));
|
sl@0
|
888 |
//4) give back old memory
|
sl@0
|
889 |
User::Free(oldPtr);
|
sl@0
|
890 |
//5) zero new memory from end of copy to end of growth
|
sl@0
|
891 |
Mem::FillZ(Ptr() + oldSize, WordsToBytes(newSize-oldSize));
|
sl@0
|
892 |
}
|
sl@0
|
893 |
}
|
sl@0
|
894 |
|
sl@0
|
895 |
void TInteger::CleanResizeL(TUint aNewSize)
|
sl@0
|
896 |
{
|
sl@0
|
897 |
assert(IsHeapBased());
|
sl@0
|
898 |
TUint newSize = RoundupSize(aNewSize);
|
sl@0
|
899 |
TUint oldSize = Size();
|
sl@0
|
900 |
if(newSize > oldSize)
|
sl@0
|
901 |
{
|
sl@0
|
902 |
CleanGrowL(aNewSize);
|
sl@0
|
903 |
}
|
sl@0
|
904 |
else if(newSize < oldSize)
|
sl@0
|
905 |
{
|
sl@0
|
906 |
TUint* oldPtr = Ptr();
|
sl@0
|
907 |
//1) zero memory above newsize
|
sl@0
|
908 |
Mem::FillZ(oldPtr+WordsToBytes(aNewSize),WordsToBytes(oldSize-newSize));
|
sl@0
|
909 |
//2) ReAlloc cell. Since our newsize is less than oldsize, it is
|
sl@0
|
910 |
//guarenteed not to move. Thus this is just freeing part of our old
|
sl@0
|
911 |
//cell to the heap for other uses.
|
sl@0
|
912 |
SetPtr((TUint*)User::ReAllocL(Ptr(), WordsToBytes(newSize)));
|
sl@0
|
913 |
SetSize(newSize);
|
sl@0
|
914 |
}
|
sl@0
|
915 |
}
|
sl@0
|
916 |
|
sl@0
|
917 |
EXPORT_C TInteger::TInteger() : iSize(0), iPtr(0)
|
sl@0
|
918 |
{
|
sl@0
|
919 |
}
|
sl@0
|
920 |
|
sl@0
|
921 |
void TInteger::Construct(const TDesC8& aValue)
|
sl@0
|
922 |
{
|
sl@0
|
923 |
assert(Size() >= BytesToWords(aValue.Size()));
|
sl@0
|
924 |
if(aValue.Size() > 0)
|
sl@0
|
925 |
{
|
sl@0
|
926 |
//People write numbers with the most significant digits first (big
|
sl@0
|
927 |
//endian) but we store our numbers in little endian. Hence we need to
|
sl@0
|
928 |
//reverse the string by bytes.
|
sl@0
|
929 |
|
sl@0
|
930 |
TUint bytes = aValue.Size();
|
sl@0
|
931 |
TUint8* i = (TUint8*)Ptr();
|
sl@0
|
932 |
TUint8* j = (TUint8*)aValue.Ptr() + bytes;
|
sl@0
|
933 |
|
sl@0
|
934 |
//Swap the endianess of the number itself
|
sl@0
|
935 |
// (msb) 01 02 03 04 05 06 (lsb) becomes ->
|
sl@0
|
936 |
// (lsb) 06 05 04 03 02 01 (msb)
|
sl@0
|
937 |
while( j != (TUint8*)aValue.Ptr() )
|
sl@0
|
938 |
{
|
sl@0
|
939 |
*i++ = *--j;
|
sl@0
|
940 |
}
|
sl@0
|
941 |
Mem::FillZ((TUint8*)Ptr() + bytes, WordsToBytes(Size()) - bytes);
|
sl@0
|
942 |
}
|
sl@0
|
943 |
else
|
sl@0
|
944 |
{
|
sl@0
|
945 |
//if size is zero, we zero the whole register
|
sl@0
|
946 |
Mem::FillZ((TUint8*)Ptr(), WordsToBytes(Size()));
|
sl@0
|
947 |
}
|
sl@0
|
948 |
SetSign(EPositive);
|
sl@0
|
949 |
}
|
sl@0
|
950 |
|
sl@0
|
951 |
void TInteger::Construct(const TInteger& aInteger)
|
sl@0
|
952 |
{
|
sl@0
|
953 |
assert(Size() >= aInteger.Size());
|
sl@0
|
954 |
CopyWords(Ptr(), aInteger.Ptr(), aInteger.Size());
|
sl@0
|
955 |
if(Size() > aInteger.Size())
|
sl@0
|
956 |
{
|
sl@0
|
957 |
Mem::FillZ(Ptr()+aInteger.Size(), WordsToBytes(Size()-aInteger.Size()));
|
sl@0
|
958 |
}
|
sl@0
|
959 |
SetSign(aInteger.Sign());
|
sl@0
|
960 |
}
|
sl@0
|
961 |
|
sl@0
|
962 |
void TInteger::Construct(TInt aInteger)
|
sl@0
|
963 |
{
|
sl@0
|
964 |
Construct((TUint)aInteger);
|
sl@0
|
965 |
if(aInteger < 0)
|
sl@0
|
966 |
{
|
sl@0
|
967 |
SetSign(ENegative);
|
sl@0
|
968 |
Ptr()[0] = -aInteger;
|
sl@0
|
969 |
}
|
sl@0
|
970 |
}
|
sl@0
|
971 |
|
sl@0
|
972 |
void TInteger::Construct(TUint aInteger)
|
sl@0
|
973 |
{
|
sl@0
|
974 |
assert(Size() >= 2);
|
sl@0
|
975 |
SetSign(EPositive);
|
sl@0
|
976 |
Ptr()[0] = aInteger;
|
sl@0
|
977 |
Mem::FillZ(Ptr()+1, WordsToBytes(Size()-1));
|
sl@0
|
978 |
}
|
sl@0
|
979 |
|
sl@0
|
980 |
void TInteger::ConstructStack(TUint aWords, TUint aInteger)
|
sl@0
|
981 |
{
|
sl@0
|
982 |
SetPtr((TUint*)(this)+2);
|
sl@0
|
983 |
//SetStackBased(); //Not strictly needed as stackbased is a 0 at bit 1
|
sl@0
|
984 |
SetSize(aWords);
|
sl@0
|
985 |
assert(Size() >= 2);
|
sl@0
|
986 |
Ptr()[0] = aInteger;
|
sl@0
|
987 |
Mem::FillZ(&(Ptr()[1]), WordsToBytes(Size()-1));
|
sl@0
|
988 |
}
|
sl@0
|
989 |
|
sl@0
|
990 |
void TInteger::ConstructStack(TUint aWords, const TInteger& aInteger)
|
sl@0
|
991 |
{
|
sl@0
|
992 |
SetPtr((TUint*)(this)+2);
|
sl@0
|
993 |
//SetStackBased(); //Not strictly needed as stackbased is a 0 at bit 1
|
sl@0
|
994 |
SetSize(aWords);
|
sl@0
|
995 |
assert( Size() >= RoundupSize(aInteger.WordCount()) );
|
sl@0
|
996 |
CopyWords(Ptr(), aInteger.Ptr(), aInteger.Size());
|
sl@0
|
997 |
Mem::FillZ(Ptr()+aInteger.Size(), WordsToBytes(Size()-aInteger.Size()));
|
sl@0
|
998 |
}
|
sl@0
|
999 |
|
sl@0
|
1000 |
// Methods are excluded from coverage due to the problem with BullsEye on ONB.
|
sl@0
|
1001 |
// Manually verified that these methods are functionally covered.
|
sl@0
|
1002 |
#ifdef _BullseyeCoverage
|
sl@0
|
1003 |
#pragma suppress_warnings on
|
sl@0
|
1004 |
#pragma BullseyeCoverage off
|
sl@0
|
1005 |
#pragma suppress_warnings off
|
sl@0
|
1006 |
#endif
|
sl@0
|
1007 |
|
sl@0
|
1008 |
EXPORT_C TInteger& TInteger::operator/=(TInt aOperand)
|
sl@0
|
1009 |
{
|
sl@0
|
1010 |
TStackInteger64 operand(aOperand);
|
sl@0
|
1011 |
*this /= operand;
|
sl@0
|
1012 |
return *this;
|
sl@0
|
1013 |
}
|
sl@0
|
1014 |
|
sl@0
|
1015 |
EXPORT_C TInteger& TInteger::operator%=(TInt aOperand)
|
sl@0
|
1016 |
{
|
sl@0
|
1017 |
TStackInteger64 operand(aOperand);
|
sl@0
|
1018 |
assert(operand.NotNegative());
|
sl@0
|
1019 |
*this %= operand;
|
sl@0
|
1020 |
return *this;
|
sl@0
|
1021 |
}
|
sl@0
|
1022 |
|
sl@0
|
1023 |
EXPORT_C TInt TInteger::ConvertToLongL(void) const
|
sl@0
|
1024 |
{
|
sl@0
|
1025 |
if(!IsConvertableToLong())
|
sl@0
|
1026 |
{
|
sl@0
|
1027 |
User::Leave(KErrTotalLossOfPrecision);
|
sl@0
|
1028 |
}
|
sl@0
|
1029 |
return ConvertToLong();
|
sl@0
|
1030 |
}
|
sl@0
|
1031 |
|
sl@0
|
1032 |
TInt TInteger::ConvertToLong(void) const
|
sl@0
|
1033 |
{
|
sl@0
|
1034 |
TUint value = ConvertToUnsignedLong();
|
sl@0
|
1035 |
return Sign() == EPositive ? value : -(static_cast<TInt>(value));
|
sl@0
|
1036 |
}
|
sl@0
|
1037 |
|
sl@0
|
1038 |
TBool TInteger::IsConvertableToLong(void) const
|
sl@0
|
1039 |
{
|
sl@0
|
1040 |
if(WordCount() > 1)
|
sl@0
|
1041 |
{
|
sl@0
|
1042 |
return EFalse;
|
sl@0
|
1043 |
}
|
sl@0
|
1044 |
TUint value = (Ptr())[0];
|
sl@0
|
1045 |
if(Sign() == EPositive)
|
sl@0
|
1046 |
{
|
sl@0
|
1047 |
return static_cast<TInt>(value) >= 0;
|
sl@0
|
1048 |
}
|
sl@0
|
1049 |
else
|
sl@0
|
1050 |
{
|
sl@0
|
1051 |
return -(static_cast<TInt>(value)) < 0;
|
sl@0
|
1052 |
}
|
sl@0
|
1053 |
}
|
sl@0
|
1054 |
|
sl@0
|
1055 |
EXPORT_C RInteger TInteger::SquaredL() const
|
sl@0
|
1056 |
{
|
sl@0
|
1057 |
//PositiveMultiplyL optimises for the squaring case already
|
sl@0
|
1058 |
//Any number squared is positive, no need for negative handling in TimesL
|
sl@0
|
1059 |
return PositiveMultiplyL(*this, *this);
|
sl@0
|
1060 |
}
|
sl@0
|
1061 |
|
sl@0
|
1062 |
EXPORT_C RInteger TInteger::DividedByL(TUint aOperand) const
|
sl@0
|
1063 |
{
|
sl@0
|
1064 |
TUint remainder;
|
sl@0
|
1065 |
RInteger quotient;
|
sl@0
|
1066 |
DivideL(remainder, quotient, *this, aOperand);
|
sl@0
|
1067 |
return quotient;
|
sl@0
|
1068 |
}
|
sl@0
|
1069 |
|
sl@0
|
1070 |
EXPORT_C RInteger TInteger::ExponentiateL(const TInteger& aExponent) const
|
sl@0
|
1071 |
{
|
sl@0
|
1072 |
//See HAC 14.85
|
sl@0
|
1073 |
|
sl@0
|
1074 |
// 1.1 Precomputation
|
sl@0
|
1075 |
// g1 <- g
|
sl@0
|
1076 |
// g2 <- g^2
|
sl@0
|
1077 |
RInteger g2 = SquaredL();
|
sl@0
|
1078 |
CleanupStack::PushL(g2);
|
sl@0
|
1079 |
RInteger g1 = RInteger::NewL(*this);
|
sl@0
|
1080 |
CleanupStack::PushL(g1);
|
sl@0
|
1081 |
TWindowSlider slider(aExponent);
|
sl@0
|
1082 |
|
sl@0
|
1083 |
// 1.2
|
sl@0
|
1084 |
// For i from 1 to (2^(k-1) -1) do g2i+1 <- g2i-1 * g2
|
sl@0
|
1085 |
TUint count = (1 << (slider.WindowSize()-1)) - 1; //2^(k-1) -1
|
sl@0
|
1086 |
RRArray<RInteger> powerArray(count+1); //+1 because we append g1
|
sl@0
|
1087 |
powerArray.AppendL(g1);
|
sl@0
|
1088 |
CleanupStack::Pop(); //g1
|
sl@0
|
1089 |
CleanupClosePushL(powerArray);
|
sl@0
|
1090 |
for(TUint k=1; k <= count; k++)
|
sl@0
|
1091 |
{
|
sl@0
|
1092 |
RInteger g2iplus1 = g2.TimesL(powerArray[k-1]);
|
sl@0
|
1093 |
powerArray.AppendL(g2iplus1);
|
sl@0
|
1094 |
}
|
sl@0
|
1095 |
|
sl@0
|
1096 |
// 2 A <- 1, i <- t
|
sl@0
|
1097 |
RInteger A = RInteger::NewL(One());
|
sl@0
|
1098 |
CleanupStack::PushL(A);
|
sl@0
|
1099 |
TInt i = aExponent.BitCount() - 1;
|
sl@0
|
1100 |
|
sl@0
|
1101 |
// 3 While i>=0 do:
|
sl@0
|
1102 |
while( i>=0 )
|
sl@0
|
1103 |
{
|
sl@0
|
1104 |
// 3.1 If ei == 0 then A <- A^2
|
sl@0
|
1105 |
if(!aExponent.Bit(i))
|
sl@0
|
1106 |
{
|
sl@0
|
1107 |
A *= A;
|
sl@0
|
1108 |
i--;
|
sl@0
|
1109 |
}
|
sl@0
|
1110 |
// 3.2 Find longest bitstring ei,ei-1,...,el s.t. i-l+1<=k and el==1
|
sl@0
|
1111 |
// and do:
|
sl@0
|
1112 |
// A <- (A^2^(i-l+1)) * g[the index indicated by the bitstring value]
|
sl@0
|
1113 |
else
|
sl@0
|
1114 |
{
|
sl@0
|
1115 |
slider.FindNextWindow(i);
|
sl@0
|
1116 |
assert(slider.Length() >= 1);
|
sl@0
|
1117 |
for(TUint j=0; j<slider.Length(); j++)
|
sl@0
|
1118 |
{
|
sl@0
|
1119 |
A *= A;
|
sl@0
|
1120 |
}
|
sl@0
|
1121 |
A *= powerArray[slider.Value()>>1];
|
sl@0
|
1122 |
i -= slider.Length();
|
sl@0
|
1123 |
}
|
sl@0
|
1124 |
}
|
sl@0
|
1125 |
CleanupStack::Pop(&A);
|
sl@0
|
1126 |
CleanupStack::PopAndDestroy(2, &g2); //powerArray, g2
|
sl@0
|
1127 |
return A;
|
sl@0
|
1128 |
}
|
sl@0
|
1129 |
|
sl@0
|
1130 |
void TInteger::DivideL(TUint& aRemainder, RInteger& aQuotient,
|
sl@0
|
1131 |
const TInteger& aDividend, TUint aDivisor) const
|
sl@0
|
1132 |
{
|
sl@0
|
1133 |
if(!aDivisor)
|
sl@0
|
1134 |
{
|
sl@0
|
1135 |
User::Leave(KErrDivideByZero);
|
sl@0
|
1136 |
}
|
sl@0
|
1137 |
|
sl@0
|
1138 |
TUint i = aDividend.WordCount();
|
sl@0
|
1139 |
aQuotient.CleanNewL(RoundupSize(i));
|
sl@0
|
1140 |
PositiveDivide(aRemainder, aQuotient, aDividend, aDivisor);
|
sl@0
|
1141 |
|
sl@0
|
1142 |
if(aDividend.NotNegative())
|
sl@0
|
1143 |
{
|
sl@0
|
1144 |
aQuotient.SetSign(TInteger::EPositive);
|
sl@0
|
1145 |
}
|
sl@0
|
1146 |
else
|
sl@0
|
1147 |
{
|
sl@0
|
1148 |
aQuotient.SetSign(TInteger::ENegative);
|
sl@0
|
1149 |
if(aRemainder)
|
sl@0
|
1150 |
{
|
sl@0
|
1151 |
--aQuotient;
|
sl@0
|
1152 |
aRemainder = aDivisor = aRemainder;
|
sl@0
|
1153 |
}
|
sl@0
|
1154 |
}
|
sl@0
|
1155 |
}
|
sl@0
|
1156 |
|
sl@0
|
1157 |
void TInteger::PositiveDivide(TUint& aRemainder, TInteger& aQuotient,
|
sl@0
|
1158 |
const TInteger& aDividend, TUint aDivisor) const
|
sl@0
|
1159 |
{
|
sl@0
|
1160 |
assert(aDivisor);
|
sl@0
|
1161 |
|
sl@0
|
1162 |
TUint i = aDividend.WordCount();
|
sl@0
|
1163 |
assert(aQuotient.Size() >= RoundupSize(i));
|
sl@0
|
1164 |
assert(aQuotient.Sign() == TInteger::EPositive);
|
sl@0
|
1165 |
aRemainder = 0;
|
sl@0
|
1166 |
while(i--)
|
sl@0
|
1167 |
{
|
sl@0
|
1168 |
aQuotient.Ptr()[i] =
|
sl@0
|
1169 |
TUint(MAKE_DWORD(aDividend.Ptr()[i], aRemainder) / aDivisor);
|
sl@0
|
1170 |
aRemainder =
|
sl@0
|
1171 |
TUint(MAKE_DWORD(aDividend.Ptr()[i], aRemainder) % aDivisor);
|
sl@0
|
1172 |
}
|
sl@0
|
1173 |
}
|