Number Theoretic Transform
Introduction
Number-theoretic transform (NTT) is the implementation of the discrete Fourier transform (DFT) over number-theoretic foundations; fast number-theoretic transform (FNTT) is the implementation of the fast Fourier transform (FFT) over number-theoretic foundations.
Number-theoretic transform is a fast algorithm for computing convolution. The most commonly used algorithm includes the aforementioned fast Fourier transform. However, FFT has some implementation drawbacks. For example, the data vector must be multiplied by a matrix of complex coefficients, and the real and imaginary parts of each complex coefficient are sine and cosine functions. Therefore, most coefficients are floating-point numbers, meaning that complex and floating-point arithmetic must be performed, resulting in higher computational complexity and larger errors due to floating-point operations.
NTT addresses polynomial multiplication with a modulus, which imposes certain restrictions and often involves large numbers. Currently, the most common modulus is 998244353.
Prerequisites
Learning NTT requires prerequisite knowledge: discrete Fourier transform, generating subgroups, primitive roots, and discrete logarithms. Relevant knowledge can be learned on the corresponding pages and will not be repeated here.
Definition
Number-theoretic Transform
In mathematics, NTT is the discrete Fourier transform (DFT) over an arbitrary ring. In the case of finite fields, it is often called the number-theoretic transform (NTT).
Number-theoretic transform (NTT) is obtained by reducing the discrete Fourier transform to \(F={\mathbb {Z}/p}\), the integers modulo a prime \(p\). This is a finite field. As long as \(n\) divides \(p-1\), there exists a primitive \(n\)-th root of unity, so we have \(p=\xi n+1\) for a positive integer \(\xi\). Specifically, for a prime \(p=qn+1, (n=2^m)\), a primitive root \(g\) satisfies \(g^{qn} \equiv 1 \pmod p\). Treating \(g_n=g^q\pmod p\) as the equivalent of \(\omega_n\), it satisfies similar properties, such as \(g_n^n \equiv 1 \pmod p, g_n^{n/2} \equiv -1 \pmod p\).
Since this involves number-theoretic transformations, \(N\) (to distinguish it from \(n\) in FFT, we denote it as \(N\) here) can be larger than \(n\) in FFT. However, simply treat \(\frac{qN}{n}\) as \(q\) here to avoid size issues.
Common examples include:
Here, \(g^{qn}\) is equivalent to \(\mathrm{e}^{2\pi \mathrm{i} n}\).
When iterating to length \(l\), \(g_l = g^{\frac{p-1}{l}}\), or \(\omega_n = g_l = g_N^{\frac{N}{l}} = g_N^{\frac{p-1}{l}}\).
Fast Number-theoretic Transform
Fast number-theoretic transform (FNTT) is the fast algorithm obtained by adding divide-and-conquer operations to the number-theoretic transform (NTT).
The divide-and-conquer method used in FNTT is exactly the same as that used in FFT. This means that by making simple modifications to the FFT code, one can obtain the FNTT code.
In competitive programming, the term NTT often actually refers to the fast number-theoretic transform, and it is generally assumed that "number-theoretic transform" means "fast number-theoretic transform."
This abbreviation follows a similar logic to that of the fast Fourier transform. In fact, the term "fast Fourier transform" (FFT) refers to "fast discrete Fourier transform" (FDFT). However, since "fast" can only be applied to discrete cases, specifically when the order of the primitive root of unity is a power of \(2\), and not to continuous cases, the word "discrete" is omitted, turning FDFT into FFT. Thus, FFT always refers to this special discrete case.
Number-theoretic transform or fast number-theoretic transform operates under modulo arithmetic, which is inherently discrete and never continuous, so there is no need to mention the word "discrete."
In the field of algorithms, operations without speed improvements are meaningless. The term DFT is introduced in the context of FFT because DFT has other specific applications in signal processing and image processing, and DFT is also the principle or prerequisite knowledge for FFT.
When no confusion arises, NTT is often used to refer to FNTT. To avoid further confusion in the following discussion, the terms NTT and FNTT are separated below.
The specific relationships among DFT, FFT, NTT, and FNTT are:
-
Based on DFT and NTT, adding divide-and-conquer operations yields FFT and FNTT. The method and principle of divide-and-conquer can be found in the fast Fourier transform article.
-
Based on DFT and FFT, replacing complex addition and multiplication with addition and multiplication modulo \(p\) (generally limited to between \(0\) and \(p-1\)), and replacing the primitive root of unity with a primitive root of the same order modulo \(p\) (with the order being a power of \(2\)), yields NTT and FNTT.
Since the replaced operations only involve addition and multiplication, DFT, FFT, NTT, and FNTT share the same principle and operate on rings satisfying addition and multiplication, without requiring the stricter condition of division operations on fields.
In fact, as long as a primitive root (i.e., a generator in group theory) exists, NTT or FNTT under that modulus can be performed. Considering that moduli \(1\), \(2\), and \(4\) are too small to be practically meaningful, for odd primes \(p\) and positive integers \(\alpha\), as long as a primitive root \(g\) is given for moduli \(p^\alpha\) and \(2p^\alpha\), NTT or FNTT can still be performed using the same method.
Template
Below is a template for large integer multiplication, referenced from here.
Reference Code
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 | |
References and Further Reading
- FWT (Fast Walsh–Hadamard Transform) Zero-Basis Detailed Explanation qaq (ACM/OI)
- FFT (Fast Fourier Transform) Zero-Basis Detailed Explanation! Including NTT (ACM/OI)
- Number-theoretic transform (NTT) - Wikipedia
- Tutorial on FFT/NTT—The tough made simple. (Part 1)
本页面最近更新:,更新历史
发现错误?想一起完善? 在 GitHub 上编辑此页!
本页面贡献者:OI-wiki
本页面的全部内容在 CC BY-SA 4.0 和 SATA 协议之条款下提供,附加条款亦可能应用