Contents
Barrett reduction
In modular arithmetic, Barrett reduction is a reduction algorithm introduced in 1986 by P.D. Barrett. A naive way of computing would be to use a fast division algorithm. Barrett reduction is an algorithm designed to optimize this operation assuming n is constant, and a<n^2, replacing divisions by multiplications. Historically, for values a, b < n, one computed by applying Barrett reduction to the full product a b. Recently, it was shown that the full product is unnecessary if we can perform precomputation on one of the operands.
General idea
We call a function an integer approximation if. For a modulus n and an integer approximation , we define as Common choices of are floor, ceiling, and rounding functions. Generally, Barrett multiplication starts by specifying two integer approximations and computes a reasonably close approximation of as 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. 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.
Single-word Barrett reduction
Barrett initially considered an integer version of the above algorithm when the values fit into machine words. We illustrate the idea for the floor-function case with b=1 and R=2^k. When calculating for unsigned integers, the obvious analog would be to use division by 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: 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 is used for unsigned arithmetic. Thus we can approximate the function above with the following: However, since, the value of in that function can end up being one too small, and thus is only guaranteed to be within [0, 2n) rather than [0, n) as is generally required. A conditional subtraction will correct this:
Single-word Barrett multiplication
Suppose b is known. This allows us to precompute before we receive a. Barrett multiplication computes a b, approximates the high part of a b with , and subtracts the approximation. Since is a multiple of n, the resulting value is a representative of.
Correspondence between Barrett and Montgomery multiplications
Recall that unsigned Montgomery multiplication computes a representative of as In fact, this value is equal to. We prove the claim as follows. Generally, for integer approximations , we have
Range of Barrett multiplication
We bound the output with . Similar bounds hold for other kinds of integer approximation functions. For example, if we choose, the rounding half up function, then we have It is common to select R such that (or in the 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.
Barrett multiplication non-constant operands
The Barrett multiplication previously described requires a constant operand b to pre-compute ahead of time. Otherwise, the operation is not efficient. It is common to use Montgomery multiplication when both operands are non-constant as it has better performance. However, Montgomery multiplication requires a conversion to and from Montgomery domain which means it is expensive when a few modular multiplications are needed. To perform Barrett multiplication with non-constant operands, one can set a as the product of the operands and set b to 1. This leads to A quick check on the bounds yield the following in case and the following in case Setting R>|a| will always yield one check on the output. However, a tighter constraint on R might be possible since is a constant that is sometimes significantly smaller than n. A small issue arises with performing the following product since a is already a product of two operands. Assuming n fits in w bits, then a would fit in 2w bits and 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: where, , , and k is the bit-length of n. Bound check in the case yields the following and for the case yields the following For any modulus and assuming, the bound inside the parenthesis in both cases is less than or equal: where in the case and in the case. Setting \beta=2 and (or in the case) will always yield one check. In some cases, testing the bounds might yield a lower \alpha and/or \beta values.
Small Barrett reduction
It is possible to perform a Barrett reduction with one less multiplication as follows where R=2^{k} and k is the bit-length of n Every modulus can be written in the form for some integer c. Therefore, reducing any for or any for 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.
Barrett Division
Barrett reduction can be used to compute floor, round or ceil division without performing expensive long division. Furthermore it can be used to compute. After pre-computing the constants, the steps are as follows:
- Compute the approximate quotient
- Compute the Barrett remainder
- Compute the quotient error where. This is done by subtracting a multiple of n to \tilde{r} until r is obtained.
- Compute the quotient 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 and appropriate constraints, the error e can be obtained from the sign of \tilde{r}.
Multi-word Barrett reduction
Barrett's primary motivation for considering reduction was the implementation of RSA, where the values in question will almost certainly exceed the size of a machine word. In this situation, Barrett provided an algorithm that approximates the single-word version above but for multi-word values. For details see section 14.3.3 of the Handbook of Applied Cryptography.
Barrett algorithm for polynomials
It is also possible to use Barrett algorithm for polynomial division, by reversing polynomials and using X-adic arithmetic.
Sources
This article is derived from Wikipedia and licensed under CC BY-SA 4.0. View the original article.
Wikipedia® is a registered trademark of the
Wikimedia Foundation, Inc.
Bliptext is not
affiliated with or endorsed by Wikipedia or the
Wikimedia Foundation.