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