Package Crypto :: Package Util :: Module number
[hide private]
[frames] | no frames]

Module number

Functions [hide private]
 
size(N)
size(N:long) : int Returns the size of the number N in bits.
 
getRandomNumber(N, randfunc=None)
Deprecated. Use getRandomInteger or getRandomNBitInteger instead.
 
getRandomInteger(N, randfunc=None)
getRandomInteger(N:int, randfunc:callable):long Return a random number with at most N bits.
 
getRandomRange(a, b, randfunc=None)
getRandomRange(a:int, b:int, randfunc:callable):long Return a random number n so that a <= n < b.
 
getRandomNBitInteger(N, randfunc=None)
getRandomInteger(N:int, randfunc:callable):long Return a random number with exactly N-bits, i.e. a random number between 2**(N-1) and (2**N)-1.
 
GCD(x, y)
GCD(x:long, y:long): long Return the GCD of x and y.
 
inverse(u, v)
inverse(u:long, v:long):long Return the inverse of u mod v.
 
getPrime(N, randfunc=None)
getPrime(N:int, randfunc:callable):long Return a random N-bit prime number.
 
_rabinMillerTest(n, rounds, randfunc=None)
_rabinMillerTest(n:long, rounds:int, randfunc:callable):int Tests if n is prime. Returns 0 when n is definitly composite. Returns 1 when n is probably prime. Returns 2 when n is definitly prime.
 
getStrongPrime(N, e=0, false_positive_prob=1e-06, randfunc=None)
getStrongPrime(N:int, e:int, false_positive_prob:float, randfunc:callable):long Return a random strong N-bit prime number. In this context p is a strong prime if p-1 and p+1 have at least one large prime factor. N should be a multiple of 128 and > 512.
 
isPrime(N, false_positive_prob=1e-06, randfunc=None)
isPrime(N:long, false_positive_prob:float, randfunc:callable):bool Return true if N is prime.
 
long_to_bytes(n, blocksize=0)
long_to_bytes(n:long, blocksize:int) : string Convert a long integer to a byte string.
 
bytes_to_long(s)
bytes_to_long(string) : long Convert a byte string to a long integer.
 
long2str(n, blocksize=0)
 
str2long(s)
 
_import_Random()
Variables [hide private]
  __revision__ = '$Id$'
  sieve_base = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, ...
  __package__ = 'Crypto.Util'
Function Details [hide private]

getRandomInteger(N, randfunc=None)

 

getRandomInteger(N:int, randfunc:callable):long Return a random number with at most N bits.

If randfunc is omitted, then Random.new().read is used.

This function is for internal use only and may be renamed or removed in the future.

getRandomRange(a, b, randfunc=None)

 

getRandomRange(a:int, b:int, randfunc:callable):long Return a random number n so that a <= n < b.

If randfunc is omitted, then Random.new().read is used.

This function is for internal use only and may be renamed or removed in the future.

getRandomNBitInteger(N, randfunc=None)

 

getRandomInteger(N:int, randfunc:callable):long Return a random number with exactly N-bits, i.e. a random number between 2**(N-1) and (2**N)-1.

If randfunc is omitted, then Random.new().read is used.

This function is for internal use only and may be renamed or removed in the future.

getPrime(N, randfunc=None)

 

getPrime(N:int, randfunc:callable):long Return a random N-bit prime number.

If randfunc is omitted, then Random.new().read is used.

_rabinMillerTest(n, rounds, randfunc=None)

 

_rabinMillerTest(n:long, rounds:int, randfunc:callable):int Tests if n is prime. Returns 0 when n is definitly composite. Returns 1 when n is probably prime. Returns 2 when n is definitly prime.

If randfunc is omitted, then Random.new().read is used.

This function is for internal use only and may be renamed or removed in the future.

getStrongPrime(N, e=0, false_positive_prob=1e-06, randfunc=None)

 

getStrongPrime(N:int, e:int, false_positive_prob:float, randfunc:callable):long Return a random strong N-bit prime number. In this context p is a strong prime if p-1 and p+1 have at least one large prime factor. N should be a multiple of 128 and > 512.

If e is provided the returned prime p-1 will be coprime to e and thus suitable for RSA where e is the public exponent.

The optional false_positive_prob is the statistical probability that true is returned even though it is not (pseudo-prime). It defaults to 1e-6 (less than 1:1000000). Note that the real probability of a false-positive is far less. This is just the mathematically provable limit.

randfunc should take a single int parameter and return that many random bytes as a string. If randfunc is omitted, then Random.new().read is used.

isPrime(N, false_positive_prob=1e-06, randfunc=None)

 

isPrime(N:long, false_positive_prob:float, randfunc:callable):bool Return true if N is prime.

The optional false_positive_prob is the statistical probability that true is returned even though it is not (pseudo-prime). It defaults to 1e-6 (less than 1:1000000). Note that the real probability of a false-positive is far less. This is just the mathematically provable limit.

If randfunc is omitted, then Random.new().read is used.

long_to_bytes(n, blocksize=0)

 

long_to_bytes(n:long, blocksize:int) : string Convert a long integer to a byte string.

If optional blocksize is given and greater than zero, pad the front of the byte string with binary zeros so that the length is a multiple of blocksize.

bytes_to_long(s)

 

bytes_to_long(string) : long Convert a byte string to a long integer.

This is (essentially) the inverse of long_to_bytes().


Variables Details [hide private]

sieve_base

Value:
(2,
 3,
 5,
 7,
 11,
 13,
 17,
 19,
...