"""Finite XOR X-code checks from Xu/Bruck equation (1), not a storage driver.""" import argparse import json from itertools import combinations from pathlib import Path def parameter(n, permit_composite=False): if type(n) is not int or not 3 <= n <= 31 or n % 2 == 0: raise ValueError("n must be an odd integer from 3 through 31") if not permit_composite and any(n % d == 0 for d in range(2, n)): raise ValueError("X-code requires prime n; composite mode is a negative control") def encode(data, *, permit_composite=False): if not isinstance(data, list) or not data: raise ValueError("data must contain n-2 rows") n = len(data) + 2 parameter(n, permit_composite) if any(not isinstance(row, list) or len(row) != n for row in data): raise ValueError("data must have n columns") if any(type(v) is not int or not 0 <= v <= 255 for row in data for v in row): raise ValueError("symbols must be integer bytes") out = [row[:] for row in data] + [[0] * n, [0] * n] for col in range(n): for row in range(n - 2): out[n - 2][col] ^= data[row][(col + row + 2) % n] out[n - 1][col] ^= data[row][(col - row - 2) % n] return out def equations(n, erased=(), *, permit_composite=False): parameter(n, permit_composite) erased = tuple(erased) if len(set(erased)) != len(erased) or any( type(c) is not int or not 0 <= c < n for c in erased): raise ValueError("erased columns must be unique in-range integers") rows = [[1 << (r * n + c) for c in range(n)] for r in range(n - 2)] parity = [[0] * n, [0] * n] # Scatter each source variable to the two parity destinations. for r in range(n - 2): for c in range(n): parity[0][(c - r - 2) % n] ^= rows[r][c] parity[1][(c + r + 2) % n] ^= rows[r][c] return [v for row in rows + parity for c, v in enumerate(row) if c not in erased] def echelon(rows): pivots = {} for value in rows: while value: pivot = value.bit_length() - 1 if pivot not in pivots: pivots[pivot] = value break value ^= pivots[pivot] return pivots def rank(rows): return len(echelon(rows)) def ambiguity(n, erased): rows = equations(n, erased, permit_composite=True) pivots = echelon(rows) free = next((i for i in range(n * (n - 2)) if i not in pivots), None) if free is None: return None vector = 1 << free for i, row in sorted(pivots.items()): if (row & vector).bit_count() % 2: vector ^= 1 << i return [[(vector >> (r * n + c)) & 1 for c in range(n)] for r in range(n - 2)] def results(): cases = [] for n in [3, 5, 7, 9, 11]: ranks = [(pair, rank(equations(n, pair, permit_composite=True))) for pair in combinations(range(n), 2)] cases.append({"n": n, "payload_bits": n * (n - 2), "encoded_bits": n * n, "erasure_pairs": len(ranks), "full_rank_pairs": sum(r == n * (n - 2) for _, r in ranks), "minimum_rank": min(r for _, r in ranks), "deficient_pairs": [{"erased": list(p), "rank": r} for p, r in ranks if r < n * (n - 2)]}) data = [[0] * 5 for _ in range(3)] data[0][0] = 1 encoded = encode(data) witness = ambiguity(9, (0, 3)) return {"schema_version": 1, "kind": "exact finite algebraic illustration", "assumptions": ["Known whole-column erasures", "Correct surviving symbols from one consistent codeword", "XOR symbols; no timing, crash or device model"], "pair_checks": cases, "single_update_n5": { "data_coordinate": [0, 0], "delta": 1, "changed_coordinates": [[r, c] for r, row in enumerate(encoded) for c, v in enumerate(row) if v]}, "composite_n9_witness": {"erased": [0, 3], "nonzero_data_coordinates": [ [r, c] for r, row in enumerate(witness) for c, v in enumerate(row) if v], "surviving_nonzero_symbols": sum( v != 0 for row in encode(witness, permit_composite=True) for c, v in enumerate(row) if c not in (0, 3))}} if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("--check", action="store_true") parser.add_argument("--write", action="store_true") args = parser.parse_args() output = json.dumps(results(), indent=2) + "\n" destination = Path(__file__).with_name("results.json") if args.check: if destination.read_text() != output: raise SystemExit("Stored results differ from recomputation") elif args.write: destination.write_text(output) else: print(output, end="")