/- CheckReplay.lean GoCheckYourself (GCY-1) Public Verification Tool Zero-Mathlib Lean 4.34.1 anti-spoofing kernel-replay verifier (`trustLevel := 0`). Enforces: - No custom axioms, no opaque constants, no unsafe constants. - Permitted standard axioms strictly limited to `propext`, `Quot.sound` (add `Classical.choice` only when auditing classical modules explicitly). - Full kernel replay from scratch via `Lean.Kernel.Environment.replay`. Usage: lean --run CheckReplay.lean [TargetDecl2 ...] -/ import Lean open Lean def allowedAxioms : List Name := [`propext, `Quot.sound] partial def collectKernelAxioms (kenv : Kernel.Environment) (root : Name) : Array Name := let rec visit (stack : List Name) (seen : Std.HashSet Name) (axs : Array Name) : Array Name := match stack with | [] => axs | nm :: rest => if seen.contains nm then visit rest seen axs else let seen' := seen.insert nm match kenv.find? nm with | none => visit rest seen' axs | some (.axiomInfo _) => visit rest seen' (axs.push nm) | some cinfo => let deps := cinfo.getUsedConstantsAsSet.toList visit (deps ++ rest) seen' axs visit [root] {} #[] def main (args : List String) : IO Unit := do match args with | [] => IO.eprintln "Usage: lean --run CheckReplay.lean [Decl2 ...]" IO.Process.exit 2 | modStr :: declStrs => initSearchPath (← findSysroot) let modName := modStr.toName let env ← importModules #[{ module := modName }] {} (trustLevel := 0) let some modIdx := env.getModuleIdx? modName | throw <| IO.userError s!"Module {modName} not found in imported environment" let modData := env.header.moduleData[modIdx.toNat]! let mut newConsts : Std.HashMap Name ConstantInfo := {} let mut constCount : Nat := 0 for cName in modData.constNames do match env.find? cName with | none => throw <| IO.userError s!"Constant {cName} listed in module data but missing from env" | some cinfo => if cinfo.isUnsafe then throw <| IO.userError s!"REJECTED: unsafe constant {cName}" match cinfo with | .axiomInfo _ => throw <| IO.userError s!"REJECTED: custom axiom {cName}" | .opaqueInfo _ => throw <| IO.userError s!"REJECTED: opaque constant {cName}" | _ => pure () newConsts := newConsts.insert cName cinfo constCount := constCount + 1 let depImports := modData.imports.filter (fun imp => imp.module != modName) let baseEnv ← importModules depImports {} (trustLevel := 0) let replayedKEnv ← Kernel.Environment.replay newConsts baseEnv.toKernelEnv let mut classicalCount : Nat := 0 for dStr in declStrs do let dName := dStr.toName if (replayedKEnv.find? dName).isNone then throw <| IO.userError s!"REJECTED: target declaration {dName} missing after kernel replay" let axs := collectKernelAxioms replayedKEnv dName for ax in axs do if ax == `Classical.choice then classicalCount := classicalCount + 1 if !(allowedAxioms.contains ax) then throw <| IO.userError s!"REJECTED: disallowed axiom {ax} in {dName}" let axList := axs.toList.map toString IO.println s!" DECL_OK {dName} axioms=[{String.intercalate ", " axList}]" IO.println s!"KERNEL_REPLAY_OK module={modName} constants={constCount} targets={declStrs.length} classicalChoice={classicalCount} trustLevel=0"