proof-aware-crypto-tooling-.../notebooks/09_dogfood_verified_crypto.ipynb

167 lines
8.2 KiB
Text
Raw Normal View History

Curriculum: the ratchet rule, the four-tier reality, and lecture 9 (dogfood) The notebooks now carry the same didactic contract as the companion book (the "ratchet rule", stated in the course map): every load-bearing idea runs twice - napkin scale, then real scale - and every pair is EXECUTABLE in the notebook, not narrated. - Lecture 1: the truth boundary updated to the proven four-tier apex, with the what-is-still-NOT-proven list (SHA-512, parsers, signing, wallets) given equal weight. - Lecture 2: napkin/real scoring pair - a two-certificate toy card scored in your head, then the shipped sixteen-certificate R4 fixture through the same function, residual blockers and per-tier boundary axioms printed. - Lecture 6: new split-view section. A runnable equivocation drill: pin a two-leaf view, grow it honestly with a consistency proof, then present a forged same-size root and watch the pin store name the attack. Real-scale pointers to --sth-store, log-consistency, log-audit, and the freshness policy; a new exercise asks students to construct the lie a size-only anchor check would miss. - Lecture 7: the wallet gate now swings BOTH ways on real evidence - a partial card denied at R3, the shipped R4 card allowed - both runnable. - Lecture 8 capstone: "design R4" became "audit R4": read the shipped card like an auditor, then design the R5 discharge plan (parser specs, verified SHA-512, signing-side, per-fork production-path mapping). - NEW Lecture 9, "Eat Your Own Dogfood": the honest coverage ledger of the proven-path verifier; a napkin PEM decode (the fixed 12-byte Ed25519 SPKI prefix, read with your eyes) paired with the mechanical extraction; live backend dispatch; the fail-closed --require-verified-verifier policy; and the hybrid-PQC section - proven-classical Ed25519 plus a required-but-honest ML-DSA slot ("blockers get fixed; placeholders get trusted"). Every code cell of the changed notebooks was executed end-to-end before committing (outputs stripped per house rules). 49/49 tests green with the notebook inventory updated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-06 08:18:06 +00:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Lecture 9: Eat Your Own Dogfood - Verified Crypto in the Agent's Own Loop\n",
"\n",
"Every lecture so far had the agent consume EVIDENCE about a verified Ed25519 implementation while checking that evidence's signatures with OpenSSL - an unverified implementation of the very primitive the evidence is about. That is a defensible bootstrap, but it leaves an ironic gap. This lecture closes it: pacta can build a verifier binary from the PINNED, PROVEN source workspace - the exact commit the dalek certificates pin, serial backend pinned exactly as the verified extraction pins it - and route its own signature checks through it.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Learning Objectives\n",
"\n",
"- State precisely which parts of the dogfood verifier are certificate-covered and which are its trusted base.\n",
"- Extract a raw Ed25519 key from an OpenSSL PEM by hand (napkin) and mechanically (real).\n",
"- Demonstrate backend dispatch and the fail-closed `--require-verified-verifier` policy.\n",
"- Defend the hybrid post-quantum posture: one proven-classical signature plus one required-but-honest ML-DSA slot.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## What \"verified\" means here - the honest ledger\n",
"\n",
"The binary calls `ed25519_dalek::VerifyingKey::verify` in the pinned workspace. The certificates cover `verify_sha512`, the extraction-refactored image of that same path (the delta is the documented hash-wrapper refactor in the pinned source). Certificate-covered: field arithmetic, the group law, scalars, encoding/decoding, constructive decompression, and the four-tier acceptance criterion. Trusted base: SHA-512 (an oracle in the theorems - the proofs hold for whatever bytes it produces), roughly fifteen lines of wire glue, rustc, and the extraction pipeline. The provenance sidecar written at build time records the source commit, the backend cfg, and this exact coverage note - the dogfood claim is itself a claim card.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Napkin: read a PEM with your eyes\n",
"\n",
"An OpenSSL Ed25519 public key PEM is a base64-wrapped DER SubjectPublicKeyInfo (RFC 8410), and for this one algorithm the DER is FIXED: twelve prefix bytes `302a300506032b6570032100`, then the raw 32-byte key. Decode one by hand:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pathlib import Path\n",
"import base64, subprocess, sys, tempfile\n",
"\n",
"repo_root = Path.cwd()\n",
"if not (repo_root / \"src\" / \"pacta\").exists():\n",
" repo_root = repo_root.parent\n",
"sys.path.insert(0, str(repo_root / \"src\"))\n",
"\n",
"from pacta.signing import generate_ed25519_keypair\n",
"\n",
"tmp = Path(tempfile.mkdtemp(prefix=\"dogfood-lecture-\"))\n",
"generate_ed25519_keypair(tmp / \"k.key\", tmp / \"k.pub\")\n",
"pem = (tmp / \"k.pub\").read_text()\n",
"print(pem)\n",
"body = \"\".join(line for line in pem.splitlines() if \"-----\" not in line)\n",
"der = base64.b64decode(body)\n",
"print(\"DER length:\", len(der), \"(should be 12 + 32 = 44)\")\n",
"print(\"prefix: \", der[:12].hex(), \"(the fixed Ed25519 SPKI header)\")\n",
"print(\"raw key: \", der[12:].hex())\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# REAL: the same extraction, mechanically, with validation - and the\n",
"# dispatch that prefers the proven-path binary when it exists.\n",
"from pacta.dogfood import locate_verifier, pem_public_key_to_raw\n",
"from pacta.signing import sign_payload_ed25519, verify_payload_ed25519_detailed\n",
"\n",
"raw = pem_public_key_to_raw(tmp / \"k.pub\")\n",
"assert raw == der[12:]\n",
"print(\"mechanical extraction matches the napkin:\", raw.hex()[:16], \"...\")\n",
"\n",
"payload = b\"the agent checks its own evidence\"\n",
"signature = sign_payload_ed25519(payload, tmp / \"k.key\")\n",
"ok, error, backend = verify_payload_ed25519_detailed(payload, signature, tmp / \"k.pub\")\n",
"print(\"valid:\", ok, \"| backend:\", backend)\n",
"binary = locate_verifier()\n",
"print(\"dogfood binary:\", binary or \"not built (OpenSSL fallback in effect - a recorded downgrade)\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Build the proven-path verifier once per machine (it needs a local checkout of the pinned source workspace and cargo):\n",
"\n",
"```bash\n",
"pacta dogfood-build --source ~/GitClone/FormalVerification/sources/curve25519-dalek-source\n",
"pacta dogfood-status\n",
"```\n",
"\n",
"With the binary in place, every receipt and attestation check reports `ed25519_backend: verified-dalek-serial`, and policies can DEMAND it:\n",
"\n",
"```bash\n",
"pacta receipt-verify ... --require-verified-verifier # fails closed on OpenSSL fallback\n",
"pacta agent ... --require-verified-verifier ...\n",
"```\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## The post-quantum line, held honestly\n",
"\n",
"The dogfood loop deliberately does NOT extend to ML-DSA. There is no formally verified ML-DSA implementation in this corpus, and pretending otherwise would poison the whole posture. The hybrid strategy is therefore asymmetric on purpose:\n",
"\n",
"- **Ed25519 (classical): proven path.** The signature everyone can check today runs on certificate-covered code.\n",
"- **ML-DSA-65 (post-quantum): required, honest, unavailable-until-real.** The tree-head slot exists in every signed structure; `--require-signatures both` fails CLOSED on hosts without a real FIPS 204 backend; and when a real backend lands, the policy flips on without a schema change.\n",
"\n",
"A migration strategy that records \"we cannot do this yet\" as a deployment blocker is strictly stronger than one that ships a placeholder. Blockers get fixed; placeholders get trusted.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pacta.postquantum import detect_ml_dsa\n",
"\n",
"capability = detect_ml_dsa()\n",
"print(\"ml-dsa available:\", capability.available)\n",
"print(\"reason:\", capability.reason)\n",
"print(\"slot as recorded in every STH:\", capability.to_signature_slot())\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercises\n",
"\n",
"- Flip one byte of a signature and verify through both backends; confirm both reject and that the BACKEND that rejected is recorded.\n",
"- The dogfood binary's trusted base includes rustc. The certificates' trusted base includes Charon/Aeneas. Draw the two trust diagrams side by side; which assumptions are shared?\n",
"- Napkin, then real: decode a second PEM by hand; then corrupt its DER prefix and confirm `pem_public_key_to_raw` rejects it.\n",
"- Policy design: when should an agent REFUSE to fall back to OpenSSL? Write the deployment rule and its recovery path.\n",
"- Research checkpoint: what would a proof-carrying SHA-512 change about the coverage note in the provenance sidecar?\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"pygments_lexer": "ipython3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}