What This Code Does Not Do
The code rebuilds data from enough distinct shards at known positions; it does not tell a wrong shard from a right one.
The question
An erasure code makes up for pieces that are known to be missing. What happens when a piece is present but wrong? And how many pieces are enough? This lesson answers both with the code of the last two lessons: two data bytes and shards made by evaluating a polynomial over GF(2^8). The bytes are constructed, the sizes are a toy, and nothing here is secret or encrypted.
Work it by hand
A receiver of an [n, k] code needs k shards with different indices. Count them by hand: with n = 6 and k = 2, the survivors 5, 3, 3 and 0 are the shards 0, 3 and 5, because index 3 is listed twice and counts once. That is 3 distinct shards, more than the 2 that are needed, so the data can be rebuilt. An index outside 0 to 5 is not a shard of this code.
RFC 5510, section 9.2.2, says that protection against corruption is achieved by a content integrity verification or sender authentication scheme, and that with such a check at object level a receiver cannot tell which symbols are corrupted. The code gives no such check.
Run it
The demo rebuilds the data of the last lesson, then changes one bit of one shard and rebuilds again.

Watch the steps (recorded video)
fn rebuild(ea: int, eb: int, xa: int, xb: int) -> string {
let s1 = gf_mul(gf_xor(ea, eb), gf_inv(gf_xor(xa, xb)));
return u8_hex(gf_xor(ea, gf_mul(s1, xa))) + u8_hex(s1);
}
let xa = gf_pow(2, place_a); let xb = gf_pow(2, place_b);
let changed = gf_xor(shard_b, mask);
let good = rebuild(shard_a, shard_b, xa, xb);
let bad = rebuild(shard_a, changed, xa, xb);
let rows = str_list();
rows.add(u8_hex(shard_a) + " " + u8_hex(shard_b) + "|" + good);
rows.add(u8_hex(shard_a) + " " + u8_hex(changed) + "|" + bad);
let answer = bad != good;
lmcode_return_put("rows", rows);
{
"answer": true,
"rows": [
"94 15|4a6f",
"94 14|edb2"
]
}Line 6 changes the second shard by the bits of mask. Lines 7 and 8 rebuild the two data bytes from the good and from the changed shard. Lines 10 and 11 put both results in the table, and line 12 is true when they differ. The shards 94 and 15 rebuild 4a6f; the shards 94 and 14, one bit apart, rebuild edb2. Neither run gives an error.
You can change the five numbers. A mask of 0 gives equal results and the answer false. Two equal places do not stop the run: the inverse of 0 is 0, so the rebuilt second byte is 00 (measured: place_b 1 gives 9400 twice). Keep the shards from 0 to 255.
Your turn
The program below should say whether the surviving shards are enough to rebuild the data. 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 lists of indices.

let seen = int_list();
let parts = str_split(present, ",");
for (i in range(0, parts.len())) {
let j = str_to_int(parts.at(i));
if (str_is_int(parts.at(i)) && j >= 0 && j < n && !seen.contains(j)) {
seen.add(j);
}
}
let answer = "rebuildable";
if (seen.len() <= k) { answer = "missing " + str(k - seen.len()); }
{
"answer": "missing 0"
}Return rebuildable if the surviving shards are enough to rebuild the k data bytes, and otherwise the text missing followed by a space and the number of further distinct shards that are needed. The text present lists the indices of the surviving shards, separated by commas without spaces, possibly with repeats; a shard counts once however often its index is listed, and only an index from 0 to n - 1 is a shard. Check runs your program on this input and on seven others.
Read the result carefully
The shown programs read the list with str_to_int after a check: an entry that has a space or a letter is not an index and is skipped (measured: 1, 2, 3 with spaces counts only the index 1), and an empty list counts no shard. Indices are counted, not shards: the program does not look at any value.
Fewer than k distinct shards cannot rebuild the data, and RFC 5510, section 6.1, says that for m = 8 there are at most 255 encoding symbols. A check that catches a wrong value is a separate tool: see HMAC Uses Two Pads for a tag, and Hamming Code Catches One Bit for check bits of another code. This code is not systematic, not a cipher and no real system is shown.
Recap
- The code rebuilds from enough distinct shards at known indices.
- Enough means k shards with different indices; fewer are not enough.
- A wrong value is rebuilt into wrong data with no error (the demo).
- Catching it needs a check outside the code (RFC 5510, section 9.2.2).