The önhage–Schönhage–Strassen algorithm is an asymptotically fast multiplication algorithm for large integers, published by Arnold Schönhage and Volker Strassen in 1971. It works by recursively applying fast Fourier transform (FFT) over the integers modulo 2n+1. The run-time bit complexity to multiply two -digit numbers using the algorithm is in big notation.
The Schönhage–Strassen algorithm was the asymptotically fastest multiplication method known from 1971 until 2007. It is asymptotically faster than older methods such as Karatsuba and Toom–Cook multiplication, and starts to outperform them in practice for numbers beyond about 10,000 to 100,000 decimal digits. In 2007, Martin Fürer published an algorithm with faster asymptotic complexity. In 2019, David Harvey and Joris van der Hoeven demonstrated that multi-digit multiplication has theoretical complexity; however, their algorithm has constant factors which make it impossibly slow for any conceivable practical problem (see galactic algorithm).
Applications of the Schönhage–Strassen algorithm include large computations done for their own sake such as the Great Internet Mersenne Prime Search and approximations of , as well as practical applications such as Lenstra elliptic curve factorization via Kronecker substitution, which reduces polynomial multiplication to integer multiplication.
Description
Every number in base B, can be written as a polynomial:
Furthermore, multiplication of two numbers could be thought of as a product of two polynomials:
Because,for : , we have a convolution.
By using FFT (Fast Fourier transform), used in original version rather than NTT, with convolution rule; we get
. That is; , where is the corresponding coefficient in fourier space. This can also be written as: fft(a * b) = fft(a) ● fft(b).
We have the same coefficients due to linearity under Fourier transform, and because these polynomials only consist of one unique term per coefficient:
and
Convolution rule:
We have reduced our convolution problem
to product problem, through FFT.
By finding ifft (polynomial interpolation), for each , one get the desired coefficients.
Algorithm uses divide and conquer strategy, to divide problem to subproblems.
Convolution under mod N
, where and in Schönhage–Strassen algorithm.
By letting:
and , where is the n-th root
One see that:
.
This mean, one can use weight , and then multiply with after.
Instead of using weight; one can due to , in first step of recursion (when ), calculate :
In normal FFT, that operates over complex numbers, one would use:
However, FFT can also be used as a NTT (number theoretic transformation) in Schönhage–Strassen. This means that we have to use that generate numbers in a finite field (for example ).
A root of unity under a finite field , is an element a such that or . For example , where is a prime, gives .
Notice that in and in . For these candiadates, under its finite field, and therefore act the way we want .
Same FFT algorithms can still be used, though, as long as is root of unity of a finite field.
To find FFT/NTT transform, we do the following:
First product gives contribution to , for each . Second gives contribution to , due to mod .
To do the inverse:
or
depending on whether fft one use normalize data or not.
One multiply by , to normailize fft data to a specific range, where mod , where is found using modular multiplicative inverse.
Implementation details
Why N = 2 + 1 in mod N
In Schönhage–Strassen algorithm, . One should think of this as a binary tree, where one have values in . By letting , one can for each find all : One can group all pairs into M different groups. Using to group pairs through convolution, is a classical problem in algorithms. For example: Let k be total income and be mans income and womans income; by using convolution, one can group into groups based on desired total income.
Having this in mind, help us to group into groups, for each group of subtasks in depth ; in tree with
Notice that , for some L. This is Fermat number. When doing mod , we have something called Fermat ring.
Because some Fermat numbers are Fermat primes, one can in some cases avoid calculations.
There are other N that could have been used, of course, with same prime number advantages. By letting , one have the maximal number in a binary number with bits. is a Mersenne number, that in some cases is a Mersenne prime. It is a natural candidate against Fermat number