Barrett reduction
In modular arithmetic, Barrett reduction is an algorithm designed to optimize the calculation of a \,\bmod\, n \, without needing a fast division algorithm. It replaces divisions with multiplications, and can be used when n is constant and a . It was introduced in 1986 by P.D. Barrett. Historically, for values a, b , one computed a b \, \bmod\, n \, by applying Barrett reduction to the full product a b . In 2021, Becker et al. showed that the full product is unnecessary if we can perform precomputation on one of the operands. Source: e01 Encyclopedia. Licence: CC BY-SA 4.0.
In modular arithmetic, Barrett reduction is an algorithm designed to optimize the calculation of a \,\bmod\, n \, without needing a fast division algorithm. It replaces divisions with multiplications, and can be used when n is constant and a . It was introduced in 1986 by P.D. Barrett. Historically, for values a, b , one computed a b \, \bmod\, n \, by applying Barrett reduction to the full product a b . In 2021, Becker et al. showed that the full product is unnecessary if we can perform precomputation on one of the operands. a \, \text{mod}^{\left[\,\right]} \, n = a - \left[a / n\right] n . Common choices of \left[\,\right] are floor, ceiling, and rounding functions. Generally, Barrett multiplication starts by specifying two integer approximations \left[\,\right]_0, \left[\,\right]_1 and computes a reasonably close approximation of ab \, \bmod \, n as a b - \left[ \frac{a \, \left[ \frac{b R}{n} \right]_0 }{R} \right]_1 n , where R is a fixed constant, typically a power of 2, chosen so that multiplication and division by R can be performed efficiently. The case b = 1 was introduced by P.D. Barrett for the floor-function case \left[\,\right]_0 = \left[\,\right]_1 = \lfloor \, \rfloor . The general case for b can be found in NTL. The integer approximation view and the correspondence between Montgomery multiplication and Barrett multiplication was discovered by Hanno Becker, Vincent Hwang, Matthias J. Kannwischer, Bo-Yin Yang, and Shang-Yi Yang. We illustrate the idea for the floor-function case with b=1 and R=2^k . When calculating a \,\bmod\, n for unsigned integers, the obvious analog would be to use division by n : func reduce(a uint) uint { q := a / n // Division implicitly returns the floor of the result. return a - q * n } However, division can be expensive and, in cryptographic settings, might not be a constant-time instruction on some CPUs, subjecting the operation to a timing attack. Thus Barrett reduction approximates 1/n with a value m/2^k because division by 2^k is just a right-shift, and so it is cheap. In order to calculate the best value for m given 2^k consider: \frac{m}{2^k} = \frac{1}{n} \;\Longleftrightarrow\; m = \frac{2^k}{n} For m to be an integer, we need to round {2^k}/{n} somehow. Rounding to the nearest integer will give the best approximation but can result in m/2^k being larger than 1/n , which can cause underflows. Thus m = \lfloor {2^k}/{n} \rfloor is used for unsigned arithmetic. Thus we can approximate the function above with the following: func reduce(a uint) uint { q := (a * m) >> k // ">> k" denotes bitshift by k. return a - q * n } However, since m/2^k \le 1/n , the value of q in that function can end up being one too small, and thus a is only guaranteed to be within [0, 2n) rather than [0, n) as is generally required. A conditional subtraction will correct this: func reduce(a uint) uint { q := (a * m) >> k a := a - q * n if a >= n { a := a - n } return a } & = & & a b - \frac{a \left\lfloor \frac{bR}{n} \right\rfloor - \left( a \left\lfloor \frac{bR}{n} \right\rfloor \, \bmod \, R \right) }{R} \, n \\ & = & & \left( \frac{a b R}{n} - a \left\lfloor \frac{bR}{n} \right\rfloor + \left( a \left\lfloor \frac{bR}{n} \right\rfloor \, \bmod \, R \right) \right) \frac{n}{R} \\ & = & & \left( \frac{a b R}{n} - a \frac{bR - \left(b R \, \bmod \, n \right)}{n} + \left( a \left\lfloor \frac{bR}{n} \right\rfloor \, \bmod \, R \right) \right) \frac{n}{R} \\ & = & & \left( \frac{a \left(b R \, \bmod \, n \right)}{n} + \left( a \left\lfloor \frac{bR}{n} \right\rfloor \, \bmod \, R \right) \right) \frac{n}{R} \\ & = & & \left( \frac{a \left(b R \, \bmod \, n \right)}{n} + \left( a \left( - b R \, \bmod \, n \right) n^{-1} \, \bmod \, R \right) \right) \frac{n}{R} \\ & = & & \frac{a \left(b R \, \bmod \, n \right) + \left( a \left( - b R \, \bmod \, n \right) n^{-1} \, \bmod \, R \right) n}{R}. \end{align} Generally, for integer approximations \left[\,\right]_0, \left[\,\right]_1 , we have a b - \left[ \frac{a \, \left[ \frac{b R}{n} \right]_0 }{R} \right]_1 \, n = \frac{a \left( b R \, \text{mod}^{\left[\,\right]_0} \, n \right) + \left( a \left( - b R \, \text{mod}^{\left[\,\right]_0} \, q \right) n^{-1} \, \text{mod}^{\left[\,\right]_1} \, R \right) n}{R} . = \frac{a \left(b R \, \bmod \, n \right) + \left( a \left( - b R \, \bmod \, n \right) n^{-1} \, \bmod \, R \right) n}{R} \leq \frac{a n + R n}{R} = n \left(1 + \frac{a}{R}\right) . Similar bounds hold for other kinds of integer approximation functions. For example, if we choose \left[\,\right]_0 = \left[\,\right]_1 = \left\lfloor\,\right\rceil , the rounding half up function, then we have \left| a b - \left\lfloor \frac{a \left\lfloor \frac{b R}{n} \right\rceil}{R} \right\rceil \, n \right| = \left| \frac{a \left(b R \, \text{mod}^{\pm} \, n \right) + \left( a \left( - b R \, \text{mod}^{\pm} \, n \right) n^{-1} \, \text{mod}^{\pm} \, R \right) n}{R} \right| \leq \frac {R} \right). It is common to select R such that \frac{a}{R} (or \frac{\left|a\right|}{R} in the \left[\,\right]_{0}=\left[\,\right]_{1}=\left\lfloor \,\right\rceil case) so that the output remains within 0 and 2n ( -n and n resp.), and therefore only one check is performed to obtain the final result between 0 and n . Furthermore, one can skip the check and perform it once at the end of an algorithm at the expense of larger inputs to the field arithmetic operations. a-\left[\frac{a\,\left[\frac{R}{n}\right]_{0}}{R}\right]_{1}\,n=\frac{a\left(R\,\text{mod}^{\left[\,\right]_{0}}\,n\right)+\left(a\left(-R\,\text{mod}^{\left[\,\right]_{0}}\,q\right)n^{-1}\,\text{mod}^{\left[\,\right]_{1}}\,R\right)n}{R} A quick check on the bounds yield the following in \left[\,\right]_{0}=\left[\,\right]_{1}=\left\lfloor \,\right\rfloor case \begin{align} a-\left\lfloor \frac{a\,\left\lfloor \frac{R}{n}\right\rfloor }{R}\right\rfloor \,n &=\frac{a\left(R\,\bmod\,n\right)+\left(a\left(-R\,\bmod\,n\right)n^{-1}\,\bmod\,R\right)n}{R} \\ &\leq\frac{a(R\,\bmod\,n)+Rn}{R}=n\left(1+\frac{a(R\,\bmod\,n)}{Rn}\right) \end{align} and the following in \left[\,\right]_{0}=\left[\,\right]_{1}=\left\lfloor \,\right\rceil case \begin{align} \left|a-\left\lfloor \frac{a\left\lfloor \frac{R}{n}\right\rceil }{R}\right\rceil \,n\right| &=\left|\frac{a\left(R\,\text{mod}^{\pm}\,n\right)+\left(a\left(-R\,\text{mod}^{\pm}\,n\right)n^{-1}\,\text{mod}^{\pm}\,R\right)n}{R}\right|\\ &\leq\frac {Rn}\right) \end{align} Setting R>|a| will always yield one check on the output. However, a tighter constraint on R might be possible since R\,\text{mod}^{\left[\,\right]_{0}}\,n is a constant that is sometimes significantly smaller than n . A small issue arises with performing the following product a\,\left[\frac{R}{n}\right]_{0} since a is already a product of two operands. Assuming n fits in w bits, then a would fit in 2w bits and \left[\frac{R}{n}\right]_{0} would fit in w bits. Their product would require a 2w\times w multiplication which might require fragmenting in systems that cannot perform the product in one operation. An alternative approach is to perform the following Barrett reduction: a-\left[\frac{\left[\frac{a}{R_{0}}\right]_{2}\,\left[\frac{R}{n}\right]_{0}}{R_{1}}\right]_{1}\,n =\frac{ a\left(R\,\text{mod}^{\left[\,\right]_{0}}\,n\right)+ \left(a\,\text{mod}^{\left[\,\right]_{2}}\,R_{0}\right)\left(R-R\,\text{mod}^{\left[\,\right]_{0}}\,n\right) + \left(\left[\frac{a}{R_{0}}\right]_{2}\,\left[\frac{R}{n}\right]_{0}\,\text{mod}^{\left[\,\right]_{1}}\,R_{1}\right)R_{0}n }{R} where R_{0}=2^{k-\beta} , R_1=2^{\alpha+\beta} , R=R_{0}\cdot R_{1}=2^{k+\alpha} , and k is the bit-length of n . Bound check in the case \left[\,\right]_{0}=\left[\,\right]_{1}=\left[\,\right]_{2}=\left\lfloor \,\right\rfloor yields the following \begin{align} a-\left\lfloor \frac{\left\lfloor \frac{a}{R_{0}}\right\rfloor \,\left\lfloor \frac{R}{n}\right\rfloor }{R_{1}}\right\rfloor \,n&\le\frac{a\left(R\,\bmod\,n\right)+R_{0}R-R_{0}\left(R\,\bmod\,n\right)+Rn}{R}\\ &=n\left(1+\frac{a(R\,\bmod\,n)}{Rn}+\frac{R_{0}}{n}-\frac{R\,\bmod\,n}{R_{1}n}\right) \end{align} and for the case \left[\,\right]_{0}=\left[\,\right]_{1}=\left[\,\right]_{2}=\left\lfloor \,\right\rceil yields the following \begin{align} \left|a-\left\lfloor \frac{\left\lfloor \frac{a}{R_{0}}\right\rceil \left\lfloor \frac{R}{n}\right\rceil }{R_{1}}\right\rceil \,n\right| &\le\frac {Rn}+\frac{R_{0}}{n}+\frac {R_{1}n}\right) \end{align} For any modulus and assuming |a| , the bound inside the parenthesis in both cases is less than or equal: 1+\frac{(2^{k+\gamma})(n)}{(2^{k+\alpha})(n)}+\frac{2^{k-\beta}}{n}+\epsilon\le1+\frac{2^{k+\gamma}}{2^{k+\alpha}}+\frac{2^{k-\beta}}{2^{k-1}}+\epsilon=1+2^{\gamma-\alpha}+2^{1-\beta}+\epsilon where \epsilon=-\frac{1}{R_{1}} in the \left\lfloor \,\right\rfloor case and \epsilon=\frac{1}{2R_{1}} in the \left\lfloor \,\right\rceil case. Setting \beta=2 and \alpha=\gamma+1 (or \alpha=\gamma+2 in the \left\lfloor \,\right\rceil case) will always yield one check. In some cases, testing the bounds might yield a lower \alpha and/or \beta values. a-\left[\frac{a}{R}\right]_{1}\,n where R=2^{k} and k is the bit-length of n Every modulus can be written in the form n=2^{k}-c=R-c for some integer c . \begin{align} a-\left[\frac{a}{R}\right]_{1}\,n&=a-\frac{a-(a\,\text{mod}^{\left[\,\right]_{1}}\,R)}{R}n \\ &=\frac{aR-an+(a\,\text{mod}^{\left[\,\right]_{1}}\,R)n}{R} \\ &=\frac{ac+(a\,\text{mod}^{\left[\,\right]_{1}}\,R)n}{R}\\ &=n\left(\frac{a\,\text{mod}^{\left[\,\right]_{1}}\,R}{R}+\frac{ac}{Rn}\right)\\ &=n\left(\frac{a\,\text{mod}^{\left[\,\right]_{1}}\,R}{R}+\frac{a}{\frac{R^{2}}{c}-R}\right) \end{align} Therefore, reducing any a for \left[\,\right]_{1}=\left\lfloor \,\right\rfloor or any |a| for \left[\,\right]_{1}=\left\lfloor \,\right\rceil yields one check. From the analysis of the constraint, it can be observed that the bound of a is larger when c is smaller. In other words, the bound is larger when n is closer to R . Compute the approximate quotient \tilde{q}=\left[\frac{a\,\left[\frac{bR}{n}\right]_{0}}{R}\right]_{1} . Compute the Barrett remainder \tilde{r}=ab-\tilde{q}n . Compute the quotient error e=(\tilde{r}-r)/n where r=a\,\text{mod}^{\left[\,\right]}\,n . This is done by subtracting a multiple of n to \tilde{r} until r is obtained. Compute the quotient q=\tilde{q}+e . If the constraints for the Barrett reduction are chosen such that there is one check, then the absolute value of e in step 3 cannot be more than 1. Using \left[\,\right]_{0}=\left[\,\right]_{1}=\left\lfloor \,\right\rceil and appropriate constraints, the error e can be obtained from the sign of \tilde{r} .
Cite this
"Barrett reduction". 1763002273. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1321875534 JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy