"""Exhaustive repeated-byte fixtures against unmodified fastcdc-py 1.7.0.""" import argparse from collections import Counter from datetime import datetime, timezone import hashlib import importlib import importlib.metadata import json from pathlib import Path import platform PARAMETERS = {"min_size": 2048, "avg_size": 8192, "max_size": 65536} INPUT_BYTES = 131073 SOURCE_HASHES = { "fastcdc.fastcdc_py": "591dd26e01f619783e9550424dadef129852350d05c8847a390429567bb80b7c", "fastcdc.utils": "c4d47b1ef5a6cf846de934cd88279fe2d27111de4ab4e2fab9407657f254757d", } def require(condition, message): if not condition: raise ValueError(message) def sha(data): return hashlib.sha256(data).hexdigest() def load_implementation(): require(importlib.metadata.version("fastcdc") == "1.7.0", "Requires fastcdc 1.7.0") for name, expected in SOURCE_HASHES.items(): module = importlib.import_module(name) require(sha(Path(module.__file__).read_bytes()) == expected, "Source mismatch: " + name) return importlib.import_module("fastcdc.fastcdc_py") def state_after(g, steps): """Closed form of p[t+1] = floor(p[t]/2) + g from p[0] = 0.""" require(type(g) is int and g > 0, "g must be a positive integer") require(type(steps) is int and steps >= 0, "steps must be a nonnegative integer") divisor = 1 << steps return 2 * g - (2 * g + divisor - 1) // divisor def predict_first_cut(g): # Fixed experiment parameters only. Once stable, an odd state cannot # satisfy either low-bit zero mask; the later phase cannot create a cut. stable_after = (2 * g - 1).bit_length() require(stable_after < 5120 - PARAMETERS["min_size"], "Transient exceeds first phase") transient = [state_after(g, t) for t in range(1, stable_after + 1)] hit = next((i + 1 for i, p in enumerate(transient) if p & 16383 == 0), None) return { "stableState": 2 * g - 1, "stableAfterUpdates": stable_after, "firstPredicateHitUpdate": hit, "firstCutBytes": PARAMETERS["min_size"] + hit if hit is not None else 65536, "reason": "early-mask-match" if hit is not None else "maximum-after-no-mask-match", } def observe(byte_value, size, module=None): require(type(byte_value) is int and 0 <= byte_value <= 255, "Invalid byte") require(type(size) is int and size >= 0, "Invalid size") module = module or load_implementation() data = bytes([byte_value]) * size chunks = list(module.fastcdc_py(data, **PARAMETERS, fat=True)) offset = 0 for chunk in chunks: require(chunk.offset == offset and chunk.length > 0, "Invalid chunk coverage") require(chunk.length == len(chunk.data), "Length mismatch") offset += chunk.length require(offset == size and b"".join(c.data for c in chunks) == data, "Reconstruction failed") g = module.GEAR[byte_value] prediction = predict_first_cut(g) lengths = [c.length for c in chunks] limit = prediction["firstCutBytes"] full, tail = divmod(size, limit) expected = [limit] * full + ([tail] if tail else []) # A short final input may match a transient predicate before EOF, which # is still exactly captured by this fixed-input repeated-byte prediction. require(lengths == expected, "Independent prediction differs from library") return { "byteValue": byte_value, "byteHex": f"0x{byte_value:02X}", "inputBytes": size, "inputSha256": sha(data), "gearValue": g, **prediction, "chunkLengthsBytes": lengths, "chunkCount": len(chunks), "reconstructedBytes": offset, "terminalChunkBytes": lengths[-1] if lengths else None, } def run(): module = load_implementation() require(len(module.GEAR) == 256, "Unexpected Gear table size") rows = [observe(b, INPUT_BYTES, module) for b in range(256)] grouped = Counter((r["firstCutBytes"], r["reason"]) for r in rows) return { "schemaVersion": "1.0", "kind": "deterministic-exhaustive-repeated-byte-experiment", "generatedAt": datetime.now(timezone.utc).isoformat(), "implementation": {"package": "fastcdc", "version": "1.7.0", "api": "fastcdc.fastcdc_py.fastcdc_py", "python": platform.python_version(), "sourceSha256": SOURCE_HASHES}, "parametersBytes": PARAMETERS, "normalizationTransitionBytes": module.center_size(8192, 2048, 65536), "masks": {"early": module.mask(14), "late": module.mask(12)}, "population": {"fixtureCount": 256, "bytesPerFixture": INPUT_BYTES, "construction": "One separate input for each byte value 0..255, repeated 131073 times."}, "groups": [{"firstCutBytes": cut, "reason": reason, "fixtureCount": n} for (cut, reason), n in sorted(grouped.items())], "limitations": [ "Exhaustive for these 256 constant-byte inputs and these parameters only.", "No throughput, real-workload deduplication ratio or physical saved-capacity measurement.", "Right-shift recurrence in this pinned Python variant; not the 2016 paper's left-shift recurrence.", "All terminal chunks retained; cut reasons inferred from a checked independent closed form.", ], "rows": rows, } def main(): parser = argparse.ArgumentParser() group = parser.add_mutually_exclusive_group(required=True) group.add_argument("--output", type=Path) group.add_argument("--check", type=Path) args = parser.parse_args() result = run() if args.check: old = json.loads(args.check.read_text()) for value in (old, result): value.pop("generatedAt", None) require(old == result, "Published result differs (including Python version)") print("All 256 fixtures reproduce exactly") else: args.output.write_text(json.dumps(result, indent=2) + "\n") print(json.dumps(result["groups"])) if __name__ == "__main__": main()