In modular arithmetic, Barrett reduction is an algorithm designed to optimize the calculation of  [1] without needing a fast division algorithm. It replaces divisions with multiplications, and can be used when
[1] without needing a fast division algorithm. It replaces divisions with multiplications, and can be used when  is constant and
 is constant and  . It was introduced in 1986 by P.D. Barrett.[2]
. It was introduced in 1986 by P.D. Barrett.[2] 
Historically, for values  , one computed
, one computed  by applying
Barrett reduction to the full product
 by applying
Barrett reduction to the full product  .
In 2021, Becker et al. showed that the full product is unnecessary if we can perform precomputation on one of the operands.[3]
.
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 ![{\displaystyle \left[\,\right]:\mathbb {R} \to \mathbb {Z} }](./_assets_/26cf7cce8fee09a0e27e1ef99370e2825214e74a.svg) an integer approximation if
 an integer approximation if ![{\displaystyle |\left[z\right]-z|\leq 1}](./_assets_/1c58ced9be029cda841359de6a4367212c24f944.svg) .
For a modulus
.
For a modulus  and an integer approximation
 and an integer approximation ![{\displaystyle \left[\,\right]}](./_assets_/5654b4b054083661bf0cc75c3d8ff3011a772ae6.svg) , 
we define
, 
we define ![{\displaystyle {\text{mod}}^{\left[\,\right]}\,n:\mathbb {Z} \to (\mathbb {Z} /n\mathbb {Z} )}](./_assets_/f43fea42f0571f9a1df5dc2c302ecd27772ad805.svg) as
 as
![{\displaystyle a\,{\text{mod}}^{\left[\,\right]}\,n=a-\left[a/n\right]n}](./_assets_/4fea208addbea6cce883716d3cbd5ebe4457ce2e.svg) . .
Common choices of ![{\displaystyle \left[\,\right]}](./_assets_/5654b4b054083661bf0cc75c3d8ff3011a772ae6.svg) are floor, ceiling, and rounding functions.
 are floor, ceiling, and rounding functions.
Generally, Barrett multiplication starts by specifying two integer approximations ![{\displaystyle \left[\,\right]_{0},\left[\,\right]_{1}}](./_assets_/e804e99686f7fec68620740d3c48e34d7852818f.svg) and computes a reasonably close approximation of
 and computes a reasonably close approximation of  as
 as
![{\displaystyle ab-\left[{\frac {a\,\left[{\frac {bR}{n}}\right]_{0}}{R}}\right]_{1}n}](./_assets_/ff258823b7b4813ed2217256a3381e9f9171092a.svg) , ,
where  is a fixed constant, typically a power of 2, chosen so that multiplication and division by
 is a fixed constant, typically a power of 2, chosen so that multiplication and division by  can be performed efficiently.
 can be performed efficiently.
The case  was introduced by P.D. Barrett [2] for the floor-function case
 was introduced by P.D. Barrett [2] for the floor-function case ![{\displaystyle \left[\,\right]_{0}=\left[\,\right]_{1}=\lfloor \,\rfloor }](./_assets_/ecf31e3f480001f0bb752aa804d3e8d43bbdb71e.svg) .
The general case for
.
The general case for  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]
 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  and
 and  .
.
When calculating  for unsigned integers, the obvious analog would be to use division by
 for unsigned integers, the obvious analog would be to use division by  :
:
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  with a value
 with a value  because division by
 because division by  is just a right-shift, and so it is cheap.
 is just a right-shift, and so it is cheap.
In order to calculate the best value for  given
 given  consider:
 consider:
 
For  to be an integer, we need to round
 to be an integer, we need to round  somehow. 
Rounding to the nearest integer will give the best approximation but can result in
 somehow. 
Rounding to the nearest integer will give the best approximation but can result in  being larger than
 being larger than  , which can cause underflows. Thus
, which can cause underflows. Thus  is used for unsigned arithmetic.
 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  , the value of
, the value of q in that function can end up being one too small, and thus a is only guaranteed to be within  rather than
 rather than  as is generally required. A conditional subtraction will correct this:
 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
}
Single-word Barrett multiplication
Suppose  is known.
This allows us to precompute
 is known.
This allows us to precompute  before we receive
 before we receive  .
Barrett multiplication computes
.
Barrett multiplication computes  , approximates the high part of
, approximates the high part of  with
with 
 ,
and subtracts the approximation.
Since
,
and subtracts the approximation.
Since 
 is a multiple of
 is a multiple of  ,
the resulting value
,
the resulting value 
 is a representative of
is a representative of  .
.
Correspondence between Barrett and Montgomery multiplications
Recall that unsigned Montgomery multiplication computes a representative of  as
as
 . .
In fact, this value is equal to  .
.
We prove the claim as follows.
 
Generally, for integer approximations ![{\displaystyle \left[\,\right]_{0},\left[\,\right]_{1}}](./_assets_/e804e99686f7fec68620740d3c48e34d7852818f.svg) ,
we have
,
we have
![{\displaystyle ab-\left[{\frac {a\,\left[{\frac {bR}{n}}\right]_{0}}{R}}\right]_{1}\,n={\frac {a\left(bR\,{\text{mod}}^{\left[\,\right]_{0}}\,n\right)+\left(a\left(-bR\,{\text{mod}}^{\left[\,\right]_{0}}\,q\right)n^{-1}\,{\text{mod}}^{\left[\,\right]_{1}}\,R\right)n}{R}}}](./_assets_/b236f2c9efd941b13c2f9026eae6edab09e15ecb.svg) .[3] .[3]
Range of Barrett multiplication
We bound the output with
 . .
Similar bounds hold for other kinds of integer approximation functions.
For example, if we choose ![{\displaystyle \left[\,\right]_{0}=\left[\,\right]_{1}=\left\lfloor \,\right\rceil }](./_assets_/cc3cee2b15a9c1721bafc76f5d8688638de0dcde.svg) , the rounding half up function,
then we have
, the rounding half up function,
then we have 
 
It is common to select R such that  (or
 (or  in the
 in the ![{\displaystyle \left[\,\right]_{0}=\left[\,\right]_{1}=\left\lfloor \,\right\rceil }](./_assets_/cc3cee2b15a9c1721bafc76f5d8688638de0dcde.svg) case) so that the output remains within
  case) so that the output remains within  and
 and  (
 ( and
 and  resp.), and therefore only one check is performed to obtain the final result between
 resp.), and therefore only one check is performed to obtain the final result between  and
 and  . 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.
. 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 ![{\displaystyle \left[{\frac {bR}{n}}\right]_{0}}](./_assets_/e04ed66831cbffd8121203a831275d6f8fd263ab.svg) 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.
 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  as the product of the operands and set
 as the product of the operands and set  to
 to  . This leads to
. This leads to
![{\displaystyle 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}}}](./_assets_/b7eb9a2ac83bd2b660cf6706b70e9adfefbe12b3.svg) 
A quick check on the bounds yield the following in ![{\displaystyle \left[\,\right]_{0}=\left[\,\right]_{1}=\left\lfloor \,\right\rfloor }](./_assets_/c189457663cf6e8dc50ae61955e04bbef2ab7855.svg) case
  case
 
and the following in ![{\displaystyle \left[\,\right]_{0}=\left[\,\right]_{1}=\left\lfloor \,\right\rceil }](./_assets_/cc3cee2b15a9c1721bafc76f5d8688638de0dcde.svg) case
  case
 
Setting  will always yield one check on the output. However, a tighter constraint on
 will always yield one check on the output. However, a tighter constraint on  might be possible since
 might be possible since ![{\displaystyle R\,{\text{mod}}^{\left[\,\right]_{0}}\,n}](./_assets_/1b28bf34b19f21f4167fae7834984bd2a55ed441.svg) is a constant that is sometimes significantly smaller than
 is a constant that is sometimes significantly smaller than  .
.
A small issue arises with performing the following product ![{\displaystyle a\,\left[{\frac {R}{n}}\right]_{0}}](./_assets_/735f9963967e1103d273b248dbd0cc60f1cfbeaf.svg) since
 since  is already a product of two operands. Assuming
 is already a product of two operands. Assuming  fits in
 fits in  bits, then
 bits, then  would fit in
 would fit in  bits and
 bits and ![{\displaystyle \left[{\frac {R}{n}}\right]_{0}}](./_assets_/a7c0998d06b47069edd846b413db25479d86f3a0.svg) would fit in
 would fit in  bits. Their product would require a
 bits. Their product would require a  multiplication which might require fragmenting in systems that cannot perform the product in one operation.
 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:
![{\displaystyle 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}}}](./_assets_/ffbbadfb25ba00075bc11ad5115be6e44609cac2.svg) 
where  ,
,  ,
,  , and
, and  is the bit-length of
 is the bit-length of  .
.
Bound check in the case ![{\displaystyle \left[\,\right]_{0}=\left[\,\right]_{1}=\left[\,\right]_{2}=\left\lfloor \,\right\rfloor }](./_assets_/5b7ae665af997a2a0375ecf2c1d7d6a02a6c6c19.svg) yields the following
  yields the following
 
and for the case ![{\displaystyle \left[\,\right]_{0}=\left[\,\right]_{1}=\left[\,\right]_{2}=\left\lfloor \,\right\rceil }](./_assets_/e6437c1d814e756dd6a4fba0441426cfb64a474b.svg) yields the following
  yields the following
 
For any modulus and assuming  , the bound inside the parenthesis in both cases is less than or equal:
, the bound inside the parenthesis in both cases is less than or equal:
 
where  in the
 in the  case and
  case and  in the
 in the  case.
 case.
Setting  and
 and  (or
  (or   in the
 in the  case) will always yield one check. In some cases, testing the bounds might yield a lower
 case) will always yield one check. In some cases, testing the bounds might yield a lower  and/or
 and/or  values.
 values.
Small Barrett reduction
It is possible to perform a Barrett reduction with one less multiplication as follows
![{\displaystyle a-\left[{\frac {a}{R}}\right]_{1}\,n}](./_assets_/859a76d3994871b9aea62131dbcc68132b740f29.svg) where where and and is the bit-length of is the bit-length of 
Every modulus can be written in the form  for some integer
 for some integer  .
.
![{\displaystyle {\begin{aligned}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{aligned}}}](./_assets_/26beda136d607800e0c164d369d759b4cc6dac4f.svg) 
Therefore, reducing any  for
 for ![{\displaystyle \left[\,\right]_{1}=\left\lfloor \,\right\rfloor }](./_assets_/17329ccf638517ac0bbd9b9be0b5c9f62574dcf2.svg) or any
 or any  for
 for ![{\displaystyle \left[\,\right]_{1}=\left\lfloor \,\right\rceil }](./_assets_/4bb5977346c2c337d4bebb281d2b12e82376a8cf.svg) yields one check.
  yields one check.
From the analysis of the constraint, it can be observed that the bound of  is larger when
 is larger when  is smaller. In other words, the bound is larger when
 is smaller. In other words, the bound is larger when  is closer to
 is closer to  .
.
Barrett Division
Barrett reduction can be used to compute floor, round or ceil division ![{\displaystyle \left[{\frac {a}{n}}\right]}](./_assets_/e265e5c27ce95518e1b37c02090d39abf9549686.svg) without performing expensive long division. Furthermore it can be used to compute
 without performing expensive long division. Furthermore it can be used to compute ![{\displaystyle \left[{\frac {ab}{n}}\right]}](./_assets_/513a9ae4846e4cdd37d361936ec5d997d97c5b50.svg) . After pre-computing the constants, the steps are as follows:
. After pre-computing the constants, the steps are as follows:
- Compute the approximate quotient ![{\displaystyle {\tilde {q}}=\left[{\frac {a\,\left[{\frac {bR}{n}}\right]_{0}}{R}}\right]_{1}}](./_assets_/63eba45bd7249aef894e9d3d3f1568d872cb1366.svg) . .
- Compute the Barrett remainder  . .
- Compute the quotient error  where where![{\displaystyle r=a\,{\text{mod}}^{\left[\,\right]}\,n}](./_assets_/861582576a8bcd5ca84ddb60d4c7c36018eb132b.svg) . This is done by subtracting a multiple of . This is done by subtracting a multiple of to to until until is obtained. 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  in step 3 cannot be more than 1. Using
 in step 3 cannot be more than 1. Using ![{\displaystyle \left[\,\right]_{0}=\left[\,\right]_{1}=\left\lfloor \,\right\rceil }](./_assets_/cc3cee2b15a9c1721bafc76f5d8688638de0dcde.svg) and appropriate constraints, the error
 and appropriate constraints, the error  can be obtained from the sign of
 can be obtained from the sign of  .
.
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
- ^ The remainder of integer division of  by by . .
- ^ a b 
Barrett, P. (1986). "Implementing the Rivest Shamir and Adleman Public Key Encryption Algorithm on a Standard Digital Signal Processor". Advances in Cryptology – CRYPTO' 86. Lecture Notes in Computer Science. Vol. 263. pp. 311–323. doi:10.1007/3-540-47721-7_24. ISBN 978-3-540-18047-0.
- ^ a b c  
Becker, Hanno; Hwang, Vincent; Kannwischer, Matthias J.; Yang, Bo-Yin; Yang, Shang-Yi (2021), "Neon NTT: Faster Dilithium, Kyber, and Saber on Cortex-A72 and Apple M1", Transactions on Cryptographic Hardware and Embedded Systems, 2022 (1): 221–244, doi:10.46586/tches.v2022.i1.221-244 
- ^ 
Shoup, Victor. "Number Theory Library".
- ^ Menezes, Alfred; Oorschot, Paul; Vanstone, Scott (1997). Handbook of Applied Cryptography (5th ed.). CRC Press. doi:10.1201/9780429466335. ISBN 0-8493-8523-7.
- ^ "Barrett reduction for polynomials". www.corsix.org. Retrieved 2022-09-07.
 
Sources