Lably
Skip to content

Schönhage–Strassen algorithm

Multiplication algorithm

  • 8 min read
  • 4 chapters

Related on Lably

Through Integer, Modular arithmetic and Big O 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 O(nlog⁡n) 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

This section has a simplified version of the algorithm, showing how to compute the product ab of two natural numbers a,b, modulo a number of the form 2n+1, where n=2kM is some fixed number. The integers a,b are to be divided into D=2k blocks of M bits, so in practical implementations, it is important to strike the right balance between the parameters M,k. In any case, this algorithm will provide a way to multiply two positive integers, provided n is chosen so that ab<2n+1.

Let n=DM be the number of bits in the signals a and b, where D=2k is a power of two. Divide the signals a and b into D blocks of M bits each, storing the resulting blocks as arrays A,B (whose entries we shall consider for simplicity as arbitrary precision integers).

We now select a modulus for the Fourier transform, as follows. Let M′ be such that DM′≥2M+k. Also put n′=DM′, and regard the elements of the arrays A,B as (arbitrary precision) integers modulo 2n′+1. Observe that since 2n′+1≥22M+k+1=D22M+1, the modulus is large enough to accommodate any carries that can result from multiplying a and b. Thus, the product ab (modulo 2n+1) can be calculated by evaluating the convolution of A,B. Also, with g=22M′, we have gD/2≡−1(mod2n′+1), and so g is a primitive Dth root of unity modulo 2n′+1.

We now take the discrete Fourier transform of the arrays A,B in the ring Z/(2n′+1)Z, using the root of unity g for the Fourier basis, giving the transformed arrays A^,B^. Because D=2k is a power of two, this can be achieved in logarithmic time using a fast Fourier transform.

Let C^i=A^iB^i (pointwise product), and compute the inverse transform C of the array C^, again using the root of unity g. The array C is now the convolution of the arrays A,B. Finally, the product ab(mod2n+1) is given by evaluating ab≡∑jCj2Mjmod2n+1.

This basic algorithm can be improved in several ways. Firstly, it is not necessary to store the digits of a,b to arbitrary precision, but rather only up to n′+1 bits, which gives a more efficient machine representation of the arrays A,B. 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 C^i=A^iB^i is evaluated. It is therefore advantageous to select the parameters D,M 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 D,M is thus an important area for further optimization of the method.

Sign in to keep reading

3 more chapters are free with a Lably account.

More in Computing and communication