SBIL++ is a C++ library designed for arbitrary-precision integer arithmetic. It provides a BigInt class capable of handling integers far exceeding the limits of standard types like long long. This library is built using modern C++20 features, focusing on correctness, clarity, and ease of use — with high-performance algorithms and an optional cryptographic-safe build mode.
This project started as a guided learning exercise and aims to provide a robust foundation for applications requiring large integer mathematics.
- Arbitrary Precision: Stores integers of virtually unlimited size.
- Core Arithmetic:
- Addition (
+,+=) - Subtraction (
-,-=) - Multiplication (
*,*=) — schoolbook + Karatsuba - Division and Modulo (
/,/=,%,%=) — Knuth Algorithm D + Burnikel-Ziegler recursive - Modular exponentiation (
mod_pow) — Montgomery (CIOS/SOS) for odd moduli, Barrett for even
- Addition (
- Comparison: Full set of comparison operators (
==,!=,<,>,<=,>=). - Sign Handling: Correctly manages positive and negative numbers.
- Construction:
- From
long long(explicit). - From
std::string(explicit).
- From
- Output: Stream insertion operator (
<<) for easy printing viastd::cout. - Number theory:
isPrime(Miller-Rabin),nextPrime,gcd,lcm,modInverse,sqrt,factorial. - PRNG:
pseudo_random(Mersenne Twister, NOT for crypto) andsecure_random(/dev/urandom). - Cryptographic-safe mode: build with
-Dcrypto_safe=trueto get constant-timemod_pow(Montgomery ladder), constant-time comparisons, and zeroing of all limb memory (SecureAllocator) — protection against timing attacks for secret data. - Modern C++: Uses C++20, RAII (via
std::vector),explicitconstructors, and the Rule of Five. - Tested: Includes a growing suite of unit tests using the Catch2 framework.
This project uses the Meson build system.
- A C++20 compliant compiler (e.g., GCC, Clang)
- Meson (
>= 0.60) - Ninja (or another Meson backend)
- Catch2 (
>= 3.x) - Handled automatically via Meson Wrap or Nix
-
Clone the Repository:
git clone <your-repo-url> cd SBIL++
-
Configure with Meson: This command sets up the build directory (
build) and detects dependencies. If using Meson Wrap, Catch2 will be downloaded automatically.meson setup build
(Optional: For a release build, use
meson setup build --buildtype=release) -
Compile:
meson compile -C build
-
Run Tests:
meson test -C build
To get constant-time operations (Montgomery ladder in mod_pow, full-scan
comparisons) and zeroing of all limb memory, build with the crypto_safe option:
meson setup build-crypto -Dcrypto_safe=true
meson compile -C build-crypto
meson test -C build-crypto # full suite must pass in BOTH modesScope limits: division, gcd, modInverse, and isPrime/nextPrime candidate
rejection remain variable-time; constant-time mod_pow throws std::invalid_argument
for even moduli. Keep exponents fixed-size in crypto use (the ladder iterates
exponent.bitLength() times).
A shell.nix file is provided for a reproducible development environment.
-
Enter the Nix Shell: Navigate to the project root and run:
nix-shell
(Or use
direnv allowif you have direnv configured) -
Build using Meson: Follow steps 2-4 from the standard build instructions above. The Nix environment provides all necessary dependencies.
#include <iostream>
#include "sbilpp/BigInt.h"
int main() {
// Construction (Explicit)
SBILPP::BigInt a("-1234567890123456789012345");
SBILPP::BigInt b(9876543210);
// Arithmetic
SBILPP::BigInt sum = a + b;
SBILPP::BigInt product = a * 100;
SBILPP::BigInt division = product / 7559;
a += b;
// Comparison
if (a < b) {
std::cout << "a is smaller than b" << std::endl;
}
// Output
std::cout << "a = " << a << std::endl;
std::cout << "b = " << b << std::endl;
std::cout << "Sum = " << sum << std::endl;
std::cout << "Product = " << product << std::endl;
std::cout << "Division = " << division << std::endl;
return 0;
}