An implementation of arbitrary length integers in pure Lua.
- Implements arithmetic operators: addition, subtraction, multiplication, division, power, unary minus, log2
- Implements bitwise operators: and, or, xor, not, shift left, shift right, set bits, unset bits, get bit, 2's complement
- Implements all comparison operators
- Construct from Lua number, string with base, array of bytes, string of bytes
- Convert to Lua number, string with base, string of bytes, little-endian string of bytes
- Reasonably fast
- Compatible with Lua 5.1+
bigint = require("bigint")
-- implementation of the 64-bit FNV-1 hash function
local fnvOffsetBasis = bigint("0xcbf29ce484222325")
local fnvPrime = bigint("0x100000001b3")
local maxValue64 = bigint("0x10000000000000000")
function fnv1(str)
local hash = fnvOffsetBasis
for i = 1, #str, 1 do
local byte = str:byte(i)
hash = hash * fnvPrime
hash = hash % maxValue64
hash = hash:Bxor(byte)
-- or in Lua 5.3+:
-- hash = hash ~ byte
end
return hash
end
print(fnv1("data to hash"):ToHex())
-- output: 0xc86b139d989958e4The available methods are listed below. Parameters of type bigint can also be converted implicitly from other types.
Constructors:
bigint(val: any, [base: number = 10]): bigint: chooses from one of the following constructors based on the type ofvalbigint.FromString(val: string, [base: number = 10]): bigint: constructs from a string representation of a number (supports 0x and 0b prefixes)bigint.FromNumber(val: number): bigint: constructs from a native Lua numberbigint.FromArray(val: array, [littleEndian: bool = false]): bigint: constructs from an array of bytesbigint.FromBytes(val: string, [littleEndian: bool = false]): bigint: constructs from a string of bytes
Converters:
bigint:ToBytes([size: number], [littleEndian: bool = false]): string: converts to a string of bytesbigint:ToNumber(): number: converts to a native Lua numberbigint:ToBool(): bool: converts to a booleanbigint:ToHex([noPrefix: bool = false]): string: converts to a hexadecimal stringbigint:ToBin([noPrefix: bool = false]): string: converts to a binary stringbigint:ToBase(base: number): string: converts to a string with the specified base
Arithmetic operators:
bigint:Add(val: bigint): bigint: add (+)bigint:Sub(val: bigint): bigint: subtract (-)bigint:Mul(val: bigint): bigint: multiply (*)bigint:Div(val: bigint): bigint: divide (/or//5.3+)bigint:DivWithRemainder(val: bigint): bigint, bigint: divide and return both quotient and remainderbigint:Mod(val: bigint): bigint: modulo (%)bigint:Pow(val: bigint): bigint: power (^)bigint:Unm(): bigint: unary minus (-)bigint:Log2(): bigint: log base 2bigint:Abs(): bigint: absolute value
Bitwise operators:
bigint:Shr(val: bigint): bigint: shift right (>>5.3+)bigint:Shl(val: bigint): bigint: shift left (<<5.3+)bigint:Band(val: bigint): bigint: bitwise and (&5.3+)bigint:Bor(val: bigint): bigint: bitwise or (|5.3+)bigint:Bxor(val: bigint): bigint: bitwise xor (~5.3+)bigint:Bnot([size: number]): bigint: bitwise not (~5.3+)bigint:SetBits(bits: number...): bigint: sets bits to 1 at the given indices (starting at 1)bigint:UnsetBits(bits: number...): bigint: sets bits to 0 at the given indicesbigint:GetBit(bit: bigint): number: returns the value of the bit at the indexbigint:CastUnsigned(size: bigint): bigint: converts a signed value withsizebytes to its unsigned 2's complement representationbigint:CastSigned(size: bigint): bigint: converts an unsigned value withsizebytes to its signed 2's complement representation
Comparison operators:
bigint:Compare(val: bigint): number: returns -1 if smaller, 0 if equal, and 1 if largerbigint:CompareU(val: bigint): number: likeComparebut ignores signbigint:CompareOp(val: bigint, op: string): bool: performs one of the below comparisons matchingopbigint:Eq(val: bigint): bool: equal (==)bigint:Ne(val: bigint): bool: not equal (~=)bigint:Lt(val: bigint): bool: less than (<)bigint:Le(val: bigint): bool: less than or equal (<=)bigint:Gt(val: bigint): bool: greater than (>)bigint:Ge(val: bigint): bool: greater than or equal (>=)
Constants:
bigint.Zerobigint.Onebigint.NegOnebigint.MaxNumber: the highest integer that can be represented accurately by native Lua numbers
Misc:
bigint.IsBigInt(val: any): bool: returnstrueifvalis a bigintbigint:Copy(): bigint: returns a copy of the bigintbigint:IsEven(): bool: efficiently determine whether bigint is evenbigint:IsOne(): bool: efficiently determine whither bigint equals one
Run python test.py [iterations = 1000] in the tests directory to perform tests.