Good afternoon!
I am trying to develop an NTT algorithm based on the naive recursive FFT implementation I already have.
Consider the following code (coefficients
' length, let it be m
, is an exact power of two):
/// <summary>
/// Calculates the result of the recursive Number Theoretic Transform.
/// </summary>
/// <param name="coefficients"></param>
/// <returns></returns>
private static BigInteger[] Recursive_NTT_Skeleton(
IList<BigInteger> coefficients,
IList<BigInteger> rootsOfUnity,
int step,
int offset)
{
// Calculate the length of vectors at the current step of recursion.
// -
int n = coefficients.Count / step - offset / step;
if (n == 1)
{
return new BigInteger[] { coefficients[offset] };
}
BigInteger[] results = new BigInteger[n];
IList<BigInteger> resultEvens =
Recursive_NTT_Skeleton(coefficients, rootsOfUnity, step * 2, offset);
IList<BigInteger> resultOdds =
Recursive_NTT_Skeleton(coefficients, rootsOfUnity, step * 2, offset + step);
for (int k = 0; k < n / 2; k++)
{
BigInteger bfly = (rootsOfUnity[k * step] * resultOdds[k]) % NTT_MODULUS;
results[k] = (resultEvens[k] + bfly) % NTT_MODULUS;
results[k + n / 2] = (resultEvens[k] - bfly) % NTT_MODULUS;
}
return results;
}
It worked for complex FFT (replace BigInteger
with a complex numeric type (I had my own)). It doesn't work here even though I changed the procedure of finding the primitive roots of unity appropriately.
Supposedly, the problem is this: rootsOfUnity
parameter passed originally contained only the first half of m
-th complex roots of unity in this order:
omega^0 = 1, omega^1, omega^2, ..., omega^(n/2)
It was enough, because on these three lines of code:
BigInteger bfly = (rootsOfUnity[k * step] * resultOdds[k]) % NTT_MODULUS;
results[k] = (resultEvens[k] + bfly) % NTT_MODULUS;
results[k + n / 2] = (resultEvens[k] - bfly) % NTT_MODULUS;
I originally made use of the fact, that at any level of recursion (for any n
and i
), the complex root of unity -omega^(i) = omega^(i + n/2)
.
However, that property obviously doesn't hold in finite fields. But is there any analogue of it which would allow me to still compute only the first half of the roots?
Or should I extend the cycle from n/2
to n
and pre-compute all the m
-th roots of unity?
Maybe there are other problems with this code?..
Thank you very much in advance!
I recently wanted to implement NTT for fast multiplication instead of DFFT too. Read a lot of confusing things, different letters everywhere and no simple solution, and also my finite fields knowledge is rusty , but today i finally got it right (after 2 days of trying and analog-ing with DFT coefficients) so here are my insights for NTT:
Computation
where
X[]
is NTT transformedx[]
of sizen
whereWn
is the NTT basis. All computations are on integer modulo arithmeticsmod p
no complex numbers anywhere.Important values
Wn = r ^ L mod p
is basis for NTTWn = r ^ (p-1-L) mod p
is basis for INTTRn = n ^ (p-2) mod p
is scaling multiplicative constant for INTT~(1/n)
p
is prime thatp mod n == 1
andp>max'
max
is max value of x[i] for NTT or X[i] for INTTr = <1,p)
L = <1,p)
and also dividesp-1
r,L
must be combined sor^(L*i) mod p == 1
ifi=0
ori=n
r,L
must be combined sor^(L*i) mod p != 1
if0 < i < n
max'
is the sub-result max value and depends onn
and type of computation. For single (I)NTT it ismax' = n*max
but for convolution of twon
sized vectors it ismax' = n*max*max
etc. See Implementing FFT over finite fields for more info about it.functional combination of
r,L,p
is different for differentn
this is important, you have to recompute or select parameters from table before each NTT layer (
n
is always half of the previous recursion).Here is my C++ code that finds the
r,L,p
parameters (needs modular arithmetics which is not included, you can replace it with (a+b)%c,(a-b)%c,(a*b)%c,... but in that case beware of overflows especial formodpow
andmodmul
) The code is not optimized yet there are ways to speed it up considerably. Also prime table is fairly limited so either use SoE or any other algo to obtain primes up tomax'
in order to work safely.and here is my slow NTT and INTT implementations (i havent got to fast NTT,INTT yet) they are both tested with Schönhage–Strassen multiplication successfully.
dst
is destination arraysrc
is source arrayn
is array sizem
is modulus (p
)w
is basis (Wn
)hope this helps to someone. If i forgot something please write ...
[edit1: fast NTT/INTT]
Finally I manage to get fast NTT/INTT to work. Was little bit more tricky than normal FFT:
[edit3]
I have optimized my code (3x times faster than code above),but still i am not satisfied with it so i started new question with it. There I have optimized my code even further (about 40x times faster than code above) so its almost the same speed as FFT on floating point of the same bit size. Link to it is here: