Schönhage–Strassen algorithm explained
. 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.[2] In 2007, Martin Fürer published an algorithm with faster asymptotic complexity.[3] 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).
[4] 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.[5] [6]
Description
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 primitive
th 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 a
fast Fourier transform.
Let
\widehatCi=\widehatAi\widehatBi
(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
\widehatCi=\widehatAi\widehatBi
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.
Details
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
:
ck=\sum(i,j):i+j=k{aibj}=
{aibk-i
} ,we have a convolution.
By using FFT (fast Fourier transform), used in the original version rather than NTT (Number-theoretic transform),[7] with convolution rule; we get
\hat{f}(a*b)=
aibk-i\right)=\hat{f}(a)\bullet\hat{f}(b).
That is;
, where
is the corresponding coefficient in Fourier space. This can also be written as:
fft(a*b)=fft(a)\bulletfft(b)
.
We have the same coefficients due to linearity under the Fourier transform, and because these polynomials only consist of one unique term per coefficient:
\hat{f}(xn)=\left(
\right)n\delta(n)
and
\hat{f}(aX(\xi)+bY(\xi))=a\hat{X}(\xi)+b\hat{Y}(\xi)
Convolution rule:
\hat{f}(X*Y)= \hat{f}(X)\bullet\hat{f}(Y)
We have reduced our convolution problemto product problem, through FFT.
By finding the FFT of the polynomial interpolation of each
, one can determine the desired coefficients.
This algorithm uses the divide-and-conquer method to divide the problem into subproblems.
Convolution under mod N
} a_ib_j , where
.
By letting:
and
where
is the n
th root, one sees that:
[8] \begin{align}
Ck&=\sum(i,j):i+j=k
} a_ib_j = \theta^ \sum_ a_i'b_j' \\[6pt]& = \theta^ \left(\sum_ a_i'b_j' + \sum_ a_i'b_j' \right) \\[6pt]& = \theta^\left(\sum_ a_ib_j\theta^k + \sum_ a_ib_j\theta^ \right) \\[6pt]& = \sum_ a_ib_j + \theta^n \sum_ a_ib_j.\end
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:
} = \sum_ a_ib_j - \sum_ a_ib_j
In a normal FFT which operates over complex numbers, one would use:
\begin{align}
Ck&=\theta-k\left(\sum(i,j):i+j=kaib
+\sum(i,j):i+j=k+naibj\thetan+k\right)\\[6pt]
&=e-i2\pi\left(\sum(i,j):i+j=kaib
+\sum(i,j):i+j=k+naib
\right)\end{align}
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 field, is an element a such that
or
. For example, where is a
prime number, gives
.
Notice that
in
and
in
\operatorname{GF}(2n+2+1)
. 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 a root of unity of a finite field.
To find FFT/NTT transform, we do the following:
\begin{align}
Ck'&=\hat{f}(k)=\hat{f}\left(\theta-k\left(\sum(i,j):i+j=kaib
+\sum(i,j):i+j=k+naib
\right)\right)\\[6pt]
Ck+k'&=\hat{f}(k+k)=\hat{f}\left(\sum(i,j):i+j=2kaibj\thetak+\sum(i,j):i+j=n+2kaibj\thetan+k\right)\\[6pt]
&=\hat{f}\left(\sum(i,j):i+j=2kaibj\thetak+\sum(i,j):i+j=2k+naibj\thetan+k\right)\\[6pt]
&=\hat{f}\left(Ak\right)\bullet\hat{f}(Bk)+\hat{f}(Ak)\bullet\hat{f}(Bk)
\end{align}
First product gives contribution to
, for each . Second gives contribution to
, due to
mod
.
To do the inverse:
} (\theta^ C_') or
}(\theta^ C_') depending whether data needs to be normalized.
One multiplies by
to normalize FFT data into a specific range, where
, where is found using the
modular multiplicative inverse.
Implementation details
Why N = 2 + 1 mod N
In Schönhage–Strassen algorithm,
. This should be thought of as a binary tree, where one have values in
. By letting
, for each 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 depth in a tree with
Notice that
, for some L. This makes N a
Fermat 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 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
In search of another N
Doing several mod calculations against different, can be helpful when it comes to solving integer product. By using the Chinese remainder theorem, after splitting into smaller different types of, one can find the answer of multiplication [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]
}, where is a number such that there exists an where
}, assuming
Furthermore;
}, where is an element that generates elements in
in a cyclic manner.
If
, where
, then
.
How to choose K for a specific N
The following formula is helpful, finding a proper (number of groups to divide bits into) given bit size by calculating efficiency :[13]
is bit size (the one used in
) at outermost level. gives
groups of bits, where
.
is found through and by finding the smallest, such that
If one assume efficiency above 50%,
and is very small compared to rest of formula; one get
This means: When something is very effective; is bound above by
or asymptotically bound above by
Pseudocode
Following alogithm, the standard Modular Schönhage-Strassen Multiplication algorithm (with some optimizations), is found in overview through [14]
- T3MUL = Toom–Cook multiplication
- SMUL = Schönhage–Strassen multiplication
- Evaluate = FFT/IFFT
Further study
For implemantion details, one can read the book Prime Numbers: A Computational Perspective.[15] This variant differs somewhat from Schönhage's original method in that it exploits the discrete weighted transform to perform negacyclic convolutions more efficiently. Another source for detailed information is Knuth's The Art of Computer Programming.[16]
Optimizations
This section explains a number of important practical optimizations, when implementing Schönhage–Strassen.
Use of other multiplications algorithm, inside algorithm
Below a certain cutoff point, it's more efficient to use other multiplication algorithms, such as Toom–Cook multiplication.[17]
Square root of 2 trick
The idea is to use
as a
root 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] Granlund's trick
By letting
, one can compute
and
. In combination with CRT (Chinese Remainder Theorem) to find exact values of multiplication
[19] Notes and References
- Schönhage . Arnold . Arnold Schönhage . Strassen . Volker . Volker Strassen . 1971 . Schnelle Multiplikation großer Zahlen . de . Fast multiplication of large numbers . Computing . 7 . 3–4 . 281–292 . 10.1007/BF02242355 . 9738629 .
- Karatsuba multiplication has asymptotic complexity of about
and Toom–Cook multiplication has asymptotic complexity of about
Rodney . Van Meter . Kohei M. . Itoh . Fast Quantum Modular Exponentiation . Physical Review . 71 . 2005 . 5 . 052320. 10.1103/PhysRevA.71.052320 . quant-ph/0408006 . 2005PhRvA..71e2320V . 14983569 . A discussion of practical crossover points between various algorithms can be found in: Overview of Magma V2.9 Features, arithmetic section Luis Carlos Coronado García, " Can Schönhage multiplication speed up the RSA encryption or decryption? Archived", University of Technology, Darmstadt (2005)The GNU 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:Web site: FFT Multiplication (GNU MP 6.2.1). 2021-07-20. gmplib.org. Web site: MUL_FFT_THRESHOLD. GMP developers' corner. 3 November 2011. dead. https://web.archive.org/web/20101124062232/http://gmplib.org/devel/MUL_FFT_THRESHOLD.html. 24 November 2010. Web site: MUL_FFT_THRESHOLD. 2021-07-20. gmplib.org.
- Fürer's algorithm has asymptotic complexity Fürer . Martin . 2007 . Faster Integer Multiplication . Proc. STOC '07 . Symposium on Theory of Computing, San Diego, Jun 2007 . 57–66 . dead . https://web.archive.org/web/20070305200654id_/http://www.cse.psu.edu/~furer/Papers/mult.pdf . 2007-03-05 . Fürer . Martin . 2009 . Faster Integer Multiplication . SIAM Journal on Computing . 39 . 3 . 979–1005 . 10.1137/070711761 . 0097-5397. Fürer's algorithm is used in the Basic Polynomial Algebra Subprograms (BPAS) open source library. See: Book: Covanov . Svyatoslav . Mohajerani . Davood . Moreno Maza . Marc . Wang . Linxiao . Proceedings of the 2019 on International Symposium on Symbolic and Algebraic Computation . Big Prime Field FFT on Multi-core Processors . 2019-07-08 . https://dl.acm.org/doi/10.1145/3326229.3326273 . en . Beijing China . ACM . 106–113 . 10.1145/3326229.3326273 . 978-1-4503-6084-5. 195848601 .
- Harvey . David . van der Hoeven . Joris . Joris van der Hoeven . 10.4007/annals.2021.193.2.4 . 2 . . 4224716 . 563–617 . Second Series . Integer multiplication in time
. 193 . 2021. 109934776 .
- This method is used in INRIA's ECM library.
- Web site: ECMNET . 2023-04-09 . members.loria.fr.
- Web site: Becker . Hanno . Hwang . Vincent . J. Kannwischer . Matthias . Panny . 2022. Lorenz . Efficient Multiplication of Somewhat Small Integers using Number-Theoretic Transforms .
- Web site: Lüders . 2014. 26. Christoph . Fast Multiplication of Large Integers: Implementation and Analysis of the DKSS Algorithm .
- Book: Kleinberg . Jon . Tardos . Eva . Algorithm Design . 2005 . Pearson . 0-321-29535-8 . 237 . 1.
- Web site: Gaudry . Pierrick . Alexander . Kruppa . Paul . Zimmermann . A GMP-based implementation of Schönhage-Strassen's large integer multiplication algorithm . 6 . 2007.
- Web site: S. Dimitrov . Vassil . V. Cooklev . Todor . D. Donevsky . 2. 1994. Borislav . Generalized Fermat-Mersenne Number Theoretic Transform .
- Web site: S. Dimitrov . Vassil . V. Cooklev . Todor . D. Donevsky . 3. 1994. Borislav . Generalized Fermat-Mersenne Number Theoretic Transform .
- Web site: Gaudry . Pierrick . Kruppa . Alexander . Zimmermann . Paul . A GMP-based Implementation of Schönhage-Strassen's Large Integer Multiplication Algorithm . 2 . 2007.
- Web site: 2014 . 28 . Lüders . Christoph . Fast Multiplication of Large Integers: Implementation and Analysis of the DKSS Algorithm .
- R. Crandall & C. Pomerance. Prime Numbers – A Computational Perspective. Second Edition, Springer, 2005. Section 9.5.6: Schönhage method, p. 502.
- Book: Knuth, Donald E. . . 1997 . 2: Seminumerical Algorithms . 3rd . Addison-Wesley . 0-201-89684-2 . § 4.3.3.C: Discrete Fourier transforms . 305–311 . https://archive.org/details/artofcomputerpro0002knut_u2o0/page/305/ .
- Web site: Gaudry . Pierrick . Kruppa . Alexander . Zimmermann . Paul . 7. 2007. A GMP-based implementation of Schönhage-Strassen's large integer multiplication algorithm .
- Web site: Gaudry . Pierrick . Kruppa . Alexander . 6 . 2007 . Zimmermann . Paul . A GMP-based implementation of Schönhage-Strassen's large integer multiplication algorithm .
- Web site: Gaudry . Pierrick . Kruppa . 6. 2007. Alexander . Zimmermann . Paul . A GMP-based implementation of Schönhage-Strassen's large integer multiplication algorithm .