Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

integer.h

Go to the documentation of this file.
00001 #ifndef CRYPTOPP_INTEGER_H
00002 #define CRYPTOPP_INTEGER_H
00003 
00004 /** \file */
00005 
00006 #include "cryptlib.h"
00007 #include "secblock.h"
00008 
00009 #include <iosfwd>
00010 #include <algorithm>
00011 
00012 #ifdef CRYPTOPP_X86ASM_AVAILABLE
00013 
00014 #ifdef _M_IX86
00015         #if (defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 500)) || (defined(__ICL) && (__ICL >= 500))
00016                 #define SSE2_INTRINSICS_AVAILABLE
00017         #elif defined(_MSC_VER)
00018                 // _mm_free seems to be the only way to tell if the Processor Pack is installed or not
00019                 #include <malloc.h>
00020                 #if defined(_mm_free)
00021                         #define SSE2_INTRINSICS_AVAILABLE
00022                 #endif
00023         #endif
00024 #endif
00025 
00026 // SSE2 intrinsics work in GCC 3.3 or later
00027 #if defined(__SSE2__) && (__GNUC_MAJOR__ > 3 || __GNUC_MINOR__ > 2)
00028         #define SSE2_INTRINSICS_AVAILABLE
00029 #endif
00030 
00031 #endif
00032 
00033 NAMESPACE_BEGIN(CryptoPP)
00034 
00035 #if defined(SSE2_INTRINSICS_AVAILABLE) || defined(_MSC_VER)
00036         // Defined this class for MSVC even if processor pack is not installed,
00037         // so that the library can be compiled with processor pack, and calling app
00038         // compiled without it.
00039         template <class T>
00040         class AlignedAllocator : public AllocatorBase<T>
00041         {
00042         public:
00043                 CRYPTOPP_INHERIT_ALLOCATOR_TYPES
00044 
00045                 pointer allocate(size_type n, const void *);
00046                 void deallocate(void *p, size_type n);
00047                 pointer reallocate(T *p, size_type oldSize, size_type newSize, bool preserve)
00048                 {
00049                         return StandardReallocate(*this, p, oldSize, newSize, preserve);
00050                 }
00051         };
00052 
00053         template class CRYPTOPP_DLL AlignedAllocator<word>;
00054         typedef SecBlock<word, AlignedAllocator<word> > SecAlignedWordBlock;
00055 
00056 #else
00057         typedef SecWordBlock SecAlignedWordBlock;
00058 #endif
00059 
00060 void CRYPTOPP_DLL DisableSSE2();
00061 
00062 //! multiple precision integer and basic arithmetics
00063 /*! This class can represent positive and negative integers
00064         with absolute value less than (256**sizeof(word)) ** (256**sizeof(int)).
00065         \nosubgrouping
00066 */
00067 class CRYPTOPP_DLL Integer : public ASN1Object
00068 {
00069 public:
00070         //! \name ENUMS, EXCEPTIONS, and TYPEDEFS
00071         //@{
00072                 //! division by zero exception
00073                 class DivideByZero : public Exception
00074                 {
00075                 public:
00076                         DivideByZero() : Exception(OTHER_ERROR, "Integer: division by zero") {}
00077                 };
00078 
00079                 //!
00080                 class RandomNumberNotFound : public Exception
00081                 {
00082                 public:
00083                         RandomNumberNotFound() : Exception(OTHER_ERROR, "Integer: no integer satisfies the given parameters") {}
00084                 };
00085 
00086                 //!
00087                 enum Sign {POSITIVE=0, NEGATIVE=1};
00088 
00089                 //!
00090                 enum Signedness {
00091                 //!
00092                         UNSIGNED,
00093                 //!
00094                         SIGNED};
00095 
00096                 //!
00097                 enum RandomNumberType {
00098                 //!
00099                         ANY,
00100                 //!
00101                         PRIME};
00102         //@}
00103 
00104         //! \name CREATORS
00105         //@{
00106                 //! creates the zero integer
00107                 Integer();
00108 
00109                 //! copy constructor
00110                 Integer(const Integer& t);
00111 
00112                 //! convert from signed long
00113                 Integer(signed long value);
00114 
00115                 //! convert from lword
00116                 Integer(Sign s, lword value);
00117 
00118                 //! convert from two words
00119                 Integer(Sign s, word highWord, word lowWord);
00120 
00121                 //! convert from string
00122                 /*! str can be in base 2, 8, 10, or 16.  Base is determined by a
00123                         case insensitive suffix of 'h', 'o', or 'b'.  No suffix means base 10.
00124                 */
00125                 explicit Integer(const char *str);
00126                 explicit Integer(const wchar_t *str);
00127 
00128                 //! convert from big-endian byte array
00129                 Integer(const byte *encodedInteger, unsigned int byteCount, Signedness s=UNSIGNED);
00130 
00131                 //! convert from big-endian form stored in a BufferedTransformation
00132                 Integer(BufferedTransformation &bt, unsigned int byteCount, Signedness s=UNSIGNED);
00133 
00134                 //! convert from BER encoded byte array stored in a BufferedTransformation object
00135                 explicit Integer(BufferedTransformation &bt);
00136 
00137                 //! create a random integer
00138                 /*! The random integer created is uniformly distributed over [0, 2**bitcount). */
00139                 Integer(RandomNumberGenerator &rng, unsigned int bitcount);
00140 
00141                 //! avoid calling constructors for these frequently used integers
00142                 static const Integer &Zero();
00143                 //! avoid calling constructors for these frequently used integers
00144                 static const Integer &One();
00145                 //! avoid calling constructors for these frequently used integers
00146                 static const Integer &Two();
00147 
00148                 //! create a random integer of special type
00149                 /*! Ideally, the random integer created should be uniformly distributed
00150                         over {x | min <= x <= max and x is of rnType and x % mod == equiv}.
00151                         However the actual distribution may not be uniform because sequential
00152                         search is used to find an appropriate number from a random starting
00153                         point.
00154                         May return (with very small probability) a pseudoprime when a prime
00155                         is requested and max > lastSmallPrime*lastSmallPrime (lastSmallPrime
00156                         is declared in nbtheory.h).
00157                         \throw RandomNumberNotFound if the set is empty.
00158                 */
00159                 Integer(RandomNumberGenerator &rng, const Integer &min, const Integer &max, RandomNumberType rnType=ANY, const Integer &equiv=Zero(), const Integer &mod=One());
00160 
00161                 //! return the integer 2**e
00162                 static Integer Power2(unsigned int e);
00163         //@}
00164 
00165         //! \name ENCODE/DECODE
00166         //@{
00167                 //! minimum number of bytes to encode this integer
00168                 /*! MinEncodedSize of 0 is 1 */
00169                 unsigned int MinEncodedSize(Signedness=UNSIGNED) const;
00170                 //! encode in big-endian format
00171                 /*! unsigned means encode absolute value, signed means encode two's complement if negative.
00172                         if outputLen < MinEncodedSize, the most significant bytes will be dropped
00173                         if outputLen > MinEncodedSize, the most significant bytes will be padded
00174                 */
00175                 unsigned int Encode(byte *output, unsigned int outputLen, Signedness=UNSIGNED) const;
00176                 //!
00177                 unsigned int Encode(BufferedTransformation &bt, unsigned int outputLen, Signedness=UNSIGNED) const;
00178 
00179                 //! encode using Distinguished Encoding Rules, put result into a BufferedTransformation object
00180                 void DEREncode(BufferedTransformation &bt) const;
00181 
00182                 //! encode absolute value as big-endian octet string
00183                 void DEREncodeAsOctetString(BufferedTransformation &bt, unsigned int length) const;
00184 
00185                 //! encode absolute value in OpenPGP format, return length of output
00186                 unsigned int OpenPGPEncode(byte *output, unsigned int bufferSize) const;
00187                 //! encode absolute value in OpenPGP format, put result into a BufferedTransformation object
00188                 unsigned int OpenPGPEncode(BufferedTransformation &bt) const;
00189 
00190                 //!
00191                 void Decode(const byte *input, unsigned int inputLen, Signedness=UNSIGNED);
00192                 //! 
00193                 //* Precondition: bt.MaxRetrievable() >= inputLen
00194                 void Decode(BufferedTransformation &bt, unsigned int inputLen, Signedness=UNSIGNED);
00195 
00196                 //!
00197                 void BERDecode(const byte *input, unsigned int inputLen);
00198                 //!
00199                 void BERDecode(BufferedTransformation &bt);
00200 
00201                 //! decode nonnegative value as big-endian octet string
00202                 void BERDecodeAsOctetString(BufferedTransformation &bt, unsigned int length);
00203 
00204                 class OpenPGPDecodeErr : public Exception
00205                 {
00206                 public: 
00207                         OpenPGPDecodeErr() : Exception(INVALID_DATA_FORMAT, "OpenPGP decode error") {}
00208                 };
00209 
00210                 //!
00211                 void OpenPGPDecode(const byte *input, unsigned int inputLen);
00212                 //!
00213                 void OpenPGPDecode(BufferedTransformation &bt);
00214         //@}
00215 
00216         //! \name ACCESSORS
00217         //@{
00218                 //! return true if *this can be represented as a signed long
00219                 bool IsConvertableToLong() const;
00220                 //! return equivalent signed long if possible, otherwise undefined
00221                 signed long ConvertToLong() const;
00222 
00223                 //! number of significant bits = floor(log2(abs(*this))) + 1
00224                 unsigned int BitCount() const;
00225                 //! number of significant bytes = ceiling(BitCount()/8)
00226                 unsigned int ByteCount() const;
00227                 //! number of significant words = ceiling(ByteCount()/sizeof(word))
00228                 unsigned int WordCount() const;
00229 
00230                 //! return the i-th bit, i=0 being the least significant bit
00231                 bool GetBit(unsigned int i) const;
00232                 //! return the i-th byte
00233                 byte GetByte(unsigned int i) const;
00234                 //! return n lowest bits of *this >> i
00235                 unsigned long GetBits(unsigned int i, unsigned int n) const;
00236 
00237                 //!
00238                 bool IsZero() const {return !*this;}
00239                 //!
00240                 bool NotZero() const {return !IsZero();}
00241                 //!
00242                 bool IsNegative() const {return sign == NEGATIVE;}
00243                 //!
00244                 bool NotNegative() const {return !IsNegative();}
00245                 //!
00246                 bool IsPositive() const {return NotNegative() && NotZero();}
00247                 //!
00248                 bool NotPositive() const {return !IsPositive();}
00249                 //!
00250                 bool IsEven() const {return GetBit(0) == 0;}
00251                 //!
00252                 bool IsOdd() const      {return GetBit(0) == 1;}
00253         //@}
00254 
00255         //! \name MANIPULATORS
00256         //@{
00257                 //!
00258                 Integer&  operator=(const Integer& t);
00259 
00260                 //!
00261                 Integer&  operator+=(const Integer& t);
00262                 //!
00263                 Integer&  operator-=(const Integer& t);
00264                 //!
00265                 Integer&  operator*=(const Integer& t)  {return *this = Times(t);}
00266                 //!
00267                 Integer&  operator/=(const Integer& t)  {return *this = DividedBy(t);}
00268                 //!
00269                 Integer&  operator%=(const Integer& t)  {return *this = Modulo(t);}
00270                 //!
00271                 Integer&  operator/=(word t)  {return *this = DividedBy(t);}
00272                 //!
00273                 Integer&  operator%=(word t)  {return *this = Modulo(t);}
00274 
00275                 //!
00276                 Integer&  operator<<=(unsigned int);
00277                 //!
00278                 Integer&  operator>>=(unsigned int);
00279 
00280                 //!
00281                 void Randomize(RandomNumberGenerator &rng, unsigned int bitcount);
00282                 //!
00283                 void Randomize(RandomNumberGenerator &rng, const Integer &min, const Integer &max);
00284                 //! set this Integer to a random element of {x | min <= x <= max and x is of rnType and x % mod == equiv}
00285                 /*! returns false if the set is empty */
00286                 bool Randomize(RandomNumberGenerator &rng, const Integer &min, const Integer &max, RandomNumberType rnType, const Integer &equiv=Zero(), const Integer &mod=One());
00287 
00288                 bool GenerateRandomNoThrow(RandomNumberGenerator &rng, const NameValuePairs &params = g_nullNameValuePairs);
00289                 void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &params = g_nullNameValuePairs)
00290                 {
00291                         if (!GenerateRandomNoThrow(rng, params))
00292                                 throw RandomNumberNotFound();
00293                 }
00294 
00295                 //! set the n-th bit to value
00296                 void SetBit(unsigned int n, bool value=1);
00297                 //! set the n-th byte to value
00298                 void SetByte(unsigned int n, byte value);
00299 
00300                 //!
00301                 void Negate();
00302                 //!
00303                 void SetPositive() {sign = POSITIVE;}
00304                 //!
00305                 void SetNegative() {if (!!(*this)) sign = NEGATIVE;}
00306 
00307                 //!
00308                 void swap(Integer &a);
00309         //@}
00310 
00311         //! \name UNARY OPERATORS
00312         //@{
00313                 //!
00314                 bool            operator!() const;
00315                 //!
00316                 Integer         operator+() const {return *this;}
00317                 //!
00318                 Integer         operator-() const;
00319                 //!
00320                 Integer&        operator++();
00321                 //!
00322                 Integer&        operator--();
00323                 //!
00324                 Integer         operator++(int) {Integer temp = *this; ++*this; return temp;}
00325                 //!
00326                 Integer         operator--(int) {Integer temp = *this; --*this; return temp;}
00327         //@}
00328 
00329         //! \name BINARY OPERATORS
00330         //@{
00331                 //! signed comparison
00332                 /*! \retval -1 if *this < a
00333                         \retval  0 if *this = a
00334                         \retval  1 if *this > a
00335                 */
00336                 int Compare(const Integer& a) const;
00337 
00338                 //!
00339                 Integer Plus(const Integer &b) const;
00340                 //!
00341                 Integer Minus(const Integer &b) const;
00342                 //!
00343                 Integer Times(const Integer &b) const;
00344                 //!
00345                 Integer DividedBy(const Integer &b) const;
00346                 //!
00347                 Integer Modulo(const Integer &b) const;
00348                 //!
00349                 Integer DividedBy(word b) const;
00350                 //!
00351                 word Modulo(word b) const;
00352 
00353                 //!
00354                 Integer operator>>(unsigned int n) const        {return Integer(*this)>>=n;}
00355                 //!
00356                 Integer operator<<(unsigned int n) const        {return Integer(*this)<<=n;}
00357         //@}
00358 
00359         //! \name OTHER ARITHMETIC FUNCTIONS
00360         //@{
00361                 //!
00362                 Integer AbsoluteValue() const;
00363                 //!
00364                 Integer Doubled() const {return Plus(*this);}
00365                 //!
00366                 Integer Squared() const {return Times(*this);}
00367                 //! extract square root, if negative return 0, else return floor of square root
00368                 Integer SquareRoot() const;
00369                 //! return whether this integer is a perfect square
00370                 bool IsSquare() const;
00371 
00372                 //! is 1 or -1
00373                 bool IsUnit() const;
00374                 //! return inverse if 1 or -1, otherwise return 0
00375                 Integer MultiplicativeInverse() const;
00376 
00377                 //! modular multiplication
00378                 CRYPTOPP_DLL friend Integer a_times_b_mod_c(const Integer &x, const Integer& y, const Integer& m);
00379                 //! modular exponentiation
00380                 CRYPTOPP_DLL friend Integer a_exp_b_mod_c(const Integer &x, const Integer& e, const Integer& m);
00381 
00382                 //! calculate r and q such that (a == d*q + r) && (0 <= r < abs(d))
00383                 static void Divide(Integer &r, Integer &q, const Integer &a, const Integer &d);
00384                 //! use a faster division algorithm when divisor is short
00385                 static void Divide(word &r, Integer &q, const Integer &a, word d);
00386 
00387                 //! returns same result as Divide(r, q, a, Power2(n)), but faster
00388                 static void DivideByPowerOf2(Integer &r, Integer &q, const Integer &a, unsigned int n);
00389 
00390                 //! greatest common divisor
00391                 static Integer Gcd(const Integer &a, const Integer &n);
00392                 //! calculate multiplicative inverse of *this mod n
00393                 Integer InverseMod(const Integer &n) const;
00394                 //!
00395                 word InverseMod(word n) const;
00396         //@}
00397 
00398         //! \name INPUT/OUTPUT
00399         //@{
00400                 //!
00401                 friend CRYPTOPP_DLL std::istream& operator>>(std::istream& in, Integer &a);
00402                 //!
00403                 friend CRYPTOPP_DLL std::ostream& operator<<(std::ostream& out, const Integer &a);
00404         //@}
00405 
00406 private:
00407         friend class ModularArithmetic;
00408         friend class MontgomeryRepresentation;
00409         friend class HalfMontgomeryRepresentation;
00410 
00411         Integer(word value, unsigned int length);
00412 
00413         int PositiveCompare(const Integer &t) const;
00414         friend void PositiveAdd(Integer &sum, const Integer &a, const Integer &b);
00415         friend void PositiveSubtract(Integer &diff, const Integer &a, const Integer &b);
00416         friend void PositiveMultiply(Integer &product, const Integer &a, const Integer &b);
00417         friend void PositiveDivide(Integer &remainder, Integer &quotient, const Integer &dividend, const Integer &divisor);
00418 
00419         SecAlignedWordBlock reg;
00420         Sign sign;
00421 };
00422 
00423 //!
00424 inline bool operator==(const CryptoPP::Integer& a, const CryptoPP::Integer& b) {return a.Compare(b)==0;}
00425 //!
00426 inline bool operator!=(const CryptoPP::Integer& a, const CryptoPP::Integer& b) {return a.Compare(b)!=0;}
00427 //!
00428 inline bool operator> (const CryptoPP::Integer& a, const CryptoPP::Integer& b) {return a.Compare(b)> 0;}
00429 //!
00430 inline bool operator>=(const CryptoPP::Integer& a, const CryptoPP::Integer& b) {return a.Compare(b)>=0;}
00431 //!
00432 inline bool operator< (const CryptoPP::Integer& a, const CryptoPP::Integer& b) {return a.Compare(b)< 0;}
00433 //!
00434 inline bool operator<=(const CryptoPP::Integer& a, const CryptoPP::Integer& b) {return a.Compare(b)<=0;}
00435 //!
00436 inline CryptoPP::Integer operator+(const CryptoPP::Integer &a, const CryptoPP::Integer &b) {return a.Plus(b);}
00437 //!
00438 inline CryptoPP::Integer operator-(const CryptoPP::Integer &a, const CryptoPP::Integer &b) {return a.Minus(b);}
00439 //!
00440 inline CryptoPP::Integer operator*(const CryptoPP::Integer &a, const CryptoPP::Integer &b) {return a.Times(b);}
00441 //!
00442 inline CryptoPP::Integer operator/(const CryptoPP::Integer &a, const CryptoPP::Integer &b) {return a.DividedBy(b);}
00443 //!
00444 inline CryptoPP::Integer operator%(const CryptoPP::Integer &a, const CryptoPP::Integer &b) {return a.Modulo(b);}
00445 //!
00446 inline CryptoPP::Integer operator/(const CryptoPP::Integer &a, CryptoPP::word b) {return a.DividedBy(b);}
00447 //!
00448 inline CryptoPP::word    operator%(const CryptoPP::Integer &a, CryptoPP::word b) {return a.Modulo(b);}
00449 
00450 NAMESPACE_END
00451 
00452 NAMESPACE_BEGIN(std)
00453 template<> inline void swap(CryptoPP::Integer &a, CryptoPP::Integer &b)
00454 {
00455         a.swap(b);
00456 }
00457 NAMESPACE_END
00458 
00459 #endif

Generated on Sat Jul 10 22:55:25 2004 for Crypto++ by doxygen 1.3.6