BigInt

Minimal pure-Kotlin unsigned big integer, base 2^32 (limbs are UInt stored in an IntArray little-endian: limb 0 is least significant). Non-negative values only. That is all KiteTorrent's crypto stack needs (MSE Diffie-Hellman modular exponentiation over a fixed 768-bit prime, and the ed25519 field/scalar arithmetic).

There is no BigInteger in the Kotlin common standard library, so libtorrent's reliance on boost::multiprecision (see src/pe_crypto.cpp) has to be reimplemented from scratch. This class provides exactly the primitives required: comparison, add / subtract / multiply, schoolbook long-division remainder (mod), modular multiplication and modular exponentiation (modPow, the analogue of boost::multiprecision::powm), plus fixed-width big-endian byte (de)serialization.

It favours clarity and correctness over speed. The numbers involved are small (<= ~768 bits for DH, <= ~512 bits intermediate for the field), so the O(n^2) multiply and bit-at-a-time division are perfectly adequate.

Works on every KMP target: only IntArray, UInt/ULong limb math and stdlib helpers are used.

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

Functions

Link copied to clipboard
fun bitLength(): Int

Number of significant bits (0 for zero).

Link copied to clipboard
open operator override fun compareTo(other: BigInt): Int
Link copied to clipboard
open operator override fun equals(other: Any?): Boolean
Link copied to clipboard
open override fun hashCode(): Int
Link copied to clipboard
operator fun minus(other: BigInt): BigInt

Subtraction; requires this >= other (non-negative result).

Link copied to clipboard
fun mod(m: BigInt): BigInt

Remainder this mod m (m must be > 0). Result in [0, m).

Link copied to clipboard
fun modMul(other: BigInt, m: BigInt): BigInt

(this * other) mod m.

Link copied to clipboard
fun modPow(exp: BigInt, m: BigInt): BigInt

Modular exponentiation: (this ^ exp) mod m, via right-to-left square-and-multiply. Equivalent to boost's powm(base, exp, mod).

Link copied to clipboard
operator fun plus(other: BigInt): BigInt
Link copied to clipboard

Test bit i (0 = LSB).

Link copied to clipboard
operator fun times(other: BigInt): BigInt
Link copied to clipboard

Big-endian byte representation, exactly width bytes, zero-padded on the left (most-significant side). Mirrors libtorrent's export_key, which right-justifies the magnitude into a fixed 96-byte field. Requires the value to fit in width bytes.