Skip to content

About

Fast Multiplication without FFT

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

Fast Multiplication

About

Fast integer multiplication is an algorithm able to perform the multiplication of two n-digit integers using O(n log ⁡n) operations.

Modular multiplication of polynomials is the basis of this method.
An intricate method is often used: the product can be rewritten as a cyclic convolution and is evaluated using Fast Fourier Transforms. It is unnecessary and the purpose of this project is to demystify the fast multiplication for large integers.

Fast modular multiplication of polynomials is a divide-and-conquer algorithm, where factorization and the Chinese remainder theorem are applied recursively. This method is described here: starting from a simple implementation of the mathematical relation, a list of elementary programs illustrates how it can be extended and improved.

The recursion is applied to compute P(x)2 mod x8 − 7, where P is a polynomial of degree 8−1. The field is the complex numbers.
P(x) mod xm − rm and P(x) mod xm + rm are calculated from P(x) mod x2m − r2m. The last step is Lagrange interpolation and we have P(x) mod x − r = P(r). P(r)2 is a scalar. Finally, P(x)2 mod x2m − r2m is calculated from P(x)2 mod xm − rm and P(x)2 mod xm + rm with the Chinese remainder theorem for polynomials.

The algorithm is extended to P(x)2 mod x22 · 32 · 52 − 7. We have x3m − r3m = (xm − rm) · (xm − jrm) · (xm − j2rm) · , where j is a primitive root of x3 − 1. The equivalent relation is applied to x5m − r5m.

Roots are calculated over a prime finite field of order p. There are two conditions:

  • If P(x)2 mod xn − r is evaluated then a nth root of r must be an element in Z/pZ.
  • If the coefficients of P(x) are non-negative integers smaller than b (P(b) is the base-b representation of an integer) then P(x)2 mod xn − r over the natural numbers can be retrieved from the result over Z/pZ if r > 0 and p > n · (b − 1)2. If r < 0 then outputs can be negative integers: the condition is p > 2 n · (b − 1)2 to determine the sign of each coefficient.

Here n = 900, r = 7 and b = 1000. p = 913262401 is chosen: we have 243771734n = 7 (mod p) and p > n · (b − 1)2 = 898200900. Rather than evaluating the square, cube or 5th root of an element, the nth root of unity and the nth root of 7 are computed and roots needed for the algorithm are of the form 1u/n · 7v/n.

The recursion is based on the relation xam − ram = Prod0≤k<a (xm − αkrm), where a is prime and α is a primitive root of xa − 1.
Let X = xm and R = rm. If X − αkR is twisted into Yk − R for each k then at each step all polynomial moduli are identical.
The transformation is a linear map. P(X) mod X − αkR = P(X) mod α−kX − R. Let Yk = α−kX, we have yk = α−k/mx.
If P(x) = Sum0≤i<m ci xi then P(yk) = Sum0≤i<m ci αik/m yki. The coefficients of P(yk) are c'i = αik/m ci. After the recursion, the coefficients of P(yk) are untwisted into P(x) with ci = α−ik/m c'i.

Twisting is the generalization of weighted transforms, where weighting is applied at each step.

With the previous version of the fast multiplication, n/2 multiplications by 1u/n · rv/n are computed at each step. With the twisted form, n/2 multiplications by rv/n and n/2 multiplications by 1u'/n are needed. The twisted transform is as efficient as the non-twisted form only if r = 1.

Twisted FFT was defined in Daniel J. Bernstein, Multidigit multiplication for mathematicians, 2001. It was restricted to r = 1 and n is a power of two.

The twisted fast multiplication is written for r = 1. Roots of r are equal to one and it can be seen that the twist factors αik/m are the twiddle factors of a Fast Fourier Transform.

The transform that splits P(x) mod xn − 1 into P(x) mod x − ωi, where ω is a primitive nth root of unity and 0≤i<n is Gentleman-Sande FFT recursion. The inverse transform that merges P(x)2 mod x − ωi into P(x)2 mod xn − 1 is Cooley-Tukey FFT recursion. Because Gentleman-Sande and Cooley-Tukey diagrams are symmetric, data ordering of the output is irrelevant, even if an iterative FFT algorithm is implemented.

The non-twisted fast multiplication can also be used for the computation of P(x)2 mod xn − 1. It is a different algorithm.
The forward transform was found by Georg Bruun, "z-Transform DFT filters and FFTs, 1978, IEEE Transactions on Acoustics, Speech, and Signal Processing, 26 (1): 56-63. Here, the Discrete Fourier Transform is written as a Z-transform filter. A Discrete Fourier Transform is evaluated with n roots of unity along the z-domain's unit circle. But this restriction does not apply to Z-transforms. If the FFT based multiplication is limited to the polynomial xn − 1 (cyclic convolution), Bruun's method can be applied to any polynomial.

The two algorithms are implemented: because P(x)2 mod xn − 1 is calculated, both are almost equivalent. But the method based on polynomial factorization can compute P(x)2 mod xn − r or P(x)2 mod Φm(xn), where Φm is the mth cyclotomic polynomial, without an extra "weighted" step.

Fast multiplication based on a recursive polynomial factorization is an in-place algorithm. The roots needed at each step can be precomputed. An iterative (non-recursive) version of fastMul_rec_GF.cpp is implemented here. Note that roots are stored in 2-3-5-reversed order: it is a generalization of bit-reversal permutation based on decomposing n into its prime factors. Rather than a division by 2, 3 or 5 at each step of the reverse process, a single division by n is computed at the end of the algorithm.

The number of operations is equal to a FFT based multiplication, where the polynomial of the convolution is x900 − 1. But here the polynomial is x900 − 7.

A full implementation of the fast multiplication of numbers of the form b2n + 1. The probable primes of this form are generated.

The recursion is b2n − (−1) = (b2n−1 − i) (b2n−1 − (−i)) = ..., where i is a root of −1.
The finite field is of order p2, where p is the Mersenne prime 261 − 1. With the irreducible polynomial X2 + 1, we have α2 = −1: operations are similar to the complex numbers. Because i and its negative are the first roots of the recursion, we can use a half length transform over GF(p2).

Three multiplications are needed for a radix-4 butterfly. If the field is GF(p), 2×4 multiplications are necessary for a radix-4 step with 8 values. Four multiplications in GF(p) are required to compute a multiplication in GF(p2). The balance is GF(p): 8 multiplications, GF(p2): 12 multiplications.
But with a prime finite field, p must be of the form k 2n+1 + 1 such that a (n+1)th root of unity exists. With GF(p2), if p = 2q − 1 the order of the multiplicative group is p2 − 1 = 2q+1 p: there is a primitive root of unity of order 2q+1.
Using 64-bit integers, on the one hand two multiplications modulo 264 − 232 + 1 on the other hand three multiplications modulo 261 − 1. The fastest implementation depends on the architecture of the processor.

About

Fast Multiplication without FFT

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages