Post · 7 min read· pp. 54–56

Karatsuba Multiplication of Dense Matrices over GF(2^e)

Elements in GF(2^e) can be represented as c_0 a^0 + c_1 a^1 + \dots + c_{e-1} a^{e-1} where a is a root of the primitive polynomial of GF(2^e) and c_i are elements in GF(2). Thus, we can rewrite a matrix over GF(2^e) as a list of e matrices over GF(2) which contain the coefficients for each degree of a. To put it in another way: we can turn the matrix with polynomial entries into a polynomial with matrix coefficients. In Bitslicing and the Method of Four Russians Over Larger Finite Fields Tomas J. Boothby and Robert Bradshaw (both Sage developers from Seattle by the way) proposed to use this representation to perform matrix multiplication.

#+HTML <!–more–>

As an example consider GF(2^2) with the primitive polynomial x^2 + x + 1. Suppose we want to compute C = AB. The matrix A can be represented as A_0x + A_1 and the matrix B can be represented as B_0x + B_1. Their product is C=A_0B_0x2+(A_0B_1+A_1B_0)x+A_1B_1.C = A\_0B\_0x^2 + (A\_0B\_1 + A\_1B\_0)x + A\_1B\_1.

Finally, reduction modulo x^2 + x + 1 gives

(A_0B_0 + A_0B_1 + A_1B_0)x + A_1B_1 + A_0B_0.

This product thus costs 4 multiplications over GF(2) and three additions over GF(2).

Optionally, this last expression can be rewritten as

((A_0 + A_1)(B_0 + B_1) + A_1B_1)x + A_1B_1 + A_0B_0

using the divide and conquer trick Karatsuba introduced in the 60s. Thus this multiplication costs 3 matrix multiplications over  GF(2) and 4 matrix additions over GF(2).

To see why this strategy for matrix multiplication might be efficient, consider the following table which shows

  1. the CPU time for multiplying dense 4,000 x 4,000 matrices over GF(2^e),
  2. to how many matrix multiplications over GF(2) this corresponds,
  3. how many GF(2) multiplications naive polynomial multiplication would need and
  4. how many Karatsuba needs.
eCPU timeGF(2) factornaiveKaratsuba
20.6649.22243
31.08416.9379
41.28817.889169
53.45650.82425
64.39664.64736
76.08084.44449
811.117163.4716427
937.842556.47381
1047.143620.261100

All times were taken on my Intel i7 powering my Macbook Pro using the M4RI and M4RIE libraries for GF(2) and GF(2^e) respectively.

So much for the theoretical complexity. Yesterday, I implemented this multiplication routine in M4RIE for GF(2^2). The tables below give the CPU time for multiplication of dense n \times n matrices over GF(2^2)

  1. for the old M4RIE Travolta + Strassen code,
  2. the new Karatsuba based code and
  3. how long three matrix multiplications over GF(2) take.

The second column includes the time needed for converting back and forth between the M4RIE matrix layout and the bitsliced representation needed for Karatsuba. The first table is again for my Macbook Pro’s Intel i7 clocked at 2.6Ghz:

nStrassen + TravoltaKaratsuba3 * GF(2) CPU time
10000.0120.0120.012
20000.0680.0520.024
30000.2240.1360.096
40000.6480.2800.336
50001.1440.5200.432
60001.9520.9841.008
70003.2721.4441.632
80004.9762.0762.484
90006.4442.7842.628
100008.7613.6683.528

The second table is for an old AMD Opteron 885 clocked at 2.6Ghz :

nStrassen + TravoltaKaratsuba3 * GF(2) CPU time
10000.1000.0360.024
20000.7240.1080.084
30002.0760.3360.264
40004.9960.7360.636
50009.5091.3601.116
600014.9892.4162.340
700022.9853.2763.204
800035.3024.7884.692
900047.6956.3045.892
1000064.7129.1017.980

A third table for a new Opteron 8439 SE (redhaw):

nStrassen + TravoltaKaratsuba3 * GF(2) CPU time
10000.0200.0200.060
20000.1400.0700.030
30000.4700.2000.150
40001.1200.4800.390
50002.0900.8700.690
60003.4901.5001.260
70005.4402.2701.950
80008.0503.2302.850
900010.7104.5604.140
1000014.5805.7705.190

Ignoring measurement imprecisions (especially in the first table) it is clear that this new approach is much faster than the old one implemented in M4RIE especially on the Opteron. However, it seems on the Opteron our conversion between M4RIE and Karatsuba has a considerable cost, input how to improve that would be very welcome since I’m out of ideas for now. To compare with Magma: for the 10,000 \times 10,000 case Magma takes 9.18 seconds on the i7 and 11.8 seconds on the Opteron 858. I didn’t compare with Tom and Robert’s implementation but I expect them to essentially be at 3 matrix multiplications over GF(2) since they have no conversion overhead.

I should point out though that the code for GF(2^2) is the least efficient in M4RIE since I only implemented 8 parallel Travolta table which means that over GF(2^2) only 8 \cdot 2 = 16 bits are dealt with at each step in the inner loop. We could use more tables to fill up L1 and we could also implement Kronrod’s method aka M4RM for GF(2^2).

While I expect that we could catch up to Karatsuba at least on Intel CPUs over GF(2^2), I assume that the Karatsuba timings are close to optimal since matrix multiplication in M4RI seems to be close to optimal at least without further tricks being discovered and 3 matrix multiplications seems to be the best one can do for degree two polynomials.

I guess I’ll implement Karatsuba for GF(2^3) and GF(2^4), but I’m not sure I can be asked to do it for bigger fields if I don’t figure out a nice generic way of implementing it where I don’t have to write code for each minimal polynomial etc.