ArticleslgStudy

computer science

Barrett reduction

Barrett reduction is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Barrett reduction rather than just read about it. In short: In modular arithmetic, Barrett reduction is an algorithm designed to optimize the calculation of amodn without needing a fast division algorithm. It replaces divisions with multiplications, and can be used when n is constant and a<n2.

Key takeaways

  • Barrett reduction belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Barrett reduction to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Barrett reduction from memory before moving on to harder problems.

Reference excerpt

In modular arithmetic, Barrett reduction is an algorithm designed to optimize the calculation of amodn 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. 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.

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 for the floor-function case []0=[]1=⌊⌋. 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=2k. When calculating amodn 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/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=1n⟺m=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:

However, since m/2k≤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:

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 ⌊a⌊bRn⌋R⌋n, and subtracts the approximation. Since ⌊a⌊bRn⌋R⌋n is a multiple of n, the resulting value ab−⌊a⌊bRn⌋R⌋n 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)n−1modR)nR. In fact, this value is equal to ab−⌊a⌊bRn⌋R⌋n. We prove the claim as follows.

ab−⌊a⌊bRn⌋R⌋n=ab−a⌊bRn⌋−(a⌊bRn⌋modR)Rn=(abRn−a⌊bRn⌋+(a⌊bRn⌋modR))nR=(abRn−abR−(bRmodn)n+(a⌊bRn⌋modR))nR=(a(bRmodn)n+(a⌊bRn⌋modR))nR=(a(bRmodn)n+(a(−bRmodn)n−1modR))nR=a(bRmodn)+(a(−bRmodn)n−1modR)nR. Generally, for integer approximations []0,[]1, we have

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

Range of Barrett multiplication We bound the output with

ab−⌊a⌊bRn⌋R⌋n=a(bRmodn)+(a(−bRmodn)n−1modR)nR≤an+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

|ab−⌊a⌊bRn⌉R⌉n|=|a(bRmod±n)+(a(−bRmod±n)n−1mod±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)n−1mod[]1R)nR A quick check on the bounds yield the following in []0=[]1=⌊⌋ case

a−⌊a⌊Rn⌋R⌋n=a(Rmodn)+(a(−Rmodn)n−1modR)nR≤a(Rmodn)+RnR=n(1+a(Rmodn)Rn) and the following in []0=[]1=⌊⌉ case

|a−⌊a⌊Rn⌉R⌉n|=|a(Rmod±n)+(a(−Rmod±n)n−1mod±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)(R−Rmod[]0n)+([aR0]2[Rn]0mod[]1R1)R0nR where R0=2k−β, R1=2α+β, R=R0⋅R1=2k+α, and k is the bit-length of n. Bound check in the case []0=[]1=[]2=⌊⌋ yields the following

a−⌊⌊aR0⌋⌊Rn⌋R1⌋n≤a(Rmodn)+R0R−R0(Rmodn)+RnR=n(1+a(Rmodn)Rn+R0n−RmodnR1n) and for the case []0=[]1=[]2=⌊⌉ yields the following

|a−⌊⌊aR0⌉⌊Rn⌉R1⌉n|≤|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:

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Barrett reduction

Start with the simplest possible case. Write down what Barrett reduction claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Barrett reduction before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Barrett reduction ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Barrett reduction

In research
Barrett reduction appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Barrett reduction in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Barrett reduction is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computer arithmetic, Modular arithmetic, so understanding it makes those chapters shorter.
In everyday life
Look for Barrett reduction outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Barrett reduction” →

How to study Barrett reduction in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Barrett reduction means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Barrett reduction out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Barrett reduction in simple terms?

In modular arithmetic, Barrett reduction is an algorithm designed to optimize the calculation of amodn without needing a fast division algorithm. It replaces divisions with multiplications, and can be used when n is constant and a<n2.

Why does Barrett reduction matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Barrett reduction?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Barrett reduction.

Tags

  • Computer arithmetic
  • Modular arithmetic

Keep exploring