|
1 /* |
|
2 * Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * ** IMPORTANT ** PublishedPartner API's in this file are published to 3rd party developers via the |
|
16 * Symbian website. Changes to these API's should be treated as PublishedAll API changes and the Security TA should be consulted. |
|
17 * |
|
18 */ |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 /** |
|
24 @file |
|
25 @publishedPartner |
|
26 @released |
|
27 */ |
|
28 |
|
29 #ifndef __BIGINT_H__ |
|
30 #define __BIGINT_H__ |
|
31 |
|
32 #include <random.h> |
|
33 #include <e32base.h> |
|
34 |
|
35 /** @internalComponent */ |
|
36 const TUint KSignMask = 0x1L; |
|
37 /** @internalComponent */ |
|
38 const TUint KHeapBasedMask = 0x2L; |
|
39 /** @internalComponent */ |
|
40 const TUint KPtrMask = 0xFFFFFFFCL; |
|
41 |
|
42 class RInteger; |
|
43 |
|
44 /** |
|
45 * Abstract base class defining the interface for handling and manipulating big |
|
46 * integers. |
|
47 * |
|
48 * TInteger is capable of representing both negative and positive integers |
|
49 * with an absolute value of less than 2^32^(2^32). To create an integer |
|
50 * look at RInteger. |
|
51 * TInteger defines an interface for the RInteger implementation - it is not |
|
52 * intended that TIntegers be copied or assigned from other TIntegers. On EKA1 |
|
53 * platforms, this is possible, but it should be avoided. |
|
54 * |
|
55 * |
|
56 * @see RInteger |
|
57 */ |
|
58 class TInteger |
|
59 { |
|
60 public: |
|
61 /** @internalComponent */ |
|
62 enum TRandomAttribute {EAllBitsRandom=0, ETopBitSet=1, ETop2BitsSet=2}; |
|
63 |
|
64 IMPORT_C HBufC8* BufferLC() const; |
|
65 IMPORT_C TUint WordCount(void) const; |
|
66 IMPORT_C TUint ByteCount(void) const; |
|
67 IMPORT_C TUint BitCount(void) const; |
|
68 |
|
69 IMPORT_C static const TInteger& Zero(void); |
|
70 IMPORT_C static const TInteger& One(void); |
|
71 IMPORT_C static const TInteger& Two(void); |
|
72 |
|
73 IMPORT_C RInteger PlusL(const TInteger& aOperand) const; |
|
74 IMPORT_C RInteger MinusL(const TInteger& aOperand) const; |
|
75 IMPORT_C RInteger TimesL(const TInteger& aOperand) const; |
|
76 IMPORT_C RInteger DividedByL(const TInteger& aOperand) const; |
|
77 IMPORT_C RInteger DividedByL(TUint aOperand) const; |
|
78 IMPORT_C RInteger ModuloL(const TInteger& aOperand) const; |
|
79 IMPORT_C TUint ModuloL(TUint aOperand) const; |
|
80 |
|
81 IMPORT_C RInteger SquaredL(void) const; |
|
82 IMPORT_C RInteger ExponentiateL(const TInteger& aExponent) const; |
|
83 IMPORT_C static RInteger ModularMultiplyL(const TInteger& aA, const TInteger& aB, |
|
84 const TInteger& aModulus); |
|
85 IMPORT_C static RInteger ModularExponentiateL(const TInteger& aBase, |
|
86 const TInteger& aExp, const TInteger& aMod); |
|
87 IMPORT_C RInteger GCDL(const TInteger& aOperand) const; |
|
88 IMPORT_C RInteger InverseModL(const TInteger& aMod) const; |
|
89 |
|
90 // These overloaded operator functions leave |
|
91 IMPORT_C TInteger& operator += (const TInteger& aOperand); |
|
92 IMPORT_C TInteger& operator -= (const TInteger& aOperand); |
|
93 IMPORT_C TInteger& operator *= (const TInteger& aOperand); |
|
94 IMPORT_C TInteger& operator /= (const TInteger& aOperand); |
|
95 IMPORT_C TInteger& operator %= (const TInteger& aOperand); |
|
96 |
|
97 IMPORT_C TInteger& operator += (TInt aOperand); |
|
98 IMPORT_C TInteger& operator -= (TInt aOperand); |
|
99 IMPORT_C TInteger& operator *= (TInt aOperand); |
|
100 IMPORT_C TInteger& operator /= (TInt aOperand); |
|
101 IMPORT_C TInteger& operator %= (TInt aOperand); |
|
102 IMPORT_C TInteger& operator -- (); |
|
103 IMPORT_C TInteger& operator ++ (); |
|
104 |
|
105 IMPORT_C TInteger& operator <<= (TUint aBits); |
|
106 // End of leaving overloaded operator functions |
|
107 IMPORT_C TInteger& operator >>= (TUint aBits); |
|
108 |
|
109 IMPORT_C TInt UnsignedCompare(const TInteger& aThat) const; |
|
110 IMPORT_C TInt SignedCompare(const TInteger& aThat) const; |
|
111 IMPORT_C TBool operator ! () const; |
|
112 inline TBool operator == (const TInteger& aInteger) const; |
|
113 inline TBool operator != (const TInteger& aInteger) const; |
|
114 inline TBool operator <= (const TInteger& aInteger) const; |
|
115 inline TBool operator >= (const TInteger& aInteger) const; |
|
116 inline TBool operator < (const TInteger& aInteger) const; |
|
117 inline TBool operator > (const TInteger& aInteger) const; |
|
118 |
|
119 IMPORT_C TInt SignedCompare(TInt aThat) const; |
|
120 inline TBool operator == (TInt aInteger) const; |
|
121 inline TBool operator != (TInt aInteger) const; |
|
122 inline TBool operator <= (TInt aInteger) const; |
|
123 inline TBool operator >= (TInt aInteger) const; |
|
124 inline TBool operator < (TInt aInteger) const; |
|
125 inline TBool operator > (TInt aInteger) const; |
|
126 |
|
127 inline TBool IsZero() const {return !*this;} |
|
128 inline TBool NotZero() const {return !IsZero();} |
|
129 inline TBool IsNegative() const {return Sign() == ENegative;} |
|
130 inline TBool NotNegative() const {return !IsNegative();} |
|
131 inline TBool IsPositive() const {return NotNegative() && NotZero();} |
|
132 inline TBool NotPositive() const {return !IsPositive();} |
|
133 inline TBool IsEven() const {return Bit(0) == EFalse;} |
|
134 inline TBool IsOdd() const {return Bit(0);} |
|
135 |
|
136 IMPORT_C TBool IsPrimeL(void) const; |
|
137 |
|
138 IMPORT_C TBool Bit(TUint aBitPos) const; |
|
139 IMPORT_C void SetBit(TUint aBitPos); |
|
140 IMPORT_C void Negate(void); |
|
141 |
|
142 IMPORT_C TInt ConvertToLongL(void) const; |
|
143 |
|
144 IMPORT_C void CopyL(const TInteger& aInteger, TBool aAllowShrink=ETrue); |
|
145 IMPORT_C void CopyL(const TInt aInteger, TBool aAllowShrink=ETrue); |
|
146 IMPORT_C void Set(const RInteger& aInteger); |
|
147 IMPORT_C HBufC8* BufferWithNoTruncationLC() const; |
|
148 |
|
149 protected: //Interface functions to algorthims |
|
150 /** @internalComponent */ |
|
151 RInteger PositiveAddL(const TInteger& aA, const TInteger& aB) const; |
|
152 /** @internalComponent */ |
|
153 RInteger PositiveSubtractL(const TInteger& aA, const TInteger& aB) const; |
|
154 /** @internalComponent */ |
|
155 RInteger PositiveMultiplyL(const TInteger& aA, const TInteger& aB) const; |
|
156 /** @internalComponent */ |
|
157 void PositiveDivideL(RInteger& aRemainder, RInteger& aQuotient, |
|
158 const TInteger& aDividend, const TInteger& aDivisor) const; |
|
159 /** @internalComponent */ |
|
160 void DivideL(RInteger& aRemainder, RInteger& aQuotient, |
|
161 const TInteger& aDividend, const TInteger& aDivisor) const; |
|
162 /** @internalComponent */ |
|
163 void PositiveDivide(TUint& aRemainder, TInteger& aQoutient, |
|
164 const TInteger& aDividend, TUint aDivisor) const; |
|
165 /** @internalComponent */ |
|
166 void DivideL(TUint& aRemainder, RInteger& aQoutient, |
|
167 const TInteger& aDividend, TUint aDivisor) const; |
|
168 /** @internalComponent */ |
|
169 TUint Modulo(const TInteger& aDividend, TUint aDivisor) const; |
|
170 |
|
171 protected: //Utility functions |
|
172 /** @internalComponent */ |
|
173 TInt ConvertToLong(void) const; |
|
174 inline TUint ConvertToUnsignedLong(void) const {return Ptr()[0];} |
|
175 /** @internalComponent */ |
|
176 TBool IsConvertableToLong(void) const; |
|
177 /** @internalComponent */ |
|
178 void RandomizeL(TUint aBits, TRandomAttribute aAttr); |
|
179 /** @internalComponent */ |
|
180 void RandomizeL(const TInteger& aMin, const TInteger& aMax); |
|
181 /** @internalComponent */ |
|
182 void PrimeRandomizeL(TUint aBits, TRandomAttribute aAttr); |
|
183 /** @internalComponent */ |
|
184 TBool SmallPrimeRandomizeL(void); |
|
185 |
|
186 protected: //Memory Handling |
|
187 /** @internalComponent */ |
|
188 void CreateNewL(TUint aNewSize); |
|
189 /** @internalComponent */ |
|
190 void CleanNewL(TUint aNewSize); |
|
191 /** @internalComponent */ |
|
192 void CleanGrowL(TUint aNewSize); |
|
193 /** @internalComponent */ |
|
194 void CleanResizeL(TUint aNewSize); |
|
195 |
|
196 protected: //Construction functions |
|
197 IMPORT_C TInteger(void); |
|
198 /** @internalComponent */ |
|
199 void Construct(const TDesC8& aValue); |
|
200 /** @internalComponent */ |
|
201 void Construct(const TInteger& aInteger); |
|
202 /** @internalComponent */ |
|
203 void Construct(TInt aInteger); |
|
204 /** @internalComponent */ |
|
205 void Construct(TUint aInteger); |
|
206 protected: //Construction functions for stack based integers |
|
207 /** @internalComponent */ |
|
208 void ConstructStack(TUint aWords, TUint aInteger); |
|
209 /** @internalComponent */ |
|
210 void ConstructStack(TUint aWords, const TInteger& aInteger); |
|
211 |
|
212 |
|
213 protected: //Member data |
|
214 enum TSign {EPositive=0, ENegative=1}; |
|
215 TUint iSize; |
|
216 TUint iPtr; |
|
217 |
|
218 protected: //Hackish functions |
|
219 /* Memory Layout |
|
220 * Word 0: Size -- Must be power of 2 (algorithms assume this) |
|
221 * Word 1: Pointer to storage location plus 2 flag bits in lsb |
|
222 * Word 1 (bit 0): Sign of the integer (+ve == 0, -ve == 1) |
|
223 * Word 1 (bit 1): Heap based flag bit (stack== 0, heap==1) |
|
224 */ |
|
225 inline TSign Sign(void) const {return (TSign)(iPtr&KSignMask);} |
|
226 inline TUint Size(void) const {return iSize;} |
|
227 inline void SetSize(TUint aSize) {iSize = aSize;} |
|
228 inline TUint* const Ptr(void) const {return (TUint*)(iPtr&KPtrMask);} |
|
229 inline TBool IsStackBased(void) const {return !IsHeapBased();} |
|
230 inline TBool IsHeapBased(void) const {return iPtr&KHeapBasedMask;} |
|
231 inline void SetPtr(TUint* aPtr) {iPtr &= ~KPtrMask; iPtr |= (TUint)aPtr;} |
|
232 inline void SetHeapBased(void) {iPtr |= KHeapBasedMask;} |
|
233 inline void SetStackBased(void) {iPtr &= ~KHeapBasedMask;} |
|
234 inline void SetSign(TSign aSign) {iPtr &= ~KSignMask; iPtr |= (TUint)aSign;} |
|
235 |
|
236 private: |
|
237 // disable default copy constructor and assignment operator |
|
238 TInteger(const TInteger& aInteger); |
|
239 TInteger& operator=(const TInteger& aInteger); |
|
240 |
|
241 friend class CMontgomeryStructure; |
|
242 friend class RInteger; //in order to have access to Size() for an argument |
|
243 }; |
|
244 |
|
245 // Inline methods for TInteger |
|
246 |
|
247 inline TBool TInteger::operator == (const TInteger& aInteger) const |
|
248 { |
|
249 return SignedCompare(aInteger) == 0; |
|
250 } |
|
251 |
|
252 inline TBool TInteger::operator != (const TInteger& aInteger) const |
|
253 { |
|
254 return SignedCompare(aInteger) != 0; |
|
255 } |
|
256 |
|
257 inline TBool TInteger::operator <= (const TInteger& aInteger) const |
|
258 { |
|
259 return SignedCompare(aInteger) <= 0; |
|
260 } |
|
261 |
|
262 inline TBool TInteger::operator >= (const TInteger& aInteger) const |
|
263 { |
|
264 return SignedCompare(aInteger) >= 0; |
|
265 } |
|
266 |
|
267 inline TBool TInteger::operator < (const TInteger& aInteger) const |
|
268 { |
|
269 return SignedCompare(aInteger) < 0; |
|
270 } |
|
271 |
|
272 TBool TInteger::operator > (const TInteger& aInteger) const |
|
273 { |
|
274 return SignedCompare(aInteger) > 0; |
|
275 } |
|
276 |
|
277 inline TBool TInteger::operator == (TInt aInteger) const |
|
278 { |
|
279 return SignedCompare(aInteger) == 0; |
|
280 } |
|
281 |
|
282 inline TBool TInteger::operator != (TInt aInteger) const |
|
283 { |
|
284 return SignedCompare(aInteger) != 0; |
|
285 } |
|
286 |
|
287 inline TBool TInteger::operator <= (TInt aInteger) const |
|
288 { |
|
289 return SignedCompare(aInteger) <= 0; |
|
290 } |
|
291 |
|
292 inline TBool TInteger::operator >= (TInt aInteger) const |
|
293 { |
|
294 return SignedCompare(aInteger) >= 0; |
|
295 } |
|
296 |
|
297 inline TBool TInteger::operator < (TInt aInteger) const |
|
298 { |
|
299 return SignedCompare(aInteger) < 0; |
|
300 } |
|
301 |
|
302 inline TBool TInteger::operator > (TInt aInteger) const |
|
303 { |
|
304 return SignedCompare(aInteger) > 0; |
|
305 } |
|
306 |
|
307 |
|
308 /** |
|
309 * A TInteger derived class allowing the construction of variable length big integers. |
|
310 * See the Cryptography API guide for further information. |
|
311 * |
|
312 * @publishedPartner |
|
313 * @released |
|
314 * |
|
315 * @see TInteger |
|
316 */ |
|
317 class RInteger : public TInteger |
|
318 { |
|
319 public: |
|
320 IMPORT_C static RInteger NewL(void); |
|
321 IMPORT_C static RInteger NewL(const TDesC8& aValue); |
|
322 IMPORT_C static RInteger NewL(const TInteger& aInteger); |
|
323 IMPORT_C static RInteger NewL(TInt aInteger); |
|
324 IMPORT_C static RInteger NewL(TUint aInteger); |
|
325 IMPORT_C static RInteger NewEmptyL(TUint aNumWords); |
|
326 |
|
327 IMPORT_C static RInteger NewRandomL(TUint aBits, |
|
328 TRandomAttribute aAttr=EAllBitsRandom); |
|
329 IMPORT_C static RInteger NewRandomL(const TInteger& aMin, |
|
330 const TInteger& aMax); |
|
331 IMPORT_C static RInteger NewPrimeL(TUint aBits, |
|
332 TRandomAttribute aAttr=EAllBitsRandom); |
|
333 |
|
334 IMPORT_C RInteger(void); |
|
335 IMPORT_C RInteger(const RInteger& aInteger); |
|
336 IMPORT_C RInteger& operator=(const RInteger& aInteger); |
|
337 |
|
338 IMPORT_C operator TCleanupItem(); |
|
339 IMPORT_C static void CallClose(TAny* aPtr); |
|
340 IMPORT_C void Close(void); |
|
341 }; |
|
342 |
|
343 #endif // __BIGINT_H__ |