12 #ifndef CRYPTOPP_INTEGER_H
13 #define CRYPTOPP_INTEGER_H
21 NAMESPACE_BEGIN(CryptoPP)
31 #if defined(CRYPTOPP_WORD128_AVAILABLE)
108 Integer(Sign sign, lword value);
114 Integer(Sign sign, word highWord, word lowWord);
165 static const Integer & CRYPTOPP_API Zero();
169 static const Integer & CRYPTOPP_API One();
173 static const Integer & CRYPTOPP_API Two();
196 static Integer CRYPTOPP_API Power2(
size_t e);
204 size_t MinEncodedSize(Signedness sign=UNSIGNED)
const;
213 void Encode(byte *output,
size_t outputLen, Signedness sign=UNSIGNED)
const;
241 size_t OpenPGPEncode(byte *output,
size_t bufferSize)
const;
254 void Decode(
const byte *input,
size_t inputLen, Signedness sign=UNSIGNED);
266 void BERDecode(
const byte *input,
size_t inputLen);
287 void OpenPGPDecode(
const byte *input,
size_t inputLen);
298 bool IsConvertableToLong()
const;
302 signed long ConvertToLong()
const;
306 unsigned int BitCount()
const;
309 unsigned int ByteCount()
const;
312 unsigned int WordCount()
const;
316 bool GetBit(
size_t i)
const;
322 lword GetBits(
size_t i,
size_t n)
const;
344 bool IsEven()
const {
return GetBit(0) == 0;}
347 bool IsOdd()
const {
return GetBit(0) == 1;}
363 Integer& operator/=(
const Integer& t) {
return *
this = DividedBy(t);}
368 Integer& operator/=(word t) {
return *
this = DividedBy(t);}
412 if (!GenerateRandomNoThrow(rng, params))
413 throw RandomNumberNotFound();
418 void SetBit(
size_t n,
bool value=1);
422 void SetByte(
size_t n, byte value);
440 bool operator!()
const;
450 Integer operator++(
int) {
Integer temp = *
this; ++*
this;
return temp;}
452 Integer operator--(
int) {
Integer temp = *
this; --*
this;
return temp;}
462 int Compare(
const Integer& a)
const;
477 Integer DividedBy(word b)
const;
480 word Modulo(word b)
const;
493 Integer Doubled()
const {
return Plus(*
this);}
500 bool IsSquare()
const;
505 Integer MultiplicativeInverse()
const;
510 static void CRYPTOPP_API Divide(word &r,
Integer &q,
const Integer &a, word d);
522 word InverseMod(word n)
const;
531 friend CRYPTOPP_DLL std::istream& CRYPTOPP_API operator>>(std::istream& in,
Integer &a);
543 friend CRYPTOPP_DLL std::ostream& CRYPTOPP_API operator<<(std::ostream& out,
const Integer &a);
546 #ifndef CRYPTOPP_DOXYGEN_PROCESSING
555 Integer(word value,
size_t length);
556 int PositiveCompare(
const Integer &t)
const;
561 #ifndef CRYPTOPP_DOXYGEN_PROCESSING
564 friend class HalfMontgomeryRepresentation;
574 inline bool operator==(
const CryptoPP::Integer& a,
const CryptoPP::Integer& b) {
return a.Compare(b)==0;}
576 inline bool operator!=(
const CryptoPP::Integer& a,
const CryptoPP::Integer& b) {
return a.Compare(b)!=0;}
578 inline bool operator> (
const CryptoPP::Integer& a,
const CryptoPP::Integer& b) {
return a.Compare(b)> 0;}
580 inline bool operator>=(
const CryptoPP::Integer& a,
const CryptoPP::Integer& b) {
return a.Compare(b)>=0;}
582 inline bool operator< (
const CryptoPP::Integer& a,
const CryptoPP::Integer& b) {
return a.Compare(b)< 0;}
584 inline bool operator<=(
const CryptoPP::Integer& a,
const CryptoPP::Integer& b) {
return a.Compare(b)<=0;}
586 inline CryptoPP::Integer
operator+(
const CryptoPP::Integer &a,
const CryptoPP::Integer &b) {
return a.Plus(b);}
588 inline CryptoPP::Integer operator-(
const CryptoPP::Integer &a,
const CryptoPP::Integer &b) {
return a.Minus(b);}
591 inline CryptoPP::Integer
operator*(
const CryptoPP::Integer &a,
const CryptoPP::Integer &b) {
return a.Times(b);}
593 inline CryptoPP::Integer operator/(
const CryptoPP::Integer &a,
const CryptoPP::Integer &b) {
return a.DividedBy(b);}
596 inline CryptoPP::Integer operator%(
const CryptoPP::Integer &a,
const CryptoPP::Integer &b) {
return a.Modulo(b);}
598 inline CryptoPP::Integer operator/(
const CryptoPP::Integer &a, CryptoPP::word b) {
return a.DividedBy(b);}
601 inline CryptoPP::word operator%(
const CryptoPP::Integer &a, CryptoPP::word b) {
return a.Modulo(b);}
Base class for all exceptions thrown by the library.
bool operator>=(const ::PolynomialMod2 &a, const ::PolynomialMod2 &b)
compares degree
bool operator>(const ::PolynomialMod2 &a, const ::PolynomialMod2 &b)
compares degree
virtual void DEREncode(BufferedTransformation &bt) const =0
Encode this object into a BufferedTransformation.
void SetNegative()
Sets the Integer to negative.
inline::Integer operator*(const ::Integer &a, const ::Integer &b)
ByteOrder
Provides the byte ordering.
bool NotZero() const
Determines if the Integer is non-0.
bool IsOdd() const
Determines if the Integer is odd parity.
void SetPositive()
Sets the Integer to positive.
bool IsEven() const
Determines if the Integer is even parity.
Secure memory block with allocator and cleanup.
Abstract base classes that provide a uniform interface to this library.
Signedness
Used when importing and exporting integers.
Ring of congruence classes modulo n.
Interface for random number generators.
bool operator==(const OID &lhs, const OID &rhs)
Compare two OIDs for equality.
Sign
Used internally to represent the integer.
Classes and functions for secure memory allocations.
bool operator!=(const OID &lhs, const OID &rhs)
Compare two OIDs for inequality.
bool IsPositive() const
Determines if the Integer is positive.
a number with no special properties
bool operator<(const OID &lhs, const OID &rhs)
Compare two OIDs for ordering.
bool IsNegative() const
Determines if the Integer is negative.
bool NotPositive() const
Determines if the Integer is non-positive.
virtual void BERDecode(BufferedTransformation &bt)=0
Decode this object from a BufferedTransformation.
Exception thrown when an error is encountered decoding an OpenPGP integer.
Interface for encoding and decoding ASN1 objects.
Performs static intialization of the Integer class.
Multiple precision integer with arithmetic operations.
OID operator+(const OID &lhs, unsigned long rhs)
Append a value to an OID.
const NameValuePairs & g_nullNameValuePairs
An empty set of name-value pairs.
RandomNumberType
Properties of a random integer.
Integer & operator*=(const Integer &t)
bool IsZero() const
Determines if the Integer is 0.
Exception thrown when division by 0 is encountered.
Exception thrown when a random number cannot be found that satisfies the condition.
Performs modular arithmetic in Montgomery representation for increased speed.
Integer & operator%=(word t)
unsigned int GetByte(ByteOrder order, T value, unsigned int index)
Gets a byte from a value.
Integer & operator%=(const Integer &t)
bool operator<=(const ::PolynomialMod2 &a, const ::PolynomialMod2 &b)
compares degree
bool NotNegative() const
Determines if the Integer is non-negative.
Interface for retrieving values given their names.