sysl

The bigint module

sysl.math.bigint — integers with no width: sign and magnitude over machine-word limbs, Karatsuba, Toom-3 and number-theoretic-transform multiplication, Knuth’s algorithm D and recursive division, Lehmer’s gcd and the half-gcd with the extended gcd and modular inverse, bit operations on an infinite two’s complement, and text in any base.

Every declaration in sysl.math.bigint, with its signature: the generated API page. This page is the argument — what the module is for, and how its pieces fit; that one is the list.

A BigInt is an integer with no width. It is here because a general-purpose language without one makes a whole class of program wrong in the same way — a hash reduced modulo something, a factorial, a cryptographic exponent, an identifier that outgrew 64 bits — and because the alternative is a package every serious program ends up depending on anyway. Go ships math/big; this takes Go’s side.

import sysl.math.bigint.{from_int, mul, one, pow}

var f = one()

for i in 1..<21
    f = mul(f, from_int(long(i)))

print(f.to_string())
print(pow(from_int(2), 128).to_string())
2432902008176640000
340282366920938463463374607431768211456

Sign and magnitude, and the magnitude is limbs

A BigInt is a sign and a magnitude, and the magnitude is a view of limbs written least significant first with no leading zeros. Zero is the empty magnitude and a clear sign, so there is exactly one representation of it and nothing has to remember whether it is negative — which is what makes Eq, Ord and Hash straightforward rather than careful.

A limb is a machine word, and the arithmetic is done in a type twice as wide. On a 64-bit target a limb is a u64 and every carry, borrow and partial product is a u128; on a 32-bit or 16-bit target (#if ptr32, #if ptr16) a limb is a u32 and the double is a u64. sysl has uN at any width, so the double-width type is an ordinary integer rather than a carry flag or an intrinsic, and a u128 product compiles to the machine’s own high-half multiply. A product of two limbs is at most (2^w - 1)^2, which fits the double width with room for two more limbs of carry, and that is what makes every step exact. x.limbs() counts machine words, so it answers differently on the two kinds of target; bit_length(x) does not.

The magnitude is a view, so copying a BigInt costs a retain rather than a copy of its limbs. That is what makes passing one around cheap, and it is why the limbs are never written through: every operation builds a new magnitude, so two values may share one and neither can see the other change.

The operators are there, and they allocate

+, -, *, / and % are all implemented, and so are &, |, ^, ~, <<, >>, ==, < and the rest. Every one of them that grows allocates — there is no in-place arithmetic and no reuse of a caller’s buffer, which is what makes a + b * c mean what it looks like:

import sysl.math.bigint.parse

val a = parse("12345678901234567890").expect("a number")
val b = parse("98765432109876543210").expect("a number")

print((a * b).to_string())
print((a + b).to_string())
print(a < b, a == a)
1219326311370217952237463801111263526900
111111111011111111100
true true

A program doing this in a loop is doing something a big-integer library is not the fastest way to do.

There are two divisions, and a program picks one

div_rem truncates toward zero and its remainder takes the dividend’s sign, which is what C, Go, Rust and sysl’s own / and % on machine integers all do. div_rem_floor rounds toward negative infinity and its remainder takes the divisor’s sign, which is what Python’s // and % do. The identity a == q * b + r holds for both, so they are two roundings of one division rather than two operations; they differ only where the remainder is non-zero and the operands’ signs disagree.

import sysl.math.bigint.{div_rem, from_int}

val (q, r) = div_rem(from_int(-7), from_int(2))

print(q.to_string(), r.to_string())
-3 -1
import sysl.math.bigint.{div_floor, div_rem_floor, from_int, mod_floor}

val (q, r) = div_rem_floor(from_int(-7), from_int(2))

print(q.to_string(), r.to_string())
print(div_floor(from_int(7), from_int(-2)).to_string(), mod_floor(from_int(7), from_int(-2)).to_string())
print(mod_floor(from_int(-1), from_int(12)).to_string())
-4 1
-4 -1
11

Which one to reach for is decided by what the remainder is for. A quotient whose magnitude should not depend on the signs wants the truncating pair. A remainder that has to land in a fixed range — an index into a ring buffer, a value reduced modulo a modulus, a signed coordinate put in a bucket — wants the floored one, and gets a negative index from the other. That is what the last line above is: mod_floor(-1, 12) is 11, where rem(-1, 12) is -1.

div_rem and div_rem_floor each answer both halves at once, which is one division rather than two. Division by zero traps, as it does on a machine integer, rather than answering a Result.

The algorithms are named so they can be checked

Each operation picks its algorithm by the size of its operands, and every one of them is a published algorithm a reader can check this one against. None changes an answer; they change how the time grows.

operationsmall operandslargerlargest
multiplyschoolbookKaratsuba (from 32 limbs), then Toom-3 (from 160 limbs), Bodrato’s interpolationnumber-theoretic transform modulo three primes (from 600 limbs)
squareschoolbook, each cross product onceKaratsuba of squaresthe transform, one forward transform per prime (from 800 limbs)
divideKnuth’s algorithm D (TAOCP vol. 2, §4.3.1)Burnikel–Ziegler recursive division (divisor and quotient both from 48 limbs)—
divide by one limbMöller–Granlund division by a precomputed reciprocal——
text, both waysa limb-sized chunk of digits at a timedivide and conquer over the squares of the chunk (from 40 limbs)—
gcd, extended gcd, inverseEuclid in machine wordsLehmer’s algorithm (TAOCP §4.5.2, algorithm L)Möller’s half-gcd (“On Schönhage’s algorithm and subquadratic integer gcd computation”, 2008), the smaller operand from 150 limbs

Karatsuba multiplies in about n^1.585 steps where schoolbook takes n^2, Toom-3 in about n^1.465, and the transform in about n log n; the recursive division costs about twice a multiplication of the same size, and the conversions to and from text a multiplication’s cost times log n — so the transform speeds up every one of them at large sizes, and pow too. The thresholds are measured crossovers on a 64-bit Apple core.

The transform is Pollard’s: a discrete Fourier transform modulo a prime, rather than the Schönhage–Strassen transform modulo 2^N + 1 that GMP uses. Each limb is one coefficient; the product’s coefficients are found modulo three primes just under 2^62, each of the form c · 2^k + 1 so that roots of unity of every needed order exist, and put together by the Chinese remainder theorem (Garner’s form). Their product is about 2^182.7, more than any coefficient of a product of two operands under 2^54 limbs can reach, so the answer is exact — there is no rounding, as there would be in floating point. A butterfly is a multiply by a precomputed twiddle in Shoup’s form, the values are reduced lazily (Harvey, 2014), and a transform is a power of two long or three times one, so a product never pays for a transform more than a third longer than it needs. It needs a 64-bit limb: on a 32-bit target Toom-3 carries the largest products.

The carrying loops are written with add_carry and sub_borrow. A limb of a sum is a[i].add_carry(b[i], carry), the prelude’s Carrying members on the unsigned integers (reference), which lower to a pair of llvm.uadd.with.overflow calls whose carry out is the next limb’s carry in, so the back end forms an add-with-carry chain without the loop reading either overflow back off a comparison. On AArch64 the five carrying loops are assembly and these are the portable reference the assembly is tested against; every other processor runs them.

Division by a single limb never divides. No 64-bit machine has an instruction for a two-limb by one-limb division, and a u128 / u128 is a call into the runtime; Möller and Granlund’s method (“Improved division by invariant integers”, 2011) replaces it with two multiplications by a reciprocal computed once per divisor. Every conversion to text is made of these.

The one thing worth knowing about algorithm D is that its quotient digit is a guess that has to be corrected twice — a loop that tightens it, and an add-back for the one case the loop still gets wrong. The add-back fires about twice in every 2^w digits for a w-bit limb, which is rare enough that a suite of generated inputs never enters it: the library’s own tests use inputs constructed to force it, one for each limb width, because two hundred random pairs left the branch green with it deleted. The recursive division has a rare branch of the same kind — a guess capped at every limb set when the top of the dividend equals the top of the divisor — and its tests force that one with a quotient whose every limb is the largest there is.

The bit operations read a value as infinitely wide two’s complement

A BigInt is stored as a sign and a magnitude, so &, |, ^, ~ and the shifts have to be told what they mean before they can be implemented. The reading is Python’s: a value is an endless run of bits, a non-negative one padded upward with zeros and a negative one padded upward with ones.

That reading is not a preference, it is the only one that is total. Any other answer needs a width to complement within, and there is nothing a caller could pass as the width that would not be a guess — so and(-6, 3) would have to be refused, or answered differently depending on a number nobody has. On this reading it is 2, for the same reason it is 2 at every machine width.

import sysl.math.bigint.{and, from_int, not, or, shl, shr, xor}

val a = from_int(-6)
val b = from_int(3)

print((a & b).to_string(), (a | b).to_string(), (a ^ b).to_string())
print((~from_int(5)).to_string(), not(from_int(-1)).to_string())
print(shl(from_int(1), 100).to_string())
print(shr(from_int(-1), 100).to_string(), shr(from_int(-7), 1).to_string())
2 -5 -7
-6 0
1267650600228229401496703205376
-1 -4

Three consequences are worth reading off that output, because each is a place a width-bound intuition gives the wrong answer:

  • ~x is -x - 1, for every x. There is no width for the complement to fill, so inverting an endless run of bits is that identity and nothing else. ~5 is -6, and ~(-1) is 0.
  • << grows rather than discarding. 1 << 100 is 2^100, which is the whole point of the type; there is no top for a bit to leave by.
  • >> floors toward negative infinity, so (-1) >> 100 is -1 and (-7) >> 1 is -4. The sign is dragged down from above, which for a negative number rounds away from zero in magnitude. It agrees with >> on every machine integer in the language, and with div_floor beside it.

Only &, | and ^ pay anything for the reading: a negative operand is complemented limb-wise into one limb more than the longer magnitude needs, which is what makes the top limb of each side a pure sign and lets the result’s sign be read off it. The queries are cheaper than that.

bit_length is Python’s: how many bits the magnitude occupies, so a value and its negation answer alike and bit_length(0) is 0. trailing_zeros counts how many times two divides the value, test_bit asks about a bit on the same two’s-complement reading — so every bit above a negative value’s magnitude is set — and is_even and is_odd are the lowest bit.

The number theory, written on purpose

import sysl.math.bigint.{from_int, gcd, gcd_ext, isqrt, lcm, mod_inverse, mod_pow, parse, pow}

print(gcd(from_int(-12), from_int(18)).to_string(), lcm(from_int(4), from_int(6)).to_string())

val (g, s, t) = gcd_ext(from_int(240), from_int(46))

print(g.to_string(), s.to_string(), t.to_string())
print(mod_inverse(from_int(3), from_int(7)).unwrap().to_string(), mod_inverse(from_int(2), from_int(4)).is_none())
print(mod_pow(from_int(2), from_int(1000000006), from_int(1000000007)).to_string())
print(isqrt(pow(from_int(10), 30)).to_string())
print(isqrt(parse("2").expect("a number")).to_string())
6 12
2 -9 47
5 true
1
1000000000000000
1

gcd is Euclid over the magnitudes and is never negative, the divisors of a number and of its negation being the same set. gcd(x, 0) is the magnitude of x. lcm divides before it multiplies — a / gcd(a, b) * b, never a * b / gcd(a, b) — because the second builds an intermediate with as many limbs as both operands together, and the quotient is exact by construction.

gcd_ext(a, b) is (g, s, t) with a s + b t = g, and its cofactors are GMP’s (mpz_gcdext): the pair with |s| < |b| / (2g) and |t| < |a| / (2g), which those bounds make unique, so two implementations agree on more than the gcd. Where the bounds leave no pair the definition forces one — |a| == |b| gives s = 0, t = sign(b); a zero b gives s = sign(a), t = 0; |b| == 2g gives s = sign(a) — and gcd_ext(0, 0) is (0, 0, 0). mod_inverse(a, m) is an Option: None where a and m share a factor, and otherwise the inverse taking the modulus’s sign, as mod_pow‘s answer does. Modulo 1 it is Some(0); a zero modulus traps.

From 150 limbs the three reduce through the half-gcd, which finds the matrix that takes the top half of the pair halfway to its gcd by recursing on the top half of that, so the whole costs a multiplication’s time times log n rather than Lehmer’s n^2. It is the same reduction for all three: gcd_ext carries one cofactor through every matrix and finds the other with one exact division at the end. The library’s tests push the thresholds down to a handful of limbs so that every branch of the recursion runs in the suite; agreement with GMP from one limb to thousands, across the real thresholds, is checked against the gmp package outside it.

mod_pow is square-and-multiply with a reduction after every step, which is what makes the second line above possible at all: pow(2, 1000000006) is a number with three hundred million digits, and the answer to it modulo a prime is 1, by Fermat’s little theorem. The answer takes the modulus’s sign, as mod_floor does. It is not constant-time, and must not be used where the exponent is a secret — the loop multiplies on a set bit and does not on a clear one, so its timing reports how many bits are set. A constant-time exponentiation is a different function and would say so.

isqrt is Newton’s method on the integers and lands exactly, with no tolerance to choose: each step from a starting over-estimate is still an over-estimate, and the first step that fails to decrease is the one that has arrived. It is the exact floor, so isqrt(2) is 1. A negative traps.

Text in any base from 2 to 36

Both directions work a chunk at a time rather than a digit at a time, the chunk being the largest power of the base that fits in a limb — on a 64-bit target 10^19 for base ten and 2^63 for binary. So rendering divides the magnitude by 10^19 and gets nineteen digits out of the remainder with ordinary machine arithmetic. A large value is first split by divide and conquer: divided by the largest 10^(19·2^k) below it, with the quotient and the remainder each rendered the same way and the remainder padded with the zeros that are digits of the whole — and reading is the mirror image, so a million digits take a fraction of a second rather than minutes.

import sysl.math.bigint.{parse, parse_radix, to_string_radix}

val x = parse("340282366920938463463374607431768211455").expect("a number")

print(to_string_radix(x, 16))
print(to_string_radix(parse("255").expect("a number"), 2))
print(parse_radix("ff", 16).expect("a number").to_long().expect("it fits"))
ffffffffffffffffffffffffffffffff
11111111
255

Parsing takes an optional sign and then at least one digit, in either case. Nothing else: no underscores, no 0x prefix, no leading or trailing space. What a document permits is the document’s business, and stripping is one call before this one — which is also the right place for a language’s own literal syntax to handle its digit separators.

"-0" parses to zero and renders as "0", which is the one input that does not round trip and could not — there is no negative zero in this representation, which is exactly what makes equality and hashing straightforward everywhere else.

Getting a machine number back out

import sysl.math.bigint.{bit_length, fits_long, from_int, from_u64, pow, to_u64}

val big = pow(from_int(10), 30)

print(bit_length(big), fits_long(big), bit_length(from_int(-255)))
print(to_u64(from_u64(18446744073709551615u64)).expect("it fits"))
print(to_u64(from_int(-1)).is_none())
100 false 8
18446744073709551615
true

to_long and to_u64 answer None rather than truncating, because a big integer that has outgrown 64 bits is exactly the case this type exists for and silently wrapping it would defeat the purpose. fits_long asks the same question on its own, for a caller whose answer decides a route rather than a value. A negative value has no unsigned reading here: to_u64 refuses it rather than picking one of the truncations of an endless run of ones.

The floating-point pair is exact about where it is not exact:

import sysl.math.Float
import sysl.math.bigint.{from_int, from_real, pow, to_real}

print(to_real(pow(from_int(10), 30)))
print(from_real(1.0e30).expect("finite").to_string())
print(to_real(pow(from_int(10), 400)).is_infinite())
print(from_real(real.nan()).is_none(), from_real(-2.7).expect("finite").to_string())
1000000000000000000000000000000
1000000000000000019884624838656
true
true -2

to_real is correctly rounded, to nearest with ties to even — the same rule the hardware applies to every arithmetic result, so a value converted here and a value computed there round alike. That needs three things out of the magnitude rather than one: the top 53 bits, the bit below them, and whether anything at all is set under that. Taking the top 53 and truncating would be off by one unit in the last place half the time, and biased toward zero always.

from_real is exact for every finite real, and truncates toward zero. The second line above is the interesting one: 1.0e30 is not the number 10^30, and this says which number it actually is.

One big number and one small one

A program built over this type rarely holds two large numbers at once. A language whose integers are a 64-bit fast path promoting to a BigInt on overflow spends its life adding a literal to a value that has already grown — and written as add(a, from_int(n)), every one of those builds a one-limb BigInt to throw away a line later.

import sysl.math.bigint.{add_long, cmp_long, div_rem_long, from_int, mul_long, pow}

var x = pow(from_int(10), 25)

print(add_long(x, 7).to_string(), mul_long(x, -3).to_string())
print(cmp_long(x, 9223372036854775807))

val (q, r) = div_rem_long(x, 7)

print(q.to_string(), r)
10000000000000000000000007 -30000000000000000000000000
1
1428571428571428571428571 3

What is actually saved varies by operation, and the module says which is which rather than implying the whole family is a fast path. cmp_long allocates nothing at all, which is the one that matters most — a comparison against a small constant is what a loop condition is made of. mul_long and div_rem_long take a single-limb route wherever the operand fits a limb, which is one pass over the limbs rather than a pass plus a general multiply or a normalized long division. add_long and sub_long are the general functions with the conversion written once, and are there so that a caller’s code reads the same for all five. div_rem_long‘s remainder comes back as a long, since it is smaller in magnitude than a divisor that was one.

What is deliberately not here

Primality. A useful test is Miller-Rabin, and which witness set or how many rounds it should use is a decision about what the caller is doing rather than one this module can make — a deterministic set below 2^64, a probabilistic round count above it. Nothing in the library needs one, and a wrong answer from a primality test is the expensive kind.

Search

Esc
↑↓ to navigate ↵ to open Esc to close