Barrett reduction

From testwiki
Jump to navigation Jump to search

Template:Short description

In modular arithmetic, Barrett reduction is an algorithm designed to optimize the calculation of amodn[1] without needing a fast division algorithm. It replaces divisions with multiplications, and can be used when n is constant and a<n2. It was introduced in 1986 by P.D. Barrett.[2]

Historically, for values a,b<n, one computed abmodn by applying Barrett reduction to the full product ab. In 2021, Becker et al. showed that the full product is unnecessary if we can perform precomputation on one of the operands.[3]

General idea

We call a function []: an integer approximation if |[z]z|1. For a modulus n and an integer approximation [], we define mod[]n:(/n) as

amod[]n=a[a/n]n.

Common choices of [] are floor, ceiling, and rounding functions.

Generally, Barrett multiplication starts by specifying two integer approximations []0,[]1 and computes a reasonably close approximation of abmodn as

ab[a[bRn]0R]1n,

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 [2] for the floor-function case []0=[]1=. The general case for b can be found in NTL.[4] 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.[3]

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=2k.

When calculating amodn 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/2k because division by 2k is just a right-shift, and so it is cheap.

In order to calculate the best value for m given 2k consider:

m2k=1nm=2kn

For m to be an integer, we need to round 2k/n somehow. Rounding to the nearest integer will give the best approximation but can result in m/2k being larger than 1/n, which can cause underflows. Thus m=2k/n 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/2k1/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 -= q * n
    if a >= n {
        a -= n
    }
    return a
}

Single-word Barrett multiplication

Suppose b is known. This allows us to precompute bRn before we receive a. Barrett multiplication computes ab, approximates the high part of ab with abRnRn, and subtracts the approximation. Since abRnRn is a multiple of n, the resulting value ababRnRn is a representative of abmodn.

Correspondence between Barrett and Montgomery multiplications

Recall that unsigned Montgomery multiplication computes a representative of abmodn as

a(bRmodn)+(a(bRmodn)n1modR)nR.

In fact, this value is equal to ababRnRn.

We prove the claim as follows.

ababRnRn=ababRn(abRnmodR)Rn=(abRnabRn+(abRnmodR))nR=(abRnabR(bRmodn)n+(abRnmodR))nR=(a(bRmodn)n+(abRnmodR))nR=(a(bRmodn)n+(a(bRmodn)n1modR))nR=a(bRmodn)+(a(bRmodn)n1modR)nR.

Generally, for integer approximations []0,[]1, we have

ab[a[bRn]0R]1n=a(bRmod[]0n)+(a(bRmod[]0q)n1mod[]1R)nR.[3]

Range of Barrett multiplication

We bound the output with ababRnRn=a(bRmodn)+(a(bRmodn)n1modR)nRan+RnR=n(1+aR).

Similar bounds hold for other kinds of integer approximation functions. For example, if we choose []0=[]1=, the rounding half up function, then we have

|ababRnRn|=|a(bRmod±n)+(a(bRmod±n)n1mod±R)nR||a|n2+R2nR=n2(1+|a|R).

It is common to select R such that aR<1 (or |a|R<1 in the []0=[]1=  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 [bRn]0 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[a[Rn]0R]1n=a(Rmod[]0n)+(a(Rmod[]0q)n1mod[]1R)nR

A quick check on the bounds yield the following in []0=[]1= case

aaRnRn=a(Rmodn)+(a(Rmodn)n1modR)nRa(Rmodn)+RnR=n(1+a(Rmodn)Rn)

and the following in []0=[]1= case

|aaRnRn|=|a(Rmod±n)+(a(Rmod±n)n1mod±R)nR||a(Rmod±n)|+R2nR=n2(1+|a(Rmod±n)|Rn)

Setting R>|a| will always yield one check on the output. However, a tighter constraint on R might be possible since Rmod[]0n is a constant that is sometimes significantly smaller than n.

A small issue arises with performing the following product a[Rn]0 since a is already a product of two operands. Assuming n fits in w bits, then a would fit in 2w bits and [Rn]0 would fit in w bits. Their product would require a 2w×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[[aR0]2[Rn]0R1]1n=a(Rmod[]0n)+(amod[]2R0)(RRmod[]0n)+([aR0]2[Rn]0mod[]1R1)R0nR

where R0=2kβ, R1=2α+β, R=R0R1=2k+α, and k is the bit-length of n.

Bound check in the case []0=[]1=[]2= yields the following

aaR0RnRna(Rmodn)+R0RR0(Rmodn)+RnR=n(1+a(Rmodn)Rn+R0nRmodnR1n)

and for the case []0=[]1=[]2= yields the following

|aaR0RnRn||a(Rmod±n)|+R0R/2+R0|(Rmod±n)|/2+Rn/2R=n2(1+2|a(Rmod±n)|Rn+R0n+|Rmod±n|R1n)

For any modulus and assuming |a|<2k+γ, the bound inside the parenthesis in both cases is less than or equal:

1+(2k+γ)(n)(2k+α)(n)+2kβn+ϵ1+2k+γ2k+α+2kβ2k1+ϵ=1+2γα+21β+ϵ where ϵ=1R1 in the case and ϵ=12R1 in the case.

Setting β=2 and α=γ+1 (or α=γ+2 in the case) will always yield one check. In some cases, testing the bounds might yield a lower α and/or β values.

Small Barrett reduction

It is possible to perform a Barrett reduction with one less multiplication as follows

a[aR]1n where R=2k and k is the bit-length of n

Every modulus can be written in the form n=2kc=Rc for some integer c.

a[aR]1n=aa(amod[]1R)Rn=aRan+(amod[]1R)nR=ac+(amod[]1R)nR=n(amod[]1RR+acRn)=n(amod[]1RR+aR2cR)

Therefore, reducing any a<R2cR for []1= or any |a|<(R2cR)/2 for []1= 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 [an] without performing expensive long division. Furthermore it can be used to compute [abn]. After pre-computing the constants, the steps are as follows:

1. Compute the approximate quotient q~=[a[bRn]0R]1

2. Compute the Barrett remainder r~=abq~n

3. Compute the quotient error e=(r~r)/n where r=amod[]n. This is done by subtracting a multiple of n to r~ until r is obtained.

4. Compute the quotient q=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 []0=[]1= and appropriate constraints, the error e can be obtained from the sign of 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.[5]

Barrett algorithm for polynomials

It is also possible to use Barrett algorithm for polynomial division, by reversing polynomials and using X-adic arithmetic.[6]

See also

References

Template:Reflist

Sources

Template:Refbegin

Template:Refend