Bignum arithmetic notes / 02

Addition, subtraction, and comparison

The basic loops are short, but their contracts are the foundation for every modular routine above them.

For two \(n\)-limb inputs, the mathematical sum is

\[s=\operatorname{val}_B(a)+\operatorname{val}_B(b)=\sum_{i=0}^{n-1}(a_i+b_i)B^i.\]

The coefficient \(a_i+b_i\) may exceed \(B-1\), so the output digits are obtained by carry propagation.

The returned carry \(c_n\) records whether the full mathematical sum exceeds \(B^n-1\).

Addition contract

Precondition: a, b, and r have length n; each input limb is in \([0,B)\). Arrays may be disjoint or have the same starting address; partial overlap is excluded. With exact aliasing, each iteration reads the required input limbs before writing its result. Postcondition:

\[\operatorname{val}(r)+cB^n=\operatorname{val}(a)+\operatorname{val}(b), \qquad c\in\{0,1\}.\]
#include <stdint.h>

typedef uint32_t limb_t;

uint32_t bn_add_n(limb_t *r, const limb_t *a, const limb_t *b, uint32_t n) {
    uint32_t carry = 0;
    for (uint32_t i = 0; i < n; i++) {
        limb_t s = a[i] + b[i];
        uint32_t c1 = (s < a[i]);
        limb_t t = s + carry;
        uint32_t c2 = (t < s);
        r[i] = t;
        carry = c1 | c2;
    }
    return carry;
}

The comparison results depend on the operands and may therefore be secret. The C source uses them as carry bits without conditional branches. Constant-time verification checks that the generated instructions preserve this control-flow and memory-access pattern.

Loop invariant

After iteration \(i\),

\[\sum_{j=0}^{i}r_jB^j+c_{i+1}B^{i+1} =\sum_{j=0}^{i}(a_j+b_j)B^j.\]

The single-limb addition has maximum \((B-1)+(B-1)+1=2B-1\), so the outgoing carry is either 0 or 1. The two overflow flags cannot both be set: if a[i]+b[i] overflows, its low word is at most \(B-2\), and adding the incoming carry cannot overflow again. Hence c1 | c2 is the total carry.

Subtraction contract

Subtraction has the same length and aliasing preconditions. It computes a - b modulo \(B^n\) and returns the final borrow. Postcondition:

\[\operatorname{val}(r)=\operatorname{val}(a)-\operatorname{val}(b)+\beta B^n, \qquad \beta\in\{0,1\}.\]

Here \(\beta=1\) means a < b and the result is the two’s-complement residue modulo \(B^n\).

uint32_t bn_sub_n(limb_t *r, const limb_t *a, const limb_t *b, uint32_t n) {
    uint32_t borrow = 0;
    for (uint32_t i = 0; i < n; i++) {
        limb_t bi = b[i] + borrow;
        uint32_t bcarry = (bi < b[i]);
        uint32_t need = (a[i] < bi) | bcarry;
        r[i] = a[i] - bi;
        borrow = need;
    }
    return borrow;
}

Constant-time comparison as subtraction

A lexicographic comparison from the most significant limb often branches early. That is acceptable only when inputs are public. For secret residues, compute a-b and use the final borrow.

Example: all-limb carry

Let \(n=3\) and \(a=(B-1,B-1,B-1)\), \(b=(1,0,0)\). The result is \(r=(0,0,0)\) and carry \(1\). Every limb participates, so this is a mandatory edge-case test.

Example: conditional subtraction after modular addition

If \(0\le a,b<m<B^n\), then \(s=a+b<2m<2B^n\). Compute the \(n\) low limbs and the carry from the addition, then subtract \(m\) from the low limbs. Keep the subtracted value if either the addition carried out of limb \(n-1\) or the subtraction did not borrow; otherwise keep the original low limbs. The carry case matters because the low limbs alone may look smaller than \(m\) even when the mathematical sum exceeded \(B^n\).

Masked select

static inline limb_t ct_select_limb(limb_t x, limb_t y, uint32_t mask) {
    return (limb_t)((x & mask) | (y & ~mask));
}

void bn_cmov(limb_t *r, const limb_t *x, const limb_t *y, uint32_t n, uint32_t bit) {
    uint32_t mask = 0u - bit; /* all ones if bit=1, else zero */
    for (uint32_t i = 0; i < n; i++) r[i] = ct_select_limb(x[i], y[i], mask);
}

The arrays in bn_cmov obey the same length and exact-aliasing contract as bn_add_n; partial overlap is excluded. The expression 0u - bit uses defined unsigned arithmetic. The precondition bit in {0,1} makes the resulting 32-bit mask exactly all-zero or all-one; other values do not implement a full-word selection.

SageMath reference values

This calculation checks the limb conversion of wrapped sums. To test bn_add_n, the harness compares its output limbs and returned carry with the exact sum and its quotient by \(B^n\).

def limbs(x, n, w):
    B = 2^w
    return [(x // B^i) % B for i in range(n)]

def value(a, w):
    B = 2^w
    return sum(a[i] * B^i for i in range(len(a)))

w, n = 32, 8
B = 2^w
cases = [(B^n-1, 1), (B^2+5, B^2+7), (0, B^n-1)]
for x, y in cases:
    print(value(limbs((x + y) % B^n, n, w), w) == (x + y) % B^n)