"""Offline integrity example. Never authorizes work; no network or file I/O on import.""" import hashlib import json import re GENERATION = "ARCH-COMPLETE-01" PUBLIC = ("generation", "binding_sha256", "public_api_sha256", "public_dictionary_sha256", "public_errors_sha256") COMMON = {"profile", "active", "generation", "api_sha256", "mcp_sha256", "dictionary_sha256", "errors_sha256", *PUBLIC} GENERIC = COMMON | {"status"} COMPILED = COMMON | {"limits_sha256", "full_profile_source_sha256", "adapters_sha256"} ARTIFACTS = {"openapi.json": "public_api_sha256", "dictionary.json": "public_dictionary_sha256", "errors.json": "public_errors_sha256"} HEX = re.compile(r"[a-f0-9]{64}\Z") def scalar_string(value): return isinstance(value, str) and all(not 0xD800 <= ord(c) <= 0xDFFF for c in value) def canonical_binding(value): """Only flat string/bool/null objects; no general JSON canonicalizer claim.""" if type(value) is not dict or any(not scalar_string(k) or not (v is None or type(v) is bool or scalar_string(v)) for k, v in value.items()): raise ValueError("unsupported_binding_shape") return json.dumps(value, sort_keys=True, separators=(",", ":"), ensure_ascii=False, allow_nan=False).encode("utf-8") def digest(value): if type(value) is not bytes: raise ValueError("artifact_must_be_bytes") return hashlib.sha256(value).hexdigest() def verify_bundle(binding, canonical, snapshot, readable=None, readable_hashes=None): """snapshot is a selected local binding/profile snapshot, not a validated live response. canonical maps four names ending .json.bytes to original bytes. readable_hashes is an optional trusted, public-only filename -> exact file SHA256 projection. Inputs and any caller-held operation identity are never modified. """ encoded = canonical_binding(binding) keys = set(binding) if keys not in (GENERIC, COMPILED): raise ValueError("unsupported_binding_fields") if type(binding["active"]) is not bool or binding["generation"] != GENERATION: raise ValueError("unknown_generation_or_invalid_active") if keys == GENERIC: if binding["profile"] is not None or binding["status"] != "generic_template_not_released" or binding["active"]: raise ValueError("invalid_generic_binding") elif not scalar_string(binding["profile"]) or not binding["profile"]: raise ValueError("invalid_profile") if any(not isinstance(v, str) or HEX.fullmatch(v) is None for k, v in binding.items() if k.endswith("_sha256")): raise ValueError("invalid_digest") if type(canonical) is not dict or set(canonical) != {n + ".bytes" for n in (*ARTIFACTS, "binding.json")}: raise ValueError("missing_or_unknown_canonical_artifact") if type(canonical["binding.json.bytes"]) is not bytes: raise ValueError("artifact_must_be_bytes") if canonical["binding.json.bytes"] != encoded: raise ValueError("binding_canonical_bytes_mismatch") without_self = {k: v for k, v in binding.items() if k != "binding_sha256"} if digest(canonical_binding(without_self)) != binding["binding_sha256"]: raise ValueError("binding_digest_mismatch") for name, field in ARTIFACTS.items(): if digest(canonical[name + ".bytes"]) != binding[field]: raise ValueError("public_artifact_digest_mismatch") if type(snapshot) is not dict or set(snapshot) != {"profile_id", "revision", "contract_binding"}: raise ValueError("invalid_snapshot_fields") if not scalar_string(snapshot["profile_id"]) or not snapshot["profile_id"] or not scalar_string(snapshot["revision"]) or not snapshot["revision"]: raise ValueError("invalid_snapshot_identity") cb = snapshot["contract_binding"] if type(cb) is not dict or set(cb) != set(PUBLIC) or any(cb[k] != binding[k] for k in PUBLIC): raise ValueError("capability_binding_mismatch") if binding["profile"] is not None and snapshot["profile_id"] != binding["profile"]: raise ValueError("capability_profile_mismatch") readable_status = "not_verified" if readable_hashes is not None: names = {*ARTIFACTS, "binding.json"} if type(readable_hashes) is not dict or set(readable_hashes) != names or type(readable) is not dict or set(readable) != names: raise ValueError("incomplete_readable_file_records") for name in names: expected = readable_hashes[name] if not isinstance(expected, str) or HEX.fullmatch(expected) is None or digest(readable[name]) != expected: raise ValueError("readable_file_digest_mismatch") readable_status = "exact_file_hashes_verified" reasons = ["offline_integrity_is_not_release_or_entitlement", "profile_revision_not_in_bundle"] if not binding["active"]: reasons.append("inactive_binding") if binding["profile"] is None: reasons.append("generic_template_not_profile") return {"canonical_integrity": "verified", "readable_integrity": readable_status, "generation": binding["generation"], "profile_id": snapshot["profile_id"], "supplied_revision": snapshot["revision"], "revision_independently_verified": False, "new_work_allowed": False, "reasons": reasons}