The Schönhage–Strassen algorithm is based on thefast Fourier transform (FFT) method of integer multiplication. This figure demonstrates multiplying 1234 × 5678 = 7006652 using the simple FFT method. Base 10 is used in place of base 2w for illustrative purposes.Schönhage (on the right) and Strassen (on the left) playing chess inOberwolfach, 1979
The Schönhage–Strassen algorithm was theasymptotically fastest multiplication method known from 1971 until 2007. It is asymptotically faster than older methods such asKaratsuba andToom–Cook multiplication, and starts to outperform them in practice for numbers beyond about 10,000 to 100,000 decimal digits.[2] In 2007, Martin Fürer publishedan algorithm with faster asymptotic complexity.[3] In 2019, David Harvey andJoris 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 (seegalactic algorithm).[4]
This section has a simplified version of the algorithm, showing how to compute the product of two natural numbers, modulo a number of the form, where is some fixed number. The integers are to be divided into blocks of bits, so in practical implementations, it is important to strike the right balance between the parameters. In any case, this algorithm will provide a way to multiply two positive integers, provided is chosen so that.
Let be the number of bits in the signals and, where is a power of two. Divide the signals and into blocks of bits each, storing the resulting blocks as arrays (whose entries we shall consider for simplicity as arbitrary precision integers).
We now select a modulus for the Fourier transform, as follows. Let be such that. Also put, and regard the elements of the arrays as (arbitrary precision) integers modulo. Observe that since, the modulus is large enough to accommodate any carries that can result from multiplying and. Thus, the product (modulo) can be calculated by evaluating the convolution of. Also, with, we have, and so is a primitiveth root of unity modulo.
We now take the discrete Fourier transform of the arrays in the ring, using the root of unity for the Fourier basis, giving the transformed arrays. Because is a power of two, this can be achieved in logarithmic time using afast Fourier transform.
Let (pointwise product), and compute the inverse transform of the array, again using the root of unity. The array is now the convolution of the arrays. Finally, the product is given by evaluating
This basic algorithm can be improved in several ways. Firstly, it is not necessary to store the digits of to arbitrary precision, but rather only up to bits, which gives a more efficient machine representation of the arrays. Secondly, it is clear that the multiplications in the forward transforms are simple bit shifts. With some care, it is also possible to compute the inverse transform using only shifts. Taking care, it is thus possible to eliminate any true multiplications from the algorithm except for where the pointwise product is evaluated. It is therefore advantageous to select the parameters so that this pointwise product can be performed efficiently, either because it is a single machine word or using some optimized algorithm for multiplying integers of a (ideally small) number of words. Selecting the parameters is thus an important area for further optimization of the method.
That is;, whereis the corresponding coefficient in Fourier space. This can also be written as:.
We have the same coefficients due to linearity under the Fourier transform, and because these polynomials only consist of one unique term per coefficient:
and
Convolution rule:
We have reduced our convolution problemto product problem, through FFT.
By finding the FFT of thepolynomial interpolation of each, one can determine the desired coefficients.
This mean, one can use weight, and then multiply with after.
Instead of using weight, as, in first step of recursion (when), one can calculate:
In a normal FFT which 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 θ to generate numbers in a finite field (for example).
A root of unity under a finite fieldGF(r), is an element a such that or. For exampleGF(p), wherep is aprime number, gives.
Notice that in and in. For these candidates, under its finite field, and therefore act the way we want .
Same FFT algorithms can still be used, though, as long asθ is aroot of unity of a finite field.
To find FFT/NTT transform, we do the following:
First product gives contribution to, for eachk. Second gives contribution to, due to mod.
To do the inverse:
or
depending whether data needs to be normalized.
One multiplies by to normalize FFT data into a specific range, where, wherem is found using themodular multiplicative inverse.
In Schönhage–Strassen algorithm,. This should be thought of as a binary tree, where one have values in. By letting, for eachK one can find all, and group all pairs into M different groups. Using to group pairs through convolution is a classical problem in algorithms.[9]
Having this in mind, help us to group into groups for each group of subtasks in depthk in a tree with
Notice that, for some L. This makes N aFermat number. When doing mod, we have a Fermat ring.
Because some Fermat numbers are Fermat primes, one can in some cases avoid calculations.
There are otherN 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
Doing several mod calculations against differentN, can be helpful when it comes to solving integer product. By using theChinese remainder theorem, after splittingM into smaller different types ofN, one can find the answer of multiplicationxy[10]
Fermat numbers and Mersenne numbers are just two types of numbers, in something called generalized Fermat Mersenne number (GSM); with formula:[11]
In this formula, is a Fermat number, and is a Mersenne number.
This formula can be used to generate sets of equations, that can be used in CRT (Chinese remainder theorem):[12]
, whereg is a number such that there exists anx where, assuming
Furthermore;, wherea is an element that generates elements in in a cyclic manner.
Following algorithm, the standard Modular Schönhage-Strassen Multiplication algorithm (with some optimizations), is found in overview through[14]
Split both input numbersa andb into n coefficients of s bits each.
Use at least bits to store them,
to allow encoding of the value
Weight both coefficient vectors according to (2.24) with powers ofθ by performing cyclic shifts on them.
Shuffle the coefficients and .
Evaluate and . Multiplications by powers of ω are cyclic shifts.
Don pointwise multiplications in. If SMUL is used recursively, provideK as parameter. Otherwise, use some other multiplication function like T3MUL and reduce modulo afterwards.
Shuffle the product coefficients.
Evaluate the product coefficients.
Apply the counterweights to the according to (2.25). Since it follows that
Normalize the with (again a cyclic shift).
Add up the and propagate the carries. Make sure to properly handle negative coefficients.
The idea is to use as aroot of unity of order in finite field (it is a solution to equation), when weighting values in NTT (number theoretic transformation) approach. It has been shown to save 10% in integer multiplication time.[18]
TheGNU Multi-Precision Library uses it for values of at least 1728 to 7808 64-bit words (33,000 to 150,000 decimal digits), depending on architecture. See:
^Fürer's algorithm has asymptotic complexityFürer, Martin (2007)."Faster Integer Multiplication"(PDF).Proc. STOC '07. Symposium on Theory of Computing, San Diego, Jun 2007. pp. 57–66.Archived(PDF) from the original on 2007-03-05. Retrieved2007-10-02.Fürer, Martin (2009). "Faster Integer Multiplication".SIAM Journal on Computing.39 (3):979–1005.doi:10.1137/070711761.ISSN0097-5397.
^R. Crandall & C. Pomerance.Prime Numbers – A Computational Perspective. Second Edition, Springer, 2005. Section 9.5.6: Schönhage method, p. 502.ISBN0-387-94777-9