Bignum Arithmetic 08: Barrett and Pseudo-Mersenne Reduction
Bignum arithmetic notes / 08
Barrett and pseudo-Mersenne reduction
Montgomery reduction is not the only way to avoid division. Reciprocal and special-form reductions trade precomputation for structured arithmetic.
Barrett reduction fixes a modulus \(m\) and precomputes an approximation to \(1/m\). For radix \(B\) and \(k\) limbs, define
\[\mu=\left\lfloor\frac{B^{2k}}{m}\right\rfloor.\]For \(0\le x<B^{2k}\), one direct reciprocal variant takes \(\hat q=\lfloor x\mu/B^{2k}\rfloor\) and computes \(r=x-\hat q m\).
Barrett contract
Precondition: \(0<m<B^k\) and \(0\le x<B^{2k}\). Postcondition: output \(r\equiv x\pmod m\) and \(0\le r<m\) after correction.
For the direct variant above, \(0\le x/m-x\mu/B^{2k}<1\), because \(0\le B^{2k}/m-\mu<1\). Hence \(\hat q\) equals \(\lfloor x/m\rfloor\) or one less, giving \(0\le r<2m\). One conditional subtraction produces the canonical residue. Limb-truncated Barrett variants save multiplication work but require their own input-size assumptions and correction bounds.
Example: one fixed RSA public modulus
For repeated public reductions modulo a fixed RSA modulus, Barrett can be useful when operands are public or when a constant-time correction schedule is implemented. For private RSA exponentiation, Montgomery is usually preferable because every multiply is already inside the same odd modulus.
Pseudo-Mersenne primes
If
\[p=2^k-c\]with small \(c\), then \(2^k\equiv c\pmod p\). A high part can be folded into the low part by multiplying by \(c\).
Example: \(2^{255}-19\)
Write \(x=x_0+x_1 2^{255}\) with \(0\le x_0<2^{255}\). Modulo \(p=2^{255}-19\),
\[x\equiv x_0+19x_1\pmod p.\]For a product input \(0\le x<2^{510}\), put \(Q=2^{255}\). The first fold gives \(y=x_0+19x_1<20Q\). Splitting again as \(y=y_0+y_1Q\) with \(0\le y_0<Q\) gives \(y_1\le19\), so the second fold satisfies \(z=y_0+19y_1\le Q+360<2p\). One conditional subtraction then gives a canonical residue. The implementation retains the extra words needed for these intermediate values.
Example: \(2^{448}-2^{224}-1\)
Here \(2^{448}\equiv 2^{224}+1\pmod p\). Writing \(x=x_0+2^{448}x_1\) with \(0\le x_0<2^{448}\) gives \(x\equiv x_0+x_1+2^{224}x_1\pmod p\). A fold therefore adds the high part at bit offsets 0 and 224, with carries propagated from both contributions.
Barrett versus Montgomery
| Situation | Better default |
|---|---|
| Many products modulo one odd modulus | Montgomery |
| One-off public reduction without a precomputed reciprocal | Division |
| Special prime with sparse relation | pseudo-Mersenne/Solinas |
| Secret-dependent correction impossible to bound | redesign |
C sketch: pseudo-Mersenne fold
/* Conceptual fold for p = 2^255 - 19 and input below 2^510. */
void fold_25519(limb_t out[8], const limb_t in[16]) {
/* Split at bit 255 across eight 32-bit words, fold high bits by 19, then carry. */
(void)out;
(void)in;
}
The fold crosses a word boundary: bit 255 lies within the eighth 32-bit word. The high one-bit slice and the upper input words must be retained during the two folds. Each word multiplication by 19 uses a low/high word pair; additions and shifts then propagate its carry. After the second fold and conditional subtraction, the result fits in eight words. The displayed function is an outline of those steps, not an implementation.
SageMath reduction check
p = 2^255 - 19
for x in [0, 1, p-1, p, 2^510 - 1]:
x0 = x % 2^255
x1 = x // 2^255
print((x0 + 19*x1) % p == x % p)
