Barrett reduction explained

In modular arithmetic, Barrett reduction is a reduction algorithm introduced in 1986 by P.D. Barrett.[1]

A naive way of computing

c=a\bmodn

would be to use a fast division algorithm. Barrett reduction is an algorithm designed to optimize this operation assuming

n

is constant, and

a<n2

, replacing divisions by multiplications.

Historically, for values

a,b<n

, one computed

ab\bmodn

by applyingBarrett reduction to the full product

ab

.Recently, it was shown that the full product is unnecessary if we can perform precomputation on one of the operands.[2]

General idea

We call a function

\left[\right]:R\toZ

an integer approximation if

|\left[z\right]-z|\leq1

.For a modulus

n

and an integer approximation

\left[\right]

, we define

mod\left[\right]n:Z\to(Z/nZ)

as

amod\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\bmodn

as

ab-\left[

a\left[
bR
n
\right]0
R

\right]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 [1] for the floor-function case

\left[\right]0=\left[\right]1=\lfloor\rfloor

.The general case for

b

can be found in NTL.[2] 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=2k

.

When calculating

a\bmodn

for unsigned integers, the obvious analog would be to use division by

n

:

func reduce(a uint) uint

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:
m
2k

=

1
n

\Longleftrightarrowm=

2k
n

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=\lfloor{2k}/{n}\rfloor

is used for unsigned arithmetic.

Thus we can approximate the function above with the following:

func reduce(a uint) uint

However, since

m/2k\le1/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

Single-word Barrett multiplication

Suppose

b

is known.This allows us to precompute

\left\lfloor

bR
n

\right\rfloor

before we receive

a

.Barrett multiplication computes

ab

, approximates the high part of

ab

with

\left\lfloor

a\left\lfloor
bR
n
\right\rfloor
R

\right\rfloorn

,and subtracts the approximation.Since

\left\lfloor

a\left\lfloor
bR
n
\right\rfloor
R

\right\rfloorn

is a multiple of

n

,the resulting value

ab-\left\lfloor

a\left\lfloor
bR
n
\right\rfloor
R

\right\rfloorn

is a representative of

ab\bmodn

.

Correspondence between Barrett and Montgomery multiplications

Recall that unsigned Montgomery multiplication computes a representative of

ab\bmodn

as
a\left(bR\bmodn\right)+\left(a\left(-bR\bmodn\right)n-1\bmodR\right)n
R

.

In fact, this value is equal to

ab-\left\lfloor

a\left\lfloor
bR
n
\right\rfloor
R

\right\rfloorn

.

We prove the claim as follows.

\begin{align} &&& ab-\left\lfloor

a\left\lfloor
bR
n
\right\rfloor
R

\right\rfloorn\\ &=&& ab-

a\left\lfloor
bR
n
\right\rfloor-\left(a\left\lfloor
bR
n
\right\rfloor\bmodR\right)
R

n\\ &=&& \left(

abR
n

-a\left\lfloor

bR
n

\right\rfloor+\left(a\left\lfloor

bR
n

\right\rfloor\bmodR\right)\right)

n
R

\\ &=&& \left(

abR
n

-a

bR-\left(bR\bmodn\right)
n

+\left(a\left\lfloor

bR
n

\right\rfloor\bmodR\right)\right)

n
R

\\ &=&& \left(

a\left(bR\bmodn\right)
n

+\left(a\left\lfloor

bR
n

\right\rfloor\bmodR\right)\right)

n
R

\\ &=&& \left(

a\left(bR\bmodn\right)
n

+\left(a\left(-bR\bmodn\right)n-1\bmodR\right)\right)

n
R

\\ &=&&

a\left(bR\bmodn\right)+\left(a\left(-bR\bmodn\right)n-1\bmodR\right)n
R

. \end{align}

Generally, for integer approximations

\left[\right]0,\left[\right]1

,we have

ab-\left[

a\left[
bR
n
\right]0
R

\right]1n =

a\left(bR
\left[\right]0
mod
n\right)+\left(a\left(-bR
\left[\right]0
mod
q\right)n-1
\left[\right]1
mod
R\right)n
R

.

Range of Barrett multiplication

We bound the output with

ab-\left\lfloor

a\left\lfloor
bR
n
\right\rfloor
R

\right\rfloorn =

a\left(bR\bmodn\right)+\left(a\left(-bR\bmodn\right)n-1\bmodR\right)n\leq
R
an+Rn
R

= n\left(1+

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|ab-\left\lfloor

a\left\lfloor
bR
n
\right\rceil
R

\right\rceiln\right| =\left|

a\left(bRmod\pmn\right)+\left(a\left(-bRmod\pmn\right)n-1mod\pmR\right)n
R

\right| \leq\left|

a
n
2
+
R
2
n
\right| =
R
n
2

\left(1+

|a|
R

\right).

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.[3]

Barrett algorithm for polynomials

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

See also

Sources

Notes and References

  1. Book: Barrett . P. . Implementing the Rivest Shamir and Adleman Public Key Encryption Algorithm on a Standard Digital Signal Processor . 10.1007/3-540-47721-7_24 . Advances in Cryptology – CRYPTO' 86 . Lecture Notes in Computer Science . 263 . 311–323 . 1986 . 978-3-540-18047-0 .
  2. Web site: Shoup . Victor. Number Theory Library.
  3. Book: Alfred . Menezes . Paul . Oorschot . Scott . Vanstone . Handbook of Applied Cryptography . 1997 . 0-8493-8523-7 . registration .
  4. Web site: Barrett reduction for polynomials . 2022-09-07 . www.corsix.org.