commit 5d10f20283effa38adb8e0072e3fe85007d4ca5c Author: saymrwulf Date: Fri Jul 3 10:51:03 2026 +0200 initial proof-aware tooling prototype diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..59b3063 --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +.venv/ +.pacta/ +.pacta-*/ +.pytest_cache/ +__pycache__/ +*.py[cod] +*.egg-info/ +.lake/ +*.olean +*.ilean diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000..4551b92 --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,13 @@ +# AGENTS.md + +Guidance for future Codex runs in this repository: + +- Do not add Charon or Aeneas transpilation flows. +- Do not regenerate Lean from Rust. Treat shipped Lean files in verified repos as the artifact. +- Do not claim end-to-end verification unless theorem coverage actually exists. +- Keep macOS and Apple Silicon portability. Do not rely on GNU `timeout`, `free`, `taskset`, Docker, Nix, or x86_64 assumptions. +- Prefer explicit theorem-boundary language over marketing language. +- Update tests with every behavior change. +- Do not silently lower risk ratings. A lower score must explain the failed or missing evidence. +- Do not hide proof failures behind warnings. +- Keep claim cards machine-readable and reports explicit about proven claims, preconditions, exclusions, trusted base, and residual risk. diff --git a/README.md b/README.md new file mode 100644 index 0000000..0e72add --- /dev/null +++ b/README.md @@ -0,0 +1,70 @@ +# proof-aware-crypto-tooling-agent + +`pacta` is a local CLI prototype for interpreting formal-verification evidence in cryptographic tooling. It is not a trading bot, does not move funds, and does not make financial decisions. + +The immediate corpus is the `saymrwulf/*-verified` family of repositories. The shipped Lean files are treated as the verification artifact. This project intentionally does not run Charon, Aeneas, extraction, or Rust-to-Lean regeneration. + +## Purpose + +An autonomous economic agent needs to answer a narrow question before trusting infrastructure: + +> Does this theorem cover the exact code path that will protect my funds? + +`pacta` helps answer that by replaying pure Lean checks where possible, auditing axioms and proof hygiene, generating machine-readable claim cards, and assigning residual-risk classifications with explicit exclusions. + +## macOS / Apple Silicon + +The prototype is written for Python 3.11+ and macOS on Apple Silicon. It does not assume GNU coreutils, Linux `free`, Linux `taskset`, GNU `timeout`, Docker, Nix, or x86_64. + +Lean tooling is detected with `shutil.which("lean")` and `shutil.which("lake")`. If neither is available, `pacta` reports clear diagnostics and still supports offline claim-card generation and static hygiene scans. + +## Install + +```bash +python3 -m venv .venv +. .venv/bin/activate +python -m pip install -e ".[dev]" +``` + +PyYAML is optional. Without it, `pacta` can still read the included simple YAML examples and JSON-compatible `.yaml` files. + +## Commands + +```bash +python -m pacta --help +pacta scan --config examples/repos.yaml +pacta claims --config examples/repos.yaml --repo-name dalek-ed25519-verified --offline-fixture --out claims.yaml +pacta audit --repo ./repos/dalek-ed25519-verified +pacta lean-check --repo ./repos/dalek-ed25519-verified +pacta report --claims claims.yaml --out report.md +pacta score --claims claims.yaml +``` + +## Truth Boundary + +The Ed25519 repositories should not be marketed as fully verified wallets or fully verified Ed25519 end-to-end. The strongest current claim is lower-layer and theorem-bound: + +For selected curve25519-dalek / Solana-Ed25519-family Rust code paths already transpiled into Lean, the repositories contain Lean-checked certificates for field arithmetic over `F_p`, `p = 2^255 - 19`, and complete twisted Edwards point-operation laws, under explicit invariants and backend constraints. + +`pacta` treats these as out of scope unless separately proven: + +- Full EdDSA signature verification. +- Complete Scalar52 arithmetic. +- SHA-512. +- Encoding, decoding, and canonicality. +- Rust compiler correctness. +- Charon/Aeneas translation faithfulness. +- Side-channel resistance. +- SIMD, AVX, hardware, zkVM, accelerator, or syscall paths. +- Wallet policy, transaction construction, RPC, chain, oracle, market, and LLM decision safety. + +## Risk Levels + +- `R0`: Unknown or untrusted. No usable evidence. +- `R1`: Tests, audits, or informal claims only. +- `R2`: Formal model exists, but it is incomplete, weakly tied to production code, or major proof gaps remain. +- `R3`: A specific lower-layer implementation artifact is Lean-checked for a specific backend and theorem boundary. +- `R4`: End-to-end primitive proof covers public API, parsing/encoding, scalar arithmetic, hashing interface, signature equation, rejection rules, and implementation boundary. +- `R5`: `R4` plus reproducible production builds, compiler/build assurance, side-channel analysis, hardware/KMS/MPC integration, and operational controls. + +Expected first-pass classification: Ed25519 field plus Edwards point arithmetic can reach `R3` if configured certificates compile and the axiom audit is clean. Full Ed25519 signature verification remains `R2` or lower unless complete scalar, encoding, hashing, and signature certificates exist. diff --git a/examples/anza-ed25519.claims.yaml b/examples/anza-ed25519.claims.yaml new file mode 100644 index 0000000..3f739e6 --- /dev/null +++ b/examples/anza-ed25519.claims.yaml @@ -0,0 +1,42 @@ +{ + "component": "anza-ed25519-verified", + "repo_url": "https://github.com/saymrwulf/anza-ed25519-verified.git", + "local_path": "repos/anza-ed25519-verified", + "repo_commit": null, + "verification_dir": "verification", + "kind": "ed25519", + "verified_backend": "serial/u64", + "certificates": [ + { + "name": "CurveFieldProofs.fieldImplementation", + "status": "proven", + "axiom_status": "clean", + "observed_axioms": ["propext", "Classical.choice", "Quot.sound"], + "expected_axioms": ["propext", "Classical.choice", "Quot.sound"] + }, + { + "name": "CurveFieldProofs.edwardsImplementation", + "status": "proven", + "axiom_status": "clean", + "observed_axioms": ["propext", "Classical.choice", "Quot.sound"], + "expected_axioms": ["propext", "Classical.choice", "Quot.sound"] + } + ], + "guarantees": [], + "preconditions": [], + "exclusions": ["Solana runtime semantics", "Solana transaction construction", "full EdDSA verification unless separately proven"], + "trusted_base": [], + "evidence": { + "lean_version": null, + "lake_version": null, + "check_log_path": null, + "axiom_log_path": null, + "scanned_files": [] + }, + "risk": { + "level": "R3", + "rationale": "Example fixture for Solana-relevant lower-layer arithmetic evidence.", + "blockers": [], + "deployment_constraints": ["Do not claim Solana transaction or runtime semantics are verified."] + } +} diff --git a/examples/dalek-ed25519.claims.yaml b/examples/dalek-ed25519.claims.yaml new file mode 100644 index 0000000..c3e1990 --- /dev/null +++ b/examples/dalek-ed25519.claims.yaml @@ -0,0 +1,42 @@ +{ + "component": "dalek-ed25519-verified", + "repo_url": "https://github.com/saymrwulf/dalek-ed25519-verified.git", + "local_path": "repos/dalek-ed25519-verified", + "repo_commit": null, + "verification_dir": "verification", + "kind": "ed25519", + "verified_backend": "serial/u64", + "certificates": [ + { + "name": "CurveFieldProofs.fieldImplementation", + "status": "proven", + "axiom_status": "clean", + "observed_axioms": ["propext", "Classical.choice", "Quot.sound"], + "expected_axioms": ["propext", "Classical.choice", "Quot.sound"] + }, + { + "name": "CurveFieldProofs.edwardsImplementation", + "status": "proven", + "axiom_status": "clean", + "observed_axioms": ["propext", "Classical.choice", "Quot.sound"], + "expected_axioms": ["propext", "Classical.choice", "Quot.sound"] + } + ], + "guarantees": [], + "preconditions": [], + "exclusions": ["full EdDSA verification", "side-channel resistance"], + "trusted_base": [], + "evidence": { + "lean_version": null, + "lake_version": null, + "check_log_path": null, + "axiom_log_path": null, + "scanned_files": [] + }, + "risk": { + "level": "R3", + "rationale": "Example fixture for lower-layer arithmetic evidence.", + "blockers": [], + "deployment_constraints": ["Use verified serial/u64 backend only."] + } +} diff --git a/examples/pasta-pallas.claims.yaml b/examples/pasta-pallas.claims.yaml new file mode 100644 index 0000000..8824015 --- /dev/null +++ b/examples/pasta-pallas.claims.yaml @@ -0,0 +1,27 @@ +{ + "component": "pasta-pallas-verified", + "repo_url": "https://github.com/saymrwulf/pasta-pallas-verified.git", + "local_path": "repos/pasta-pallas-verified", + "repo_commit": null, + "verification_dir": "verification", + "kind": "pasta_pallas", + "verified_backend": null, + "certificates": [], + "guarantees": ["Foundation evidence only unless aggregate certificates are present."], + "preconditions": ["Require add/mul/reduce/square/invert and aggregate fieldImplementation coverage."], + "exclusions": ["production-ready Pallas arithmetic unless add/mul/reduce/square/invert are proven"], + "trusted_base": [], + "evidence": { + "lean_version": null, + "lake_version": null, + "check_log_path": null, + "axiom_log_path": null, + "scanned_files": [] + }, + "risk": { + "level": "R2", + "rationale": "Foundation work without aggregate production field certificate.", + "blockers": ["No aggregate fieldImplementation-style certificate."], + "deployment_constraints": ["Do not classify as complete production field implementation."] + } +} diff --git a/examples/repos.yaml b/examples/repos.yaml new file mode 100644 index 0000000..56f40df --- /dev/null +++ b/examples/repos.yaml @@ -0,0 +1,68 @@ +repos: + - name: dalek-ed25519-verified + url: https://github.com/saymrwulf/dalek-ed25519-verified.git + kind: ed25519 + verification_dir: verification + verified_backend: serial/u64 + certificates: + - CurveFieldProofs.fieldImplementation + - CurveFieldProofs.edwardsImplementation + expected_axioms: + - propext + - Classical.choice + - Quot.sound + known_exclusions: + - full EdDSA verification + - Scalar52 arithmetic completeness + - SHA-512 + - encoding/canonicality unless separately proven + - side-channel resistance + - compiler correctness + - SIMD/AVX/accelerator paths + + - name: anza-ed25519-verified + url: https://github.com/saymrwulf/anza-ed25519-verified.git + kind: ed25519 + verification_dir: verification + verified_backend: serial/u64 + certificates: + - CurveFieldProofs.fieldImplementation + - CurveFieldProofs.edwardsImplementation + expected_axioms: + - propext + - Classical.choice + - Quot.sound + known_exclusions: + - Solana runtime semantics + - Solana transaction construction + - full EdDSA verification unless separately proven + - accelerator paths + + - name: risc0-ed25519-verified + url: https://github.com/saymrwulf/risc0-ed25519-verified.git + kind: ed25519 + verification_dir: verification + verified_backend: serial/u64 + backend_warning: pure Rust path only; do not treat zkVM accelerator/syscall path as verified + certificates: + - CurveFieldProofs.fieldImplementation + - CurveFieldProofs.edwardsImplementation + + - name: betrusted-ed25519-verified + url: https://github.com/saymrwulf/betrusted-ed25519-verified.git + kind: ed25519 + verification_dir: verification + verified_backend: serial/u64 + backend_warning: pure Rust path only; do not treat Engine25519/hardware accelerator as verified + certificates: + - CurveFieldProofs.fieldImplementation + - CurveFieldProofs.edwardsImplementation + + - name: pasta-pallas-verified + url: https://github.com/saymrwulf/pasta-pallas-verified.git + kind: pasta_pallas + verification_dir: verification + known_status: foundation only unless aggregate field certificate is present + known_exclusions: + - full fieldImplementation certificate unless present and axiom-clean + - production-ready Pallas arithmetic unless add/mul/reduce/square/invert are proven diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..d3c6437 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,27 @@ +[build-system] +requires = ["setuptools>=68"] +build-backend = "setuptools.build_meta" + +[project] +name = "proof-aware-crypto-tooling-agent" +version = "0.1.0" +description = "Proof-aware crypto tooling evidence interpreter and residual-risk scorer." +readme = "README.md" +requires-python = ">=3.11" +license = { text = "MIT" } +authors = [{ name = "PACTA prototype maintainers" }] +dependencies = [] + +[project.optional-dependencies] +yaml = ["PyYAML>=6"] +dev = ["pytest>=8"] + +[project.scripts] +pacta = "pacta.cli:main" + +[tool.setuptools.packages.find] +where = ["src"] + +[tool.pytest.ini_options] +pythonpath = ["src"] +testpaths = ["tests"] diff --git a/src/pacta/__init__.py b/src/pacta/__init__.py new file mode 100644 index 0000000..b085edf --- /dev/null +++ b/src/pacta/__init__.py @@ -0,0 +1,5 @@ +"""Proof-Aware Crypto Tooling Agent.""" + +__all__ = ["__version__"] + +__version__ = "0.1.0" diff --git a/src/pacta/__main__.py b/src/pacta/__main__.py new file mode 100644 index 0000000..a049ad7 --- /dev/null +++ b/src/pacta/__main__.py @@ -0,0 +1,5 @@ +from .cli import main + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/src/pacta/audit.py b/src/pacta/audit.py new file mode 100644 index 0000000..955c239 --- /dev/null +++ b/src/pacta/audit.py @@ -0,0 +1,133 @@ +from __future__ import annotations + +import re +from dataclasses import dataclass +from pathlib import Path + +from .manifest import VerificationLayout + + +@dataclass(slots=True) +class HygieneIssue: + severity: str + code: str + path: str + line: int + message: str + + def to_dict(self) -> dict[str, object]: + return { + "severity": self.severity, + "code": self.code, + "path": self.path, + "line": self.line, + "message": self.message, + } + + +SORRY_RE = re.compile(r"\bsorry\b") +AXIOM_RE = re.compile(r"^\s*axiom\s+[A-Za-z0-9_'.]+", re.MULTILINE) +TRUE_TARGET_RE = re.compile(r"\b(?:theorem|lemma)\s+[A-Za-z0-9_'.]+[\s\S]{0,240}?:\s*True\s*:=") +BY_TRIVIAL_RE = re.compile(r"\bby\s+trivial\b") +NATIVE_DECIDE_RE = re.compile(r"\bnative_decide\b") + + +def scan_hygiene(layout: VerificationLayout, certificates: list[str] | None = None) -> list[HygieneIssue]: + issues: list[HygieneIssue] = [] + certs = certificates or [] + seen_text = "" + for path in layout.lean_files: + rel = _rel(path, layout.verification_dir) + text = path.read_text(encoding="utf-8", errors="ignore") + code = strip_lean_comments(text) + seen_text += "\n" + code + issues.extend(_scan_file(path, rel, code)) + for path in layout.files_not_in_manifest: + issues.append( + HygieneIssue( + severity="warning", + code="proof-file-not-in-manifest", + path=_rel(path, layout.verification_dir), + line=1, + message="Lean proof file is not listed in an explicit manifest.", + ) + ) + for cert in certs: + if cert and cert not in seen_text: + issues.append( + HygieneIssue( + severity="error", + code="missing-certificate-name", + path=str(layout.verification_dir), + line=1, + message=f"Configured certificate name was not found in scanned Lean files: {cert}", + ) + ) + return issues + + +def strip_lean_comments(text: str) -> str: + out: list[str] = [] + i = 0 + depth = 0 + while i < len(text): + if depth == 0 and text.startswith("--", i): + while i < len(text) and text[i] != "\n": + i += 1 + if i < len(text): + out.append("\n") + i += 1 + continue + if text.startswith("/-", i): + depth += 1 + i += 2 + continue + if depth > 0: + if text.startswith("-/", i): + depth -= 1 + i += 2 + else: + if text[i] == "\n": + out.append("\n") + i += 1 + continue + out.append(text[i]) + i += 1 + return "".join(out) + + +def _scan_file(path: Path, rel: str, code: str) -> list[HygieneIssue]: + issues: list[HygieneIssue] = [] + for match in SORRY_RE.finditer(code): + issues.append(_issue("error", "sorry", rel, code, match.start(), "Executable Lean code contains 'sorry'.")) + if _is_under_proofs(path) and AXIOM_RE.search(code): + for match in AXIOM_RE.finditer(code): + issues.append(_issue("error", "axiom-in-proofs", rel, code, match.start(), "Proofs/ contains an axiom declaration.")) + for match in TRUE_TARGET_RE.finditer(code): + issues.append(_issue("warning", "trivial-true-target", rel, code, match.start(), "Theorem or lemma target appears to be trivially True.")) + if _is_spec_or_certificate(path): + for match in BY_TRIVIAL_RE.finditer(code): + issues.append(_issue("warning", "by-trivial-in-spec", rel, code, match.start(), "Spec/certificate/root file contains 'by trivial'.")) + for match in NATIVE_DECIDE_RE.finditer(code): + issues.append(_issue("advisory", "native-decide", rel, code, match.start(), "native_decide appears in proof code; review dependency cone.")) + return issues + + +def _issue(severity: str, code_name: str, rel: str, code: str, offset: int, message: str) -> HygieneIssue: + return HygieneIssue(severity=severity, code=code_name, path=rel, line=code.count("\n", 0, offset) + 1, message=message) + + +def _is_under_proofs(path: Path) -> bool: + return "Proofs" in path.parts + + +def _is_spec_or_certificate(path: Path) -> bool: + lower = path.name.lower() + return any(token in lower for token in ("spec", "certificate", "main", "root")) + + +def _rel(path: Path, root: Path) -> str: + try: + return str(path.relative_to(root)) + except ValueError: + return str(path) diff --git a/src/pacta/claims.py b/src/pacta/claims.py new file mode 100644 index 0000000..406fe5c --- /dev/null +++ b/src/pacta/claims.py @@ -0,0 +1,125 @@ +from __future__ import annotations + +from dataclasses import dataclass, field +from pathlib import Path +from typing import Any + +from .config import RepoConfig +from .lean import AxiomAuditResult, CertificateAxiomResult, LeanCheckResult, detect_tools +from .manifest import VerificationLayout +from .profiles import get_profile +from .repo import git_commit +from .risk import score_claim_card + + +@dataclass(slots=True) +class CertificateClaim: + name: str + status: str = "unknown" + axiom_status: str = "not_checked" + observed_axioms: list[str] = field(default_factory=list) + expected_axioms: list[str] = field(default_factory=list) + + def to_dict(self) -> dict[str, Any]: + return { + "name": self.name, + "status": self.status, + "axiom_status": self.axiom_status, + "observed_axioms": self.observed_axioms, + "expected_axioms": self.expected_axioms, + } + + +def build_claim_card( + repo: RepoConfig, + local_path: str | Path, + layout: VerificationLayout | None = None, + lean_check: LeanCheckResult | None = None, + axiom_audit: AxiomAuditResult | None = None, + offline_fixture: bool = False, +) -> dict[str, Any]: + path = Path(local_path) + profile = get_profile(repo.kind, repo) + tools = detect_tools() + certs = _certificate_claims(repo, axiom_audit, offline_fixture) + scanned_files = layout.relative_files() if layout else [] + card: dict[str, Any] = { + "component": repo.name, + "repo_url": repo.url, + "local_path": str(path), + "repo_commit": None if offline_fixture else git_commit(path), + "verification_dir": repo.verification_dir, + "kind": repo.kind, + "verified_backend": repo.verified_backend, + "certificates": [cert.to_dict() for cert in certs], + "guarantees": profile.guarantees, + "preconditions": profile.preconditions, + "exclusions": profile.exclusions, + "trusted_base": profile.trusted_base, + "evidence": { + "lean_version": tools.lean_version, + "lake_version": tools.lake_version, + "check_log_path": lean_check.log_path if lean_check else None, + "axiom_log_path": axiom_audit.log_path if axiom_audit else None, + "scanned_files": scanned_files, + }, + "risk": { + "level": "R0", + "rationale": "Not scored yet.", + "blockers": [], + "deployment_constraints": profile.deployment_constraints, + }, + "meta": { + "profile_axiom_imports": profile.axiom_imports, + "what_would_invalidate_this_evidence": profile.invalidation_conditions, + "next_proof_milestones": profile.next_milestones, + }, + } + assessment = score_claim_card(card) + card["risk"] = assessment.to_dict() + return card + + +def _certificate_claims( + repo: RepoConfig, + axiom_audit: AxiomAuditResult | None, + offline_fixture: bool, +) -> list[CertificateClaim]: + profile = get_profile(repo.kind, repo) + names = repo.certificates or profile.default_certificates + if axiom_audit: + by_name = {cert.name: cert for cert in axiom_audit.certificates} + return [_from_axiom_result(name, by_name.get(name), profile.expected_axioms) for name in names] + if offline_fixture: + return [ + CertificateClaim( + name=name, + status="proven", + axiom_status="clean", + observed_axioms=list(profile.expected_axioms), + expected_axioms=list(profile.expected_axioms), + ) + for name in names + ] + return [ + CertificateClaim( + name=name, + status="unknown", + axiom_status="not_checked", + observed_axioms=[], + expected_axioms=list(profile.expected_axioms), + ) + for name in names + ] + + +def _from_axiom_result(name: str, result: CertificateAxiomResult | None, expected_axioms: list[str]) -> CertificateClaim: + if not result: + return CertificateClaim(name=name, status="missing", axiom_status="not_checked", expected_axioms=expected_axioms) + return CertificateClaim( + name=name, + status=result.status, + axiom_status=result.axiom_status, + observed_axioms=result.observed_axioms, + expected_axioms=result.expected_axioms, + ) diff --git a/src/pacta/cli.py b/src/pacta/cli.py new file mode 100644 index 0000000..4abf87b --- /dev/null +++ b/src/pacta/cli.py @@ -0,0 +1,248 @@ +from __future__ import annotations + +import argparse +import sys +from pathlib import Path +from typing import Any + +from .audit import scan_hygiene +from .claims import build_claim_card +from .config import RepoConfig, load_config +from .lean import LeanCheckResult, lean_check_files, run_axiom_audit +from .manifest import discover_layout +from .profiles import get_profile +from .repo import clone_or_fetch, status_for +from .report import render_markdown +from .risk import score_claim_card +from .yamlio import dump_data, load_data + + +def main(argv: list[str] | None = None) -> int: + parser = build_parser() + args = parser.parse_args(argv) + try: + return args.func(args) + except (KeyError, ValueError, RuntimeError) as exc: + print(f"error: {exc}", file=sys.stderr) + return 2 + + +def build_parser() -> argparse.ArgumentParser: + parser = argparse.ArgumentParser(prog="pacta", description="Proof-aware crypto tooling evidence interpreter.") + sub = parser.add_subparsers(dest="command", required=True) + + scan = sub.add_parser("scan", help="Show configured repositories and local verification status.") + scan.add_argument("--config", required=True) + scan.add_argument("--base-dir", default="repos") + scan.add_argument("--clone", action="store_true", help="Clone missing repositories.") + scan.add_argument("--fetch", action="store_true", help="Fetch existing repositories when cloning/scanning.") + scan.set_defaults(func=cmd_scan) + + audit = sub.add_parser("audit", help="Run static proof hygiene checks.") + audit.add_argument("--repo", required=True) + audit.add_argument("--verification-dir", default="verification") + audit.add_argument("--config") + audit.add_argument("--repo-name") + audit.set_defaults(func=cmd_audit) + + lean_check = sub.add_parser("lean-check", help="Replay portable Lean checks without Linux-only scripts.") + lean_check.add_argument("--repo", required=True) + lean_check.add_argument("--verification-dir", default="verification") + lean_check.add_argument("--timeout", type=int, default=120) + lean_check.add_argument("--log-dir", default=".pacta") + lean_check.set_defaults(func=cmd_lean_check) + + check = sub.add_parser("check", help="Alias for lean-check.") + check.add_argument("--repo", required=True) + check.add_argument("--verification-dir", default="verification") + check.add_argument("--timeout", type=int, default=120) + check.add_argument("--log-dir", default=".pacta") + check.set_defaults(func=cmd_lean_check) + + axiom = sub.add_parser("axioms", help="Run configured Lean axiom audit.") + axiom.add_argument("--repo", required=True) + axiom.add_argument("--config", required=True) + axiom.add_argument("--repo-name", required=True) + axiom.add_argument("--timeout", type=int, default=120) + axiom.add_argument("--log-dir", default=".pacta") + axiom.set_defaults(func=cmd_axioms) + + claims = sub.add_parser("claims", help="Generate a machine-readable claim card.") + claims.add_argument("--config", required=True) + claims.add_argument("--repo-name", required=True) + claims.add_argument("--repo") + claims.add_argument("--base-dir", default="repos") + claims.add_argument("--out") + claims.add_argument("--offline-fixture", action="store_true") + claims.add_argument("--run-axioms", action="store_true") + claims.add_argument("--timeout", type=int, default=120) + claims.add_argument("--log-dir", default=".pacta") + claims.set_defaults(func=cmd_claims) + + report = sub.add_parser("report", help="Generate a human-readable Markdown risk report.") + report.add_argument("--claims") + report.add_argument("--config") + report.add_argument("--repo-name") + report.add_argument("--repo") + report.add_argument("--base-dir", default="repos") + report.add_argument("--offline-fixture", action="store_true") + report.add_argument("--out") + report.set_defaults(func=cmd_report) + + score = sub.add_parser("score", help="Score an existing claim card.") + score.add_argument("--claims", required=True) + score.set_defaults(func=cmd_score) + return parser + + +def cmd_scan(args: argparse.Namespace) -> int: + config = load_config(args.config) + for repo in config.repos: + status = clone_or_fetch(repo, args.base_dir, fetch=args.fetch) if args.clone else status_for(repo, args.base_dir) + marker = "ok" if status.verification_exists else "missing" + print(f"{repo.name}: {marker}") + print(f" url: {repo.url}") + print(f" local: {status.local_path}") + print(f" verification: {status.verification_dir}") + print(f" commit: {status.commit or 'unknown'}") + return 0 + + +def cmd_audit(args: argparse.Namespace) -> int: + repo_config = _repo_from_optional_config(args.config, args.repo_name, args.verification_dir) + layout = discover_layout(args.repo, repo_config.verification_dir) + issues = scan_hygiene(layout, repo_config.certificates) + for warning in layout.warnings: + print(f"warning: {warning}") + if not issues: + print("No proof hygiene issues found.") + return 0 + for issue in issues: + print(f"{issue.severity}: {issue.path}:{issue.line}: {issue.code}: {issue.message}") + return 1 if any(issue.severity == "error" for issue in issues) else 0 + + +def cmd_lean_check(args: argparse.Namespace) -> int: + layout = discover_layout(args.repo, args.verification_dir) + for warning in layout.warnings: + print(f"warning: {warning}") + result = lean_check_files(layout.compile_order, layout.verification_dir, timeout=args.timeout, log_dir=args.log_dir) + if not result.attempted: + print("; ".join(result.diagnostics)) + return 2 + print(f"checked: {len(result.checked_files)}") + print(f"failed: {len(result.failed_files)}") + print(f"log: {result.log_path}") + return 0 if result.ok else 1 + + +def cmd_axioms(args: argparse.Namespace) -> int: + config = load_config(args.config) + repo = config.repo_named(args.repo_name) + profile = get_profile(repo.kind, repo) + layout = discover_layout(args.repo, repo.verification_dir) + check_result = lean_check_files(layout.compile_order, layout.verification_dir, timeout=args.timeout, log_dir=args.log_dir) + if check_result.log_path: + print(f"check log: {check_result.log_path}") + if not check_result.attempted: + print("; ".join(check_result.diagnostics)) + return 2 + if not check_result.ok: + print(f"portable Lean replay failed for {len(check_result.failed_files)} file(s)") + result = run_axiom_audit( + Path(args.repo) / repo.verification_dir, + profile.axiom_imports, + repo.certificates or profile.default_certificates, + profile.expected_axioms, + timeout=args.timeout, + log_dir=args.log_dir, + ) + for cert in result.certificates: + print(f"{cert.name}: {cert.status}, axioms={cert.axiom_status}, observed={cert.observed_axioms}") + if result.log_path: + print(f"log: {result.log_path}") + return 0 if result.ok and check_result.ok else 1 + + +def cmd_claims(args: argparse.Namespace) -> int: + config = load_config(args.config) + repo = config.repo_named(args.repo_name) + local_path = Path(args.repo) if args.repo else status_for(repo, args.base_dir).local_path + layout = None + axiom_result = None + check_result: LeanCheckResult | None = None + if local_path.exists(): + layout = discover_layout(local_path, repo.verification_dir) + elif not args.offline_fixture: + raise ValueError(f"Local repo does not exist: {local_path}") + if args.run_axioms: + if layout is None: + raise ValueError("--run-axioms requires a local repository") + profile = get_profile(repo.kind, repo) + check_result = lean_check_files(layout.compile_order, layout.verification_dir, timeout=args.timeout, log_dir=args.log_dir) + axiom_result = run_axiom_audit( + local_path / repo.verification_dir, + profile.axiom_imports, + repo.certificates or profile.default_certificates, + profile.expected_axioms, + timeout=args.timeout, + log_dir=args.log_dir, + ) + card = build_claim_card( + repo, + local_path, + layout=layout, + lean_check=check_result, + axiom_audit=axiom_result, + offline_fixture=args.offline_fixture, + ) + if args.out: + dump_data(card, args.out) + else: + print_yaml(card) + return 0 + + +def cmd_report(args: argparse.Namespace) -> int: + if args.claims: + card = load_data(args.claims) + else: + if not args.config or not args.repo_name: + raise ValueError("report requires --claims or both --config and --repo-name") + config = load_config(args.config) + repo = config.repo_named(args.repo_name) + local_path = Path(args.repo) if args.repo else status_for(repo, args.base_dir).local_path + layout = discover_layout(local_path, repo.verification_dir) if local_path.exists() else None + card = build_claim_card(repo, local_path, layout=layout, offline_fixture=args.offline_fixture) + markdown = render_markdown(card) + if args.out: + Path(args.out).write_text(markdown, encoding="utf-8") + else: + print(markdown) + return 0 + + +def cmd_score(args: argparse.Namespace) -> int: + card = load_data(args.claims) + assessment = score_claim_card(card) + print(f"{assessment.level}: {assessment.rationale}") + if assessment.blockers: + print("blockers:") + for blocker in assessment.blockers: + print(f" - {blocker}") + return 0 + + +def _repo_from_optional_config(config_path: str | None, repo_name: str | None, verification_dir: str) -> RepoConfig: + if config_path: + config = load_config(config_path) + if not repo_name: + raise ValueError("--repo-name is required with --config") + return config.repo_named(repo_name) + return RepoConfig(name=Path(".").resolve().name, verification_dir=verification_dir) + + +def print_yaml(data: dict[str, Any]) -> None: + from .yamlio import dumps + + print(dumps(data), end="") diff --git a/src/pacta/config.py b/src/pacta/config.py new file mode 100644 index 0000000..328e205 --- /dev/null +++ b/src/pacta/config.py @@ -0,0 +1,65 @@ +from __future__ import annotations + +from dataclasses import dataclass, field +from pathlib import Path +from typing import Any + +from .yamlio import load_data + + +STANDARD_LEAN_AXIOMS = ["propext", "Classical.choice", "Quot.sound"] + + +@dataclass(slots=True) +class RepoConfig: + name: str + url: str | None = None + kind: str = "unknown" + verification_dir: str = "verification" + verified_backend: str | None = None + backend_warning: str | None = None + known_status: str | None = None + certificates: list[str] = field(default_factory=list) + expected_axioms: list[str] = field(default_factory=lambda: STANDARD_LEAN_AXIOMS.copy()) + known_exclusions: list[str] = field(default_factory=list) + axiom_imports: list[str] = field(default_factory=list) + + @classmethod + def from_dict(cls, raw: dict[str, Any]) -> "RepoConfig": + if "name" not in raw: + raise ValueError("Repo config is missing required field 'name'") + return cls( + name=str(raw["name"]), + url=raw.get("url"), + kind=str(raw.get("kind", "unknown")), + verification_dir=str(raw.get("verification_dir", "verification")), + verified_backend=raw.get("verified_backend"), + backend_warning=raw.get("backend_warning"), + known_status=raw.get("known_status"), + certificates=list(raw.get("certificates") or []), + expected_axioms=list(raw.get("expected_axioms") or STANDARD_LEAN_AXIOMS), + known_exclusions=list(raw.get("known_exclusions") or []), + axiom_imports=list(raw.get("axiom_imports") or []), + ) + + +@dataclass(slots=True) +class AppConfig: + repos: list[RepoConfig] + + def repo_named(self, name: str) -> RepoConfig: + for repo in self.repos: + if repo.name == name: + return repo + available = ", ".join(repo.name for repo in self.repos) + raise KeyError(f"Unknown repo '{name}'. Available: {available}") + + +def load_config(path: str | Path) -> AppConfig: + raw = load_data(path) + if not isinstance(raw, dict) or "repos" not in raw: + raise ValueError(f"{path} must contain a top-level 'repos' list") + repos = raw["repos"] + if not isinstance(repos, list): + raise ValueError("'repos' must be a list") + return AppConfig(repos=[RepoConfig.from_dict(item) for item in repos]) diff --git a/src/pacta/lean.py b/src/pacta/lean.py new file mode 100644 index 0000000..37f5b1f --- /dev/null +++ b/src/pacta/lean.py @@ -0,0 +1,273 @@ +from __future__ import annotations + +import os +import re +import shutil +import subprocess +import tempfile +from dataclasses import dataclass, field +from pathlib import Path + +from .config import STANDARD_LEAN_AXIOMS + + +@dataclass(slots=True) +class LeanTools: + lean: str | None + lake: str | None + lean_version: str | None = None + lake_version: str | None = None + + +@dataclass(slots=True) +class LeanCheckResult: + attempted: bool + ok: bool + missing_tool: str | None + checked_files: list[str] = field(default_factory=list) + failed_files: list[str] = field(default_factory=list) + log_path: str | None = None + diagnostics: list[str] = field(default_factory=list) + + +@dataclass(slots=True) +class CertificateAxiomResult: + name: str + status: str + axiom_status: str + observed_axioms: list[str] + expected_axioms: list[str] + diagnostics: list[str] = field(default_factory=list) + + +@dataclass(slots=True) +class AxiomAuditResult: + attempted: bool + ok: bool + missing_tool: str | None + certificates: list[CertificateAxiomResult] + log_path: str | None + diagnostics: list[str] = field(default_factory=list) + + +def detect_tools() -> LeanTools: + lean = shutil.which("lean") + lake = shutil.which("lake") + return LeanTools( + lean=lean, + lake=lake, + lean_version=_version([lean, "--version"]) if lean else None, + lake_version=_version([lake, "--version"]) if lake else None, + ) + + +def build_lean_env(verification_dir: str | Path, base_env: dict[str, str] | None = None) -> dict[str, str]: + verification = Path(verification_dir).resolve() + env = dict(base_env or os.environ) + candidates = [verification / "gen", verification] + existing = [str(path) for path in candidates if path.exists()] + old = env.get("LEAN_PATH") + if old: + existing.append(old) + if existing: + env["LEAN_PATH"] = os.pathsep.join(existing) + return env + + +def build_lean_invocation( + file_path: str | Path, + tools: LeanTools, + use_lake_env: bool = False, + output_path: str | Path | None = None, +) -> list[str]: + args = ["lean"] + if output_path is not None: + args.extend(["-o", str(output_path)]) + args.append(str(file_path)) + if use_lake_env and tools.lake: + return [tools.lake, "env", *args] + if not tools.lean: + raise RuntimeError("lean is not available") + return [tools.lean, *args[1:]] + + +def lean_check_files( + files: list[Path], + verification_dir: str | Path, + timeout: int = 120, + log_dir: str | Path | None = None, +) -> LeanCheckResult: + if not files: + return LeanCheckResult( + attempted=False, + ok=False, + missing_tool=None, + diagnostics=[f"No Lean files discovered under {verification_dir}."], + ) + tools = detect_tools() + if not tools.lean and not tools.lake: + return LeanCheckResult( + attempted=False, + ok=False, + missing_tool="lean", + diagnostics=["Neither lean nor lake was found on PATH."], + ) + verification = Path(verification_dir) + use_lake_env = tools.lake is not None and ((verification / "lakefile.lean").exists() or (verification / "lakefile.toml").exists()) + logs = _log_file(log_dir, "lean-check.log") + checked: list[str] = [] + failed: list[str] = [] + diagnostics: list[str] = [] + env = build_lean_env(verification) + with logs.open("w", encoding="utf-8") as log: + log.write(f"lean: {tools.lean}\n") + log.write(f"lake: {tools.lake}\n") + log.write(f"lean_version: {tools.lean_version}\n") + log.write(f"lake_version: {tools.lake_version}\n\n") + for path in files: + cmd = build_lean_invocation(path, tools, use_lake_env=use_lake_env, output_path=path.with_suffix(".olean")) + log.write("$ " + " ".join(cmd) + "\n") + try: + completed = subprocess.run( + cmd, + check=False, + capture_output=True, + text=True, + timeout=timeout, + cwd=str(verification), + env=env, + ) + except subprocess.TimeoutExpired: + failed.append(str(path)) + message = f"Timed out after {timeout}s: {path}" + diagnostics.append(message) + log.write(message + "\n") + continue + checked.append(str(path)) + log.write(completed.stdout) + log.write(completed.stderr) + log.write(f"\nexit_code: {completed.returncode}\n\n") + if completed.returncode != 0: + failed.append(str(path)) + return LeanCheckResult( + attempted=True, + ok=not failed, + missing_tool=None, + checked_files=checked, + failed_files=failed, + log_path=str(logs), + diagnostics=diagnostics, + ) + + +def run_axiom_audit( + verification_dir: str | Path, + imports: list[str], + certificates: list[str], + expected_axioms: list[str] | None = None, + timeout: int = 120, + log_dir: str | Path | None = None, +) -> AxiomAuditResult: + tools = detect_tools() + expected = expected_axioms or STANDARD_LEAN_AXIOMS + if not tools.lean and not tools.lake: + cert_results = [ + CertificateAxiomResult(cert, "unknown", "not_checked", [], expected, ["Neither lean nor lake was found on PATH."]) + for cert in certificates + ] + return AxiomAuditResult(False, False, "lean", cert_results, None, ["Neither lean nor lake was found on PATH."]) + verification = Path(verification_dir) + use_lake_env = tools.lake is not None and ((verification / "lakefile.lean").exists() or (verification / "lakefile.toml").exists()) + logs = _log_file(log_dir, "axiom-audit.log") + imports_text = "\n".join(f"import {module}" for module in imports) + prints_text = "\n".join(f"#print axioms {cert}" for cert in certificates) + env = build_lean_env(verification) + with tempfile.TemporaryDirectory(prefix="pacta-axioms-") as tmp: + audit_file = Path(tmp) / "AxiomAudit.lean" + audit_file.write_text(f"{imports_text}\n\n{prints_text}\n", encoding="utf-8") + cmd = build_lean_invocation(audit_file, tools, use_lake_env=use_lake_env) + try: + completed = subprocess.run( + cmd, + check=False, + capture_output=True, + text=True, + timeout=timeout, + cwd=str(verification), + env=env, + ) + output = completed.stdout + completed.stderr + return_code = completed.returncode + except subprocess.TimeoutExpired as exc: + output = (exc.stdout or "") + (exc.stderr or "") + f"\nTimed out after {timeout}s\n" + return_code = 124 + logs.write_text(output, encoding="utf-8") + parsed = parse_axiom_output(output, certificates) + cert_results: list[CertificateAxiomResult] = [] + for cert in certificates: + observed = parsed.get(cert, []) + if return_code != 0 and not observed: + status = "failed" + axiom_status = "not_checked" + else: + status = "proven" if observed or _mentions_no_axioms(output) else "unknown" + axiom_status = "clean" if sorted(observed) == sorted(expected) else "dirty" + cert_results.append(CertificateAxiomResult(cert, status, axiom_status, observed, expected)) + return AxiomAuditResult( + attempted=True, + ok=return_code == 0 and all(cert.axiom_status == "clean" for cert in cert_results), + missing_tool=None, + certificates=cert_results, + log_path=str(logs), + diagnostics=[] if return_code == 0 else [f"Lean axiom audit exited with {return_code}."], + ) + + +def parse_axiom_output(output: str, certificates: list[str]) -> dict[str, list[str]]: + results: dict[str, list[str]] = {} + lines = output.splitlines() + for cert in certificates: + cert_results: list[str] | None = None + for i, line in enumerate(lines): + if cert not in line: + continue + window = "\n".join(lines[i : i + 4]) + bracket = re.search(r"\[([^\]]*)\]", window) + if bracket: + cert_results = [item.strip() for item in bracket.group(1).split(",") if item.strip()] + break + if _mentions_no_axioms(window): + cert_results = [] + break + if cert_results is not None: + results[cert] = cert_results + return results + + +def _mentions_no_axioms(text: str) -> bool: + lowered = text.lower() + return "no axioms" in lowered or "does not depend on any axioms" in lowered + + +def _version(cmd: list[str | None]) -> str | None: + if not cmd[0]: + return None + try: + completed = subprocess.run( + [part for part in cmd if part], + check=False, + capture_output=True, + text=True, + timeout=10, + ) + except (OSError, subprocess.TimeoutExpired): + return None + if completed.returncode != 0: + return None + return (completed.stdout or completed.stderr).strip() or None + + +def _log_file(log_dir: str | Path | None, name: str) -> Path: + directory = Path(log_dir) if log_dir else Path(".pacta") + directory.mkdir(parents=True, exist_ok=True) + return directory / name diff --git a/src/pacta/manifest.py b/src/pacta/manifest.py new file mode 100644 index 0000000..7a15d0f --- /dev/null +++ b/src/pacta/manifest.py @@ -0,0 +1,118 @@ +from __future__ import annotations + +import re +from dataclasses import dataclass, field +from pathlib import Path + + +IMPORT_RE = re.compile(r"^\s*import\s+([A-Za-z0-9_'.]+)", re.MULTILINE) + + +@dataclass(slots=True) +class VerificationLayout: + repo_path: Path + verification_dir: Path + lean_files: list[Path] + manifest_files: list[Path] = field(default_factory=list) + manifest_entries: list[Path] = field(default_factory=list) + compile_order: list[Path] = field(default_factory=list) + files_not_in_manifest: list[Path] = field(default_factory=list) + warnings: list[str] = field(default_factory=list) + + def relative_files(self) -> list[str]: + return [str(path.relative_to(self.verification_dir)) for path in self.lean_files] + + +def discover_layout(repo_path: str | Path, verification_dir_name: str = "verification") -> VerificationLayout: + repo = Path(repo_path).resolve() + verification_dir = repo / verification_dir_name + lean_files = sorted(verification_dir.rglob("*.lean")) if verification_dir.exists() else [] + manifest_files, manifest_entries = _read_manifest_entries(verification_dir) + warnings: list[str] = [] + files_not_in_manifest: list[Path] = [] + if manifest_files: + manifest_set = {path.resolve() for path in manifest_entries if path.exists()} + files_not_in_manifest = [path for path in lean_files if path.resolve() not in manifest_set] + if not verification_dir.exists(): + warnings.append(f"Verification directory does not exist: {verification_dir}") + compile_order, topo_warnings = topological_order(lean_files, verification_dir) + warnings.extend(topo_warnings) + return VerificationLayout( + repo_path=repo, + verification_dir=verification_dir, + lean_files=lean_files, + manifest_files=manifest_files, + manifest_entries=manifest_entries, + compile_order=compile_order, + files_not_in_manifest=files_not_in_manifest, + warnings=warnings, + ) + + +def parse_imports(path: str | Path) -> list[str]: + try: + text = Path(path).read_text(encoding="utf-8") + except UnicodeDecodeError: + text = Path(path).read_text(encoding="utf-8", errors="ignore") + return IMPORT_RE.findall(text) + + +def topological_order(files: list[Path], verification_dir: Path) -> tuple[list[Path], list[str]]: + module_map = _module_aliases(files, verification_dir) + dependencies: dict[Path, set[Path]] = {path: set() for path in files} + for path in files: + for module in parse_imports(path): + dep = module_map.get(module) + if dep and dep != path: + dependencies[path].add(dep) + ordered: list[Path] = [] + ready = sorted([path for path, deps in dependencies.items() if not deps]) + remaining = {path: set(deps) for path, deps in dependencies.items()} + while ready: + path = ready.pop(0) + if path not in remaining: + continue + ordered.append(path) + del remaining[path] + for other, deps in list(remaining.items()): + deps.discard(path) + if not deps and other not in ready: + ready.append(other) + ready.sort() + if remaining: + return sorted(files), ["Local Lean import cycle or unresolved topo state; falling back to lexical order."] + return ordered, [] + + +def _module_aliases(files: list[Path], verification_dir: Path) -> dict[str, Path]: + aliases: dict[str, Path] = {} + gen_dir = verification_dir / "gen" + for path in files: + for root in (verification_dir, gen_dir): + try: + rel = path.relative_to(root) + except ValueError: + continue + module = ".".join(rel.with_suffix("").parts) + aliases.setdefault(module, path) + return aliases + + +def _read_manifest_entries(verification_dir: Path) -> tuple[list[Path], list[Path]]: + if not verification_dir.exists(): + return [], [] + candidates: list[Path] = [] + for pattern in ("*manifest*", "*proofs*.txt", "*lean-files*.txt", "proofs.txt", "lean_files.txt"): + candidates.extend(sorted(verification_dir.glob(pattern))) + candidates = [path for path in dict.fromkeys(candidates) if path.is_file()] + entries: list[Path] = [] + for manifest in candidates: + for raw in manifest.read_text(encoding="utf-8", errors="ignore").splitlines(): + line = raw.split("#", 1)[0].split("--", 1)[0].strip().strip('"').strip("'") + if not line.endswith(".lean"): + continue + path = Path(line) + if not path.is_absolute(): + path = verification_dir / path + entries.append(path) + return candidates, entries diff --git a/src/pacta/profiles/__init__.py b/src/pacta/profiles/__init__.py new file mode 100644 index 0000000..43e77af --- /dev/null +++ b/src/pacta/profiles/__init__.py @@ -0,0 +1,65 @@ +from __future__ import annotations + +from dataclasses import dataclass, field + +from pacta.config import RepoConfig, STANDARD_LEAN_AXIOMS + + +@dataclass(slots=True) +class Profile: + kind: str + default_certificates: list[str] = field(default_factory=list) + axiom_imports: list[str] = field(default_factory=list) + guarantees: list[str] = field(default_factory=list) + preconditions: list[str] = field(default_factory=list) + exclusions: list[str] = field(default_factory=list) + trusted_base: list[str] = field(default_factory=list) + deployment_constraints: list[str] = field(default_factory=list) + invalidation_conditions: list[str] = field(default_factory=list) + next_milestones: list[str] = field(default_factory=list) + expected_axioms: list[str] = field(default_factory=lambda: STANDARD_LEAN_AXIOMS.copy()) + + def merge_repo(self, repo: RepoConfig) -> "Profile": + certs = repo.certificates or self.default_certificates + imports = repo.axiom_imports or self.axiom_imports + exclusions = [*self.exclusions, *repo.known_exclusions] + constraints = list(self.deployment_constraints) + if repo.backend_warning: + constraints.append(repo.backend_warning) + if repo.known_status: + constraints.append(repo.known_status) + return Profile( + kind=self.kind, + default_certificates=certs, + axiom_imports=imports, + guarantees=list(self.guarantees), + preconditions=list(self.preconditions), + exclusions=_dedupe(exclusions), + trusted_base=list(self.trusted_base), + deployment_constraints=_dedupe(constraints), + invalidation_conditions=list(self.invalidation_conditions), + next_milestones=list(self.next_milestones), + expected_axioms=repo.expected_axioms or self.expected_axioms, + ) + + +def get_profile(kind: str, repo: RepoConfig | None = None) -> Profile: + from .ed25519 import ED25519_PROFILE + from .pasta import PASTA_PALLAS_PROFILE + + profiles = { + "ed25519": ED25519_PROFILE, + "pasta_pallas": PASTA_PALLAS_PROFILE, + } + profile = profiles.get(kind, Profile(kind=kind)) + return profile.merge_repo(repo) if repo else profile + + +def _dedupe(values: list[str]) -> list[str]: + seen: set[str] = set() + out: list[str] = [] + for value in values: + if value and value not in seen: + seen.add(value) + out.append(value) + return out diff --git a/src/pacta/profiles/ed25519.py b/src/pacta/profiles/ed25519.py new file mode 100644 index 0000000..0d7b65b --- /dev/null +++ b/src/pacta/profiles/ed25519.py @@ -0,0 +1,68 @@ +from __future__ import annotations + +from . import Profile + + +ED25519_PROFILE = Profile( + kind="ed25519", + default_certificates=[ + "CurveFieldProofs.fieldImplementation", + "CurveFieldProofs.edwardsImplementation", + ], + axiom_imports=[ + "Proofs.FieldMain", + "Proofs.EdMain", + ], + guarantees=[ + "FieldElement51 arithmetic is checked through denotation over F_p, p = 2^255 - 19, for the configured backend.", + "Selected complete twisted Edwards point-operation laws are checked under ExtValid and OnCurveExt invariants.", + "Panic and overflow freedom evidence applies under the stated limb-bound preconditions.", + "Implementation laws are interpreted through the shipped Lean denotation layer.", + ], + preconditions=[ + "Use the exact pinned source or a reviewed diff against the checked artifact.", + "Use the configured serial/u64 backend unless another backend has its own certificate.", + "Maintain limb bounds and representation invariants required by the Lean statements.", + "Treat Charon/Aeneas translation faithfulness as trusted base, not as proven by this tool.", + ], + exclusions=[ + "Full EdDSA signature verification is not proven by field/Edwards certificates alone.", + "Complete Scalar52 arithmetic is out of scope unless separately certified.", + "SHA-512 is out of scope.", + "Encoding, decoding, and canonicality are out of scope unless separately certified.", + "Rust compiler correctness is out of scope.", + "Charon/Aeneas translation faithfulness is out of scope.", + "Side-channel resistance is out of scope.", + "SIMD, AVX, hardware, zkVM, accelerator, and syscall paths are out of scope unless separately certified.", + "Wallet policy, transaction construction, RPC, oracle, market, and LLM decision safety are out of scope.", + ], + trusted_base=[ + "Lean kernel and standard library axioms.", + "Lake/Lean package resolution used for replay.", + "Correctness of the shipped Rust-to-Lean translation artifact.", + "Correct mapping from the production code path to the checked backend.", + "Operating system, filesystem, and subprocess execution environment.", + ], + deployment_constraints=[ + "Use exact pinned source or reviewed diff.", + "Use verified serial/u64 backend only.", + "Disable accelerator, syscall, hardware, SIMD, and AVX paths unless separately certified.", + "Do not treat this as full EdDSA verification.", + "Keep key custody behind HSM, MPC, or policy firewall until signing-stack proof coverage improves.", + "Use ordinary tests and fuzzing at encoding, API, and transaction boundaries.", + ], + invalidation_conditions=[ + "The deployed code path differs from the checked backend or source revision.", + "A certificate stops compiling under the pinned Lean environment.", + "Unexpected axioms appear in certificate audits.", + "Required invariants are not enforced before calling verified routines.", + "The implementation uses accelerator or syscall paths not covered by the certificates.", + ], + next_milestones=[ + "Prove complete scalar arithmetic.", + "Prove encoding, decoding, canonicality, and rejection rules.", + "Connect field, scalar, hash, and point proofs into an EdDSA verification theorem.", + "Document a reproducible production build and code-path selection story.", + "Add side-channel and backend-specific assurance evidence.", + ], +) diff --git a/src/pacta/profiles/pasta.py b/src/pacta/profiles/pasta.py new file mode 100644 index 0000000..1db657f --- /dev/null +++ b/src/pacta/profiles/pasta.py @@ -0,0 +1,41 @@ +from __future__ import annotations + +from . import Profile + + +PASTA_PALLAS_PROFILE = Profile( + kind="pasta_pallas", + default_certificates=[], + axiom_imports=["Proofs.FieldMain"], + guarantees=[ + "Pallas/Pasta arithmetic evidence is treated as foundation work unless aggregate certificates prove otherwise.", + ], + preconditions=[ + "Require explicit add, mul, reduce, square, and invert proof coverage before production field claims.", + "Require an aggregate fieldImplementation-style theorem before classifying the field implementation as complete.", + ], + exclusions=[ + "Production-ready Pallas arithmetic is not assumed unless shipped certificates prove it.", + "Full protocol, wallet, proof-system, or transaction safety is out of scope.", + "Compiler correctness, translation faithfulness, and side-channel resistance are out of scope.", + ], + trusted_base=[ + "Lean kernel and standard library axioms.", + "Correctness of the shipped translation artifact.", + "Correct mapping from production code to checked routines.", + ], + deployment_constraints=[ + "Do not classify as a complete production field implementation without aggregate field coverage.", + "Keep downstream protocol use behind independent tests and audits.", + ], + invalidation_conditions=[ + "Expected operation certificates are absent or fail replay.", + "Unexpected axioms appear in certificate audits.", + "Production code uses routines not covered by the shipped proof artifact.", + ], + next_milestones=[ + "Prove add, mul, reduce, square, and invert.", + "Add an aggregate fieldImplementation-style theorem.", + "Map the checked theorem boundary to public production APIs.", + ], +) diff --git a/src/pacta/repo.py b/src/pacta/repo.py new file mode 100644 index 0000000..62c9295 --- /dev/null +++ b/src/pacta/repo.py @@ -0,0 +1,72 @@ +from __future__ import annotations + +import shutil +import subprocess +from dataclasses import dataclass +from pathlib import Path + +from .config import RepoConfig + + +@dataclass(slots=True) +class RepoStatus: + name: str + url: str | None + local_path: Path + exists: bool + verification_dir: Path + verification_exists: bool + commit: str | None + + +def configured_local_path(repo: RepoConfig, base_dir: str | Path = "repos") -> Path: + return Path(base_dir) / repo.name + + +def status_for(repo: RepoConfig, base_dir: str | Path = "repos", explicit_path: str | Path | None = None) -> RepoStatus: + local_path = Path(explicit_path) if explicit_path else configured_local_path(repo, base_dir) + verification_dir = local_path / repo.verification_dir + return RepoStatus( + name=repo.name, + url=repo.url, + local_path=local_path, + exists=local_path.exists(), + verification_dir=verification_dir, + verification_exists=verification_dir.exists(), + commit=git_commit(local_path) if local_path.exists() else None, + ) + + +def git_commit(path: str | Path) -> str | None: + git = shutil.which("git") + if not git: + return None + try: + completed = subprocess.run( + [git, "-C", str(path), "rev-parse", "HEAD"], + check=False, + capture_output=True, + text=True, + timeout=10, + ) + except (OSError, subprocess.TimeoutExpired): + return None + if completed.returncode != 0: + return None + return completed.stdout.strip() or None + + +def clone_or_fetch(repo: RepoConfig, base_dir: str | Path = "repos", fetch: bool = False) -> RepoStatus: + if not repo.url: + raise ValueError(f"{repo.name} has no URL") + git = shutil.which("git") + if not git: + raise RuntimeError("git is not available") + local_path = configured_local_path(repo, base_dir) + local_path.parent.mkdir(parents=True, exist_ok=True) + if local_path.exists(): + if fetch: + subprocess.run([git, "-C", str(local_path), "fetch", "--all", "--prune"], check=True, timeout=120) + else: + subprocess.run([git, "clone", repo.url, str(local_path)], check=True, timeout=300) + return status_for(repo, base_dir) diff --git a/src/pacta/report.py b/src/pacta/report.py new file mode 100644 index 0000000..d6776f2 --- /dev/null +++ b/src/pacta/report.py @@ -0,0 +1,87 @@ +from __future__ import annotations + +from typing import Any + + +def render_markdown(card: dict[str, Any]) -> str: + risk = card.get("risk", {}) + evidence = card.get("evidence", {}) + meta = card.get("meta", {}) + lines: list[str] = [ + f"# {card.get('component', 'Component')}", + "", + "## Verdict", + "", + f"Residual risk: **{risk.get('level', 'R0')}**", + "", + str(risk.get("rationale", "No rationale recorded.")), + "", + "## Proven claims", + "", + *_bullets(card.get("guarantees") or ["No proven claims recorded."]), + "", + "## Preconditions", + "", + *_bullets(card.get("preconditions") or ["No preconditions recorded."]), + "", + "## Exclusions", + "", + *_bullets(card.get("exclusions") or ["No exclusions recorded."]), + "", + "## Trusted base", + "", + *_bullets(card.get("trusted_base") or ["No trusted base recorded."]), + "", + "## Axiom audit", + "", + ] + for cert in card.get("certificates") or []: + observed = ", ".join(cert.get("observed_axioms") or []) + expected = ", ".join(cert.get("expected_axioms") or []) + lines.extend( + [ + f"- `{cert.get('name')}`: status `{cert.get('status')}`, axioms `{cert.get('axiom_status')}`", + f" Observed: {observed or 'none recorded'}", + f" Expected: {expected or 'none recorded'}", + ] + ) + lines.extend( + [ + "", + "## macOS replay status", + "", + f"- Lean: {evidence.get('lean_version') or 'not detected'}", + f"- Lake: {evidence.get('lake_version') or 'not detected'}", + f"- Check log: {evidence.get('check_log_path') or 'not recorded'}", + f"- Axiom log: {evidence.get('axiom_log_path') or 'not recorded'}", + "", + "## Deployment constraints", + "", + *_bullets(risk.get("deployment_constraints") or ["No deployment constraints recorded."]), + "", + "## Residual risk score", + "", + f"`{risk.get('level', 'R0')}`", + "", + ] + ) + blockers = risk.get("blockers") or [] + if blockers: + lines.extend(["Blockers:", "", *_bullets(blockers), ""]) + lines.extend( + [ + "## What would invalidate this evidence", + "", + *_bullets(meta.get("what_would_invalidate_this_evidence") or ["No invalidation conditions recorded."]), + "", + "## Next proof milestones", + "", + *_bullets(meta.get("next_proof_milestones") or ["No proof milestones recorded."]), + "", + ] + ) + return "\n".join(lines) + + +def _bullets(values: list[str]) -> list[str]: + return [f"- {value}" for value in values] diff --git a/src/pacta/risk.py b/src/pacta/risk.py new file mode 100644 index 0000000..71e5c75 --- /dev/null +++ b/src/pacta/risk.py @@ -0,0 +1,105 @@ +from __future__ import annotations + +from dataclasses import dataclass, field +from typing import Any + + +RISK_ORDER = ["R0", "R1", "R2", "R3", "R4", "R5"] + + +@dataclass(slots=True) +class RiskAssessment: + level: str + rationale: str + blockers: list[str] = field(default_factory=list) + deployment_constraints: list[str] = field(default_factory=list) + + def to_dict(self) -> dict[str, Any]: + return { + "level": self.level, + "rationale": self.rationale, + "blockers": self.blockers, + "deployment_constraints": self.deployment_constraints, + } + + +def score_claim_card(card: dict[str, Any]) -> RiskAssessment: + kind = card.get("kind", "unknown") + certificates = card.get("certificates") or [] + exclusions = [str(item).lower() for item in card.get("exclusions") or []] + constraints = list(card.get("risk", {}).get("deployment_constraints") or []) + blockers: list[str] = [] + + clean_proven = [ + cert + for cert in certificates + if cert.get("status") == "proven" and cert.get("axiom_status") == "clean" + ] + failed = [cert.get("name", "") for cert in certificates if cert.get("status") == "failed"] + dirty = [cert.get("name", "") for cert in certificates if cert.get("axiom_status") == "dirty"] + missing = [cert.get("name", "") for cert in certificates if cert.get("status") in {"missing", "unknown"}] + + if failed: + blockers.append("Certificate replay failed: " + ", ".join(failed)) + if dirty: + blockers.append("Unexpected axioms observed: " + ", ".join(dirty)) + + if not certificates: + if kind == "pasta_pallas": + return RiskAssessment( + "R2", + "Pallas/Pasta foundation profile exists, but no aggregate production field certificate was configured.", + ["No configured aggregate certificate."], + constraints, + ) + return RiskAssessment("R0", "No formal certificates were configured or observed.", ["No usable evidence."], constraints) + + if not clean_proven: + level = "R2" if kind in {"ed25519", "pasta_pallas"} else "R0" + rationale = "Formal artifact boundary is known, but no configured certificate is both proven and axiom-clean." + blockers.extend(f"Missing or unchecked certificate: {name}" for name in missing) + return RiskAssessment(level, rationale, blockers or ["No axiom-clean certificate evidence."], constraints) + + if kind == "ed25519": + names = {cert.get("name") for cert in clean_proven} + required = {"CurveFieldProofs.fieldImplementation", "CurveFieldProofs.edwardsImplementation"} + if required.issubset(names): + blockers.extend(f"Missing or unchecked certificate: {name}" for name in missing) + full_eddsa_excluded = any("eddsa" in item or "signature" in item for item in exclusions) + if not full_eddsa_excluded: + blockers.append("Full EdDSA boundary is not explicitly excluded in the claim card.") + return RiskAssessment( + "R3", + "Configured field and Edwards implementation certificates are proven and axiom-clean for a specific lower-layer backend boundary.", + blockers, + constraints, + ) + blockers.append("Ed25519 profile requires both fieldImplementation and edwardsImplementation for the expected R3 arithmetic classification.") + return RiskAssessment("R2", "Some formal Ed25519 evidence is present, but the expected field plus Edwards certificate pair is incomplete.", blockers, constraints) + + if kind == "pasta_pallas": + aggregate = [ + cert + for cert in clean_proven + if "fieldImplementation" in str(cert.get("name")) or "field_implementation" in str(cert.get("name")) + ] + if aggregate: + return RiskAssessment( + "R3", + "An aggregate Pallas/Pasta field certificate is proven and axiom-clean, but end-to-end protocol coverage is still out of scope.", + blockers, + constraints, + ) + return RiskAssessment( + "R2", + "Pallas/Pasta formal evidence is present, but no aggregate production field implementation certificate was observed.", + blockers or ["No aggregate fieldImplementation-style certificate."], + constraints, + ) + + return RiskAssessment( + "R2", + "At least one certificate is proven and axiom-clean, but this component has no stronger profile-specific classification.", + blockers, + constraints, + ) diff --git a/src/pacta/yamlio.py b/src/pacta/yamlio.py new file mode 100644 index 0000000..6f99e83 --- /dev/null +++ b/src/pacta/yamlio.py @@ -0,0 +1,151 @@ +from __future__ import annotations + +import json +from pathlib import Path +from typing import Any + + +def load_data(path: str | Path) -> Any: + text = Path(path).read_text(encoding="utf-8") + return loads(text) + + +def dump_data(data: Any, path: str | Path) -> None: + Path(path).write_text(dumps(data), encoding="utf-8") + + +def loads(text: str) -> Any: + stripped = text.strip() + if not stripped: + return None + if stripped[0] in "[{": + return json.loads(stripped) + try: + import yaml # type: ignore + + return yaml.safe_load(text) + except ModuleNotFoundError: + return _SimpleYAML(text).parse() + + +def dumps(data: Any) -> str: + try: + import yaml # type: ignore + + return yaml.safe_dump(data, sort_keys=False) + except ModuleNotFoundError: + return json.dumps(data, indent=2, sort_keys=False) + "\n" + + +class _SimpleYAML: + """Small YAML subset reader for the repository examples. + + It supports indentation-based mappings, lists, strings, booleans, nulls, + integers, and JSON-style inline arrays/objects. It is not a general YAML + implementation; install PyYAML for broader syntax. + """ + + def __init__(self, text: str) -> None: + self.lines = [ + line.rstrip() + for line in text.splitlines() + if line.strip() and not line.lstrip().startswith("#") + ] + + def parse(self) -> Any: + if not self.lines: + return None + value, index = self._parse_block(0, self._indent(self.lines[0])) + if index != len(self.lines): + raise ValueError(f"Could not parse YAML near line {index + 1}") + return value + + def _parse_block(self, index: int, indent: int) -> tuple[Any, int]: + stripped = self.lines[index].lstrip() + if stripped.startswith("-"): + return self._parse_list(index, indent) + return self._parse_mapping(index, indent) + + def _parse_mapping(self, index: int, indent: int) -> tuple[dict[str, Any], int]: + result: dict[str, Any] = {} + while index < len(self.lines): + line = self.lines[index] + current = self._indent(line) + if current < indent: + break + if current > indent: + raise ValueError(f"Unexpected indentation near line {index + 1}") + stripped = line.lstrip() + if stripped.startswith("-"): + break + if ":" not in stripped: + raise ValueError(f"Expected key/value near line {index + 1}") + key, raw = stripped.split(":", 1) + key = key.strip() + raw = raw.strip() + if raw: + result[key] = self._scalar(raw) + index += 1 + else: + index += 1 + if index >= len(self.lines) or self._indent(self.lines[index]) <= current: + result[key] = {} + else: + result[key], index = self._parse_block(index, self._indent(self.lines[index])) + return result, index + + def _parse_list(self, index: int, indent: int) -> tuple[list[Any], int]: + result: list[Any] = [] + while index < len(self.lines): + line = self.lines[index] + current = self._indent(line) + if current < indent: + break + if current != indent: + raise ValueError(f"Unexpected list indentation near line {index + 1}") + stripped = line.lstrip() + if not stripped.startswith("-"): + break + item = stripped[1:].strip() + index += 1 + if not item: + if index < len(self.lines) and self._indent(self.lines[index]) > current: + value, index = self._parse_block(index, self._indent(self.lines[index])) + result.append(value) + else: + result.append(None) + continue + if ":" in item and not item.startswith(("http://", "https://")): + key, raw = item.split(":", 1) + obj: dict[str, Any] = {key.strip(): self._scalar(raw.strip()) if raw.strip() else {}} + if index < len(self.lines) and self._indent(self.lines[index]) > current: + more, index = self._parse_mapping(index, self._indent(self.lines[index])) + obj.update(more) + result.append(obj) + else: + result.append(self._scalar(item)) + return result, index + + @staticmethod + def _indent(line: str) -> int: + return len(line) - len(line.lstrip(" ")) + + @staticmethod + def _scalar(raw: str) -> Any: + if raw == "": + return "" + lowered = raw.lower() + if lowered in {"null", "none", "~"}: + return None + if lowered == "true": + return True + if lowered == "false": + return False + if raw.startswith(("[", "{")): + return json.loads(raw) + if (raw.startswith('"') and raw.endswith('"')) or (raw.startswith("'") and raw.endswith("'")): + return raw[1:-1] + try: + return int(raw) + except ValueError: + return raw diff --git a/tests/fixtures/mini-ed25519-verified/verification/Proofs/EdMain.lean b/tests/fixtures/mini-ed25519-verified/verification/Proofs/EdMain.lean new file mode 100644 index 0000000..ba82ea1 --- /dev/null +++ b/tests/fixtures/mini-ed25519-verified/verification/Proofs/EdMain.lean @@ -0,0 +1,7 @@ +import Proofs.FieldMain + +namespace CurveFieldProofs + +theorem edwardsImplementation : 2 = 2 := rfl + +end CurveFieldProofs diff --git a/tests/fixtures/mini-ed25519-verified/verification/Proofs/FieldMain.lean b/tests/fixtures/mini-ed25519-verified/verification/Proofs/FieldMain.lean new file mode 100644 index 0000000..1240ac4 --- /dev/null +++ b/tests/fixtures/mini-ed25519-verified/verification/Proofs/FieldMain.lean @@ -0,0 +1,5 @@ +namespace CurveFieldProofs + +theorem fieldImplementation : 1 = 1 := rfl + +end CurveFieldProofs diff --git a/tests/fixtures/mini-ed25519-verified/verification/proofs.txt b/tests/fixtures/mini-ed25519-verified/verification/proofs.txt new file mode 100644 index 0000000..0c0ef02 --- /dev/null +++ b/tests/fixtures/mini-ed25519-verified/verification/proofs.txt @@ -0,0 +1,2 @@ +Proofs/FieldMain.lean +Proofs/EdMain.lean diff --git a/tests/test_claim_cards.py b/tests/test_claim_cards.py new file mode 100644 index 0000000..5543ba5 --- /dev/null +++ b/tests/test_claim_cards.py @@ -0,0 +1,20 @@ +from pacta.claims import build_claim_card +from pacta.config import RepoConfig +from pacta.yamlio import dump_data, load_data + + +def test_offline_claim_card_serializes(tmp_path): + repo = RepoConfig( + name="dalek-ed25519-verified", + url="https://github.com/saymrwulf/dalek-ed25519-verified.git", + kind="ed25519", + verified_backend="serial/u64", + certificates=["CurveFieldProofs.fieldImplementation", "CurveFieldProofs.edwardsImplementation"], + ) + card = build_claim_card(repo, tmp_path / "missing", offline_fixture=True) + assert card["risk"]["level"] == "R3" + out = tmp_path / "claims.yaml" + dump_data(card, out) + loaded = load_data(out) + assert loaded["component"] == "dalek-ed25519-verified" + assert loaded["certificates"][0]["axiom_status"] == "clean" diff --git a/tests/test_hygiene.py b/tests/test_hygiene.py new file mode 100644 index 0000000..5036ee5 --- /dev/null +++ b/tests/test_hygiene.py @@ -0,0 +1,34 @@ +from pacta.audit import scan_hygiene, strip_lean_comments +from pacta.manifest import discover_layout + + +def test_strip_comments_avoids_false_sorry(): + text = "-- sorry\n/- axiom hidden -/\ntheorem ok : True := by trivial\n" + stripped = strip_lean_comments(text) + assert "sorry" not in stripped + assert "axiom hidden" not in stripped + assert "by trivial" in stripped + + +def test_hygiene_detects_forbidden_patterns(tmp_path): + proofs = tmp_path / "repo" / "verification" / "Proofs" + proofs.mkdir(parents=True) + (proofs / "SpecMain.lean").write_text( + """ +axiom bad : True +theorem empty : True := by trivial +theorem gap : Nat := by + sorry +def x := by native_decide +""", + encoding="utf-8", + ) + layout = discover_layout(tmp_path / "repo") + issues = scan_hygiene(layout, certificates=["Missing.cert"]) + codes = {issue.code for issue in issues} + assert "axiom-in-proofs" in codes + assert "trivial-true-target" in codes + assert "by-trivial-in-spec" in codes + assert "sorry" in codes + assert "native-decide" in codes + assert "missing-certificate-name" in codes diff --git a/tests/test_lean.py b/tests/test_lean.py new file mode 100644 index 0000000..4827084 --- /dev/null +++ b/tests/test_lean.py @@ -0,0 +1,48 @@ +from pathlib import Path + +from pacta.lean import LeanTools, build_lean_env, build_lean_invocation, lean_check_files, parse_axiom_output + + +def test_parse_axiom_output_multiline(): + output = """ +'CurveFieldProofs.fieldImplementation' depends on axioms: +[propext, Classical.choice, Quot.sound] +'CurveFieldProofs.edwardsImplementation' depends on axioms: +[propext, Classical.choice, Quot.sound] +""" + parsed = parse_axiom_output( + output, + ["CurveFieldProofs.fieldImplementation", "CurveFieldProofs.edwardsImplementation"], + ) + assert parsed["CurveFieldProofs.fieldImplementation"] == ["propext", "Classical.choice", "Quot.sound"] + assert parsed["CurveFieldProofs.edwardsImplementation"] == ["propext", "Classical.choice", "Quot.sound"] + + +def test_parse_axiom_output_no_axioms_wording(): + output = "'CurveFieldProofs.fieldImplementation' does not depend on any axioms\n" + parsed = parse_axiom_output(output, ["CurveFieldProofs.fieldImplementation"]) + assert parsed["CurveFieldProofs.fieldImplementation"] == [] + + +def test_mac_safe_lean_command_is_argument_list(): + tools = LeanTools(lean="/usr/local/bin/lean", lake=None) + cmd = build_lean_invocation(Path("Proofs/A.lean"), tools, output_path=Path("Proofs/A.olean")) + assert cmd == ["/usr/local/bin/lean", "-o", "Proofs/A.olean", "Proofs/A.lean"] + assert "timeout" not in cmd + assert "taskset" not in cmd + assert "free" not in cmd + + +def test_lean_path_uses_python_pathsep(tmp_path): + verification = tmp_path / "verification" + (verification / "gen").mkdir(parents=True) + env = build_lean_env(verification, base_env={"LEAN_PATH": "old"}) + assert str(verification / "gen") in env["LEAN_PATH"] + assert str(verification) in env["LEAN_PATH"] + assert "old" in env["LEAN_PATH"] + + +def test_lean_check_requires_discovered_files(tmp_path): + result = lean_check_files([], tmp_path / "verification") + assert not result.attempted + assert "No Lean files" in result.diagnostics[0] diff --git a/tests/test_manifest_parsing.py b/tests/test_manifest_parsing.py new file mode 100644 index 0000000..b6f71b2 --- /dev/null +++ b/tests/test_manifest_parsing.py @@ -0,0 +1,15 @@ +from pacta.manifest import discover_layout, parse_imports + + +def test_manifest_and_topological_order(tmp_path): + verification = tmp_path / "repo" / "verification" + proofs = verification / "Proofs" + proofs.mkdir(parents=True) + (proofs / "A.lean").write_text("import Proofs.B\n", encoding="utf-8") + (proofs / "B.lean").write_text("def b := 1\n", encoding="utf-8") + (verification / "proofs.txt").write_text("Proofs/A.lean\n", encoding="utf-8") + layout = discover_layout(tmp_path / "repo") + assert parse_imports(proofs / "A.lean") == ["Proofs.B"] + ordered = [path.name for path in layout.compile_order] + assert ordered.index("B.lean") < ordered.index("A.lean") + assert "Proofs/B.lean" in [str(path.relative_to(verification)) for path in layout.files_not_in_manifest] diff --git a/tests/test_profiles.py b/tests/test_profiles.py new file mode 100644 index 0000000..a2c44d8 --- /dev/null +++ b/tests/test_profiles.py @@ -0,0 +1,25 @@ +from pacta.config import RepoConfig, load_config +from pacta.profiles import get_profile + + +def test_ed25519_profile_defaults(): + profile = get_profile("ed25519") + assert "Proofs.FieldMain" in profile.axiom_imports + assert "CurveFieldProofs.fieldImplementation" in profile.default_certificates + assert any("Full EdDSA" in exclusion for exclusion in profile.exclusions) + + +def test_repo_config_merges_backend_warning(): + repo = RepoConfig( + name="risc0-ed25519-verified", + kind="ed25519", + backend_warning="pure Rust path only; do not treat zkVM accelerator/syscall path as verified", + ) + profile = get_profile("ed25519", repo) + assert any("zkVM" in item for item in profile.deployment_constraints) + + +def test_load_examples_config(): + config = load_config("examples/repos.yaml") + assert config.repo_named("dalek-ed25519-verified").kind == "ed25519" + assert config.repo_named("pasta-pallas-verified").kind == "pasta_pallas" diff --git a/tests/test_risk.py b/tests/test_risk.py new file mode 100644 index 0000000..3042f38 --- /dev/null +++ b/tests/test_risk.py @@ -0,0 +1,39 @@ +from pacta.risk import score_claim_card + + +def test_ed25519_field_and_edwards_clean_scores_r3(): + card = { + "kind": "ed25519", + "certificates": [ + {"name": "CurveFieldProofs.fieldImplementation", "status": "proven", "axiom_status": "clean"}, + {"name": "CurveFieldProofs.edwardsImplementation", "status": "proven", "axiom_status": "clean"}, + ], + "exclusions": ["full EdDSA verification"], + "risk": {"deployment_constraints": ["Use verified serial/u64 backend only."]}, + } + result = score_claim_card(card) + assert result.level == "R3" + assert "lower-layer" in result.rationale + + +def test_no_certificates_is_r0_for_unknown(): + result = score_claim_card({"kind": "unknown", "certificates": []}) + assert result.level == "R0" + assert result.blockers + + +def test_dirty_axioms_do_not_score_r3(): + card = { + "kind": "ed25519", + "certificates": [ + {"name": "CurveFieldProofs.fieldImplementation", "status": "proven", "axiom_status": "dirty"}, + ], + } + result = score_claim_card(card) + assert result.level == "R2" + assert any("Unexpected axioms" in blocker for blocker in result.blockers) + + +def test_pasta_without_aggregate_is_foundation_r2(): + result = score_claim_card({"kind": "pasta_pallas", "certificates": []}) + assert result.level == "R2"