"""Asserts every normative vector printed in SPEC.md section 10.""" import sys from attestation import ( Chain, Policy, commitment, policy_hash, round_record, settle, settle_default, spread_bp, verify_reveal, ) P = Policy() NH = "a1" * 32 NI = "b2" * 32 fails = [] def eq(label, got, want): if got != want: fails.append(f"{label}: got {got!r}, want {want!r}") # 6.1 policy hash eq("policy_hash", policy_hash(P), "f5bbc16aaba884177e6581aa528aa6b84c489aa842e592a9998909154ed5c0c1") # 10.1 commitment args = dict(round_id="R-2026-08-0417", asset_id="SC-LT-000391", party_id="ACME-HOLDINGS", role="holder", amount_cents=42000, grade="B2", nonce_hex=NH) C = commitment(**args) eq("commitment", C, "f40ccaef3378cbfb0574d0499f8a8d3bfd35d2990edcf7943e5f163ed2db9165") eq("reveal ok", verify_reveal(C, {**args, "nonce": NH}), True) eq("bad amount", verify_reveal(C, {**args, "amount_cents": 42001, "nonce": NH}), False) eq("bad round", verify_reveal(C, {**args, "round_id": "R-2026-08-0418", "nonce": NH}), False) eq("role swap", verify_reveal(C, {**args, "role": "inspector", "nonce": NH}), False) # 10.2 settlement (holder, inspector, index, spread, outcome, settled, bonus, total) VECTORS = [ ("TV-1", 42000, 39000, None, 769, "auto_settled", 40500, 0, 40500), ("TV-2", 40000, 40000, None, 0, "auto_settled", 40000, 1000, 41000), ("TV-3", 44000, 40000, None, 1000, "auto_settled", 42000, 0, 42000), ("TV-4", 44100, 40000, 41500, 1025, "escalated", 41500, 0, 41500), ("TV-5", 90000, 30000, 20000, 20000, "escalated", 30000, 0, 30000), ("TV-6", 41000, 40000, None, 250, "auto_settled", 40500, 1013, 41513), ("TV-7", 4000, 3200, None, 2500, "auto_settled", 3600, 0, 3600), ] for name, hc, ic, idx, sp, oc, amt, bon, tot in VECTORS: eq(f"{name} spread", spread_bp(hc, ic), sp) s = settle(hc, ic, P, index_cents=idx) eq(f"{name} outcome", s.outcome, oc) eq(f"{name} amount", s.amount_cents, amt) eq(f"{name} bonus", s.bonus_cents, bon) eq(f"{name} total", s.total_cents, tot) d = settle_default(38500, P) eq("TV-8 outcome", d.outcome, "defaulted") eq("TV-8 total", d.total_cents, 38500) eq("TV-8 bonus", d.bonus_cents, 0) # 10.3 chain chain = Chain() hr = dict(round_id="R-2026-08-0417", asset_id="SC-LT-000391", party_id="ACME-HOLDINGS", role="holder", amount_cents=42000, grade="B2", nonce=NH) ir = dict(round_id="R-2026-08-0417", asset_id="SC-LT-000391", party_id="SC-BENCH-04", role="inspector", amount_cents=39000, grade="B3", nonce=NI) rec = round_record( round_id="R-2026-08-0417", asset_id="SC-LT-000391", policy=P, holder_commit=commitment(**{k: v for k, v in hr.items() if k != "nonce"}, nonce_hex=NH), inspector_commit=commitment(**{k: v for k, v in ir.items() if k != "nonce"}, nonce_hex=NI), holder_reveal=hr, inspector_reveal=ir, settlement=settle(42000, 39000, P), timestamps={"commit_opened": "2026-08-29T18:00:00Z", "commit_closed": "2026-09-01T18:00:00Z", "reveal_closed": "2026-09-02T18:00:00Z", "settled": "2026-09-02T18:04:11Z"}) e0 = chain.append({"type": "round_settled", "record": rec}) e1 = chain.append({"type": "placement", "asset_id": "SC-LT-000391", "tier": "C"}) eq("chain e0", e0["entry_hash"], "5472ffe25bd5e7e2f2cbfc38351017781c8280d6f14670c4c3bbb246c787f133") eq("chain e1", e1["entry_hash"], "375e31cf0216123658529e6575925543c865b83964def4393a34625b8b1ae1ad") eq("e1.prev links", e1["prev"], e0["entry_hash"]) eq("chain verifies", chain.verify(), True) chain.entries[0]["body"]["record"]["settlement"]["amount_cents"] = 41000 eq("tamper detected", chain.verify(), False) # invariants beyond the printed vectors try: settle(90000, 30000, P) fails.append("missing index should raise") except ValueError: pass try: commitment(**{**args, "nonce_hex": "ab" * 16}) fails.append("short nonce should raise") except ValueError: pass try: commitment(**{**args, "amount_cents": 420.0}) fails.append("float amount should raise") except TypeError: pass # settlement is order-independent for a, b in [(42000, 39000), (44100, 40000)]: s1 = settle(a, b, P, index_cents=41500) s2 = settle(b, a, P, index_cents=41500) eq(f"symmetry {a}/{b}", (s1.outcome, s1.total_cents), (s2.outcome, s2.total_cents)) # escalation never lands outside the revealed range for idx in (0, 1, 29999, 30000, 60000, 90000, 90001, 10**9): s = settle(90000, 30000, P, index_cents=idx) if not (30000 <= s.amount_cents <= 90000): fails.append(f"clamp escaped range at index={idx}: {s.amount_cents}") if fails: print("FAIL") for f in fails: print(" -", f) sys.exit(1) print(f"PASS — all SPEC.md section 10 vectors and invariants reproduce")