Security Scol plugin
Classes | List of all members
Integer Class Reference

Multiple precision integer with arithmetic operations. More...

#include <integer.h>

Inheritance diagram for Integer:
InitializeInteger ASN1Object

Classes

class  DivideByZero
 Exception thrown when division by 0 is encountered. More...
 
class  OpenPGPDecodeErr
 Exception thrown when an error is encountered decoding an OpenPGP integer. More...
 
class  RandomNumberNotFound
 Exception thrown when a random number cannot be found that satisfies the condition. More...
 

ENUMS, EXCEPTIONS, and TYPEDEFS

enum  Sign { POSITIVE =0 , NEGATIVE =1 }
 Used internally to represent the integer. More...
 
enum  Signedness { UNSIGNED , SIGNED }
 Used when importing and exporting integers. More...
 
enum  RandomNumberType { ANY , PRIME }
 Properties of a random integer. More...
 

INPUT/OUTPUT

class ModularArithmetic
 
class MontgomeryRepresentation
 
class HalfMontgomeryRepresentation
 
CRYPTOPP_DLL std::istream &CRYPTOPP_API operator>> (std::istream &in, Integer &a)
 Extraction operator.
 
CRYPTOPP_DLL std::ostream &CRYPTOPP_API operator<< (std::ostream &out, const Integer &a)
 Insertion operator.
 
CRYPTOPP_DLL friend Integer CRYPTOPP_API a_times_b_mod_c (const Integer &x, const Integer &y, const Integer &m)
 Modular multiplication.
 
CRYPTOPP_DLL friend Integer CRYPTOPP_API a_exp_b_mod_c (const Integer &x, const Integer &e, const Integer &m)
 Modular exponentiation.
 
void PositiveAdd (Integer &sum, const Integer &a, const Integer &b)
 
void PositiveSubtract (Integer &diff, const Integer &a, const Integer &b)
 
void PositiveMultiply (Integer &product, const Integer &a, const Integer &b)
 
void PositiveDivide (Integer &remainder, Integer &quotient, const Integer &dividend, const Integer &divisor)
 
Integer InverseModNext (const Integer &n) const
 

CREATORS

 Integer ()
 Creates the zero integer.
 
 Integer (const Integer &t)
 copy constructor
 
 Integer (signed long value)
 Convert from signed long.
 
 Integer (Sign sign, lword value)
 Convert from lword.
 
 Integer (Sign sign, word highWord, word lowWord)
 Convert from two words.
 
 Integer (const char *str, ByteOrder order=BIG_ENDIAN_ORDER)
 Convert from a C-string.
 
 Integer (const wchar_t *str, ByteOrder order=BIG_ENDIAN_ORDER)
 Convert from a wide C-string.
 
 Integer (const byte *encodedInteger, size_t byteCount, Signedness sign=UNSIGNED, ByteOrder order=BIG_ENDIAN_ORDER)
 Convert from a big-endian byte array.
 
 Integer (BufferedTransformation &bt, size_t byteCount, Signedness sign=UNSIGNED, ByteOrder order=BIG_ENDIAN_ORDER)
 Convert from a big-endian array.
 
 Integer (BufferedTransformation &bt)
 Convert from a BER encoded byte array.
 
 Integer (RandomNumberGenerator &rng, size_t bitCount)
 Create a random integer.
 
 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.
 
static const Integer &CRYPTOPP_API Zero ()
 Integer representing 0.
 
static const Integer &CRYPTOPP_API One ()
 Integer representing 1.
 
static const Integer &CRYPTOPP_API Two ()
 Integer representing 2.
 
static Integer CRYPTOPP_API Power2 (size_t e)
 Exponentiates to a power of 2.
 

ENCODE/DECODE

size_t MinEncodedSize (Signedness sign=UNSIGNED) const
 Minimum number of bytes to encode this integer.
 
void Encode (byte *output, size_t outputLen, Signedness sign=UNSIGNED) const
 Encode in big-endian format.
 
void Encode (BufferedTransformation &bt, size_t outputLen, Signedness sign=UNSIGNED) const
 Encode in big-endian format.
 
void DEREncode (BufferedTransformation &bt) const
 Encode in DER format.
 
void DEREncodeAsOctetString (BufferedTransformation &bt, size_t length) const
 Encode absolute value as big-endian octet string.
 
size_t OpenPGPEncode (byte *output, size_t bufferSize) const
 Encode absolute value in OpenPGP format.
 
size_t OpenPGPEncode (BufferedTransformation &bt) const
 Encode absolute value in OpenPGP format.
 
void Decode (const byte *input, size_t inputLen, Signedness sign=UNSIGNED)
 Decode from big-endian byte array.
 
void Decode (BufferedTransformation &bt, size_t inputLen, Signedness sign=UNSIGNED)
 Decode nonnegative value from big-endian byte array.
 
void BERDecode (const byte *input, size_t inputLen)
 Decode from BER format.
 
void BERDecode (BufferedTransformation &bt)
 Decode from BER format.
 
void BERDecodeAsOctetString (BufferedTransformation &bt, size_t length)
 Decode nonnegative value from big-endian octet string.
 
void OpenPGPDecode (const byte *input, size_t inputLen)
 Decode from OpenPGP format.
 
void OpenPGPDecode (BufferedTransformation &bt)
 Decode from OpenPGP format.
 

ACCESSORS

bool IsConvertableToLong () const
 Determines if the Integer is convertable to Long.
 
signed long ConvertToLong () const
 Convert the Integer to Long.
 
unsigned int BitCount () const
 Determines the number of bits required to represent the Integer.
 
unsigned int ByteCount () const
 Determines the number of bytes required to represent the Integer.
 
unsigned int WordCount () const
 Determines the number of words required to represent the Integer.
 
bool GetBit (size_t i) const
 Provides the i-th bit of the Integer.
 
byte GetByte (size_t i) const
 Provides the i-th byte of the Integer.
 
lword GetBits (size_t i, size_t n) const
 Provides the low order bits of the Integer.
 
bool IsZero () const
 Determines if the Integer is 0.
 
bool NotZero () const
 Determines if the Integer is non-0.
 
bool IsNegative () const
 Determines if the Integer is negative.
 
bool NotNegative () const
 Determines if the Integer is non-negative.
 
bool IsPositive () const
 Determines if the Integer is positive.
 
bool NotPositive () const
 Determines if the Integer is non-positive.
 
bool IsEven () const
 Determines if the Integer is even parity.
 
bool IsOdd () const
 Determines if the Integer is odd parity.
 

MANIPULATORS

Integeroperator= (const Integer &t)
 Assignment.
 
Integeroperator+= (const Integer &t)
 Addition Assignment.
 
Integeroperator-= (const Integer &t)
 Subtraction Assignment.
 
Integeroperator*= (const Integer &t)
 Multiplication Assignment.
 
Integeroperator/= (const Integer &t)
 Division Assignment.
 
Integeroperator%= (const Integer &t)
 Remainder Assignment.
 
Integeroperator/= (word t)
 Division Assignment.
 
Integeroperator%= (word t)
 Remainder Assignment.
 
Integeroperator<<= (size_t n)
 Left-shift Assignment.
 
Integeroperator>>= (size_t n)
 Right-shift Assignment.
 
Integeroperator&= (const Integer &t)
 Bitwise AND Assignment.
 
Integeroperator|= (const Integer &t)
 Bitwise OR Assignment.
 
Integeroperator^= (const Integer &t)
 Bitwise XOR Assignment.
 
void Randomize (RandomNumberGenerator &rng, size_t bitCount)
 Set this Integer to random integer.
 
void Randomize (RandomNumberGenerator &rng, const Integer &min, const Integer &max)
 Set this Integer to random integer.
 
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.
 
bool GenerateRandomNoThrow (RandomNumberGenerator &rng, const NameValuePairs &params=g_nullNameValuePairs)
 Generate a random number.
 
void GenerateRandom (RandomNumberGenerator &rng, const NameValuePairs &params=g_nullNameValuePairs)
 Generate a random number.
 
void SetBit (size_t n, bool value=1)
 Set the n-th bit to value.
 
void SetByte (size_t n, byte value)
 Set the n-th byte to value.
 
void Negate ()
 Reverse the Sign of the Integer.
 
void SetPositive ()
 Sets the Integer to positive.
 
void SetNegative ()
 Sets the Integer to negative.
 
void swap (Integer &a)
 Swaps this Integer with another Integer.
 

UNARY OPERATORS

bool operator! () const
 Negation.
 
Integer operator+ () const
 Addition.
 
Integer operator- () const
 Subtraction.
 
Integeroperator++ ()
 Pre-increment.
 
Integeroperator-- ()
 Pre-decrement.
 
Integer operator++ (int)
 Post-increment.
 
Integer operator-- (int)
 Post-decrement.
 

BINARY OPERATORS

int Compare (const Integer &a) const
 Perform signed comparison.
 
Integer Plus (const Integer &b) const
 Addition.
 
Integer Minus (const Integer &b) const
 Subtraction.
 
Integer Times (const Integer &b) const
 Multiplication.
 
Integer DividedBy (const Integer &b) const
 Division.
 
Integer Modulo (const Integer &b) const
 Remainder.
 
Integer DividedBy (word b) const
 Division.
 
word Modulo (word b) const
 Remainder.
 
Integer And (const Integer &t) const
 Bitwise AND.
 
Integer Or (const Integer &t) const
 Bitwise OR.
 
Integer Xor (const Integer &t) const
 Bitwise XOR.
 
Integer operator>> (size_t n) const
 Right-shift.
 
Integer operator<< (size_t n) const
 Left-shift.
 

OTHER ARITHMETIC FUNCTIONS

Integer AbsoluteValue () const
 Retrieve the absolute value of this integer.
 
Integer Doubled () const
 Add this integer to itself.
 
Integer Squared () const
 Multiply this integer by itself.
 
Integer SquareRoot () const
 Extract square root.
 
bool IsSquare () const
 Determine whether this integer is a perfect square.
 
bool IsUnit () const
 Determine if 1 or -1.
 
Integer MultiplicativeInverse () const
 Calculate multiplicative inverse.
 
Integer InverseMod (const Integer &n) const
 Calculate multiplicative inverse.
 
word InverseMod (word n) const
 Calculate multiplicative inverse.
 
static void CRYPTOPP_API Divide (Integer &r, Integer &q, const Integer &a, const Integer &d)
 Extended Division.
 
static void CRYPTOPP_API Divide (word &r, Integer &q, const Integer &a, word d)
 Extended Division.
 
static void CRYPTOPP_API DivideByPowerOf2 (Integer &r, Integer &q, const Integer &a, unsigned int n)
 Extended Division.
 
static Integer CRYPTOPP_API Gcd (const Integer &a, const Integer &n)
 Calculate greatest common divisor.
 

Additional Inherited Members

- Public Member Functions inherited from ASN1Object
virtual void BEREncode (BufferedTransformation &bt) const
 Encode this object into a BufferedTransformation.
 

Detailed Description

Multiple precision integer with arithmetic operations.

The Integer class can represent positive and negative integers with absolute value less than (256**sizeof(word))(256**sizeof(int)).

Internally, the library uses a sign magnitude representation, and the class has two data members. The first is a IntegerSecBlock (a SecBlock<word>) and it is used to hold the representation. The second is a Sign (an enumeration), and it is used to track the sign of the Integer.

For details on how the Integer class initializes its function pointers using InitializeInteger and how it creates Integer::Zero(), Integer::One(), and Integer::Two(), then see the comments at the top of integer.cpp.

Since
Crypto++ 1.0

Definition at line 49 of file integer.h.

Member Enumeration Documentation

◆ RandomNumberType

Properties of a random integer.

Enumerator
ANY 

a number with no special properties

PRIME 

a number which is probabilistically prime

Definition at line 91 of file integer.h.

◆ Sign

Used internally to represent the integer.

Sign is used internally to represent the integer. It is also used in a few API functions.

See also
SetPositive(), SetNegative(), Signedness
Enumerator
POSITIVE 

the value is positive or 0

NEGATIVE 

the value is negative

Definition at line 73 of file integer.h.

◆ Signedness

Used when importing and exporting integers.

Signedness is usually used in API functions.

See also
Sign
Enumerator
UNSIGNED 

an unsigned value

SIGNED 

a signed value

Definition at line 83 of file integer.h.

Constructor & Destructor Documentation

◆ Integer() [1/12]

Integer::Integer ( )

Creates the zero integer.

Definition at line 2966 of file integer.cpp.

◆ Integer() [2/12]

Integer::Integer ( const Integer t)

copy constructor

Definition at line 2972 of file integer.cpp.

◆ Integer() [3/12]

Integer::Integer ( signed long  value)

Convert from signed long.

Definition at line 2985 of file integer.cpp.

◆ Integer() [4/12]

Integer::Integer ( Sign  sign,
lword  value 
)

Convert from lword.

Parameters
signenumeration indicating Sign
valuethe long word

Definition at line 2978 of file integer.cpp.

◆ Integer() [5/12]

Integer::Integer ( Sign  sign,
word  highWord,
word  lowWord 
)

Convert from two words.

Parameters
signenumeration indicating Sign
highWordthe high word
lowWordthe low word

Definition at line 2999 of file integer.cpp.

◆ Integer() [6/12]

Integer::Integer ( const char *  str,
ByteOrder  order = BIG_ENDIAN_ORDER 
)
explicit

Convert from a C-string.

Parameters
strC-string value
orderthe ByteOrder of the string to be processed

str can be in base 8, 10, or 16. Base is determined by a case insensitive suffix of 'o' (8), '.' (10), or 'h' (16). No suffix means base 10.

Byte order was added at Crypto++ 5.7 to allow use of little-endian integers with curve25519, Poly1305 and Microsoft CAPI.

Definition at line 3338 of file integer.cpp.

◆ Integer() [7/12]

Integer::Integer ( const wchar_t *  str,
ByteOrder  order = BIG_ENDIAN_ORDER 
)
explicit

Convert from a wide C-string.

Parameters
strwide C-string value
orderthe ByteOrder of the string to be processed

str can be in base 8, 10, or 16. Base is determined by a case insensitive suffix of 'o' (8), '.' (10), or 'h' (16). No suffix means base 10.

Byte order was added at Crypto++ 5.7 to allow use of little-endian integers with curve25519, Poly1305 and Microsoft CAPI.

Definition at line 3344 of file integer.cpp.

◆ Integer() [8/12]

Integer::Integer ( const byte encodedInteger,
size_t  byteCount,
Signedness  sign = UNSIGNED,
ByteOrder  order = BIG_ENDIAN_ORDER 
)

Convert from a big-endian byte array.

Parameters
encodedIntegerbig-endian byte array
byteCountlength of the byte array
signenumeration indicating Signedness
orderthe ByteOrder of the array to be processed

Byte order was added at Crypto++ 5.7 to allow use of little-endian integers with curve25519, Poly1305 and Microsoft CAPI.

Definition at line 3047 of file integer.cpp.

◆ Integer() [9/12]

Integer::Integer ( BufferedTransformation bt,
size_t  byteCount,
Signedness  sign = UNSIGNED,
ByteOrder  order = BIG_ENDIAN_ORDER 
)

Convert from a big-endian array.

Parameters
btBufferedTransformation object with big-endian byte array
byteCountlength of the byte array
signenumeration indicating Signedness
orderthe ByteOrder of the data to be processed

Byte order was added at Crypto++ 5.7 to allow use of little-endian integers with curve25519, Poly1305 and Microsoft CAPI.

Definition at line 3029 of file integer.cpp.

◆ Integer() [10/12]

Integer::Integer ( BufferedTransformation bt)
explicit

Convert from a BER encoded byte array.

Parameters
btBufferedTransformation object with BER encoded byte array

Definition at line 3070 of file integer.cpp.

◆ Integer() [11/12]

Integer::Integer ( RandomNumberGenerator rng,
size_t  bitCount 
)

Create a random integer.

Parameters
rngRandomNumberGenerator used to generate material
bitCountthe number of bits in the resulting integer

The random integer created is uniformly distributed over [0, 2bitCount].

Definition at line 3076 of file integer.cpp.

◆ Integer() [12/12]

Integer::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.

Parameters
rngRandomNumberGenerator used to generate material
minthe minimum value
maxthe maximum value
rnTypeRandomNumberType to specify the type
equivthe equivalence class based on the parameter mod
modthe modulus used to reduce the equivalence class
Exceptions
RandomNumberNotFoundif the set is empty.

Ideally, the random integer created should be uniformly distributed over {x | min <= x <= max and x is of rnType and x % mod == equiv}. However the actual distribution may not be uniform because sequential search is used to find an appropriate number from a random starting point.

May return (with very small probability) a pseudoprime when a prime is requested and max > lastSmallPrime*lastSmallPrime. lastSmallPrime is declared in nbtheory.h.

Definition at line 3081 of file integer.cpp.

Member Function Documentation

◆ AbsoluteValue()

Integer Integer::AbsoluteValue ( ) const

Retrieve the absolute value of this integer.

Definition at line 3166 of file integer.cpp.

◆ And()

Integer Integer::And ( const Integer t) const

Bitwise AND.

Parameters
tthe other Integer
Returns
the result of *this & t

And() performs a bitwise AND on the operands. Missing bits are truncated at the most significant bit positions, so the result is as small as the smaller of the operands.

Internally, Crypto++ uses a sign-magnitude representation. The library does not attempt to interpret bits, and the result is always POSITIVE. If needed, the integer should be converted to a 2's compliment representation before performing the operation.

Since
Crypto++ 6.0

Definition at line 3800 of file integer.cpp.

◆ BERDecode() [1/2]

void Integer::BERDecode ( BufferedTransformation bt)
virtual

Decode from BER format.

Parameters
btBufferedTransformation object

Implements ASN1Object.

Definition at line 3463 of file integer.cpp.

◆ BERDecode() [2/2]

void Integer::BERDecode ( const byte input,
size_t  inputLen 
)

Decode from BER format.

Parameters
inputbig-endian byte array
inputLenlength of the byte array

Definition at line 3456 of file integer.cpp.

◆ BERDecodeAsOctetString()

void Integer::BERDecodeAsOctetString ( BufferedTransformation bt,
size_t  length 
)

Decode nonnegative value from big-endian octet string.

Parameters
btBufferedTransformation object
lengthlength of the byte array

Definition at line 3479 of file integer.cpp.

◆ BitCount()

unsigned int Integer::BitCount ( ) const

Determines the number of bits required to represent the Integer.

Returns
number of significant bits

BitCount is calculated as floor(log2(abs(*this))) + 1.

Definition at line 3364 of file integer.cpp.

◆ ByteCount()

unsigned int Integer::ByteCount ( ) const

Determines the number of bytes required to represent the Integer.

Returns
number of significant bytes

ByteCount is calculated as ceiling(BitCount()/8).

Definition at line 3355 of file integer.cpp.

◆ Compare()

int Integer::Compare ( const Integer a) const

Perform signed comparison.

Parameters
athe Integer to compare
Return values
-1if *this < a
0if *this = a
1if *this > a

Definition at line 4397 of file integer.cpp.

◆ ConvertToLong()

signed long Integer::ConvertToLong ( ) const

Convert the Integer to Long.

Returns
equivalent signed long if possible, otherwise undefined
See also
IsConvertableToLong()

Definition at line 3020 of file integer.cpp.

◆ Decode() [1/2]

void Integer::Decode ( BufferedTransformation bt,
size_t  inputLen,
Signedness  sign = UNSIGNED 
)

Decode nonnegative value from big-endian byte array.

Parameters
btBufferedTransformation object
inputLenlength of the byte array
signenumeration indicating Signedness
Note
bt.MaxRetrievable() >= inputLen.

Definition at line 3380 of file integer.cpp.

◆ Decode() [2/2]

void Integer::Decode ( const byte input,
size_t  inputLen,
Signedness  sign = UNSIGNED 
)

Decode from big-endian byte array.

Parameters
inputbig-endian byte array
inputLenlength of the byte array
signenumeration indicating Signedness

Definition at line 3373 of file integer.cpp.

◆ DEREncode()

void Integer::DEREncode ( BufferedTransformation bt) const
virtual

Encode in DER format.

Parameters
btBufferedTransformation object

Encodes the Integer using Distinguished Encoding Rules The result is placed into a BufferedTransformation object

Implements ASN1Object.

Definition at line 3449 of file integer.cpp.

◆ DEREncodeAsOctetString()

void Integer::DEREncodeAsOctetString ( BufferedTransformation bt,
size_t  length 
) const

Encode absolute value as big-endian octet string.

Parameters
btBufferedTransformation object
lengththe number of mytes to decode

Definition at line 3472 of file integer.cpp.

◆ Divide() [1/2]

void Integer::Divide ( Integer r,
Integer q,
const Integer a,
const Integer d 
)
static

Extended Division.

Parameters
ra reference for the remainder
qa reference for the quotient
areference to the dividend
dreference to the divisor

Divide calculates r and q such that (a == d*q + r) && (0 <= r < abs(d)).

Definition at line 4240 of file integer.cpp.

◆ Divide() [2/2]

void Integer::Divide ( word &  r,
Integer q,
const Integer a,
word  d 
)
static

Extended Division.

Parameters
ra reference for the remainder
qa reference for the quotient
areference to the dividend
dreference to the divisor

Divide calculates r and q such that (a == d*q + r) && (0 <= r < abs(d)). This overload uses a faster division algorithm because the divisor is short.

Definition at line 4300 of file integer.cpp.

◆ DivideByPowerOf2()

void Integer::DivideByPowerOf2 ( Integer r,
Integer q,
const Integer a,
unsigned int  n 
)
static

Extended Division.

Parameters
ra reference for the remainder
qa reference for the quotient
areference to the dividend
nreference to the divisor

DivideByPowerOf2 calculates r and q such that (a == d*q + r) && (0 <= r < abs(d)). It returns same result as Divide(r, q, a, Power2(n)), but faster. This overload uses a faster division algorithm because the divisor is a power of 2.

Definition at line 4258 of file integer.cpp.

◆ DividedBy() [1/2]

Integer Integer::DividedBy ( const Integer b) const

Division.

Definition at line 4286 of file integer.cpp.

◆ DividedBy() [2/2]

Integer Integer::DividedBy ( word  b) const

Division.

Definition at line 4336 of file integer.cpp.

◆ Doubled()

Integer Integer::Doubled ( ) const
inline

Add this integer to itself.

Definition at line 630 of file integer.h.

◆ Encode() [1/2]

void Integer::Encode ( BufferedTransformation bt,
size_t  outputLen,
Signedness  sign = UNSIGNED 
) const

Encode in big-endian format.

Parameters
btBufferedTransformation object
outputLenlength of the encoding
signenumeration indicating Signedness

Unsigned means encode absolute value, signed means encode two's complement if negative.

outputLen can be used to ensure an Integer is encoded to an exact size (rather than a minimum size). An exact size is useful, for example, when encoding to a field element size.

Definition at line 3434 of file integer.cpp.

◆ Encode() [2/2]

void Integer::Encode ( byte output,
size_t  outputLen,
Signedness  sign = UNSIGNED 
) const

Encode in big-endian format.

Parameters
outputbig-endian byte array
outputLenlength of the byte array
signenumeration indicating Signedness

Unsigned means encode absolute value, signed means encode two's complement if negative.

outputLen can be used to ensure an Integer is encoded to an exact size (rather than a minimum size). An exact size is useful, for example, when encoding to a field element size.

Definition at line 3427 of file integer.cpp.

◆ Gcd()

Integer Integer::Gcd ( const Integer a,
const Integer n 
)
static

Calculate greatest common divisor.

Parameters
areference to the first number
nreference to the secind number
Returns
the greatest common divisor a and n.

Definition at line 4468 of file integer.cpp.

◆ GenerateRandom()

void Integer::GenerateRandom ( RandomNumberGenerator rng,
const NameValuePairs params = g_nullNameValuePairs 
)
inline

Generate a random number.

Parameters
rngRandomNumberGenerator used to generate material
paramsadditional parameters that cannot be passed directly to the function
Exceptions
RandomNumberNotFoundif a random number is not found

GenerateRandom attempts to generate a random number according to the parameters specified in params.

The example below generates a prime number using NameValuePairs that Integer class recognizes. The names are not provided in argnames.h.

   AutoSeededRandomPool prng;
   AlgorithmParameters params = MakeParameters("BitLength", 2048)
                                              ("RandomNumberType", Integer::PRIME);
   Integer x;
   try { x.GenerateRandom(prng, params); }
   catch (RandomNumberNotFound&) { x = -1; }

Definition at line 508 of file integer.h.

◆ GenerateRandomNoThrow()

bool Integer::GenerateRandomNoThrow ( RandomNumberGenerator rng,
const NameValuePairs params = g_nullNameValuePairs 
)

Generate a random number.

Parameters
rngRandomNumberGenerator used to generate material
paramsadditional parameters that cannot be passed directly to the function
Returns
true if a random number was generated, false otherwise

GenerateRandomNoThrow attempts to generate a random number according to the parameters specified in params. The function does not throw RandomNumberNotFound.

The example below generates a prime number using NameValuePairs that Integer class recognizes. The names are not provided in argnames.h.

   AutoSeededRandomPool prng;
   AlgorithmParameters params = MakeParameters("BitLength", 2048)
                                              ("RandomNumberType", Integer::PRIME);
   Integer x;
   if (x.GenerateRandomNoThrow(prng, params) == false)
       throw std::runtime_error("Failed to generate prime number");

Definition at line 3584 of file integer.cpp.

◆ GetBit()

bool Integer::GetBit ( size_t  i) const

Provides the i-th bit of the Integer.

Returns
the i-th bit, i=0 being the least significant bit

Definition at line 3111 of file integer.cpp.

◆ GetBits()

lword Integer::GetBits ( size_t  i,
size_t  n 
) const

Provides the low order bits of the Integer.

Returns
n lowest bits of *this >> i

Definition at line 3150 of file integer.cpp.

◆ GetByte()

byte Integer::GetByte ( size_t  i) const

Provides the i-th byte of the Integer.

Returns
the i-th byte

Definition at line 3134 of file integer.cpp.

◆ InverseMod() [1/2]

Integer Integer::InverseMod ( const Integer n) const

Calculate multiplicative inverse.

Parameters
nreference to the modulus
Returns
an Integer *this % n.

InverseMod returns the multiplicative inverse of the Integer *this modulo the Integer n. If no Integer exists then Integer 0 is returned.

See also
a_times_b_mod_c() and a_exp_b_mod_c()

Definition at line 4473 of file integer.cpp.

◆ InverseMod() [2/2]

word Integer::InverseMod ( word  n) const

Calculate multiplicative inverse.

Parameters
nthe modulus
Returns
a word *this % n.

InverseMod returns the multiplicative inverse of the Integer *this modulo the word n. If no Integer exists then word 0 is returned.

See also
a_times_b_mod_c() and a_exp_b_mod_c()

Definition at line 4512 of file integer.cpp.

◆ InverseModNext()

Integer Integer::InverseModNext ( const Integer n) const
protected

Definition at line 4488 of file integer.cpp.

◆ IsConvertableToLong()

bool Integer::IsConvertableToLong ( ) const

Determines if the Integer is convertable to Long.

Returns
true if *this can be represented as a signed long
See also
ConvertToLong()

Definition at line 3006 of file integer.cpp.

◆ IsEven()

bool Integer::IsEven ( ) const
inline

Determines if the Integer is even parity.

Returns
true if the Integer is even, false otherwise

Definition at line 353 of file integer.h.

◆ IsNegative()

bool Integer::IsNegative ( ) const
inline

Determines if the Integer is negative.

Returns
true if the Integer is negative, false otherwise

Definition at line 341 of file integer.h.

◆ IsOdd()

bool Integer::IsOdd ( ) const
inline

Determines if the Integer is odd parity.

Returns
true if the Integer is odd, false otherwise

Definition at line 356 of file integer.h.

◆ IsPositive()

bool Integer::IsPositive ( ) const
inline

Determines if the Integer is positive.

Returns
true if the Integer is positive, false otherwise

Definition at line 347 of file integer.h.

◆ IsSquare()

bool Integer::IsSquare ( ) const

Determine whether this integer is a perfect square.

Definition at line 4433 of file integer.cpp.

◆ IsUnit()

bool Integer::IsUnit ( ) const

Determine if 1 or -1.

Returns
true if this integer is 1 or -1, false otherwise

Definition at line 4439 of file integer.cpp.

◆ IsZero()

bool Integer::IsZero ( ) const
inline

Determines if the Integer is 0.

Returns
true if the Integer is 0, false otherwise

Definition at line 335 of file integer.h.

◆ MinEncodedSize()

size_t Integer::MinEncodedSize ( Signedness  sign = UNSIGNED) const

Minimum number of bytes to encode this integer.

Parameters
signenumeration indicating Signedness
Note
The MinEncodedSize() of 0 is 1.

Definition at line 3412 of file integer.cpp.

◆ Minus()

Integer Integer::Minus ( const Integer b) const

Subtraction.

Definition at line 4032 of file integer.cpp.

◆ Modulo() [1/2]

Integer Integer::Modulo ( const Integer b) const

Remainder.

See also
a_times_b_mod_c() and a_exp_b_mod_c()

Definition at line 4293 of file integer.cpp.

◆ Modulo() [2/2]

word Integer::Modulo ( word  b) const

Remainder.

See also
a_times_b_mod_c() and a_exp_b_mod_c()

Definition at line 4344 of file integer.cpp.

◆ MultiplicativeInverse()

Integer Integer::MultiplicativeInverse ( ) const

Calculate multiplicative inverse.

Returns
MultiplicativeInverse inverse if 1 or -1, otherwise return 0.

Definition at line 4444 of file integer.cpp.

◆ Negate()

void Integer::Negate ( )

Reverse the Sign of the Integer.

Definition at line 4381 of file integer.cpp.

◆ NotNegative()

bool Integer::NotNegative ( ) const
inline

Determines if the Integer is non-negative.

Returns
true if the Integer is non-negative, false otherwise

Definition at line 344 of file integer.h.

◆ NotPositive()

bool Integer::NotPositive ( ) const
inline

Determines if the Integer is non-positive.

Returns
true if the Integer is non-positive, false otherwise

Definition at line 350 of file integer.h.

◆ NotZero()

bool Integer::NotZero ( ) const
inline

Determines if the Integer is non-0.

Returns
true if the Integer is non-0, false otherwise

Definition at line 338 of file integer.h.

◆ One()

const Integer & Integer::One ( )
static

Integer representing 1.

Returns
an Integer representing 1

One() avoids calling constructors for frequently used integers

Definition at line 4920 of file integer.cpp.

◆ OpenPGPDecode() [1/2]

void Integer::OpenPGPDecode ( BufferedTransformation bt)

Decode from OpenPGP format.

Parameters
btBufferedTransformation object

Definition at line 3512 of file integer.cpp.

◆ OpenPGPDecode() [2/2]

void Integer::OpenPGPDecode ( const byte input,
size_t  inputLen 
)

Decode from OpenPGP format.

Parameters
inputbig-endian byte array
inputLenlength of the byte array

Definition at line 3505 of file integer.cpp.

◆ OpenPGPEncode() [1/2]

size_t Integer::OpenPGPEncode ( BufferedTransformation bt) const

Encode absolute value in OpenPGP format.

Parameters
btBufferedTransformation object
Returns
length of the output

OpenPGPEncode places result into a BufferedTransformation object and returns the number of bytes used for the encoding

Definition at line 3496 of file integer.cpp.

◆ OpenPGPEncode() [2/2]

size_t Integer::OpenPGPEncode ( byte output,
size_t  bufferSize 
) const

Encode absolute value in OpenPGP format.

Parameters
outputbig-endian byte array
bufferSizelength of the byte array
Returns
length of the output

OpenPGPEncode places result into the buffer and returns the number of bytes used for the encoding

Definition at line 3488 of file integer.cpp.

◆ operator!()

bool Integer::operator! ( ) const

Negation.

Definition at line 3094 of file integer.cpp.

◆ operator%=() [1/2]

Integer & Integer::operator%= ( const Integer t)
inline

Remainder Assignment.

Parameters
tthe other Integer
Returns
the result of *this % t
See also
a_times_b_mod_c() and a_exp_b_mod_c()

Definition at line 386 of file integer.h.

◆ operator%=() [2/2]

Integer & Integer::operator%= ( word  t)
inline

Remainder Assignment.

Parameters
tthe other word
Returns
the result of *this % t
See also
a_times_b_mod_c() and a_exp_b_mod_c()

Definition at line 395 of file integer.h.

◆ operator&=()

Integer & Integer::operator&= ( const Integer t)

Bitwise AND Assignment.

Parameters
tthe other Integer
Returns
the result of *this & t

operator&=() performs a bitwise AND on *this. Missing bits are truncated at the most significant bit positions, so the result is as small as the smaller of the operands.

Internally, Crypto++ uses a sign-magnitude representation. The library does not attempt to interpret bits, and the result is always POSITIVE. If needed, the integer should be converted to a 2's compliment representation before performing the operation.

Since
Crypto++ 6.0

Definition at line 4104 of file integer.cpp.

◆ operator*=()

Integer & Integer::operator*= ( const Integer t)
inline

Multiplication Assignment.

Parameters
tthe other Integer
Returns
the result of *this * t
See also
a_times_b_mod_c() and a_exp_b_mod_c()

Definition at line 377 of file integer.h.

◆ operator+()

Integer Integer::operator+ ( ) const
inline

Addition.

Definition at line 540 of file integer.h.

◆ operator++() [1/2]

Integer & Integer::operator++ ( )

Pre-increment.

Definition at line 3759 of file integer.cpp.

◆ operator++() [2/2]

Integer Integer::operator++ ( int  )
inline

Post-increment.

Definition at line 548 of file integer.h.

◆ operator+=()

Integer & Integer::operator+= ( const Integer t)

Addition Assignment.

Parameters
tthe other Integer
Returns
the result of *this + t

Definition at line 4009 of file integer.cpp.

◆ operator-()

Integer Integer::operator- ( ) const

Subtraction.

Definition at line 3159 of file integer.cpp.

◆ operator--() [1/2]

Integer & Integer::operator-- ( )

Pre-decrement.

Definition at line 3780 of file integer.cpp.

◆ operator--() [2/2]

Integer Integer::operator-- ( int  )
inline

Post-decrement.

Definition at line 550 of file integer.h.

◆ operator-=()

Integer & Integer::operator-= ( const Integer t)

Subtraction Assignment.

Parameters
tthe other Integer
Returns
the result of *this - t

Definition at line 4055 of file integer.cpp.

◆ operator/=() [1/2]

Integer & Integer::operator/= ( const Integer t)
inline

Division Assignment.

Parameters
tthe other Integer
Returns
the result of *this / t

Definition at line 381 of file integer.h.

◆ operator/=() [2/2]

Integer & Integer::operator/= ( word  t)
inline

Division Assignment.

Parameters
tthe other word
Returns
the result of *this / t

Definition at line 390 of file integer.h.

◆ operator<<()

Integer Integer::operator<< ( size_t  n) const
inline

Left-shift.

Definition at line 622 of file integer.h.

◆ operator<<=()

Integer & Integer::operator<<= ( size_t  n)

Left-shift Assignment.

Parameters
nnumber of bits to shift
Returns
reference to this Integer

Definition at line 4078 of file integer.cpp.

◆ operator=()

Integer & Integer::operator= ( const Integer t)

Assignment.

Parameters
tthe other Integer
Returns
the result of assignment

Definition at line 3099 of file integer.cpp.

◆ operator>>()

Integer Integer::operator>> ( size_t  n) const
inline

Right-shift.

Definition at line 620 of file integer.h.

◆ operator>>=()

Integer & Integer::operator>>= ( size_t  n)

Right-shift Assignment.

Parameters
nnumber of bits to shift
Returns
reference to this Integer

Definition at line 4090 of file integer.cpp.

◆ operator^=()

Integer & Integer::operator^= ( const Integer t)

Bitwise XOR Assignment.

Parameters
tthe other Integer
Returns
the result of *this ^ t

operator^=() performs a bitwise XOR on *this. Missing bits are shifted in at the most significant bit positions, so the result is as large as the larger of the operands.

Internally, Crypto++ uses a sign-magnitude representation. The library does not attempt to interpret bits, and the result is always POSITIVE. If needed, the integer should be converted to a 2's compliment representation before performing the operation.

Since
Crypto++ 6.0

Definition at line 4137 of file integer.cpp.

◆ operator|=()

Integer & Integer::operator|= ( const Integer t)

Bitwise OR Assignment.

Parameters
tthe second Integer
Returns
the result of *this | t

operator|=() performs a bitwise OR on *this. Missing bits are shifted in at the most significant bit positions, so the result is as large as the larger of the operands.

Internally, Crypto++ uses a sign-magnitude representation. The library does not attempt to interpret bits, and the result is always POSITIVE. If needed, the integer should be converted to a 2's compliment representation before performing the operation.

Since
Crypto++ 6.0

Definition at line 4116 of file integer.cpp.

◆ Or()

Integer Integer::Or ( const Integer t) const

Bitwise OR.

Parameters
tthe other Integer
Returns
the result of *this | t

Or() performs a bitwise OR on the operands. Missing bits are shifted in at the most significant bit positions, so the result is as large as the larger of the operands.

Internally, Crypto++ uses a sign-magnitude representation. The library does not attempt to interpret bits, and the result is always POSITIVE. If needed, the integer should be converted to a 2's compliment representation before performing the operation.

Since
Crypto++ 6.0

Definition at line 3838 of file integer.cpp.

◆ Plus()

Integer Integer::Plus ( const Integer b) const

Addition.

Definition at line 3986 of file integer.cpp.

◆ Power2()

Integer Integer::Power2 ( size_t  e)
static

Exponentiates to a power of 2.

Returns
the Integer 2e
See also
a_times_b_mod_c() and a_exp_b_mod_c()

Definition at line 3087 of file integer.cpp.

◆ Randomize() [1/3]

void Integer::Randomize ( RandomNumberGenerator rng,
const Integer min,
const Integer max 
)

Set this Integer to random integer.

Parameters
rngRandomNumberGenerator used to generate material
minthe minimum value
maxthe maximum value

The random integer created is uniformly distributed over [min, max].

Definition at line 3530 of file integer.cpp.

◆ Randomize() [2/3]

bool Integer::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.

Parameters
rngRandomNumberGenerator used to generate material
minthe minimum value
maxthe maximum value
rnTypeRandomNumberType to specify the type
equivthe equivalence class based on the parameter mod
modthe modulus used to reduce the equivalence class
Exceptions
RandomNumberNotFoundif the set is empty.

Ideally, the random integer created should be uniformly distributed over {x | min <= x <= max and x is of rnType and x % mod == equiv}. However the actual distribution may not be uniform because sequential search is used to find an appropriate number from a random starting point.

May return (with very small probability) a pseudoprime when a prime is requested and max > lastSmallPrime*lastSmallPrime. lastSmallPrime is declared in nbtheory.h.

Definition at line 3547 of file integer.cpp.

◆ Randomize() [3/3]

void Integer::Randomize ( RandomNumberGenerator rng,
size_t  bitCount 
)

Set this Integer to random integer.

Parameters
rngRandomNumberGenerator used to generate material
bitCountthe number of bits in the resulting integer

The random integer created is uniformly distributed over [0, 2bitCount].

Definition at line 3520 of file integer.cpp.

◆ SetBit()

void Integer::SetBit ( size_t  n,
bool  value = 1 
)

Set the n-th bit to value.

0-based numbering.

Definition at line 3120 of file integer.cpp.

◆ SetByte()

void Integer::SetByte ( size_t  n,
byte  value 
)

Set the n-th byte to value.

0-based numbering.

Definition at line 3143 of file integer.cpp.

◆ SetNegative()

void Integer::SetNegative ( )
inline

Sets the Integer to negative.

Definition at line 529 of file integer.h.

◆ SetPositive()

void Integer::SetPositive ( )
inline

Sets the Integer to positive.

Definition at line 526 of file integer.h.

◆ Squared()

Integer Integer::Squared ( ) const
inline

Multiply this integer by itself.

See also
a_times_b_mod_c() and a_exp_b_mod_c()

Definition at line 633 of file integer.h.

◆ SquareRoot()

Integer Integer::SquareRoot ( ) const

Extract square root.

if negative return 0, else return floor of square root

Definition at line 4415 of file integer.cpp.

◆ swap()

void Integer::swap ( Integer a)

Swaps this Integer with another Integer.

Definition at line 3173 of file integer.cpp.

◆ Times()

Integer Integer::Times ( const Integer b) const

Multiplication.

See also
a_times_b_mod_c() and a_exp_b_mod_c()

Definition at line 4182 of file integer.cpp.

◆ Two()

const Integer & Integer::Two ( )
static

Integer representing 2.

Returns
an Integer representing 2

Two() avoids calling constructors for frequently used integers

Definition at line 4932 of file integer.cpp.

◆ WordCount()

unsigned int Integer::WordCount ( ) const

Determines the number of words required to represent the Integer.

Returns
number of significant words

WordCount is calculated as ceiling(ByteCount()/sizeof(word)).

Definition at line 3350 of file integer.cpp.

◆ Xor()

Integer Integer::Xor ( const Integer t) const

Bitwise XOR.

Parameters
tthe other Integer
Returns
the result of *this ^ t

Xor() performs a bitwise XOR on the operands. Missing bits are shifted in at the most significant bit positions, so the result is as large as the larger of the operands.

Internally, Crypto++ uses a sign-magnitude representation. The library does not attempt to interpret bits, and the result is always POSITIVE. If needed, the integer should be converted to a 2's compliment representation before performing the operation.

Since
Crypto++ 6.0

Definition at line 3876 of file integer.cpp.

◆ Zero()

const Integer & Integer::Zero ( )
static

Integer representing 0.

Returns
an Integer representing 0

Zero() avoids calling constructors for frequently used integers

Definition at line 4908 of file integer.cpp.

Friends And Related Symbol Documentation

◆ a_exp_b_mod_c

CRYPTOPP_DLL friend Integer CRYPTOPP_API a_exp_b_mod_c ( const Integer x,
const Integer e,
const Integer m 
)
friend

Modular exponentiation.

Parameters
xreference to the base
ereference to the exponent
mreference to the modulus
Returns
an Integer (a ^ b) % m.

Definition at line 4458 of file integer.cpp.

◆ a_times_b_mod_c

CRYPTOPP_DLL friend Integer CRYPTOPP_API a_times_b_mod_c ( const Integer x,
const Integer y,
const Integer m 
)
friend

Modular multiplication.

Parameters
xreference to the first term
yreference to the second term
mreference to the modulus
Returns
an Integer (a * b) % m.

Definition at line 4449 of file integer.cpp.

◆ HalfMontgomeryRepresentation

friend class HalfMontgomeryRepresentation
friend

Definition at line 747 of file integer.h.

◆ ModularArithmetic

friend class ModularArithmetic
friend

Definition at line 745 of file integer.h.

◆ MontgomeryRepresentation

friend class MontgomeryRepresentation
friend

Definition at line 746 of file integer.h.

◆ operator<<

CRYPTOPP_DLL std::ostream &CRYPTOPP_API operator<< ( std::ostream &  out,
const Integer a 
)
friend

Insertion operator.

Parameters
outreference to a std::ostream
aa constant reference to an Integer
Returns
reference to a std::ostream reference

The output integer responds to std::hex, std::oct, std::hex, std::upper and std::lower. The output includes the suffix h (for hex), . (dot, for dec) and o (for octal). There is currently no way to suppress the suffix.

If you want to print an Integer without the suffix or using an arbitrary base, then use IntToString<Integer>().

See also
IntToString<Integer>

Definition at line 3712 of file integer.cpp.

◆ operator>>

CRYPTOPP_DLL std::istream &CRYPTOPP_API operator>> ( std::istream &  in,
Integer a 
)
friend

Extraction operator.

Parameters
inreference to a std::istream
areference to an Integer
Returns
reference to a std::istream reference

Definition at line 3677 of file integer.cpp.

◆ PositiveAdd

void PositiveAdd ( Integer sum,
const Integer a,
const Integer b 
)
friend

Definition at line 3912 of file integer.cpp.

◆ PositiveDivide

void PositiveDivide ( Integer remainder,
Integer quotient,
const Integer dividend,
const Integer divisor 
)
friend

Definition at line 4211 of file integer.cpp.

◆ PositiveMultiply

void PositiveMultiply ( Integer product,
const Integer a,
const Integer b 
)
friend

Definition at line 4162 of file integer.cpp.

◆ PositiveSubtract

void PositiveSubtract ( Integer diff,
const Integer a,
const Integer b 
)
friend

Definition at line 3941 of file integer.cpp.


The documentation for this class was generated from the following files: