Speed

Lose Shards and Solve

Any two shards of a two-byte code give two equations in the two data bytes; with different places they have exactly one solution.

The question

Some shards are lost, and the receiver knows which ones. Can the data still be rebuilt, and from how many shards? Two data bytes were turned into shards as in the last lesson, shard j being s_0 + s_1 * alpha^j. Two survivors give two equations in the two unknown bytes. This is the way the toy book Erasure Codes Rebuild a Missing Number rebuilds a line from two points, now over bytes. The bytes are constructed, and nothing is secret.

Work it by hand

RFC 5510, section 8.2.1, says that the Vandermonde matrix it uses generates an MDS code, and section 8.3.1 says that the decoder recovers the k source elements from any set of k received elements. For k = 2 the reason is short. Two shards at places x_a and x_b give e_a = s_0 + s_1 x_a and e_b = s_0 + s_1 x_b. Add the two: s_0 cancels, because a byte plus itself is 0, so e_a + e_b = s_1 (x_a + x_b).

Divide by x_a + x_b, which is not 0 when the two places differ: s_1 = (e_a + e_b) / (x_a + x_b). Then s_0 = e_a + s_1 x_a.

Try shards 0x18 at index 1 and 0x42 at index 4. The places are 2 and 16. The sum of the shards is 0x5a, the sum of the places is 0x12, and 0x5a / 0x12 is 5, because 5 times 0x12 in the field is 0x5a. So s_1 = 5, and putting it back into the first equation gives s_0 = 0x18 + 5 * 2 = 0x18 + 0x0a = 0x12.

Run it

The demo finds the data another way: it tries all 256 values of the second byte and keeps those that fit both shards.

A table with three rows: 1 candidate of 256 fits, the second data byte is 6f and the first data byte is 4a, found from the shards 94 at place 1 and 15 at place 3. The answer is 111, the second data byte as a number.
Watch the steps (recorded video)
let xa = gf_pow(2, place_a); let xb = gf_pow(2, place_b);
let found = 0 - 1; let fits = 0; let low = 0;
for (s in range(0, 256)) {
  let s0 = gf_xor(shard_a, gf_mul(s, xa));
  if (gf_xor(s0, gf_mul(s, xb)) == shard_b) {
    fits = fits + 1;
    if (found < 0) { found = s; low = s0; }
  }
}
if (found < 0) { out.fail("no data fits these shards"); }
let rows = str_list();
rows.add("candidates that fit|" + str(fits));
rows.add("second data byte|" + u8_hex(found));
rows.add("first data byte|" + u8_hex(low));
let answer = found;
lmcode_return_put("rows", rows);
{
  "answer": 111,
  "rows": [
    "candidates that fit|1",
    "second data byte|6f",
    "first data byte|4a"
  ]
}

The shards are 0x94 at index 1 and 0x15 at index 3 of the encoding of two bytes. Line 4 computes the first byte that each candidate implies from the first shard, and line 5 tests it against the second shard. Lines 6 and 7 count the fits and keep the first one. The table says that 1 candidate of 256 fits, and that the data are 4a and 6f. The answer, set on line 15, is the second byte as a number, 111.

You can change the four numbers. If the two places are the same element and the shards differ, the run stops with "no data fits these shards". If they are the same and the shards are equal, all 256 candidates fit (measured: place_b 1 and shard_b 148) and the first, 0, is shown. A place above 254 repeats the places (2 to the 255 is 1).

Your turn

The program below should rebuild two data bytes from two surviving shards. Several candidate programs are offered. Read them, run them on inputs of your own, and decide which of them is right for every input. The cases are constructed data bytes with different indices from 0 to 5.

Two surviving shards, 2 bytes, the indices 1 and 2 that they had, and the text of two data bytes that the shown program returns.
let ea = hex_u8(shards, 0); let eb = hex_u8(shards, 1);
let xa = gf_pow(2, ja); let xb = gf_pow(2, jb);
let s1 = gf_mul(gf_xor(ea, eb), gf_inv(gf_xor(xa, xb)));
let s0 = gf_xor(ea, s1);
let answer = u8_hex(s0) + u8_hex(s1);
lmcode_return_put("places", u8_hex(xa) + u8_hex(xb));
{
  "answer": "daa9",
  "places": "0204"
}

Return the two data bytes s_0 and s_1 as hex text, four digits, from two surviving shards. The shards were made as in the last lesson from two data bytes: shard j is s_0 + s_1 * a^j in GF(2^8) with a = 2 and the polynomial 285, the sum is XOR. The hex input shards holds the shard with index ja and then the shard with index jb; ja and jb are different and from 0 to 5. Check runs your program on this input and on five others.

Read the result carefully

Rebuilding needs the same index to be used only once. With the same index twice, the shown solution does not stop: the helper for the inverse returns 0 for 0, so it returns the first shard as the first byte and 00 as the second (measured: shards 73a2, both indices 2, give 7300). That is an answer to a question with many solutions, not a rebuilt message.

The code rebuilds missing positions, not wrong values; the last lesson shows what a wrong shard does. This is the k = 2 case: decoding three unknown bytes needs a longer program than a page allows, and this book does not show it. The code is a toy size, not a system, and nothing here is encrypted.

Recap

  • Two shards at different places give two equations; adding them removes s_0.
  • The sum of the two shards leaves one equation for s_1; then s_0 follows from either shard.
  • Any two different indices work: the code is MDS (RFC 5510, sections 8.2.1 and 8.3.1).
  • The same index twice gives no unique answer.