Bignum arithmetic notes / 11

Testing, verification, and fuzzing

Bignum tests compare the implementation with exact arithmetic. Boundary inputs stress carry propagation and accumulator bounds; random inputs explore combinations beyond those cases.

Testing has two layers: algebraic equality against a reference model and implementation hygiene under compilers and sanitizers.

Reference model

SageMath integers are exact. Use them to define the value relation for each C routine.

def limbs(x, n, w):
    B = 2^w
    return [(Integer(x) >> (w*i)) & (B-1) for i in range(n)]

def value(a, w):
    B = 2^w
    return sum(Integer(ai) * B^i for i, ai in enumerate(a))

Mandatory edge cases

Case Why it matters
zero and one identity behavior
all limbs 0xffff...ffff carry across every limb
alternating bits catches endian and shift mistakes
modulus minus one canonical boundary
product of maximal operands accumulator bound
noncanonical residue input validation policy

Example: addition vectors

w, n = 32, 8
B = 2^w
cases = [
    (0, 0),
    (B^n - 1, 1),
    (B^n - 1, B^n - 1),
    (sum(B^i for i in range(n)), sum((B-1)*B^i for i in range(n))),
]
for a, b in cases:
    s = a + b
    print(limbs(a, n, w), limbs(b, n, w), limbs(s % B^n, n, w), s // B^n)

Example: Montgomery vectors

w, n = 32, 4
B, R = 2^w, 2^(w*n)
m = 2^127 - 1
mp = (-inverse_mod(m, B)) % B
for x, y in [(1, 1), (2, 3), (m-1, m-1), (123456789, 987654321)]:
    xt, yt = x*R % m, y*R % m
    zt = xt*yt*inverse_mod(R, m) % m
    print(zt == x*y*R % m)

P-256 vector shape

Because the teaching model uses 32-bit words, P-256 vectors are emitted as eight little-endian words:

p = 2^256 - 2^224 + 2^192 + 2^96 - 1
def words(x, n=8):
    return [hex((Integer(x) >> (32*i)) & 0xffffffff) for i in range(n)]
for x, y in [(1, 2), (p-2, p-3)]:
    print(words((x*y) % p))

This keeps the article vectors and the P-256 C harness in the same representation.

Differential testing loop

A C test harness can print hex limbs; SageMath can parse them and compare. For faster iteration, Python’s built-in int is enough for many tests, while SageMath is useful for finite-field and modular inverse checks.

Sanitizers and warnings

Compile test builds with flags such as:

cc -std=c11 -Wall -Wextra -Wconversion -Wshadow -fsanitize=undefined,address test_bn.c

Unsigned wraparound is defined and will not be reported as undefined behavior. Treat detected signed overflow, invalid shifts, out-of-bounds accesses, and uninitialized reads as failures. The displayed sanitizer pair does not detect every uninitialized read; that check requires suitable additional compiler diagnostics or a memory-initialization sanitizer.

Proof audit notes

Every test file should identify the invariant it stresses. For the multiplier in Article 03, all-one limbs exercise two-word products and long carry chains. Every partial accumulator is a sum of a subset of nonnegative product contributions, so

\[0\le A_{\mathrm{partial}}\le \operatorname{val}(a)\operatorname{val}(b)<B^{2n}.\]

Thus the extra scratch limb must remain zero. A row-wise multiply-add implementation has a different local bound: if \(0\le r_{i+j},c<B\), then \(a_i b_j+r_{i+j}+c\le B^2-1\).

Tests do not replace the proof, but they catch mismatches between the proof’s algorithm and the code that was actually written.