#!/usr/bin/env python3 """Wolfram Alpha cross-validation of AVM ISA arithmetic across all ports.""" import json, os, subprocess, sys from pathlib import Path WOLFRAM_KEY = os.environ.get("WOLFRAM_APP_ID", "") SILVER = Path(__file__).resolve().parent.parent def wolfram(query): """Query Wolfram Alpha and return result.""" if not WOLFRAM_KEY: return None import urllib.request, urllib.parse params = urllib.parse.urlencode({"input": query, "appid": WOLFRAM_KEY, "format": "plaintext"}) try: with urllib.request.urlopen(f"https://api.wolframalpha.com/v2/query?{params}", timeout=15) as r: data = r.read().decode() import re texts = re.findall(r'(.*?)</plaintext>', data, re.DOTALL) return ' '.join(t.strip() for t in texts if t.strip()) if texts else None except: return None def verify_arithmetic(): """Verify AVM arithmetic matches Wolfram Alpha ground truth.""" cases = [ ("INT32_MAX", 2147483647, "2147483647"), ("Q16 scale (2^16)", 65536, "65536"), ("neg(-2147483647)", "-(-2147483647)", "2147483647"), ("floor(-1.666)", "floor(-1.666)", "-2"), ] results = [] for name, py_val, wa_expected in cases: wa_result = wolfram(name) if isinstance(py_val, str) else wolfram(str(py_val)) match = wa_result and wa_expected in wa_result results.append({"property": name, "python": py_val, "wolfram": wa_result, "match": match}) return results def run_port_tests(): """Run all AVM port test harnesses and collect results.""" results = {} ports = [ ("Python", [sys.executable, "tests/test_avm_python.py"], {"cwd": SILVER, "env": {**os.environ, "PYTHONPATH": str(SILVER / "python")}}), ("Rust", ["cargo", "test"], {"cwd": SILVER / "rust"}), ] for name, cmd, kw in ports: try: r = subprocess.run(cmd, capture_output=True, text=True, timeout=60, **kw) results[name] = {"passed": r.returncode == 0, "output": r.stdout[-200:]} except Exception as e: results[name] = {"passed": False, "error": str(e)} return results def main(): report = { "schema": "avm_port_verification_v1", "wolfram_arithmetic": verify_arithmetic(), "port_tests": run_port_tests(), } # Write receipt out = SILVER / "signatures" / "avm_verification_receipt.json" out.write_text(json.dumps(report, indent=2)) print(f"Receipt written to {out}") # Summary print("\n=== Wolfram Verification ===") for c in report["wolfram_arithmetic"]: print(f" {'✅' if c['match'] else '❌'} {c['property']}: matched={c['match']}") print("\n=== Port Tests ===") for name, r in report["port_tests"].items(): status = "✅" if r.get("passed") else "❌" print(f" {status} {name}: {'pass' if r.get('passed') else 'fail'}") success = all(c["match"] for c in report["wolfram_arithmetic"] if c["wolfram"]) return 0 if success else 1 if __name__ == "__main__": sys.exit(main())