#!/usr/bin/env python3 # SPDX-License-Identifier: MIT # Copyright (c) 2026 Resvanta, Inc. (DE) & Resvanta Technologies Inc. (BC) """ Resvanta Standalone Cryptographic & RFC 6962 Merkle Verifier (Zero Dependencies). Verifies Resvanta public trust manifests, Ed25519 signatures (RFC 8032), RFC 6962 SHA-256 Merkle inclusion proofs, and binary .sav dataset integrity using ONLY the Python 3 standard library—online against did:web:survey.resvanta.com or 100% offline. Usage: python3 verify_resvanta_receipt.py # Live did:web key fetch + local file checks python3 verify_resvanta_receipt.py --offline # Air-gapped (--network none) pinned-key mode python3 verify_resvanta_receipt.py --dir ./artifacts # Custom artifact directory """ from __future__ import annotations import argparse, base64, hashlib, json, pathlib, sys, urllib.request PINNED_KID = "rre-ed25519-4d13cc175692ffe6" PINNED_JWK_X = "ajejvHzPYoHokAiEyqdjC107Z6aR0J3TITzhifaHpyE" # --- Pure-Python RFC 8032 Section 5.1 Ed25519 Verification (0 external deps) --- _P = 2**255 - 19 _L = 2**252 + 27742317777372353535851937790883648493 _D = (-121665 * pow(121666, _P - 2, _P)) % _P _I = pow(2, (_P - 1) // 4, _P) def _xrecover(y: int) -> int: xx = (y * y - 1) * pow(_D * y * y + 1, _P - 2, _P) % _P x = pow(xx, (_P + 3) // 8, _P) if (x * x - xx) % _P != 0: x = (x * _I) % _P if (x * x - xx) % _P != 0: raise ValueError("Point off Curve25519") return (_P - x) if (x & 1) else x _BY = (4 * pow(5, _P - 2, _P)) % _P _BX = _xrecover(_BY) _B = (_BX % _P, _BY % _P, 1, (_BX * _BY) % _P) def _ed_add(p1: tuple[int, int, int, int], p2: tuple[int, int, int, int]) -> tuple[int, int, int, int]: x1, y1, z1, t1 = p1 x2, y2, z2, t2 = p2 a, b = ((y1 - x1) * (y2 - x2)) % _P, ((y1 + x1) * (y2 + x2)) % _P c, d = (2 * _D * t1 * t2) % _P, (2 * z1 * z2) % _P e, f, g, h = (b - a) % _P, (d - c) % _P, (d + c) % _P, (b + a) % _P return (e * f) % _P, (g * h) % _P, (f * g) % _P, (e * h) % _P def _ed_mul(pt: tuple[int, int, int, int], scalar: int) -> tuple[int, int, int, int]: res, cur = (0, 1, 1, 0), pt while scalar > 0: if scalar & 1: res = _ed_add(res, cur) cur = _ed_add(cur, cur) scalar >>= 1 return res def _encode_point(pt: tuple[int, int, int, int]) -> bytes: x, y, z, _ = pt zi = pow(z, _P - 2, _P) xp, yp = (x * zi) % _P, (y * zi) % _P raw = bytearray(yp.to_bytes(32, "little")) raw[31] |= (xp & 1) << 7 return bytes(raw) def _decode_point(enc: bytes) -> tuple[int, int, int, int]: if len(enc) != 32: raise ValueError("Invalid point length") y = int.from_bytes(enc, "little") & ((1 << 255) - 1) if y >= _P: raise ValueError("Invalid y coordinate") x = _xrecover(y) if (x & 1) != ((enc[31] >> 7) & 1): x = (_P - x) % _P return x, y, 1, (x * y) % _P def verify_ed25519_rfc8032(pub_bytes: bytes, msg: bytes, sig: bytes) -> bool: if len(pub_bytes) != 32 or len(sig) != 64: return False r_bytes, s_int = sig[:32], int.from_bytes(sig[32:], "little") if s_int >= _L: return False try: a_pt, r_pt = _decode_point(pub_bytes), _decode_point(r_bytes) except ValueError: return False k = int.from_bytes(hashlib.sha512(r_bytes + pub_bytes + msg).digest(), "little") % _L return _encode_point(_ed_mul(_B, s_int)) == _encode_point(_ed_add(r_pt, _ed_mul(a_pt, k))) def b64url_decode(val: str) -> bytes: return base64.urlsafe_b64decode(val + "=" * ((4 - len(val) % 4) % 4)) def resolve_did_public_key(did_url: str, offline: bool, local_dir: pathlib.Path) -> tuple[bytes, str]: local_did = local_dir / "did.json" if offline: if not local_did.exists(): return b64url_decode(PINNED_JWK_X), f"offline-pinned ({PINNED_KID})" doc = json.loads(local_did.read_text(encoding="utf-8")) else: req = urllib.request.Request(did_url, headers={"User-Agent": "Resvanta-Trust-Verifier/1.0"}) with urllib.request.urlopen(req, timeout=10) as resp: doc = json.loads(resp.read().decode("utf-8")) for vm in doc.get("verificationMethod", []): jwk = vm.get("publicKeyJwk") if jwk and jwk.get("crv") == "Ed25519" and jwk.get("x"): return b64url_decode(jwk["x"]), f"live {doc.get('id')} ({jwk.get('kid')})" raise RuntimeError("No Ed25519 publicKeyJwk found in DID document") def verify_envelope(env: dict, pub_bytes: bytes) -> bool: signing_input = f"{env['domainTag']}\n{env['canonicalPayload']}".encode("utf-8") if hashlib.sha256(signing_input).hexdigest() != env.get("payloadSha256"): return False return verify_ed25519_rfc8032(pub_bytes, signing_input, bytes.fromhex(env["signatureHex"])) def verify_rfc6962_path(raw_bytes: bytes, leaf_hex: str, path: list[dict], expected_root: str) -> bool: cur = hashlib.sha256(b"\x00" + raw_bytes).hexdigest() if cur != leaf_hex: return False for step in path: sib = step["hash"] pair = (bytes.fromhex(sib) + bytes.fromhex(cur)) if step["side"] == "left" else (bytes.fromhex(cur) + bytes.fromhex(sib)) cur = hashlib.sha256(b"\x01" + pair).hexdigest() return cur == expected_root def main() -> int: ap = argparse.ArgumentParser(description="Verify Resvanta Ed25519 & RFC 6962 Trust Artifacts") ap.add_argument("--dir", default=str(pathlib.Path(__file__).resolve().parent), help="Artifact folder") ap.add_argument("--offline", action="store_true", help="Verify strictly offline (--network none)") args = ap.parse_args() folder = pathlib.Path(args.dir) manifest = json.loads((folder / "resvanta-trust-manifest.json").read_text(encoding="utf-8")) pub_bytes, source_label = resolve_did_public_key(manifest["didDocumentUrl"], args.offline, folder) print(f"[1/4] Resolved Ed25519 Authority Key: {source_label}") if not verify_envelope(manifest["signatureEnvelope"], pub_bytes): print("[FAIL] Master manifest Ed25519 signature verification failed!", file=sys.stderr) return 2 print(f"[2/4] Verified RFC 8032 Ed25519 Signature on Master Manifest (Merkle Root: {manifest['merkleRootSha256'][:20]}...)") checked = 0 for item in manifest["artifacts"]: fpath = folder / item["fileName"] if not fpath.exists(): continue raw = fpath.read_bytes() if hashlib.sha256(raw).hexdigest() != item["fileSha256"]: print(f"[FAIL] SHA-256 mismatch on {item['fileName']}", file=sys.stderr) return 3 if not verify_rfc6962_path(raw, item["rfc6962LeafHash"], item["rfc6962AuditPath"], manifest["merkleRootSha256"]): print(f"[FAIL] RFC 6962 Merkle proof invalid on {item['fileName']}", file=sys.stderr) return 4 checked += 1 print(f" + {item['fileName']} ({len(raw):,} B): SHA-256 & RFC 6962 Merkle inclusion OK") rre_p, sav_p = folder / "rre-fre902-14-manifest.json", folder / "rre-synthetic-b2b-study.sav" if rre_p.exists() and sav_p.exists(): rre_m, sav_raw = json.loads(rre_p.read_text(encoding="utf-8")), sav_p.read_bytes() if not sav_raw.startswith(b"$FL2") or hashlib.sha256(sav_raw).hexdigest() != rre_m["savSha256"] or not verify_envelope(rre_m["signatureEnvelope"], pub_bytes): print("[FAIL] Binary .sav or signed FRE 902(14) manifest verification failed!", file=sys.stderr) return 5 print("[3/4] Verified Binary .sav ($FL2 magic) & Signed FRE 902(14)-Ready Hash Manifest") print(f"[4/4] PASS: Verified {checked}/{manifest['artifactCount']} present artifacts with 0 cryptographic errors.") return 0 if __name__ == "__main__": sys.exit(main())