LogoLogo
LogoLogo
  • Introduction
    • About Us
    • Notations & Definitions
      • MPC
      • ZK
    • Contribute to this Site!
  • Primitives
    • Multiplication
      • Karatsuba Multiplication
      • Toom-Cook Multiplication
    • NAF (Non-adjacent form)
    • Chinese Remainder Theorem (CRT)
    • Euclidean Algorithm
      • Extended Euclidean Algorithm
      • Binary Euclidean Algorithm
      • Extended Binary Euclidean Algorithm
    • Coding Theory
      • Linear Code
    • Number Theoretic Transform
    • Abstract Algebra
      • Group
        • -Morphisms
        • Batch Inverse
      • Elliptic Curve
        • Weierstrass Curve
          • Coordinate Forms
          • Fast Elliptic Curve Arithmetic and Improved WEIL Pairing Evaluation
        • Edwards Curve
          • Coordinate Forms
          • Twisted Edwards ↔ Short Weierstrass Transformation
        • Batch Inverse for Batch Point Additions
        • Scalar Multiplication
          • Double-and-add
          • GLV Decomposition
        • MSM
          • Pippenger's Algorithm
          • Signed Bucket Index
          • CycloneMSM
          • EdMSM
          • cuZK
        • 2-Chain and 2-Cycle of Elliptic Curves
    • Encryption Scheme
      • ElGamal Encryption
    • Modular Arithmetic
      • Modular Reduction
        • Barrett Reduction
        • Montgomery Reduction
      • Modular Inverse
        • Bernstein-Yang's Inverse
    • Multiset Check
    • Sumcheck
    • Commitment Scheme
      • Fflonk
      • SHPlonk
      • Zeromorph
  • MPC
    • Yao's Garbled Circuits
    • GMW
    • BMR
  • ZK
    • Arithmetization
      • R1CS
      • PLONK
      • AIR
    • Folding
      • LatticeFold
      • Nova
        • Nova over Cycles of Curves
    • Lookup
      • Lasso
      • LogUp-GKR
    • SNARK
      • Groth16
      • HyperPlonk
      • Spartan
        • SPARK
    • STARK
      • Additive NTT
      • Basefold
      • Binius
      • Brakedown
      • CircleSTARK
      • FRI
        • FRI Security Features and Optimizations
      • DEEP FRI
      • STIR
      • WHIR
    • Distributed ZK
      • Ryan's Trick for Distributed Groth16
  • Application
    • zkLogin
    • zkHoldem
    • zkTLS
      • DECO
      • Proxying is enough
  • zkVM
Powered by GitBook
On this page
  • The Core Idea
  • How it works
  • Setup and Precomputation
  • Approximating the Quotient
  • How Close is the Approximation?
  • Final Reduction Algorithm
  • Cost Analysis of Modular Multiplication
Export as PDF
  1. Primitives
  2. Modular Arithmetic
  3. Modular Reduction

Barrett Reduction

PreviousModular ReductionNextMontgomery Reduction

Last updated 1 month ago

,The primary goal of Barrett reduction is to perform modular reduction efficiently. The standard calculation, x−⌊x/n⌋⋅nx - \lfloor x / n \rfloor \cdot nx−⌊x/n⌋⋅n, involves a division (x/nx / nx/n), which can be slow. Barrett reduction aims to replace this division.

The Core Idea

Barrett reduction uses multiplications, subtractions, and bit shifts (fast division by powers of 2) instead of a general division. It achieves this using a precomputed value based on the modulus nnn to approximate the quotient value ⌊x/n⌋\lfloor x / n \rfloor⌊x/n⌋.

How it works

Setup and Precomputation

Suppose we are working with integers base bbb. (Typically b=2b=2b=2 in computers). Consider an integer kkk such that bk>nb^k > nbk>n. Often, kkk is chosen such that bkb^kbk is roughly n2n^2n2 (e.g., if nnn fits in www bits, k=2wk = 2wk=2w is common). Now, we precompute the value mmm:

m=⌊bk/n⌋m = \lfloor b^k / n \rfloorm=⌊bk/n⌋

which acts as a scaled approximation of division by nnn.

Approximating the Quotient

The true remainder is r=x−q⋅nr = x - q \cdot nr=x−q⋅n, where the true quotient is q=⌊x/n⌋q = \lfloor x / n \rfloorq=⌊x/n⌋. So in Barrett's method we would like to estimate qqq without dividing xxx by nnn.

Consider the product x⋅mx \cdot mx⋅m. Substituting the definition of mmm, we get:

x⋅m=x⋅⌊bk/n⌋≤x⋅(bk/n)x \cdot m = x \cdot \lfloor b^k / n \rfloor\leq x\cdot (b^k/n)x⋅m=x⋅⌊bk/n⌋≤x⋅(bk/n)

How Close is the Approximation?

Final Reduction Algorithm

Cost Analysis of Modular Multiplication

Single-Precision case

Dividing both sides by bkb^kbk (which is a right shift by kkk if b=2b=2b=2):

(x⋅m)/bk≤(x⋅bk/n)/bk=x/n(x \cdot m) / b^k \le (x \cdot b^k / n) / b^k = x / n(x⋅m)/bk≤(x⋅bk/n)/bk=x/n

This gives us the approximation to qqq:

⌊(x⋅m)/bk⌋≤⌊x/n⌋=q\lfloor(x \cdot m) / b^k\rfloor \le \lfloor x / n \rfloor = q⌊(x⋅m)/bk⌋≤⌊x/n⌋=q

Let's denote this approximation as q^\hat{q}q^​:

q^=⌊(x⋅m)/bk⌋≤q\hat{q} = \lfloor (x \cdot m) / b^k \rfloor\le q q^​=⌊(x⋅m)/bk⌋≤q

Notice that we can write bk=⌊bk/n⌋⋅n+r′b^k=\lfloor b^k/n\rfloor \cdot n + r'bk=⌊bk/n⌋⋅n+r′ for some remainder r′<nr'<nr′<n. Solving for mmm, we havem=(bk−r′)/nm=(b^k-r')/nm=(bk−r′)/n. Replacing mmm for the q^\hat{q}q^​ equation, we get:

q^=⌊(x⋅m)/bk⌋=⌊x⋅(bk−r′)/nbk⌋=⌊(x/n)−(x⋅r′/(n⋅bk))⌋\hat{q} = \lfloor (x \cdot m) / b^k \rfloor = \left\lfloor x \cdot \frac{(b^k - r')/n}{b^k} \right\rfloor = \lfloor (x/n) - (x \cdot r' / (n \cdot b^k)) \rfloorq^​=⌊(x⋅m)/bk⌋=⌊x⋅bk(bk−r′)/n​⌋=⌊(x/n)−(x⋅r′/(n⋅bk))⌋

Comparing q^\hat{q}q^​ to q=⌊x/n⌋q = \lfloor x/n \rfloorq=⌊x/n⌋, the difference depends on the term (x⋅r′/(n⋅bk))(x \cdot r' / (n \cdot b^k))(x⋅r′/(n⋅bk)), which is small if bkb^kbk is large enough relative to xxx.

For example, if bk>n2b^k > n^2bk>n2, then (x⋅r′/(n⋅bk))<1(x \cdot r' / (n \cdot b^k)) < 1(x⋅r′/(n⋅bk))<1 which gives us a nice tight bound q−1≤q^q-1\leq \hat{q}q−1≤q^​ so we need only 1 subtraction at the worst case. If we choose a smaller kkk such that bk>nb^k > nbk>n, q^\hat{q}q^​ can get as low as q−2q-2q−2, but typically we choose a large bk>n2b^k > n^2bk>n2.

Precomputation (once for fixed nnn):

Choose kkk (e.g., k=2⋅bitlength(n)k = 2 \cdot \mathsf{bitlength}(n)k=2⋅bitlength(n)). We assume b=2b=2b=2.

Calculate m=⌊2k/n⌋m = \lfloor 2^k / n \rfloorm=⌊2k/n⌋.

Reduction (for each xxx):

Calculate q^=⌊(x⋅m)/2k⌋\hat{q} = \lfloor (x \cdot m) / 2^k \rfloorq^​=⌊(x⋅m)/2k⌋ (intermediate product x⋅mx \cdot mx⋅m might need k+bitlength(x/n)k + \mathsf{bitlength}(x/n)k+bitlength(x/n) bits; often only the higher bits are needed since we do a kkk bit-shift afterwards).

Calculate r^=x−q^⋅n\hat{r} = x - \hat{q} \cdot nr^=x−q^​⋅n.

Correction: while r^≥n\hat{r} \geq nr^≥n, r^=r^−n\hat{r}=\hat{r}-nr^=r^−n

Suppose the modulus nn n has bit width of w<word_sizew< \mathsf{word\_size}w<word_size. Then, the cost looks like:

Computing x=a⋅b:x=a\cdot b:x=a⋅b: multiplication of bitwidth w×w→2ww\times w\rightarrow 2ww×w→2w .

Computing x⋅m:x\cdot m:x⋅m: multiplication of bitwidth 2w×(k−w)→w+k2w\times (k-w)\rightarrow w+k2w×(k−w)→w+k.

Computing q^⋅n:\hat{q}\cdot n:q^​⋅n: multiplication of bitwidth w×w→2ww\times w\rightarrow 2ww×w→2w.

TODO(): add multi-precision case

Written by from

A41
BATZORIG ZORIGOO
BATZORIG ZORIGOO