Integer Class Reference

Multiple precision integer with arithmetic operations. More...

Inheritance diagram for Integer:

## 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...

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

## ENCODE/DECODE

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

## ACCESSORS

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

## MANIPULATORS

Integer & | operator= (const Integer &t)
Integer & | operator+= (const Integer &t)
Integer & | operator-= (const Integer &t)
Integer & | operator*= (const Integer &t)
Integer & | operator/= (const Integer &t)
Integer & | operator%= (const Integer &t)
Integer & | operator/= (word t)
Integer & | operator%= (word t)
Integer & | operator<<= (size_t)
Integer & | operator>>= (size_t)
void | Randomize (RandomNumberGenerator &rng, size_t bitCount)
| Set this Integer to random integer. More...
void | Randomize (RandomNumberGenerator &rng, const Integer &min, const Integer &max)
| Set this Integer to random integer. More...
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. More...
bool | GenerateRandomNoThrow (RandomNumberGenerator &rng, const NameValuePairs &params=g_nullNameValuePairs)
void | GenerateRandom (RandomNumberGenerator &rng, const NameValuePairs &params=g_nullNameValuePairs)
void | SetBit (size_t n, bool value=1)
| Set the n-th bit to value. More...
void | SetByte (size_t n, byte value)
| Set the n-th byte to value. More...
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
Integer | operator+ () const
Integer | operator- () const
Integer & | operator++ ()
Integer & | operator-- ()
Integer | operator++ (int)
Integer | operator-- (int)

## BINARY OPERATORS

int | Compare (const Integer &a) const
| Perform signed comparison. More...
Integer | Plus (const Integer &b) const
Integer | Minus (const Integer &b) const
Integer | Times (const Integer &b) const
Integer | DividedBy (const Integer &b) const
Integer | Modulo (const Integer &b) const
Integer | DividedBy (word b) const
word | Modulo (word b) const
Integer | operator>> (size_t n) const
Integer | operator<< (size_t n) const

## OTHER ARITHMETIC FUNCTIONS

Integer | AbsoluteValue () const
Integer | Doubled () const
Integer | Squared () const
Integer | SquareRoot () const
| extract square root, if negative return 0, else return floor of square root
bool | IsSquare () const
| return whether this integer is a perfect square
bool | IsUnit () const
| is 1 or -1
Integer | MultiplicativeInverse () const
| return inverse if 1 or -1, otherwise return 0
Integer | InverseMod (const Integer &n) const
| calculate multiplicative inverse of *this mod n More...
word | InverseMod (word n) const
static void | Divide (Integer &r, Integer &q, const Integer &a, const Integer &d)
| calculate r and q such that (a == d*q + r) && (0 <= r < abs(d))
static void | Divide (word &r, Integer &q, const Integer &a, word d)
| use a faster division algorithm when divisor is short
static void | DivideByPowerOf2 (Integer &r, Integer &q, const Integer &a, unsigned int n)
| returns same result as Divide(r, q, a, Power2(n)), but faster
static Integer | Gcd (const Integer &a, const Integer &n)
| greatest common divisor

## INPUT/OUTPUT

std::istream & | operator>> (std::istream &in, Integer &a)
| Extraction operator. More...
std::ostream & | operator<< (std::ostream &out, const Integer &a)
| Insertion operator. More...

## Additional Inherited Members

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

## 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, and its is used to track the sign of the Integer.

Definition at line 45 of file integer.h.

## Member Enumeration Documentation

## Sign

enum Integer::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
  Signedness

Enumerator
POSITIVE | the value is positive or 0
NEGATIVE | the value is negative

Definition at line 69 of file integer.h.

## Signedness

enum Integer::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 79 of file integer.h.

## RandomNumberType

enum Integer::RandomNumberType

Properties of a random integer.

Enumerator
ANY | a number with no special properties
PRIME | a number which is probabilistically prime

Definition at line 87 of file integer.h.

## Constructor & Destructor Documentation

## Integer() [1/9]

Integer::Integer | ( | Sign | sign,
|  | lword | value
| ) |  |

Convert from lword.

Parameters

  sign | enumeration indicating Sign
  value | the long word

Definition at line 2881 of file integer.cpp.

## Integer() [2/9]

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

Convert from two words.

Parameters

  sign | enumeration indicating Sign
  highWord | the high word
  lowWord | the low word

Definition at line 2902 of file integer.cpp.

## Integer() [3/9]

Integer::Integer | ( | const char * | str | ) |

explicit

Convert from a C-string.

Parameters

  str | C-string value
  order | byte order

`str` can be in base 2, 8, 10, or 16. Base is determined by a case insensitive suffix of 'h', 'o', or 'b'. 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 3251 of file integer.cpp.

## Integer() [4/9]

Integer::Integer | ( | const wchar_t * | str | ) |

explicit

Convert from a wide C-string.

Parameters

  str | wide C-string value
  order | byte order

`str` can be in base 2, 8, 10, or 16. Base is determined by a case insensitive suffix of 'h', 'o', or 'b'. 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 3263 of file integer.cpp.

## Integer() [5/9]

Integer::Integer | ( | const byte * | encodedInteger,
|  | size_t | byteCount,
|  | Signedness | sign = `UNSIGNED`
| ) |  |

Convert from a big-endian byte array.

Parameters

  encodedInteger | big-endian byte array
  byteCount | length of the byte array
  sign | enumeration indicating Signedness
  order | byte order

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

Definition at line 2954 of file integer.cpp.

## Integer() [6/9]

Integer::Integer | ( | BufferedTransformation & | bt,
|  | size_t | byteCount,
|  | Signedness | sign = `UNSIGNED`
| ) |  |

Convert from a big-endian array.

Parameters

  bt | BufferedTransformation object with big-endian byte array
  byteCount | length of the byte array
  sign | enumeration indicating Signedness
  order | byte order

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

Definition at line 2932 of file integer.cpp.

## Integer() [7/9]

Integer::Integer | ( | BufferedTransformation & | bt | ) |

explicit

Convert from a BER encoded byte array.

Parameters

  bt | BufferedTransformation object with BER encoded byte array

Definition at line 2979 of file integer.cpp.

## Integer() [8/9]

Integer::Integer | ( | RandomNumberGenerator & | rng,
|  | size_t | bitCount
| ) |  |

Create a random integer.

Parameters

  rng | RandomNumberGenerator used to generate material
  bitCount | the number of bits in the resulting integer

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

Definition at line 2984 of file integer.cpp.

## Integer() [9/9]

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

  rng | RandomNumberGenerator used to generate material
  min | the minimum value
  max | the maximum value
  rnType | RandomNumberType to specify the type
  equiv | the equivalence class based on the parameter `mod`
  mod | the modulus used to reduce the equivalence class

Exceptions

  RandomNumberNotFound | if 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 2989 of file integer.cpp.

## Member Function Documentation

## 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 3011 of file integer.cpp.

## 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 3016 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 3021 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 2995 of file integer.cpp.

## MinEncodedSize()

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

The minimum number of bytes to encode this integer.

Parameters

  sign | enumeration indicating Signedness

Note
  The MinEncodedSize() of 0 is 1.

Definition at line 3338 of file integer.cpp.

## Encode() [1/2]

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

Encode in big-endian format.

Parameters

  output | big-endian byte array
  outputLen | length of the byte array
  sign | enumeration 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 3350 of file integer.cpp.

## Encode() [2/2]

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

Encode in big-endian format.

Parameters

  bt | BufferedTransformation object
  outputLen | length of the encoding
  sign | enumeration 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 3357 of file integer.cpp.

## DEREncode()

void Integer::DEREncode | ( | BufferedTransformation & | bt | ) | const

virtual

Encode in DER format.

Parameters

  bt | BufferedTransformation object

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

Implements ASN1Object.

Definition at line 3372 of file integer.cpp.

## DEREncodeAsOctetString()

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

encode absolute value as big-endian octet string

Parameters

  bt | BufferedTransformation object
  length | the number of mytes to decode

Definition at line 3394 of file integer.cpp.

## OpenPGPEncode() [1/2]

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

Encode absolute value in OpenPGP format.

Parameters

  output | big-endian byte array
  bufferSize | length of the byte array

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 3410 of file integer.cpp.

## OpenPGPEncode() [2/2]

size_t Integer::OpenPGPEncode | ( | BufferedTransformation & | bt | ) | const

Encode absolute value in OpenPGP format.

Parameters

  bt | BufferedTransformation 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 3416 of file integer.cpp.

## Decode() [1/2]

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

Decode from big-endian byte array.

Parameters

  input | big-endian byte array
  inputLen | length of the byte array
  sign | enumeration indicating Signedness

Definition at line 3298 of file integer.cpp.

## Decode() [2/2]

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

Decode nonnegative value from big-endian byte array.

Parameters

  bt | BufferedTransformation object
  inputLen | length of the byte array
  sign | enumeration indicating Signedness

Note
  `bt.MaxRetrievable() >= inputLen`.

Definition at line 3304 of file integer.cpp.

## BERDecode() [1/2]

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

Decode from BER format.

Parameters

  input | big-endian byte array
  inputLen | length of the byte array

Definition at line 3379 of file integer.cpp.

## BERDecode() [2/2]

void Integer::BERDecode | ( | BufferedTransformation & | bt | ) |

virtual

Decode from BER format.

Parameters

  bt | BufferedTransformation object

Implements ASN1Object.

Definition at line 3385 of file integer.cpp.

## BERDecodeAsOctetString()

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

Decode nonnegative value from big-endian octet string.

Parameters

  bt | BufferedTransformation object
  length | length of the byte array

Definition at line 3401 of file integer.cpp.

## OpenPGPDecode() [1/2]

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

Decode from OpenPGP format.

Parameters

  input | big-endian byte array
  inputLen | length of the byte array

Definition at line 3425 of file integer.cpp.

## OpenPGPDecode() [2/2]

void Integer::OpenPGPDecode | ( | BufferedTransformation & | bt | ) |

Decode from OpenPGP format.

Parameters

  bt | BufferedTransformation object

Definition at line 3431 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 2909 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 2923 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 = floor(log2(abs(*this))) + 1

Definition at line 3289 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 = ceiling(BitCount()/8)

Definition at line 3280 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 = ceiling(ByteCount()/sizeof(word))

Definition at line 3275 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 3043 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 3065 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 3080 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 330 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 333 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 336 of file integer.h.

## 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 339 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 342 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 345 of file integer.h.

## 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 348 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 351 of file integer.h.

## operator*=()

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

inline

See also
  a_times_b_mod_c() and a_exp_b_mod_c()

Definition at line 365 of file integer.h.

## operator%=() [1/2]

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

inline

See also
  a_times_b_mod_c() and a_exp_b_mod_c()

Definition at line 370 of file integer.h.

## operator%=() [2/2]

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

inline

See also
  a_times_b_mod_c() and a_exp_b_mod_c()

Definition at line 375 of file integer.h.

## Randomize() [1/3]

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

Set this Integer to random integer.

Parameters

  rng | RandomNumberGenerator used to generate material
  bitCount | the number of bits in the resulting integer

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

Definition at line 3439 of file integer.cpp.

## Randomize() [2/3]

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

Set this Integer to random integer.

Parameters

  rng | RandomNumberGenerator used to generate material
  min | the minimum value
  max | the maximum value

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

Definition at line 3449 of file integer.cpp.

## Randomize() [3/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

  rng | RandomNumberGenerator used to generate material
  min | the minimum value
  max | the maximum value
  rnType | RandomNumberType to specify the type
  equiv | the equivalence class based on the parameter `mod`
  mod | the modulus used to reduce the equivalence class

Exceptions

  RandomNumberNotFound | if 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 3466 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 3051 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 3073 of file integer.cpp.

## Compare()

int Integer::Compare | ( | const Integer & | a | ) | const

Perform signed comparison.

Parameters

  a | the Integer to comapre

Return values

  -1 | if `*this < a`
  0 | if `*this = a`
  1 | if `*this > a`

Definition at line 4139 of file integer.cpp.

## Times()

Integer Integer::Times | ( | const Integer & | b | ) | const

See also
  a_times_b_mod_c() and a_exp_b_mod_c()

Definition at line 3925 of file integer.cpp.

## Modulo() [1/2]

Integer Integer::Modulo | ( | const Integer & | b | ) | const

See also
  a_times_b_mod_c() and a_exp_b_mod_c()

Definition at line 4036 of file integer.cpp.

## Modulo() [2/2]

word Integer::Modulo | ( | word | b | ) | const

See also
  a_times_b_mod_c() and a_exp_b_mod_c()

Definition at line 4087 of file integer.cpp.

## Squared()

Integer Integer::Squared | ( |  | ) | const

inline

See also
  a_times_b_mod_c() and a_exp_b_mod_c()

Definition at line 500 of file integer.h.

## InverseMod() [1/2]

Integer Integer::InverseMod | ( | const Integer & | n | ) | const

calculate multiplicative inverse of *this mod n

See also
  a_times_b_mod_c() and a_exp_b_mod_c()

Definition at line 4207 of file integer.cpp.

## InverseMod() [2/2]

word Integer::InverseMod | ( | word | n | ) | const

See also
  a_times_b_mod_c() and a_exp_b_mod_c()

Definition at line 4232 of file integer.cpp.

## Friends And Related Function Documentation

## operator>>

std::istream& operator>> | ( | std::istream & | in,
|  | Integer & | a
| ) |  |

friend

Extraction operator.

Parameters

  in | a reference to a std::istream
  a | a reference to an Integer

Returns
  a reference to a std::istream reference

Definition at line 3585 of file integer.cpp.

## operator<<

std::ostream& operator<< | ( | std::ostream & | out,
|  | const Integer & | a
| ) |  |

friend

Insertion operator.

Parameters

  out | a reference to a std::ostream
  a | a constant reference to an Integer

Returns
  a 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 supress 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 3610 of file integer.cpp.

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

- integer.h
- integer.cpp