bigint-kotlin

📘 BigInt – Arbitrary-Precision Integer Arithmetic for Kotlin Multiplatform

🚧⚠️ Warning! Construction Zone ⚠️🚧

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.


✨ Features

💡 Common Use Cases

🔧 Installation

Gradle

dependencies {
    implementation("com.decimal128:bigint:<version-coming-soon>")
}

BigInt is written in Kotlin and has no external dependencies.


🚀 Quick Start

Creating values

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()

Basic arithmetic

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

Mixed primitive operations

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.


🧱 Internal Representation


🧮 MutableBigInt

MutableBigInt is a mutable companion type for efficient in-place accumulation, dramatically reducing temporary heap allocations during large summation-heavy workloads and intense crypto calculations.

Basic usage

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.

🔒 Cryptography Support

BigInt provides primitives for cryptographic applications:

⚠️ 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.

🎯 Design Philosophy

BigInt prioritizes:

vs java.math.BigInteger (JVM only)

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.


🏗️ Building

./gradlew build

Run tests:

./gradlew test

📄 License

This project is licensed under the MIT License — see the LICENSE file for details.


🙋 Contributing

We’re especially interested in: