Cryptodome :: Math :: _Numbers_gmp :: Integer :: Class Integer

Class Integer

object --+
         |
        Integer

A fast, arbitrary precision integer
Instance Methods
 
__init__(self, value)
Initialize the integer to the given value.
 
__int__(self)
 
__str__(self)
str(x)
 
__repr__(self)
repr(x)
 
to_bytes(self, block_size=0)
Convert the number into a byte string.
 
__eq__(self, term)
 
__ne__(self, term)
 
__lt__(self, term)
 
__le__(self, term)
 
__gt__(self, term)
 
__ge__(self, term)
 
__nonzero__(self)
 
is_negative(self)
 
__add__(self, term)
 
__sub__(self, term)
 
__mul__(self, term)
 
__floordiv__(self, divisor)
 
__mod__(self, divisor)
 
inplace_pow(self, exponent, modulus=None)
 
__pow__(self, exponent, modulus=None)
 
__abs__(self)
 
sqrt(self)
Return the largest Integer that does not exceed the square root
 
__iadd__(self, term)
 
__isub__(self, term)
 
__imul__(self, term)
 
__imod__(self, divisor)
 
__and__(self, term)
 
__or__(self, term)
 
__rshift__(self, pos)
 
__irshift__(self, pos)
 
__lshift__(self, pos)
 
__ilshift__(self, pos)
 
get_bit(self, n)
Return True if the n-th bit is set to 1. Bit 0 is the least significant.
 
is_odd(self)
 
is_even(self)
 
size_in_bits(self)
Return the minimum number of bits that can encode the number.
 
size_in_bytes(self)
Return the minimum number of bytes that can encode the number.
 
is_perfect_square(self)
 
fail_if_divisible_by(self, small_prime)
Raise an exception if the small prime is a divisor.
 
multiply_accumulate(self, a, b)
Increment the number by the product of a and b.
 
set(self, source)
Set the Integer to have the given value
 
inplace_inverse(self, modulus)
Compute the inverse of this number in the ring of modulo integers.
 
inverse(self, modulus)
 
gcd(self, term)
Compute the greatest common denominator between this number and another term.
 
lcm(self, term)
Compute the least common multiplier between this number and another term.
 
__del__(self)

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __setattr__, __sizeof__, __subclasshook__

Static Methods
 
from_bytes(byte_string)
Convert a byte string into a number.
 
jacobi_symbol(a, n)
Compute the Jacobi symbol
 
random(**kwargs)
Generate a random natural integer of a certain size.
 
random_range(**kwargs)
Generate a random integer within a given internal.
Properties

Inherited from object: __class__

Method Details

__init__(self, value)
(Constructor)

 
Initialize the integer to the given value.
Overrides: object.__init__

__str__(self)
(Informal representation operator)

 
str(x)
Overrides: object.__str__
(inherited documentation)

__repr__(self)
(Representation operator)

 
repr(x)
Overrides: object.__repr__
(inherited documentation)

to_bytes(self, block_size=0)

 

Convert the number into a byte string.

This method encodes the number in network order and prepends as many zero bytes as required. It only works for non-negative values.

Parameters:
  • block_size (integer) - The exact size the output byte string must have. If zero, the string has the minimal length.
Returns:
A byte string.
Raises:
  • ValueError - If the value is negative or if block_size is provided and the length of the byte string would exceed it.

from_bytes(byte_string)
Static Method

 
Convert a byte string into a number.
Parameters:
  • byte_string (byte string) - The input number, encoded in network order. It can only be non-negative.
Returns:
The Integer object carrying the same value as the input.

inplace_inverse(self, modulus)

 

Compute the inverse of this number in the ring of modulo integers.

Raise an exception if no inverse exists.

random(**kwargs)
Static Method

 
Generate a random natural integer of a certain size.
Parameters:
  • exact_bits (positive integer) - The length in bits of the resulting random Integer number. The number is guaranteed to fulfil the relation:

    2^bits > result >= 2^(bits - 1)
  • max_bits (positive integer) - The maximum length in bits of the resulting random Integer number. The number is guaranteed to fulfil the relation:

    2^bits > result >=0
  • randfunc (callable) - A function that returns a random byte string. The length of the byte string is passed as parameter. Optional. If not provided (or None), randomness is read from the system RNG.
Returns:
a Integer object

random_range(**kwargs)
Static Method

 
Generate a random integer within a given internal.
Parameters:
  • min_inclusive (integer) - The lower end of the interval (inclusive).
  • max_inclusive (integer) - The higher end of the interval (inclusive).
  • max_exclusive (integer) - The higher end of the interval (exclusive).
  • randfunc (callable) - A function that returns a random byte string. The length of the byte string is passed as parameter. Optional. If not provided (or None), randomness is read from the system RNG.
Returns:
An Integer randomly taken in the given interval.