---
title: Walking through an NTT butterfly network
description: A short, exact-arithmetic walkthrough of the radix-2 NTT, with the code and a live demo.
pubDate: 2026-10-20
tags: ['cryptography', 'walkthrough']
---

A number-theoretic transform (NTT) is an FFT that works over a finite field instead of
the complex numbers: no floating point, no rounding, every value is exact. For a
transform over GF(998244353) — the zk-community classic, because p = 119 · 2²³ + 1 — the
whole transform is butterflies like this one:

```c
void butterfly(uint64_t* a, size_t i, size_t j, uint64_t w) {
    uint64_t u = a[i], v = a[j];
    a[i] = add(u, v);          // (u + v) mod p
    a[j] = mul(sub(u, v), w);  // ((u - v) * w) mod p
}
```

That is the entire primitive. A radix-2 decimation-in-frequency transform over n = 2ᵏ
points is just k passes of these butterflies: the first stage pairs points n/2 apart,
the next pairs n/4 apart, and so on until the span is 1. After log₂(n) stages the
result is in bit-reversed order, and a static reordering — the dashed criss-cross in
the demo — restores the natural sequence.

The demo on the [project page](/projects/fft-ntt/) walks this network live: press play,
read the u, v and w in every butterfly, and watch the output land. It self-checks
against an O(n²) reference transform after every run, so the numbers you see are not a
renderer's decoration — they are verified arithmetic.
