Chapter 4Ring-LWE, Module-LWE, and the Number Theoretic Transform

The Negacyclic NTT

August 25, 20266 min readbeginner

Everything is now in place. This note states the transform, computes one completely by hand, and checks that it does what it is supposed to do.

Everything is now in place. This note states the transform, computes one completely by hand, and checks that it does what it is supposed to do.

01.The definition

Assume 2n2n divides q−1q - 1, and let ζ\zeta be a primitive 2n2nth root of unity in Fq\mathbb{F}_q.

For a(X)=a0+a1X+⋯+an−1Xn−1a(X) = a_0 + a_1 X + \cdots + a_{n-1}X^{n-1} in RqR_q, the negacyclic NTT is

a^k  =  a(ζ2k+1)  =  ∑j=0n−1aj ζ(2k+1)j,k=0,1,…,n−1.\hat{a}_k \;=\; a\bigl(\zeta^{2k+1}\bigr) \;=\; \sum_{j=0}^{n-1} a_j \, \zeta^{(2k+1)j}, \qquad k = 0, 1, \ldots, n-1 .

Read it as what it is: evaluate the polynomial at each of the nn odd powers of ζ\zeta. Nothing more.

The inverse recovers the coefficients:

aj  =  1n ζ−j∑k=0n−1a^k ζ−2jk.a_j \;=\; \frac{1}{n} \, \zeta^{-j} \sum_{k=0}^{n-1} \hat{a}_k \, \zeta^{-2jk} .

The ζ−j\zeta^{-j} factor out front is the fingerprint of the negacyclic version. An ordinary inverse DFT has only the 1/n1/n and the sum. That extra twist is what undoes the odd-power evaluation grid.

02.A complete worked transform

Take q=17q = 17, n=4n = 4, ζ=2\zeta = 2, and the evaluation grid (2,8,15,9)(2, 8, 15, 9) from Roots of Unity in a Finite Field.

Transform

a(X)  =  3+X+4X2+2X3,a=(3,1,4,2).a(X) \;=\; 3 + X + 4X^2 + 2X^3, \qquad a = (3, 1, 4, 2).

At X=2X = 2. Powers: 22=42^2 = 4, 23=82^3 = 8.

3+1⋅2+4⋅4+2⋅8  =  3+2+16+16  =  37  ≡  3(mod17),3 + 1 \cdot 2 + 4 \cdot 4 + 2 \cdot 8 \;=\; 3 + 2 + 16 + 16 \;=\; 37 \;\equiv\; 3 \pmod{17},

since 37=2⋅17+337 = 2 \cdot 17 + 3.

At X=8X = 8. Reduce the powers first: 82=64≡138^2 = 64 \equiv 13, and 83≡8⋅13=104≡28^3 \equiv 8 \cdot 13 = 104 \equiv 2, since 104=6⋅17+2104 = 6 \cdot 17 + 2.

3+1⋅8+4⋅13+2⋅2  =  3+8+52+4  =  67  ≡  16(mod17).3 + 1 \cdot 8 + 4 \cdot 13 + 2 \cdot 2 \;=\; 3 + 8 + 52 + 4 \;=\; 67 \;\equiv\; 16 \pmod{17} .

At X=15X = 15. Easier as 15≡−215 \equiv -2. Then 152≡415^2 \equiv 4 and 153≡−8≡915^3 \equiv -8 \equiv 9.

3+1⋅(−2)+4⋅4+2⋅9  =  3−2+16+18  =  35  ≡  1(mod17).3 + 1 \cdot (-2) + 4 \cdot 4 + 2 \cdot 9 \;=\; 3 - 2 + 16 + 18 \;=\; 35 \;\equiv\; 1 \pmod{17} .

At X=9X = 9. As 9≡−89 \equiv -8: 92≡64≡139^2 \equiv 64 \equiv 13, and 93≡9⋅13=117≡15≡−29^3 \equiv 9 \cdot 13 = 117 \equiv 15 \equiv -2.

3+1⋅9+4⋅13+2⋅(−2)  =  3+9+52−4  =  60  ≡  9(mod17).3 + 1 \cdot 9 + 4 \cdot 13 + 2 \cdot (-2) \;=\; 3 + 9 + 52 - 4 \;=\; 60 \;\equiv\; 9 \pmod{17} .

So

a^  =  (3,  16,  1,  9).\hat{a} \;=\; (3,\; 16,\; 1,\; 9).

03.Checking that it multiplies

The claim to verify is that pointwise multiplication in the transformed domain corresponds to negacyclic multiplication in RqR_q.

Take a second polynomial, b(X)=1+Xb(X) = 1 + X, so b=(1,1,0,0)b = (1, 1, 0, 0). Evaluating at the same four points:

b(2)=3,b(8)=9,b(15)=1+15=16,b(9)=10.b(2) = 3, \qquad b(8) = 9, \qquad b(15) = 1 + 15 = 16, \qquad b(9) = 10 .

So b^=(3,9,16,10)\hat{b} = (3, 9, 16, 10).

Multiply pointwise modulo 1717:

a^⊙b^  =  (3⋅3,  16⋅9,  1⋅16,  9⋅10)  =  (9,  144,  16,  90)  ≡  (9,  8,  16,  5)(mod17),\hat{a} \odot \hat{b} \;=\; (3 \cdot 3,\; 16 \cdot 9,\; 1 \cdot 16,\; 9 \cdot 10) \;=\; (9,\; 144,\; 16,\; 90) \;\equiv\; (9,\; 8,\; 16,\; 5) \pmod{17},

using 144=8⋅17+8144 = 8 \cdot 17 + 8 and 90=5⋅17+590 = 5 \cdot 17 + 5.

Now compute the same product the slow way, directly in R17R_{17}:

a(X)⋅b(X)=(3+X+4X2+2X3)(1+X)=3+4X+5X2+6X3+2X4.\begin{aligned} a(X) \cdot b(X) &= (3 + X + 4X^2 + 2X^3)(1 + X) \\ &= 3 + 4X + 5X^2 + 6X^3 + 2X^4 . \end{aligned}

The X4X^4 term must be reduced. In RqR_q the rule is X4=−1X^4 = -1, so 2X42X^4 becomes −2-2, and it lands on the constant coefficient:

a⋅b  =  (3−2)+4X+5X2+6X3  =  1+4X+5X2+6X3.a \cdot b \;=\; (3 - 2) + 4X + 5X^2 + 6X^3 \;=\; 1 + 4X + 5X^2 + 6X^3 .

So the coefficient list is (1,4,5,6)(1, 4, 5, 6).

Transform that directly and see whether it agrees. Evaluating 1+4X+5X2+6X31 + 4X + 5X^2 + 6X^3 at X=2X = 2:

1+8+20+48  =  77  ≡  9(mod17),1 + 8 + 20 + 48 \;=\; 77 \;\equiv\; 9 \pmod{17},

since 77=4⋅17+977 = 4 \cdot 17 + 9. That matches the first pointwise product. The remaining three match as well, giving (9,8,16,5)(9, 8, 16, 5) both ways.

The transform is multiplicative. Applying the inverse to (9,8,16,5)(9, 8, 16, 5) returns (1,4,5,6)(1, 4, 5, 6), which is the product.

04.The full multiplication procedure

Collecting it into the recipe an implementation follows:

  1. Transform aa. Cost: one NTT.
  2. Transform bb. Cost: one NTT.
  3. Multiply the two transforms pointwise. Cost: nn multiplications.
  4. Inverse-transform the result. Cost: one inverse NTT.

Three transforms and one cheap pointwise step, replacing n2n^2 coefficient multiplications.

There is a detail worth noting because it changes how real code is organised. Many quantities get multiplied repeatedly. In ML-KEM the public matrix AA is used in every operation, so it is stored already transformed and steps 1 through 4 shrink to steps 3 and 4 alone. FIPS 203 in fact specifies the public key in the transformed domain, so the transform never has to be applied to it at all. Keeping data in the NTT domain for as long as possible is one of the main structural optimisations in every implementation.

05.What this cost

Being honest about the running example: it saved nothing.

Direct evaluation at four points, with four coefficients each, is 1616 multiplications. Schoolbook multiplication of two length-44 polynomials is also 1616. At n=4n = 4 the transform is a demonstration, not an improvement.

The saving requires two things that n=4n = 4 does not provide. The transform has to be computed by the fast recursive method rather than by direct evaluation, which is the next note. And nn has to be large enough for nlog⁡nn \log n to beat n2n^2 by a worthwhile margin.

At n=256n = 256 it does. Schoolbook is 65,53665{,}536 multiplications. The three-transform route is about 3,3003{,}300. That is the factor of twenty, and it only appears once the transform itself stops costing n2n^2.

FeedbackBook mode
post-quantum-cryptographycryptographymathematics