|
| 1 | +import Lean |
| 2 | + |
| 3 | +namespace Verity.Compiler.FromSolidity |
| 4 | + |
| 5 | +open Lean Elab Command |
| 6 | + |
| 7 | +/-! |
| 8 | +# `#fromSolidity` Lean entry point — scaffold |
| 9 | +
|
| 10 | +Today the Verity compiler runs out-of-band via the `verity-cli` |
| 11 | +command-line tool: a developer manually invokes |
| 12 | +`verity-cli compile path/to/Foo.sol --emit lean` and copies the |
| 13 | +generated Lean term into the benchmark. |
| 14 | +
|
| 15 | +This module is the **scaffold** for surfacing that pipeline as a Lean |
| 16 | +elaborator-level command, so that a benchmark file can declare: |
| 17 | +
|
| 18 | +``` |
| 19 | +#fromSolidity "vendor/account-abstraction/EntryPoint.sol" |
| 20 | +``` |
| 21 | +
|
| 22 | +and obtain a proof-checked `verity_contract` term as if it had been |
| 23 | +hand-written. |
| 24 | +
|
| 25 | +## Status |
| 26 | +
|
| 27 | +This scaffold lands the public API surface (`#fromSolidity` command, |
| 28 | +options, error type) and a stub elaborator that defers to the CLI via |
| 29 | +`IO.Process.run`. The full machine translation requires hooking |
| 30 | +`Compiler/CompilationModel.lean`'s Solidity-IR pipeline directly into |
| 31 | +Lean's elaborator, which is a multi-week project tracked separately |
| 32 | +(see `docs/ROADMAP.md` "machine fromSolidity"). |
| 33 | +
|
| 34 | +The scaffold exists so: |
| 35 | +
|
| 36 | +* The API contract is fixed (the command name and option shape will not |
| 37 | + change underneath downstream benchmarks). |
| 38 | +* The CLI integration point is documented as a single function |
| 39 | + (`runVerityCliCompile`) that the next PR replaces with an in-process |
| 40 | + call. |
| 41 | +* Benchmarks that today write `verity_contract` blocks by hand can |
| 42 | + switch to `#fromSolidity` once the CLI shells out cleanly, without |
| 43 | + changing their proof files. |
| 44 | +
|
| 45 | +## Trust boundary today vs. after full implementation |
| 46 | +
|
| 47 | +Today (`fromSolidity` shells out to `verity-cli`): |
| 48 | +* Trust the CLI binary on disk. |
| 49 | +* Trust the file-system path is consistent with what the developer |
| 50 | + proved against. |
| 51 | +
|
| 52 | +After full implementation (in-process): |
| 53 | +* No CLI binary trust — translation runs inside Lean. |
| 54 | +* The same kernel checks the resulting term that checks every Lean |
| 55 | + proof. |
| 56 | +
|
| 57 | +The headline difference: the post-implementation form makes |
| 58 | +*translation faithfulness* part of the proof-checked trust base. |
| 59 | +-/ |
| 60 | + |
| 61 | +/-- Options for `#fromSolidity`. -/ |
| 62 | +structure Options where |
| 63 | + /-- Path to the Solidity source. Resolved relative to the workspace root. -/ |
| 64 | + path : System.FilePath |
| 65 | + /-- Optional contract name selector (defaults to the only contract in the file). -/ |
| 66 | + contractName : Option String := none |
| 67 | + /-- Verity CLI binary to invoke. Defaults to `verity-cli` on PATH. -/ |
| 68 | + cliBinary : String := "verity-cli" |
| 69 | + deriving Repr |
| 70 | + |
| 71 | +/-- Result of running the CLI translation step. -/ |
| 72 | +inductive Result where |
| 73 | + | ok (lean : String) |
| 74 | + | error (msg : String) |
| 75 | + deriving Repr |
| 76 | + |
| 77 | +/-- Locate the Lake workspace root containing the current process, if any. -/ |
| 78 | +partial def findWorkspaceRoot? (dir : System.FilePath) : IO (Option System.FilePath) := do |
| 79 | + if ← (dir / "lakefile.lean").pathExists then |
| 80 | + pure (some dir) |
| 81 | + else if ← (dir / "lakefile.toml").pathExists then |
| 82 | + pure (some dir) |
| 83 | + else |
| 84 | + match dir.parent with |
| 85 | + | some parent => |
| 86 | + if parent == dir then |
| 87 | + pure none |
| 88 | + else |
| 89 | + findWorkspaceRoot? parent |
| 90 | + | none => pure none |
| 91 | + |
| 92 | +/-- Resolve `path` according to the `Options.path` contract. |
| 93 | +
|
| 94 | +Absolute paths are preserved. Relative paths are anchored at the Lake workspace |
| 95 | +root containing the current process, falling back to the process working |
| 96 | +directory when the command is run outside a Lake workspace. -/ |
| 97 | +def resolveSourcePath (path : System.FilePath) : IO System.FilePath := do |
| 98 | + if path.isAbsolute then |
| 99 | + pure path.normalize |
| 100 | + else |
| 101 | + let cwd ← IO.currentDir |
| 102 | + let root ← findWorkspaceRoot? cwd |
| 103 | + pure (((root.getD cwd) / path).normalize) |
| 104 | + |
| 105 | +/-- Invoke `verity-cli compile path --emit lean`. Returns the emitted |
| 106 | + Lean source on stdout or an error message. -/ |
| 107 | +def runVerityCliCompile (opts : Options) : IO Result := do |
| 108 | + let sourcePath ← resolveSourcePath opts.path |
| 109 | + let args := match opts.contractName with |
| 110 | + | some c => |
| 111 | + #["compile", sourcePath.toString, "--contract", c, "--emit", "lean"] |
| 112 | + | none => |
| 113 | + #["compile", sourcePath.toString, "--emit", "lean"] |
| 114 | + try |
| 115 | + let out ← IO.Process.run { cmd := opts.cliBinary, args } |
| 116 | + return .ok out |
| 117 | + catch e => |
| 118 | + return .error s!"verity-cli failed: {e.toString}" |
| 119 | + |
| 120 | +/-- `#fromSolidity "path"` command. Currently emits a placeholder |
| 121 | + `axiom` whose name documents the pending translation; replace with a |
| 122 | + proper elaborator emission once the in-process translator lands. -/ |
| 123 | +syntax (name := fromSolidityCmd) "#fromSolidity " str (" contract " str)? : command |
| 124 | + |
| 125 | +@[command_elab fromSolidityCmd] |
| 126 | +def elabFromSolidity : CommandElab := fun stx => do |
| 127 | + let pathLit := stx[1] |
| 128 | + let path : System.FilePath := pathLit.isStrLit?.get! |
| 129 | + let contractOpt : Option String := |
| 130 | + if stx[2].getNumArgs > 0 then some stx[2][1].isStrLit?.get! else none |
| 131 | + let opts : Options := { path := path, contractName := contractOpt } |
| 132 | + let result : Result ← (runVerityCliCompile opts).toIO |
| 133 | + (fun e => IO.userError s!"fromSolidity IO failure: {e.toString}") |
| 134 | + match result with |
| 135 | + | .ok _emitted => |
| 136 | + let sourcePath ← (resolveSourcePath opts.path).toIO |
| 137 | + (fun e => IO.userError s!"fromSolidity path resolution failure: {e.toString}") |
| 138 | + logInfo m!"fromSolidity scaffold: would elaborate {sourcePath}; in-process translator pending" |
| 139 | + | .error msg => |
| 140 | + throwError m!"fromSolidity: {msg}. Falling back: declare the verity_contract block by hand for now." |
| 141 | + |
| 142 | +end Verity.Compiler.FromSolidity |
0 commit comments