iLua Math Module

This module implements arbitrary precision arithmetic, hash functions, and cryptographically secure random number generation.

String conversion utilities

base64.encode(str)
base64.decode(str)
Converts binary strings to and from base64 encoding.
ascii85.encode(str)
ascii85.decode(str)
Converts binary strings to and from ascii85 encoding.

Hash library

Supports the following algorithms: CRC32, MD5, SHA-0, SHA-1, SHA-2, SHA-224, SHA-256, SHA-384, SHA-512, SHA-512/224, SHA-512/256, Keccak-224, Keccak-256, Keccak-384, Keccak-512.
hash.init(type)
Create a hash state for a given algorithm (specified by name).
hash.digest(str[, algorithm])
Returns the hash of 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)
Shorthands for the previous function.
hash:update(str)
Update hash state with new input.
hash:finish()
Finish and return message digest.

Random generator library

Implements Hash_DRBG as specified in NIST SP 800-90A. Default generator is not initially seeded.

random.new([seed[, algorithm]])
Create a new random.generator initialized from seed string, using specified hash algorithm (default is SHA-256).
random.seed(str)
random.generator:seed(str)
Seeds a generator with an input string.
random.get(count[, input])
random.generator:get(count[, input])
Generate count random bytes, optionally using input string to re-seed.
random.number([a[, b]])
random.generator:number([a[, b]])
Generate a random real number in range [a, b) (defaults to [0, 1)).
random.integer(a[, b])
random.generator:integer(a[, b])
Generate a random integer range [a, b] (defaults to [1, a]).
random.choice(...)
random.generator:choice(...)
Returns one of its argument at random.
random.shuffle(...)
random.generator:shuffle(...)
Returns its arguments in random order.
random.sample(k, ...)
random.generator:sample(k, ...)
Returns a random sample of k distinct arguments.
random.uniform([a[, b]])
random.generator:uniform([a[, b]])
Generate a random number with uniform distribution (equivalent to random.number).
random.triangular([a[, b[, c]]])
random.generator:uniform([a[, b[, c]]])
Generate a random number with triangular distribution from 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]])
Generate a random number with normal distribution N(mu, sigma^2) (defaults to N(0,1)).
random.lognormvariate([mu[, sigma]])
random.generator:lognormvariate([mu[, sigma]])
Generate a random number with log-normal distribution.
random.expovariate([lambda])
random.generator:expovariate([lambda])
Generate a random number with exponential distribution.
random.default
Default random number generator.

Arbitrary precision arithmetic library

Supports integers and fractional numbers with arbitrary precision (see bn.precision). Supports negative/positive infinity and NaNs.

bn.precision([exp, len])
Get or set precision (returns current values for 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])
Create a 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)
Return the sum, difference, product and ratio of x and y. Division uses maximum available precision.
bn.divmod(x, y)
Returns r, q such that x = y * r + q and 0 <= q < y.
bn.mod(x, y)
Returns q as defined in bn.divmod.
bn.pow(x, y)
Returns 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)
Binary shift x by y bits to the left (use negative values to shift right).
bn.sqrt(x)
Returns square root of x.
bn.floor(x)
bn.ceil(x)
bn.round(x)
Round x up/down/to the nearest integer.
bn.neg(x)
Return -x.
bn.abs(x)
Return absolute value of x.
bn.compare(x, y)
Compare x and y, returns -1, 0, 1 (less, equal, greater) or nil if one of the numbers was NaN.
bn.gcd(x, y)
Greatest common divisor of x and y (supports fractional numbers).
bn.powmod(x, y, z)
Returns x ^ y modulo z, integers only.
bn.invmod(x, y)
Returns inverse of x modulo y, integers only.
bn.band(x, y)
bn.bor(x, y)
bn.bxor(x, y)
Returns binary and/or/xor of x and y.
bn.tonumber(x)
Returns x as a Lua number.
bn.tostring(x[, base])
Returns 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)
Checks whether x is negative, zero, one, an integer, odd, or prime.
bn.clear(x)
Clear memory used by x (setting it to 0).
bn.bits(x)
Returns a pair a, b, where a is the number of bits before the 'decimal' point, b is the number of fractional bits.
bn.bit(x, i)
Returns bit i of x, where 0 is the first bit before the 'decimal' point, and -1 is the first fractional bit
bn.random([gen][, bits])
Generate a random number with 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)
Generate a random number in range [0,x), using maximum allowed fractional bits.
bn.randprime([gen,] bits)
Generate a random bits-bit prime number.

RivSoft.net by Riv (riv@rivsoft.net) - All rights reserved