diff --git a/crates/aien-proof/formal/formal-job.sh b/crates/aien-proof/formal/formal-job.sh new file mode 100644 index 00000000..2b5f53f1 --- /dev/null +++ b/crates/aien-proof/formal/formal-job.sh @@ -0,0 +1,79 @@ +#!/bin/sh +# Run one formal oracle job on the proof board and bind the result to its evidence. +# +# formal-job.sh formal/jobs/.job +# +# Needs: AIEN_ARCH_DIR (aien-architecture checkout holding the oracle and manifests), +# aien-proof on PATH (or AIEN_PROOF_BIN), gh authenticated (to fetch the covered file once). +# Optional: AIEN_FORMAL_CACHE (default ~/.cache/aien-formal), AIEN_PROOF_DIR (board dir). +# Prints the receipt ID. Exit code follows the receipt result (0 PASS). +# +# The covered implementation file is fetched at the pinned COVERED_COMMIT into the cache and +# hashed like any input, so the pin is part of the job fingerprint. Changing the job file, this +# script, the invariant manifest, any Lean source, or the covered file changes the job key. +set -eu +job_file=${1:?usage: formal-job.sh JOBFILE} +here=$(cd "$(dirname "$0")" && pwd) +job_file=$(cd "$(dirname "$job_file")" && pwd)/$(basename "$job_file") +arch=${AIEN_ARCH_DIR:?set AIEN_ARCH_DIR to the aien-architecture checkout} +proof=${AIEN_PROOF_BIN:-aien-proof} +cache=${AIEN_FORMAL_CACHE:-$HOME/.cache/aien-formal} + +# shellcheck disable=SC1090 +. "$job_file" + +mode=${2:-start} + +covered_abs="$cache/${COVERED_REPO}/$COVERED_COMMIT/$COVERED_PATH" +out="$cache/out/$JOB.out" + +inputs_flags() { + printf '%s\n' --base "$arch" --invariant "$INVARIANT" + for m in $MODEL; do printf '%s\n' --model "$m"; done + printf '%s\n' --proof-source "$ORACLE_DIR" --covered "$covered_abs" +} +# word-safe enough: no path here contains spaces +iflags=$(inputs_flags | tr '\n' ' ') + +if [ "$mode" = "exec" ]; then + # Inside the board job: write the digest line, then the oracle output, keep both. + mkdir -p "$(dirname "$out")" + # shellcheck disable=SC2086 + d=$("$proof" formal digest $iflags) + { echo "inputs_digest: $d"; sh "$arch/$ORACLE_DIR/$RUNNER"; } >"$out" 2>&1 && rc=0 || rc=$? + cat "$out" + exit "$rc" +fi + +# Fetch the covered file at the pinned commit (once). +if [ ! -s "$covered_abs" ]; then + mkdir -p "$(dirname "$covered_abs")" + gh api -H "Accept: application/vnd.github.raw" \ + "repos/$COVERED_REPO/contents/$COVERED_PATH?ref=$COVERED_COMMIT" >"$covered_abs.tmp" + mv "$covered_abs.tmp" "$covered_abs" +fi + +export PATH="$HOME/.elan/bin:$PATH" +toolchain="$(tr -d '\n' <"$arch/$ORACLE_DIR/lean-toolchain") / $(lean --version | head -1) / $(lake --version | head -1)" +theorems=$(sed -n 's/^theorem \([A-Za-z_0-9.]*\).*/\1/p' "$arch/$THEOREM_FILE" | sed 's/^/--theorem /' | tr '\n' ' ') +arch_commit=$(git -C "$arch" rev-parse HEAD) +dirty="" +[ -n "$(git -C "$arch" status --porcelain -- "$INVARIANT" "$ORACLE_DIR")" ] && dirty="--dirty" + +# The board job. Inputs are the invariant manifest, the Lean sources, this script, the job +# file, and the covered file. Run from the architecture checkout so inputs hash by relative path. +cd "$arch" +rc=0 +"$proof" run --job "$JOB" \ + --input "$INVARIANT" --input "$ORACLE_DIR" \ + --input "$here/formal-job.sh" --input "$job_file" --input "$covered_abs" \ + -- sh "$here/formal-job.sh" "$job_file" exec || rc=$? +[ -s "$out" ] || { echo "no output recorded for $JOB (rc=$rc)"; exit 1; } + +# shellcheck disable=SC2086 +exec "$proof" formal bind $iflags \ + --oracle-toolchain "$toolchain" $theorems \ + --result-prefix "$RESULT_PREFIX" --output-file "$out" \ + --repo "https://github.com/$COVERED_REPO" --commit "$COVERED_COMMIT" \ + --machine "$(hostname)" --procedure "$JOB" \ + --external-ref "aien-architecture@$arch_commit" $dirty diff --git a/crates/aien-proof/formal/jobs/formal-cap-attenuation-v1.job b/crates/aien-proof/formal/jobs/formal-cap-attenuation-v1.job new file mode 100644 index 00000000..2d4eabf0 --- /dev/null +++ b/crates/aien-proof/formal/jobs/formal-cap-attenuation-v1.job @@ -0,0 +1,13 @@ +# Job definition: capability attenuation, formal oracle bootstrap (ADR 0027, FORMAL-5). +# Plain KEY=VALUE shell assignments, read by formal/formal-job.sh. Paths are relative to the +# aien-architecture checkout (AIEN_ARCH_DIR) except COVERED_PATH, which is a path inside COVERED_REPO. +JOB=formal-cap-attenuation-v1 +INVARIANT=formal/invariants/capability-attenuation-v1.toml +ORACLE_DIR=bootstrap/formal-oracles/lean +RUNNER=run-cap-formal.sh +RESULT_PREFIX=CAP_FORMAL_BOOTSTRAP +MODEL="bootstrap/formal-oracles/lean/AienCap/Model.lean" +THEOREM_FILE=bootstrap/formal-oracles/lean/AienCap/Theorems.lean +COVERED_REPO=aien-dev/aienos +COVERED_COMMIT=2ee61b4b158a385c5fcf2c925a625ac043babb21 +COVERED_PATH=crates/aienos-aegis/src/capability.rs diff --git a/crates/aien-proof/formal/jobs/formal-receipt-consistency-v1.job b/crates/aien-proof/formal/jobs/formal-receipt-consistency-v1.job new file mode 100644 index 00000000..dbcd8fc2 --- /dev/null +++ b/crates/aien-proof/formal/jobs/formal-receipt-consistency-v1.job @@ -0,0 +1,12 @@ +# Job definition: admission receipt consistency, formal oracle bootstrap (ADR 0027, FORMAL-5). +# See formal-cap-attenuation-v1.job for the format. +JOB=formal-receipt-consistency-v1 +INVARIANT=formal/invariants/artifact-receipt-v1.toml +ORACLE_DIR=bootstrap/formal-oracles/lean +RUNNER=run-receipt-formal.sh +RESULT_PREFIX=RECEIPT_FORMAL_BOOTSTRAP +MODEL="bootstrap/formal-oracles/lean/AienReceipt/Model.lean bootstrap/formal-oracles/lean/AienReceipt/Layout.lean" +THEOREM_FILE=bootstrap/formal-oracles/lean/AienReceipt/Theorems.lean +COVERED_REPO=aien-dev/aienos +COVERED_COMMIT=2ee61b4b158a385c5fcf2c925a625ac043babb21 +COVERED_PATH=crates/aienos-artifact/src/receipt.rs diff --git a/crates/aien-proof/src/fingerprint.rs b/crates/aien-proof/src/fingerprint.rs index ea4a754e..1ca6781f 100644 --- a/crates/aien-proof/src/fingerprint.rs +++ b/crates/aien-proof/src/fingerprint.rs @@ -10,8 +10,9 @@ use std::os::unix::ffi::OsStrExt; use std::path::{Component, Path, PathBuf}; use std::process::Command; -/// Directory names never hashed: build output, git internals, live crumb scents. -const SKIP: &[&str] = &["target", ".git", ".crumb.local"]; +/// Directory names never hashed: build output (Cargo `target`, oracle `.lake`), git +/// internals, live crumb scents. +const SKIP: &[&str] = &["target", ".git", ".crumb.local", ".lake"]; fn field(h: &mut blake3::Hasher, bytes: &[u8]) { h.update(&(bytes.len() as u64).to_be_bytes()); @@ -75,22 +76,8 @@ fn inside(input: &Path) -> bool { input.is_relative() && !input.components().any(|c| c == Component::ParentDir) } -pub fn job_key( - base: &Path, - job: &str, - cmd: &[String], - toolchain: &str, - inputs: &[PathBuf], -) -> io::Result { - let mut h = blake3::Hasher::new(); - h.update(b"AIEN_PROOF_JOB_V2"); - field(&mut h, job.as_bytes()); - h.update(&(cmd.len() as u64).to_be_bytes()); - for arg in cmd { - field(&mut h, arg.as_bytes()); - } - field(&mut h, toolchain.as_bytes()); - +/// Feed the sorted file set under `inputs` (paths and content hashes) into `h`. +fn hash_inputs(h: &mut blake3::Hasher, base: &Path, inputs: &[PathBuf]) -> io::Result<()> { // Inputs inside the checkout go through git's view; anything outside it // (sibling repos reached by `../` path dependencies) is walked directly. let (local, outside): (Vec<&PathBuf>, Vec<&PathBuf>) = inputs.iter().partition(|p| inside(p)); @@ -122,14 +109,44 @@ pub fn job_key( h.update(&(files.len() as u64).to_be_bytes()); for file in &files { let rel = file.strip_prefix(base).unwrap_or(file); - field(&mut h, rel.to_string_lossy().as_bytes()); + field(h, rel.to_string_lossy().as_bytes()); match fs::read(file) { - Ok(data) => field(&mut h, blake3::hash(&data).as_bytes()), + Ok(data) => field(h, blake3::hash(&data).as_bytes()), // Tracked in git but deleted in this worktree. - Err(e) if e.kind() == io::ErrorKind::NotFound => field(&mut h, b""), + Err(e) if e.kind() == io::ErrorKind::NotFound => field(h, b""), Err(e) => return Err(e), } } + Ok(()) +} + +pub fn job_key( + base: &Path, + job: &str, + cmd: &[String], + toolchain: &str, + inputs: &[PathBuf], +) -> io::Result { + let mut h = blake3::Hasher::new(); + h.update(b"AIEN_PROOF_JOB_V2"); + field(&mut h, job.as_bytes()); + h.update(&(cmd.len() as u64).to_be_bytes()); + for arg in cmd { + field(&mut h, arg.as_bytes()); + } + field(&mut h, toolchain.as_bytes()); + hash_inputs(&mut h, base, inputs)?; + Ok(h.finalize().to_hex().to_string()) +} + +/// Digest of the file set under `inputs` alone (no command, no toolchain). +/// Receipts use it to name one slice of a job's inputs, such as a formal +/// model or the implementation file a proof covers, with the same file rules +/// the job fingerprint uses. +pub fn inputs_digest(base: &Path, inputs: &[PathBuf]) -> io::Result { + let mut h = blake3::Hasher::new(); + h.update(b"AIEN_PROOF_INPUTS_V1"); + hash_inputs(&mut h, base, inputs)?; Ok(h.finalize().to_hex().to_string()) } diff --git a/crates/aien-proof/src/formal.rs b/crates/aien-proof/src/formal.rs new file mode 100644 index 00000000..1eb45dd4 --- /dev/null +++ b/crates/aien-proof/src/formal.rs @@ -0,0 +1,547 @@ +//! Formal oracle bindings: tie a formal proof job to the exact evidence it ran on. +//! +//! A formal job (a theorem prover or model checker run) is one more +//! qualification procedure. Its result becomes an ordinary `EvidenceReceiptV1` +//! of kind [`KIND`]. No new receipt fields and no new canonical encoding exist: +//! each bound item is a stable assertion ID, the four input digests sit in +//! `input_artifacts`, the oracle toolchain sits in `toolchain`, and the output +//! digest and dependencies use the native fields. Names are tool neutral +//! (`oracle_toolchain`, `proof_source_digest`) so a later oracle can replace +//! the bootstrap one without a format change (ADR 0027 point 5). +//! +//! A PASS here is one evidence input bound to one source and model revision. +//! It is not authority (ADR 0027 point 6); `authority` stays empty. +//! +//! Invalidation rests on [`crate::fingerprint`]: a job whose inputs include +//! the invariant manifest, the formal model and proof sources, and the covered +//! implementation file gets a new job key when any of them changes, so an old +//! stamp is never replayed for new evidence. + +use std::path::{Path, PathBuf}; + +use crate::evidence::{Assertion, Receipt, Verdict}; +use crate::fingerprint::inputs_digest; +use crate::import::{build_receipt, ImportRequest}; + +/// Receipt kind for formal oracle bindings. +pub const KIND: &str = "formal-oracle-binding"; + +pub const A_INVARIANT_ID: &str = "oracle_binding.invariant_id"; +pub const A_INVARIANT_VERSION: &str = "oracle_binding.invariant_version"; +pub const A_MODEL_DIGEST: &str = "oracle_binding.model_digest"; +pub const A_ORACLE_TOOLCHAIN: &str = "oracle_binding.oracle_toolchain"; +pub const A_PROOF_SOURCE_DIGEST: &str = "oracle_binding.proof_source_digest"; +pub const A_COVERED_IMPL_DIGEST: &str = "oracle_binding.covered_impl_digest"; +pub const A_MANIFEST_DIGEST: &str = "oracle_binding.manifest_digest"; +/// Prefix of one assertion per theorem checked: `oracle_binding.theorem:`. +pub const A_THEOREM_PREFIX: &str = "oracle_binding.theorem:"; + +const SOURCE: &str = "aien-proof formal bind"; + +/// Everything a formal job binds, as plain data. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct FormalBinding { + pub invariant_id: String, + pub invariant_version: u32, + /// Digest of the formal model files. + pub model_digest: String, + /// Oracle toolchain identity (version, commit, platform), free text. + pub oracle_toolchain: String, + /// Digest of all proof sources the oracle compiled (model, theorems, corpus, mutants). + pub proof_source_digest: String, + /// Digest of the implementation file(s) the model claims to cover. + pub covered_impl_digest: String, + /// Digest of the invariant manifest. + pub manifest_digest: String, + pub theorems_checked: Vec, +} + +/// Paths (relative to `base`) that make up each slice of a formal job. +#[derive(Debug, Clone)] +pub struct FormalInputs { + pub base: PathBuf, + pub manifest: PathBuf, + pub model: Vec, + pub proof_source: Vec, + pub covered: Vec, +} + +impl FormalInputs { + /// One digest over every input of the job. The job writes it into its + /// captured output so a receipt can refuse output from a different input set. + pub fn run_digest(&self) -> Result { + let mut all = vec![self.manifest.clone()]; + all.extend(self.model.iter().cloned()); + all.extend(self.proof_source.iter().cloned()); + all.extend(self.covered.iter().cloned()); + inputs_digest(&self.base, &all).map_err(|e| format!("cannot digest inputs: {e}")) + } + + fn slice(&self, paths: &[PathBuf]) -> Result { + if paths.is_empty() { + return Err("formal binding needs at least one path per input slice".to_string()); + } + inputs_digest(&self.base, paths).map_err(|e| format!("cannot digest inputs: {e}")) + } +} + +/// Read `id` and `version` from the top level of an invariant manifest. +/// Only the leading key/value block (before the first table header) is read. +pub fn manifest_identity(path: &Path) -> Result<(String, u32), String> { + let text = std::fs::read_to_string(path).map_err(|e| format!("cannot read manifest: {e}"))?; + let (mut id, mut version) = (None, None); + for line in text.lines() { + if line.starts_with('[') { + break; + } + if let Some(rest) = line.strip_prefix("id = ") { + id = Some(rest.trim().trim_matches('"').to_string()); + } else if let Some(rest) = line.strip_prefix("version = ") { + version = rest.trim().parse::().ok(); + } + } + match (id, version) { + (Some(i), Some(v)) if !i.is_empty() => Ok((i, v)), + _ => Err("manifest has no top level id and numeric version".to_string()), + } +} + +/// Result of the job taken from the oracle's own final line, `PREFIX: PASS|FAIL|NOT_RUN`. +/// Anything else is refused; there is no default. +pub fn parse_result_line(output: &str, prefix: &str) -> Result { + let last = output + .lines() + .rev() + .find(|l| !l.trim().is_empty()) + .ok_or_else(|| "output is empty".to_string())?; + match last.strip_prefix(prefix).and_then(|r| r.strip_prefix(": ")) { + Some("PASS") => Ok(Verdict::Pass), + Some("FAIL") => Ok(Verdict::Fail), + Some("NOT_RUN") => Ok(Verdict::Skipped), + _ => Err(format!( + "last output line is not a {prefix} result line: {last:?}" + )), + } +} + +/// The job output must carry the digest of the exact inputs it ran on. +pub fn check_output_binds_inputs(output: &str, run_digest: &str) -> Result<(), String> { + let want = format!("inputs_digest: {run_digest}"); + if output.lines().any(|l| l == want) { + Ok(()) + } else { + Err("output does not carry this input set's digest (stale or foreign output)".to_string()) + } +} + +/// Compute the binding from files on disk. +pub fn compute_binding( + inputs: &FormalInputs, + oracle_toolchain: &str, + theorems: &[String], +) -> Result { + let (invariant_id, invariant_version) = manifest_identity(&inputs.base.join(&inputs.manifest))?; + Ok(FormalBinding { + invariant_id, + invariant_version, + model_digest: inputs.slice(&inputs.model)?, + oracle_toolchain: oracle_toolchain.to_string(), + proof_source_digest: inputs.slice(&inputs.proof_source)?, + covered_impl_digest: inputs.slice(&inputs.covered)?, + manifest_digest: inputs.slice(std::slice::from_ref(&inputs.manifest))?, + theorems_checked: theorems.to_vec(), + }) +} + +fn fact(id: &str, value: &str) -> Assertion { + Assertion { + id: id.to_string(), + expected: String::new(), + observed: value.to_string(), + pass: true, + source: SOURCE.to_string(), + note: String::new(), + } +} + +/// Assertions carrying the binding. Theorem assertions pass only when the +/// job's result is PASS. +pub fn binding_assertions(b: &FormalBinding, result: Verdict) -> Vec { + let mut out = vec![ + fact(A_INVARIANT_ID, &b.invariant_id), + fact(A_INVARIANT_VERSION, &b.invariant_version.to_string()), + fact(A_MODEL_DIGEST, &b.model_digest), + fact(A_ORACLE_TOOLCHAIN, &b.oracle_toolchain), + fact(A_PROOF_SOURCE_DIGEST, &b.proof_source_digest), + fact(A_COVERED_IMPL_DIGEST, &b.covered_impl_digest), + fact(A_MANIFEST_DIGEST, &b.manifest_digest), + ]; + for t in &b.theorems_checked { + let mut a = fact(&format!("{A_THEOREM_PREFIX}{t}"), "checked"); + a.pass = result == Verdict::Pass; + out.push(a); + } + out +} + +fn get<'a>(r: &'a Receipt, id: &str) -> Result<&'a str, String> { + r.assertions + .iter() + .find(|a| a.id == id) + .map(|a| a.observed.as_str()) + .filter(|v| !v.trim().is_empty()) + .ok_or_else(|| format!("formal receipt lacks assertion {id}")) +} + +/// Read the binding back out of a receipt, checking it is self consistent: +/// the four digests in `input_artifacts` equal the digest assertions, the +/// toolchain field equals the oracle toolchain, and at least one theorem is listed. +pub fn parse_binding(r: &Receipt) -> Result { + if r.kind != KIND { + return Err(format!("not a formal oracle binding: kind {:?}", r.kind)); + } + let b = FormalBinding { + invariant_id: get(r, A_INVARIANT_ID)?.to_string(), + invariant_version: get(r, A_INVARIANT_VERSION)? + .parse() + .map_err(|_| "invariant_version is not a number".to_string())?, + model_digest: get(r, A_MODEL_DIGEST)?.to_string(), + oracle_toolchain: get(r, A_ORACLE_TOOLCHAIN)?.to_string(), + proof_source_digest: get(r, A_PROOF_SOURCE_DIGEST)?.to_string(), + covered_impl_digest: get(r, A_COVERED_IMPL_DIGEST)?.to_string(), + manifest_digest: get(r, A_MANIFEST_DIGEST)?.to_string(), + theorems_checked: r + .assertions + .iter() + .filter_map(|a| a.id.strip_prefix(A_THEOREM_PREFIX)) + .map(str::to_string) + .collect(), + }; + if b.theorems_checked.is_empty() { + return Err("formal receipt lists no theorems".to_string()); + } + let want = artifact_digests(&b); + if r.input_artifacts != want { + return Err("input_artifacts do not match the bound digests".to_string()); + } + if r.toolchain != b.oracle_toolchain { + return Err("toolchain field differs from oracle_toolchain".to_string()); + } + Ok(b) +} + +fn artifact_digests(b: &FormalBinding) -> Vec { + [ + &b.manifest_digest, + &b.model_digest, + &b.proof_source_digest, + &b.covered_impl_digest, + ] + .iter() + .map(|d| format!("blake3:{d}")) + .collect() +} + +/// Profile checks for kind [`KIND`], called from `build_receipt`. +pub fn validate_formal_profile( + req: &ImportRequest, + assertions: &[Assertion], + result: Verdict, +) -> Result<(), String> { + if !req.authority.trim().is_empty() { + return Err("a formal binding is evidence, not authority: authority must be empty".into()); + } + if req.toolchain.trim().is_empty() { + return Err("formal binding requires an oracle toolchain identity".to_string()); + } + if req.tier != "HOST_TEST" { + return Err(format!("formal binding refuses tier {:?}", req.tier)); + } + for id in [ + A_INVARIANT_ID, + A_INVARIANT_VERSION, + A_MODEL_DIGEST, + A_ORACLE_TOOLCHAIN, + A_PROOF_SOURCE_DIGEST, + A_COVERED_IMPL_DIGEST, + A_MANIFEST_DIGEST, + ] { + let ok = assertions + .iter() + .any(|a| a.id == id && !a.observed.trim().is_empty()); + if !ok { + return Err(format!("formal binding missing assertion {id}")); + } + } + if !assertions + .iter() + .any(|a| a.id.starts_with(A_THEOREM_PREFIX)) + { + return Err("formal binding lists no theorems".to_string()); + } + if result == Verdict::Pass && assertions.iter().any(|a| !a.pass) { + return Err("formal binding claims PASS but an assertion failed".to_string()); + } + if req.input_artifacts.len() != 4 { + return Err("formal binding needs the four input digests".to_string()); + } + Ok(()) +} + +/// Facts a caller supplies beyond the binding itself. +#[derive(Debug, Clone)] +pub struct FormalRun { + /// Repository and commit of the covered implementation. + pub repo: String, + pub commit: String, + pub dirty: bool, + pub machine: String, + pub procedure: String, + pub result: Verdict, + pub output_digest: String, + pub dependencies: Vec, + pub external_refs: Vec, + pub timestamp: Option, +} + +/// Mint the receipt for a formal job. +pub fn bind_receipt(b: &FormalBinding, run: &FormalRun) -> Result { + let req = ImportRequest { + repo: run.repo.clone(), + commit: run.commit.clone(), + dirty: run.dirty, + kind: KIND.to_string(), + tier: "HOST_TEST".to_string(), + toolchain: b.oracle_toolchain.clone(), + procedure: run.procedure.clone(), + machine: run.machine.clone(), + input_artifacts: artifact_digests(b), + dependencies: run.dependencies.clone(), + output_digest: Some(run.output_digest.clone()), + external_refs: run.external_refs.clone(), + timestamp: run.timestamp, + ..ImportRequest::default() + }; + build_receipt(&req, binding_assertions(b, run.result), run.result) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::evidence::verify_bytes; + use crate::fingerprint::job_key; + use crate::testutil::temp_dir; + use std::fs; + + const MANIFEST: &str = + "schema_version = 0\nid = \"AIEN.INV.TEST.V1\"\nversion = 3\n\n[[x]]\nid = \"nope\"\n"; + + fn tree(root: &Path) -> FormalInputs { + fs::create_dir_all(root.join("inv")).unwrap(); + fs::create_dir_all(root.join("lean/Model")).unwrap(); + fs::create_dir_all(root.join("src")).unwrap(); + fs::write(root.join("inv/a.toml"), MANIFEST).unwrap(); + fs::write(root.join("lean/Model/Model.lean"), "def m := 1").unwrap(); + fs::write( + root.join("lean/Theorems.lean"), + "theorem t : True := trivial", + ) + .unwrap(); + fs::write(root.join("src/cap.rs"), "fn derive() {}").unwrap(); + FormalInputs { + base: root.to_path_buf(), + manifest: "inv/a.toml".into(), + model: vec!["lean/Model".into()], + proof_source: vec!["lean".into()], + covered: vec!["src/cap.rs".into()], + } + } + + fn all_inputs(i: &FormalInputs) -> Vec { + let mut v = vec![i.manifest.clone()]; + v.extend(i.model.clone()); + v.extend(i.proof_source.clone()); + v.extend(i.covered.clone()); + v + } + + fn key(i: &FormalInputs) -> String { + job_key( + &i.base, + "formal-test-v1", + &["sh".into(), "run.sh".into()], + "lean 4", + &all_inputs(i), + ) + .unwrap() + } + + fn run() -> FormalRun { + FormalRun { + repo: "https://github.com/aien-dev/aienos".into(), + commit: "a".repeat(40), + dirty: false, + machine: "spark".into(), + procedure: "formal-test-v1".into(), + result: Verdict::Pass, + output_digest: "c".repeat(64), + dependencies: vec![], + external_refs: vec![], + timestamp: Some(1), + } + } + + fn binding(i: &FormalInputs) -> FormalBinding { + compute_binding(i, "lean 4.34.1", &["t1".into(), "t2".into()]).unwrap() + } + + #[test] + fn manifest_identity_reads_top_level_only() { + let root = temp_dir("formal-id"); + let i = tree(&root); + assert_eq!( + manifest_identity(&root.join(&i.manifest)).unwrap(), + ("AIEN.INV.TEST.V1".to_string(), 3) + ); + } + + #[test] + fn receipt_round_trips_and_verifies() { + let root = temp_dir("formal-rt"); + let i = tree(&root); + let b = binding(&i); + let r = bind_receipt(&b, &run()).unwrap(); + let text = serde_json::to_vec(&r).unwrap(); + assert!(verify_bytes(&text).unwrap().1.ok()); + assert_eq!(parse_binding(&r).unwrap(), b); + assert_eq!(r.output_digest, "c".repeat(64)); + assert_eq!(r.authority, ""); + } + + #[test] + fn changed_implementation_model_proof_or_manifest_changes_key_digest_and_receipt() { + for (rel, what) in [ + ("src/cap.rs", "implementation"), + ("lean/Model/Model.lean", "model"), + ("lean/Theorems.lean", "proof source"), + ("inv/a.toml", "manifest"), + ] { + let root = temp_dir("formal-inv"); + let i = tree(&root); + let (k0, d0, b0) = (key(&i), i.run_digest().unwrap(), binding(&i)); + let id0 = bind_receipt(&b0, &run()).unwrap().id; + let old = fs::read_to_string(root.join(rel)).unwrap(); + fs::write(root.join(rel), format!("{old}\n-- changed")).unwrap(); + assert_ne!(k0, key(&i), "{what}: job key must change"); + assert_ne!( + d0, + i.run_digest().unwrap(), + "{what}: run digest must change" + ); + let b1 = binding(&i); + assert_ne!(b0, b1, "{what}: binding must change"); + assert_ne!( + id0, + bind_receipt(&b1, &run()).unwrap().id, + "{what}: receipt id" + ); + } + } + + #[test] + fn each_slice_digest_moves_only_with_its_own_files() { + let root = temp_dir("formal-slice"); + let i = tree(&root); + let b0 = binding(&i); + fs::write(root.join("src/cap.rs"), "fn derive() { /* x */ }").unwrap(); + let b1 = binding(&i); + assert_ne!(b0.covered_impl_digest, b1.covered_impl_digest); + assert_eq!(b0.model_digest, b1.model_digest); + assert_eq!(b0.proof_source_digest, b1.proof_source_digest); + assert_eq!(b0.manifest_digest, b1.manifest_digest); + } + + #[test] + fn lake_build_output_does_not_change_the_key() { + let root = temp_dir("formal-lake"); + let i = tree(&root); + let k0 = key(&i); + fs::create_dir_all(root.join("lean/.lake/build")).unwrap(); + fs::write(root.join("lean/.lake/build/x.olean"), "bin").unwrap(); + assert_eq!(k0, key(&i)); + } + + #[test] + fn result_line_must_match_the_oracle_prefix() { + assert_eq!( + parse_result_line("a\nCAP_FORMAL_BOOTSTRAP: PASS\n", "CAP_FORMAL_BOOTSTRAP"), + Ok(Verdict::Pass) + ); + assert_eq!( + parse_result_line("CAP_FORMAL_BOOTSTRAP: FAIL", "CAP_FORMAL_BOOTSTRAP"), + Ok(Verdict::Fail) + ); + assert_eq!( + parse_result_line("CAP_FORMAL_BOOTSTRAP: NOT_RUN", "CAP_FORMAL_BOOTSTRAP"), + Ok(Verdict::Skipped) + ); + assert!( + parse_result_line("RECEIPT_FORMAL_BOOTSTRAP: PASS", "CAP_FORMAL_BOOTSTRAP").is_err() + ); + assert!( + parse_result_line("CAP_FORMAL_BOOTSTRAP: PASS\nextra", "CAP_FORMAL_BOOTSTRAP").is_err() + ); + assert!(parse_result_line("", "CAP_FORMAL_BOOTSTRAP").is_err()); + } + + #[test] + fn output_from_another_input_set_is_refused() { + let root = temp_dir("formal-stale"); + let i = tree(&root); + let d = i.run_digest().unwrap(); + let out = format!("inputs_digest: {d}\nCAP_FORMAL_BOOTSTRAP: PASS\n"); + assert!(check_output_binds_inputs(&out, &d).is_ok()); + fs::write(root.join("src/cap.rs"), "fn derive() { evil() }").unwrap(); + assert!(check_output_binds_inputs(&out, &i.run_digest().unwrap()).is_err()); + } + + #[test] + fn failing_result_is_never_sealed_and_theorems_are_not_passing() { + let root = temp_dir("formal-fail"); + let b = binding(&tree(&root)); + let mut r = run(); + r.result = Verdict::Fail; + assert!( + bind_receipt(&b, &r).is_err(), + "the receipt layer refuses to seal FAIL" + ); + assert!(binding_assertions(&b, Verdict::Fail) + .iter() + .filter(|a| a.id.starts_with(A_THEOREM_PREFIX)) + .all(|a| !a.pass)); + } + + #[test] + fn profile_refuses_incomplete_or_authoritative_bindings() { + let root = temp_dir("formal-refuse"); + let b = binding(&tree(&root)); + let mut no_theorems = b.clone(); + no_theorems.theorems_checked.clear(); + assert!(bind_receipt(&no_theorems, &run()).is_err()); + let mut no_tool = b.clone(); + no_tool.oracle_toolchain = " ".into(); + assert!(bind_receipt(&no_tool, &run()).is_err()); + let mut r = run(); + r.output_digest = "zz".into(); + assert!(bind_receipt(&b, &r).is_err()); + } + + #[test] + fn tampered_artifacts_fail_binding_parse() { + let root = temp_dir("formal-tamper"); + let b = binding(&tree(&root)); + let mut r = bind_receipt(&b, &run()).unwrap(); + r.input_artifacts[3] = format!("blake3:{}", "e".repeat(64)); + assert!(parse_binding(&r).is_err()); + } +} diff --git a/crates/aien-proof/src/import.rs b/crates/aien-proof/src/import.rs index 5e20f6f3..5afe2ae7 100644 --- a/crates/aien-proof/src/import.rs +++ b/crates/aien-proof/src/import.rs @@ -268,6 +268,9 @@ pub fn build_receipt( if req.kind.starts_with("store-") { validate_store_profile(req, &assertions, result)?; } + if req.kind == crate::formal::KIND { + crate::formal::validate_formal_profile(req, &assertions, result)?; + } let tier = Tier::parse(&req.tier) .ok_or_else(|| format!("unknown qualification tier {:?}", req.tier))?; check_commit(&req.commit)?; diff --git a/crates/aien-proof/src/lib.rs b/crates/aien-proof/src/lib.rs index c42ba276..76733fd3 100644 --- a/crates/aien-proof/src/lib.rs +++ b/crates/aien-proof/src/lib.rs @@ -13,6 +13,7 @@ pub mod board; pub mod chain; pub mod evidence; pub mod fingerprint; +pub mod formal; pub mod gate; pub mod import; pub mod lease; diff --git a/crates/aien-proof/src/main.rs b/crates/aien-proof/src/main.rs index 02b49d1d..8e36b074 100644 --- a/crates/aien-proof/src/main.rs +++ b/crates/aien-proof/src/main.rs @@ -13,9 +13,10 @@ //! aien-proof gate status [--store DIR] [--gate FILE] //! aien-proof gate explain [--store DIR] [--gate FILE] //! aien-proof gate examples [--out DIR] +//! aien-proof formal digest|bind ... (formal oracle bindings, see formal.rs) use aien_proof::board::{mem_available, Board, Job, Outcome}; -use aien_proof::{chain, evidence, gate, import, ledger, workspace}; +use aien_proof::{chain, evidence, formal, gate, import, ledger, workspace}; use std::path::PathBuf; use std::process::{exit, Command}; use std::time::Instant; @@ -33,7 +34,8 @@ const USAGE: &str = "usage: aien-proof verify-chain [--store DIR] aien-proof gate status [--store DIR] [--gate FILE] aien-proof gate explain [--store DIR] [--gate FILE] - aien-proof gate examples [--out DIR]"; + aien-proof gate examples [--out DIR] + aien-proof formal digest|bind ... (see: aien-proof formal)"; struct Flags { values: Vec<(String, String)>, @@ -694,6 +696,136 @@ fn cmd_gate_examples(args: &[String]) -> i32 { } } +const FORMAL_USAGE: &str = "usage: + aien-proof formal digest [--base DIR] --invariant P --model P... --proof-source P... --covered P... + aien-proof formal bind [--base DIR] --invariant P --model P... --proof-source P... --covered P... + --oracle-toolchain TEXT --theorem NAME... --result-prefix PREFIX --output-file FILE + --repo URL --commit SHA --machine M --procedure PROC [--dirty] [--dep ID]... [--external-ref R]... [--store DIR] + paths are relative to --base (default: current directory); bind refuses output that + lacks the `inputs_digest:` line printed by `formal digest`, or whose last line is not + `PREFIX: PASS|FAIL|NOT_RUN`"; + +fn formal_inputs(flags: &Flags) -> Option { + let one = flags.get("--invariant")?; + Some(formal::FormalInputs { + base: flags + .get("--base") + .map(PathBuf::from) + .unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."))), + manifest: PathBuf::from(one), + model: flags + .all("--model") + .into_iter() + .map(PathBuf::from) + .collect(), + proof_source: flags + .all("--proof-source") + .into_iter() + .map(PathBuf::from) + .collect(), + covered: flags + .all("--covered") + .into_iter() + .map(PathBuf::from) + .collect(), + }) +} + +fn cmd_formal(args: &[String]) -> i32 { + let Some(sub) = args.first() else { + eprintln!("{FORMAL_USAGE}"); + return 2; + }; + let flags = Flags::parse(&args[1..], &["--dirty"]); + let Some(inputs) = formal_inputs(&flags) else { + eprintln!("{FORMAL_USAGE}"); + return 2; + }; + let run_digest = match inputs.run_digest() { + Ok(d) => d, + Err(e) => { + eprintln!("[aien-proof] {e}"); + return 1; + } + }; + match sub.as_str() { + "digest" => { + println!("{run_digest}"); + 0 + } + "bind" => formal_bind(&flags, &inputs, &run_digest), + _ => { + eprintln!("{FORMAL_USAGE}"); + 2 + } + } +} + +fn formal_bind(flags: &Flags, inputs: &formal::FormalInputs, run_digest: &str) -> i32 { + for required in [ + "--oracle-toolchain", + "--result-prefix", + "--output-file", + "--repo", + "--commit", + "--machine", + "--procedure", + ] { + if flags.get(required).is_none() { + eprintln!("missing {required}\n{FORMAL_USAGE}"); + return 2; + } + } + let refuse = |e: String| { + eprintln!("[aien-proof] formal bind refused: {e}"); + 1 + }; + let bytes = match std::fs::read(flags.get("--output-file").unwrap()) { + Ok(b) => b, + Err(e) => return refuse(format!("cannot read output file: {e}")), + }; + let text = String::from_utf8_lossy(&bytes); + if let Err(e) = formal::check_output_binds_inputs(&text, run_digest) { + return refuse(e); + } + let result = match formal::parse_result_line(&text, flags.get("--result-prefix").unwrap()) { + Ok(v) => v, + Err(e) => return refuse(e), + }; + let binding = match formal::compute_binding( + inputs, + flags.get("--oracle-toolchain").unwrap(), + &flags.all("--theorem"), + ) { + Ok(b) => b, + Err(e) => return refuse(e), + }; + let run = formal::FormalRun { + repo: flags.get("--repo").unwrap().to_string(), + commit: flags.get("--commit").unwrap().to_string(), + dirty: flags.has("--dirty"), + machine: flags.get("--machine").unwrap().to_string(), + procedure: flags.get("--procedure").unwrap().to_string(), + result, + output_digest: blake3::hash(&bytes).to_hex().to_string(), + dependencies: flags.all("--dep"), + external_refs: flags.all("--external-ref"), + timestamp: None, + }; + let receipt = match formal::bind_receipt(&binding, &run) { + Ok(r) => r, + Err(e) => return refuse(e), + }; + match evidence::store_receipt(&store_dir(flags), &receipt) { + Ok(id) => { + println!("{id}"); + println!("{}: {}", receipt.kind, receipt.result.as_str()); + verdict_code(receipt.result) + } + Err(e) => refuse(e), + } +} + fn main() { let args: Vec = std::env::args().skip(1).collect(); let code = match args.first().map(String::as_str) { @@ -706,6 +838,7 @@ fn main() { Some("qualify") => cmd_receipt_import(&args[1..]), Some("verify-chain") => cmd_verify_chain(&args[1..]), Some("gate") => cmd_gate(&args[1..]), + Some("formal") => cmd_formal(&args[1..]), _ => { eprintln!("{USAGE}"); 2