Update contrib.
2 * Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
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.
33 const TUint KSignMask = 0x1L;
34 const TUint KHeapBasedMask = 0x2L;
35 const TUint KPtrMask = 0xFFFFFFFCL;
40 * Abstract base class defining the interface for handling and manipulating big
43 * TInteger is capable of representing both negative and positive integers
44 * with an absolute value of less than 2^32^(2^32). To create an integer
46 * TInteger defines an interface for the RInteger implementation - it is not
47 * intended that TIntegers be copied or assigned from other TIntegers. On EKA1
48 * platforms, this is possible, but it should be avoided.
56 /** @internalComponent */
57 enum TRandomAttribute {EAllBitsRandom=0, ETopBitSet=1, ETop2BitsSet=2};
59 IMPORT_C HBufC8* BufferLC() const;
60 IMPORT_C TUint WordCount(void) const;
61 IMPORT_C TUint ByteCount(void) const;
62 IMPORT_C TUint BitCount(void) const;
64 IMPORT_C static const TInteger& Zero(void);
65 IMPORT_C static const TInteger& One(void);
66 IMPORT_C static const TInteger& Two(void);
68 IMPORT_C RInteger PlusL(const TInteger& aOperand) const;
69 IMPORT_C RInteger MinusL(const TInteger& aOperand) const;
70 IMPORT_C RInteger TimesL(const TInteger& aOperand) const;
71 IMPORT_C RInteger DividedByL(const TInteger& aOperand) const;
72 IMPORT_C RInteger DividedByL(TUint aOperand) const;
73 IMPORT_C RInteger ModuloL(const TInteger& aOperand) const;
74 IMPORT_C TUint ModuloL(TUint aOperand) const;
76 IMPORT_C RInteger SquaredL(void) const;
77 IMPORT_C RInteger ExponentiateL(const TInteger& aExponent) const;
78 IMPORT_C static RInteger ModularMultiplyL(const TInteger& aA, const TInteger& aB,
79 const TInteger& aModulus);
80 IMPORT_C static RInteger ModularExponentiateL(const TInteger& aBase,
81 const TInteger& aExp, const TInteger& aMod);
82 IMPORT_C RInteger GCDL(const TInteger& aOperand) const;
83 IMPORT_C RInteger InverseModL(const TInteger& aMod) const;
85 // These overloaded operator functions leave
86 IMPORT_C TInteger& operator += (const TInteger& aOperand);
87 IMPORT_C TInteger& operator -= (const TInteger& aOperand);
88 IMPORT_C TInteger& operator *= (const TInteger& aOperand);
89 IMPORT_C TInteger& operator /= (const TInteger& aOperand);
90 IMPORT_C TInteger& operator %= (const TInteger& aOperand);
92 IMPORT_C TInteger& operator += (TInt aOperand);
93 IMPORT_C TInteger& operator -= (TInt aOperand);
94 IMPORT_C TInteger& operator *= (TInt aOperand);
95 IMPORT_C TInteger& operator /= (TInt aOperand);
96 IMPORT_C TInteger& operator %= (TInt aOperand);
97 IMPORT_C TInteger& operator -- ();
98 IMPORT_C TInteger& operator ++ ();
100 IMPORT_C TInteger& operator <<= (TUint aBits);
101 // End of leaving overloaded operator functions
102 IMPORT_C TInteger& operator >>= (TUint aBits);
104 IMPORT_C TInt UnsignedCompare(const TInteger& aThat) const;
105 IMPORT_C TInt SignedCompare(const TInteger& aThat) const;
106 IMPORT_C TBool operator ! () const;
107 inline TBool operator == (const TInteger& aInteger) const;
108 inline TBool operator != (const TInteger& aInteger) const;
109 inline TBool operator <= (const TInteger& aInteger) const;
110 inline TBool operator >= (const TInteger& aInteger) const;
111 inline TBool operator < (const TInteger& aInteger) const;
112 inline TBool operator > (const TInteger& aInteger) const;
114 IMPORT_C TInt SignedCompare(TInt aThat) const;
115 inline TBool operator == (TInt aInteger) const;
116 inline TBool operator != (TInt aInteger) const;
117 inline TBool operator <= (TInt aInteger) const;
118 inline TBool operator >= (TInt aInteger) const;
119 inline TBool operator < (TInt aInteger) const;
120 inline TBool operator > (TInt aInteger) const;
122 inline TBool IsZero() const {return !*this;}
123 inline TBool NotZero() const {return !IsZero();}
124 inline TBool IsNegative() const {return Sign() == ENegative;}
125 inline TBool NotNegative() const {return !IsNegative();}
126 inline TBool IsPositive() const {return NotNegative() && NotZero();}
127 inline TBool NotPositive() const {return !IsPositive();}
128 inline TBool IsEven() const {return Bit(0) == EFalse;}
129 inline TBool IsOdd() const {return Bit(0);}
131 IMPORT_C TBool IsPrimeL(void) const;
133 IMPORT_C TBool Bit(TUint aBitPos) const;
134 IMPORT_C void SetBit(TUint aBitPos);
135 IMPORT_C void Negate(void);
137 IMPORT_C TInt ConvertToLongL(void) const;
139 IMPORT_C void CopyL(const TInteger& aInteger, TBool aAllowShrink=ETrue);
140 IMPORT_C void CopyL(const TInt aInteger, TBool aAllowShrink=ETrue);
141 IMPORT_C void Set(const RInteger& aInteger);
142 IMPORT_C HBufC8* BufferWithNoTruncationLC() const;
144 protected: //Interface functions to algorthims
145 /** @internalComponent */
146 RInteger PositiveAddL(const TInteger& aA, const TInteger& aB) const;
147 /** @internalComponent */
148 RInteger PositiveSubtractL(const TInteger& aA, const TInteger& aB) const;
149 /** @internalComponent */
150 RInteger PositiveMultiplyL(const TInteger& aA, const TInteger& aB) const;
151 /** @internalComponent */
152 void PositiveDivideL(RInteger& aRemainder, RInteger& aQuotient,
153 const TInteger& aDividend, const TInteger& aDivisor) const;
154 /** @internalComponent */
155 void DivideL(RInteger& aRemainder, RInteger& aQuotient,
156 const TInteger& aDividend, const TInteger& aDivisor) const;
157 /** @internalComponent */
158 void PositiveDivide(TUint& aRemainder, TInteger& aQoutient,
159 const TInteger& aDividend, TUint aDivisor) const;
160 /** @internalComponent */
161 void DivideL(TUint& aRemainder, RInteger& aQoutient,
162 const TInteger& aDividend, TUint aDivisor) const;
163 /** @internalComponent */
164 TUint Modulo(const TInteger& aDividend, TUint aDivisor) const;
166 protected: //Utility functions
167 /** @internalComponent */
168 TInt ConvertToLong(void) const;
169 inline TUint ConvertToUnsignedLong(void) const {return Ptr()[0];}
170 /** @internalComponent */
171 TBool IsConvertableToLong(void) const;
172 /** @internalComponent */
173 void RandomizeL(TUint aBits, TRandomAttribute aAttr);
174 /** @internalComponent */
175 void RandomizeL(const TInteger& aMin, const TInteger& aMax);
176 /** @internalComponent */
177 void PrimeRandomizeL(TUint aBits, TRandomAttribute aAttr);
178 /** @internalComponent */
179 TBool SmallPrimeRandomizeL(void);
181 protected: //Memory Handling
182 /** @internalComponent */
183 void CreateNewL(TUint aNewSize);
184 /** @internalComponent */
185 void CleanNewL(TUint aNewSize);
186 /** @internalComponent */
187 void CleanGrowL(TUint aNewSize);
188 /** @internalComponent */
189 void CleanResizeL(TUint aNewSize);
191 protected: //Construction functions
192 IMPORT_C TInteger(void);
193 /** @internalComponent */
194 void Construct(const TDesC8& aValue);
195 /** @internalComponent */
196 void Construct(const TInteger& aInteger);
197 /** @internalComponent */
198 void Construct(TInt aInteger);
199 /** @internalComponent */
200 void Construct(TUint aInteger);
201 protected: //Construction functions for stack based integers
202 /** @internalComponent */
203 void ConstructStack(TUint aWords, TUint aInteger);
204 /** @internalComponent */
205 void ConstructStack(TUint aWords, const TInteger& aInteger);
208 protected: //Member data
209 enum TSign {EPositive=0, ENegative=1};
213 protected: //Hackish functions
215 * Word 0: Size -- Must be power of 2 (algorithms assume this)
216 * Word 1: Pointer to storage location plus 2 flag bits in lsb
217 * Word 1 (bit 0): Sign of the integer (+ve == 0, -ve == 1)
218 * Word 1 (bit 1): Heap based flag bit (stack== 0, heap==1)
220 inline TSign Sign(void) const {return (TSign)(iPtr&KSignMask);}
221 inline TUint Size(void) const {return iSize;}
222 inline void SetSize(TUint aSize) {iSize = aSize;}
223 inline TUint* const Ptr(void) const {return (TUint*)(iPtr&KPtrMask);}
224 inline TBool IsStackBased(void) const {return !IsHeapBased();}
225 inline TBool IsHeapBased(void) const {return iPtr&KHeapBasedMask;}
226 inline void SetPtr(TUint* aPtr) {iPtr &= ~KPtrMask; iPtr |= (TUint)aPtr;}
227 inline void SetHeapBased(void) {iPtr |= KHeapBasedMask;}
228 inline void SetStackBased(void) {iPtr &= ~KHeapBasedMask;}
229 inline void SetSign(TSign aSign) {iPtr &= ~KSignMask; iPtr |= (TUint)aSign;}
232 // disable default copy constructor and assignment operator
233 TInteger(const TInteger& aInteger);
234 TInteger& operator=(const TInteger& aInteger);
236 friend class CMontgomeryStructure;
237 friend class RInteger; //in order to have access to Size() for an argument
240 // Inline methods for TInteger
242 inline TBool TInteger::operator == (const TInteger& aInteger) const
244 return SignedCompare(aInteger) == 0;
247 inline TBool TInteger::operator != (const TInteger& aInteger) const
249 return SignedCompare(aInteger) != 0;
252 inline TBool TInteger::operator <= (const TInteger& aInteger) const
254 return SignedCompare(aInteger) <= 0;
257 inline TBool TInteger::operator >= (const TInteger& aInteger) const
259 return SignedCompare(aInteger) >= 0;
262 inline TBool TInteger::operator < (const TInteger& aInteger) const
264 return SignedCompare(aInteger) < 0;
267 TBool TInteger::operator > (const TInteger& aInteger) const
269 return SignedCompare(aInteger) > 0;
272 inline TBool TInteger::operator == (TInt aInteger) const
274 return SignedCompare(aInteger) == 0;
277 inline TBool TInteger::operator != (TInt aInteger) const
279 return SignedCompare(aInteger) != 0;
282 inline TBool TInteger::operator <= (TInt aInteger) const
284 return SignedCompare(aInteger) <= 0;
287 inline TBool TInteger::operator >= (TInt aInteger) const
289 return SignedCompare(aInteger) >= 0;
292 inline TBool TInteger::operator < (TInt aInteger) const
294 return SignedCompare(aInteger) < 0;
297 inline TBool TInteger::operator > (TInt aInteger) const
299 return SignedCompare(aInteger) > 0;
304 * A TInteger derived class allowing the construction of variable length big integers.
305 * See the Cryptography API guide for further information.
310 class RInteger : public TInteger
313 IMPORT_C static RInteger NewL(void);
314 IMPORT_C static RInteger NewL(const TDesC8& aValue);
315 IMPORT_C static RInteger NewL(const TInteger& aInteger);
316 IMPORT_C static RInteger NewL(TInt aInteger);
317 IMPORT_C static RInteger NewL(TUint aInteger);
318 IMPORT_C static RInteger NewEmptyL(TUint aNumWords);
320 IMPORT_C static RInteger NewRandomL(TUint aBits,
321 TRandomAttribute aAttr=EAllBitsRandom);
322 IMPORT_C static RInteger NewRandomL(const TInteger& aMin,
323 const TInteger& aMax);
324 IMPORT_C static RInteger NewPrimeL(TUint aBits,
325 TRandomAttribute aAttr=EAllBitsRandom);
327 IMPORT_C RInteger(void);
328 IMPORT_C RInteger(const RInteger& aInteger);
329 IMPORT_C RInteger& operator=(const RInteger& aInteger);
331 IMPORT_C operator TCleanupItem();
332 IMPORT_C static void CallClose(TAny* aPtr);
333 IMPORT_C void Close(void);
336 #endif // __BIGINT_H__