Bignum arithmetic notes / 06

Modular add, subtract, and reduction

Input bounds determine how many additions or subtractions of the modulus are needed. Each routine preserves a specified value relation and output interval.

Fix a modulus \(m\) with \(0<m<B^n\). The addition and subtraction routines in this article do not require \(m\) to be odd. A canonical residue satisfies \(0\le x<m\). A lazy residue may satisfy a wider bound such as \(0\le x<2m\).

Modular addition

For canonical inputs \(a,b<m\),

\[0\le a+b<2m.\]

So one subtraction of \(m\) suffices to canonicalize.

#define BN_MAX_LIMBS 256

void bn_mod_add(limb_t *r, const limb_t *a, const limb_t *b,
                const limb_t *m, uint32_t n) {
    limb_t tmp[BN_MAX_LIMBS];
    uint32_t carry = bn_add_n(r, a, b, n);
    uint32_t borrow = bn_sub_n(tmp, r, m, n);
    uint32_t use_tmp = carry | (borrow ^ 1u);
    bn_cmov(r, tmp, r, n, use_tmp);
}

This snippet requires \(1\le n\le\texttt{BN\_MAX\_LIMBS}\), with arrays of at least n limbs. The output may exactly alias a or b, but partial overlap is excluded and the modulus must not overlap the output. These preconditions ensure that adding into r leaves the modulus intact for the subtraction.

Modular subtraction

If \(a,b<m\), compute \(a-b\). If it borrows, add \(m\) back.

\[r=\begin{cases} a-b,&a\ge b,\\ a-b+m,&a<b. \end{cases}\]

This returns a canonical residue because \(-(m-1)\le a-b\le m-1\). When subtraction borrows, its stored value is \(a-b+B^n\). Adding \(m\) produces \(B^n+(a-b+m)\), so the low \(n\) limbs contain the canonical result and the outgoing carry is discarded.

Constant-time conditional subtraction

For \(0\le x<2m\) already stored in \(n\) limbs, canonicalization computes \(x-m\) and keeps it if no borrow occurred. The range bound is what makes one subtraction sufficient.

#define BN_MAX_LIMBS 256

void bn_cond_sub(limb_t *x, const limb_t *m, uint32_t n) {
    limb_t tmp[BN_MAX_LIMBS];
    uint32_t borrow = bn_sub_n(tmp, x, m, n);
    bn_cmov(x, tmp, x, n, borrow ^ 1u);
}

The same public length bound applies to bn_cond_sub, and its modulus array must not overlap the writable array x. A checked API establishes these conditions before either routine writes its scratch buffer.

This helper assumes the value to canonicalize is already represented in exactly n limbs. If an operation also returns a carry or high limb, that extra bit must be part of the selection condition; otherwise the low limbs may appear below m while the mathematical value is still at least \(B^n\).

One-subtraction proof

If \(0\le a,b<m\), then \(0\le s=a+b<2m\). Mathematically, \(s\) is either already in \([0,m)\) or \(s-m\) is. In an \(n\)-limb implementation, compute the low limbs of \(s\) and the carry bit. Select the subtracted result when the carry is 1 or when subtracting \(m\) from the low limbs does not borrow. This implements the same one-subtraction proof even when \(s\ge B^n\).

Example: the high carry determines reduction

Let \(m=B^n-17\) and \(a=b=m-1\). Then \(a+b=2B^n-36\). The low \(n\) limbs are \(B^n-36\) and the carry is 1. The carry must force selection of the subtracted result, because the mathematical sum exceeded \(B^n\) even though the low limbs may appear below \(m\).

Example: lazy range in elliptic-curve formulas

A field implementation may allow \(0\le x<2p\) between additions to save reductions. If a later multiplication assumes canonical input, the interface is broken. A lazy range is part of the interface contract. For moduli close to \(B^n\), representing every value below \(2m\) also requires an extra carry bit or a wider array.

SageMath range test

B = 2^32
n = 4
m = B^n - 189
for a in [0, 1, m-1, m//2]:
    for b in [0, 2, m-1, m//3]:
        s = a + b
        r = s - m if s >= m else s
        print(0 <= r < m and r == (a + b) % m)