use crate::bridge_statement::{
self, BridgeKind, MAX_BRIDGE_STATEMENT_LEN, SourceEncoder, render_bridge_statement,
statement_is_root_qualified,
};
use crate::cache::{
ArtifactBuildCache, KeyMaterial as ArtifactCacheKeyMaterial, ModuleOutputCache,
};
use crate::lean_process::LeanRunner;
use crate::output::{self, Stream, Style};
use crate::prelude_cache::PristineWallCache;
use crate::{format, lean_gate, wall};
use serde_json::Value;
use sha2::{Digest, Sha256};
use std::path::{Path, PathBuf};
const AXIOM_WHITELIST: [&str; 3] = ["propext", "Classical.choice", "Quot.sound"];
const CHECKED_ROOT: &str = "AverCertChecker.checked";
const LAW_PIN_PREFIX: &str = "AverCertChecker.law_pin_";
const LAW_AUDIT_MARKER: &str = "AVER_LAW_AUDIT";
const BRIDGED_LAW_PIN_PREFIX: &str = "AverCertChecker.bridged_law_pin_";
const LAW_BRIDGE_AUDIT_MARKER: &str = "AVER_LAW_BRIDGE_AUDIT";
const LAW_BRIDGED_COROLLARY_SUFFIX: &str = "_bridged";
const BRIDGE_PIN_PREFIX: &str = "AverCertChecker.bridge_pin_";
const LAW_STATEMENT_PREFIX: &str = "AverCertChecker.law_statement_";
const BRIDGE_STATEMENT_PREFIX: &str = "AverCertChecker.bridge_statement_";
const BRIDGE_AUDIT_MARKER: &str = "AVER_BRIDGE_AUDIT";
const BRIDGE_NAMESPACE: &str = "AverCert.Bridge";
const BRIDGE_COROLLARY_SUFFIX: &str = "_certified";
const TOOLCHAIN_ROOTS: [&str; 4] = ["Init", "Lake", "Lean", "Std"];
const FRESH_REPLAY_ARGS: [&str; 4] = ["env", "leanchecker", "--fresh", WITNESS_MODULE];
const WITNESS_MODULE: &str = "CheckerWitness";
const REPORT_PIN_PREFIX: &str = "AverCertChecker.report_pin_";
const AUDIT_DECLINE_MARKER: &str = "AVER_AUDIT_DECLINE";
const AUDIT_OK_MARKER: &str = "AVER_AUDIT_OK";
const PROOF_BUILD_PHASE: &str = "certificate proof build";
pub const ARTIFACT_DECODE_LINE: &str =
"artifact-check: exact bytes and manifest accepted by the checker-owned Lean predicate";
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Verdict {
Certified {
summary: String,
faces: Vec<String>,
laws: Vec<String>,
bridged_laws: Vec<String>,
source_bridges: Vec<String>,
},
NoExports(String),
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum CheckVerdict {
Checked {
summary: String,
faces: Vec<String>,
laws: Vec<String>,
bridged_laws: Vec<String>,
source_bridges: Vec<String>,
},
NoExports(String),
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Explanation {
Certified,
NoExports,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum ReplayMode {
Fresh,
Paranoid,
TrustBuiltOleans,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Kernels {
Lean,
Paranoid,
}
impl Kernels {
fn replay_mode(self) -> ReplayMode {
match self {
Self::Lean => ReplayMode::Fresh,
Self::Paranoid => ReplayMode::Paranoid,
}
}
}
struct CertifiedExport {
name: String,
policy: String,
face: String,
certified_model: String,
}
#[derive(Debug)]
struct LawOutcome {
label: String,
offending: Vec<String>,
}
#[derive(Debug)]
struct BridgeOutcome {
export: String,
model: String,
kind: BridgeKind,
statement: String,
offending: Vec<String>,
}
struct TrustedReport {
exports: Vec<CertifiedExport>,
laws: Vec<LawOutcome>,
bridged_laws: Vec<LawOutcome>,
source_bridges: Vec<BridgeOutcome>,
contracts: Vec<String>,
target: String,
profile: String,
abi: String,
artifact_hash: String,
manifest: Value,
}
struct CertifiedCandidate {
name: String,
class: String,
facets: Vec<String>,
policy: String,
policy_lean: &'static str,
termination_lean: String,
}
#[derive(Clone, Copy)]
enum StringHostRole {
Eq,
Concat,
}
type HostRoleTable = (
Option<u32>,
Option<u32>,
Option<u32>,
Option<u32>,
Option<u32>,
Option<u32>,
Option<u32>,
Option<u32>,
);
struct ManifestIdentity {
target: String,
profile: String,
abi: String,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum ArtifactTarget {
WasmGc,
Wasip2,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct Wasip2EnvelopeDeclaration {
inner: format::Wasip2ComponentEnvelopeDeclaration,
}
#[derive(Debug)]
struct PreparedArtifact<'a> {
artifact_hash: String,
target_artifact_bytes: &'a [u8],
core_module_bytes: &'a [u8],
lifted_core_exports: Vec<String>,
}
struct LawCandidate {
label: String,
theorem: String,
statement: String,
corollary: String,
prefix: String,
bridges: Vec<usize>,
}
struct SourceBridgeCandidate {
export: String,
corollary: String,
model: String,
kind: BridgeKind,
statement: String,
params: Vec<SourceEncoder>,
result: SourceEncoder,
}
struct Candidates {
certified: Vec<CertifiedCandidate>,
laws: Vec<LawCandidate>,
source_bridges: Vec<SourceBridgeCandidate>,
contracts: Vec<String>,
declared_uncertified: Vec<(String, String)>,
capabilities: Vec<(String, String)>,
start: Option<u32>,
host_role_table: Option<HostRoleTable>,
string_host_roles: Vec<(u32, StringHostRole)>,
target: String,
profile: String,
abi: String,
wasip2_component_envelope: Option<format::Wasip2ComponentEnvelopeDeclaration>,
}
pub fn verify(artifact: &Path, cert_dir: &Path) -> Result<Verdict, String> {
verify_with(artifact, cert_dir, Kernels::Lean)
}
pub fn verify_with(artifact: &Path, cert_dir: &Path, kernels: Kernels) -> Result<Verdict, String> {
let report = trusted_check(artifact, cert_dir, kernels.replay_mode())?;
let summary = summarize_report(artifact, report, "certified");
if summary.count == 0 {
Ok(Verdict::NoExports(summary.text))
} else {
Ok(Verdict::Certified {
summary: summary.text,
faces: summary.faces,
laws: summary.uncredited_laws,
bridged_laws: summary.uncredited_bridged_laws,
source_bridges: summary.uncredited_bridges,
})
}
}
pub fn check(artifact: &Path, cert_dir: &Path) -> Result<CheckVerdict, String> {
let report = trusted_check(artifact, cert_dir, ReplayMode::TrustBuiltOleans)?;
let summary = summarize_report(artifact, report, "checked");
if summary.count == 0 {
Ok(CheckVerdict::NoExports(summary.text))
} else {
Ok(CheckVerdict::Checked {
summary: summary.text,
faces: summary.faces,
laws: summary.uncredited_laws,
bridged_laws: summary.uncredited_bridged_laws,
source_bridges: summary.uncredited_bridges,
})
}
}
struct ReportSummary {
text: String,
faces: Vec<String>,
uncredited_laws: Vec<String>,
uncredited_bridged_laws: Vec<String>,
uncredited_bridges: Vec<String>,
count: usize,
}
fn summarize_report(artifact: &Path, report: TrustedReport, status: &'static str) -> ReportSummary {
let count = report.exports.len();
let has_total = report
.exports
.iter()
.any(|export| export.policy == "simulatesModelTotally");
let has_partial = report
.exports
.iter()
.any(|export| export.policy == "simulatesModel");
let level = match (has_partial, has_total) {
(true, true) => "mixed L1/L3",
(false, true) => "L3",
_ => "L1",
};
let law_clause = if report.laws.is_empty() {
String::new()
} else {
let credited = report
.laws
.iter()
.filter(|law| law.offending.is_empty())
.count();
format!("; law-claims: {credited} of {} credited", report.laws.len())
};
let bridged_law_clause = if report.bridged_laws.is_empty() {
String::new()
} else {
let credited = report
.bridged_laws
.iter()
.filter(|law| law.offending.is_empty())
.count();
format!(
"; bridged-laws: {credited} of {} credited",
report.bridged_laws.len()
)
};
let bridge_clause = if report.source_bridges.is_empty() {
String::new()
} else {
let credited = report
.source_bridges
.iter()
.filter(|bridge| bridge.offending.is_empty())
.count();
format!(
"; source-bridges: {credited} of {} credited",
report.source_bridges.len()
)
};
let text = format!(
"{} ({} {status} export{}, level {}{law_clause}{bridged_law_clause}{bridge_clause})",
shown_path(artifact),
count,
if count == 1 { "" } else { "s" },
level,
);
let uncredited_laws = report
.laws
.iter()
.filter(|law| !law.offending.is_empty())
.map(|law| {
format!(
"law-claim not credited: {} (proof depends on {})",
display_safe(&law.label),
display_safe(&law.offending.join(", ")),
)
})
.collect();
let uncredited_bridged_laws = report
.bridged_laws
.iter()
.filter(|law| !law.offending.is_empty())
.map(|law| {
format!(
"bridged law-claim not credited: {} (proof depends on {})",
display_safe(&law.label),
display_safe(&law.offending.join(", ")),
)
})
.collect();
let uncredited_bridges = report
.source_bridges
.iter()
.filter(|bridge| !bridge.offending.is_empty())
.map(|bridge| {
format!(
"source-bridge not credited: {} (proof depends on {})",
display_safe(&bridge.export),
display_safe(&bridge.offending.join(", ")),
)
})
.collect();
let faces = report
.exports
.into_iter()
.map(|export| {
format!(
"{} policy: {} {}",
export.name, export.policy, export.face
)
})
.collect();
ReportSummary {
text,
faces,
uncredited_laws,
uncredited_bridged_laws,
uncredited_bridges,
count,
}
}
const PARALLEL_REPLAY_ENV: &str = "AVER_CERT_PARALLEL_REPLAY";
fn kernel_replay_args(mode: ReplayMode) -> Option<Vec<String>> {
let override_binary = std::env::var(PARALLEL_REPLAY_ENV).ok();
replay_args_for(mode, override_binary.as_deref())
}
fn replay_args_for(mode: ReplayMode, override_binary: Option<&str>) -> Option<Vec<String>> {
match mode {
ReplayMode::Fresh => Some(match override_binary {
Some(binary) if !binary.trim().is_empty() => vec![
"env".to_string(),
binary.to_string(),
WITNESS_MODULE.to_string(),
CHECKED_ROOT.to_string(),
"replay".to_string(),
"8".to_string(),
"32".to_string(),
],
_ => FRESH_REPLAY_ARGS.iter().map(|a| (*a).to_string()).collect(),
}),
ReplayMode::Paranoid | ReplayMode::TrustBuiltOleans => None,
}
}
const WITNESS_NAMESPACE: &str = "AverCertChecker";
const PARANOID_EXPORT_FILE: &str = "witness-closure.ndjson";
const SORRY_FREE_EXPORT_FILE: &str = "witness-closure-sorry-free.ndjson";
const NANODA_CONFIG_FILE: &str = "nanoda.json";
fn paranoid_replay(
lean: &LeanRunner,
build_dir: &Path,
kernels: &[crate::kernels::Kernel],
credited: &[String],
) -> Result<(), String> {
let started = std::time::Instant::now();
let exported = lean
.run_lake(
build_dir,
"paranoid export",
&["env", crate::kernels::EXPORTER, WITNESS_MODULE],
)
.map_err(|error| error.to_string())?;
report_step_timing("paranoid export", started.elapsed(), &[]);
if !exported.status.success() {
return Err(format!(
"certificate failed paranoid kernel replay: `{}` did not export the witness closure:\n{}",
crate::kernels::EXPORTER,
tail(&String::from_utf8_lossy(&exported.stderr), 30)
));
}
if !crate::kernels::export_mentions(&exported.stdout, WITNESS_NAMESPACE) {
return Err(
"certificate failed paranoid kernel replay: the export of the witness closure \
does not contain the witness"
.to_string(),
);
}
let export_path = build_dir.join(PARANOID_EXPORT_FILE);
let config_path = build_dir.join(NANODA_CONFIG_FILE);
let export_text = export_path.to_string_lossy().into_owned();
let config_text = config_path.to_string_lossy().into_owned();
std::fs::write(&export_path, &exported.stdout)
.map_err(|error| format!("cannot write the paranoid export: {error}"))?;
let mut sorry_free_text = export_text.clone();
let sorry_free_path = build_dir.join(SORRY_FREE_EXPORT_FILE);
let started = std::time::Instant::now();
let notice =
crate::sorry_free_export::write_for(kernels, &exported.stdout, &sorry_free_path, credited)
.map_err(|error| format!("certificate failed paranoid kernel replay: {error}"))?;
report_step_timing("sorry-free export", started.elapsed(), &[]);
if let Some(notice) = notice {
sorry_free_text = sorry_free_path.to_string_lossy().into_owned();
output::line(
Stream::Out,
"paranoid:",
Style::Yellow,
¬ice,
Style::Plain,
);
}
drop(exported);
for kernel in kernels {
let export = kernel.export(&export_text, &sorry_free_text);
if kernel.input == crate::kernels::KernelInput::NanodaConfig {
std::fs::write(&config_path, crate::kernels::nanoda_config(export))
.map_err(|error| format!("cannot write the nanoda configuration: {error}"))?;
}
let phase = format!("{} kernel", kernel.name);
let mut arguments = vec!["env".to_string(), kernel.binary.to_string()];
arguments.extend(kernel.arguments(export, &config_text));
let arguments: Vec<&str> = arguments.iter().map(String::as_str).collect();
let replayed = run_lake(lean, build_dir, &phase, &arguments)?;
if !replayed.status.success() {
return Err(format!(
"certificate failed paranoid kernel replay: the {} kernel (`{}`) did not accept \
the witness closure; a missing kernel binary is a failure, not a skip:\n{}",
kernel.name,
kernel.binary,
tail(&replayed.combined, 30)
));
}
}
let _ = std::fs::remove_file(&export_path);
let _ = std::fs::remove_file(&sorry_free_path);
Ok(())
}
fn trusted_check(
artifact: &Path,
cert_dir: &Path,
replay_mode: ReplayMode,
) -> Result<TrustedReport, String> {
let bytes = std::fs::read(artifact)
.map_err(|error| format!("cannot read artifact {}: {error}", shown_path(artifact)))?;
let manifest = read_manifest(cert_dir)?;
let schema_version = manifest_u64(&manifest, "schema_version")?;
if schema_version != format::CERT_SCHEMA_VERSION as u64 {
return Err(format!(
"unsupported certificate schema_version {schema_version}; this checker accepts {}",
format::CERT_SCHEMA_VERSION
));
}
let identity = read_manifest_identity(&manifest)?;
let artifact_target = require_supported_identity(&identity)?;
let target_envelope = read_artifact_target_envelope(artifact_target, &manifest)?;
let PreparedArtifact {
artifact_hash: actual_hash,
target_artifact_bytes,
core_module_bytes,
lifted_core_exports,
} = prepare_artifact_for_target(artifact_target, &bytes, target_envelope)?;
let pinned_hash = manifest_str(&manifest, "wasm_sha256")?;
if pinned_hash != actual_hash {
return Err(format!(
"artifact hash mismatch: {} hashes to {actual_hash}, certificate pins {pinned_hash}",
shown_path(artifact)
));
}
let format_object = manifest
.get("format")
.and_then(Value::as_object)
.ok_or_else(|| "cert-manifest.json is missing object field `format`".to_string())?;
let format_version = format_object
.get("version")
.and_then(Value::as_u64)
.ok_or_else(|| "cert-manifest.json `format.version` must be an integer".to_string())?;
if format_version != format::FORMAT_VERSION as u64 {
return Err(format!(
"unsupported certificate format version {format_version}; this checker accepts {}",
format::FORMAT_VERSION
));
}
let wall_id = format_object
.get("wall_id")
.and_then(Value::as_str)
.ok_or_else(|| "cert-manifest.json `format.wall_id` must be a string".to_string())?;
let selected_wall = wall::resolve(wall_id).ok_or_else(|| {
format!(
"unsupported certificate wall `{}`; no embedded wall matches",
display_safe(wall_id)
)
})?;
let artifact_root = manifest_str(&manifest, "artifact_certificate_root")?;
if artifact_root != format::ARTIFACT_CERTIFICATE_ROOT {
return Err(format!(
"artifact certificate root mismatch: certificate pins {}, checker expects {}",
display_safe(artifact_root),
format::ARTIFACT_CERTIFICATE_ROOT
));
}
let candidates = read_candidates(&manifest, identity, target_envelope.map(|env| env.inner))?;
if let Some(certified) = candidates
.certified
.iter()
.find(|candidate| lifted_core_exports.contains(&candidate.name))
{
return Err(format!(
"wasip2 component lifts certified core export `{}`; Aver lifts only its world entry points, so this checker admits no component signature for a certified export",
display_safe(&certified.name)
));
}
let lean = LeanRunner::new(selected_wall.toolchain)?;
let stage_started = std::time::Instant::now();
let build = assemble_build(
cert_dir,
&actual_hash,
core_module_bytes,
target_artifact_bytes,
selected_wall,
lean.memory_limit_mb(),
)?;
let caches_allowed = replay_mode == ReplayMode::TrustBuiltOleans;
if !caches_allowed && crate::cache::any_cache_configured() {
output::plain(
Stream::Err,
"note: aver-cert verify ignores AVER_CERT_DATA_CACHE and AVER_CERT_PRELUDE_CACHE; \
only `check` uses a build cache",
);
}
let cache_pins = [("wasm_sha256", pinned_hash), ("wall_id", wall_id)];
let mut cache = ArtifactBuildCache::prepare(
&build.path,
&ArtifactCacheKeyMaterial {
schema_version,
pinned_sha256: &cache_pins,
toolchain_version: selected_wall.toolchain.trim(),
},
caches_allowed,
);
let data_cache_hit = cache.was_hit();
report_step_timing("staging and data cache", stage_started.elapsed(), &[]);
let wall_cache_started = std::time::Instant::now();
let mut wall_cache = if data_cache_hit || !caches_allowed {
PristineWallCache::disabled()
} else {
PristineWallCache::prepare(&build.path, selected_wall, &lean)
};
report_step_timing("wall cache restore", wall_cache_started.elapsed(), &[]);
let module_cache_started = std::time::Instant::now();
let module_cache = if data_cache_hit || !caches_allowed {
ModuleOutputCache::disabled()
} else {
let wall_sources: Vec<&str> = selected_wall.sources.iter().map(|s| s.name).collect();
ModuleOutputCache::prepare(
&build.path,
&[
("wall_id", wall_id),
("toolchain_version", selected_wall.toolchain.trim()),
("schema_version", &schema_version.to_string()),
],
&wall_sources,
)
};
report_step_timing(
&format!("module cache restore ({} modules)", module_cache.restored()),
module_cache_started.elapsed(),
&[],
);
let mut data_build = run_lake(&lean, &build.path, PROOF_BUILD_PHASE, &["build"])?;
if !data_build.status.success()
&& (data_cache_hit || wall_cache.was_seeded() || module_cache.restored() > 0)
{
if data_cache_hit {
cache.invalidate(&build.path);
} else {
wall_cache.clear_build(&build.path);
}
data_build = run_lake(&lean, &build.path, PROOF_BUILD_PHASE, &["build"])?;
if data_build.status.success() && wall_cache.was_seeded() {
wall_cache.evict();
}
}
if !data_build.status.success() {
return Err(format!(
"certificate data did not build:\n{}",
surface_build_failure(&data_build.combined, 30)
));
}
cache.publish(&build.path);
module_cache.publish(&build.path);
let witness = checker_witness(&actual_hash, &candidates);
std::fs::write(build.path.join("CheckerWitness.lean"), witness)
.map_err(|error| format!("cannot write checker witness: {error}"))?;
let elaborated = run_lake(
&lean,
&build.path,
"artifact witness check",
&[
"env",
"lean",
"-o",
".lake/build/lib/lean/CheckerWitness.olean",
"CheckerWitness.lean",
],
)?;
if !elaborated.status.success() {
return Err(format!(
"certificate does not bind to this artifact: the checker-owned Lean witness failed:\n{}",
tail(&elaborated.combined, 30)
));
}
std::fs::write(
build.path.join("CheckerAudit.lean"),
checker_audit(&candidates, &build.package_roots),
)
.map_err(|error| format!("cannot write checker audit: {error}"))?;
let audited = run_lake(
&lean,
&build.path,
"artifact audit",
&["env", "lean", "--run", "CheckerAudit.lean"],
)?;
if let Some(reason) = audited
.combined
.lines()
.find_map(|line| line.trim().strip_prefix(AUDIT_DECLINE_MARKER))
{
return Err(format!(
"certificate declined by the checker audit:{}",
display_safe(reason)
));
}
if !audited.status.success()
|| !audited
.combined
.lines()
.any(|line| line.trim() == AUDIT_OK_MARKER)
{
return Err(format!(
"the checker audit did not complete:\n{}",
tail(&audited.combined, 30)
));
}
let laws = parse_law_audits(&audited.combined, &candidates.laws)?;
let bridged_laws = parse_bridged_law_audits(&audited.combined, &candidates.laws)?;
let source_bridges = parse_bridge_audits(&audited.combined, &candidates.source_bridges)?;
if replay_mode == ReplayMode::Paranoid {
let credited = credited_roots(&laws, &bridged_laws, &source_bridges);
paranoid_replay(
&lean,
&build.path,
&crate::kernels::PARANOID_KERNELS,
&credited,
)?;
} else if let Some(replay_args) = kernel_replay_args(replay_mode) {
let replay_args: Vec<&str> = replay_args.iter().map(String::as_str).collect();
let replayed = run_lake(&lean, &build.path, "final kernel replay", &replay_args)?;
if !replayed.status.success() {
return Err(format!(
"certificate failed fresh-environment kernel replay:\n{}",
tail(&replayed.combined, 30)
));
}
}
let exports = candidates
.certified
.iter()
.map(|candidate| CertifiedExport {
name: candidate.name.clone(),
policy: candidate.policy.clone(),
face: report_face(candidate),
certified_model: certified_model_line(candidate, &source_bridges),
})
.collect();
Ok(TrustedReport {
exports,
laws,
bridged_laws,
source_bridges,
contracts: candidates.contracts,
target: candidates.target,
profile: candidates.profile,
abi: candidates.abi,
artifact_hash: actual_hash,
manifest,
})
}
fn credited_roots(
laws: &[LawOutcome],
bridged_laws: &[LawOutcome],
source_bridges: &[BridgeOutcome],
) -> Vec<String> {
let pins = |prefix: &str, clean: Vec<bool>| -> Vec<String> {
(clean.into_iter().enumerate())
.filter(|(_, clean)| *clean)
.map(|(index, _)| format!("{prefix}{index}"))
.collect()
};
let clean = |laws: &[LawOutcome]| laws.iter().map(|law| law.offending.is_empty()).collect();
std::iter::once(CHECKED_ROOT.to_string())
.chain((0..REPORT_PIN_COUNT).map(|index| format!("{REPORT_PIN_PREFIX}{index}")))
.chain(pins(LAW_PIN_PREFIX, clean(laws)))
.chain(pins(BRIDGED_LAW_PIN_PREFIX, clean(bridged_laws)))
.chain(pins(
BRIDGE_PIN_PREFIX,
(source_bridges.iter())
.map(|bridge| bridge.offending.is_empty())
.collect(),
))
.collect()
}
fn report_face(candidate: &CertifiedCandidate) -> String {
if candidate.facets.is_empty() {
format!("class: {}", candidate.class)
} else {
format!(
"class: {} ({})",
candidate.class,
candidate.facets.join(", ")
)
}
}
fn certified_model_line(candidate: &CertifiedCandidate, bridges: &[BridgeOutcome]) -> String {
let credited = bridges
.iter()
.find(|bridge| bridge.export == candidate.name && bridge.offending.is_empty());
match credited {
Some(bridge) => match bridge.kind {
BridgeKind::Exact => format!(
"model: plan ≡ {} (credited source-bridge; see SOURCE-BRIDGES)",
display_safe(&bridge.model)
),
BridgeKind::Adequate => format!(
"model: plan ≡ {} wherever the plan returns (credited adequate \
source-bridge, not a totality claim; see SOURCE-BRIDGES)",
display_safe(&bridge.model)
),
},
None => "model: plan (the export's optimized MIR body)".to_string(),
}
}
fn bridged_law_indices(laws: &[LawCandidate]) -> Vec<usize> {
laws.iter()
.enumerate()
.filter(|(_, law)| !law.bridges.is_empty())
.map(|(index, _)| index)
.collect()
}
fn lean_nat(value: impl std::fmt::Display) -> String {
format!("(nat_lit {value})")
}
fn lean_int(value: i64) -> String {
if value >= 0 {
format!("(_root_.Int.ofNat {})", lean_nat(value))
} else {
format!(
"(_root_.Int.negSucc {})",
lean_nat(value.unsigned_abs() - 1)
)
}
}
fn checker_witness(sha: &str, candidates: &Candidates) -> String {
let bridged_law_indices = bridged_law_indices(&candidates.laws);
let names = lean_str_list(
&candidates
.certified
.iter()
.map(|candidate| candidate.name.clone())
.collect::<Vec<_>>(),
);
let report_entries = lean_string_pair_list(
&candidates
.certified
.iter()
.map(|candidate| (candidate.name.clone(), candidate.class.clone()))
.collect::<Vec<_>>(),
);
let report_facets = format!(
"[{}]",
candidates
.certified
.iter()
.map(|candidate| format!(
"(\"{}\", {})",
candidate.name,
lean_str_list(&candidate.facets)
))
.collect::<Vec<_>>()
.join(", ")
);
let policies = format!(
"[{}]",
candidates
.certified
.iter()
.map(|candidate| candidate.policy_lean)
.collect::<Vec<_>>()
.join(", ")
);
let terminations = format!(
"[{}]",
candidates
.certified
.iter()
.map(|candidate| candidate.termination_lean.as_str())
.collect::<Vec<_>>()
.join(", ")
);
let contracts = lean_str_list(&candidates.contracts);
let declared = lean_string_pair_list(&candidates.declared_uncertified);
let capabilities = lean_string_pair_list(&candidates.capabilities);
let start = lean_option_nat(candidates.start);
let roles = match candidates.host_role_table {
Some((
box_role,
add_role,
mul_role,
sub_role,
to_index_role,
cmp_role,
eq_role,
divmod_role,
)) => {
format!(
"_root_.Option.some ({{ box := {}, add := {}, mul := {}, sub := {}, toIndex := {}, \
cmp := {}, eq := {}, divmod := {} }} : \
_root_.CertDecode.AddSub.Roles)",
lean_option_nat(box_role),
lean_option_nat(add_role),
lean_option_nat(mul_role),
lean_option_nat(sub_role),
lean_option_nat(to_index_role),
lean_option_nat(cmp_role),
lean_option_nat(eq_role),
lean_option_nat(divmod_role),
)
}
None => "(_root_.Option.none : _root_.Option _root_.CertDecode.AddSub.Roles)".to_string(),
};
let string_roles = format!(
"[{}]",
candidates
.string_host_roles
.iter()
.map(|(index, role)| {
let role = match role {
StringHostRole::Eq => ".eq",
StringHostRole::Concat => ".concat",
};
format!("({}, {role})", lean_nat(index))
})
.collect::<Vec<_>>()
.join(", ")
);
let wasip2_component_envelope =
lean_wasip2_component_envelope(candidates.wasip2_component_envelope);
let law_import = if candidates.laws.is_empty() {
String::new()
} else {
"import Laws\n".to_string()
};
let bridge_import = if candidates.source_bridges.is_empty() {
String::new()
} else {
"import Bridge\n".to_string()
};
let mut law_pins = String::new();
for (index, law) in candidates.laws.iter().enumerate() {
law_pins.push_str(&format!(
"def _root_.{LAW_STATEMENT_PREFIX}{index} : Prop :=\n ("
));
law_pins.push_str(&law.statement);
law_pins.push_str(")\n\n");
law_pins.push_str(&format!(
"theorem _root_.{LAW_PIN_PREFIX}{index} :\n \
_root_.{LAW_STATEMENT_PREFIX}{index} ∧ \
(_root_.AverCert.Schema.Holds _root_.AverCert.manifest)"
));
law_pins.push_str(" :=\n _root_.AverCert.Laws.");
law_pins.push_str(&law.corollary);
law_pins.push_str("\n\n");
if !law.bridges.is_empty() {
let bridged_index = bridged_law_indices
.iter()
.position(|at| *at == index)
.expect("every bridged law is enumerated");
law_pins.push_str(&format!(
"theorem _root_.{BRIDGED_LAW_PIN_PREFIX}{bridged_index} :\n \
_root_.{LAW_STATEMENT_PREFIX}{index} ∧ \
(_root_.AverCert.Schema.Holds _root_.AverCert.manifest)"
));
for bridge in &law.bridges {
law_pins.push_str(&format!(
" ∧\n _root_.{BRIDGE_STATEMENT_PREFIX}{bridge}"
));
}
law_pins.push_str(" :=\n _root_.AverCert.Laws.");
law_pins.push_str(&law.corollary);
law_pins.push_str(LAW_BRIDGED_COROLLARY_SUFFIX);
law_pins.push_str("\n\n");
}
}
let mut bridge_pins = String::new();
for (index, bridge) in candidates.source_bridges.iter().enumerate() {
bridge_pins.push_str(&format!(
"def _root_.{BRIDGE_STATEMENT_PREFIX}{index} : Prop :=\n ("
));
bridge_pins.push_str(&bridge.statement);
bridge_pins.push_str(")\n\n");
bridge_pins.push_str(&format!(
"theorem _root_.{BRIDGE_PIN_PREFIX}{index} :\n \
_root_.{BRIDGE_STATEMENT_PREFIX}{index} ∧ \
(_root_.AverCert.Schema.Holds _root_.AverCert.manifest) :=\n _root_."
));
bridge_pins.push_str(&bridge.corollary);
bridge_pins.push_str("\n\n");
}
let data = "_root_.AverCert.Artifact.data";
let manifest = "_root_.AverCert.manifest";
let datum =
|field: &str| format!("(_root_.AverCert.AcceptedArtifact.ArtifactData.{field} {data})");
let subject = |field: &str| {
format!(
"(_root_.AverCert.Schema.Subject.{field} \
(_root_.AverCert.Schema.Manifest.subject {manifest}))"
)
};
let obligations = |field: &str| {
format!(
"_root_.List.map _root_.AverCert.Schema.Obligation.{field} \
(_root_.AverCert.Schema.Manifest.obligations {manifest})"
)
};
let report_pins: Vec<(String, &str)> = vec![
(
format!(
"{} = _root_.AverCert.ArtifactBytes.modBytes",
datum("modBytes")
),
"rfl",
),
(
format!("{} = _root_.AverCert.ArtifactBytes.modLen", datum("modLen")),
"rfl",
),
(format!("{} = {manifest}", datum("manifest")), "rfl"),
(
format!(
"{} = {wasip2_component_envelope}",
datum("wasip2ComponentEnvelope")
),
"rfl",
),
(format!("{} = \"{sha}\"", subject("artifactHash")), "rfl"),
(
format!(
"{} = \"{}\"",
subject("artifactRoot"),
format::ARTIFACT_CERTIFICATE_ROOT
),
"rfl",
),
(format!("{} = {names}", obligations("export_")), "rfl"),
(format!("{} = {names}", subject("exports")), "rfl"),
(
format!("_root_.AverCert.ClaimAxes.reportEntries {data} = {report_entries}"),
KERNEL_REPORT_PROOF,
),
(
format!("_root_.AverCert.ClaimAxes.reportFacets {data} = {report_facets}"),
FACETS_REPORT_PROOF,
),
(
format!("{} = {policies}", obligations("policy")),
POLICIES_REPORT_PROOF,
),
(
format!("{} = {terminations}", obligations("termination?")),
TERMINATIONS_REPORT_PROOF,
),
(format!("{} = {contracts}", subject("contracts")), "rfl"),
(
format!("{} = {declared}", subject("declaredUncertified")),
"rfl",
),
(
format!("{} = {capabilities}", subject("capabilities")),
"rfl",
),
(format!("{} = {start}", subject("start")), "rfl"),
(format!("{} = {roles}", subject("hostRoleTable")), "rfl"),
(
format!("{} = {string_roles}", subject("stringHostRoles")),
"rfl",
),
(
format!("{} = \"{}\"", subject("target"), candidates.target),
"rfl",
),
(
format!("{} = \"{}\"", subject("profile"), candidates.profile),
"rfl",
),
(
format!("{} = \"{}\"", subject("abi"), candidates.abi),
"rfl",
),
];
assert_eq!(
report_pins.len(),
REPORT_PIN_COUNT,
"the audit walks exactly the report pins the witness writes"
);
let mut report = String::new();
for (index, (statement, proof)) in report_pins.iter().enumerate() {
if *proof != "rfl" {
report.push_str("set_option maxHeartbeats 4000000 in\n");
}
report.push_str(&format!(
"theorem _root_.{REPORT_PIN_PREFIX}{index} :\n {statement} :=\n {proof}\n\n"
));
}
format!(
"-- Authored by aver-cert; never accepted from the certificate.\n\
import AcceptedArtifact\n\
import ArtifactBytes\n\
import Manifest\n\
import Artifact\n\
{law_import}\
{bridge_import}\
import ArtifactCertificate\n\n\
set_option maxRecDepth 200000\n\n\
set_option autoImplicit false\n\n\
{bridge_pins}\
{law_pins}\
{report}\
theorem _root_.{CHECKED_ROOT} :\n \
_root_.AverCert.AcceptedArtifact.accepted _root_.AverCert.Artifact.data :=\n \
_root_.AverCert.Artifact.certificate\n"
)
}
const KERNEL_REPORT_PROOF: &str = "by first\n \
| exact _root_.AverCert.Artifact.report_entries\n \
| decide +kernel\n \
| rfl";
const FACETS_REPORT_PROOF: &str = "_root_.Eq.trans\n \
(_root_.AverCert.ClaimAxes.reportFacets_eq_fast _root_.AverCert.Artifact.data)\n \
(by first | exact _root_.AverCert.Artifact.report_facets | decide +kernel)";
const POLICIES_REPORT_PROOF: &str = "_root_.Eq.trans\n \
(_root_.AverCert.ClaimAxes.map_policy_of_derived (artifact := _root_.AverCert.Artifact.data)\n \
(_root_.And.left (_root_.And.right (_root_.And.right (_root_.And.right\n \
_root_.AverCert.Artifact.certificate)))))\n \
(by first | exact _root_.AverCert.Artifact.report_policies | decide +kernel)";
const TERMINATIONS_REPORT_PROOF: &str = "_root_.Eq.trans\n \
(_root_.AverCert.ClaimAxes.map_termination_of_derived (artifact := _root_.AverCert.Artifact.data)\n \
(_root_.And.left (_root_.And.right (_root_.And.right (_root_.And.right\n \
_root_.AverCert.Artifact.certificate)))))\n \
(by first | exact _root_.AverCert.Artifact.report_terminations | decide +kernel)";
const REPORT_PIN_COUNT: usize = 21;
fn lean_name_list(names: &[String]) -> String {
format!(
"[{}]",
names
.iter()
.map(|name| format!("`{name}"))
.collect::<Vec<_>>()
.join(", ")
)
}
fn checker_audit(candidates: &Candidates, package_modules: &[String]) -> String {
let strict_roots: Vec<String> = std::iter::once(CHECKED_ROOT.to_string())
.chain((0..REPORT_PIN_COUNT).map(|index| format!("{REPORT_PIN_PREFIX}{index}")))
.collect();
let law_roots: Vec<String> = (0..candidates.laws.len())
.map(|index| format!("{LAW_PIN_PREFIX}{index}"))
.collect();
let bridged_law_roots: Vec<String> = (0..bridged_law_indices(&candidates.laws).len())
.map(|index| format!("{BRIDGED_LAW_PIN_PREFIX}{index}"))
.collect();
let bridge_roots: Vec<String> = (0..candidates.source_bridges.len())
.map(|index| format!("{BRIDGE_PIN_PREFIX}{index}"))
.collect();
let law_model_uses = law_model_uses(candidates);
let allowed: Vec<String> = AXIOM_WHITELIST
.iter()
.map(|name| name.to_string())
.collect();
let mut records: Vec<(String, Vec<String>)> = Vec::new();
let mut sums: Vec<(String, Vec<(String, usize)>)> = Vec::new();
for bridge in &candidates.source_bridges {
for encoder in bridge.params.iter().chain(std::iter::once(&bridge.result)) {
collect_encoder_shapes(encoder, &mut records, &mut sums);
}
}
let records = format!(
"[{}]",
records
.iter()
.map(|(ty, fields)| format!("(`{ty}, {})", lean_name_list(fields)))
.collect::<Vec<_>>()
.join(", ")
);
let sums = format!(
"[{}]",
sums.iter()
.map(|(ty, ctors)| format!(
"(`{ty}, [{}])",
ctors
.iter()
.map(|(ctor, fields)| format!("(`{ctor}, {})", lean_nat(fields)))
.collect::<Vec<_>>()
.join(", ")
))
.collect::<Vec<_>>()
.join(", ")
);
AUDIT_TEMPLATE
.replace("@RECORDS@", &records)
.replace("@SUMS@", &sums)
.replace("@PACKAGE_MODULES@", &lean_name_list(package_modules))
.replace(
"@WALL_ROOTS@",
&lean_name_list(&WALL_NAMESPACE_ROOTS.map(str::to_string)),
)
.replace(
"@PACKAGE_AVERCERT_CHILDREN@",
&lean_name_list(&PACKAGE_AVERCERT_CHILDREN.map(str::to_string)),
)
.replace(
"@PACKAGE_AVERCERT_LEAVES@",
&lean_name_list(&PACKAGE_AVERCERT_LEAVES.map(str::to_string)),
)
.replace("@ALLOWED@", &lean_name_list(&allowed))
.replace("@STRICT_ROOTS@", &lean_name_list(&strict_roots))
.replace("@LAW_MODEL_USES@", &law_model_uses)
.replace("@LAW_ROOTS@", &lean_name_list(&law_roots))
.replace("@BRIDGED_LAW_ROOTS@", &lean_name_list(&bridged_law_roots))
.replace("@BRIDGE_ROOTS@", &lean_name_list(&bridge_roots))
.replace("@LAW_MARKER@", LAW_AUDIT_MARKER)
.replace("@BRIDGED_LAW_MARKER@", LAW_BRIDGE_AUDIT_MARKER)
.replace("@BRIDGE_MARKER@", BRIDGE_AUDIT_MARKER)
.replace("@DECLINE_MARKER@", AUDIT_DECLINE_MARKER)
.replace("@OK_MARKER@", AUDIT_OK_MARKER)
}
fn law_model_uses(candidates: &Candidates) -> String {
let uses = candidates
.laws
.iter()
.enumerate()
.filter(|(_, law)| !law.bridges.is_empty())
.map(|(index, law)| {
let models: Vec<String> = law
.bridges
.iter()
.map(|bridge| candidates.source_bridges[*bridge].model.clone())
.collect();
format!(
"(`{LAW_STATEMENT_PREFIX}{index}, {})",
lean_name_list(&models)
)
})
.collect::<Vec<_>>()
.join(", ");
format!("[{uses}]")
}
fn collect_encoder_shapes(
encoder: &SourceEncoder,
records: &mut Vec<(String, Vec<String>)>,
sums: &mut Vec<(String, Vec<(String, usize)>)>,
) {
let bare = |name: &str| {
name.strip_prefix(bridge_statement::ROOT_PREFIX)
.unwrap_or(name)
.to_string()
};
match encoder {
SourceEncoder::Int | SourceEncoder::Bool | SourceEncoder::Float | SourceEncoder::Str => {}
SourceEncoder::Record {
lean_type, fields, ..
} => {
let ty = bare(lean_type);
let listed: Vec<String> = fields
.iter()
.map(|(accessor, _)| {
accessor
.rsplit_once('.')
.map_or(accessor.clone(), |(_, field)| field.to_string())
})
.collect();
if !records.iter().any(|(seen, _)| *seen == ty) {
records.push((ty, listed));
}
for (_, field) in fields {
collect_encoder_shapes(field, records, sums);
}
}
SourceEncoder::Sum {
lean_type, ctors, ..
} => {
let ty = bare(lean_type);
if !sums.iter().any(|(seen, _)| *seen == ty) {
sums.push((
ty,
ctors
.iter()
.map(|(ctor, fields)| (bare(ctor), fields.len()))
.collect(),
));
}
for field in ctors.iter().flat_map(|(_, fields)| fields) {
collect_encoder_shapes(field, records, sums);
}
}
SourceEncoder::Option(inner)
| SourceEncoder::List(inner)
| SourceEncoder::Vector(inner) => collect_encoder_shapes(inner, records, sums),
SourceEncoder::Result { ok, err } => {
collect_encoder_shapes(ok, records, sums);
collect_encoder_shapes(err, records, sums);
}
SourceEncoder::Tuple { elems, .. } => {
for elem in elems {
collect_encoder_shapes(elem, records, sums);
}
}
}
}
const WALL_NAMESPACE_ROOTS: [&str; 9] = [
"AcceptanceSoundness",
"ArithTemplateDerisk",
"AverBits",
"AverCertChecker",
"CertDecode",
"CertModule",
"CertPrelude",
"InterpreterSequencing",
"AverCertAudit",
];
const PACKAGE_AVERCERT_CHILDREN: [&str; 5] = ["Artifact", "Bridge", "Final", "Laws", "Plans"];
const PACKAGE_AVERCERT_LEAVES: [&str; 2] = ["manifest", "subject"];
const AUDIT_TEMPLATE: &str = include_str!("checker_audit.lean");
fn read_manifest(cert_dir: &Path) -> Result<Value, String> {
let path = cert_dir.join("cert-manifest.json");
let text = std::fs::read_to_string(&path)
.map_err(|error| format!("cannot read {}: {error}", shown_path(&path)))?;
serde_json::from_str(&text)
.map_err(|error| format!("cert-manifest.json is not valid JSON: {error}"))
}
fn manifest_str<'a>(manifest: &'a Value, key: &str) -> Result<&'a str, String> {
manifest
.get(key)
.and_then(Value::as_str)
.ok_or_else(|| format!("cert-manifest.json is missing string field `{key}`"))
}
fn manifest_u64(manifest: &Value, key: &str) -> Result<u64, String> {
manifest
.get(key)
.and_then(Value::as_u64)
.ok_or_else(|| format!("cert-manifest.json is missing integer field `{key}`"))
}
fn read_manifest_identity(manifest: &Value) -> Result<ManifestIdentity, String> {
let identity = ManifestIdentity {
target: manifest_str(manifest, "target")?.to_string(),
profile: manifest_str(manifest, "profile")?.to_string(),
abi: manifest_str(manifest, "abi")?.to_string(),
};
gate_candidate("target", &identity.target)?;
gate_candidate("profile", &identity.profile)?;
gate_candidate("abi", &identity.abi)?;
Ok(identity)
}
fn require_supported_identity(identity: &ManifestIdentity) -> Result<ArtifactTarget, String> {
if identity.profile != format::PROFILE_ID {
return Err(format!(
"unsupported certificate profile `{}`; this checker accepts {}",
display_safe(&identity.profile),
format::PROFILE_ID
));
}
match identity.target.as_str() {
format::TARGET_WASM_GC if identity.abi == format::RUNTIME_ABI_WASM_GC => {
Ok(ArtifactTarget::WasmGc)
}
format::TARGET_WASIP2 if identity.abi == format::RUNTIME_ABI_WASIP2 => {
Ok(ArtifactTarget::Wasip2)
}
format::TARGET_WASM_GC | format::TARGET_WASIP2 => Err(format!(
"unsupported certificate ABI `{}` for target `{}`",
display_safe(&identity.abi),
display_safe(&identity.target)
)),
_ => Err(format!(
"unsupported certificate target `{}`; this checker accepts {}, {}",
display_safe(&identity.target),
format::TARGET_WASM_GC,
format::TARGET_WASIP2
)),
}
}
fn read_artifact_target_envelope(
target: ArtifactTarget,
manifest: &Value,
) -> Result<Option<Wasip2EnvelopeDeclaration>, String> {
match target {
ArtifactTarget::WasmGc => {
if manifest
.get(format::WASIP2_COMPONENT_ENVELOPE_FIELD)
.is_some()
{
return Err(format!(
"cert-manifest.json `{}` is only valid for target `{}`",
format::WASIP2_COMPONENT_ENVELOPE_FIELD,
format::TARGET_WASIP2
));
}
Ok(None)
}
ArtifactTarget::Wasip2 => read_wasip2_component_envelope(manifest).map(Some),
}
}
fn prepare_artifact_for_target<'a>(
target: ArtifactTarget,
bytes: &'a [u8],
envelope: Option<Wasip2EnvelopeDeclaration>,
) -> Result<PreparedArtifact<'a>, String> {
match (target, envelope) {
(ArtifactTarget::WasmGc, None) => prepare_wasm_gc_artifact(bytes),
(ArtifactTarget::Wasip2, Some(envelope)) => {
prepare_wasip2_artifact_with_declared_envelope(bytes, envelope)
}
(ArtifactTarget::WasmGc, Some(_)) => Err(format!(
"{} envelope cannot be used for target `{}`",
format::TARGET_WASIP2,
format::TARGET_WASM_GC
)),
(ArtifactTarget::Wasip2, None) => Err(format!(
"target `{}` requires `{}`",
format::TARGET_WASIP2,
format::WASIP2_COMPONENT_ENVELOPE_FIELD
)),
}
}
fn prepare_wasm_gc_artifact(bytes: &[u8]) -> Result<PreparedArtifact<'_>, String> {
wasmparser::Validator::new()
.validate_all(bytes)
.map_err(|error| format!("artifact is not valid WebAssembly: {error}"))?;
if !wasmparser::Parser::is_core_wasm(bytes) {
return Err("artifact is not a core WebAssembly module".to_string());
}
Ok(PreparedArtifact {
artifact_hash: sha256_hex(bytes),
target_artifact_bytes: bytes,
core_module_bytes: bytes,
lifted_core_exports: Vec::new(),
})
}
fn read_wasip2_component_envelope(manifest: &Value) -> Result<Wasip2EnvelopeDeclaration, String> {
let field = format::WASIP2_COMPONENT_ENVELOPE_FIELD;
let envelope = manifest
.get(field)
.ok_or_else(|| format!("cert-manifest.json is missing object field `{field}`"))?;
exact_object_fields(
envelope,
field,
&[
format::WASIP2_COMPONENT_ENVELOPE_KIND_FIELD,
format::WASIP2_COMPONENT_ENVELOPE_PREFIX_LEN_FIELD,
format::WASIP2_COMPONENT_ENVELOPE_CORE_LEN_FIELD,
format::WASIP2_COMPONENT_ENVELOPE_SUFFIX_LEN_FIELD,
],
)?;
let kind = envelope
.get(format::WASIP2_COMPONENT_ENVELOPE_KIND_FIELD)
.and_then(Value::as_str)
.ok_or_else(|| {
format!(
"cert-manifest.json `{field}.{}` must be a string",
format::WASIP2_COMPONENT_ENVELOPE_KIND_FIELD
)
})?;
if kind != format::WASIP2_COMPONENT_ENVELOPE_KIND {
return Err(format!(
"unsupported wasip2 component envelope kind `{}`; this checker expects {}",
display_safe(kind),
format::WASIP2_COMPONENT_ENVELOPE_KIND
));
}
Ok(Wasip2EnvelopeDeclaration {
inner: format::Wasip2ComponentEnvelopeDeclaration::from_lengths(
envelope_u64(envelope, format::WASIP2_COMPONENT_ENVELOPE_PREFIX_LEN_FIELD)?,
envelope_u64(envelope, format::WASIP2_COMPONENT_ENVELOPE_CORE_LEN_FIELD)?,
envelope_u64(envelope, format::WASIP2_COMPONENT_ENVELOPE_SUFFIX_LEN_FIELD)?,
),
})
}
fn prepare_wasip2_artifact_with_declared_envelope<'a>(
component_bytes: &'a [u8],
declaration: Wasip2EnvelopeDeclaration,
) -> Result<PreparedArtifact<'a>, String> {
if !wasmparser::Parser::is_component(component_bytes) {
return Err("artifact is not a WebAssembly component".to_string());
}
wasmparser::Validator::new()
.validate_all(component_bytes)
.map_err(|error| format!("artifact is not a valid WebAssembly component: {error}"))?;
let declaration = declaration.inner;
if declaration.embedded_core_module_len == 0 {
return Err("wasip2 component envelope declares an empty embedded core module".to_string());
}
let declared_len = declaration.component_len().ok_or_else(|| {
"wasip2 component envelope length overflow while summing prefix/core/suffix".to_string()
})?;
let actual_len = u64::try_from(component_bytes.len())
.map_err(|_| "delivered component length does not fit in u64".to_string())?;
if declared_len != actual_len {
return Err(format!(
"wasip2 component envelope length mismatch: declaration totals {declared_len} bytes, delivered component has {actual_len} bytes"
));
}
let (_prefix, core_module_bytes, _suffix) = declaration
.split_component(component_bytes)
.ok_or_else(|| "wasip2 component envelope split failed".to_string())?;
if !wasmparser::Parser::is_core_wasm(core_module_bytes) {
return Err("declared embedded core module is not a core WebAssembly module".to_string());
}
wasmparser::Validator::new()
.validate_all(core_module_bytes)
.map_err(|error| {
format!("declared embedded core module is not valid WebAssembly: {error}")
})?;
let core_start = usize::try_from(declaration.prefix_len)
.map_err(|_| "wasip2 component envelope prefix does not fit in usize".to_string())?;
let lifted_core_exports = crate::wasip2_binding::confirm_declared_core_binding(
component_bytes,
core_start..core_start + core_module_bytes.len(),
)?;
Ok(PreparedArtifact {
artifact_hash: sha256_hex(component_bytes),
target_artifact_bytes: component_bytes,
core_module_bytes,
lifted_core_exports,
})
}
fn envelope_u64(envelope: &Value, key: &str) -> Result<u64, String> {
envelope.get(key).and_then(Value::as_u64).ok_or_else(|| {
format!(
"cert-manifest.json `{}.{key}` must be a u64",
format::WASIP2_COMPONENT_ENVELOPE_FIELD
)
})
}
fn read_candidates(
manifest: &Value,
identity: ManifestIdentity,
wasip2_component_envelope: Option<format::Wasip2ComponentEnvelopeDeclaration>,
) -> Result<Candidates, String> {
let certified_json = manifest
.get("certified")
.and_then(Value::as_array)
.ok_or_else(|| "cert-manifest.json is missing array field `certified`".to_string())?;
let mut certified = Vec::with_capacity(certified_json.len());
for entry in certified_json {
let name = required_string(entry, "name", "certified[]")?;
let class = required_string(entry, "class", "certified[]")?;
let policy = required_string(entry, "policy", "certified[]")?;
let policy_lean = match policy.as_str() {
"simulatesModel" => ".simulatesModel",
"simulatesModelTotally" => ".simulatesModelTotally",
other => {
return Err(format!(
"certified export {name:?} uses unsupported policy {other:?}"
));
}
};
let termination_lean = parse_termination(entry.get("termination_witness"), &name)?;
match (policy.as_str(), entry.get("termination_witness")) {
("simulatesModel", None) | ("simulatesModelTotally", Some(_)) => {}
("simulatesModel", Some(_)) => {
return Err(format!(
"partial export {name:?} must not carry a termination witness"
));
}
("simulatesModelTotally", None) => {
return Err(format!(
"total export {name:?} is missing `termination_witness`"
));
}
_ => unreachable!(),
}
if class != format::PLAN_CLASS {
return Err(format!(
"certified export `{}` reports class `{}`; schema {} has the one class `{}`",
display_safe(&name),
display_safe(&class),
format::CERT_SCHEMA_VERSION,
format::PLAN_CLASS
));
}
let facets = entry
.get("facets")
.and_then(Value::as_array)
.ok_or_else(|| {
format!(
"certified export `{}` is missing `facets`",
display_safe(&name)
)
})?
.iter()
.map(|facet| {
facet.as_str().map(str::to_string).ok_or_else(|| {
"cert-manifest.json `certified[].facets[]` is not a string".to_string()
})
})
.collect::<Result<Vec<_>, _>>()?;
certified.push(CertifiedCandidate {
name,
class,
facets,
policy,
policy_lean,
termination_lean,
});
}
let bridges_json = manifest
.get("sourceBridges")
.and_then(Value::as_array)
.ok_or_else(|| "cert-manifest.json is missing array field `sourceBridges`".to_string())?;
let certified_names: Vec<&str> = certified
.iter()
.map(|candidate| candidate.name.as_str())
.collect();
let mut source_bridges: Vec<SourceBridgeCandidate> = Vec::with_capacity(bridges_json.len());
for (index, entry) in bridges_json.iter().enumerate() {
let context = format!("sourceBridges[{index}]");
exact_object_fields(
entry,
&context,
&[
"export",
"theorem",
"corollary",
"model",
"kind",
"params",
"result",
],
)?;
let kind_tag = required_string(entry, "kind", &context)?;
let kind = BridgeKind::from_tag(&kind_tag).ok_or_else(|| {
format!(
"cert-manifest.json `{context}.kind` is not a bridge statement kind: `{}`",
display_safe(&kind_tag)
)
})?;
let declared_params = entry["params"]
.as_array()
.ok_or_else(|| format!("cert-manifest.json `{context}.params` is not an array"))?;
let mut params = Vec::with_capacity(declared_params.len());
for (position, declared) in declared_params.iter().enumerate() {
params.push(read_source_encoder(
declared,
&format!("{context}.params[{position}]"),
)?);
}
let bridge = RawSourceBridge {
export: required_string(entry, "export", &context)?,
theorem: required_string(entry, "theorem", &context)?,
corollary: required_string(entry, "corollary", &context)?,
model: required_string(entry, "model", &context)?,
kind,
params,
result: read_source_encoder(&entry["result"], &format!("{context}.result"))?,
};
let bridge = validate_source_bridge_candidate(bridge)?;
if !certified_names.contains(&bridge.export.as_str()) {
return Err(format!(
"source-bridge names `{}`, which is not a certified export",
display_safe(&bridge.export)
));
}
if source_bridges
.iter()
.any(|seen| seen.export == bridge.export)
{
return Err(format!(
"source-bridges declare export `{}` twice",
display_safe(&bridge.export)
));
}
source_bridges.push(bridge);
}
let laws_json = manifest
.get("laws")
.and_then(Value::as_array)
.ok_or_else(|| "cert-manifest.json is missing array field `laws`".to_string())?;
let mut laws = Vec::with_capacity(laws_json.len());
for (index, entry) in laws_json.iter().enumerate() {
let context = format!("laws[{index}]");
exact_object_fields(
entry,
&context,
&["label", "theorem", "statement", "corollary", "bridges"],
)?;
let law = LawCandidate {
label: required_string(entry, "label", &context)?,
theorem: required_string(entry, "theorem", &context)?,
statement: required_string(entry, "statement", &context)?,
corollary: required_string(entry, "corollary", &context)?,
prefix: String::new(),
bridges: Vec::new(),
};
let mut law = validate_law_candidate(law)?;
let declared_bridges = entry
.get("bridges")
.and_then(Value::as_array)
.ok_or_else(|| format!("cert-manifest.json `{context}.bridges` is not an array"))?;
for value in declared_bridges {
let export = value.as_str().ok_or_else(|| {
format!("cert-manifest.json `{context}.bridges[]` is not a string")
})?;
let at = source_bridges
.iter()
.position(|bridge| bridge.export == export)
.ok_or_else(|| {
format!(
"law-claim `{}` cites source-bridge `{}`, which is not declared",
display_safe(&law.label),
display_safe(export)
)
})?;
if law.bridges.contains(&at) {
return Err(format!(
"law-claim `{}` cites source-bridge `{}` twice",
display_safe(&law.label),
display_safe(export)
));
}
law.bridges.push(at);
}
if !law.bridges.is_empty() {
let models: Vec<&str> = source_bridges
.iter()
.map(|bridge| bridge.model.as_str())
.collect();
if let Some(model) =
bridge_statement::law_names_model_unqualified(&law.statement, &models)
{
return Err(format!(
"law-claim `{}` names the bridged model `{}` without `_root_.`",
display_safe(&law.label),
display_safe(model)
));
}
let mentioned = bridge_statement::law_mentioned_bridges(&law.statement, &models);
if law.bridges != mentioned {
return Err(format!(
"law-claim `{}` cites bridges that are not exactly those of the functions \
its statement names",
display_safe(&law.label)
));
}
}
laws.push(law);
}
let mut seen_corollaries = std::collections::BTreeSet::new();
for law in &laws {
if !seen_corollaries.insert(law.corollary.as_str()) {
return Err(format!(
"law-claims declare duplicate corollary `{}`",
display_safe(&law.corollary)
));
}
}
let contracts = string_array(manifest, "runtime_contracts")?;
let declared_uncertified =
object_pair_array(manifest, "declaredUncertified", "name", "reason")?;
let capabilities = object_pair_array(manifest, "capabilities", "module", "name")?;
let start_object = manifest
.get("start")
.ok_or_else(|| "cert-manifest.json is missing object field `start`".to_string())?;
exact_object_fields(start_object, "start", &["present", "function_index"])?;
let present = start_object
.get("present")
.and_then(Value::as_bool)
.ok_or_else(|| "cert-manifest.json `start.present` is not a boolean".to_string())?;
let start = match (present, start_object.get("function_index")) {
(false, Some(Value::Null)) => None,
(true, Some(value)) => Some(value_u32(value, "start.function_index")?),
(false, _) => {
return Err("absent start must use null `function_index`".to_string());
}
(true, None) => unreachable!("exact fields checked"),
};
let host_roles = manifest
.get("hostRoleTable")
.ok_or_else(|| "cert-manifest.json is missing object field `hostRoleTable`".to_string())?;
let host_role_table = if host_roles.is_null() {
None
} else {
exact_object_fields(
host_roles,
"hostRoleTable",
&["box", "add", "mul", "sub", "toIndex", "cmp", "eq", "divmod"],
)?;
let optional_index = |key: &str| -> Result<Option<u32>, String> {
match &host_roles[key] {
Value::Null => Ok(None),
value => Ok(Some(value_u32(value, &format!("hostRoleTable.{key}"))?)),
}
};
Some((
optional_index("box")?,
optional_index("add")?,
optional_index("mul")?,
optional_index("sub")?,
optional_index("toIndex")?,
optional_index("cmp")?,
optional_index("eq")?,
optional_index("divmod")?,
))
};
let string_roles_json = manifest
.get("stringHostRoles")
.and_then(Value::as_array)
.ok_or_else(|| "cert-manifest.json is missing array field `stringHostRoles`".to_string())?;
let mut string_host_roles = Vec::with_capacity(string_roles_json.len());
for (index, entry) in string_roles_json.iter().enumerate() {
exact_object_fields(
entry,
&format!("stringHostRoles[{index}]"),
&["function_index", "role"],
)?;
let function_index = value_u32(
&entry["function_index"],
&format!("stringHostRoles[{index}].function_index"),
)?;
let role = match entry.get("role").and_then(Value::as_str) {
Some("stringEq") => StringHostRole::Eq,
Some("stringConcat") => StringHostRole::Concat,
_ => {
return Err(format!(
"stringHostRoles[{index}].role must be stringEq or stringConcat"
));
}
};
string_host_roles.push((function_index, role));
}
let candidates = Candidates {
certified,
laws,
source_bridges,
contracts,
declared_uncertified,
capabilities,
start,
host_role_table,
string_host_roles,
target: identity.target,
profile: identity.profile,
abi: identity.abi,
wasip2_component_envelope,
};
gate_candidates(&candidates)?;
Ok(candidates)
}
fn validate_law_candidate(mut law: LawCandidate) -> Result<LawCandidate, String> {
if let Err(field) = lean_gate::law_claim_identifiers(&law.label, &law.theorem, &law.corollary) {
return Err(format!(
"law-claim `{}` field `{field}` is not a plain dotted Lean identifier",
display_safe(&law.label)
));
}
if law.corollary != law.label.replace('.', "_") {
return Err(format!(
"law-claim `{}` corollary `{}` is not the label's flattening",
law.label, law.corollary
));
}
if !statement_is_single_plain_line(&law.statement) {
return Err(format!(
"law-claim `{}` statement is not a single plain term-position line",
law.label
));
}
law.prefix = law
.theorem
.rsplit_once('.')
.map(|(prefix, _)| prefix.to_string())
.unwrap_or_default();
Ok(law)
}
const MAX_STATEMENT_LEN: usize = MAX_BRIDGE_STATEMENT_LEN;
fn statement_is_single_plain_line(statement: &str) -> bool {
bridge_statement::statement_is_single_plain_line(statement, MAX_STATEMENT_LEN)
}
struct RawSourceBridge {
export: String,
theorem: String,
corollary: String,
model: String,
kind: BridgeKind,
params: Vec<SourceEncoder>,
result: SourceEncoder,
}
fn validate_source_bridge_candidate(
bridge: RawSourceBridge,
) -> Result<SourceBridgeCandidate, String> {
if !bridge_statement::is_plain_export_name(&bridge.export) {
return Err(format!(
"source-bridge export `{}` is not a plain Lean identifier",
display_safe(&bridge.export)
));
}
if !bridge_statement::is_plain_dotted_name(&bridge.model) {
return Err(format!(
"source-bridge `{}` model `{}` is not a plain dotted Lean identifier",
display_safe(&bridge.export),
display_safe(&bridge.model)
));
}
let expected_theorem = format!("{BRIDGE_NAMESPACE}.{}", bridge.export);
let expected_corollary = format!("{expected_theorem}{BRIDGE_COROLLARY_SUFFIX}");
if bridge.theorem != expected_theorem || bridge.corollary != expected_corollary {
return Err(format!(
"source-bridge `{}` must declare theorem `{expected_theorem}` and corollary \
`{expected_corollary}`",
display_safe(&bridge.export)
));
}
for (position, encoder) in bridge
.params
.iter()
.chain(std::iter::once(&bridge.result))
.enumerate()
{
if !encoder.is_well_formed() {
let what = if position < bridge.params.len() {
format!("parameter {position}")
} else {
"result".to_string()
};
return Err(format!(
"source-bridge `{}` {what} encoder does not name a `_root_`-qualified type \
and its own accessors and constructors, or exceeds the encoder caps",
display_safe(&bridge.export)
));
}
}
let statement = render_bridge_statement(
&bridge.export,
&bridge.model,
bridge.kind,
&bridge.params,
&bridge.result,
);
if !statement_is_single_plain_line(&statement) {
return Err(format!(
"source-bridge `{}` renders a statement that is not a single plain \
term-position line",
display_safe(&bridge.export)
));
}
if !statement_is_root_qualified(&statement) {
return Err(format!(
"source-bridge `{}` renders a statement naming something that is not \
`_root_`-qualified",
display_safe(&bridge.export)
));
}
Ok(SourceBridgeCandidate {
export: bridge.export,
corollary: bridge.corollary,
model: bridge.model,
kind: bridge.kind,
statement,
params: bridge.params,
result: bridge.result,
})
}
fn read_source_encoder(value: &Value, context: &str) -> Result<SourceEncoder, String> {
read_source_encoder_at(value, context, 0)
}
fn read_source_encoder_at(
value: &Value,
context: &str,
depth: usize,
) -> Result<SourceEncoder, String> {
if depth >= bridge_statement::MAX_ENCODER_DEPTH {
return Err(format!(
"cert-manifest.json `{context}` nests encoders deeper than {}",
bridge_statement::MAX_ENCODER_DEPTH
));
}
let kind = value
.get(bridge_statement::ENCODER_KIND_KEY)
.and_then(Value::as_str)
.ok_or_else(|| format!("cert-manifest.json `{context}.kind` is not a string"))?;
let tid = |value: &Value| -> Result<u32, String> {
value
.get("tid")
.and_then(Value::as_u64)
.and_then(|t| u32::try_from(t).ok())
.ok_or_else(|| format!("cert-manifest.json `{context}.tid` is not a type id"))
};
let child = |key: &str| -> Result<Box<SourceEncoder>, String> {
Ok(Box::new(read_source_encoder_at(
&value[key],
&format!("{context}.{key}"),
depth + 1,
)?))
};
let array = |key: &str| -> Result<&Vec<Value>, String> {
value[key]
.as_array()
.ok_or_else(|| format!("cert-manifest.json `{context}.{key}` is not an array"))
};
match kind {
bridge_statement::ENCODER_KIND_INT
| bridge_statement::ENCODER_KIND_BOOL
| bridge_statement::ENCODER_KIND_FLOAT
| bridge_statement::ENCODER_KIND_STRING => {
exact_object_fields(value, context, &[bridge_statement::ENCODER_KIND_KEY])?;
Ok(match kind {
bridge_statement::ENCODER_KIND_INT => SourceEncoder::Int,
bridge_statement::ENCODER_KIND_BOOL => SourceEncoder::Bool,
bridge_statement::ENCODER_KIND_FLOAT => SourceEncoder::Float,
_ => SourceEncoder::Str,
})
}
bridge_statement::ENCODER_KIND_RECORD => {
exact_object_fields(value, context, &["kind", "tid", "type", "fields"])?;
let mut fields = Vec::new();
for (index, field) in array("fields")?.iter().enumerate() {
let at = format!("{context}.fields[{index}]");
exact_object_fields(field, &at, &["accessor", "encoder"])?;
fields.push((
required_string(field, "accessor", &at)?,
read_source_encoder_at(&field["encoder"], &format!("{at}.encoder"), depth + 1)?,
));
}
Ok(SourceEncoder::Record {
tid: tid(value)?,
lean_type: required_string(value, "type", context)?,
fields,
})
}
bridge_statement::ENCODER_KIND_SUM => {
exact_object_fields(value, context, &["kind", "tid", "type", "ctors"])?;
let mut ctors = Vec::new();
for (index, ctor) in array("ctors")?.iter().enumerate() {
let at = format!("{context}.ctors[{index}]");
exact_object_fields(ctor, &at, &["ctor", "fields"])?;
let mut fields = Vec::new();
for (position, field) in ctor["fields"]
.as_array()
.ok_or_else(|| format!("cert-manifest.json `{at}.fields` is not an array"))?
.iter()
.enumerate()
{
fields.push(read_source_encoder_at(
field,
&format!("{at}.fields[{position}]"),
depth + 1,
)?);
}
ctors.push((required_string(ctor, "ctor", &at)?, fields));
}
Ok(SourceEncoder::Sum {
tid: tid(value)?,
lean_type: required_string(value, "type", context)?,
ctors,
})
}
bridge_statement::ENCODER_KIND_OPTION => {
exact_object_fields(value, context, &["kind", "elem"])?;
Ok(SourceEncoder::Option(child("elem")?))
}
bridge_statement::ENCODER_KIND_RESULT => {
exact_object_fields(value, context, &["kind", "ok", "err"])?;
Ok(SourceEncoder::Result {
ok: child("ok")?,
err: child("err")?,
})
}
bridge_statement::ENCODER_KIND_TUPLE => {
exact_object_fields(value, context, &["kind", "tid", "elems"])?;
let mut elems = Vec::new();
for (index, elem) in array("elems")?.iter().enumerate() {
elems.push(read_source_encoder_at(
elem,
&format!("{context}.elems[{index}]"),
depth + 1,
)?);
}
Ok(SourceEncoder::Tuple {
tid: tid(value)?,
elems,
})
}
bridge_statement::ENCODER_KIND_LIST => {
exact_object_fields(value, context, &["kind", "elem"])?;
Ok(SourceEncoder::List(child("elem")?))
}
bridge_statement::ENCODER_KIND_VECTOR => {
exact_object_fields(value, context, &["kind", "elem"])?;
Ok(SourceEncoder::Vector(child("elem")?))
}
other => Err(format!(
"cert-manifest.json `{context}` declares unknown source-bridge encoder kind `{}`",
display_safe(other)
)),
}
}
fn parse_law_audits(output: &str, laws: &[LawCandidate]) -> Result<Vec<LawOutcome>, String> {
let audited = parse_pin_audits(
output,
LAW_AUDIT_MARKER,
LAW_PIN_PREFIX,
"law-claim",
laws.len(),
)?;
let mut outcomes = Vec::with_capacity(laws.len());
for (index, (law, audit)) in laws.iter().zip(audited).enumerate() {
let offending = audit.ok_or_else(|| {
format!(
"checker witness reported no axiom audit for law-claim `{}` \
(pin {LAW_PIN_PREFIX}{index}); refusing to credit an unaudited claim",
display_safe(&law.label)
)
})?;
outcomes.push(LawOutcome {
label: law.label.clone(),
offending,
});
}
Ok(outcomes)
}
fn parse_bridged_law_audits(
output: &str,
laws: &[LawCandidate],
) -> Result<Vec<LawOutcome>, String> {
let bridged = bridged_law_indices(laws);
let audited = parse_pin_audits(
output,
LAW_BRIDGE_AUDIT_MARKER,
BRIDGED_LAW_PIN_PREFIX,
"bridged law-claim",
bridged.len(),
)?;
let mut outcomes = Vec::with_capacity(bridged.len());
for (position, (at, audit)) in bridged.into_iter().zip(audited).enumerate() {
let law = &laws[at];
let offending = audit.ok_or_else(|| {
format!(
"checker witness reported no axiom audit for bridged law-claim `{}` \
(pin {BRIDGED_LAW_PIN_PREFIX}{position}); refusing to credit an unaudited claim",
display_safe(&law.label)
)
})?;
outcomes.push(LawOutcome {
label: law.label.clone(),
offending,
});
}
Ok(outcomes)
}
fn parse_bridge_audits(
output: &str,
bridges: &[SourceBridgeCandidate],
) -> Result<Vec<BridgeOutcome>, String> {
let audited = parse_pin_audits(
output,
BRIDGE_AUDIT_MARKER,
BRIDGE_PIN_PREFIX,
"source-bridge",
bridges.len(),
)?;
let mut outcomes = Vec::with_capacity(bridges.len());
for (index, (bridge, audit)) in bridges.iter().zip(audited).enumerate() {
let offending = audit.ok_or_else(|| {
format!(
"checker witness reported no axiom audit for source-bridge `{}` \
(pin {BRIDGE_PIN_PREFIX}{index}); refusing to credit an unaudited bridge",
display_safe(&bridge.export)
)
})?;
outcomes.push(BridgeOutcome {
export: bridge.export.clone(),
model: bridge.model.clone(),
kind: bridge.kind,
statement: bridge.statement.clone(),
offending,
});
}
Ok(outcomes)
}
fn parse_pin_audits(
output: &str,
marker: &str,
pin_prefix: &str,
kind: &str,
expected: usize,
) -> Result<Vec<Option<Vec<String>>>, String> {
let mut audited: Vec<Option<Vec<String>>> = vec![None; expected];
for line in output.lines() {
let Some((_, tail)) = line.split_once(marker) else {
continue;
};
let fields: Vec<&str> = tail.split_whitespace().collect();
let malformed = || {
format!(
"checker witness emitted a malformed {kind} audit line: {}",
display_safe(line.trim())
)
};
let (pin, offending): (&str, Vec<String>) = match fields.as_slice() {
[pin, "ok"] => (pin, Vec::new()),
[pin, "axioms", names] => (pin, names.split(',').map(str::to_string).collect()),
_ => return Err(malformed()),
};
if offending.iter().any(String::is_empty) {
return Err(malformed());
}
let index = pin
.strip_prefix(pin_prefix)
.and_then(|index| index.parse::<usize>().ok())
.filter(|index| *index < expected)
.ok_or_else(|| {
format!(
"checker witness audited an unknown {kind} pin `{}`",
display_safe(pin)
)
})?;
if audited[index].is_some() {
return Err(format!(
"checker witness audited {kind} pin `{}` more than once",
display_safe(pin)
));
}
audited[index] = Some(offending);
}
Ok(audited)
}
fn parse_termination(value: Option<&Value>, export: &str) -> Result<String, String> {
let Some(value) = value else {
return Ok("_root_.Option.none".to_string());
};
let measure = value
.get("measure")
.and_then(Value::as_object)
.ok_or_else(|| format!("export `{export}` has malformed termination measure"))?;
if measure.get("kind").and_then(Value::as_str) != Some("intNatAbs") {
return Err(format!(
"export `{export}` uses an unsupported termination measure"
));
}
let parameter = measure
.get("param_index")
.and_then(Value::as_u64)
.and_then(|value| u32::try_from(value).ok())
.ok_or_else(|| format!("export `{export}` has invalid termination parameter"))?;
let descent = value
.get("descent")
.and_then(Value::as_i64)
.ok_or_else(|| format!("export `{export}` has invalid termination descent"))?;
Ok(format!(
"_root_.Option.some ({{ measure := .intNatAbs {}, descent := {} }} : _root_.AverCert.Schema.TerminationWitness)",
lean_nat(parameter),
lean_int(descent)
))
}
fn required_string(value: &Value, key: &str, context: &str) -> Result<String, String> {
value
.get(key)
.and_then(Value::as_str)
.map(str::to_string)
.ok_or_else(|| format!("cert-manifest.json `{context}.{key}` is not a string"))
}
fn string_array(manifest: &Value, key: &str) -> Result<Vec<String>, String> {
manifest
.get(key)
.and_then(Value::as_array)
.ok_or_else(|| format!("cert-manifest.json is missing array field `{key}`"))?
.iter()
.map(|value| {
value
.as_str()
.map(str::to_string)
.ok_or_else(|| format!("cert-manifest.json `{key}[]` is not a string"))
})
.collect()
}
fn object_pair_array(
manifest: &Value,
key: &str,
left: &str,
right: &str,
) -> Result<Vec<(String, String)>, String> {
manifest
.get(key)
.and_then(Value::as_array)
.ok_or_else(|| format!("cert-manifest.json is missing array field `{key}`"))?
.iter()
.enumerate()
.map(|(index, value)| {
exact_object_fields(value, &format!("{key}[{index}]"), &[left, right])?;
Ok((
required_string(value, left, &format!("{key}[{index}]"))?,
required_string(value, right, &format!("{key}[{index}]"))?,
))
})
.collect()
}
fn exact_object_fields(value: &Value, context: &str, expected: &[&str]) -> Result<(), String> {
let object = value
.as_object()
.ok_or_else(|| format!("cert-manifest.json `{context}` is not an object"))?;
if object.len() != expected.len() || expected.iter().any(|key| !object.contains_key(*key)) {
return Err(format!(
"cert-manifest.json `{context}` must contain exactly fields {}",
expected.join(", ")
));
}
Ok(())
}
fn value_u32(value: &Value, context: &str) -> Result<u32, String> {
value
.as_u64()
.and_then(|value| u32::try_from(value).ok())
.ok_or_else(|| format!("cert-manifest.json `{context}` must be a u32"))
}
fn gate_candidates(candidates: &Candidates) -> Result<(), String> {
for candidate in &candidates.certified {
gate_candidate("certified export name", &candidate.name)?;
gate_candidate("certified class", &candidate.class)?;
for facet in &candidate.facets {
gate_candidate("certified facet", facet)?;
}
}
for contract in &candidates.contracts {
gate_candidate("runtime contract", contract)?;
}
for (name, reason) in &candidates.declared_uncertified {
gate_candidate("declared-uncertified name", name)?;
gate_candidate("declared-uncertified reason", reason)?;
}
for (module, name) in &candidates.capabilities {
gate_candidate("capability module", module)?;
gate_candidate("capability name", name)?;
}
gate_candidate("target", &candidates.target)?;
gate_candidate("profile", &candidates.profile)?;
gate_candidate("abi", &candidates.abi)
}
fn gate_candidate(kind: &str, value: &str) -> Result<(), String> {
let safe = value.len() <= crate::format::MAX_CANDIDATE_LEN
&& value
.bytes()
.all(|byte| (0x20..=0x7e).contains(&byte) && byte != b'"' && byte != b'\\');
if safe {
Ok(())
} else {
Err(format!(
"certificate {kind} is outside the allowed printable ASCII subset: {value:?}"
))
}
}
fn is_checker_owned(name: &str, selected_wall: &wall::Wall) -> bool {
selected_wall
.sources
.iter()
.any(|source| source.name == name)
|| matches!(
name,
"ArtifactBytes.lean"
| "ArtifactComponentBytes.lean"
| "Module.lean"
| "lakefile.lean"
| "CheckerWitness.lean"
| "CheckerAudit.lean"
)
}
fn assemble_build(
cert_dir: &Path,
artifact_hash: &str,
core_module_bytes: &[u8],
target_artifact_bytes: &[u8],
selected_wall: &wall::Wall,
memory_limit_mb: u64,
) -> Result<BuildDir, String> {
let mut build = BuildDir::new()?;
let mut roots = Vec::new();
let mut flat_files: Vec<(String, PathBuf)> = Vec::new();
let mut subdirectories: Vec<(String, PathBuf)> = Vec::new();
let entries = std::fs::read_dir(cert_dir)
.map_err(|error| format!("cannot read cert dir {}: {error}", shown_path(cert_dir)))?;
for entry in entries {
let entry = entry.map_err(|error| format!("cert dir read: {error}"))?;
let Ok(kind) = entry.file_type() else {
continue;
};
let name = entry.file_name().to_string_lossy().into_owned();
if kind.is_dir() {
if !name.starts_with('.') {
subdirectories.push((name, entry.path()));
}
continue;
}
if !kind.is_file() {
continue;
}
if !name.ends_with(".lean") || is_checker_owned(&name, selected_wall) {
continue;
}
flat_files.push((name, entry.path()));
}
let mut staged_paths: std::collections::BTreeMap<String, String> =
std::collections::BTreeMap::new();
let mut admitted: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
for (name, path) in &flat_files {
let root = lean_module_root(name)?;
reject_shadowed_root(&root, selected_wall)?;
note_staged_path(&mut staged_paths, name)?;
let contents = std::fs::read(path)
.map_err(|error| format!("cannot read cert file {name:?}: {error}"))?;
scan_for_code_exec(name, &contents)?;
if matches!(
name.as_str(),
"Manifest.lean" | "Certificate.lean" | "Bridge.lean" | "Laws.lean"
) {
collect_import_lines(&String::from_utf8_lossy(&contents), &mut admitted);
}
std::fs::write(build.path.join(name), contents)
.map_err(|error| format!("cannot stage {name:?}: {error}"))?;
roots.push(root);
}
let mut nested_files: Vec<(String, PathBuf)> = Vec::new();
for (name, path) in &subdirectories {
collect_nested_lean_files(path, name, 1, &mut nested_files)?;
}
nested_files.sort();
for (relative, path) in &nested_files {
let root = lean_module_root(relative)?;
reject_shadowed_root(&root, selected_wall)?;
if !admitted.contains(&root) {
continue;
}
note_staged_path(&mut staged_paths, relative)?;
let contents = std::fs::read(path)
.map_err(|error| format!("cannot read cert file {relative:?}: {error}"))?;
scan_for_code_exec(relative, &contents)?;
let destination = relative
.split('/')
.fold(build.path.clone(), |path, segment| path.join(segment));
if let Some(parent) = destination.parent() {
std::fs::create_dir_all(parent)
.map_err(|error| format!("cannot stage {relative:?}: {error}"))?;
}
std::fs::write(destination, contents)
.map_err(|error| format!("cannot stage {relative:?}: {error}"))?;
roots.push(root);
}
build.package_roots = roots.clone();
for source in selected_wall.sources {
std::fs::write(build.path.join(source.name), source.contents)
.map_err(|error| format!("cannot stage {}: {error}", source.name))?;
roots.push(
source
.name
.strip_suffix(".lean")
.expect("wall source is Lean")
.to_string(),
);
}
std::fs::write(
build.path.join("ArtifactBytes.lean"),
wall::render_artifact_bytes(core_module_bytes),
)
.map_err(|error| format!("cannot stage ArtifactBytes.lean: {error}"))?;
roots.push("ArtifactBytes".to_string());
std::fs::write(
build.path.join("ArtifactComponentBytes.lean"),
wall::render_artifact_component_bytes(target_artifact_bytes),
)
.map_err(|error| format!("cannot stage ArtifactComponentBytes.lean: {error}"))?;
roots.push("ArtifactComponentBytes".to_string());
std::fs::write(
build.path.join("Module.lean"),
wall::render_module(artifact_hash),
)
.map_err(|error| format!("cannot stage Module.lean: {error}"))?;
roots.push("Module".to_string());
roots.sort();
roots.dedup();
std::fs::write(
build.path.join("lakefile.lean"),
checker_lakefile(&roots, memory_limit_mb),
)
.map_err(|error| format!("cannot write checker lakefile: {error}"))?;
std::fs::write(build.path.join("lean-toolchain"), selected_wall.toolchain)
.map_err(|error| format!("cannot write lean-toolchain: {error}"))?;
Ok(build)
}
fn lean_module_root(name: &str) -> Result<String, String> {
lean_gate::lean_module_root(name)
}
fn reject_shadowed_root(root: &str, selected_wall: &wall::Wall) -> Result<(), String> {
let segments: Vec<&str> = root.split('.').collect();
for length in 1..=segments.len() {
let prefix = segments[..length].join(".");
let shadows_toolchain = TOOLCHAIN_ROOTS
.iter()
.any(|reserved| reserved.eq_ignore_ascii_case(&prefix));
let shadows_checker = selected_wall.sources.iter().any(|source| {
source
.name
.strip_suffix(".lean")
.is_some_and(|reserved| reserved.eq_ignore_ascii_case(&prefix))
}) || [
"ArtifactBytes",
"ArtifactComponentBytes",
"Module",
"CheckerWitness",
"CheckerAudit",
"lakefile",
]
.iter()
.any(|reserved| reserved.eq_ignore_ascii_case(&prefix));
if shadows_toolchain || shadows_checker {
return Err(format!(
"cert data module `{root}` shadows a checker/toolchain import"
));
}
}
Ok(())
}
fn collect_import_lines(text: &str, admitted: &mut std::collections::BTreeSet<String>) {
for line in text.lines() {
if let Some(rest) = line.trim().strip_prefix("import ") {
let module = rest.trim();
if !module.is_empty() {
admitted.insert(module.to_string());
}
}
}
}
fn note_staged_path(
staged: &mut std::collections::BTreeMap<String, String>,
relative: &str,
) -> Result<(), String> {
if let Some(previous) = staged.insert(relative.to_ascii_lowercase(), relative.to_string()) {
return Err(format!(
"cert files {previous:?} and {relative:?} collide case-insensitively"
));
}
Ok(())
}
const MAX_NESTED_DEPTH: usize = 16;
fn collect_nested_lean_files(
dir: &Path,
relative: &str,
depth: usize,
out: &mut Vec<(String, PathBuf)>,
) -> Result<(), String> {
if depth > MAX_NESTED_DEPTH {
return Err(format!(
"cert subdirectory {relative:?} exceeds the maximum nesting depth of {MAX_NESTED_DEPTH}"
));
}
let entries = std::fs::read_dir(dir)
.map_err(|error| format!("cannot read cert dir {}: {error}", shown_path(dir)))?;
for entry in entries {
let entry = entry.map_err(|error| format!("cert dir read: {error}"))?;
let Ok(kind) = entry.file_type() else {
continue;
};
let name = entry.file_name().to_string_lossy().into_owned();
if kind.is_dir() {
if !name.starts_with('.') {
collect_nested_lean_files(
&entry.path(),
&format!("{relative}/{name}"),
depth + 1,
out,
)?;
}
} else if kind.is_file() && name.ends_with(".lean") {
out.push((format!("{relative}/{name}"), entry.path()));
}
}
Ok(())
}
fn scan_for_code_exec(name: &str, contents: &[u8]) -> Result<(), String> {
let text = String::from_utf8_lossy(contents);
if let Some(token) = lean_gate::code_exec_token(&text) {
return Err(format!(
"cert data file {name:?} contains refused construct `{token}`"
));
}
Ok(())
}
fn checker_lakefile(roots: &[String], memory_limit_mb: u64) -> String {
let roots = roots
.iter()
.map(|root| format!("`{root}"))
.collect::<Vec<_>>()
.join(", ");
format!(
"import Lake\nopen Lake DSL\n\npackage «avercert» where\n version := v!\"0.1.0\"\n\n@[default_target]\nlean_lib «AverCert» where\n srcDir := \".\"\n roots := #[{roots}]\n moreLeanArgs := #[\"--memory={memory_limit_mb}\"]\n"
)
}
fn lean_str_list(items: &[String]) -> String {
format!(
"[{}]",
items
.iter()
.map(|item| format!("\"{item}\""))
.collect::<Vec<_>>()
.join(", ")
)
}
fn lean_string_pair_list(items: &[(String, String)]) -> String {
format!(
"[{}]",
items
.iter()
.map(|(left, right)| format!("(\"{left}\", \"{right}\")"))
.collect::<Vec<_>>()
.join(", ")
)
}
fn lean_option_nat(value: Option<u32>) -> String {
value.map_or_else(
|| "_root_.Option.none".to_string(),
|value| format!("(_root_.Option.some {})", lean_nat(value)),
)
}
fn lean_wasip2_component_envelope(
value: Option<format::Wasip2ComponentEnvelopeDeclaration>,
) -> String {
value.map_or_else(
|| "(_root_.Option.none : _root_.Option _root_.AverCert.Wasip2Envelope.ComponentEnvelope)"
.to_string(),
|value| {
format!(
"_root_.Option.some ({{ prefixLen := {}, embeddedCoreModuleLen := {}, suffixLen := {} }} : _root_.AverCert.Wasip2Envelope.ComponentEnvelope)",
lean_nat(value.prefix_len),
lean_nat(value.embedded_core_module_len),
lean_nat(value.suffix_len)
)
},
)
}
fn sha256_hex(bytes: &[u8]) -> String {
format!("{:x}", Sha256::digest(bytes))
}
struct BuildDir {
path: PathBuf,
package_roots: Vec<String>,
}
impl BuildDir {
fn new() -> Result<Self, String> {
let path = checker_temp_root()?.join(format!(
"aver-cert-check-{}-{}",
std::process::id(),
unique_nanos()
));
let mut builder = std::fs::DirBuilder::new();
#[cfg(unix)]
{
use std::os::unix::fs::DirBuilderExt;
builder.mode(0o700);
}
builder
.create(&path)
.map_err(|error| format!("cannot create checker build dir: {error}"))?;
Ok(Self {
path,
package_roots: Vec::new(),
})
}
}
fn checker_temp_root() -> Result<PathBuf, String> {
#[cfg(unix)]
{
Ok(PathBuf::from("/tmp"))
}
#[cfg(windows)]
{
let home = std::env::var_os("USERPROFILE")
.or_else(|| std::env::var_os("HOME"))
.ok_or_else(|| {
"cannot select checker temp root: USERPROFILE/HOME is not set".to_string()
})?;
let root = PathBuf::from(home)
.join("AppData")
.join("Local")
.join("Temp");
std::fs::create_dir_all(&root)
.map_err(|error| format!("cannot create checker temp root: {error}"))?;
Ok(root)
}
#[cfg(not(any(unix, windows)))]
{
let home = std::env::var_os("HOME")
.ok_or_else(|| "cannot select checker temp root: HOME is not set".to_string())?;
let root = PathBuf::from(home).join(".aver-cert-tmp");
std::fs::create_dir_all(&root)
.map_err(|error| format!("cannot create checker temp root: {error}"))?;
Ok(root)
}
}
impl Drop for BuildDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.path);
}
}
fn unique_nanos() -> u128 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|duration| duration.as_nanos())
.unwrap_or(0)
}
struct LakeOut {
status: std::process::ExitStatus,
combined: String,
}
fn run_lake(
lean: &LeanRunner,
build_dir: &Path,
phase: &str,
arguments: &[&str],
) -> Result<LakeOut, String> {
let started = std::time::Instant::now();
let output = lean
.run_lake(build_dir, phase, arguments)
.map_err(|error| error.to_string())?;
report_step_timing(phase, started.elapsed(), &output.stdout);
Ok(LakeOut {
status: output.status,
combined: format!(
"{}{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
),
})
}
fn report_step_timing(phase: &str, elapsed: std::time::Duration, stdout: &[u8]) {
if std::env::var_os("AVER_CERT_TIMINGS").is_none_or(|value| value.is_empty() || value == "0") {
return;
}
for line in String::from_utf8_lossy(stdout).lines() {
if line.contains("Built ") || line.contains("Replayed ") {
output::line(
Stream::Err,
"aver-cert timing: ",
Style::Plain,
line.trim(),
Style::Plain,
);
}
}
output::plain(
Stream::Err,
&format!("aver-cert timing: {phase}: {:.1}s", elapsed.as_secs_f64()),
);
}
fn tail(text: &str, lines: usize) -> String {
let all = text.lines().collect::<Vec<_>>();
all[all.len().saturating_sub(lines)..].join("\n")
}
fn surface_build_failure(text: &str, lines: usize) -> String {
let is_progress_or_rollup = |trimmed: &str| {
trimmed.starts_with('✔')
|| trimmed.starts_with('ℹ')
|| trimmed.starts_with('⚠')
|| trimmed.starts_with("info:")
|| trimmed.starts_with("warning:")
|| trimmed.starts_with("Some required targets")
};
let is_file_diagnostic =
|trimmed: &str| trimmed.starts_with("error:") && trimmed.contains(".lean:");
let mut diagnostics: Vec<&str> = Vec::new();
let mut capturing = false;
for line in text.lines() {
let trimmed = line.trim_start();
if is_file_diagnostic(trimmed) {
capturing = true;
diagnostics.push(line);
} else if is_progress_or_rollup(trimmed)
|| (trimmed.starts_with("error:") && !is_file_diagnostic(trimmed))
{
capturing = false;
} else if capturing {
diagnostics.push(line);
}
}
if diagnostics.is_empty() {
return tail(text, lines.max(200));
}
diagnostics.join("\n")
}
pub(crate) fn shown_path(path: &Path) -> String {
format!("{path:?}")
}
fn display_safe(value: &str) -> String {
value
.chars()
.map(|character| {
if character.is_control() {
'?'
} else {
character
}
})
.collect()
}
const INT_INPUT_DOMAIN_LINE: &str = "domain: every Int input (an argument, or a field, \
element or payload inside one) is assumed to be a canonical carrier word, the \
runtime's normal form; a non-canonical word is outside the certified domain. Every \
Int result is proved canonical.";
fn section(title: &'static str, style: Style) {
output::plain(Stream::Out, "");
output::line(Stream::Out, title, style, "", Style::Plain);
}
pub fn explain(artifact: &Path, cert_dir: &Path) -> Result<Explanation, String> {
let report = trusted_check(artifact, cert_dir, ReplayMode::Fresh)?;
output::line(
Stream::Out,
"Artifact certificate",
Style::Bold,
"",
Style::Plain,
);
output::plain(
Stream::Out,
&format!(" artifact: {}", shown_path(artifact)),
);
output::plain(
Stream::Out,
&format!(" pinned sha256: {}", report.artifact_hash),
);
output::plain(
Stream::Out,
&format!(
" target: {} profile: {} abi: {}",
report.target, report.profile, report.abi
),
);
if report.exports.is_empty() {
section("NO CERTIFIED EXPORTS", Style::Yellow);
return Ok(Explanation::NoExports);
}
section("CERTIFIED", Style::Green);
for export in report.exports {
output::line(Stream::Out, " ", Style::Plain, &export.name, Style::Bold);
output::plain(Stream::Out, &format!(" policy: {}", export.policy));
output::plain(Stream::Out, &format!(" {}", export.face));
output::plain(Stream::Out, &format!(" {}", export.certified_model));
}
section("Certified domain", Style::Yellow);
output::plain(Stream::Out, &format!(" {INT_INPUT_DOMAIN_LINE}"));
if !report.contracts.is_empty() {
section("Runtime contracts", Style::Yellow);
for contract in report.contracts {
output::plain(Stream::Out, &format!(" - {contract}"));
}
}
let manifest = report.manifest;
if let Some(laws) = manifest.get("laws").and_then(Value::as_array)
&& !laws.is_empty()
{
section("LAW-CLAIMS", Style::Green);
for entry in laws {
let label = entry
.get("label")
.and_then(Value::as_str)
.map(display_safe)
.unwrap_or_else(|| "<unknown>".to_string());
let statement = entry
.get("statement")
.and_then(Value::as_str)
.map(display_safe)
.unwrap_or_else(|| "<unknown>".to_string());
output::line(Stream::Out, " ", Style::Plain, &label, Style::Bold);
output::plain(Stream::Out, &format!(" {statement}"));
}
output::plain(
Stream::Out,
" these are the DECLARED claims; per-claim credit is decided by \
`aver cert check` / `aver cert verify`",
);
}
if !report.source_bridges.is_empty() {
section("SOURCE-BRIDGES", Style::Green);
for bridge in &report.source_bridges {
let credit = if bridge.offending.is_empty() {
"credited".to_string()
} else {
format!(
"NOT credited (proof depends on {})",
display_safe(&bridge.offending.join(", "))
)
};
output::plain(
Stream::Out,
&format!(
" {} ≡ {} ({}) [{credit}]",
display_safe(&bridge.export),
display_safe(&bridge.model),
bridge.kind.tag()
),
);
output::plain(
Stream::Out,
&format!(" {}", display_safe(&bridge.statement)),
);
}
output::plain(
Stream::Out,
" the statement under each bridge is RENDERED BY THE CHECKER from the \
manifest's declared structure, never read from the package; a credited \
bridge is one whose proof of exactly that statement uses no axiom \
outside the kernel whitelist",
);
}
if let Some(declined) = manifest
.get("sourceBridgesDeclined")
.and_then(Value::as_array)
&& !declined.is_empty()
{
section("SOURCE-BRIDGES DECLINED (informational)", Style::Yellow);
for entry in declined {
let export = entry
.get("export")
.and_then(Value::as_str)
.map(display_safe)
.unwrap_or_else(|| "<unknown>".to_string());
let reason = entry
.get("reason")
.and_then(Value::as_str)
.map(display_safe)
.unwrap_or_else(|| "unspecified".to_string());
output::plain(Stream::Out, &format!(" {export}: {reason}"));
}
output::plain(
Stream::Out,
" these exports keep `model: plan`; the reasons are declared, not checked",
);
}
if let Some(declined) = manifest.get("source_level_only").and_then(Value::as_array)
&& !declined.is_empty()
{
section("DECLINED (informational)", Style::Yellow);
for entry in declined {
let name = entry
.get("name")
.and_then(Value::as_str)
.map(display_safe)
.unwrap_or_else(|| "<unknown>".to_string());
let reason = entry
.get("reason")
.and_then(Value::as_str)
.map(display_safe)
.unwrap_or_else(|| "unspecified".to_string());
output::plain(Stream::Out, &format!(" {name}: {reason}"));
}
}
Ok(Explanation::Certified)
}
#[cfg(test)]
mod tests {
use super::*;
fn law_candidates(labels: &[&str]) -> Vec<LawCandidate> {
labels
.iter()
.map(|label| LawCandidate {
label: (*label).to_string(),
theorem: format!("Domain.{label}"),
statement: "True".to_string(),
corollary: label.replace('.', "_"),
prefix: "Domain".to_string(),
bridges: Vec::new(),
})
.collect()
}
fn audit_line(index: usize, verdict: &str) -> String {
format!(
"CheckerWitness.lean:9:0: info: {LAW_AUDIT_MARKER} {LAW_PIN_PREFIX}{index} {verdict}"
)
}
#[test]
fn law_audit_credits_only_pins_the_witness_reported_clean() {
let laws = law_candidates(&["A.one", "A.two"]);
let output = format!(
"building CheckerWitness\n{}\n{}\n",
audit_line(0, "ok"),
audit_line(1, "axioms sorryAx"),
);
let outcomes = parse_law_audits(&output, &laws).expect("well-formed audit parses");
assert_eq!(outcomes.len(), 2);
assert_eq!(outcomes[0].label, "A.one");
assert!(outcomes[0].offending.is_empty());
assert_eq!(outcomes[1].label, "A.two");
assert_eq!(outcomes[1].offending, vec!["sorryAx".to_string()]);
}
#[test]
fn law_audit_names_every_offending_axiom() {
let laws = law_candidates(&["A.one"]);
let output = audit_line(0, "axioms sorryAx,Lean.ofReduceBool,Domain.myAxiom");
let outcomes = parse_law_audits(&output, &laws).expect("multi-axiom audit parses");
assert_eq!(
outcomes[0].offending,
vec![
"sorryAx".to_string(),
"Lean.ofReduceBool".to_string(),
"Domain.myAxiom".to_string()
]
);
}
#[test]
fn law_audit_without_a_line_declines_instead_of_crediting() {
let laws = law_candidates(&["A.one", "A.two"]);
let error = parse_law_audits(&audit_line(0, "ok"), &laws).unwrap_err();
assert!(
error.contains("no axiom audit for law-claim `A.two`")
&& error.contains("refusing to credit an unaudited claim"),
"the decline names the unaudited claim: {error}"
);
let renamed = audit_line(0, "ok").replace(LAW_AUDIT_MARKER, "AVER_LAW_TRACE");
assert!(parse_law_audits(&renamed, &law_candidates(&["A.one"])).is_err());
}
#[test]
fn law_audit_rejects_malformed_and_repeated_lines() {
let laws = law_candidates(&["A.one"]);
for bad in [
audit_line(0, ""),
audit_line(0, "axioms"),
audit_line(0, "ok extra"),
audit_line(0, "clean"),
audit_line(0, "axioms sorryAx,"),
audit_line(9, "ok"),
] {
assert!(
parse_law_audits(&bad, &laws).is_err(),
"malformed audit line must decline: {bad}"
);
}
let repeated = format!("{}\n{}\n", audit_line(0, "ok"), audit_line(0, "ok"));
let error = parse_law_audits(&repeated, &laws).unwrap_err();
assert!(
error.contains("more than once"),
"a repeated pin declines: {error}"
);
}
#[test]
fn law_audit_keyword_separates_credit_from_an_axiom_named_ok() {
let laws = law_candidates(&["A.one"]);
let outcomes = parse_law_audits(&audit_line(0, "axioms ok"), &laws)
.expect("an axiom named `ok` still parses");
assert_eq!(outcomes[0].offending, vec!["ok".to_string()]);
}
fn raw_bridge(export: &str) -> RawSourceBridge {
RawSourceBridge {
export: export.to_string(),
theorem: format!("{BRIDGE_NAMESPACE}.{export}"),
corollary: format!("{BRIDGE_NAMESPACE}.{export}{BRIDGE_COROLLARY_SUFFIX}"),
model: format!("Domain.{export}"),
kind: BridgeKind::Exact,
params: vec![SourceEncoder::Int],
result: SourceEncoder::Int,
}
}
fn record_encoder(accessors: &[&str]) -> SourceEncoder {
SourceEncoder::Record {
tid: 0,
lean_type: "_root_.Domain.Fraction".to_string(),
fields: accessors
.iter()
.map(|accessor| (accessor.to_string(), SourceEncoder::Int))
.collect(),
}
}
fn fraction_encoder() -> SourceEncoder {
record_encoder(&[
"_root_.Domain.Fraction.top",
"_root_.Domain.Fraction.bottom",
])
}
fn bridge_candidates(exports: &[&str]) -> Vec<SourceBridgeCandidate> {
exports
.iter()
.map(|export| {
validate_source_bridge_candidate(raw_bridge(export))
.expect("an honest bridge entry validates")
})
.collect()
}
fn bridge_audit_line(index: usize, verdict: &str) -> String {
format!(
"CheckerWitness.lean:9:0: info: {BRIDGE_AUDIT_MARKER} \
{BRIDGE_PIN_PREFIX}{index} {verdict}"
)
}
#[test]
fn bridge_audit_credits_only_pins_the_witness_reported_clean() {
let bridges = bridge_candidates(&["one", "two"]);
let output = format!(
"building CheckerWitness\n{}\n{}\n",
bridge_audit_line(0, "ok"),
bridge_audit_line(1, "axioms sorryAx"),
);
let outcomes = parse_bridge_audits(&output, &bridges).expect("well-formed audit parses");
assert_eq!(outcomes.len(), 2);
assert!(outcomes[0].offending.is_empty());
assert_eq!(outcomes[1].export, "two");
assert_eq!(outcomes[1].offending, vec!["sorryAx".to_string()]);
}
#[test]
fn bridge_audit_without_a_line_declines_instead_of_crediting() {
let bridges = bridge_candidates(&["one", "two"]);
let error = parse_bridge_audits(&bridge_audit_line(0, "ok"), &bridges).unwrap_err();
assert!(
error.contains("no axiom audit for source-bridge `two`")
&& error.contains("refusing to credit an unaudited bridge"),
"the decline names the unaudited bridge: {error}"
);
let renamed = bridge_audit_line(0, "ok").replace(BRIDGE_AUDIT_MARKER, LAW_AUDIT_MARKER);
assert!(parse_bridge_audits(&renamed, &bridge_candidates(&["one"])).is_err());
}
#[test]
fn bridge_audit_rejects_malformed_and_repeated_lines() {
let bridges = bridge_candidates(&["one"]);
for bad in [
bridge_audit_line(0, ""),
bridge_audit_line(0, "axioms"),
bridge_audit_line(0, "ok extra"),
bridge_audit_line(0, "clean"),
bridge_audit_line(0, "axioms sorryAx,"),
bridge_audit_line(9, "ok"),
] {
assert!(
parse_bridge_audits(&bad, &bridges).is_err(),
"malformed audit line must decline: {bad}"
);
}
let repeated = format!(
"{}\n{}\n",
bridge_audit_line(0, "ok"),
bridge_audit_line(0, "ok")
);
assert!(
parse_bridge_audits(&repeated, &bridges)
.unwrap_err()
.contains("more than once"),
"a repeated pin declines"
);
}
#[test]
fn bridge_candidate_gate_pins_the_two_declaration_names_and_the_encoders() {
assert!(validate_source_bridge_candidate(raw_bridge("one")).is_ok());
let mut wrong_theorem = raw_bridge("one");
wrong_theorem.theorem = "AverCert.Laws.one".to_string();
assert!(validate_source_bridge_candidate(wrong_theorem).is_err());
let mut wrong_corollary = raw_bridge("one");
wrong_corollary.corollary = "AverCert.Bridge.one".to_string();
assert!(validate_source_bridge_candidate(wrong_corollary).is_err());
let mut dotted_export = raw_bridge("one");
dotted_export.export = "Domain.one".to_string();
assert!(validate_source_bridge_candidate(dotted_export).is_err());
let mut bad_model = raw_bridge("one");
bad_model.model = "Domain one".to_string();
assert!(validate_source_bridge_candidate(bad_model).is_err());
let mut bare_names = raw_bridge("one");
bare_names.result = SourceEncoder::Record {
tid: 0,
lean_type: "Domain.Fraction".to_string(),
fields: vec![("Domain.Fraction.top".to_string(), SourceEncoder::Int)],
};
assert!(validate_source_bridge_candidate(bare_names).is_err());
let mut foreign_accessor = raw_bridge("one");
foreign_accessor.result = record_encoder(&["_root_.Other.Record.top"]);
assert!(validate_source_bridge_candidate(foreign_accessor).is_err());
let mut foreign_ctor = raw_bridge("one");
foreign_ctor.params = vec![SourceEncoder::Sum {
tid: 1,
lean_type: "_root_.Domain.Op".to_string(),
ctors: vec![("_root_.Domain.Tag.a".to_string(), Vec::new())],
}];
assert!(validate_source_bridge_candidate(foreign_ctor).is_err());
}
#[test]
fn a_keyword_escaped_model_name_passes_the_bridge_gate() {
let mut escaped = raw_bridge("Domain_Policy_none");
escaped.model = "Domain.Policy.none'".to_string();
let candidate =
validate_source_bridge_candidate(escaped).expect("the escaped model name is admitted");
assert!(
candidate.statement.contains("_root_.Domain.Policy.none'"),
"{}",
candidate.statement
);
for refused in [
"Domain.Policy.'none",
"Domain.Policy..none",
"Domain.Policy.none\u{ab}",
] {
let mut entry = raw_bridge("Domain_Policy_none");
entry.model = refused.to_string();
assert!(
validate_source_bridge_candidate(entry).is_err(),
"{refused}"
);
}
let mut primed_export = raw_bridge("Domain_Policy_none'");
primed_export.model = "Domain.Policy.none'".to_string();
assert!(validate_source_bridge_candidate(primed_export).is_err());
}
#[test]
fn a_bridge_statement_is_rendered_by_the_checker_not_declared() {
let mut entry = raw_bridge("Domain_Rational_plus");
entry.model = "Domain.Rational.plus".to_string();
entry.params = vec![fraction_encoder(), fraction_encoder()];
entry.result = fraction_encoder();
let candidate = validate_source_bridge_candidate(entry).expect("an honest entry validates");
assert_eq!(
candidate.statement,
render_bridge_statement(
"Domain_Rational_plus",
"Domain.Rational.plus",
BridgeKind::Exact,
&[fraction_encoder(), fraction_encoder()],
&fraction_encoder(),
),
"the pinned statement is the renderer's output and nothing else"
);
assert!(
candidate.statement.starts_with(
"_root_.AverCert.GrammarBridge.Exact _root_.AverCert.manifest \
\"Domain_Rational_plus\""
),
"the claim's left-hand side is the export's own obligation model: {}",
candidate.statement
);
let mut adequate = raw_bridge("Domain_Rational_plus");
adequate.model = "Domain.Rational.plus".to_string();
adequate.kind = BridgeKind::Adequate;
adequate.params = vec![fraction_encoder(), fraction_encoder()];
adequate.result = fraction_encoder();
let adequate = validate_source_bridge_candidate(adequate).expect("it still validates");
assert_ne!(adequate.statement, candidate.statement);
let mut renamed = raw_bridge("Domain_Rational_minus");
renamed.model = "Domain.Rational.plus".to_string();
renamed.params = vec![fraction_encoder(), fraction_encoder()];
renamed.result = fraction_encoder();
let renamed = validate_source_bridge_candidate(renamed).expect("it still validates");
assert_ne!(renamed.statement, candidate.statement);
let permuted = record_encoder(&[
"_root_.Domain.Fraction.bottom",
"_root_.Domain.Fraction.top",
]);
let mut swapped = raw_bridge("Domain_Rational_plus");
swapped.model = "Domain.Rational.plus".to_string();
swapped.params = vec![permuted, fraction_encoder()];
swapped.result = fraction_encoder();
let swapped = validate_source_bridge_candidate(swapped).expect("it still validates");
assert_ne!(swapped.statement, candidate.statement);
}
#[test]
fn bridge_encoder_kinds_outside_the_closed_set_decline() {
assert_eq!(
read_source_encoder(&serde_json::json!({"kind": "int"}), "e").unwrap(),
SourceEncoder::Int
);
assert_eq!(
read_source_encoder(&serde_json::json!({"kind": "bool"}), "e").unwrap(),
SourceEncoder::Bool
);
assert_eq!(
read_source_encoder(
&serde_json::json!({
"kind": "record",
"tid": 0,
"type": "_root_.Domain.Fraction",
"fields": [{"accessor": "_root_.Domain.Fraction.top", "encoder": {"kind": "int"}}],
}),
"e"
)
.unwrap(),
record_encoder(&["_root_.Domain.Fraction.top"])
);
let op = SourceEncoder::Sum {
tid: 1,
lean_type: "_root_.Domain.Op".to_string(),
ctors: vec![
("_root_.Domain.Op.add".to_string(), vec![SourceEncoder::Int]),
("_root_.Domain.Op.zero".to_string(), Vec::new()),
],
};
for encoder in [
SourceEncoder::Float,
SourceEncoder::Str,
fraction_encoder(),
op.clone(),
SourceEncoder::Option(Box::new(op.clone())),
SourceEncoder::Result {
ok: Box::new(SourceEncoder::Int),
err: Box::new(SourceEncoder::Str),
},
SourceEncoder::Tuple {
tid: 2,
elems: vec![SourceEncoder::Int, fraction_encoder()],
},
SourceEncoder::List(Box::new(SourceEncoder::Bool)),
SourceEncoder::Vector(Box::new(SourceEncoder::Int)),
] {
let json: Value = serde_json::from_str(&encoder.to_json()).expect("valid JSON");
assert_eq!(read_source_encoder(&json, "e").unwrap(), encoder);
}
let mut deep = serde_json::json!({"kind": "int"});
for _ in 0..bridge_statement::MAX_ENCODER_DEPTH {
deep = serde_json::json!({"kind": "option", "elem": deep});
}
for bad in [
serde_json::json!({"kind": "decimal"}),
serde_json::json!({}),
serde_json::json!({"kind": "int", "type": "_root_.Domain.Fraction"}),
serde_json::json!({"kind": "record", "tid": 0, "type": "_root_.Domain.Fraction"}),
serde_json::json!({"kind": "record", "tid": 0, "type": "_root_.Domain.Fraction", "fields": "top"}),
serde_json::json!({"kind": "record", "tid": 0, "type": "_root_.Domain.Fraction", "fields": [7]}),
serde_json::json!({"kind": "record", "type": "_root_.Domain.Fraction", "fields": []}),
serde_json::json!({"kind": "option"}),
serde_json::json!({"kind": "sum", "tid": 1, "type": "_root_.Domain.Op", "ctors": [{"ctor": "_root_.Domain.Op.a"}]}),
deep,
] {
assert!(
read_source_encoder(&bad, "e").is_err(),
"an encoder outside the closed set must decline: {bad}"
);
}
}
#[test]
fn a_bridge_entry_carrying_a_statement_field_declines() {
let fields = [
"export",
"theorem",
"corollary",
"model",
"kind",
"params",
"result",
];
let honest = serde_json::json!({
"export": "one",
"theorem": "AverCert.Bridge.one",
"corollary": "AverCert.Bridge.one_certified",
"model": "Domain.one",
"kind": "exact",
"params": [],
"result": {"kind": "int"},
});
assert!(exact_object_fields(&honest, "sourceBridges[0]", &fields).is_ok());
let mut smuggled = honest.clone();
smuggled["statement"] = serde_json::json!("_root_.Domain.one = _root_.Domain.one");
assert!(exact_object_fields(&smuggled, "sourceBridges[0]", &fields).is_err());
}
#[test]
fn bridge_clause_counts_credit_and_names_the_uncredited() {
let summary = summarize_report(
Path::new("app.wasm"),
TrustedReport {
exports: Vec::new(),
laws: Vec::new(),
bridged_laws: Vec::new(),
source_bridges: vec![
BridgeOutcome {
export: "one".to_string(),
model: "Domain.one".to_string(),
kind: BridgeKind::Exact,
statement: "_root_.One".to_string(),
offending: Vec::new(),
},
BridgeOutcome {
export: "two".to_string(),
model: "Domain.two".to_string(),
kind: BridgeKind::Adequate,
statement: "_root_.Two".to_string(),
offending: vec!["sorryAx".to_string()],
},
],
contracts: Vec::new(),
target: String::new(),
profile: String::new(),
abi: String::new(),
artifact_hash: String::new(),
manifest: Value::Null,
},
"checked",
);
assert_eq!(
summary.text,
"\"app.wasm\" (0 checked exports, level L1; source-bridges: 1 of 2 credited)"
);
assert_eq!(
summary.uncredited_bridges,
vec!["source-bridge not credited: two (proof depends on sorryAx)".to_string()]
);
}
#[test]
fn law_clause_is_absent_without_law_claims() {
let bare = summarize_report(
Path::new("app.wasm"),
TrustedReport {
exports: Vec::new(),
laws: Vec::new(),
bridged_laws: Vec::new(),
source_bridges: Vec::new(),
contracts: Vec::new(),
target: String::new(),
profile: String::new(),
abi: String::new(),
artifact_hash: String::new(),
manifest: Value::Null,
},
"checked",
);
assert_eq!(bare.text, "\"app.wasm\" (0 checked exports, level L1)");
assert!(bare.uncredited_laws.is_empty());
}
#[test]
fn law_clause_counts_credit_and_names_the_uncredited() {
let summary = summarize_report(
Path::new("app.wasm"),
TrustedReport {
exports: Vec::new(),
laws: vec![
LawOutcome {
label: "A.one".to_string(),
offending: Vec::new(),
},
LawOutcome {
label: "A.two".to_string(),
offending: vec!["sorryAx".to_string()],
},
],
bridged_laws: Vec::new(),
source_bridges: Vec::new(),
contracts: Vec::new(),
target: String::new(),
profile: String::new(),
abi: String::new(),
artifact_hash: String::new(),
manifest: Value::Null,
},
"checked",
);
assert_eq!(
summary.text,
"\"app.wasm\" (0 checked exports, level L1; law-claims: 1 of 2 credited)"
);
assert_eq!(
summary.uncredited_laws,
vec!["law-claim not credited: A.two (proof depends on sorryAx)".to_string()]
);
}
#[test]
fn bridged_law_clause_is_counted_apart_from_the_law_clause() {
let summary = summarize_report(
Path::new("app.wasm"),
TrustedReport {
exports: Vec::new(),
laws: vec![
LawOutcome {
label: "A.one".to_string(),
offending: Vec::new(),
},
LawOutcome {
label: "A.two".to_string(),
offending: Vec::new(),
},
],
bridged_laws: vec![
LawOutcome {
label: "A.one".to_string(),
offending: Vec::new(),
},
LawOutcome {
label: "A.two".to_string(),
offending: vec!["sorryAx".to_string()],
},
],
source_bridges: Vec::new(),
contracts: Vec::new(),
target: String::new(),
profile: String::new(),
abi: String::new(),
artifact_hash: String::new(),
manifest: Value::Null,
},
"checked",
);
assert_eq!(
summary.text,
"\"app.wasm\" (0 checked exports, level L1; law-claims: 2 of 2 credited; \
bridged-laws: 1 of 2 credited)"
);
assert!(summary.uncredited_laws.is_empty());
assert_eq!(
summary.uncredited_bridged_laws,
vec!["bridged law-claim not credited: A.two (proof depends on sorryAx)".to_string()]
);
}
#[test]
fn bridged_law_pins_are_numbered_over_the_bridged_claims_only() {
let mut laws = law_candidates(&["A.one", "A.two", "A.three"]);
laws[1].bridges = vec![0];
laws[2].bridges = vec![0];
assert_eq!(bridged_law_indices(&laws), vec![1, 2]);
let line = |index: usize, verdict: &str| {
format!(
"CheckerWitness.lean:9:0: info: {LAW_BRIDGE_AUDIT_MARKER} \
{BRIDGED_LAW_PIN_PREFIX}{index} {verdict}"
)
};
let outcomes = parse_bridged_law_audits(
&format!("{}\n{}\n", line(0, "ok"), line(1, "axioms sorryAx")),
&laws,
)
.expect("well-formed audit parses");
assert_eq!(outcomes.len(), 2);
assert_eq!(outcomes[0].label, "A.two");
assert!(outcomes[0].offending.is_empty());
assert_eq!(outcomes[1].label, "A.three");
assert_eq!(outcomes[1].offending, vec!["sorryAx".to_string()]);
assert!(parse_bridged_law_audits(&line(0, "ok"), &laws).is_err());
let renamed = line(0, "ok").replace(LAW_BRIDGE_AUDIT_MARKER, LAW_AUDIT_MARKER);
assert!(parse_bridged_law_audits(&format!("{renamed}\n{}", line(1, "ok")), &laws).is_err());
}
#[test]
fn report_face_prints_only_kernel_pinned_facts() {
let candidate = CertifiedCandidate {
name: "sumTo".to_string(),
class: format::PLAN_CLASS.to_string(),
facets: vec!["recursive".to_string(), "calls".to_string()],
policy: "simulatesModelTotally".to_string(),
policy_lean: ".simulatesModelTotally",
termination_lean: "none".to_string(),
};
assert_eq!(
report_face(&candidate),
"class: source-plan-v1 (recursive, calls)"
);
assert_eq!(
certified_model_line(&candidate, &[]),
"model: plan (the export's optimized MIR body)"
);
let bare = CertifiedCandidate {
facets: Vec::new(),
..candidate
};
assert_eq!(report_face(&bare), "class: source-plan-v1");
}
#[test]
fn module_names_are_plain() {
assert_eq!(lean_module_root("Artifact.lean").unwrap(), "Artifact");
assert!(lean_module_root("../Artifact.lean").is_err());
assert!(lean_module_root("A, `Injected.lean").is_err());
assert_eq!(
lean_module_root("Apps/Notepad/Store.lean").unwrap(),
"Apps.Notepad.Store"
);
assert!(lean_module_root("Apps/../X.lean").is_err());
assert!(lean_module_root("Apps/.lake/X.lean").is_err());
assert!(lean_module_root("Apps/Bad Name.lean").is_err());
assert!(lean_module_root("Apps/`Tick/X.lean").is_err());
assert!(lean_module_root("Apps/A,B/X.lean").is_err());
assert!(lean_module_root("Apps.Notepad/Store.lean").is_err());
assert!(lean_module_root("/Apps/Store.lean").is_err());
assert!(lean_module_root("Apps//Store.lean").is_err());
}
#[test]
fn staged_paths_that_collide_case_insensitively_are_rejected() {
let mut staged = std::collections::BTreeMap::new();
assert!(note_staged_path(&mut staged, "Apps/Store.lean").is_ok());
assert!(note_staged_path(&mut staged, "Apps/Other.lean").is_ok());
assert!(note_staged_path(&mut staged, "Manifest.lean").is_ok());
let error = note_staged_path(&mut staged, "apps/Store.lean").unwrap_err();
assert!(
error.contains("Apps/Store.lean")
&& error.contains("apps/Store.lean")
&& error.contains("collide case-insensitively"),
"collision error names both paths: {error}"
);
assert!(note_staged_path(&mut staged, "MANIFEST.lean").is_err());
}
#[test]
fn nested_walk_rejects_unbounded_depth() {
let root = std::env::temp_dir().join(format!(
"aver-cert-depth-cap-{}-{}",
std::process::id(),
unique_nanos()
));
let mut deep = root.clone();
for _ in 0..(MAX_NESTED_DEPTH + 2) {
deep = deep.join("D");
}
std::fs::create_dir_all(&deep).unwrap();
std::fs::write(deep.join("X.lean"), "def x : Nat := 0\n").unwrap();
let mut out = Vec::new();
let error = collect_nested_lean_files(&root.join("D"), "D", 1, &mut out).unwrap_err();
assert!(
error.contains("maximum nesting depth"),
"depth cap error names the gate: {error}"
);
let _ = std::fs::remove_dir_all(root);
}
#[test]
fn import_line_scan_is_literal() {
let mut admitted = std::collections::BTreeSet::new();
collect_import_lines(
"import Schema\n import Apps.Notepad.Store \n/-\nimport Inside.Block.Comment\n-/\n-- import Behind.Line.Comment\nnot an import\nimport\n",
&mut admitted,
);
assert!(admitted.contains("Schema"));
assert!(admitted.contains("Apps.Notepad.Store"));
assert!(admitted.contains("Inside.Block.Comment"));
assert!(!admitted.contains("Behind.Line.Comment"));
assert_eq!(admitted.len(), 3);
}
#[test]
fn audit_namespace_roots_cover_every_wall_namespace() {
let wall = wall::resolve(wall::current_id()).expect("embedded wall resolves");
for source in wall.sources {
let mut depth = 0usize;
for line in source.contents.lines() {
let words: Vec<&str> = line.split_whitespace().collect();
match words.as_slice() {
["namespace", name, ..] => {
if depth == 0 {
let root = name.split('.').next().unwrap();
assert!(
root == "AverCert" || WALL_NAMESPACE_ROOTS.contains(&root),
"{} opens namespace {name}, whose root the audit does not reserve",
source.name
);
}
depth += 1;
}
["section", ..] | ["noncomputable", "section", ..] | ["mutual", ..] => {
depth += 1
}
["end", ..] => depth = depth.saturating_sub(1),
_ => {}
}
}
}
let rendered = wall::render_module(&"0".repeat(64));
assert!(rendered.contains("namespace CertModule"));
assert!(WALL_NAMESPACE_ROOTS.contains(&"CertModule"));
}
#[test]
fn nested_roots_shadowing_reserved_prefixes_are_rejected() {
let wall = wall::resolve(wall::current_id()).expect("embedded wall resolves");
assert!(reject_shadowed_root("Schema", wall).is_err());
assert!(reject_shadowed_root("Manifest", wall).is_ok());
assert!(reject_shadowed_root("Lean.Extra", wall).is_err());
assert!(reject_shadowed_root("Schema.Sub", wall).is_err());
assert!(reject_shadowed_root("ArtifactBytes.Decoy", wall).is_err());
assert!(reject_shadowed_root("ArtifactComponentBytes.Decoy", wall).is_err());
assert!(reject_shadowed_root("Module", wall).is_err());
assert!(reject_shadowed_root("Module.Decoy", wall).is_err());
assert!(reject_shadowed_root("CheckerWitness.X.Y", wall).is_err());
assert!(reject_shadowed_root("Apps.Schema", wall).is_ok());
assert!(reject_shadowed_root("Apps.Notepad.Store", wall).is_ok());
}
#[test]
fn wasm_gc_identity_selects_wasm_gc_artifact_preparation() {
assert_eq!(
require_supported_identity(&ManifestIdentity {
target: format::TARGET_WASM_GC.to_string(),
profile: format::PROFILE_ID.to_string(),
abi: format::RUNTIME_ABI_WASM_GC.to_string(),
})
.unwrap(),
ArtifactTarget::WasmGc
);
}
#[test]
fn wasip2_identity_selects_wasip2_artifact_preparation() {
assert_eq!(
require_supported_identity(&ManifestIdentity {
target: format::TARGET_WASIP2.to_string(),
profile: format::PROFILE_ID.to_string(),
abi: format::RUNTIME_ABI_WASIP2.to_string(),
})
.unwrap(),
ArtifactTarget::Wasip2
);
}
#[test]
fn target_identity_rejects_wrong_target_abi_pairs() {
let error = require_supported_identity(&ManifestIdentity {
target: format::TARGET_WASIP2.to_string(),
profile: format::PROFILE_ID.to_string(),
abi: format::RUNTIME_ABI_WASM_GC.to_string(),
})
.unwrap_err();
assert!(error.contains("unsupported certificate ABI"), "{error}");
assert!(error.contains(format::TARGET_WASIP2), "{error}");
}
#[test]
fn target_artifact_preparation_keeps_schema_six_on_core_wasm_modules() {
let bytes = b"\0asm\x01\0\0\0";
let prepared = prepare_artifact_for_target(ArtifactTarget::WasmGc, bytes, None)
.expect("empty wasm module is a valid schema-6 wasm-gc artifact envelope");
assert_eq!(prepared.artifact_hash, sha256_hex(bytes));
assert_eq!(prepared.target_artifact_bytes, bytes);
assert_eq!(prepared.core_module_bytes, bytes);
}
#[test]
fn wasm_gc_artifact_preparation_still_rejects_non_wasm_bytes() {
let error = prepare_artifact_for_target(ArtifactTarget::WasmGc, b"not a wasm module", None)
.unwrap_err();
assert!(
error.contains("artifact is not valid WebAssembly"),
"{error}"
);
}
#[test]
fn wasip2_missing_envelope_rejects_before_wasm_validation() {
let root = std::env::temp_dir().join(format!(
"aver-cert-wasip2-missing-envelope-{}-{}",
std::process::id(),
unique_nanos()
));
std::fs::create_dir_all(&root).unwrap();
let artifact = root.join("artifact.component.wasm");
std::fs::write(&artifact, b"not even wasm").unwrap();
std::fs::write(
root.join("cert-manifest.json"),
format!(
"{{\n \"schema_version\": {},\n \"target\": \"{}\",\n \"profile\": \"{}\",\n \"abi\": \"{}\"\n}}\n",
format::CERT_SCHEMA_VERSION,
format::TARGET_WASIP2,
format::PROFILE_ID,
format::RUNTIME_ABI_WASIP2
),
)
.unwrap();
let error = match trusted_check(&artifact, &root, ReplayMode::TrustBuiltOleans) {
Ok(_) => panic!("wasip2 target must require its manifest envelope"),
Err(error) => error,
};
assert!(
error.contains("cert-manifest.json is missing object field `wasip2ComponentEnvelope`"),
"{error}"
);
assert!(!error.contains("WebAssembly"), "{error}");
let _ = std::fs::remove_dir_all(root);
}
#[test]
fn wasip2_envelope_manifest_parser_reads_declared_lengths() {
let manifest = serde_json::json!({
"wasip2ComponentEnvelope": {
"kind": format::WASIP2_COMPONENT_ENVELOPE_KIND,
"prefix_len": 10,
"embedded_core_module_len": 8,
"suffix_len": 3
}
});
let declaration = read_wasip2_component_envelope(&manifest).unwrap();
assert_eq!(
declaration.inner,
format::Wasip2ComponentEnvelopeDeclaration::from_lengths(10, 8, 3)
);
}
#[test]
fn wasip2_envelope_manifest_parser_rejects_bad_shape() {
let missing = read_wasip2_component_envelope(&serde_json::json!({})).unwrap_err();
assert!(missing.contains("wasip2ComponentEnvelope"), "{missing}");
let bad_kind = read_wasip2_component_envelope(&serde_json::json!({
"wasip2ComponentEnvelope": {
"kind": "prefix-core-suffix/v2",
"prefix_len": 10,
"embedded_core_module_len": 8,
"suffix_len": 0
}
}))
.unwrap_err();
assert!(
bad_kind.contains("unsupported wasip2 component envelope kind"),
"{bad_kind}"
);
let non_u64 = read_wasip2_component_envelope(&serde_json::json!({
"wasip2ComponentEnvelope": {
"kind": format::WASIP2_COMPONENT_ENVELOPE_KIND,
"prefix_len": "10",
"embedded_core_module_len": 8,
"suffix_len": 0
}
}))
.unwrap_err();
assert!(
non_u64.contains("prefix_len") && non_u64.contains("u64"),
"{non_u64}"
);
}
#[test]
fn wasip2_declared_envelope_preparation_splits_component_without_discovery() {
let core = b"\0asm\x01\0\0\0";
let mut component = component_with_embedded_core(core);
let prefix_len = component.len() - core.len();
let instance_section = [2, 4, 1, 0, 0, 0];
component.extend_from_slice(&instance_section);
let declaration = Wasip2EnvelopeDeclaration {
inner: format::Wasip2ComponentEnvelopeDeclaration::from_lengths(
u64::try_from(prefix_len).unwrap(),
u64::try_from(core.len()).unwrap(),
u64::try_from(instance_section.len()).unwrap(),
),
};
let prepared = prepare_wasip2_artifact_with_declared_envelope(&component, declaration)
.expect("declared component envelope is valid");
assert_eq!(prepared.artifact_hash, sha256_hex(&component));
assert_eq!(prepared.core_module_bytes, core);
}
#[test]
fn wasip2_declared_envelope_preparation_refuses_a_core_the_component_never_instantiates() {
let core = b"\0asm\x01\0\0\0";
let component = component_with_embedded_core(core);
let error = prepare_wasip2_artifact_with_declared_envelope(
&component,
Wasip2EnvelopeDeclaration {
inner: format::Wasip2ComponentEnvelopeDeclaration::from_lengths(
u64::try_from(component.len() - core.len()).unwrap(),
u64::try_from(core.len()).unwrap(),
0,
),
},
)
.unwrap_err();
assert!(error.contains("never instantiated"), "{error}");
}
#[test]
fn wasip2_declared_envelope_preparation_rejects_bad_lengths() {
let core = b"\0asm\x01\0\0\0";
let component = component_with_embedded_core(core);
let empty_core = prepare_wasip2_artifact_with_declared_envelope(
&component,
Wasip2EnvelopeDeclaration {
inner: format::Wasip2ComponentEnvelopeDeclaration::from_lengths(
u64::try_from(component.len()).unwrap(),
0,
0,
),
},
)
.unwrap_err();
assert!(empty_core.contains("empty embedded core"), "{empty_core}");
let mismatch = prepare_wasip2_artifact_with_declared_envelope(
&component,
Wasip2EnvelopeDeclaration {
inner: format::Wasip2ComponentEnvelopeDeclaration::from_lengths(0, 8, 0),
},
)
.unwrap_err();
assert!(mismatch.contains("length mismatch"), "{mismatch}");
let overflow = prepare_wasip2_artifact_with_declared_envelope(
&component,
Wasip2EnvelopeDeclaration {
inner: format::Wasip2ComponentEnvelopeDeclaration::from_lengths(u64::MAX, 1, 0),
},
)
.unwrap_err();
assert!(overflow.contains("length overflow"), "{overflow}");
}
#[test]
fn wasip2_declared_envelope_preparation_rejects_non_module_core() {
let empty_component = b"\0asm\x0d\0\x01\0";
let error = prepare_wasip2_artifact_with_declared_envelope(
empty_component,
Wasip2EnvelopeDeclaration {
inner: format::Wasip2ComponentEnvelopeDeclaration::from_lengths(0, 8, 0),
},
)
.unwrap_err();
assert!(error.contains("not a core WebAssembly module"), "{error}");
}
fn component_with_embedded_core(core: &[u8]) -> Vec<u8> {
let mut component = b"\0asm\x0d\0\x01\0".to_vec();
component.push(1);
push_u32_leb(core.len().try_into().unwrap(), &mut component);
component.extend_from_slice(core);
component
}
fn push_u32_leb(mut value: u32, bytes: &mut Vec<u8>) {
loop {
let mut byte = (value & 0x7f) as u8;
value >>= 7;
if value != 0 {
byte |= 0x80;
}
bytes.push(byte);
if value == 0 {
break;
}
}
}
#[test]
fn printed_paths_escape_control_characters() {
let path = Path::new("dir/a\nCERTIFIED fake\r\t\u{1b}[32m\".wasm");
let shown = shown_path(path);
assert_eq!(shown, r#""dir/a\nCERTIFIED fake\r\t\u{1b}[32m\".wasm""#);
assert!(!shown.chars().any(char::is_control), "{shown}");
}
#[test]
fn a_declined_verdict_does_not_print_raw_control_characters_from_paths() {
let root = std::env::temp_dir().join(format!(
"aver-cert-path-escape-{}-{}",
std::process::id(),
unique_nanos()
));
let artifact = root.join("x\n law-claims: 99 of 99 credited\r\t\u{1b}.wasm");
let error = match trusted_check(&artifact, &root, ReplayMode::TrustBuiltOleans) {
Ok(_) => panic!("a missing artifact must not verify"),
Err(error) => error,
};
assert!(error.contains("cannot read artifact"), "{error}");
assert!(
error.contains(r"\n law-claims: 99 of 99 credited\r\t\u{1b}"),
"{error}"
);
assert!(
!error.contains('\n') && !error.contains('\r') && !error.contains('\u{1b}'),
"{error}"
);
}
#[test]
fn candidate_strings_cannot_escape_lean_literals() {
assert!(gate_candidate("test", "plain ASCII").is_ok());
assert!(gate_candidate("test", "quote: \"").is_err());
assert!(gate_candidate("test", "line\nbreak").is_err());
}
#[test]
fn artifact_bytes_are_little_endian_nat() {
let rendered = wall::render_artifact_bytes(&[0x00, 0x61, 0x73, 0x6d]);
assert!(rendered.contains("noncomputable def chunks : List Nat :=\n [0x6d736100]\n"));
assert!(rendered.contains(
"noncomputable def modBytes : Nat :=\n AverCert.ScaleBytes.joinTree 1024 32 chunks\n"
));
assert!(rendered.contains("def modLen : Nat := 4"));
let mut long = vec![0u8; 2049];
long[0] = 0x01;
long[1023] = 0xcd;
long[1024] = 0xab;
long[2048] = 0xef;
let rendered = wall::render_artifact_bytes(&long);
let first = format!("0xcd{}01", "00".repeat(1022));
let second = format!("0x{}ab", "00".repeat(1023));
assert!(rendered.contains(&format!(
"noncomputable def chunks : List Nat :=\n [{first},\n {second},\n 0xef]\n"
)));
assert!(rendered.contains("def modLen : Nat := 2049"));
let empty = wall::render_artifact_bytes(&[]);
assert!(empty.contains("noncomputable def chunks : List Nat :=\n []\n"));
assert!(empty.contains("def modLen : Nat := 0"));
}
#[test]
fn strict_and_trusted_olean_modes_differ_only_at_fresh_replay_dispatch() {
let stock: Vec<String> = FRESH_REPLAY_ARGS.iter().map(|a| (*a).to_string()).collect();
assert_eq!(replay_args_for(ReplayMode::Fresh, None), Some(stock));
assert_eq!(replay_args_for(ReplayMode::TrustBuiltOleans, None), None);
}
#[test]
fn the_replayer_override_is_off_unless_it_names_a_binary() {
let stock: Vec<String> = FRESH_REPLAY_ARGS.iter().map(|a| (*a).to_string()).collect();
for absent in [None, Some(""), Some(" ")] {
assert_eq!(
replay_args_for(ReplayMode::Fresh, absent),
Some(stock.clone()),
"override {absent:?} must not divert the replay"
);
}
let diverted = replay_args_for(ReplayMode::Fresh, Some("/opt/parreplay"))
.expect("fresh mode always replays");
assert_eq!(diverted[0], "env");
assert_eq!(diverted[1], "/opt/parreplay");
assert!(
diverted.contains(&"replay".to_string()),
"must request the single-pass mode, not the comparison mode: {diverted:?}"
);
assert!(
!diverted.contains(&"leanchecker".to_string()),
"stock replayer must not also run: {diverted:?}"
);
assert_eq!(
replay_args_for(ReplayMode::TrustBuiltOleans, Some("/opt/parreplay")),
None
);
}
fn scan(src: &str) -> Result<(), String> {
scan_for_code_exec("Test.lean", src.as_bytes())
}
#[test]
fn code_exec_scanner_is_lexically_context_aware() {
assert!(scan(r#"def x := "elab""#).is_ok());
let err = scan("elab foo").unwrap_err();
assert!(err.contains("elab"), "{err}");
assert!(scan(r#"def x := "« inside a string »""#).is_ok());
let err = scan("def «weird» := 0").unwrap_err();
assert!(err.contains('«'), "{err}");
assert!(scan("def relabel := 0").is_ok());
assert!(scan("def macroexpanded := 0").is_ok());
let err = scan("#eval IO.println \"x\"").unwrap_err();
assert!(err.contains("#eval"), "{err}");
assert!(scan("-- #eval IO.println \"x\"\ndef y := 0").is_ok());
assert!(scan("/- outer /- inner #eval -/ still -/\ndef y := 0").is_ok());
let err = scan("def x := \"unterminated #eval").unwrap_err();
assert!(err.contains("#eval"), "{err}");
assert!(scan("-- elab is only mentioned here\ndef y := 0").is_ok());
}
#[test]
fn code_exec_scanner_defaults_to_code_on_ambiguity() {
let err = scan("def c : Char := '\"'\n#eval evil").unwrap_err();
assert!(err.contains("#eval"), "{err}");
let err = scan("def c : Char := '\\\"'\n#eval evil").unwrap_err();
assert!(err.contains("#eval"), "{err}");
let err = scan("def x := r\"#eval evil\"").unwrap_err();
assert!(err.contains("#eval"), "{err}");
let err = scan("/- never closed #eval").unwrap_err();
assert!(err.contains("#eval"), "{err}");
let err = scan("elab\"x\"").unwrap_err();
assert!(err.contains("elab"), "{err}");
let err = scan("elab_rules foo").unwrap_err();
assert!(err.contains("elab_rules"), "{err}");
}
fn witness_candidates() -> Candidates {
let mut laws = law_candidates(&["Domain.plus.comm"]);
laws[0].bridges = vec![0];
let mut bridge = raw_bridge("Domain_plus");
bridge.model = "Domain.plus".to_string();
bridge.params = vec![fraction_encoder(), fraction_encoder()];
bridge.result = fraction_encoder();
Candidates {
certified: vec![CertifiedCandidate {
name: "Domain_plus".to_string(),
class: format::PLAN_CLASS.to_string(),
facets: vec!["recursive".to_string()],
policy: "simulatesModelTotally".to_string(),
policy_lean: ".simulatesModelTotally",
termination_lean: parse_termination(
Some(&serde_json::json!({
"measure": {"kind": "intNatAbs", "param_index": 1},
"descent": -3
})),
"Domain_plus",
)
.unwrap(),
}],
laws,
source_bridges: vec![validate_source_bridge_candidate(bridge).unwrap()],
contracts: vec!["c".to_string()],
declared_uncertified: Vec::new(),
capabilities: Vec::new(),
start: Some(7),
host_role_table: Some((Some(7), Some(8), None, None, None, None, None, Some(13))),
string_host_roles: vec![(21, StringHostRole::Eq)],
target: "wasm-gc".to_string(),
profile: "AverUserProfile/v1".to_string(),
abi: "aver-wasm-gc/0".to_string(),
wasip2_component_envelope: None,
}
}
#[test]
fn the_witness_names_everything_from_the_root() {
let witness = checker_witness("ab12", &witness_candidates());
assert!(!witness.contains("import Lean"), "{witness}");
assert!(!witness.contains("run_cmd") && !witness.contains("#eval"));
assert!(!witness.contains("namespace AverCertChecker"));
assert!(!witness.contains("namespace "), "{witness}");
for (at, _) in witness.match_indices("AverCert") {
let before = &witness[..at];
assert!(
before.ends_with("_root_.") || before.ends_with('"'),
"unqualified name at {at}: {}",
&witness[at.saturating_sub(40)..(at + 40).min(witness.len())]
);
}
assert!(witness.contains(&format!(
"theorem _root_.{CHECKED_ROOT} :\n \
_root_.AverCert.AcceptedArtifact.accepted _root_.AverCert.Artifact.data :=\n \
_root_.AverCert.Artifact.certificate"
)));
for index in 0..REPORT_PIN_COUNT {
assert!(witness.contains(&format!("theorem _root_.{REPORT_PIN_PREFIX}{index} :")));
}
for package_constant in [
"_root_.AverCert.manifest.",
"_root_.AverCert.subject.",
"_root_.AverCert.Artifact.data.",
"_root_.AverCert.Artifact.certificate.",
] {
assert!(
!witness.contains(package_constant),
"a dotted path past {package_constant}: {witness}"
);
}
assert!(witness.contains(
"(_root_.AverCert.Schema.Subject.contracts \
(_root_.AverCert.Schema.Manifest.subject _root_.AverCert.manifest)) = [\"c\"]"
));
assert!(witness.contains(
"(_root_.AverCert.AcceptedArtifact.ArtifactData.manifest \
_root_.AverCert.Artifact.data) = _root_.AverCert.manifest"
));
assert!(witness.contains("(_root_.Option.some (nat_lit 7))"));
assert!(witness.contains("(_root_.Int.negSucc (nat_lit 2))"));
assert!(witness.contains("((nat_lit 21), .eq)"));
assert!(!witness.contains("some 7") && !witness.contains("≤"));
}
#[test]
fn the_witness_conjoins_statement_definitions_not_statement_text() {
let candidates = witness_candidates();
let witness = checker_witness("ab12", &candidates);
let holds = "(_root_.AverCert.Schema.Holds _root_.AverCert.manifest)";
assert!(witness.contains(&format!(
"def _root_.{LAW_STATEMENT_PREFIX}0 : Prop :=\n ({})\n",
candidates.laws[0].statement
)));
assert!(witness.contains(&format!(
"def _root_.{BRIDGE_STATEMENT_PREFIX}0 : Prop :=\n ({})\n",
candidates.source_bridges[0].statement
)));
assert!(witness.contains(&format!(
"theorem _root_.{LAW_PIN_PREFIX}0 :\n _root_.{LAW_STATEMENT_PREFIX}0 ∧ {holds} :="
)));
assert!(witness.contains(&format!(
"theorem _root_.{BRIDGED_LAW_PIN_PREFIX}0 :\n \
_root_.{LAW_STATEMENT_PREFIX}0 ∧ {holds} ∧\n \
_root_.{BRIDGE_STATEMENT_PREFIX}0 :="
)));
assert!(witness.contains(&format!(
"theorem _root_.{BRIDGE_PIN_PREFIX}0 :\n _root_.{BRIDGE_STATEMENT_PREFIX}0 ∧ {holds} :="
)));
assert_eq!(
witness
.matches(candidates.laws[0].statement.as_str())
.count(),
1
);
}
#[test]
fn audit_axiom_memo_reports_each_root_as_a_fresh_walk() {
let toolchain = wall::LEAN_TOOLCHAIN.trim();
let available = std::process::Command::new("elan")
.args(["run", "--install", toolchain, "lake", "--version"])
.output();
if !matches!(available, Ok(output) if output.status.success()) {
assert!(
std::env::var_os("AVER_CERT_REQUIRE_PINNED_LEAN").is_none(),
"pinned Elan toolchain is required for the axiom memo test"
);
eprintln!("skipping the axiom memo test: pinned Elan toolchain unavailable");
return;
}
let begin = AUDIT_TEMPLATE
.find("-- The axiom memo begins")
.expect("the audit marks its axiom memo");
let end = AUDIT_TEMPLATE
.find("-- The axiom memo ends.")
.expect("the audit marks the end of its axiom memo");
let memo = &AUDIT_TEMPLATE[begin..end];
let probe = format!(
"import Lean\n\
open Lean\n\n\
namespace AverCertAudit\n{memo}\nend AverCertAudit\n\n\
axiom axA : True\n\
axiom axB : 1 = 1\n\
theorem onlyA : True := axA\n\
theorem onlyB : 1 = 1 := axB\n\
theorem shared : True ∧ 1 = 1 := ⟨onlyA, onlyB⟩\n\
theorem clean : 2 = 2 := rfl\n\
def TyB : Type := (fun (_ : 1 = 1) => Nat) axB\n\
inductive Cyc where\n | plain : Cyc\n | viaB : TyB → Cyc\n\
def usesCyc : Cyc := Cyc.plain\n\
def usesPlain : Cyc → Cyc := fun _ => Cyc.plain\n\n\
def roots : List Name :=\n \
[`Cyc, `usesPlain, `Cyc.plain, `onlyA, `shared, `onlyB, `clean, `usesCyc]\n\n\
#eval show CoreM Unit from do\n \
let env ← getEnv\n \
for order in [roots, roots.reverse] do\n \
let memo ← IO.mkRef ({{}} : AverCertAudit.AxiomMemo)\n \
for root in order do\n \
let fresh := (← collectAxioms root).qsort Name.lt\n \
let viaMemo ← AverCertAudit.axiomsOf env memo root\n \
unless fresh == viaMemo do\n \
throwError s!\"memo {{root}}: {{viaMemo}} but fresh {{fresh}}\"\n \
let memo ← IO.mkRef ({{}} : AverCertAudit.AxiomMemo)\n \
let expect (root : Name) (axs : Array Name) : CoreM Unit := do\n \
let got ← AverCertAudit.axiomsOf env memo root\n \
unless got == axs do throwError s!\"{{root}}: {{got}}, expected {{axs}}\"\n \
expect `Cyc #[`axB]\n \
expect `Cyc.plain #[`axB]\n \
expect `onlyA #[`axA]\n \
expect `onlyB #[`axB]\n \
expect `shared #[`axA, `axB]\n \
expect `clean #[]\n \
IO.println \"memo agrees\"\n"
);
let dir = std::env::temp_dir().join(format!(
"aver-cert-axiom-memo-{}-{}",
std::process::id(),
unique_nanos()
));
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("lean-toolchain"), wall::LEAN_TOOLCHAIN).unwrap();
std::fs::write(
dir.join("lakefile.lean"),
"import Lake\nopen Lake DSL\npackage «memoprobe» where\n",
)
.unwrap();
std::fs::write(dir.join("MemoProbe.lean"), probe).unwrap();
let output = crate::lean_process::LeanRunner::new(wall::LEAN_TOOLCHAIN)
.unwrap()
.run_lake(&dir, "axiom memo probe", &["env", "lean", "MemoProbe.lean"])
.unwrap();
let text = format!(
"{}{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
let _ = std::fs::remove_dir_all(&dir);
assert!(
output.status.success() && text.contains("memo agrees"),
"the axiom memo disagrees with a fresh walk:\n{text}"
);
}
#[test]
fn the_audit_program_walks_every_pin() {
let candidates = witness_candidates();
let audit = checker_audit(&candidates, &["Manifest".to_string(), "Laws".to_string()]);
assert!(!audit.contains('@'), "an unfilled placeholder: {audit}");
assert!(audit.contains("def packageModules : List Name := [`Manifest, `Laws]"));
assert!(audit.contains(&format!("`{CHECKED_ROOT}, `{REPORT_PIN_PREFIX}0,")));
assert!(audit.contains(&format!("`{REPORT_PIN_PREFIX}{}]", REPORT_PIN_COUNT - 1)));
assert!(audit.contains(&format!("[`{LAW_PIN_PREFIX}0]")));
assert!(audit.contains(&format!("[`{BRIDGED_LAW_PIN_PREFIX}0]")));
assert!(audit.contains(&format!("[`{BRIDGE_PIN_PREFIX}0]")));
assert!(
audit.contains("[(`Domain.Fraction, [`top, `bottom])]"),
"{audit}"
);
}
#[test]
fn the_fresh_replay_replays_the_witness_closure() {
assert_eq!(FRESH_REPLAY_ARGS[3], "CheckerWitness");
assert_eq!(
replay_args_for(ReplayMode::Fresh, None).unwrap(),
vec!["env", "leanchecker", "--fresh", "CheckerWitness"]
);
}
#[test]
fn a_law_lists_exactly_the_bridges_its_statement_names() {
let models = ["Domain.plus", "Domain.times"];
let qualified = "∀ (a : Int), _root_.Domain.times (_root_.Domain.plus a a) a = \
_root_.Domain.plus a a";
assert_eq!(
bridge_statement::law_mentioned_bridges(qualified, &models),
vec![1, 0]
);
assert_eq!(
bridge_statement::law_names_model_unqualified(qualified, &models),
None
);
assert!(bridge_statement::law_mentioned_bridges("∀ (a : Int), a = a", &models).is_empty());
for spelling in [
"∀ (a : Int), Domain.plus a a = a",
"∀ (a : Int), Evil.Domain.plus a a = a",
] {
assert!(bridge_statement::law_mentioned_bridges(spelling, &models).is_empty());
assert_eq!(
bridge_statement::law_names_model_unqualified(spelling, &models),
Some("Domain.plus")
);
}
}
#[test]
fn the_audit_checks_what_each_law_statement_uses() {
let mut candidates = witness_candidates();
candidates.laws[0].statement = "∀ (a : Int), _root_.Domain.plus a a = a".to_string();
candidates.laws[0].prefix = "Evil.Inner".to_string();
assert_eq!(
law_model_uses(&candidates),
format!("[(`{LAW_STATEMENT_PREFIX}0, [`Domain.plus])]")
);
let audit = checker_audit(&candidates, &["Laws".to_string()]);
assert!(!audit.contains("Evil.Domain.plus"), "{audit}");
assert!(!audit.contains("Evil.Inner.Domain.plus"), "{audit}");
}
#[test]
fn paranoid_replay_refuses_an_empty_export_and_a_missing_kernel() {
let toolchain = wall::LEAN_TOOLCHAIN.trim();
let available = std::process::Command::new("elan")
.args(["run", "--install", toolchain, "lake", "--version"])
.output();
if !matches!(available, Ok(output) if output.status.success()) {
assert!(
std::env::var_os("AVER_CERT_REQUIRE_PINNED_LEAN").is_none(),
"pinned Elan toolchain is required for this fail-closed gate"
);
eprintln!("skipping paranoid replay test: pinned Elan toolchain unavailable");
return;
}
let dir = std::env::temp_dir().join(format!(
"aver-cert-paranoid-{}-{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |d| d.as_nanos())
));
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("lean-toolchain"), wall::LEAN_TOOLCHAIN).unwrap();
std::fs::write(
dir.join("lakefile.lean"),
format!(
"import Lake\nopen Lake DSL\npackage «paranoidprobe» where\n\
@[default_target]\nlean_lib «Probe» where\n roots := #[`{WITNESS_MODULE}]\n"
),
)
.unwrap();
let runner = LeanRunner::new(wall::LEAN_TOOLCHAIN).unwrap();
let build = |source: &str| {
std::fs::write(dir.join(format!("{WITNESS_MODULE}.lean")), source).unwrap();
let built = runner.run_lake(&dir, "probe build", &["build"]).unwrap();
assert!(
built.status.success(),
"probe build failed:\n{}{}",
String::from_utf8_lossy(&built.stdout),
String::from_utf8_lossy(&built.stderr)
);
};
let missing = crate::kernels::Kernel {
name: "absent",
binary: "definitely-missing-kernel",
args: &[],
input: crate::kernels::KernelInput::ExportPath,
admits_sorry: true,
};
build("def Elsewhere.marker : Nat := 1\n");
let error = paranoid_replay(&runner, &dir, &[missing], &[]).unwrap_err();
assert!(error.contains("does not contain the witness"), "{error}");
build(&format!("def {WITNESS_NAMESPACE}.marker : Nat := 1\n"));
let error = paranoid_replay(&runner, &dir, &[missing], &[]).unwrap_err();
assert!(error.contains("`definitely-missing-kernel`"), "{error}");
assert!(error.contains("not a skip"), "{error}");
let refusing: Vec<_> = (crate::kernels::PARANOID_KERNELS.into_iter())
.filter(|kernel| !kernel.admits_sorry)
.collect();
assert_eq!(refusing.len(), 3);
build(&format!(
"theorem {WITNESS_NAMESPACE}.open_claim : 1 = 2 := sorry\n\
theorem {WITNESS_NAMESPACE}.cites_open : 1 = 2 ∧ True := ⟨{WITNESS_NAMESPACE}.open_claim, trivial⟩\n\
theorem {WITNESS_NAMESPACE}.credited : 1 = 1 := rfl\n"
));
let credited = [format!("{WITNESS_NAMESPACE}.credited")];
paranoid_replay(&runner, &dir, &refusing, &credited).unwrap();
let credited = [format!("{WITNESS_NAMESPACE}.cites_open")];
let error = paranoid_replay(&runner, &dir, &refusing, &credited).unwrap_err();
assert!(
error.contains("`AverCertChecker.cites_open` depends on sorryAx"),
"{error}"
);
let _ = std::fs::remove_dir_all(&dir);
}
}