#!/usr/bin/env python3 """Exact reconstruction of Plank-Ding UT-CS-03-504, p. 4; not a storage codec.""" import hashlib import itertools import json import sys from datetime import datetime, timezone from pathlib import Path # Table 1, visually checked against the six-page 2003 technical report. EXP = (1, 2, 4, 8, 3, 6, 12, 11, 5, 10, 7, 14, 15, 13, 9) LOG = {value: index for index, value in enumerate(EXP)} EXPECTED = ((1, 0, 0), (0, 1, 0), (0, 0, 1), (1, 1, 1), (15, 8, 6), (14, 9, 6)) def symbol(value): if type(value) is not int or not 0 <= value < 16: raise ValueError("Expected an integer GF(16) symbol from 0 through 15") return value def mul(a, b): symbol(a) symbol(b) return 0 if a == 0 or b == 0 else EXP[(LOG[a] + LOG[b]) % 15] def polynomial_mul(a, b): """Independent shift/XOR oracle in the polynomial basis modulo x^4+x+1.""" symbol(a) symbol(b) result = 0 while b: if b & 1: result ^= a b >>= 1 a <<= 1 if a & 16: a ^= 0x13 return result def validate_matrix(matrix): if (not isinstance(matrix, (list, tuple)) or not 1 <= len(matrix) <= 6 or not isinstance(matrix[0], (list, tuple)) or not 1 <= len(matrix[0]) <= 6): raise ValueError("Expected a nonempty matrix of at most 6 by 6") width = len(matrix[0]) for row in matrix: if not isinstance(row, (list, tuple)) or len(row) != width: raise ValueError("Expected a rectangular matrix") for value in row: symbol(value) def dot(a, b, product=mul): value = 0 for x, y in zip(a, b, strict=True): value ^= product(x, y) return value def multiply(a, b): validate_matrix(a) validate_matrix(b) if len(a[0]) != len(b): raise ValueError("Incompatible dimensions") return [[dot(row, col) for col in zip(*b)] for row in a] def inverse(matrix): validate_matrix(matrix) n = len(matrix) if n != len(matrix[0]): raise ValueError("Expected a square matrix") work = [list(row) + [int(i == j) for j in range(n)] for i, row in enumerate(matrix)] for col in range(n): pivot = next((i for i in range(col, n) if work[i][col]), None) if pivot is None: return None work[col], work[pivot] = work[pivot], work[col] scale = EXP[-LOG[work[col][col]] % 15] work[col] = [mul(value, scale) for value in work[col]] for i in range(n): if i != col: factor = work[i][col] work[i] = [a ^ mul(factor, b) for a, b in zip(work[i], work[col], strict=True)] return [row[n:] for row in work] def encode(matrix, data, product=mul): validate_matrix(matrix) if not isinstance(data, (list, tuple)) or len(data) != len(matrix[0]): raise ValueError("Payload width must match the generator") for value in data: symbol(value) return [dot(row, data, product) for row in matrix] def constructions(): vandermonde = [[1, x, mul(x, x)] for x in range(6)] generator = multiply(vandermonde, inverse(vandermonde[:3])) mixed = [row[:] for row in generator] mixed[5] = [a ^ b for a, b in zip(mixed[5], mixed[4], strict=True)] return vandermonde, generator, mixed def results(): vandermonde, generator, mixed = constructions() sets = list(itertools.combinations(range(6), 3)) rows = [] for name, matrix in [("paper-systematic", generator), ("row-mixed-control", mixed)]: singular = [list(ids) for ids in sets if inverse([matrix[i] for i in ids]) is None] rows.append({"id": name, "generator": matrix, "checked": len(sets), "recoverable": len(sets) - len(singular), "singular": singular}) return { "schemaVersion": "1.0.0", "kind": "exact-published-example-reconstruction-with-negative-control", "source": { "title": "Plank and Ding, Note: Correction to the 1997 Tutorial on Reed-Solomon Coding", "version": "UT-CS-03-504, 24 April 2003, section 3 and Table 1, pages 2-4", "url": "https://library.eecs.utk.edu/files/ut-cs-03-504.pdf", "sha256": "a133db45aff097525c0a2c5fdde5b65aa9f0f5dc89d33e9fa5ef4561cb3ee0a8", "accessed": "2026-10-19", "laterJournalDoi": "10.1002/spe.631", "rights": "Source paper not redistributed; only mathematical values and new code." }, "field": {"name": "GF(16)", "basis": "polynomial", "modulus": "x^4 + x + 1", "modulusHex": "0x13", "addition": "bitwise XOR", "exponentTable": list(EXP)}, "dataSymbols": 3, "encodedSymbols": 6, "payloads": 4096, "method": "Reconstruct V T^-1 with the paper's log table; enumerate all 20 survivor sets. Tests compare all 256 scalar products with polynomial reduction and all 81920 payload/survivor round trips.", "limits": "Not a reproduction of the later journal version, production codec certification, hardware measurement or failure probability. The row-mixed negative control is Hesela's example, not the paper's.", "vandermonde": vandermonde, "rows": rows, "negativeControl": { "operation": "row 5 <- row 5 XOR row 4 (zero-based)", "fullRankBefore": 3, "fullRankAfter": 3, "reason": "The first three identity rows are unchanged.", "survivors": [0, 1, 5], "dataA": [0, 0, 0], "dataB": [0, 0, 1], "observedA": [encode(mixed, [0, 0, 0])[i] for i in [0, 1, 5]], "observedB": [encode(mixed, [0, 0, 1])[i] for i in [0, 1, 5]] } } def document(): output = results() output["generatedAt"] = datetime.now(timezone.utc).isoformat() output["pythonVersion"] = sys.version.split()[0] output["sourceSha256"] = { name: hashlib.sha256(Path(__file__).with_name(name).read_bytes()).hexdigest() for name in ("rs-correction.py", "rs-correction.test.py") } return output if __name__ == "__main__": print(json.dumps(document(), indent=2))