16#ifndef CRYPTOPP_INTEGER_H
17#define CRYPTOPP_INTEGER_H
353 bool IsEven()
const {
return GetBit(0) == 0;}
356 bool IsOdd()
const {
return GetBit(0) == 1;}
511 if (!GenerateRandomNoThrow(rng, params))
740 int PositiveCompare(
const Integer &t)
const;
745#ifndef CRYPTOPP_DOXYGEN_PROCESSING
748 friend class HalfMontgomeryRepresentation;
758inline bool operator==(
const CryptoPP::Integer& a,
const CryptoPP::Integer& b) {
return a.Compare(b)==0;}
760inline bool operator!=(
const CryptoPP::Integer& a,
const CryptoPP::Integer& b) {
return a.Compare(b)!=0;}
762inline bool operator> (
const CryptoPP::Integer& a,
const CryptoPP::Integer& b) {
return a.Compare(b)> 0;}
764inline bool operator>=(
const CryptoPP::Integer& a,
const CryptoPP::Integer& b) {
return a.Compare(b)>=0;}
766inline bool operator< (
const CryptoPP::Integer& a,
const CryptoPP::Integer& b) {
return a.Compare(b)< 0;}
768inline bool operator<=(
const CryptoPP::Integer& a,
const CryptoPP::Integer& b) {
return a.Compare(b)<=0;}
770inline CryptoPP::Integer
operator+(
const CryptoPP::Integer &a,
const CryptoPP::Integer &b) {
return a.
Plus(b);}
772inline CryptoPP::Integer
operator-(
const CryptoPP::Integer &a,
const CryptoPP::Integer &b) {
return a.
Minus(b);}
775inline CryptoPP::Integer
operator*(
const CryptoPP::Integer &a,
const CryptoPP::Integer &b) {
return a.
Times(b);}
777inline CryptoPP::Integer
operator/(
const CryptoPP::Integer &a,
const CryptoPP::Integer &b) {
return a.
DividedBy(b);}
780inline CryptoPP::Integer
operator%(
const CryptoPP::Integer &a,
const CryptoPP::Integer &b) {
return a.
Modulo(b);}
782inline CryptoPP::Integer
operator/(
const CryptoPP::Integer &a, CryptoPP::word b) {
return a.
DividedBy(b);}
785inline CryptoPP::word
operator%(
const CryptoPP::Integer &a, CryptoPP::word b) {
return a.
Modulo(b);}
799inline CryptoPP::Integer
operator&(
const CryptoPP::Integer &a,
const CryptoPP::Integer &b) {
return a.
And(b);}
813inline CryptoPP::Integer
operator|(
const CryptoPP::Integer &a,
const CryptoPP::Integer &b) {
return a.
Or(b);}
827inline CryptoPP::Integer
operator^(
const CryptoPP::Integer &a,
const CryptoPP::Integer &b) {
return a.
Xor(b);}
833inline void swap(CryptoPP::Integer &a, CryptoPP::Integer &b)
Interface for encoding and decoding ASN1 objects.
Base class for all exceptions thrown by the library.
Exception thrown when division by 0 is encountered.
Exception thrown when an error is encountered decoding an OpenPGP integer.
Exception thrown when a random number cannot be found that satisfies the condition.
Multiple precision integer with arithmetic operations.
Integer operator--(int)
Post-decrement.
static void Divide(Integer &r, Integer &q, const Integer &a, const Integer &d)
Extended Division.
void DEREncode(BufferedTransformation &bt) const
Encode in DER format.
Integer & operator>>=(size_t n)
Right-shift Assignment.
Integer & operator/=(const Integer &t)
Division Assignment.
Integer & operator&=(const Integer &t)
Bitwise AND Assignment.
void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs ¶ms=g_nullNameValuePairs)
Generate a random number.
bool GetBit(size_t i) const
Provides the i-th bit of the Integer.
void SetByte(size_t n, byte value)
Set the n-th byte to value.
static void DivideByPowerOf2(Integer &r, Integer &q, const Integer &a, unsigned int n)
Extended Division.
bool IsPositive() const
Determines if the Integer is positive.
Integer operator++(int)
Post-increment.
friend CRYPTOPP_DLL std::istream & operator>>(std::istream &in, Integer &a)
Extraction operator.
Integer(BufferedTransformation &bt, size_t byteCount, Signedness sign=UNSIGNED, ByteOrder order=BIG_ENDIAN_ORDER)
Convert from a big-endian array.
Integer Minus(const Integer &b) const
Subtraction.
Integer(const byte *encodedInteger, size_t byteCount, Signedness sign=UNSIGNED, ByteOrder order=BIG_ENDIAN_ORDER)
Convert from a big-endian byte array.
signed long ConvertToLong() const
Convert the Integer to Long.
Integer operator-() const
Subtraction.
void SetBit(size_t n, bool value=1)
Set the n-th bit to value.
word InverseMod(word n) const
Calculate multiplicative inverse.
Integer & operator+=(const Integer &t)
Addition Assignment.
Integer And(const Integer &t) const
Bitwise AND.
bool IsSquare() const
Determine whether this integer is a perfect square.
Integer Plus(const Integer &b) const
Addition.
Integer DividedBy(const Integer &b) const
Division.
Integer DividedBy(word b) const
Division.
Integer & operator++()
Pre-increment.
void DEREncodeAsOctetString(BufferedTransformation &bt, size_t length) const
Encode absolute value as big-endian octet string.
void OpenPGPDecode(const byte *input, size_t inputLen)
Decode from OpenPGP format.
Integer Doubled() const
Add this integer to itself.
bool NotZero() const
Determines if the Integer is non-0.
Integer Times(const Integer &b) const
Multiplication.
Integer operator>>(size_t n) const
Right-shift.
void BERDecodeAsOctetString(BufferedTransformation &bt, size_t length)
Decode nonnegative value from big-endian octet string.
Integer & operator--()
Pre-decrement.
byte GetByte(size_t i) const
Provides the i-th byte of the Integer.
static const Integer & Zero()
Integer representing 0.
word Modulo(word b) const
Remainder.
void Randomize(RandomNumberGenerator &rng, size_t bitCount)
Set this Integer to random integer.
CRYPTOPP_DLL friend Integer a_times_b_mod_c(const Integer &x, const Integer &y, const Integer &m)
Modular multiplication.
Integer & operator%=(word t)
Remainder Assignment.
bool IsConvertableToLong() const
Determines if the Integer is convertable to Long.
Integer(Sign sign, lword value)
Convert from lword.
void Encode(BufferedTransformation &bt, size_t outputLen, Signedness sign=UNSIGNED) const
Encode in big-endian format.
Integer Or(const Integer &t) const
Bitwise OR.
lword GetBits(size_t i, size_t n) const
Provides the low order bits of the Integer.
static Integer Power2(size_t e)
Exponentiates to a power of 2.
Integer Squared() const
Multiply this integer by itself.
Integer()
Creates the zero integer.
void BERDecode(const byte *input, size_t inputLen)
Decode from BER format.
Integer(BufferedTransformation &bt)
Convert from a BER encoded byte array.
size_t MinEncodedSize(Signedness sign=UNSIGNED) const
Minimum number of bytes to encode this integer.
bool NotPositive() const
Determines if the Integer is non-positive.
bool Randomize(RandomNumberGenerator &rng, const Integer &min, const Integer &max, RandomNumberType rnType, const Integer &equiv=Zero(), const Integer &mod=One())
Set this Integer to random integer of special form.
Integer & operator/=(word t)
Division Assignment.
void SetNegative()
Sets the Integer to negative.
unsigned int BitCount() const
Determines the number of bits required to represent the Integer.
Integer & operator|=(const Integer &t)
Bitwise OR Assignment.
void BERDecode(BufferedTransformation &bt)
Decode from BER format.
void Negate()
Reverse the Sign of the Integer.
bool NotNegative() const
Determines if the Integer is non-negative.
Integer(RandomNumberGenerator &rng, const Integer &min, const Integer &max, RandomNumberType rnType=ANY, const Integer &equiv=Zero(), const Integer &mod=One())
Create a random integer of special form.
void SetPositive()
Sets the Integer to positive.
unsigned int WordCount() const
Determines the number of words required to represent the Integer.
CRYPTOPP_DLL friend Integer a_exp_b_mod_c(const Integer &x, const Integer &e, const Integer &m)
Modular exponentiation.
Integer & operator^=(const Integer &t)
Bitwise XOR Assignment.
Integer operator+() const
Addition.
Integer & operator=(const Integer &t)
Assignment.
Integer & operator-=(const Integer &t)
Subtraction Assignment.
RandomNumberType
Properties of a random integer.
@ ANY
a number with no special properties
bool operator!() const
Negation.
Integer(const Integer &t)
copy constructor
Integer AbsoluteValue() const
Retrieve the absolute value of this integer.
void OpenPGPDecode(BufferedTransformation &bt)
Decode from OpenPGP format.
Integer & operator*=(const Integer &t)
Multiplication Assignment.
Integer(const char *str, ByteOrder order=BIG_ENDIAN_ORDER)
Convert from a C-string.
Signedness
Used when importing and exporting integers.
@ UNSIGNED
an unsigned value
Integer Xor(const Integer &t) const
Bitwise XOR.
Integer(Sign sign, word highWord, word lowWord)
Convert from two words.
void Randomize(RandomNumberGenerator &rng, const Integer &min, const Integer &max)
Set this Integer to random integer.
Integer operator<<(size_t n) const
Left-shift.
int Compare(const Integer &a) const
Perform signed comparison.
Integer Modulo(const Integer &b) const
Remainder.
Integer(signed long value)
Convert from signed long.
size_t OpenPGPEncode(BufferedTransformation &bt) const
Encode absolute value in OpenPGP format.
size_t OpenPGPEncode(byte *output, size_t bufferSize) const
Encode absolute value in OpenPGP format.
void swap(Integer &a)
Swaps this Integer with another Integer.
static const Integer & Two()
Integer representing 2.
Integer & operator%=(const Integer &t)
Remainder Assignment.
bool IsZero() const
Determines if the Integer is 0.
Integer MultiplicativeInverse() const
Calculate multiplicative inverse.
bool GenerateRandomNoThrow(RandomNumberGenerator &rng, const NameValuePairs ¶ms=g_nullNameValuePairs)
Generate a random number.
bool IsNegative() const
Determines if the Integer is negative.
void Decode(const byte *input, size_t inputLen, Signedness sign=UNSIGNED)
Decode from big-endian byte array.
Integer & operator<<=(size_t n)
Left-shift Assignment.
Sign
Used internally to represent the integer.
Integer(const wchar_t *str, ByteOrder order=BIG_ENDIAN_ORDER)
Convert from a wide C-string.
unsigned int ByteCount() const
Determines the number of bytes required to represent the Integer.
void Decode(BufferedTransformation &bt, size_t inputLen, Signedness sign=UNSIGNED)
Decode nonnegative value from big-endian byte array.
bool IsUnit() const
Determine if 1 or -1.
Integer(RandomNumberGenerator &rng, size_t bitCount)
Create a random integer.
bool IsOdd() const
Determines if the Integer is odd parity.
static void Divide(word &r, Integer &q, const Integer &a, word d)
Extended Division.
friend CRYPTOPP_DLL std::ostream & operator<<(std::ostream &out, const Integer &a)
Insertion operator.
static Integer Gcd(const Integer &a, const Integer &n)
Calculate greatest common divisor.
void Encode(byte *output, size_t outputLen, Signedness sign=UNSIGNED) const
Encode in big-endian format.
Integer InverseMod(const Integer &n) const
Calculate multiplicative inverse.
Integer SquareRoot() const
Extract square root.
static const Integer & One()
Integer representing 1.
bool IsEven() const
Determines if the Integer is even parity.
Ring of congruence classes modulo n.
Performs modular arithmetic in Montgomery representation for increased speed.
Interface for retrieving values given their names.
Interface for random number generators.
Secure memory block with allocator and cleanup.
#define CRYPTOPP_API
Win32 calling convention.
word64 word
Full word used for multiprecision integer arithmetic.
word64 lword
Large word type.
Abstract base classes that provide a uniform interface to this library.
const NameValuePairs & g_nullNameValuePairs
An empty set of name-value pairs.
ByteOrder
Provides the byte ordering.
@ BIG_ENDIAN_ORDER
byte order is big-endian
inline ::Integer operator&(const ::Integer &a, const ::Integer &b)
Bitwise AND.
inline ::Integer operator%(const ::Integer &a, const ::Integer &b)
Remainder.
bool operator<(const ::Integer &a, const ::Integer &b)
Comparison.
inline ::Integer operator-(const ::Integer &a, const ::Integer &b)
Subtraction.
inline ::Integer operator^(const ::Integer &a, const ::Integer &b)
Bitwise XOR.
inline ::Integer operator|(const ::Integer &a, const ::Integer &b)
Bitwise OR.
bool operator>=(const ::Integer &a, const ::Integer &b)
Comparison.
inline ::Integer operator*(const ::Integer &a, const ::Integer &b)
Multiplication.
bool operator<=(const ::Integer &a, const ::Integer &b)
Comparison.
inline ::Integer operator/(const ::Integer &a, const ::Integer &b)
Division.
bool operator==(const ::Integer &a, const ::Integer &b)
Comparison.
bool operator>(const ::Integer &a, const ::Integer &b)
Comparison.
bool operator!=(const ::Integer &a, const ::Integer &b)
Comparison.
inline ::Integer operator+(const ::Integer &a, const ::Integer &b)
Addition.
Crypto++ library namespace.
Classes and functions for secure memory allocations.
Performs static initialization of the Integer class.