proof-aware-crypto-tooling-.../tests/test_dogfood.py
mrwulf b8ffbafa7f 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 15:21:17 +02:00

90 lines
3.6 KiB
Python

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
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"