#!/usr/bin/env python3 """Standalone verifier for a CAIN-42 live Byzantine-evidence bundle. Imports no CAIN implementation. Ship it next to `verify_byzantine_evidence.py` and `verify_pbft_qc_bundle.py` (both also CAIN-free); it loads those two by path. python3 verify_byzantine_bundle.py bundle.json python3 verify_byzantine_bundle.py https://cainstudio.online/proof/bundle//BYZANTINE_BUNDLE.json Checks, from the bundle only: * membership: configuration_hash recomputed from ids + Ed25519 keys; * every honest node's hash-chained Byzantine evidence log: chain + every embedded equivocation/deviation proof (Ed25519 over the exact signed message); * every honest node's COMMIT_QC chain: contiguous from genesis, parent-bound, >= quorum distinct signers, identical head on every honest node; * containment: at least f+1 honest nodes hold a PROVEN equivocation proof against the named node AND quarantine it; * liveness after containment: a certificate committed in a later view; * negative controls: tampered QC, tampered proof and tampered evidence chain are all rejected. """ from __future__ import annotations import copy import json import sys import urllib.request from pathlib import Path HERE = Path(__file__).resolve().parent sys.path.insert(0, str(HERE)) import verify_byzantine_evidence as BE # noqa: E402 import verify_pbft_qc_bundle as Q # noqa: E402 def load(src: str) -> dict: raw = urllib.request.urlopen(src, timeout=30).read() if src.startswith(("http://", "https://")) \ else Path(src).read_bytes() return json.loads(raw) def main() -> int: if len(sys.argv) != 2: print(__doc__) return 2 b = load(sys.argv[1]) checks: list[tuple[str, bool, str]] = [] def check(name, ok, detail=""): checks.append((name, bool(ok), detail)) return ok mbc = b.get("membership") try: mb = Q.Membership(mbc) except Exception as e: # noqa: BLE001 print(f"FAIL membership_parse ({e})") return 1 keys = {m["node_id"]: m["public_key_b64"] for m in mbc["members"]} check("membership_configuration_hash", mb.configuration_hash == b.get("configuration_hash"), f"{mb.configuration_hash[:16]}… vs {str(b.get('configuration_hash'))[:16]}…") byz = b.get("byzantine_node") honest = b.get("honest_nodes") or [] height = int(b.get("height") or 0) base_view = int(b.get("base_view") or 0) nodes = b.get("nodes") or {} check("byzantine_node_is_not_an_honest_node", byz not in honest, f"byz={byz} honest={honest}") check("enough_honest_nodes", len(honest) >= 3, str(len(honest))) heads, apps, proven = set(), set(), [] for n in honest: nd = nodes.get(n) or {} check(f"{n}: reports_the_bundle_configuration", nd.get("configuration_hash") == b.get("configuration_hash")) ev = nd.get("evidence") or {} rep = BE.verify(ev, keys) check(f"{n}: evidence_chain_valid", rep["chain_valid"], rep["chain_detail"]) proofs = rep.get("proofs") or [] bad = [p for p in proofs if p["verdict"] == "INVALID"] check(f"{n}: no_invalid_embedded_proofs", not bad, ", ".join(p["detail"] for p in bad)[:200]) against = [p for p in proofs if p["type"] == "EQUIVOCATION_PROOF" and p["verdict"] == "PROVEN" and p["node_id"] == byz] if against: proven.append(n) check(f"{n}: quarantines_the_equivocating_node", byz in (nd.get("quarantined_nodes") or []), str(nd.get("quarantined_nodes"))) qcs = nd.get("commit_qcs") or [] parent = Q.genesis(mb.cluster_id, mb.epoch) okq, why = True, "" max_view = 0 for i, qc in enumerate(qcs, start=1): okq, why = Q.verify_qc(qc, mb, expected_parent=parent) if not okq: break if qc.get("sequence") != i: okq, why = False, f"sequence {qc.get('sequence')} at position {i}" break max_view = max(max_view, int(qc.get("view") or 0)) parent = Q.qc_decision_hash(qc) check(f"{n}: commit_chain_contiguous_and_signed", okq and len(qcs) == height, f"{len(qcs)}/{height} certificates; {why}"[:220]) check(f"{n}: head_matches_the_folded_chain", okq and parent == nd.get("head_decision_hash"), f"{str(parent)[:16]}… vs {str(nd.get('head_decision_hash'))[:16]}…") check(f"{n}: committed_a_certificate_after_containment", max_view > base_view, f"max view {max_view} > base {base_view}") heads.add(nd.get("head_decision_hash")) apps.add(nd.get("application_state_hash")) check("honest_nodes_share_one_decision_chain", len(heads) == 1, f"{len(heads)} distinct heads") check("honest_nodes_share_one_application_state", len(apps) == 1, f"{len(apps)} distinct states") needed = mb.f + 1 check(f"at_least_f+1_nodes_proved_the_equivocation", len(proven) >= needed, f"{sorted(proven)}; need {needed}") # ---------------------------------------------------------------- negative controls if honest: n0 = honest[0] tampered = copy.deepcopy(nodes[n0]["commit_qcs"][0]) tampered["proposal_digest"] = ("0" * 64) if tampered.get("proposal_digest") != "0" * 64 else ("1" * 64) okq, _ = Q.verify_qc(tampered, mb) check("negative_control: tampered_qc_rejected", not okq) ev = nodes[n0].get("evidence") or {} proof = next((r for r in ev.get("records", []) if r.get("event_type") == "EQUIVOCATION_PROOF"), None) if proof: bad = copy.deepcopy(proof["payload"]) bad["message_B"]["request_digest"] = "f" * 64 verdict, _ = BE.verify_equivocation(bad, keys) check("negative_control: tampered_equivocation_proof_rejected", verdict != "PROVEN") else: check("negative_control: tampered_equivocation_proof_rejected", False, "no proof to tamper") recs = copy.deepcopy(ev.get("records") or []) if recs: recs[0]["event_type"] = "TAMPERED" okc, _ = BE.verify_chain(recs) check("negative_control: tampered_evidence_chain_rejected", not okc) for name, ok, detail in checks: print(f"{'PASS' if ok else 'FAIL'} {name}" + (f" ({detail})" if detail else "")) ok = all(c[1] for c in checks) print(f"\n{'ALL_CHECKS_PASSED' if ok else 'FAILED'}: {sum(c[1] for c in checks)}/{len(checks)}") return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())