-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathntt.html
More file actions
385 lines (359 loc) · 16.6 KB
/
ntt.html
File metadata and controls
385 lines (359 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<!DOCTYPE html>
<html lang="en" data-theme="dark">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>NTT Implementation Guide - Privacy-Preserving AI Computing</title>
<meta name="description" content="Guide to Number Theoretic Transform (NTT) implementations.">
<!-- Google Fonts: Inter & Outfit for a more modern look -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link
href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=Outfit:wght@400;500;700;800&display=swap"
rel="stylesheet">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="app-layout">
<!-- Sidebar Navigation -->
<aside class="sidebar">
<div class="sidebar-header">
<div class="logo">CPA TUTORIAL</div>
<button id="mobile-close-btn" class="icon-btn" aria-label="Close Menu">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M18 6L6 18M6 6l12 12" />
</svg>
</button>
</div>
<nav class="sidebar-nav">
<!-- New Sections -->
<div class="nav-section">
<h3 class="nav-title">tutorial</h3>
<ul class="nav-links">
<li><a href="index.html">ASPLOS'26@Pittsburgh</a></li>
</ul>
</div>
<div class="nav-section">
<h3 class="nav-title">docs</h3>
<ul class="nav-links">
<li><a href="beginner.html">Easy HE Background</a></li>
<li><a href="cross.html">CROSS</a></li>
<li><a href="tpu.html">TPU Novice to Master</a></li>
<li><a href="ntt.html" class="active">NTT Algorithms</a></li>
<li><a href="open_challenge.html">Open Challenge</a></li>
</ul>
</div>
</nav>
</aside>
<!-- Main Content Area -->
<main class="main-content">
<header class="top-bar">
<button id="mobile-menu-btn" class="icon-btn" aria-label="Open Menu">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M3 12h18M3 6h18M3 18h18" />
</svg>
</button>
<div class="header-title">NTT Implementation Guide</div>
<div class="top-actions">
<button id="theme-toggle" class="icon-btn" aria-label="Toggle Theme">
<!-- Sun Icon -->
<svg class="sun-icon" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="2">
<circle cx="12" cy="12" r="5" />
<path
d="M12 1v2M12 21v2M4.22 4.22l1.42 1.42M18.36 18.36l1.42 1.42M1 12h2M21 12h2M4.22 19.78l1.42-1.42M18.36 5.64l1.42-1.42" />
</svg>
<!-- Moon Icon -->
<svg class="moon-icon" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="2">
<path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z" />
</svg>
</button>
</div>
</header>
<div class="content-wrapper">
<article class="doc-content">
<section id="ntt-introduction">
<h2>NTT Implementation Guide</h2>
<p>It could run by calling <code>python3 ntt_test.py</code>.</p>
<p>This directory (<code>pedagagy</code>, <a
href="https://github.com/EfficientPPML/CROSS/blob/main/jaxite_word/pedagagy">link</a>) contains
educational and production-oriented implementations of Number Theoretic Transform (NTT) algorithms.
The code illustrates the evolution from mathematical definitions to highly optimized, hardware-accelerated
kernels.</p>
</section>
<section id="ntt-variants">
<h2>1. NTT Variants Include</h2>
<p>We implement two primary mathematical variants of the NTT, essential for Homomorphic Encryption (BFV,
CKKS,
etc.):</p>
<ul>
<li><strong>Cyclic NTT / INTT</strong>:
<ul>
<li>The standard transform over a finite field.</li>
<li>Used for polynomial multiplication in <span class="math">R<sub>q</sub> = ℤ<sub>q</sub>[X] /
(X<sup>N</sup>
- 1)</span>.</li>
</ul>
</li>
<li><strong>Negacyclic NTT / INTT</strong>:
<ul>
<li>Used for polynomial rings of the form <span class="math">R<sub>q</sub> = ℤ<sub>q</sub>[X] /
(X<sup>N</sup>
+ 1)</span>.</li>
<li><strong>Implementation</strong>: Realized by "Pre-twisting" (multiplying coefficients by powers of
<span class="math">ψ</span>, where <span class="math">ψ<sup>2</sup> = ω</span>), running a Cyclic
NTT, and
"Post-twisting" for the inverse.
</li>
</ul>
</li>
</ul>
</section>
<section id="ntt-algorithms">
<h2>2. Algorithms of NTT</h2>
<p>We provide implementations with different algorithmic complexities and structural properties:</p>
<ul>
<li><strong>Original / Naive (<span class="math">O(N<sup>2</sup>)</span>)</strong>:
<ul>
<li>Direct matrix-vector multiplication.</li>
<li><strong>Purpose</strong>: Golden reference for correctness and understanding the definition.</li>
</ul>
</li>
<li><strong>Cooley-Tukey (<span class="math">O(N log N)</span>)</strong>:
<ul>
<li>Standard recursive decomposition (FFT).</li>
<li><strong>Implementation</strong>: Iterative "Bit-Reverse" approach.</li>
</ul>
</li>
<li><strong>4-Step NTT (<span class="math">O(N<sup>1.5</sup>)</span>)</strong>:
<ul>
<li>Decomposes the transform into: Column NTT → Twiddle multiplication → Row NTT.</li>
<li><strong>Purpose</strong>: Improves memory locality and parallelization structure.</li>
</ul>
</li>
<li><strong>3-Step NTT (<span class="math">O(N<sup>1.5</sup>)</span>)</strong>:
<ul>
<li>A "Layout Invariant" or "Stockham-like" approach optimized for hardware (TPU) with high penalty
for layout
transformation.</li>
<li><strong>Purpose</strong>: Expresses NTT purely as a sequence of large matrix multiplications to
maximize
arithmetic density.</li>
</ul>
</li>
</ul>
</section>
<section id="modular-reduction">
<h2>3. Modular Reduction Algorithms</h2>
<p>Efficient modular reduction is critical for performance. We support:</p>
<ul>
<li><strong>Barrett Reduction</strong>:
<ul>
<li>Uses precomputed <code>s_w</code>, <code>w</code>, <code>m</code> scalar factors to estimate
quotients.
</li>
<li>Dominant in the JAX/TPU implementations.</li>
</ul>
</li>
<li><strong>Montgomery Reduction</strong>:
<ul>
<li>Transforms values into Montgomery form to replace division with logical shifts.</li>
<li>Used in specific high-performance variants (<code>ntt_three_step_bat_montgomery_batch</code>).
</li>
</ul>
</li>
</ul>
</section>
<section id="layouts-batching">
<h2>4. Layouts and Batching</h2>
<p>To support various hardware configurations and use-cases, we support different data layouts:</p>
<ul>
<li><strong>Batching</strong>:
<ul>
<li><strong>Batch First</strong>: <code>(Batch, N, ...)</code></li>
<li><strong>Batch Second</strong>: <code>(Moduli, Batch, N, ...)</code> - Optimization to keep batch
dimensions adjacent to dense compute dimensions or for specific sharding strategies.</li>
</ul>
</li>
<li><strong>Multi-Moduli (RNS)</strong>:
<ul>
<li>Native support for processing multiple Residue Number System (RNS) limbs concurrently.</li>
</ul>
</li>
<li><strong>Sharding</strong>:
<ul>
<li>Implicit support via JAX <code>pmap</code> / <code>vmap</code> compatible structures, allowing
distribution across devices by batch or modulus.</li>
</ul>
</li>
</ul>
</section>
<section id="summary-table">
<h2>5. Summary Tables</h2>
<p>The following table maps the algorithmic concepts to their functional APIs in <code>ntt.py</code> and
corresponding tests in <code>ntt_test.py</code>.</p>
<h3>Cyclic NTT</h3>
<table>
<thead>
<tr>
<th>Algorithm</th>
<th>Complexity</th>
<th>Reduction</th>
<th>Implementation API</th>
<th>Test Case (in <code>ntt_test.py</code>)</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Original</strong></td>
<td><span class="math">O(N<sup>2</sup>)</span></td>
<td>Python <code>%</code></td>
<td><code>ntt_original_form</code></td>
<td><code>test_C_NTT_None_Barrett_BatchFirst_none</code></td>
</tr>
<tr>
<td><strong>Cooley-Tukey</strong></td>
<td><span class="math">O(N log N)</span></td>
<td>Python <code>%</code></td>
<td><code>ntt_bit_reverse</code></td>
<td><code>test_br_C_NTT_None_Barrett_BatchFirst_none</code></td>
</tr>
<tr>
<td><strong>4-Step</strong></td>
<td><span class="math">O(N<sup>1.5</sup>)</span></td>
<td>Python <code>%</code></td>
<td><code>ntt_four_step</code></td>
<td><code>test_4_step_C_NTT_None_Barrett_BatchFirst_none</code></td>
</tr>
</tbody>
</table>
<h3>Negacyclic NTT</h3>
<table>
<thead>
<tr>
<th>Algorithm</th>
<th>Complexity</th>
<th>Reduction</th>
<th>Implementation API</th>
<th>Test Case (in <code>ntt_test.py</code>)</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Cooley-Tukey</strong></td>
<td><span class="math">O(N log N)</span></td>
<td>Python <code>%</code></td>
<td><code>ntt_negacyclic_bit_reverse</code></td>
<td><code>test_br_N_NTT_None_Barrett_BatchFirst_none</code></td>
</tr>
<tr>
<td><strong>Cooley-Tukey</strong></td>
<td><span class="math">O(N log N)</span></td>
<td><strong>Barrett</strong></td>
<td><code>ntt_negacyclic_bit_reverse_jax</code></td>
<td><code>test_br_N_NTT_None_Barrett_BatchFirst_none_Jax</code></td>
</tr>
<tr>
<td><strong>4-Step</strong></td>
<td><span class="math">O(N<sup>1.5</sup>)</span></td>
<td>Python <code>%</code></td>
<td><code>ntt_negacyclic_four_step</code></td>
<td><code>test_4_step_N_NTT_None_Barrett_BatchFirst_none</code></td>
</tr>
<tr>
<td><strong>3-Step</strong></td>
<td><span class="math">O(N<sup>1.5</sup>)</span></td>
<td>Python <code>%</code></td>
<td><code>ntt_negacyclic_three_step</code></td>
<td><code>test_3_step_N_NTT_None_Barrett_BatchFirst_none</code></td>
</tr>
</tbody>
</table>
<h3>Optimized / Hardware Implementations (Negacyclic)</h3>
<table>
<thead>
<tr>
<th>Category</th>
<th>Variant</th>
<th>Reduction</th>
<th>Implementation API</th>
<th>Test Case</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>JAX / BAT</strong></td>
<td>3-Step, Batched</td>
<td><strong>Barrett</strong></td>
<td><code>ntt_three_step_bat_barrett_batch</code></td>
<td><code>test_3_step_N_NTT_SMB_Barrett_BatchFirst_none</code></td>
</tr>
<tr>
<td><strong>JAX / BAT</strong></td>
<td>3-Step, Multi-Mod</td>
<td><strong>Barrett</strong></td>
<td><code>ntt_three_step_bat_barrett_multi_moduli</code></td>
<td><code>test_3_step_N_NTT_MMB_Barrett_BatchSecond_none</code></td>
</tr>
<tr>
<td><strong>JAX / BAT</strong></td>
<td>3-Step, Batched</td>
<td><strong>Montgomery</strong></td>
<td><code>ntt_three_step_bat_montgomery_batch</code></td>
<td><em>Integrated in performance tests</em></td>
</tr>
</tbody>
</table>
<h3>Configurable Knobs Reference</h3>
<ul>
<li><code>q</code>: Prime modulus.</li>
<li><code>psi</code>: Primitive <span class="math">2N</span>-th root of unity (for negacyclic).</li>
<li><code>omega</code>: Primitive <span class="math">N</span>-th root of unity (<span class="math">ω =
ψ<sup>2</sup></span>).</li>
<li><code>r</code>, <code>c</code>: Row and column factors where <span class="math">N = r × c</span>.</li>
<li><code>s_w</code>, <code>w</code>, <code>m</code>: Barrett reduction precomputed constants.</li>
<li><code>tf_step*</code>: Twiddle factor matrices (precomputed control constants).</li>
</ul>
</section>
<!-- Citation Section -->
<section id="citation" class="citation-section">
<h2>Citation</h2>
<p>If you find this tutorial helpful, feel free to:</p>
<ul>
<li>Star CROSS repo at <a
href="https://github.com/EfficientPPML/CROSS">https://github.com/EfficientPPML/CROSS</a></li>
<li>Cite our paper with biblatex below:</li>
</ul>
<pre><code>@inproceedings{tong2025CROSS,
author = {Jianming Tong and Tianhao Huang and Jingtian Dang and Leo de Castro and Anirudh Itagi and Anupam
Golder and Asra Ali and Jevin Jiang and Jeremy Kun and Arvind and G. Edward Suh and Tushar Krishna},
title = {Leveraging ASIC AI Chips for Homomorphic Encryption},
year = {2026},
publisher = {2026 IEEE International Symposium on High Performance Computer Architecture (HPCA)},
address = {Australia},
keywords = {AI ASICs, TPU, Fully Homomorphic Encryption},
location = {Australia},
series = {HPCA'26} }</code></pre>
</section>
</article>
<aside class="on-this-page">
<h4>On this page</h4>
<ul>
<li><a href="#ntt-variants">Variants</a></li>
<li><a href="#ntt-algorithms">Algorithms</a></li>
<li><a href="#modular-reduction">Reductions</a></li>
<li><a href="#layouts-batching">Layouts</a></li>
<li><a href="#summary-table">Summary</a></li>
<li><a href="#citation">Citation</a></li>
</ul>
</aside>
</div>
<footer class="site-footer">
<p>© 2026 Cryptography Primitives Acceleration Tutorial.</p>
</footer>
</main>
</div>
<script src="script.js"></script>
</body>
</html>