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;}
510 if (!GenerateRandomNoThrow(rng, params))
739 int PositiveCompare(
const Integer &t)
const;
744 #ifndef CRYPTOPP_DOXYGEN_PROCESSING
747 friend class HalfMontgomeryRepresentation;
757 inline bool operator==(
const CryptoPP::Integer& a,
const CryptoPP::Integer& b) {
return a.Compare(b)==0;}
759 inline bool operator!=(
const CryptoPP::Integer& a,
const CryptoPP::Integer& b) {
return a.Compare(b)!=0;}
761 inline bool operator> (
const CryptoPP::Integer& a,
const CryptoPP::Integer& b) {
return a.Compare(b)> 0;}
763 inline bool operator>=(
const CryptoPP::Integer& a,
const CryptoPP::Integer& b) {
return a.Compare(b)>=0;}
765 inline bool operator< (
const CryptoPP::Integer& a,
const CryptoPP::Integer& b) {
return a.Compare(b)< 0;}
767 inline bool operator<=(
const CryptoPP::Integer& a,
const CryptoPP::Integer& b) {
return a.Compare(b)<=0;}
769 inline CryptoPP::Integer
operator+(
const CryptoPP::Integer &a,
const CryptoPP::Integer &b) {
return a.Plus(b);}
771 inline CryptoPP::Integer
operator-(
const CryptoPP::Integer &a,
const CryptoPP::Integer &b) {
return a.Minus(b);}
774 inline CryptoPP::Integer
operator*(
const CryptoPP::Integer &a,
const CryptoPP::Integer &b) {
return a.Times(b);}
776 inline CryptoPP::Integer
operator/(
const CryptoPP::Integer &a,
const CryptoPP::Integer &b) {
return a.DividedBy(b);}
779 inline CryptoPP::Integer
operator%(
const CryptoPP::Integer &a,
const CryptoPP::Integer &b) {
return a.Modulo(b);}
798 inline CryptoPP::Integer
operator&(
const CryptoPP::Integer &a,
const CryptoPP::Integer &b) {
return a.And(b);}
812 inline CryptoPP::Integer
operator|(
const CryptoPP::Integer &a,
const CryptoPP::Integer &b) {
return a.Or(b);}
826 inline CryptoPP::Integer
operator^(
const CryptoPP::Integer &a,
const CryptoPP::Integer &b) {
return a.Xor(b);}
832 inline 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.
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.
Integer & operator&=(const Integer &t)
Bitwise AND Assignment.
bool IsPositive() const
Determines if the Integer is positive.
Integer operator++(int)
Post-increment.
friend CRYPTOPP_DLL std::ostream & operator<<(std::ostream &out, const Integer &a)
Insertion 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.
friend CRYPTOPP_DLL std::istream & operator>>(std::istream &in, Integer &a)
Extraction operator.
word InverseMod(word n) const
Calculate multiplicative inverse.
Integer & operator%=(word t)
Remainder 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%=(const Integer &t)
Remainder Assignment.
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.
Integer & operator^=(const Integer &t)
Bitwise XOR Assignment.
bool NotZero() const
Determines if the Integer is non-0.
Integer Times(const Integer &b) const
Multiplication.
Integer & operator=(const Integer &t)
Assignment.
Integer operator>>(size_t n) const
Right-shift.
void BERDecodeAsOctetString(BufferedTransformation &bt, size_t length)
Decode nonnegative value from big-endian octet string.
byte GetByte(size_t i) const
Provides the i-th byte of the Integer.
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.
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 & operator/=(word t)
Division Assignment.
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.
Integer & operator+=(const Integer &t)
Addition Assignment.
static Integer Power2(size_t e)
Exponentiates to a power of 2.
Integer & operator|=(const Integer &t)
Bitwise OR Assignment.
Integer Squared() const
Multiply this integer by itself.
Integer()
Creates the zero integer.
Integer & operator/=(const Integer &t)
Division Assignment.
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.
void SetNegative()
Sets the Integer to negative.
static const Integer & One()
Integer representing 1.
unsigned int BitCount() const
Determines the number of bits required to represent the Integer.
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.
Integer & operator++()
Pre-increment.
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--()
Pre-decrement.
Integer operator+() const
Addition.
RandomNumberType
Properties of a random integer.
@ ANY
a number with no special properties
bool operator!() const
Negation.
static const Integer & Zero()
Integer representing 0.
Integer & operator-=(const Integer &t)
Subtraction Assignment.
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(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.
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.
Integer & operator<<=(size_t n)
Left-shift Assignment.
bool IsNegative() const
Determines if the Integer is negative.
Integer & operator*=(const Integer &t)
Multiplication Assignment.
void Decode(const byte *input, size_t inputLen, Signedness sign=UNSIGNED)
Decode from big-endian byte array.
Sign
Used internally to represent the integer.
Integer(const wchar_t *str, ByteOrder order=BIG_ENDIAN_ORDER)
Convert from a wide C-string.
static const Integer & Two()
Integer representing 2.
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.
Integer & operator>>=(size_t n)
Right-shift Assignment.
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.
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.
void swap(::SecBlock< T, A > &a, ::SecBlock< T, A > &b)
Swap two SecBlocks.
Performs static initialization of the Integer class.