bignum

Batched arbitrary-precision integer arithmetic on the GPU, loadable through kernels: addition, subtraction, multiplication, signed comparison, and modular reduction over a batch of wide integers, exact at every width. The reference baseline is Python's native integer arithmetic, compared digit for digit, with GMP as the throughput yardstick.

Exact algorithms over the integers, Hermite and Smith normal forms, lattice basis reduction, integer relation detection, carry coefficients that grow past 64 bits and are not decomposable by CRT the way determinants are. They need wide arithmetic on the device itself, and GPUs have no such type. This kernel supplies it as a batch primitive: thousands of 8192-bit values computed at once, so the wide-integer inner loops of exact linear algebra run where the rest of the computation lives.

A 64 by 64 lane wall pulses as batches of 2048-bit modular multiplies sweep through, with completion counters running against GMP

4,096 lanes of 2048-bit a*b mod M in flight: 2.5 million exact modular multiplies per second, 2.6x one GMP core, every lane digit-for-digit equal to GMP.

Usage

import torch
from kernels import get_kernel

bn = get_kernel("phanerozoic/bignum", version=1, trust_remote_code=True)

a = bn.to_device([3**400, -(5**300), 2**1000])
b = bn.to_device([7**200, 11**150, 1], limbs=a.size(0))

bn.from_device(bn.add(a, b))
bn.from_device(bn.mul(a, b))
bn.compare(a, b)                       # -1, 0 or 1 per lane
bn.from_device(bn.mod(a, (1 << 127) - 1))

version selects the release branch; trust_remote_code is required by kernels for publishers without the trusted-publisher mark.

API

Symbol Purpose
to_device(values, limbs=None) pack Python ints into an [L, N] limb tensor
from_device(tensor) unpack a limb tensor into Python ints
limbs_for(bits) limb count holding a signed value of that magnitude
add(a, b), sub(a, b) elementwise sum and difference, wrapping at the operand width
mul(a, b) elementwise product, widened to La + Lb limbs
compare(a, b) elementwise signed comparison, -1, 0 or 1 per lane
mod(a, M) every lane reduced modulo one modulus, into [0, M)
mulmod(a, b, M) elementwise a * b mod M
barrett_mu(M, operand_limbs) reusable Barrett constant for a fixed modulus and operand width

Method

A batch of N integers, each L limbs of 64 bits, is an [L, N] int64 tensor: limb i of value t sits at i * N + t, so consecutive threads touch consecutive addresses on every limb access and the carry chains that are sequential within one value amortize across the batch. Values are little-endian two's complement at a fixed width, up to 128 limbs (8192 bits).

Addition, subtraction, and comparison run directly on the two's-complement representation, one thread per value. Multiplication converts to sign and magnitude, accumulates schoolbook partial products through __umul64hi, and reapplies the sign. Modular reduction is Barrett: q = floor(a * mu / 2^(64 * La)) with mu precomputed on the host, then a - q * M corrected by at most a few subtractions. Sizing mu to the operand width rather than the modulus width keeps the quotient estimate valid for any dividend below 2^(64 * La), so wide values reduce against a small modulus without a narrowing step. Working buffers live in a host-allocated global scratch region indexed by thread.

Measured

Batched a*b mod M at 2048 bits, 4,096 lanes, against GMP via gmpy2 on one CPU core of the same host, results compared digit for digit:

ops/s
bignum, one GPU 2,543,000
GMP, one CPU core 971,000

The batch is the regime: per-op latency favors GMP, aggregate throughput and residency next to tensors favor the device.

Verification

Every operation is compared against Python's native integer arithmetic, digit for digit: round-trip packing at the signed word boundaries, addition and subtraction at 8 to 700 bits, multiplication across all four sign combinations, comparison at the boundaries, and reduction against moduli of 61 to 256 bits with dividends far exceeding the modulus squared.

Requirements and limits

  • NVIDIA GPU with compute capability 8.0+.
  • Fixed width per batch, up to 128 limbs (8192 bits); mod/mulmod take one shared modulus per call.
  • Single-value latency is not the target; batch width is what fills the device.

References

Barrett, "Implementing the Rivest Shamir and Adleman public key encryption algorithm on a standard digital signal processor" (1986); GMP as the CPU reference implementation.

License

Apache-2.0.

Downloads last month
-
apache-2.0
Supported hardwares new
CUDA
8.08.68.99.010.012.0
GPU
B300
288GB
NVIDIA SXM
B200
192GB
NVIDIA SXM
H200
141GB
NVIDIA SXM
H100
80GB
GPU
H800
80GB
GPU
H20
96GB
GPU
L40s
48GB
GPU
L40
48GB
GPU
L20
48GB
GPU
L4
24GB
DGX Spark
GB10
128GB
GPU
RTX PRO 6000 WS
96GB
GPU
RTX PRO 6000 Max-Q
96GB
GPU
RTX PRO 5000
48GB
GPU
RTX PRO 4500 WS
32GB
GPU
RTX PRO 4000
24GB
GPU
RTX PRO 4000 SFF
24GB
GPU
RTX PRO 2000
16GB
GPU
RTX 6000 Ada
48GB
GPU
RTX 5880 Ada
48GB
RTX
RTX 5000 Ada
32GB
GPU
RTX 4500 Ada
24GB
RTX
RTX 4000 Ada
20GB
RTX
RTX 4000 SFF Ada
20GB
GPU
RTX 3500 Ada Mobile
12GB
GPU
RTX 2000 Ada
16GB
GPU
RTX A6000
48GB
GPU
RTX A5000
8GB
GPU
RTX A5000 Max-Q
16GB
GPU
RTX A5000 Mobile
16GB
GPU
RTX A4000
16GB
GPU
RTX A4000 Max-Q
8GB
GPU
RTX A4000 Mobile
8GB
GPU
RTX A3000 Mobile
6GB
GPU
RTX A2000
6GB
GPU
RTX A2000 Embedded
4GB
GPU
RTX A2000 Max-Q
4GB
GPU
RTX A2000 Mobile
4GB
GPU
A800
40GB
GPU
A100
80GB
GPU
A40
48GB
GPU
A30
24GB
GPU
A10
24GB
GPU
A2
16GB
RTX
RTX 5090
32GB
RTX
RTX 5090 D
32GB
RTX
RTX 5090 Mobile
24GB
RTX
RTX 5080
16GB
RTX
RTX 5080 Mobile
16GB
RTX
RTX 5070
12GB
RTX
RTX 5070 Mobile
8GB
RTX
RTX 5070 Ti
16GB
RTX
RTX 5070 Ti Mobile
12GB
RTX
RTX 5060 Ti
16GB
RTX
RTX 5060
8GB
RTX
RTX 5060 Mobile
8GB
RTX
RTX 5050
8GB
RTX
RTX 5050 Mobile
8GB
RTX
RTX 4090
24GB
RTX
RTX 4090D
24GB
RTX
RTX 4090 Mobile
16GB
RTX
RTX 4080 SUPER
16GB
RTX
RTX 4080
16GB
RTX
RTX 4080 Mobile
12GB
RTX
RTX 4070
12GB
RTX
RTX 4070 Mobile
8GB
RTX
RTX 4070 Ti
12GB
RTX
RTX 4070 Super
12GB
RTX
RTX 4070 Ti Super
16GB
RTX
RTX 4060
8GB
RTX
RTX 4060 Ti
8GB
RTX
RTX 4090 Laptop
16GB
RTX
RTX 4080 Laptop
12GB
RTX
RTX 4070 Laptop
8GB
RTX
RTX 4060 Laptop
8GB
RTX
RTX 4050 Laptop
6GB
RTX
RTX 3090
24GB
RTX
RTX 3090 Ti
24GB
RTX
RTX 3080
12GB
RTX
RTX 3080 Ti
12GB
RTX
RTX 3080 Mobile
16GB
RTX
RTX 3070
8GB
RTX
RTX 3070 Ti
8GB
RTX
RTX 3070 Ti Mobile
8GB
RTX
RTX 3060 Ti
8GB
RTX
RTX 3060
12GB
RTX
RTX 3060 Mobile
6GB
RTX
RTX 3050 Mobile
4GB
GPU
RTX 2050 Mobile
4GB
Jetson
Jetson AGX Orin 64GB
64GB
Jetson
Jetson AGX Orin 32GB
32GB
Jetson
Jetson Orin NX 16GB
16GB
Jetson
Jetson Orin NX 8GB
8GB
Jetson
Jetson Orin Nano 8GB
8GB
Jetson
Jetson Orin Nano 4GB
4GB
OS
linux
Arch
x86_64
Kernel Builder
19aaa64