Bignum Arithmetic 13: A Minimal Cryptographic Bignum Library
Bignum arithmetic notes / 13
A minimal cryptographic bignum library
A usable primitive layer connects its types, arithmetic invariants, scratch-space contracts, and tests so that each caller can check the next routine's preconditions.
A minimal library for fixed-size public-key arithmetic can avoid dynamic allocation. In the API below the modulus and its context constants are public; residues and exponents may be secret. RSA operations modulo secret CRT factors require a different context contract covering those factors and the computation of their Montgomery constants.
File organization
bn_word.h word types, static assertions, low/high multiply
bn_core.c add, sub, compare-public, cmov, shifts
bn_mul.c schoolbook multiply and square
bn_mod.c modular add/sub and conditional reduction
bn_mont.c Montgomery context and multiplication
bn_exp.c fixed-window or ladder exponentiation
test_bn.c known-answer and randomized tests
sage/ SageMath vector scripts
Type sketch
typedef uint32_t limb_t;
#define BN_MAX_LIMBS 256
typedef struct {
uint32_t n;
limb_t m[BN_MAX_LIMBS];
limb_t m0inv;
limb_t r2[BN_MAX_LIMBS];
} bn_mont_ctx;
typedef struct {
uint32_t n;
limb_t v[BN_MAX_LIMBS];
} bn;
The fixed maximum keeps allocation out of arithmetic. Context creation checks \(1\le n\le\texttt{BN\_MAX\_LIMBS}\) and an odd modulus \(1<m<B^n\), then computes \(\texttt{m0inv}=-m_0^{-1}\bmod B\) and \(\texttt{r2}=B^{2n}\bmod m\). Residue operands must use the context’s public limb count; the exponent’s storage width and the ebits parameter are public as well.
API contracts
int bn_mont_init(bn_mont_ctx *ctx, const limb_t *m, uint32_t n);
void bn_to_mont(bn *r, const bn *x, const bn_mont_ctx *ctx);
void bn_from_mont(bn *r, const bn *x, const bn_mont_ctx *ctx);
void bn_mont_mul(bn *r, const bn *a, const bn *b, const bn_mont_ctx *ctx);
void bn_modexp_public(bn *r, const bn *x, const bn *e, const bn_mont_ctx *ctx);
void bn_modexp_secret(bn *r, const bn *x, const bn *e, uint32_t ebits, const bn_mont_ctx *ctx);
In this API sketch, bn_to_mont maps a canonical ordinary residue \(x\) to \(xR\bmod m\), and bn_from_mont performs the inverse conversion. bn_mont_mul and both exponentiation functions take canonical Montgomery-encoded residues and return canonical integers in the same encoding. The exponent e is an ordinary nonnegative integer, never a Montgomery residue. Thus the conversions in the RSA example below occur exactly once at each boundary.
For bn_modexp_secret, require \(e<2^{\texttt{ebits}}\), e.n <= BN_MAX_LIMBS, and ebits <= 32*e.n. The fixed capacity bounds this product by 8192. The public width determines how many bits are read. The public variant may choose its schedule from exponent bits; the secret variant uses a fixed schedule and constant-time table selection.
Example: toy RSA modulus
For an educational RSA modulus \(N=pq\) and public exponent \(e=65537\):
- initialize a Montgomery context for \(N\);
- convert message representative \(m\) to Montgomery form;
- run public exponentiation;
- convert back.
With a message representative as input, these steps exercise the RSA encryption primitive. For signature verification, the same exponentiation is applied to the signature representative, and its output is checked against the signature scheme’s encoded message. The arithmetic is shared; the meaning and validation of its inputs differ.
Example: modular multiplication service
A Diffie-Hellman implementation may need only multiplication, squaring, and exponentiation modulo one prime. In that case, expose a field-specific wrapper rather than a generic arbitrary-modulus API. Fewer degrees of freedom means fewer invalid states.
Test harness skeleton
int main(void) {
test_add_edges();
test_sub_edges();
test_mul_against_vectors();
test_montgomery_vectors();
test_public_modexp_vectors();
return 0;
}
Every test name should correspond to an invariant from the articles: carry propagation, borrow relation, product bound, REDC cancellation, or exponentiation invariant.
Production boundary
This series is a design for an auditable primitive layer. Deployment adds explicit requirements for the supported platforms and threat model:
- validated C, intrinsics, or assembly for each supported target;
- continuous side-channel testing;
- fault and invalid-input policy;
- secure zeroization where secrets are stored;
- review of compiler output for arithmetic and leakage contracts;
- protocol-level validation above the arithmetic layer.
These checks connect the primitive-layer proofs to the compiled binary and to the protocol that uses it. Record their results alongside the supported compiler options and target platforms.
SageMath end-to-end check
p = 2^127 - 1
R = 2^(32*4)
x, y = 123456789, 987654321
print((x*y) % p == (x * R % p) * (y * R % p) * inverse_mod(R, p)^2 % p)
print(power_mod(x, 65537, p) == pow(x, 65537, p))
The final implementation should generate C vectors from this model and round-trip them through the compiled test binary.
