fhe-ntt

fhe-ntt is a batched, RNS, negacyclic Number-Theoretic Transform for homomorphic encryption and lattice cryptography, loadable through kernels. The reference baselines are schoolbook negacyclic convolution, compared exactly, and CRT reconstruction across the RNS base against a bignum convolution.

The NTT is the finite-field FFT: it turns polynomial multiplication in the RLWE ring Z_q[X]/(X^n + 1) into transform, pointwise product, inverse transform, and it is the dominant cost of every homomorphic multiply, rotation, and bootstrap in BGV, BFV, and CKKS. FHE runs in a residue number system, a large modulus split over word-size primes, so one operation is a batch of independent NTTs. This kernel runs that whole batch on the GPU, millions of transforms per second, with results that are exact integers rather than approximations.

Butterfly arcs sweep stage by stage across an eight-prime RNS lane stack as a transform completes and returns bitwise

The Cooley-Tukey stages of one lane, butterflies doubling in span each stage, over the eight-prime RNS stack; the roundtrip INTT(NTT(a)) == a returns bitwise, the negacyclic product equals schoolbook exactly, and the batch sustains 1.6 million transforms per second at n = 4096 over 8 primes.

Usage

import torch
from kernels import get_kernel

fhe = get_kernel("phanerozoic/fhe-ntt", version=1, trust_remote_code=True)

n = 4096
primes = [576460752308273153, 576460752312401921]   # 59-bit, each = 1 (mod 2n)
ctx = fhe.NTTContext(n, primes)                       # precompute twiddles once

# coefficients are [L, n] or [B, L, n] int64 in [0, q_l)
a = torch.stack([torch.randint(0, q, (n,), dtype=torch.int64, device="cuda") for q in primes])
b = torch.stack([torch.randint(0, q, (n,), dtype=torch.int64, device="cuda") for q in primes])

c = ctx.poly_mul(a, b)          # negacyclic product per prime, coefficient domain
A = ctx.ntt(a.clone())          # forward NTT in place
ctx.intt(A)                     # inverse, recovers a

version selects the release branch; trust_remote_code is required by kernels for publishers without the trusted-publisher mark. poly_mul(a, b, primes) is also exposed as a free function that caches a context for the shape.

API

Symbol Purpose
NTTContext(n, primes, device) precompute twiddle and Shoup tables for a degree and RNS base
ctx.ntt(a) / ctx.intt(a) forward / inverse negacyclic NTT, in place
ctx.poly_mul(a, b) negacyclic a * b per prime in the coefficient domain
poly_mul(a, b, primes), ntt(a, primes), intt(a, primes) context-caching free functions

Method

The forward transform is a Cooley-Tukey decimation-in-time NTT and the inverse a Gentleman-Sande decimation-in-frequency NTT, both with the negacyclic twist folded into the twiddles (a primitive 2n-th root of unity per prime), so no separate pre/post weighting is needed. Each butterfly is a Shoup modular multiply: with the twiddle w fixed, its reciprocal floor(w * 2^64 / q) is precomputed, and x * w mod q is one multiply-high and one conditional subtraction. Coefficients and twiddles are < q < 2^62 and carried in int64; the pointwise product, where both operands vary, uses a 128-bit multiply and one reduction. The whole [B, L, n] batch runs in log2(n) stage launches per transform.

Measured

Forward NTT, batch of 16 polynomials over 8 primes, transforms per second:

n L4 (sm_89) H200 (sm_90) RTX PRO 6000 (sm_120)
4096 1.37 M 2.05 M 2.34 M
8192 0.72 M 1.27 M 1.51 M

The transform is memory-bound integer work with no tensor-core dependency, so it runs on every architecture from sm_80 to sm_120.

Correctness

  • Roundtrip identity: INTT(NTT(a)) == a bitwise for n from 256 to 8192 and primes from 14 to 59 bits.
  • Polynomial multiply: poly_mul(a, b) equals the schoolbook negacyclic convolution in Z_q[X]/(X^n + 1) exactly, per prime, at 59-bit primes.
  • RNS consistency: across an RNS base the per-prime results CRT-reconstruct to the exact integer negacyclic product (a ~2^175 modulus reproducing the full bignum convolution), so the independent single-prime transforms are jointly correct.
  • Deterministic: integer arithmetic; repeated runs are bitwise identical.

Requirements and limits

  • NVIDIA GPU with compute capability 8.0+.
  • n a power of two; primes < 2^62, each = 1 (mod 2n) so the 2n-th root exists; coefficients int64 in [0, q).
  • The transform is per-prime and per-polynomial independent; supply the RNS base as the L axis and batch polynomials on the B axis.

References

Longa and Naehrig, "Speeding up the Number Theoretic Transform for Faster Ideal Lattice-Based Cryptography" (2016); Harvey, "Faster arithmetic for number-theoretic transforms" (2014); the negacyclic RNS NTT of Microsoft SEAL and OpenFHE.

License

Apache-2.0.

Downloads last month
-
apache-2.0
Supported hardwares new
CUDA
8.08.68.99.010.012.0
GPU
B300
288GB
NVIDIA SXM
B200
192GB
NVIDIA SXM
H200
141GB
NVIDIA SXM
H100
80GB
GPU
H800
80GB
GPU
H20
96GB
GPU
L40s
48GB
GPU
L40
48GB
GPU
L20
48GB
GPU
L4
24GB
DGX Spark
GB10
128GB
GPU
RTX PRO 6000 WS
96GB
GPU
RTX PRO 6000 Max-Q
96GB
GPU
RTX PRO 5000
48GB
GPU
RTX PRO 4500 WS
32GB
GPU
RTX PRO 4000
24GB
GPU
RTX PRO 4000 SFF
24GB
GPU
RTX PRO 2000
16GB
GPU
RTX 6000 Ada
48GB
GPU
RTX 5880 Ada
48GB
RTX
RTX 5000 Ada
32GB
GPU
RTX 4500 Ada
24GB
RTX
RTX 4000 Ada
20GB
RTX
RTX 4000 SFF Ada
20GB
GPU
RTX 3500 Ada Mobile
12GB
GPU
RTX 2000 Ada
16GB
GPU
RTX A6000
48GB
GPU
RTX A5000
8GB
GPU
RTX A5000 Max-Q
16GB
GPU
RTX A5000 Mobile
16GB
GPU
RTX A4000
16GB
GPU
RTX A4000 Max-Q
8GB
GPU
RTX A4000 Mobile
8GB
GPU
RTX A3000 Mobile
6GB
GPU
RTX A2000
6GB
GPU
RTX A2000 Embedded
4GB
GPU
RTX A2000 Max-Q
4GB
GPU
RTX A2000 Mobile
4GB
GPU
A800
40GB
GPU
A100
80GB
GPU
A40
48GB
GPU
A30
24GB
GPU
A10
24GB
GPU
A2
16GB
RTX
RTX 5090
32GB
RTX
RTX 5090 D
32GB
RTX
RTX 5090 Mobile
24GB
RTX
RTX 5080
16GB
RTX
RTX 5080 Mobile
16GB
RTX
RTX 5070
12GB
RTX
RTX 5070 Mobile
8GB
RTX
RTX 5070 Ti
16GB
RTX
RTX 5070 Ti Mobile
12GB
RTX
RTX 5060 Ti
16GB
RTX
RTX 5060
8GB
RTX
RTX 5060 Mobile
8GB
RTX
RTX 5050
8GB
RTX
RTX 5050 Mobile
8GB
RTX
RTX 4090
24GB
RTX
RTX 4090D
24GB
RTX
RTX 4090 Mobile
16GB
RTX
RTX 4080 SUPER
16GB
RTX
RTX 4080
16GB
RTX
RTX 4080 Mobile
12GB
RTX
RTX 4070
12GB
RTX
RTX 4070 Mobile
8GB
RTX
RTX 4070 Ti
12GB
RTX
RTX 4070 Super
12GB
RTX
RTX 4070 Ti Super
16GB
RTX
RTX 4060
8GB
RTX
RTX 4060 Ti
8GB
RTX
RTX 4090 Laptop
16GB
RTX
RTX 4080 Laptop
12GB
RTX
RTX 4070 Laptop
8GB
RTX
RTX 4060 Laptop
8GB
RTX
RTX 4050 Laptop
6GB
RTX
RTX 3090
24GB
RTX
RTX 3090 Ti
24GB
RTX
RTX 3080
12GB
RTX
RTX 3080 Ti
12GB
RTX
RTX 3080 Mobile
16GB
RTX
RTX 3070
8GB
RTX
RTX 3070 Ti
8GB
RTX
RTX 3070 Ti Mobile
8GB
RTX
RTX 3060 Ti
8GB
RTX
RTX 3060
12GB
RTX
RTX 3060 Mobile
6GB
RTX
RTX 3050 Mobile
4GB
GPU
RTX 2050 Mobile
4GB
Jetson
Jetson AGX Orin 64GB
64GB
Jetson
Jetson AGX Orin 32GB
32GB
Jetson
Jetson Orin NX 16GB
16GB
Jetson
Jetson Orin NX 8GB
8GB
Jetson
Jetson Orin Nano 8GB
8GB
Jetson
Jetson Orin Nano 4GB
4GB
OS
linux
Arch
x86_64
Kernel Builder
570dcf4