proof-aware-crypto-tooling-.../tests/test_dogfood.py

91 lines
3.6 KiB
Python
Raw Normal View History

Dogfood cryptography: pacta verifies signatures through the PROVEN code path "Eat your own dogfood": pacta consumes certificates about a verified Ed25519 implementation while checking those certificates' signatures with OpenSSL. Now it can use the object of its own evidence: - dogfood/pacta-verified-verify: a ~90-line Rust binary built against the PINNED proven source workspace (saymrwulf/curve25519-dalek-source at the exact commit the dalek certificates pin - the build records it: aa0f6ab...) with the serial backend pinned via RUSTFLAGS exactly as the verified extraction pins it. Cargo.toml is committed as a template ({{SOURCE}} placeholder) so no machine path is hardcoded; the rendered file, target/, and the built binary are gitignored. - pacta dogfood-build --source <workspace>: renders, builds, installs to dogfood/state/, and writes a provenance sidecar (source commit, backend cfg, rustc, and an honest coverage note: the certificates cover verify_sha512, the extraction-refactored image of this verify path; SHA-512 and the wire glue remain the theorems' documented boundary). pacta dogfood-status reports the active backend. - signing.verify_payload_ed25519_detailed: dispatch - the dogfood binary when present (backend "verified-dalek-serial"), OpenSSL fallback otherwise, and the backend that ACTUALLY ran is recorded in receipt signature statuses and attestation evidence. Fallback is never silent. - --require-verified-verifier (receipt-verify + agent): policy fails closed when verification did not run on the certificate-covered path. - ML-DSA is deliberately unchanged: no proven implementation exists, so the slot stays fail-closed "unavailable" - the honest hybrid-PQC posture is one proven-classical signature plus one required-but- unproven PQC slot, never a pretend backend. Validated live: receipt verification through the proven verifier (backend recorded), a corrupted signature bit rejected BY the proven binary, tampered attestations rejected, and the policy failing closed when the binary is absent. 49/49 tests green (incl. PEM-SPKI raw-key cross-check against openssl, dispatch/backend recording with a stub, and a real-binary roundtrip that skips gracefully where unbuilt). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-06 08:13:48 +00:00
import os
import stat
import subprocess
from pacta.dogfood import BACKEND_VERIFIED, locate_verifier, pem_public_key_to_raw
from pacta.signing import generate_ed25519_keypair, sign_payload_ed25519, verify_payload_ed25519_detailed
def test_pem_spki_to_raw_roundtrip(tmp_path):
private_key = tmp_path / "k.key"
public_key = tmp_path / "k.pub"
generate_ed25519_keypair(private_key, public_key)
raw = pem_public_key_to_raw(public_key)
assert len(raw) == 32
# cross-check against openssl's own raw dump
dumped = subprocess.run(
["openssl", "pkey", "-pubin", "-in", str(public_key), "-outform", "DER"],
capture_output=True, check=True,
).stdout
assert dumped.endswith(raw)
def test_pem_rejects_non_ed25519(tmp_path):
bogus = tmp_path / "bogus.pub"
bogus.write_text("-----BEGIN PUBLIC KEY-----\nAAAA\n-----END PUBLIC KEY-----\n")
import pytest
with pytest.raises(ValueError):
pem_public_key_to_raw(bogus)
def test_dispatch_prefers_dogfood_binary_and_records_backend(tmp_path, monkeypatch):
# a fake verifier that accepts everything: proves dispatch + backend label
fake = tmp_path / "fake-verify"
fake.write_text("#!/bin/sh\necho OK\nexit 0\n")
fake.chmod(fake.stat().st_mode | stat.S_IEXEC)
private_key = tmp_path / "k.key"
public_key = tmp_path / "k.pub"
generate_ed25519_keypair(private_key, public_key)
payload = b"dogfood dispatch test"
signature = sign_payload_ed25519(payload, private_key)
monkeypatch.setenv("PACTA_DOGFOOD_VERIFIER", str(fake))
ok, error, backend = verify_payload_ed25519_detailed(payload, signature, public_key)
assert ok and backend == BACKEND_VERIFIED
monkeypatch.setenv("PACTA_DOGFOOD_VERIFIER", str(tmp_path / "missing"))
assert locate_verifier() is None
ok, error, backend = verify_payload_ed25519_detailed(payload, signature, public_key)
assert ok and backend == "openssl"
def test_real_dogfood_binary_if_built(tmp_path):
import pytest
binary = locate_verifier()
if binary is None or "fake" in str(binary):
pytest.skip("dogfood verifier not built on this host")
private_key = tmp_path / "k.key"
public_key = tmp_path / "k.pub"
generate_ed25519_keypair(private_key, public_key)
payload = b"the proven path verifies this"
signature = sign_payload_ed25519(payload, private_key)
ok, error, backend = verify_payload_ed25519_detailed(payload, signature, public_key)
assert ok and backend == BACKEND_VERIFIED
# flip one payload byte: the proven path must reject
ok, error, backend = verify_payload_ed25519_detailed(payload + b"x", signature, public_key)
assert not ok and backend == BACKEND_VERIFIED
The provider eats its own dogfood: root signatures via the merkleized library The dogfood principle now runs in BOTH directions. Agents already verified signatures through the proven dalek path; now the provider SIGNS with it too, and proves to itself that the signing code is in its own log before every signature: - dogfood binary gains a `sign` mode (seed over stdin, never argv; ed25519_dalek::SigningKey from the same pinned merkleized workspace). Honesty ledger unchanged: the library's VERIFY path is certificate-covered; its signing path is declared trusted base - but it is the ATTESTED artifact, not an un-attested third implementation. - sign_payload_ed25519_detailed: signing dispatch mirroring the verify dispatch; the backend that actually signed is recorded in every attestation signature block and STH. - THE SELF-REFERENTIAL CHECK: before signing any tree head, the provider runs the SAME Merkle inclusion verification an agent runs - against the very tree it is about to sign - for the newest leaf attesting the signing library itself, and embeds the result in the signature block: signing_provenance: signing_backend: verified-dalek-serial signing_library_component: dalek-ed25519-verified signing_library_source_commit: aa0f6ab... self_inclusion: verified signing_library_leaf_index: 4 signing_library_certificates_proven: 16/16 A root signature that names the leaf vouching for the code that produced it. First-append chicken-and-egg is handled honestly (self_inclusion: library_not_in_log). - Evidence refreshed: all four receipts re-issued under dogfood-signed STHs; the full agent verify loop re-run green. 50/50 tests (new signing roundtrip test, skip-safe where unbuilt). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-06 13:21:17 +00:00
def test_dogfood_signing_roundtrip_if_built(tmp_path):
import pytest
from pacta.dogfood import locate_verifier, pem_private_key_to_seed, sign_payload_dogfood
from pacta.signing import verify_payload_ed25519_detailed
import base64
binary = locate_verifier()
if binary is None or "fake" in str(binary):
pytest.skip("dogfood verifier not built on this host")
private_key = tmp_path / "k.key"
public_key = tmp_path / "k.pub"
from pacta.signing import generate_ed25519_keypair
generate_ed25519_keypair(private_key, public_key)
assert len(pem_private_key_to_seed(private_key)) == 32
payload = b"signed by the merkleized library"
signature = sign_payload_dogfood(payload, private_key, binary)
ok, error, backend = verify_payload_ed25519_detailed(
payload, base64.b64encode(signature).decode(), public_key
)
assert ok and backend == "verified-dalek-serial"