-
Notifications
You must be signed in to change notification settings - Fork 5
biginteger library
The biginteger
library exposes Java's BigInteger
class to Lua to enable fast implementations of cryptographic algorithms.
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))
The biginteger
library can be enabled or disabled using the APIs.bigInteger
config option.
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 |
Create a new biginteger from a number. If the argument is a string then it will be converted to a number.
Convert a biginteger to a string. This can also be done with the tostring
function.
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.
Return the modular multiplicative inverse. If no such value exists (value is not relatively prime to mod) then NaN
is returned.
Return the greatest common divisor of a
and b
. Returns 0 if both a
and b
are 0.
Returns a number whose value is (value ^ exponent) % mod
. Returns NaN
if mod
is less than or equal to 0.
Returns the absolute value of this number.
Returns the minimum value of these values.
Returns the maximum value of these values.
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.
Returns the next number that is probably prime. This uses a certainty of 100.
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.
Set the seed for the random number generator.