This module implements arbitrary precision arithmetic, hash functions, and cryptographically secure random number generation.
base64.encode(str) base64.decode(str)
ascii85.encode(str) ascii85.decode(str)
hash.init(type)
hash.digest(str[, algorithm])
str
using specified algorithm (default is MD5).
hash.crc32(str) hash.md5(str) hash.sha0(str) hash.sha1(str) hash.sha2(str) hash.sha224(str) hash.sha256(str) hash.sha384(str) hash.sha512(str)
hash:update(str)
hash:finish()
Implements Hash_DRBG as specified in NIST SP 800-90A. Default generator is not initially seeded.
random.new([seed[, algorithm]])
random.generator
initialized from seed
string, using specified hash algorithm (default is SHA-256).
random.seed(str) random.generator:seed(str)
random.get(count[, input]) random.generator:get(count[, input])
count
random bytes, optionally using input
string to re-seed.
random.number([a[, b]]) random.generator:number([a[, b]])
[a, b)
(defaults to [0, 1)
).
random.integer(a[, b]) random.generator:integer(a[, b])
[a, b]
(defaults to [1, a]
).
random.choice(...) random.generator:choice(...)
random.shuffle(...) random.generator:shuffle(...)
random.sample(k, ...) random.generator:sample(k, ...)
k
distinct arguments.
random.uniform([a[, b]]) random.generator:uniform([a[, b]])
random.number
).
random.triangular([a[, b[, c]]]) random.generator:uniform([a[, b[, c]]])
a
to b
with mid-point at c
.
Defaults to (0, 1)
with mid-point at (a+b)/2
.
random.normalvariate([mu[, sigma]]) random.generator:normalvariate([mu[, sigma]])
mu
, sigma^2
) (defaults to N(0,1)).
random.lognormvariate([mu[, sigma]]) random.generator:lognormvariate([mu[, sigma]])
random.expovariate([lambda]) random.generator:expovariate([lambda])
random.default
Supports integers and fractional numbers with arbitrary precision (see bn.precision
). Supports negative/positive infinity and NaNs.
bn.precision([exp, len])
exp, len
).exp
specifies the maximum digits after 'decimal' point, len
is the maximum number of significant digits.
Numbers are base-2^32. Default is 0 for exp
(integers only) and infinity for len
. Be careful when specifying
large numbers for exp
as some operations such as division will use maximum available precision.
bn.number(x[, base])
bn.number
from a number or string, optionally using base
(2-36 or 256). Numbers support all
arithmetic operators.
bn.add(x, y) bn.sub(x, y) bn.mul(x, y) bn.div(x, y)
x
and y
. Division uses maximum available precision.
bn.divmod(x, y)
r, q
such that x = y * r + q
and 0 <= q < y
.
bn.mod(x, y)
q
as defined in bn.divmod
.
bn.pow(x, y)
x
to the power of y
. Non-integer powers are allowed, but are calculated by retrieving a square root
for every bit of precision, which can be very slow.
bn.shift(x, y)
x
by y
bits to the left (use negative values to shift right).
bn.sqrt(x)
x
.
bn.floor(x) bn.ceil(x) bn.round(x)
x
up/down/to the nearest integer.
bn.neg(x)
-x
.
bn.abs(x)
x
.
bn.compare(x, y)
x
and y
, returns -1, 0, 1 (less, equal, greater) or nil
if one of the numbers was NaN.
bn.gcd(x, y)
x
and y
(supports fractional numbers).
bn.powmod(x, y, z)
x ^ y
modulo z
, integers only.
bn.invmod(x, y)
x
modulo y
, integers only.
bn.band(x, y) bn.bor(x, y) bn.bxor(x, y)
x
and y
.
bn.tonumber(x)
x
as a Lua number.
bn.tostring(x[, base])
x
as string, optionally using base
(2-36 or 256).
bn.isneg(x) bn.iszero(x) bn.isone(x) bn.isint(x) bn.isodd(x) bn.isprime(x)
x
is negative, zero, one, an integer, odd, or prime.
bn.clear(x)
x
(setting it to 0).
bn.bits(x)
a, b
, where a
is the number of bits before the 'decimal' point, b
is the number of fractional bits.
bn.bit(x, i)
i
of x
, where 0 is the first bit before the 'decimal' point, and -1 is the first fractional bit
bn.random([gen][, bits])
bits
bits before the 'decimal' point (and maximum allowed fractional bits). Default for bits
is 0,
which generates a number in range [0,1)
.
bn.randrange([gen,] x)
[0,x)
, using maximum allowed fractional bits.
bn.randprime([gen,] bits)
bits
-bit prime number.