#!/usr/bin/env python3 # SPDX-License-Identifier: CC0-1.0 """Replay the pinned CQF contract checks without editing upstream source.""" import argparse import hashlib import json import platform import subprocess import sys import tempfile from datetime import datetime, timezone from pathlib import Path COMMIT = "68939f587b0f33f8c45be4f0e2a18e12bffa9fa6" EXPECTED = { "schemaVersion": 1, "upstreamCommit": COMMIT, "hashMode": "QF_HASH_DEFAULT", "seed": 0, "keyBits": 10, "initialSlots": 64, "resizedSlots": 128, "keyA": 0, "collidingKeyB": 1044, "noncollidingKeyC": 1, "countAfterA5": 5, "countAfterA5B2": 7, "countAfterResize": 7, "countAfterMatchedRemoveA5": 2, "countAfterAbsentRemoveB1": 0, "countAfterRemoveZero": 5, "countAfterDeleteAll": 0, } def run(args, cwd=None): return subprocess.run(args, cwd=cwd, check=True, capture_output=True, text=True).stdout.strip() def output_destination(path, source): if path.exists() or path.is_symlink(): raise FileExistsError("Output must be a new file: " + str(path)) destination = path.resolve() if destination.is_relative_to(source): raise ValueError("Output must be outside the upstream source tree") return destination def main(): parser = argparse.ArgumentParser() parser.add_argument("source", type=Path) parser.add_argument("--output", type=Path, required=True) args = parser.parse_args() source = args.source.resolve() output = output_destination(args.output, source) harness = Path(__file__).with_name("check.c").resolve() if platform.machine() != "x86_64" or sys.byteorder != "little": raise SystemExit("This receipt is scoped to little-endian x86_64.") if run(["git", "rev-parse", "HEAD"], source) != COMMIT: raise SystemExit("Unexpected upstream commit") if run(["git", "status", "--porcelain", "--untracked-files=no"], source): raise SystemExit("Tracked upstream source is modified") files = ["src/gqf.c", "src/hashutil.c", "src/partitioned_counter.c"] variants = [] with tempfile.TemporaryDirectory(prefix="hesela-cqf-") as tmp: for flags in [["-O0"], ["-O2"], ["-O2", "-DNDEBUG"]]: binary = str(Path(tmp) / "check") command = ["gcc", "-std=gnu11", *flags, "-Iinclude", str(harness), *files, "-lpthread", "-lm", "-o", binary] run(command, source) observed = json.loads(run([binary])) if observed != EXPECTED: raise SystemExit("Observed contract differs: " + repr(observed)) variants.append({"flags": flags, "passed": True}) hashes = {p: hashlib.sha256((source / p).read_bytes()).hexdigest() for p in [*files, "include/gqf.h", "include/gqf_int.h", "LICENSE"]} hashes["check.c"] = hashlib.sha256(harness.read_bytes()).hexdigest() receipt = {"checkedAt": datetime.now(timezone.utc).isoformat(), "compiler": run(["gcc", "--version"]).splitlines()[0], "platform": platform.platform(), "observations": EXPECTED, "variants": variants, "sha256": hashes} output.parent.mkdir(parents=True, exist_ok=True) with output.open("x", encoding="utf-8") as stream: stream.write(json.dumps(receipt, indent=2) + "\n") print(json.dumps(receipt)) if __name__ == "__main__": main()