Speed

Shards Are Evaluations

The data bytes are the coefficients of a polynomial, and shard j is its value at the field element 2 to the power j.

The question

A code that rebuilds lost pieces must first make extra pieces. Here the pieces are bytes. How are three data bytes turned into five shards, so that any three of the five could be enough? The idea is the one of the toy book Erasure Codes Rebuild a Missing Number: a line through points, with the numbers modulo 5 replaced by the bytes of GF(2^8). The bytes are constructed; the code is a toy size (at most 6 shards), and nothing is secret.

Work it by hand

RFC 5510, section 8.2.1, says that encoding multiplies the source vector s by a generator matrix, e = s * GM, and builds a Vandermonde matrix whose entry in row i and column j is alpha to the power i times j, with alpha the root of the primitive polynomial. It adds that this matrix generates an MDS code which is not systematic. This book uses that matrix, so shard j is s_0 + s_1 * alpha^j + s_2 * alpha^(2j) + ..., with alpha = 2.

Two data bytes, 0x03 and 0x05, give shard j = 0x03 + 0x05 * 2^j. For j = 0 to 3 the powers are 1, 2, 4, 8 and the products 0x05, 0x0a, 0x14, 0x28, so the shards are 0x03 XOR each: 0x06, 0x09, 0x17, 0x2b. Shard 0 is always the XOR of all the data bytes.

Run it

The demo encodes two bytes, 0x4a and 0x6f, into four shards.

A table of four rows for the data bytes 4a and 6f: j 0, 1, 2, 3 with alpha^j 01, 02, 04, 08 and the shards 25, 94, eb, 15. The answer is the four shards in one text, 2594eb15.
Watch the steps (recorded video)
let rows = str_list(); let all = "";
if (count < 1) { out.fail("count must be at least 1"); }
for (j in range(0, count)) {
  let x = gf_pow(2, j);
  let shard = gf_xor(s0, gf_mul(s1, x));
  rows.add(str(j) + "|" + u8_hex(x) + "|" + u8_hex(shard));
  all = all + u8_hex(shard);
}
let answer = all;
lmcode_return_put("rows", rows);
{
  "answer": "2594eb15",
  "rows": [
    "0|01|25",
    "1|02|94",
    "2|04|eb",
    "3|08|15"
  ]
}

For each j on line 3, line 4 takes alpha to the power j and line 5 evaluates the line: the shard is s0 plus s1 times that power, in the field. Line 6 adds a row and line 7 appends the shard to the answer text, which line 9 returns: 2594eb15. The table shows the powers 01, 02, 04, 08 and the shards 25, 94, eb, 15.

You can change s0, s1 and count. A count below 1 stops the run with "count must be at least 1". A count of 40 runs, but the picture shows at most 16 rows. Keep s0 and s1 from 0 to 255: 300 gives odd shards (measured: 43f28d73 for a count of 4).

Your turn

The program below should encode three data bytes into n shards. Several candidate programs are offered. Read them, run them on data of your own, and decide which of them is right for every input. The cases are constructed bytes and a shard count from 4 to 6.

The three data bytes 2d 9a c1, the shard count 5, and the text of shards that the shown program returns.
let answer = "";
for (j in range(0, n)) {
  let e = 0;
  for (i in range(0, hex_len(data))) {
    let t = gf_mul(hex_u8(data, hex_len(data) - 1 - i), gf_pow(2, i * j));
    e = gf_xor(e, t);
  }
  answer = answer + u8_hex(e);
}
lmcode_return_put("shards", n);
{
  "answer": "765c79ea92",
  "shards": 5
}

Return the n shards of the three data bytes in data as hex text, two digits per shard, in the order of their index j from 0 to n - 1. The data bytes s_0, s_1, s_2 are the coefficients of a polynomial, and shard j is s_0 + s_1 * a^j + s_2 * a^(2j) in GF(2^8) with a = 2 and the polynomial 285; the sum is XOR. The input n is from 4 to 6. Check runs your program on this input and on five others.

Read the result carefully

This is the non-systematic form: no shard is a data byte in general, which is why a real system multiplies by the inverse of the first columns, as RFC 5510 does (GM = V_kk^-1 * V_kn), and then the first k shards equal the data. This book does not do that, so its shards are not the RFC's output for the same data. The RFC prints no test vector here: the values are computed by the oracle of this lesson.

The sizes are tiny. The field has 255 non-zero elements and RFC 5510, section 6.1, allows at most 255 encoding symbols for m = 8; no packet format, block size or speed is shown. Encoding is not encryption: anyone can read the data from the shards.

Recap

  • The data bytes are the coefficients of a polynomial over GF(2^8).
  • Shard j is the polynomial's value at alpha^j, with alpha = 2; the sum is XOR.
  • Shard 0 is the XOR of all data bytes.
  • This form is not systematic, and no shard is secret.