Bignum Arithmetic 00: Roadmap for Cryptographic Integers
Bignum arithmetic notes / 00
Roadmap for cryptographic integers
Public-key implementations use arrays of machine words to represent large integers. Each arithmetic routine needs a value relation, an intermediate-value bound, and a stated memory-access pattern.
These notes fix the implementation target before the algorithms: C with fixed-width unsigned integer types no wider than 32 bits. The teaching model uses little-endian uint32_t words with mathematical radix \(B=2^{32}\). Because a 32-by-32 product cannot be stored in one C object under this rule, multiplication decomposes each word into 16-bit halves and accumulates the resulting two-word product explicitly. SageMath remains the executable specification. The intended caller is cryptographic code: RSA exponentiation, finite-field Diffie-Hellman, elliptic-curve scalar multiplication, and support arithmetic for post-quantum implementations.
The companion elliptic arithmetic series starts where this series ends: prime-field operations become point operations, and Montgomery/lazy range contracts become preconditions for curve formulas.
The bignum layer represents nonnegative integers with fixed-length limb arrays, implements arithmetic with stated bounds, and provides constant-time routines for secret operands.
Global representation
Let \(w=32\) be the teaching word width and \(B=2^w\). For an array \(a=(a_0,\ldots,a_{n-1})\) with \(0\le a_i<B\), define
\[\operatorname{val}_B(a)=\sum_{i=0}^{n-1} a_iB^i.\]The value map fixes the meaning of every array position. We choose little-endian limb order so that carry propagation follows increasing array indices; byte order within each machine word is a separate representation choice.
Core invariants
- Every limb has type
uint32_tand stores a value in \([0,B)\) with \(B=2^{32}\). - A routine returning \(n\) limbs states whether the value is exact, reduced modulo a specified modulus, or in an explicitly bounded lazy range.
- A routine that may overflow \(n\) limbs returns the carry, borrow, or high limb explicitly.
- Constant-time routines keep control flow and memory addresses independent of secrets; the timing of the generated instructions must also satisfy the target’s leakage model.
Route through the series
| Articles | Purpose |
|---|---|
| 01-02 | Define limbs, unsigned C arithmetic, comparison, addition, subtraction, and masks. |
| 03-04 | Build multiplication and squaring from convolution with accumulator bounds. |
| 05-08 | Turn integer arithmetic into reduction algorithms: division, modular add/sub, Montgomery, Barrett, and pseudo-Mersenne. |
| 09-10 | Use modular multiplication for exponentiation and prime-field arithmetic. |
| 11-13 | Test, fuzz, harden, and assemble a minimal cryptographic bignum API. |
The algorithms build on these contracts. The limb value map and carry invariant establish Montgomery reduction’s cancellation steps; conditional moves and full-table selection provide the operations needed by constant-time exponentiation.
Example: RSA modulus arithmetic
For a 2048-bit RSA modulus under this 32-bit teaching model, choose \(w=32\) and \(n=64\). A residue is an array of 64 words. A product before reduction has at most 128 words because
\[0\le ab < m^2 < B^{128}.\]The product-then-REDC implementation used here allocates \(2n+1\) words: \(2n\) for the product and one for reduction carries. Its multiplication proof represents each 32-by-32 product as two uint32_t words. Integrated Montgomery schedules can use different scratch sizes.
Example: a 255-bit prime field
For \(p=2^{255}-19\) under this 32-bit teaching model, a uniform representation uses eight uint32_t words, with the top bit unused for canonical residues. The fold by 19 must still be implemented without a wider scalar product: use the two-word product helper for multiplication by 19, retain its high word during carry propagation, and establish the range after each fold.
Example: P-256 word model
For NIST P-256, the same teaching model gives eight little-endian uint32_t words and mathematical radix \(B=2^{32}\). One word product is not held in a wider type; it is written as four 16-by-16 products and then accumulated with explicit carries. The proof obligations at the word-product boundary are discharged by the half-word product lemma in the multiplication article.
SageMath as oracle
SageMath is used to generate exact expected values, not to justify C behavior. C behavior must be proved from the C type rules and word bounds.
B = 2^32
limbs = [0xffffffff, 0, 7]
x = sum(limbs[i] * B^i for i in range(len(limbs)))
print(x == 7*B^2 + B - 1)
Leakage contract
The constant-time contract requires branch decisions and memory addresses to be independent of secret values. A secret exponent controlling a branch or a secret limb controlling an array index violates that contract. Operand-dependent instruction timing must be checked on the supported targets as well. Early rejection of public invalid data is compatible with this contract; a secret-dependent rejection condition belongs in the stated leakage interface.
