BigInt is currently EXPERIMENTAL.
The implementation is quite stable, but aspects of the user-facing API
are still subject to change.
BigInt is a high-performance arbitrary-precision
signed integer type for Kotlin Multiplatform, designed to
bring efficient big-integer arithmetic to
JVM, Native, WASM, and JavaScript with no external dependencies.
It provides idiomatic Kotlin arithmetic operators, efficient mixed-primitive arithmetic, reduced heap allocation overhead, and a clean, understandable implementation suitable for applications needing hundreds or thousands of digits.
+ - * / % mod< <= == != >= >sqr() isqrt() pow(n) abs() gcd() factorial(n)Int, UInt, Long, ULong) without BigInteger boxingmodAdd modSub modMul modPow for cryptographyIntArrayMutableBigInt class for cryptography and other computationally intensive algorithms.MutableBigIntdependencies {
implementation("com.decimal128:bigint:<version-coming-soon>")
}
BigInt is written in Kotlin and has no external dependencies.
BigInt values should be initialized from Kotlin primitive integer and String types using the supplied extension functions:
val zero = 0.toBigInt()
val small = 123456789L.toBigInt()
val dec = "123456789012345678901234567890".toBigInt()
val nines = "-999_999_999_999_999".toBigInt()
val hex = "0xCAFE_BABE_FACE_DEAD_BEEF_CEDE_FEED_BEAD_FADE".toBigInt()
val a = "123456789012345678901234567890".toBigInt()
val b = "9999999999888888888877777777776666666666".toBigInt()
val sum = a + 123
val diff = a - 678
val prod = a * b
val quot = a / b
val rem = a % 1_000_000_000_000_000_000uL
Mixing with primitive integer operands allows clean infix expressions:
val x = a + 5 // Int
val y = a * 42u // UInt
val z = a - 123456789L // Long
No BigInteger conversion means reduced heap pressure.
IntArrayMutableBigInt is a mutable companion type for
efficient in-place accumulation, dramatically reducing
temporary heap allocations during large summation-heavy
workloads and intense crypto calculations.
val s = MutableBigInt()
val s2 = MutableBigInt()
for (x in myBigData) {
s += x
s2.addSquareOf(x)
}
val sum = BigInt.from(s)
val sumOfSquares = BigInt.from(s2)
// simple factorial
val f = MutableBigInt(1) // start at 1
for (i in 2..n)
f *= i
val factorial = f.toBigInt()
Useful for statistical calculations on big data sets.
BigInt provides primitives for cryptographic applications:
ModContext with Montgomery reduction⚠️ Security Notice: This library has not undergone formal security audit. Cryptographic protocols (RSA, DH, DSA) require proper padding, key generation, and implementation of security best practices beyond raw modular arithmetic. For production cryptography, use established, audited libraries.
BigInt prioritizes:
java.math.BigInteger is mature and battle-tested, but:
BigInt does not implement some advanced algorithms (like Toom-Cook-3 and FFT multiplication) and has not been performance tested for operations involving tens-of-thousand of digits.
BigInt’s combination of optimized schoolbook multiplication, schoolbook squaring, and Karatsuba squaring, low heap pressure, and mutable operations provides practical performance benefits.
Benchmarks coming soon.
./gradlew build
Run tests:
./gradlew test
This project is licensed under the MIT License — see the LICENSE file for details.
We’re especially interested in: