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