Bignum Arithmetic 05: Division, Reduction, and Normalization
Bignum arithmetic notes / 05
Division, reduction, and normalization
General division is the most delicate classical bignum operation. Cryptographic code often avoids it in the hot path, but reduction by arbitrary moduli still requires understanding it.
Given integers \(u\ge 0\) and \(v>0\), division computes
\[u=qv+r,\qquad 0\le r<v.\]In limb algorithms, quotient estimation is performed from the most significant limbs after normalization.
Normalization
Normalize the divisor so that its top limb satisfies
\[B/2\le v_{m-1}<B.\]This can be achieved by left-shifting both dividend and divisor by the same bit count. Normalization improves quotient-digit estimates because the leading limb carries enough information.
Precondition for Knuth-style division
Let the current dividend window \(U=\sum_{i=0}^{m}u_{j+i}B^i\) satisfy \(0\le U<Bv\), so that its true quotient digit \(q=\lfloor U/v\rfloor\) lies in \([0,B)\). For divisor length \(m\ge 2\) and normalized top limb \(v_{m-1}\ge B/2\), use the clamped estimate
\[\hat q=\min\!\left(B-1,\left\lfloor\frac{u_{j+m}B+u_{j+m-1}}{v_{m-1}}\right\rfloor\right).\]Then \(q\le\hat q\le q+2\). Normalization makes this error bound independent of the number of limbs.
In the 32-bit-only model, the numerator \(u_{j+m}B+u_{j+m-1}\) is represented by two words. A quotient estimator must operate on those two words explicitly. This representation requirement applies to both public and secret inputs; secret inputs additionally require a fixed correction schedule.
Quotient correction
Starting from the estimate above, subtract \(\hat qv\) from the window. Each add-back of \(v\), accompanied by a decrement of \(\hat q\), corrects one unit of overestimation; at most two are needed. Knuth-style implementations first refine the estimate using the next divisor limb, leaving at most one add-back after the full multiply-subtract. These are distinct correction stages; Handbook of Applied Cryptography, Section 14.2.5 gives the normalized division algorithm and its correction bounds.
For secret operands, execute the proved maximum number of correction steps and select their effects with masks. A data-dependent number of corrections exposes information about the quotient estimate.
Example: decimal analogy
In base 10, take divisor \(v=59\) and window \(U=589<10v\). The top-limb estimate is \(\lfloor 58/5\rfloor=11\); clamping gives \(\hat q=9\), which equals \(\lfloor 589/59\rfloor\). The clamp enforces the digit range \([0,B)\) even when the leading-limb division returns \(B\) or more.
Example: modular reduction by an RSA modulus
A 4096-bit intermediate reduced modulo a 2048-bit RSA modulus can be handled by long division, but RSA private operations normally use Montgomery reduction because repeated divisions would dominate cost and may introduce variable-time correction behavior.
C shape for public division
/* Variable-time skeleton for public inputs only. */
int bn_div_public(limb_t *q, limb_t *r,
const limb_t *u, uint32_t un,
const limb_t *v, uint32_t vn) {
/* 1. reject v=0
2. normalize u and v
3. estimate quotient limbs from high words
4. multiply-subtract
5. correct overestimates
6. denormalize remainder */
(void)q; (void)r; (void)u; (void)un; (void)v; (void)vn;
return 0;
}
The function body lists the implementation stages and performs no division yet. Its public contract permits data-dependent quotient corrections; the later Montgomery routines provide the reduction structure used for secret modular products.
Reduction contract
For public modulus \(m\) and input \(x\), a reduction routine returns \(r\) such that
\[r\equiv x\pmod m, \qquad 0\le r<m.\]If the routine returns a lazy residue, its range must be explicit, for example \(0\le r<2m\).
SageMath quotient tests
cases = [(9876, 98), (2^4096 - 123, 2^2048 - 159), (2^256 + 17, 2^127 - 1)]
for u, v in cases:
q, r = u.quo_rem(v)
print(u == q*v + r)
print(0 <= r < v)
Use these values to generate known-answer tests for a variable-time public division implementation.
