diff --git a/ClAsm/Codegen/Emit.lean b/ClAsm/Codegen/Emit.lean index 7bbf8ac..1c4a78d 100644 --- a/ClAsm/Codegen/Emit.lean +++ b/ClAsm/Codegen/Emit.lean @@ -1,41 +1,19 @@ -import RiscvZkvm.Rv64.Program +import ClAsm.Rv64 +import LeanAsm.Rv64.Emit namespace ClAsm.Codegen open RiscvZkvm.Rv64 -/-- Only supported real instructions are emitted; pseudoinstructions cannot - silently change the size or meaning of the proved Program. -/ -def emitInstr : Instr → Except String String - | .LD dst src offset => .ok s!"ld x{dst.toNat}, {offset.toInt}(x{src.toNat})" - | .SD dst src offset => .ok s!"sd x{src.toNat}, {offset.toInt}(x{dst.toNat})" - | .SRLI dst src shift => .ok s!"srli x{dst.toNat}, x{src.toNat}, {shift.toNat}" - | .SLLI dst src shift => .ok s!"slli x{dst.toNat}, x{src.toNat}, {shift.toNat}" - | .SLTIU dst src imm => .ok s!"sltiu x{dst.toNat}, x{src.toNat}, {imm.toInt}" - | .ADDI dst src imm => .ok s!"addi x{dst.toNat}, x{src.toNat}, {imm.toInt}" - | .ANDI dst src imm => .ok s!"andi x{dst.toNat}, x{src.toNat}, {imm.toInt}" - | .XORI dst src imm => .ok s!"xori x{dst.toNat}, x{src.toNat}, {imm.toInt}" - | .ORI dst src imm => .ok s!"ori x{dst.toNat}, x{src.toNat}, {imm.toInt}" - | .LUI dst imm => .ok s!"lui x{dst.toNat}, {imm.toNat}" - | .SUB dst src1 src2 => .ok s!"sub x{dst.toNat}, x{src1.toNat}, x{src2.toNat}" - | .ADD dst src1 src2 => .ok s!"add x{dst.toNat}, x{src1.toNat}, x{src2.toNat}" - | .SLTU dst src1 src2 => .ok s!"sltu x{dst.toNat}, x{src1.toNat}, x{src2.toNat}" - | .XOR dst src1 src2 => .ok s!"xor x{dst.toNat}, x{src1.toNat}, x{src2.toNat}" - | .AND dst src1 src2 => .ok s!"and x{dst.toNat}, x{src1.toNat}, x{src2.toNat}" - | .BNE src1 src2 offset => .ok s!"bne x{src1.toNat}, x{src2.toNat}, . + {offset.toInt}" - | .JAL dst offset => .ok s!"jal x{dst.toNat}, . + {offset.toInt}" - | .JALR dst src offset => .ok s!"jalr x{dst.toNat}, {offset.toInt}(x{src.toNat})" - | _ => .error "unsupported RISC-V instruction" +/-- Only fixed-width instructions accepted by LeanAsm are emitted; + pseudoinstructions cannot silently change proved program size. -/ +def emitInstr (instruction : Instr) : Except String String := do + return LeanAsm.Rv64.emitInstr (← ClAsm.Rv64.Upstream.instr instruction) def symbolName (name : String) : String := name.replace "-" "_" -def assembly (name : String) (program : Program) : Except String String := do - let symbol := symbolName name - let lines ← program.mapM emitInstr - return ".option norvc\n.option norelax\n.section .text\n.balign 4\n" ++ - s!".globl {symbol}\n.type {symbol}, @function\n{symbol}:\n" ++ - String.join (lines.map (fun line => s!" {line}\n")) ++ - s!".size {symbol}, . - {symbol}\n" +def assembly (name : String) (program : Program) : Except String String := + LeanAsm.emitAsm (symbolName name) program end ClAsm.Codegen diff --git a/ClAsm/Rv64.lean b/ClAsm/Rv64.lean new file mode 100644 index 0000000..b3c84c6 --- /dev/null +++ b/ClAsm/Rv64.lean @@ -0,0 +1,103 @@ +import LeanAsm.Frontend +import RiscvZkvm.Rv64.Program + +/-! Temporary syntax adapter while cl-asm proofs are ported to LeanAsm. -/ + +namespace ClAsm.Rv64 + +namespace Upstream + +open RiscvZkvm.Rv64 + +private def reg : Reg → LeanAsm.Rv64.Reg + | .x0 => .x0 | .x1 => .x1 | .x2 => .x2 | .x3 => .x3 + | .x4 => .x4 | .x5 => .x5 | .x6 => .x6 | .x7 => .x7 + | .x8 => .x8 | .x9 => .x9 | .x10 => .x10 | .x11 => .x11 + | .x12 => .x12 | .x13 => .x13 | .x14 => .x14 | .x15 => .x15 + | .x16 => .x16 | .x17 => .x17 | .x18 => .x18 | .x19 => .x19 + | .x20 => .x20 | .x21 => .x21 | .x22 => .x22 | .x23 => .x23 + | .x24 => .x24 | .x25 => .x25 | .x26 => .x26 | .x27 => .x27 + | .x28 => .x28 | .x29 => .x29 | .x30 => .x30 | .x31 => .x31 + +def instr : Instr → Except String LeanAsm.Rv64.Instr + | .ADD d a b => .ok (.ADD (reg d) (reg a) (reg b)) + | .SUB d a b => .ok (.SUB (reg d) (reg a) (reg b)) + | .SLL d a b => .ok (.SLL (reg d) (reg a) (reg b)) + | .SRL d a b => .ok (.SRL (reg d) (reg a) (reg b)) + | .SRA d a b => .ok (.SRA (reg d) (reg a) (reg b)) + | .AND d a b => .ok (.AND (reg d) (reg a) (reg b)) + | .OR d a b => .ok (.OR (reg d) (reg a) (reg b)) + | .XOR d a b => .ok (.XOR (reg d) (reg a) (reg b)) + | .SLT d a b => .ok (.SLT (reg d) (reg a) (reg b)) + | .SLTU d a b => .ok (.SLTU (reg d) (reg a) (reg b)) + | .ADDI d a n => .ok (.ADDI (reg d) (reg a) n) + | .ANDI d a n => .ok (.ANDI (reg d) (reg a) n) + | .ORI d a n => .ok (.ORI (reg d) (reg a) n) + | .XORI d a n => .ok (.XORI (reg d) (reg a) n) + | .SLTI d a n => .ok (.SLTI (reg d) (reg a) n) + | .SLTIU d a n => .ok (.SLTIU (reg d) (reg a) n) + | .SLLI d a n => .ok (.SLLI (reg d) (reg a) n) + | .SRLI d a n => .ok (.SRLI (reg d) (reg a) n) + | .SRAI d a n => .ok (.SRAI (reg d) (reg a) n) + | .LUI d n => .ok (.LUI (reg d) n) + | .AUIPC d n => .ok (.AUIPC (reg d) n) + | .LD d a n => .ok (.LD (reg d) (reg a) n) + | .SD a b n => .ok (.SD (reg a) (reg b) n) + | .LW d a n => .ok (.LW (reg d) (reg a) n) + | .LWU d a n => .ok (.LWU (reg d) (reg a) n) + | .SW a b n => .ok (.SW (reg a) (reg b) n) + | .LB d a n => .ok (.LB (reg d) (reg a) n) + | .LH d a n => .ok (.LH (reg d) (reg a) n) + | .LBU d a n => .ok (.LBU (reg d) (reg a) n) + | .LHU d a n => .ok (.LHU (reg d) (reg a) n) + | .SB a b n => .ok (.SB (reg a) (reg b) n) + | .SH a b n => .ok (.SH (reg a) (reg b) n) + | .BEQ a b n => .ok (.BEQ (reg a) (reg b) n) + | .BNE a b n => .ok (.BNE (reg a) (reg b) n) + | .BLT a b n => .ok (.BLT (reg a) (reg b) n) + | .BGE a b n => .ok (.BGE (reg a) (reg b) n) + | .BLTU a b n => .ok (.BLTU (reg a) (reg b) n) + | .BGEU a b n => .ok (.BGEU (reg a) (reg b) n) + | .JAL d n => .ok (.JAL (reg d) n) + | .JALR d a n => .ok (.JALR (reg d) (reg a) n) + | .ADDIW d a n => .ok (.ADDIW (reg d) (reg a) n) + | .FENCE => .ok .FENCE + | .MUL d a b => .ok (.MUL (reg d) (reg a) (reg b)) + | .MULH d a b => .ok (.MULH (reg d) (reg a) (reg b)) + | .MULHSU d a b => .ok (.MULHSU (reg d) (reg a) (reg b)) + | .MULHU d a b => .ok (.MULHU (reg d) (reg a) (reg b)) + | .DIV d a b => .ok (.DIV (reg d) (reg a) (reg b)) + | .DIVU d a b => .ok (.DIVU (reg d) (reg a) (reg b)) + | .REM d a b => .ok (.REM (reg d) (reg a) (reg b)) + | .REMU d a b => .ok (.REMU (reg d) (reg a) (reg b)) + | _ => .error "instruction has no portable LeanAsm representation" + +/-- Opcodes accepted by the portable cl-asm code-generation boundary. -/ +def isaProfile : LeanAsm.Rv64.IsaProfile := + ⟨[.ADD, .SUB, .SLL, .SRL, .SRA, .AND, .OR, .XOR, .SLT, .SLTU, + .ADDI, .ANDI, .ORI, .XORI, .SLTI, .SLTIU, .SLLI, .SRLI, .SRAI, + .ADDIW, .LUI, .AUIPC, + .LB, .LH, .LW, .LD, .LBU, .LHU, .LWU, .SB, .SH, .SW, .SD, + .BEQ, .BNE, .BLT, .BGE, .BLTU, .BGEU, .JAL, .JALR, + .MUL, .MULH, .MULHSU, .MULHU, .DIV, .DIVU, .REM, .REMU, .FENCE]⟩ + +theorem instr_supported {source : Instr} {target : LeanAsm.Rv64.Instr} + (h : instr source = .ok target) : isaProfile.Supports target := by + cases source <;> simp [instr] at h + all_goals cases h + all_goals simp [isaProfile, LeanAsm.Rv64.IsaProfile.Supports, + LeanAsm.Rv64.Instr.opcode] + +def program (source : Program) : Except String LeanAsm.Rv64.Program := + source.mapM instr + +instance : LeanAsm.LowerRv64 Program where + lower := program + +instance : LeanAsm.EmitAsm Program where + emit name source := do + let target ← program source + return LeanAsm.Rv64.emitProgram name target + +end Upstream +end ClAsm.Rv64 diff --git a/README.md b/README.md index 298b6e7..e16f5ab 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,14 @@ The standalone `aggregate` routine computes the caller's three balances in steps. Its generated ELF is checked against the pinned official reference; its ZisK integration is not yet included. +Portable instruction emission is being moved to +[LeanAsm](https://github.com/NyxFoundation/LeanAsm). The `riscv-zkvm` +dependency remains in cl-asm for its existing proof environment and optional +zkVM integration; zkVM-specific semantics are not part of LeanAsm. Portable +programs implement LeanAsm's common `LowerRv64` and `EmitAsm` capabilities via +a partial adapter; unsupported zkVM and pseudo instructions remain explicit +lowering errors. + - [DESIGN.md](DESIGN.md): scope, ABI, memory layout, trust boundaries. - [Roadmap](docs/roadmap.md): why the consensus layer is being proved, zkVM findings, PoC decisions, and remaining work. diff --git a/lake-manifest.json b/lake-manifest.json index 96d23c3..a055ed5 100644 --- a/lake-manifest.json +++ b/lake-manifest.json @@ -11,6 +11,16 @@ "inputRev": "afcbc45e1c74c2d296e032b5606dd977a304e8ad", "inherited": false, "configFile": "lakefile.toml"}, + {"url": "https://github.com/NyxFoundation/LeanAsm", + "type": "git", + "subDir": null, + "scope": "", + "rev": "76bf12c339bf6dce140c817d334d2a4d0f0f48d7", + "name": "LeanAsm", + "manifestFile": "lake-manifest.json", + "inputRev": "76bf12c339bf6dce140c817d334d2a4d0f0f48d7", + "inherited": false, + "configFile": "lakefile.toml"}, {"url": "https://github.com/dhsorens/lean-sail", "type": "git", "subDir": null, diff --git a/lakefile.toml b/lakefile.toml index 69d59ae..bf734aa 100644 --- a/lakefile.toml +++ b/lakefile.toml @@ -2,6 +2,13 @@ name = "ClAsm" version = "0.1.0" defaultTargets = ["ClAsm", "cl-asm", "cl-asm-test", "cl-asm-weigh", "cl-asm-aggregate"] +[[require]] +name = "LeanAsm" +git = "https://github.com/NyxFoundation/LeanAsm" +rev = "76bf12c339bf6dce140c817d334d2a4d0f0f48d7" + +# cl-asm retains this dependency for its existing proof environment and +# optional zkVM integration. LeanAsm does not absorb zkVM-specific semantics. [[require]] name = "riscv-zkvm" git = "https://github.com/Verified-zkEVM/riscv-zkvm"