Speed

Multiply With a Reduction

Bytes multiply as polynomials over GF(2); the product has up to 15 bits and is reduced modulo the field polynomial 285 (0x11d).

The question

Two bytes multiplied as polynomials can give a result with 15 bits, and the field has only 256 elements. How does the product come back to one byte? The answer is a reduction by a fixed polynomial, the way a number is reduced modulo a prime. This lesson builds the multiplication of GF(2^8) from the addition of the last lesson. The bytes are constructed, nothing here is secret, and no OpenSSL call is used.

Work it by hand

RFC 5510, section 8.1, says that the multiplication is the multiplication modulo a given irreducible polynomial of degree m, and lists for m = 8 the vector 101110001, which is 1 + x^2 + x^3 + x^4 + x^8. As a number with x^8 as bit 8 that is 0x11d, 285.

Shift and add: for each bit of one factor that is set, add the other factor shifted left by that many places, and whenever a shifted value reaches 256, add 285 to it (add means XOR). Take 0x90 times 2. The factor 2 has one bit set, at place 1. Shifted once, 0x90 is 0x120. That is 256 or more, so XOR 0x11d: 0x120 xor 0x11d is 0x3d.

Run it

The demo multiplies 0x57 by 0x83 another way: it first builds the unreduced product, then removes the high bits one at a time, like a long division.

A table of the product of 0x57 and 0x83 as polynomials: the unreduced product, then one row for every bit from 14 down to 8 that was set and was cleared by adding the polynomial 285 shifted. The values are shown as four hex digits; the last row is 0031. The answer is 2, the number of reduction steps.
Watch the steps (recorded video)
fn xr(a: int, b: int) -> int {
  let r = 0; let bit = 1;
  for (i in range(0, 16)) {
    if ((a / bit) % 2 != (b / bit) % 2) { r = r + bit; }
    bit = bit * 2;
  }
  return r;
}
let rows = str_list(); let p = 0; let steps = 0;
for (i in range(0, 8)) {
  if ((right / math_pow(2, i)) % 2 == 1) { p = xr(p, left * math_pow(2, i)); }
}
rows.add("product|-|" + u16_hex(p));
for (k in range(0, 7)) {
  let t = 14 - k;
  if ((p / math_pow(2, t)) % 2 == 1) {
    p = xr(p, poly * math_pow(2, t - 8)); steps = steps + 1;
    rows.add("reduce|" + str(t) + "|" + u16_hex(p));
  }
}
let answer = steps;
lmcode_return_put("rows", rows);
lmcode_return_put("product", p);
{
  "answer": 2,
  "rows": [
    "product|-|2b79",
    "reduce|13|08d9",
    "reduce|11|0031"
  ],
  "product": 49
}

Lines 10 to 12 add the shifted copies of left without any reduction, so the product can have up to 15 bits. Lines 14 to 20 look at bits 14 down to 8: whenever one is set, line 17 adds poly shifted to that place, which clears it. The table has the unreduced product 0x2b79, then two reduction steps, the last leaving 0x0031. The answer, on line 21, is the number of steps: 2.

Change poly to 283 and the same two bytes give 0xc1, 193, instead of 0x31: the product depends on the polynomial (measured; 283 gives another representation of the field with a different multiplication table, not a statement about any standard). Keep left and right from 0 to 255 and poly from 257 to 511: a poly such as 29 leaves a product that is not a byte (measured: 10289), and a left of 300 does the same.

Your turn

The program below should multiply two bytes in GF(2^8). Several candidate programs are offered. Read them, run them on pairs of your own, and decide which of them is right for every pair. The cases are constructed bytes.

The numbers a = 32 and b = 12 and the number that the shown program returns.
let r = 0; let x = a; let y = b;
while (y > 0) {
  if (y % 2 == 1) { r = xor_byte(r, x); }
  y = y / 2;
  x = x * 2;
  if (x > 256) { x = xor_byte(x - 256, 29); }
}
let answer = r;
{
  "answer": 128
}

Return the product of the bytes a and b in GF(2^8), as an integer from 0 to 255. The product is the polynomial product of the two bytes, reduced modulo the polynomial 285 (x^8 + x^4 + x^3 + x^2 + 1, 0x11d): whenever a shifted value reaches 256 or more, add 285 to it by XOR until it is below 256. The inputs a and b are from 0 to 255. Check runs your program on these two bytes and on six others.

Read the result carefully

A reduction needs a field polynomial that cannot be split into smaller ones; this book uses the one that RFC 5510 lists for m = 8, and its multiplication is not the multiplication of any other table of bytes. The same shift-and-add works for any polynomial, which is why two different polynomials give two different products.

Zero times anything is zero, and one is the identity, so every non-zero byte can be divided by later: that needs an inverse, which the last lessons find by search. The CRC of CRC as Long Division divides by a polynomial too, with no reduction of a product. The programs here multiply a few pairs: all 65,536 products took 6.7 seconds with the native tool on the author's machine (measured), too long for a page run, which has 10 seconds.

Recap

  • Multiplying two bytes as polynomials gives up to 15 bits.
  • The product is reduced modulo 285 (0x11d), RFC 5510, section 8.1, for m = 8.
  • Shift and add: XOR 285 whenever a shifted value reaches 256.