Download projects/spatial-0/scripts/build_proof_bundle.py from KineJIng/kinejing-open-source: direct link, hf CLI and curl.
- Browser
- Download file 4.78 kB
-
https://huggingface.co/datasets/KineJIng/kinejing-open-source/resolve/main/projects/spatial-0/scripts/build_proof_bundle.py
- Command line
-
hf download hf://datasets/KineJIng/kinejing-open-source/projects/spatial-0/scripts/build_proof_bundle.py
-
curl -L -o build_proof_bundle.py https://huggingface.co/datasets/KineJIng/kinejing-open-source/resolve/main/projects/spatial-0/scripts/build_proof_bundle.py
4.78 kB
| from __future__ import annotations | |
| import argparse | |
| import hashlib | |
| import json | |
| from datetime import datetime, timezone | |
| from pathlib import Path | |
| def canonical_json(value: object) -> bytes: | |
| return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":")).encode("utf-8") | |
| def sha256_bytes(value: bytes) -> str: | |
| return hashlib.sha256(value).hexdigest() | |
| def sha256_file(path: Path) -> str: | |
| digest = hashlib.sha256() | |
| with path.open("rb") as stream: | |
| for block in iter(lambda: stream.read(1024 * 1024), b""): | |
| digest.update(block) | |
| return digest.hexdigest() | |
| def merkle_root(leaves: list[str]) -> str: | |
| if not leaves: | |
| return sha256_bytes(b"") | |
| level = [bytes.fromhex(item) for item in leaves] | |
| while len(level) > 1: | |
| if len(level) % 2: | |
| level.append(level[-1]) | |
| level = [hashlib.sha256(level[index] + level[index + 1]).digest() for index in range(0, len(level), 2)] | |
| return level[0].hex() | |
| def build(run_path: Path, output_path: Path) -> dict: | |
| run = json.loads(run_path.read_text(encoding="utf-8")) | |
| run_dir = run_path.parent | |
| source = run["input"]["capture"]["source"] | |
| world_seed = { | |
| "capture_sha256": source["sha256"], | |
| "frames": [item["sha256"] for item in run["input"]["files"]], | |
| "model": run["model"], | |
| "settings": run["settings"], | |
| } | |
| artifacts = [] | |
| artifact_candidates = ( | |
| "exports/npz/results.npz", | |
| "prediction.npz", | |
| "scene.glb", | |
| "evidence.glb", | |
| "evidence.ply", | |
| ) | |
| for name in artifact_candidates: | |
| path = run_dir / name | |
| if path.is_file(): | |
| artifacts.append({"path": name, "bytes": path.stat().st_size, "sha256": sha256_file(path)}) | |
| leaves = [sha256_bytes(canonical_json(item)) for item in run["input"]["files"]] | |
| leaves.extend(sha256_bytes(canonical_json(item)) for item in artifacts) | |
| proof = { | |
| "schema": "kineworld.spatial-lineage.proof.v1", | |
| "created_at": datetime.now(timezone.utc).isoformat(), | |
| "world_id": f"kw:world:{sha256_bytes(canonical_json(world_seed))}", | |
| "mode_contract": { | |
| "imagine": "same persistent world with evidence overlay hidden", | |
| "verify": "same persistent world with lineage and confidence exposed", | |
| "camera_and_state_preserved": True, | |
| }, | |
| "capture_commitment": { | |
| "name": source["name"], | |
| "bytes": source["bytes"], | |
| "sha256": source["sha256"], | |
| "source_media_embedded": False, | |
| }, | |
| "frame_commitments": run["input"]["files"], | |
| "producer": {"model": run["model"], "settings": run["settings"]}, | |
| "provenance": run["provenance"], | |
| "artifacts": artifacts, | |
| "merkle": {"algorithm": "sha256-pair-duplicate-last", "leaf_count": len(leaves), "root": merkle_root(leaves)}, | |
| "limitations": [ | |
| "This bundle is content integrity evidence, not proof that model estimates are physically correct.", | |
| "Confidence values are model scores and are not calibrated probabilities.", | |
| "The reconstruction scale is model-relative unless separately calibrated.", | |
| ], | |
| } | |
| output_path.write_text(json.dumps(proof, ensure_ascii=False, indent=2), encoding="utf-8") | |
| return proof | |
| def verify(proof_path: Path, run_dir: Path) -> None: | |
| proof = json.loads(proof_path.read_text(encoding="utf-8")) | |
| for artifact in proof["artifacts"]: | |
| path = run_dir / artifact["path"] | |
| if not path.is_file() or sha256_file(path) != artifact["sha256"]: | |
| raise SystemExit(f"verification failed: {artifact['path']}") | |
| leaves = [sha256_bytes(canonical_json(item)) for item in proof["frame_commitments"]] | |
| leaves.extend(sha256_bytes(canonical_json(item)) for item in proof["artifacts"]) | |
| if merkle_root(leaves) != proof["merkle"]["root"]: | |
| raise SystemExit("verification failed: Merkle root") | |
| print(f"verified {proof['world_id']} ({len(leaves)} commitments)") | |
| def main() -> None: | |
| parser = argparse.ArgumentParser(description="Build or verify a KineJing spatial-lineage proof") | |
| parser.add_argument("run", type=Path, help="run.json when building; proof.json when verifying") | |
| parser.add_argument("--output", type=Path) | |
| parser.add_argument("--verify", action="store_true") | |
| args = parser.parse_args() | |
| if args.verify: | |
| verify(args.run.resolve(), args.run.resolve().parent) | |
| return | |
| output = args.output.resolve() if args.output else args.run.resolve().with_name("proof.json") | |
| proof = build(args.run.resolve(), output) | |
| print(f"wrote {output}\n{proof['world_id']}\nmerkle {proof['merkle']['root']}") | |
| if __name__ == "__main__": | |
| main() | |