home *** CD-ROM | disk | FTP | other *** search
- <TITLE>mpz -- Python library reference</TITLE>
- Next: <A HREF="../r/rotor" TYPE="Next">rotor</A>
- Prev: <A HREF="../m/md5" TYPE="Prev">md5</A>
- Up: <A HREF="../c/cryptographic_services" TYPE="Up">Cryptographic Services</A>
- Top: <A HREF="../t/top" TYPE="Top">Top</A>
- <H1>12.2. Built-in Module <CODE>mpz</CODE></H1>
- This is an optional module. It is only available when Python is
- configured to include it, which requires that the GNU MP software is
- installed.
- <P>
- This module implements the interface to part of the GNU MP library,
- which defines arbitrary precision integer and rational number
- arithmetic routines. Only the interfaces to the <I>integer</I>
- (`<SAMP>mpz_<R>...</R></SAMP>') routines are provided. If not stated
- otherwise, the description in the GNU MP documentation can be applied.
- <P>
- In general, <DFN>mpz</DFN>-numbers can be used just like other standard
- Python numbers, e.g. you can use the built-in operators like <CODE>+</CODE>,
- <CODE>*</CODE>, etc., as well as the standard built-in functions like
- <CODE>abs</CODE>, <CODE>int</CODE>, ..., <CODE>divmod</CODE>, <CODE>pow</CODE>.
- <B>Please note:</B> the <I>bitwise-xor</I> operation has been implemented as
- a bunch of <I>and</I>s, <I>invert</I>s and <I>or</I>s, because the library
- lacks an <CODE>mpz_xor</CODE> function, and I didn't need one.
- <P>
- You create an mpz-number by calling the function called <CODE>mpz</CODE> (see
- below for an exact description). An mpz-number is printed like this:
- <CODE>mpz(<VAR>value</VAR>)</CODE>.
- <P>
- <DL><DT><B>mpz</B> (<VAR>value</VAR>) -- function of module mpz<DD>
- Create a new mpz-number. <VAR>value</VAR> 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 <CODE>binary</CODE>
- method, described below.
- </DL>
- A number of <I>extra</I> functions are defined in this module. Non
- mpz-arguments are converted to mpz-values first, and the functions
- return mpz-numbers.
- <P>
- <DL><DT><B>powm</B> (<VAR>base</VAR>, <VAR>exponent</VAR>, <VAR>modulus</VAR>) -- function of module mpz<DD>
- Return <CODE>pow(<VAR>base</VAR>, <VAR>exponent</VAR>) % <VAR>modulus</VAR></CODE>. If
- <CODE><VAR>exponent</VAR> == 0</CODE>, return <CODE>mpz(1)</CODE>. In contrast to the
- C-library function, this version can handle negative exponents.
- </DL>
- <DL><DT><B>gcd</B> (<VAR>op1</VAR>, <VAR>op2</VAR>) -- function of module mpz<DD>
- Return the greatest common divisor of <VAR>op1</VAR> and <VAR>op2</VAR>.
- </DL>
- <DL><DT><B>gcdext</B> (<VAR>a</VAR>, <VAR>b</VAR>) -- function of module mpz<DD>
- Return a tuple <CODE>(<VAR>g</VAR>, <VAR>s</VAR>, <VAR>t</VAR>)</CODE>, such that
- <CODE><VAR>a</VAR>*<VAR>s</VAR> + <VAR>b</VAR>*<VAR>t</VAR> == <VAR>g</VAR> == gcd(<VAR>a</VAR>, <VAR>b</VAR>)</CODE>.
- </DL>
- <DL><DT><B>sqrt</B> (<VAR>op</VAR>) -- function of module mpz<DD>
- Return the square root of <VAR>op</VAR>. The result is rounded towards zero.
- </DL>
- <DL><DT><B>sqrtrem</B> (<VAR>op</VAR>) -- function of module mpz<DD>
- Return a tuple <CODE>(<VAR>root</VAR>, <VAR>remainder</VAR>)</CODE>, such that
- <CODE><VAR>root</VAR>*<VAR>root</VAR> + <VAR>remainder</VAR> == <VAR>op</VAR></CODE>.
- </DL>
- <DL><DT><B>divm</B> (<VAR>numerator</VAR>, <VAR>denominator</VAR>, <VAR>modulus</VAR>) -- function of module mpz<DD>
- Returns a number <VAR>q</VAR>. such that
- <CODE><VAR>q</VAR> * <VAR>denominator</VAR> % <VAR>modulus</VAR> == <VAR>numerator</VAR></CODE>.
- One could also implement this function in Python, using <CODE>gcdext</CODE>.
- </DL>
- An mpz-number has one method:
- <P>
- <DL><DT><B>binary</B> () -- Method on mpz<DD>
- 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.
- <P>
- The mpz-number must have a value greater than or equal to zero,
- otherwise a <CODE>ValueError</CODE>-exception will be raised.
- </DL>
-