Next: rotor
Prev: md5
Up: CRYPTOGRAPHIC EXTENSIONS
Top: Top
7.2. Built-in module mpz
This module implements the interface to part of the GNU MP library.
This library contains arbitrary precision integer and rational number
arithmetic routines. Only the interfaces to the integer
(`mpz_...') routines are provided. If not stated
otherwise, the description in the GNU MP documentation can be applied.
In general, mpz-numbers can be used just like other standard
Python numbers, e.g. you can use the built-in operators like +
,
*
, etc., as well as the standard built-in functions like
abs
, int
, ..., divmod
, pow
.
Please note: the bitwise-xor operation has been implemented as
a bunch of ands, inverts and ors, because the library
lacks an mpz_xor
function, and I didn't need one.
You create an mpz-number, by calling the function called mpz
(see
below for an excact description). An mpz-number is printed like this:
mpz(value)
.
- mpz (value) -- function of module mpz
-
Create a new mpz-number. value can be an integer, a long,
another mpz-number, or even a string. If it is a string, it is
interpreted as an array of radix-256 digits, least significant digit
first, resulting in a positive number. See also the
binary
method, described below.
A number of extra functions are defined in this module. Non
mpz-arguments are converted to mpz-values first, and the functions
return mpz-numbers.
- powm (base, exponent, modulus) -- function of module mpz
-
Return
pow(base, exponent) % modulus
. If
exponent == 0
, return mpz(1)
. In contrast to the
C-library function, this version can handle negative exponents.
- gcd (op1, op2) -- function of module mpz
-
Return the greatest common divisor of op1 and op2.
- gcdext (a, b) -- function of module mpz
-
Return a tuple
(g, s, t)
, such that
a*s + b*t == g == gcd(a, b)
.
- sqrt (op) -- function of module mpz
-
Return the square root of op. The result is rounded towards zero.
- sqrtrem (op) -- function of module mpz
-
Return a tuple
(root, remainder)
, such that
root*root + remainder == op
.
- divm (numerator, denominator, modulus) -- function of module mpz
-
Returns a number q. such that
q * denominator % modulus == numerator
.
One could also implement this function in python, using gcdext
.
An mpz-number has one method:
- binary () -- Method on mpz
-
Convert this mpz-number to a binary string, where the number has been
stored as an array of radix-256 digits, least significant digit first.
The mpz-number must have a value greater than- or equal to zero,
otherwise a ValueError
-exception will be raised.