"""Finite Liber8tion matrix check, not a storage implementation (BSD-3-Clause). Matrix coordinates follow Plank, NCA 2008, Figure 3 and Jerasure commit 414c96ef2b9934953b6facb31d803d79b1dd1405. See LICENSE.txt. """ from fractions import Fraction from itertools import combinations import json from pathlib import Path import sys # Each row selects one input column; the separate pair adds one coefficient. PERMUTATIONS = ( (0, 1, 2, 3, 4, 5, 6, 7), (7, 3, 0, 2, 6, 1, 5, 4), (6, 2, 4, 0, 7, 3, 1, 5), (2, 5, 7, 6, 0, 3, 4, 1), (5, 6, 1, 7, 2, 4, 3, 0), (1, 2, 3, 4, 5, 6, 7, 0), (3, 0, 6, 5, 1, 7, 4, 2), (4, 7, 1, 5, 3, 2, 0, 6), ) EXTRA = (None, (4, 7), (1, 3), (5, 4), (2, 0), (7, 2), (6, 5), (3, 1)) def require(ok, message): if not ok: raise ValueError(message) def rows(k): require(type(k) is int and 2 <= k <= 8, "k must be an integer in 2..8") data = [1 << i for i in range(8 * k)] p = [sum(1 << (8 * disk + row) for disk in range(k)) for row in range(8)] q = [] for row in range(8): bits = sum(1 << (8 * disk + PERMUTATIONS[disk][row]) for disk in range(k)) for disk in range(1, k): extra_row, col = EXTRA[disk] if row == extra_row: bits ^= 1 << (8 * disk + col) q.append(bits) return data + p + q def rank(matrix): pivots = {} for value in matrix: require(type(value) is int and value >= 0, "coefficient rows must be nonnegative integers") while value: top = value.bit_length() - 1 if top in pivots: value ^= pivots[top] else: pivots[top] = value break return len(pivots) def surviving(matrix, erased): return [row for i, row in enumerate(matrix) if i // 8 not in erased] def targets(k, coordinate): matrix = rows(k) require(type(coordinate) is int and 0 <= coordinate < 8*k, "invalid data coordinate") return [(("P", "Q")[i // 8], i % 8) for i, row in enumerate(matrix[8*k:]) if row & (1 << coordinate)] def configuration(k): matrix = rows(k) pairs = list(combinations(range(k + 2), 2)) failed = [list(pair) for pair in pairs if rank(surviving(matrix, pair)) != 8*k] weights = [len(targets(k, i)) for i in range(8*k)] density = sum(row.bit_count() for row in matrix[8*k:]) return { "dataDevices": k, "parityDevices": 2, "positionsPerDevice": 8, "erasurePairs": len(pairs), "unrecoverablePairs": failed, "requiredSurvivorRank": 8*k, "codingMatrixOnes": density, "parityUpdateCounts": {"two": weights.count(2), "three": weights.count(3)}, "meanParityUpdates": str(Fraction(sum(weights), 8*k)), "directEncodingXors": density - 16, } def results(): configs = [configuration(k) for k in range(2, 9)] broken = rows(2) broken[16 + 8 + 4] ^= 1 << 15 return { "schemaVersion": 1, "kind": "exact finite algebraic verification, not measured storage data", "matrixSource": "https://github.com/tsuraan/Jerasure/blob/414c96ef2b9934953b6facb31d803d79b1dd1405/src/liberation.c", "configurations": configs, "totalConfigurations": len(configs), "totalErasurePairs": sum(c["erasurePairs"] for c in configs), "updateExample": {"dataDevices": 8, "dataDevice": 1, "position": 7, "parityLocations": targets(8, 15)}, "negativeControl": { "mutation": "Delete the extra Q4 <- D1[7] coefficient at k=2", "erasedDevices": [0, 1], "survivorRank": rank(surviving(broken, (0, 1))), "requiredRank": 16, }, "assumptions": [ "Erased device positions are known; surviving symbols are intact.", "All devices belong to the same stripe and codeword version.", "One algebraic symbol is one bit; equal-sized XOR packets apply bitwise.", "Average update counts weight all data coordinates equally.", ], "limitations": [ "No hardware I/O, timing, workload sampling or failure probability.", "No silent-error detection, torn-write recovery or authentication.", "No reproduction of the original search for 48 matrix classes.", "The 2..8 data-device range is this checker's explicit supported range.", ], } if __name__ == "__main__": value = json.dumps(results(), indent=2) + "\n" path = Path(__file__).with_name("results.json") if sys.argv[1:] == ["--write"]: path.write_text(value) elif sys.argv[1:] == ["--check"]: require(path.read_text() == value, "stale results.json") print("Liber8tion results match") elif not sys.argv[1:]: print(value, end="") else: raise SystemExit("usage: model.py [--write|--check]")