Skip to content
This repository has been archived by the owner on Jul 1, 2018. It is now read-only.

biginteger library

SquidDev edited this page Dec 11, 2016 · 2 revisions

The biginteger library exposes Java's BigInteger class to Lua to enable fast implementations of cryptographic algorithms.

Examples

The biginteger library has support for all basic bit and arithmetic operations.

local a = biginteger.new(23)
print(a ^ 100)
print(biginteger.bnot(a))

Like Lua's string library, all methods exposed in the biginteger library can be directly accessed from instances of the object.

print(a:bor(1))

Configuration

The biginteger library can be enabled or disabled using the APIs.bigInteger config option.

Methods

Operators

All arithmetic and bit operands are implemented and can be accessed through the library or using the relevant operator. Any number of arguments to these methods can be a biginteger: all other arguments will be coerced to a number.

Any illegal operation (such as dividing by 0) will result in NaN.

Name Method Symbol Type
Unary minus unm - Unary
Addition add + Binary
Subtraction sub - Binary
Multiplication mul * Binary
Modulus mod % Binary
Exponent pow ^ Binary
Division div / Binary
Integer division idiv // Binary
Binary and band & Binary
Binary or bor ` `
Binary xor bxor ~ Binary
Left shift shl << Binary
Right shift shr >> Binary
Unary binary not bnot ~ Unary
Equals eq == Comparison
Less than lt < Comparison
Less than equals le <= Comparison

Conversion methods

biginteger.new(value:number)

Create a new biginteger from a number. If the argument is a string then it will be converted to a number.

bitinteger.tostring(value:biginteger)

Convert a biginteger to a string. This can also be done with the tostring function.

bitinteger.tonumber(value:biginteger)

Convert a biginteger to a number. This can also be done with the tonumber function.

Warning: There may be a loss of precision when converting to a number using this method: this converts to 64 bit double before converting to a Lua number.

Arithmetic methods

biginteger.modinv(value:biginteger, mod:biginteger)

Return the modular multiplicative inverse. If no such value exists (value is not relatively prime to mod) then NaN is returned.

biginteger.gcd(a:biginteger, b:biginteger)

Return the greatest common divisor of a and b. Returns 0 if both a and b are 0.

biginteger.modpow(value:biginteger, exponent:biginteger, mod:biginteger)

Returns a number whose value is (value ^ exponent) % mod. Returns NaN if mod is less than or equal to 0.

biginteger.abs(value:biginteger)

Returns the absolute value of this number.

biginteger.min(a:biginteger, ...:biginteger)

Returns the minimum value of these values.

biginteger.max(a:biginteger, ...:biginteger)

Returns the maximum value of these values.

Prime number methods

biginteger.isProbPrime(value:biginteger[, certainty:int])

Returns true if this number is probably prime. If this method returns true then the probability that this value is prime is greater than 1 - 0.5^certainty. This defaults to a certainty of 100.

biginteger.nextProbPrime(value:biginteger)

Returns the next number that is probably prime. This uses a certainty of 100.

biginteger.newProbPrime(len:int[, seed:int])

Returns a positive biginteger that is probably prime with the specified number of bits. This uses a certainty of 100. This will use the global biginteger seed if one is not specified.

biginteger.seed(value:biginteger)

Set the seed for the random number generator.