From 75371f5063f4156906f0afe63a48b60ac91bee6d Mon Sep 17 00:00:00 2001 From: adust09 Date: Fri, 13 Nov 2026 14:56:32 +0900 Subject: [PATCH 1/5] Route portable instruction emission through LeanAsm --- ClAsm/Codegen/Emit.lean | 29 ++++------------ ClAsm/Rv64.lean | 76 +++++++++++++++++++++++++++++++++++++++++ README.md | 4 +++ lake-manifest.json | 10 ++++++ lakefile.toml | 6 ++++ 5 files changed, 102 insertions(+), 23 deletions(-) create mode 100644 ClAsm/Rv64.lean diff --git a/ClAsm/Codegen/Emit.lean b/ClAsm/Codegen/Emit.lean index 7bbf8ac..43f07b7 100644 --- a/ClAsm/Codegen/Emit.lean +++ b/ClAsm/Codegen/Emit.lean @@ -1,31 +1,14 @@ -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 "-" "_" diff --git a/ClAsm/Rv64.lean b/ClAsm/Rv64.lean new file mode 100644 index 0000000..dc8cb8f --- /dev/null +++ b/ClAsm/Rv64.lean @@ -0,0 +1,76 @@ +import LeanAsm.Rv64.Basic +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" + +end Upstream +end ClAsm.Rv64 diff --git a/README.md b/README.md index 298b6e7..3bf8b88 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,10 @@ 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 existing proof logic +remains temporarily pinned to `riscv-zkvm` until its reusable parts are ported. + - [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..becfa70 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": "c04af0c5510d9074dae52a0874369f646c40c98a", + "name": "LeanAsm", + "manifestFile": "lake-manifest.json", + "inputRev": "c04af0c5510d9074dae52a0874369f646c40c98a", + "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..b76a4d8 100644 --- a/lakefile.toml +++ b/lakefile.toml @@ -2,6 +2,12 @@ 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 = "c04af0c5510d9074dae52a0874369f646c40c98a" + +# Retained temporarily while its generic proof logic is moved into LeanAsm. [[require]] name = "riscv-zkvm" git = "https://github.com/Verified-zkEVM/riscv-zkvm" From 69ecfee27c04b2f9bdb043ec7288cdd9b4089f2f Mon Sep 17 00:00:00 2001 From: adust09 Date: Fri, 13 Nov 2026 15:23:54 +0900 Subject: [PATCH 2/5] Clarify the zkVM integration boundary --- README.md | 5 +++-- lakefile.toml | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3bf8b88..2fe4cd8 100644 --- a/README.md +++ b/README.md @@ -12,8 +12,9 @@ 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 existing proof logic -remains temporarily pinned to `riscv-zkvm` until its reusable parts are ported. +[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. - [DESIGN.md](DESIGN.md): scope, ABI, memory layout, trust boundaries. - [Roadmap](docs/roadmap.md): why the consensus layer is being proved, zkVM diff --git a/lakefile.toml b/lakefile.toml index b76a4d8..76097dd 100644 --- a/lakefile.toml +++ b/lakefile.toml @@ -7,7 +7,8 @@ name = "LeanAsm" git = "https://github.com/NyxFoundation/LeanAsm" rev = "c04af0c5510d9074dae52a0874369f646c40c98a" -# Retained temporarily while its generic proof logic is moved into LeanAsm. +# 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" From 0d67d3b537c82846e2818d6b934e79ecee376f58 Mon Sep 17 00:00:00 2001 From: adust09 Date: Fri, 13 Nov 2026 15:38:39 +0900 Subject: [PATCH 3/5] Update LeanAsm pin --- lake-manifest.json | 4 ++-- lakefile.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lake-manifest.json b/lake-manifest.json index becfa70..570894b 100644 --- a/lake-manifest.json +++ b/lake-manifest.json @@ -15,10 +15,10 @@ "type": "git", "subDir": null, "scope": "", - "rev": "c04af0c5510d9074dae52a0874369f646c40c98a", + "rev": "b292dc77fb9affb1174052fca819135776aa24c6", "name": "LeanAsm", "manifestFile": "lake-manifest.json", - "inputRev": "c04af0c5510d9074dae52a0874369f646c40c98a", + "inputRev": "b292dc77fb9affb1174052fca819135776aa24c6", "inherited": false, "configFile": "lakefile.toml"}, {"url": "https://github.com/dhsorens/lean-sail", diff --git a/lakefile.toml b/lakefile.toml index 76097dd..25637e4 100644 --- a/lakefile.toml +++ b/lakefile.toml @@ -5,7 +5,7 @@ defaultTargets = ["ClAsm", "cl-asm", "cl-asm-test", "cl-asm-weigh", "cl-asm-aggr [[require]] name = "LeanAsm" git = "https://github.com/NyxFoundation/LeanAsm" -rev = "c04af0c5510d9074dae52a0874369f646c40c98a" +rev = "b292dc77fb9affb1174052fca819135776aa24c6" # cl-asm retains this dependency for its existing proof environment and # optional zkVM integration. LeanAsm does not absorb zkVM-specific semantics. From 640946bbf121200640907e466b0c417eb8ff11e4 Mon Sep 17 00:00:00 2001 From: adust09 Date: Fri, 13 Nov 2026 17:13:51 +0900 Subject: [PATCH 4/5] Expose portable code through LeanAsm frontend capabilities --- ClAsm/Codegen/Emit.lean | 9 ++------- ClAsm/Rv64.lean | 22 +++++++++++++++++++++- README.md | 5 ++++- lake-manifest.json | 4 ++-- lakefile.toml | 2 +- 5 files changed, 30 insertions(+), 12 deletions(-) diff --git a/ClAsm/Codegen/Emit.lean b/ClAsm/Codegen/Emit.lean index 43f07b7..1c4a78d 100644 --- a/ClAsm/Codegen/Emit.lean +++ b/ClAsm/Codegen/Emit.lean @@ -13,12 +13,7 @@ def emitInstr (instruction : Instr) : Except String String := do 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 index dc8cb8f..feb6879 100644 --- a/ClAsm/Rv64.lean +++ b/ClAsm/Rv64.lean @@ -1,4 +1,4 @@ -import LeanAsm.Rv64.Basic +import LeanAsm.Frontend import RiscvZkvm.Rv64.Program /-! Temporary syntax adapter while cl-asm proofs are ported to LeanAsm. -/ @@ -72,5 +72,25 @@ def instr : Instr → Except String LeanAsm.Rv64.Instr | .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]⟩ + +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 2fe4cd8..e16f5ab 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,10 @@ 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. +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 diff --git a/lake-manifest.json b/lake-manifest.json index 570894b..a055ed5 100644 --- a/lake-manifest.json +++ b/lake-manifest.json @@ -15,10 +15,10 @@ "type": "git", "subDir": null, "scope": "", - "rev": "b292dc77fb9affb1174052fca819135776aa24c6", + "rev": "76bf12c339bf6dce140c817d334d2a4d0f0f48d7", "name": "LeanAsm", "manifestFile": "lake-manifest.json", - "inputRev": "b292dc77fb9affb1174052fca819135776aa24c6", + "inputRev": "76bf12c339bf6dce140c817d334d2a4d0f0f48d7", "inherited": false, "configFile": "lakefile.toml"}, {"url": "https://github.com/dhsorens/lean-sail", diff --git a/lakefile.toml b/lakefile.toml index 25637e4..bf734aa 100644 --- a/lakefile.toml +++ b/lakefile.toml @@ -5,7 +5,7 @@ defaultTargets = ["ClAsm", "cl-asm", "cl-asm-test", "cl-asm-weigh", "cl-asm-aggr [[require]] name = "LeanAsm" git = "https://github.com/NyxFoundation/LeanAsm" -rev = "b292dc77fb9affb1174052fca819135776aa24c6" +rev = "76bf12c339bf6dce140c817d334d2a4d0f0f48d7" # cl-asm retains this dependency for its existing proof environment and # optional zkVM integration. LeanAsm does not absorb zkVM-specific semantics. From 8aac71a084943f2cc4235f24ba1b862b5d1413b7 Mon Sep 17 00:00:00 2001 From: adust09 Date: Fri, 13 Nov 2026 17:18:22 +0900 Subject: [PATCH 5/5] Prove successful cl instruction lowering respects its profile --- ClAsm/Rv64.lean | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ClAsm/Rv64.lean b/ClAsm/Rv64.lean index feb6879..b3c84c6 100644 --- a/ClAsm/Rv64.lean +++ b/ClAsm/Rv64.lean @@ -81,6 +81,13 @@ def isaProfile : LeanAsm.Rv64.IsaProfile := .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