"""Finite GF(2) checks of Liberation codes; not a production codec.""" from fractions import Fraction from itertools import combinations import json from pathlib import Path import sys def require(condition, message): if not condition: raise ValueError(message) def validate(k, w): require(type(k) is int and type(w) is int, "k and w must be integers, not booleans") require(3 <= w <= 31 and all(w % d for d in range(2, int(w**0.5) + 1)), "w must be an odd prime in this finite checker's range 3..31") require(2 <= k <= w, "k must be in 2..w") def _rows(k, w, extra=True): # Each integer is one coefficient row over the k*w data coordinates. data = [1 << i for i in range(k * w)] parity = [sum(1 << (i * w + r) for i in range(k)) for r in range(w)] second = [] for r in range(w): row = 0 for i in range(k): row ^= 1 << (i * w + (r + i) % w) if extra and i and r == (i * (w - 1) // 2) % w: row ^= 1 << (i * w + (r + i - 1) % w) second.append(row) return data + parity + second def rows(k, w): validate(k, w) return _rows(k, w) def rank(coefficients): pivots = {} for row in coefficients: require(type(row) is int and row >= 0, "rows must be nonnegative integers") while row: pivot = row.bit_length() - 1 if pivot in pivots: row ^= pivots[pivot] else: pivots[pivot] = row break return len(pivots) def survivors(matrix, w, erased): return [row for position, row in enumerate(matrix) if position // w not in erased] def encode(payload, k, w): matrix = rows(k, w) require(type(payload) is int and 0 <= payload < 1 << (k * w), "payload outside codeword") return [(row & payload).bit_count() % 2 for row in matrix] def parity_targets(k, w, coordinate): matrix = rows(k, w) require(type(coordinate) is int and 0 <= coordinate < k * w, "invalid data coordinate") return [(k + position // w, position % w) for position, row in enumerate(matrix[k*w:]) if row & (1 << coordinate)] def block_targets(targets, symbols_per_block): require(type(symbols_per_block) is int and symbols_per_block > 0, "invalid block size") return sorted({(device, symbol // symbols_per_block) for device, symbol in targets}) def configuration(k, w): matrix = rows(k, w) pairs = list(combinations(range(k + 2), 2)) failures = [list(pair) for pair in pairs if rank(survivors(matrix, w, pair)) != k * w] weights = [len(parity_targets(k, w, i)) for i in range(k*w)] return {"k": k, "w": w, "dataSymbols": k*w, "devices": k+2, "erasurePairs": len(pairs), "unrecoverablePairs": failures, "parityColumnWeights": {"2": weights.count(2), "3": weights.count(3)}, "meanParityUpdates": str(Fraction(sum(weights), k*w)), "unscheduledEncodingXors": sum(row.bit_count()-1 for row in matrix[k*w:]), "requiredSurvivorRank": k*w} def results(): configurations = [configuration(k, w) for w in (3, 5, 7, 11) for k in range(2, w+1)] # D1, symbol 2 at k=w=5 changes P2, Q1 and Q2. targets = parity_targets(5, 5, 7) composite = _rows(4, 9) bad_pairs = [list(pair) for pair in combinations(range(6), 2) if rank(survivors(composite, 9, pair)) != 36] return { "schemaVersion": 1, "kind": "deterministic finite algebraic verification; not measured storage data", "modelVersion": "1", "assumptions": ["Known whole-device erasures.", "Every survivor is correct and from the same codeword version.", "One bit per algebraic symbol; packetwise XOR applies bit by bit.", "Each input coordinate is weighted equally for the reported mean.", "Packed-block examples count distinct parity locations only, not actual I/O."], "reproduce": ["python3 model.py --check", "python3 -m unittest test_model.py"], "artifacts": ["https://hesela.dev/research/liberation/" + name for name in ("model.py", "test_model.py", "results.json", "README.txt")], "configurations": configurations, "totalConfigurations": len(configurations), "totalErasurePairs": sum(item["erasurePairs"] for item in configurations), "updateExample": {"k": 5, "w": 5, "dataDevice": 1, "symbol": 2, "paritySymbolLocations": targets, "oneSymbolPerBlock": block_targets(targets, 1), "fiveSymbolsPerBlock": block_targets(targets, 5)}, "negativeControl": {"description": "Formula evaluated outside its admitted prime-w domain; not a Liberation code guarantee.", "k": 4, "w": 9, "unrecoverablePairs": bad_pairs}, "limitations": ["No proof for all primes.", "No decoder performance, physical I/O, failure probability or hardware timing.", "No silent-error correction, torn-write consistency or authenticated identity checks."] } if __name__ == "__main__": result = results() text = json.dumps(result, indent=2) + "\n" target = Path(__file__).with_name("results.json") if sys.argv[1:] == ["--write"]: target.write_text(text) elif sys.argv[1:] == ["--check"]: if not target.exists() or target.read_text() != text: raise SystemExit("results.json differs: run model.py --write and review") print("Liberation results match") elif not sys.argv[1:]: print(text, end="") else: raise SystemExit("usage: model.py [--write|--check]")