Barrett Reduction
August 25, 20264 min readbeginner
Barrett's technique, from 1986, is the most direct of the three. It replaces division by a multiplication by a precomputed approximation of 1/q.
Barrett's technique, from 1986, is the most direct of the three. It replaces division by a multiplication by a precomputed approximation of .
01.The idea
Integers cannot hold . But they can hold scaled up.
Precompute
for a fixed with . Think of as shifted left by bits and truncated.
For ML-KEM's modulus, taking :
a 15-bit constant that fits in one multiplier input.
Now for any , the quantity is approximately , because . So
is approximately , and the error is at most one. Then lands in , and one conditional subtraction finishes it.
Dividing by is a right shift, which is free.
02.The algorithm
Given :
B1. Compute the wide product .
B2. Let . In hardware this is "take the top bits of the product", which is wiring rather than logic.
B3. Compute . Because underestimates by at most one, .
B4. If , subtract . Return .
Three multiplications' worth of work, one shift, one subtract, one conditional subtract. No division.
03.A worked example
Take , , , and reduce , which is inside .
B1. .
B2. .
B3. .
B4. , so no subtraction. Return .
Check directly: , so the true quotient is and the true remainder is . Barrett agreed, using one wide multiply, one shift, one narrow multiply and two subtractions.
Notice that was exactly the true quotient here. The guarantee is only that it is within one of it, and when it is one too small, step B4 does the correcting.
04.A correction to the source material
The LaTeX manuscript this chapter was migrated from gives . That value is wrong. The correct floor is , since
The error is worth recording rather than quietly fixing, because of how it hid. With the worked example above still produces and the correct answer , so checking the example would not have caught it.
It is not harmless. Barrett's correctness rests on never overestimating , which is what keeps in and makes a single conditional subtraction sufficient. An that is one too large breaks that guarantee for some inputs. Sampling the input range with the inflated constant finds inputs where falls outside , meaning one conditional subtraction is no longer enough and the routine returns a value that is not reduced.
The general lesson is one this chapter is a good place for. A constant that is off by one produces correct answers on most inputs and wrong ones on a few, so testing with a handful of values proves nothing. Constants like this need to be derived, not typed, and verified across the full input range.
05.Hardware cost
One wide multiplication , which on an FPGA is two DSP slices chained. One right shift, which is wires. One narrower multiplication . One subtraction, one conditional subtraction. At a typical 200 MHz fabric clock the whole thing fits in about four pipeline stages.
The one hazard is step B4. Written as a branch, it takes an extra cycle when the subtraction is needed and not otherwise, which is a timing channel on secret data.
The fix is standard and must be applied: compute both and , then select between them using the sign bit of the subtraction as a mask. Both paths always execute, the selection is arithmetic rather than control flow, and the timing is identical regardless of the value. Every constant-time implementation in this chapter ends with the same pattern.