Speed

Powers of Two Walk the Field

With the polynomial 285 the powers of 2 take every non-zero byte exactly once, so every non-zero byte has an exponent.

The question

Start at 1 and keep doubling, reducing whenever a value reaches 256. How many different bytes does the walk visit before it comes back to 1? If the answer is all 255 non-zero bytes, every one of them can be named by how many doublings reach it. This lesson counts the visits for two polynomials and then asks for the exponent of a byte. The bytes are constructed and nothing here is secret; no OpenSSL call is used.

Work it by hand

RFC 5510, section 8.1, says that its polynomials are also primitive: any element of GF(2^m) can be expressed as a power of a given root of the polynomial. For m = 8 and the polynomial 285 that root is the byte 2, the polynomial x.

Doubling a byte is multiplying by 2. Start from 2 to the 10th, which is 116. Doubling gives 232, the 11th power. Doubling again gives 464, which is 256 or more, so XOR 285: 0x1d0 xor 0x11d is 0xcd, 205, the 12th power. Going on this way for 255 steps visits every non-zero byte once, and the walk returns to 1.

Run it

The demo counts the different values that the walk visits with two polynomials.

A table with two rows: the polynomial 285 gives 255 different powers of 2, and the polynomial 283 gives 51. The answer is 1, the number of polynomials for which the powers of 2 take all 255 non-zero values.
Watch the steps (recorded video)
fn xor9(a: int, b: int) -> int {
  let r = 0; let bit = 1;
  for (i in range(0, 9)) {
    if ((a / bit) % 2 != (b / bit) % 2) { r = r + bit; }
    bit = bit * 2;
  }
  return r;
}
fn distinct(poly: int) -> int {
  let seen = int_list(); let x = 1;
  for (i in range(0, 255)) {
    if (!seen.contains(x)) { seen.add(x); }
    x = x * 2;
    if (x > 255) { x = xor9(x, poly); }
  }
  return seen.len();
}
let first = distinct(first_poly); let second = distinct(second_poly);
let answer = first / 255 + second / 255;
let rows = str_list();
rows.add(str(first_poly) + "|" + str(first));
rows.add(str(second_poly) + "|" + str(second));
lmcode_return_put("rows", rows);
{
  "answer": 1,
  "rows": [
    "285|255",
    "283|51"
  ]
}

The function on lines 1 to 8 is an exclusive or for 9 bits, and the function on lines 9 to 17 walks 255 doublings with a list of the values seen. Line 18 counts for both polynomials, and line 19 adds the number of walks that visited all 255 values: a count divided by 255 is 1 only for 255. With 285 the count is 255; with 283 it is 51, so for that polynomial 2 does not reach the whole field.

You can change the two polynomials. Another polynomial can give 255 too (measured: 299), and a number that is not a degree-8 polynomial gives a short walk (measured: 257 gives 8, 29 gives 40). The program stops for none of them.

Your turn

The program below should return the exponent of a byte: how many doublings from 1 reach it. Several candidate programs are offered. Read them, run them on bytes of your own, and decide which of them is right for every byte. The cases are constructed bytes from 1 to 255.

The byte 87 and the exponent that the shown program returns.
let x = 1; let answer = 0 - 1;
for (i in range(0, 255)) {
  if (x == value) { answer = i; }
  x = x * 2;
  if (x > 255) { x = x - 256; }
}
{
  "answer": -1
}

Return the exponent k from 0 to 254 for which 2^k equals value in GF(2^8) with the polynomial 285, where 2^0 is 1 and every further power doubles the previous one and reduces by 285 when it reaches 256. The input value is from 1 to 255. Check runs your program on this byte and on six others.

Read the result carefully

Every byte from 1 to 255 has exactly one exponent from 0 to 254, and 2 to the 255th is 1 again (computed in Python, not by a page program). Zero has no exponent: no power of 2 is 0. Run on 0, or on 256, the shown solution returns -1.

The result is a fact about this polynomial; the counts of the demo show that another polynomial may not have it. An exponent is a logarithm, and finding it by walking is slow; a table of exponents would avoid the walk, and this page builds none. For powers of numbers modulo a number, see Modular Exponentiation.

Recap

  • With the polynomial 285 the powers of 2 visit all 255 non-zero bytes once.
  • Every non-zero byte has one exponent from 0 to 254; zero has none.
  • With the polynomial 283 the powers of 2 visit only 51 bytes.
  • The walk comes back to 1 after 255 doublings.