"""Exact byte-splice demonstration, not SSD or power-fault measurements. Run with Python 3.9+: python3 power-cut-patterns.py Use --output PATH to regenerate the JSON publication asset. """ import argparse import hashlib import json from pathlib import Path RECORD_BYTES = 4096 PIECE_BYTES = 512 LAYOUTS = ("header-only", "repeated-tag", "repeated-tag-xor") MASK = hashlib.shake_256(b"Hesela power-cut pattern example v1").digest(RECORD_BYTES) def record(version, layout): if type(version) is not int or version not in (1, 2): raise ValueError("version must be 1 or 2") if layout not in LAYOUTS: raise ValueError("unknown layout") tag = version.to_bytes(8, "big") piece = tag + bytes(PIECE_BYTES - len(tag)) raw = (piece + bytes(RECORD_BYTES - PIECE_BYTES) if layout == "header-only" else piece * (RECORD_BYTES // PIECE_BYTES)) return bytes(a ^ b for a, b in zip(raw, MASK)) if layout.endswith("-xor") else raw def splice(old, new, boundary, direction): if len(old) != RECORD_BYTES or len(new) != RECORD_BYTES: raise ValueError("records must be 4096 bytes") if type(boundary) is not int or not 0 < boundary < RECORD_BYTES or boundary % PIECE_BYTES: raise ValueError("boundary must be an interior multiple of 512 bytes") if direction == "new-prefix": return new[:boundary] + old[boundary:] if direction == "new-suffix": return old[:boundary] + new[boundary:] raise ValueError("unknown direction") def classify(observed, old, new): if len(observed) != RECORD_BYTES: raise ValueError("observation must be 4096 bytes") if observed == old: return "old" if observed == new: return "new" return "mixed" def byte_cut_sweep(): """Exhaustive two-endpoint, one-cut coverage; counts are not probabilities.""" summaries = [] for layout in LAYOUTS: old, new = record(1, layout), record(2, layout) differences = [i for i, (a, b) in enumerate(zip(old, new)) if a != b] counts = {"old": 0, "new": 0, "mixed": 0} for cut in range(1, RECORD_BYTES): for observed in (new[:cut] + old[cut:], old[:cut] + new[cut:]): counts[classify(observed, old, new)] += 1 summaries.append({ "layout": layout, "interiorSplices": 2 * (RECORD_BYTES - 1), "indistinguishableFromEndpoint": counts["old"] + counts["new"], "detectablyMixed": counts["mixed"], "equalsOld": counts["old"], "equalsNew": counts["new"], "firstDifferingByte": differences[0], "lastDifferingByte": differences[-1], }) return { "boundaryCount": RECORD_BYTES - 1, "directionsPerBoundary": 2, "method": "Enumerate every interior byte cut in both directions, separately from the aligned matrix.", "interpretation": "For distinct endpoints, a splice is mixed exactly when firstDifferingByte < cut <= lastDifferingByte; byte offsets start at zero.", "limit": "These are equally enumerated cut positions, not measured or assumed probabilities of physical faults. Noncontiguous mixtures and arbitrary corruption are outside this sweep.", "summaries": summaries, } def experiment(): rows = [] summaries = [] controls = [] unaligned_controls = [] for layout in LAYOUTS: old, new = record(1, layout), record(2, layout) current = [] for boundary in range(PIECE_BYTES, RECORD_BYTES, PIECE_BYTES): for direction in ("new-prefix", "new-suffix"): observed = splice(old, new, boundary, direction) outcome = classify(observed, old, new) row = { "layout": layout, "boundaryBytes": boundary, "direction": direction, "classification": outcome, "sha256": hashlib.sha256(observed).hexdigest(), "endpointHashMatch": hashlib.sha256(observed).digest() in (hashlib.sha256(old).digest(), hashlib.sha256(new).digest()), } rows.append(row) current.append(row) summaries.append({ "layout": layout, "interiorSplices": len(current), "indistinguishableFromEndpoint": sum(r["classification"] != "mixed" for r in current), "detectablyMixed": sum(r["classification"] == "mixed" for r in current), }) for name, value in (("old", old), ("new", new)): controls.append({"layout": layout, "control": name, "classification": classify(value, old, new), "sha256": hashlib.sha256(value).hexdigest()}) if layout != "header-only": # Outside the aligned matrix: byte 0 is identical in both versions. for direction in ("new-prefix", "new-suffix"): observed = (new[:1] + old[1:] if direction == "new-prefix" else old[:1] + new[1:]) unaligned_controls.append({ "layout": layout, "boundaryBytes": 1, "direction": direction, "classification": classify(observed, old, new), "sha256": hashlib.sha256(observed).hexdigest(), }) return { "schemaVersion": 1, "evidenceType": "synthetic-byte-demonstration", "recordBytes": RECORD_BYTES, "pieceBytes": PIECE_BYTES, "method": "Splice old/new byte arrays at every interior 512-byte boundary in both directions; compare exact bytes with both complete endpoints.", "source": {"url": "https://www.usenix.org/system/files/conference/fast13/fast13-final80.pdf", "locator": "Sections 3.1 and 3.1.1; motivation only, not a recreation of the authors' record format."}, "limits": [ "No storage device, filesystem, power switch or command completion is modeled.", "A 512-byte splice boundary is an example parameter, not an asserted device atomicity unit.", "Endpoint equality hides execution history, not a demonstrated loss of differing bytes.", "A mixed result is not by itself a violation of an unspecified write contract.", "The XOR mask demonstrates reversible encoding only; no compression or device stress is measured.", "Repeated version tags do not distinguish every possible byte-level cut.", "No address or operation identity is encoded; misplaced writes and order are not checked.", "The public fixed XOR mask provides no authentication.", ], "summaries": summaries, "cases": rows, "controls": controls, "unalignedControls": unaligned_controls, "byteCutSweep": byte_cut_sweep(), } if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--output", type=Path) args = parser.parse_args() output = json.dumps(experiment(), indent=2) + "\n" if args.output: args.output.write_text(output, encoding="utf-8") else: print(output, end="")