Skip to content
Merged
Show file tree
Hide file tree
Changes from 129 commits
Commits
Show all changes
143 commits
Select commit Hold shift + click to select a range
33956eb
initial changes
dtumad Jul 9, 2025
d0b6fb3
working build
dtumad Jul 9, 2025
f990bd4
finish updating bitwise and jal
dtumad Jul 9, 2025
492d6ab
build errors
dtumad Jul 9, 2025
93a025c
revert sail change
dtumad Jul 9, 2025
a41aed0
outline mul operation
dtumad Jul 9, 2025
4c34407
import missing
dtumad Jul 9, 2025
7c36fac
divRem and lt
dtumad Jul 9, 2025
aaf0c1d
organize files
dtumad Jul 9, 2025
a6694ac
fixes
dtumad Jul 10, 2025
314c3a8
some add proof
dtumad Jul 10, 2025
7fe3fcb
add ITypeReader
GZGavinZhao Jul 10, 2025
9b32376
add AddiChip
GZGavinZhao Jul 10, 2025
4cd1286
add AddwOperation
GZGavinZhao Jul 10, 2025
2ed105f
add AddwChip
GZGavinZhao Jul 10, 2025
acb2974
add ShiftLeftChip
GZGavinZhao Jul 10, 2025
1309558
add ShiftRightChip
GZGavinZhao Jul 10, 2025
9217aa2
add SubOperation
GZGavinZhao Jul 10, 2025
fe15061
add SubChip
GZGavinZhao Jul 10, 2025
deaf4d8
add SubwOperation
GZGavinZhao Jul 10, 2025
3c08f2e
add SubwChip
GZGavinZhao Jul 10, 2025
ae3dc77
working add op
dtumad Jul 12, 2025
81045b7
fix add chip
dtumad Jul 12, 2025
d970bce
feat: import RV64IM (#48)
GZGavinZhao Jul 14, 2025
27a8835
basic JAL outline with sailM
dtumad Jul 15, 2025
fddb0c4
slightly less axiomatic
dtumad Jul 15, 2025
0ed2dd1
break out lemmas
dtumad Jul 15, 2025
4f0cb2d
Sync DivRemChip
GZGavinZhao Jul 14, 2025
bc86160
python script to sync constraints
GZGavinZhao Jul 15, 2025
a5f281f
sync Operations
GZGavinZhao Jul 15, 2025
b7dde88
sync Compare
GZGavinZhao Jul 16, 2025
553ae66
sync Readers
GZGavinZhao Jul 16, 2025
d7037c3
toStateProp with SailState
GZGavinZhao Jul 16, 2025
59b02f7
AirInteraction::Program for trusted properties
GZGavinZhao Jul 16, 2025
d589675
sync Reader AirInteraction::program
GZGavinZhao Jul 17, 2025
6884aa0
Sync JalrChip
GZGavinZhao Jul 17, 2025
9dcf66b
Jalr LHS
GZGavinZhao Jul 17, 2025
30bca7d
pc valid limbs assumption
GZGavinZhao Jul 18, 2025
6ad2d31
reusable simpM for monad simp
GZGavinZhao Jul 18, 2025
86452cc
b_is_u64 from reader_cstrs
GZGavinZhao Jul 18, 2025
d62f93d
strip some whitespace for vim jumping
GZGavinZhao Jul 18, 2025
567ca4c
reduction to hash map inserts
GZGavinZhao Jul 18, 2025
e88dc3e
not x0 case map ready except bit access
GZGavinZhao Jul 18, 2025
0218136
not x0 case except bitvec
GZGavinZhao Jul 18, 2025
a73f7f7
fix some is_U64 sorrys
GZGavinZhao Jul 18, 2025
893245c
small cleanup
GZGavinZhao Jul 18, 2025
f14df0d
is x0 case pending x0 read/write handling
GZGavinZhao Jul 18, 2025
5cfba92
return 0 when read_reg
GZGavinZhao Jul 18, 2025
532749f
move simpM
GZGavinZhao Jul 18, 2025
cb210e8
match working case 0 and 1
GZGavinZhao Jul 19, 2025
46bb488
SailState.get_reg?_is_rX proof
GZGavinZhao Jul 19, 2025
f1b2ff7
correct write when op_a is not x0
GZGavinZhao Jul 19, 2025
b5a6cd4
use the correct write behavior
GZGavinZhao Jul 19, 2025
42c13a4
well actually arg 2 is the match input
GZGavinZhao Jul 19, 2025
31aced3
establish wX_bits with regidx_write
GZGavinZhao Jul 19, 2025
021a621
not x0 case correct write behavior
GZGavinZhao Jul 20, 2025
3032582
is x0 case op_a = 0 proof
GZGavinZhao Jul 20, 2025
257d801
op_b + imm divides 4 proof
GZGavinZhao Jul 21, 2025
d1a3844
incorporate op_b_val_plus_imm and state InteractionKind::Program cons…
GZGavinZhao Jul 21, 2025
738b5c6
mul4 BB and BitVec helpers
GZGavinZhao Jul 21, 2025
e671268
some sailM lemmas
dtumad Jul 21, 2025
aa6c56e
Merge branch 'gzgz/trusted-instr-and-jalr' of https://github.com/succ…
dtumad Jul 21, 2025
2a16520
working build
dtumad Jul 21, 2025
33e53dd
op_a_one case
dtumad Jul 21, 2025
f3f2cee
most of a full proof
dtumad Jul 21, 2025
9bc2357
more general mul4 constraints
GZGavinZhao Jul 21, 2025
86068e9
jalr sorry-less proof except misa
GZGavinZhao Jul 22, 2025
d0642c7
more jal
dtumad Jul 22, 2025
e2a48d5
Merge branch 'gzgz/trusted-instr-and-jalr' of https://github.com/succ…
dtumad Jul 22, 2025
76b8cf8
missing modulus
dtumad Jul 22, 2025
755ad6c
sorry-less AddOperation proof
GZGavinZhao Jul 22, 2025
a112fea
remove wrong BitVec lemma
GZGavinZhao Jul 22, 2025
f517f71
SailState.sp1_no_misa axiom
GZGavinZhao Jul 22, 2025
324e425
Jalr proof with correct signExtend
GZGavinZhao Jul 22, 2025
f0cc58d
slightly better slightly worse
dtumad Jul 22, 2025
abb60b7
Merge branch 'gzgz/trusted-instr-and-jalr' of https://github.com/succ…
dtumad Jul 22, 2025
90864b4
slightly better slightly worse
dtumad Jul 22, 2025
3473f1a
minor cleanup, partial proofs
Jul 22, 2025
a69efa8
EStateM lemmas
dtumad Jul 22, 2025
0352067
build error
dtumad Jul 22, 2025
7afdef4
proof of insert_insert_insert_cancel
Jul 22, 2025
6278afe
axiom situation
dtumad Jul 22, 2025
44f44a3
working through more jalr cleanup
dtumad Jul 22, 2025
eb7cfd2
setup AddChip proof
GZGavinZhao Jul 22, 2025
8f55f01
clean jalr
dtumad Jul 23, 2025
2df3ff2
Merge branch 'gzgz/trusted-instr-and-jalr' of https://github.com/succ…
dtumad Jul 23, 2025
3901b1c
swap jalr
dtumad Jul 23, 2025
a2baf10
proof of AddChip
Jul 23, 2025
16d545b
merge with gzgz/trusted-instr-and-jalr
Jul 24, 2025
3f81388
Merge branch 'main' into petar/64-bit-ALU-verification
Jul 24, 2025
c25764e
ADDW
Jul 25, 2025
3ffc546
project-wide mcp setup for subtrees/workspaces
GZGavinZhao Jul 24, 2025
be50126
i_type Program assumptions
GZGavinZhao Jul 24, 2025
b538672
corrected i_type
GZGavinZhao Jul 25, 2025
18d38d7
trusted_instr for ADDW
GZGavinZhao Jul 24, 2025
57d6a02
more Program assumptions
GZGavinZhao Jul 24, 2025
beac0af
JAL calculate imm with Nat to prevent BB overflow
GZGavinZhao Jul 25, 2025
4e1c1b8
u_type prevent overflow as well with Nat
GZGavinZhao Jul 25, 2025
75b6ac6
no need op_c_(0|1) constraints in ADDIW case
GZGavinZhao Jul 25, 2025
3b16569
shift w
GZGavinZhao Jul 25, 2025
373539a
u_type should only constrain op_b
GZGavinZhao Jul 25, 2025
cef25c2
ECALL and EBREAK
GZGavinZhao Jul 25, 2025
e61b97b
corrected ADDW
GZGavinZhao Jul 25, 2025
bc82483
shift w no redundant op_b
GZGavinZhao Jul 25, 2025
7969d0b
catch stray LWU
GZGavinZhao Jul 25, 2025
73bec4c
M extension is just r_type
GZGavinZhao Jul 25, 2025
282678c
ADDW is just r_type or i_type
GZGavinZhao Jul 25, 2025
37c6f92
correct mixed up w_shift and shift
GZGavinZhao Jul 25, 2025
c45f000
correct JAL/j_type
GZGavinZhao Jul 25, 2025
eb806ef
some commenting
GZGavinZhao Jul 25, 2025
4d22de2
trusted opcodes and more
Jul 26, 2025
be48291
consolidating Word
Jul 26, 2025
11fe3a4
ADDIW
Jul 27, 2025
02e410f
streamlining proofs
Jul 28, 2025
33647fc
streamlining ...W proofs
Jul 28, 2025
1a8fc94
modular specifications
Jul 28, 2025
144007c
simpler Jalr proof
Jul 28, 2025
b6e30a9
simpler Jal proof
Jul 28, 2025
cee547e
LT opcodes
Jul 29, 2025
c1833e7
SLL
Jul 30, 2025
b5e7dd5
SLLs with sorried-out proofs
Jul 31, 2025
a4aadcb
SR partly working, SL needs fixing
Aug 1, 2025
43997ae
SL revisited
Aug 2, 2025
742a808
SL revisited - correction
Aug 2, 2025
a00adad
SL revisited - correction
Aug 2, 2025
1044513
all SRA opcodes
Aug 4, 2025
948315e
merge with main
Aug 4, 2025
a26cb2f
full proofs for shifts
Aug 4, 2025
9699ffc
Merge remote-tracking branch 'origin/main' into petar/64-bit-ALU-veri…
Aug 4, 2025
1067492
spec_sub
Aug 5, 2025
46f3cf9
others_sub
Aug 5, 2025
6a0babd
Bitwise chips and infrastructure
Aug 6, 2025
28d1941
Bitwise operations
Aug 7, 2025
fc9b172
all proofs except mul and divrem
Aug 7, 2025
43805f9
initial mul reasoning
Aug 10, 2025
52a0754
MulOperation
Aug 11, 2025
0f145c4
Restoring powers
Aug 11, 2025
e18183d
Mul
Aug 12, 2025
e128bc9
bhword and core_mulw
Aug 12, 2025
9121b20
better MUL proofs
Aug 13, 2025
f859588
minor corrections
Aug 13, 2025
69b6d4a
mul operation with U64, some grind and aesop
Aug 16, 2025
dba7998
some further automation and a bit of DivRem
Aug 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions SP1Chips/AddChip.lean
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ variable

def spec_add (rs2 rs1 rd : regidx) : SailM Unit := do
Sail.writeReg Register.nextPC ((← Sail.readReg Register.PC) + 4#64)
_ ← execute_RTYPE rs2 rs1 rd rop.ADD
_ ← execute (.RTYPE (rs2, rs1, rd, rop.ADD))
pure ()

def sp1_op_a : BitVec 5 :=
Expand Down Expand Up @@ -73,7 +73,7 @@ def sp1_add : SailM Unit := do

open Sail

theorem correct
theorem correct_add
(state_cstrs : (constraints Main).initialState s) :
let op_c := sp1_op_c Main cstrs h_is_real
let op_b := sp1_op_b Main cstrs h_is_real
Expand All @@ -95,7 +95,7 @@ theorem correct
simp at *

-- Now the monadic manipulation
simp [spec_add, sp1_add, execute_RTYPE]
simp [spec_add, sp1_add, execute, execute_RTYPE']
rw [run_readReg, read_pc]
simp [sp1_op_b, read_op_b (by omega)]
simp [sp1_op_c, read_op_c (by omega)]
Expand Down
96 changes: 96 additions & 0 deletions SP1Chips/AddiChip.lean
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,99 @@ import SP1Operations.Operation.AddOperation
import SP1Operations.Reader.CPUState
import SP1Operations.Reader.ITypeReader
import SP1Chips.Addi.Constraints

import SP1Chips.Add.Constraints

open LeanRV64IM.Functions BitVec

namespace Addi

variable
(Main : Vector (Fin BB) 30)
(s : SailState)
(cstrs : (constraints Main).allHold)
(h_is_real : Main[29] = 1)

def spec_addi (imm : BitVec 12) (rs1 rd : regidx) : SailM Unit := do
Sail.writeReg Register.nextPC ((← Sail.readReg Register.PC) + 4#64)
_ ← execute (.ITYPE (imm, rs1, rd, iop.ADDI))
pure ()

def sp1_op_a : BitVec 5 :=
by
refine BitVec.ofNatLT Main[6] ?_
simp
show Main[6] < 32

have reader_cstrs := by
simp [SP1ConstraintList.allHold, constraints, SP1Constraint.toProp] at cstrs
exact cstrs.2.2.1

clear cstrs
simp [ITypeReader.constraints, h_is_real, Opcode.ofNat, Nat.ble, Nat.beq, SP1Constraint.toProp] at reader_cstrs

exact reader_cstrs.1.2.1

def sp1_op_b : BitVec 5 :=
by
refine BitVec.ofNatLT Main[14] ?_
simp
show Main[14] < 32

have reader_cstrs := by
simp [SP1ConstraintList.allHold, constraints, SP1Constraint.toProp] at cstrs
exact cstrs.2.2.1

clear cstrs
simp [ITypeReader.constraints, h_is_real, Opcode.ofNat, Nat.ble, Nat.beq, SP1Constraint.toProp] at reader_cstrs

exact reader_cstrs.1.1.1

def sp1_op_c : BitVec 12 := BitVec.ofNat 12 Main[21].val

def sp1_addi : SailM Unit := do
let op_a := sp1_op_a Main cstrs h_is_real
Sail.writeReg Register.nextPC (Word.toBitVec64 #v[Main[3], Main[4], Main[5], 0] + 4)
Sail.write_reg op_a (Word.toBitVec64 #v[Main[25], Main[26], Main[27], Main[28]])

open Sail

theorem correct_addi
(state_cstrs : (constraints Main).initialState s) :
let op_c := sp1_op_c Main
let op_b := sp1_op_b Main cstrs h_is_real
let op_a := sp1_op_a Main cstrs h_is_real
(spec_addi op_c (.Regidx op_b) (.Regidx op_a)).run s = (sp1_addi Main cstrs h_is_real).run s
:= by
-- Obtain and simplify state and pure constraints
simp [SP1ConstraintList.initialState, constraints, SP1Constraint.toStateProp, List.Forall, AddOperation.constraints, CPUState.constraints, ITypeReader.constraints, h_is_real] at state_cstrs
obtain ⟨read_pc, trusted_instr_state, read_op_b, read_op_c⟩ := state_cstrs
simp [constraints] at cstrs
obtain ⟨add_op_cstrs, cpu_cstrs, reader_cstrs, rest⟩ := cstrs
apply AddOperation.correct (h_is_real := h_is_real) at add_op_cstrs
rw [CPUState.allHold_constraints_iff_is_real h_is_real] at cpu_cstrs
rw [ITypeReader.allHold_constraints_iff_is_real h_is_real] at reader_cstrs
obtain ⟨ trusted_instr_prop, _, _, c0, c1, c2, c3, _, _, _, _, _, _, _, _, _, _, ⟨ is_U64_a, is_U64_b, _ ⟩⟩ := reader_cstrs
simp_all [Opcode.ofNat, Nat.ble]
have is_U64_c : Word.isU64 #v[Main[21], Main[22], Main[23], Main[24]]
:= by apply Word.isU64_of_cases _ c0 c1 c2 c3
specialize add_op_cstrs is_U64_b is_U64_c
obtain ⟨ is_U64_val, is_add ⟩ := add_op_cstrs
simp_all

-- Now the monadic manipulation
simp [spec_addi, sp1_addi, execute, execute_ITYPE]
rw [run_readReg, read_pc]
simp [sp1_op_b, read_op_b]
simp [sp1_op_c, read_op_c]
simp [sp1_op_a]

by_cases h_is_op_a_0 : Main[6] = 0 <;> simp_all
. rw [← is_add]
simp [Word.toBitVec64, Word.toNat]
. rw [if_neg (by simpa [← BitVec.toNat_inj])]
rw [if_neg (by simpa [← BitVec.toNat_inj])]
simp [Word.toBitVec64, Word.toNat]
rfl

end Addi
221 changes: 221 additions & 0 deletions SP1Chips/AddwChip.lean
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,224 @@ import SP1Operations.Operation.AddwOperation
import SP1Operations.Reader.CPUState
import SP1Operations.Reader.ALUTypeReader
import SP1Chips.Addw.Constraints

open LeanRV64IM.Functions
open BitVec

namespace Addw

variable
(Main : Vector (Fin BB) 37)
(s : SailState)
(cstrs : (constraints Main).allHold)
(h_is_real : Main[35] = 1)
(h_is_addw : Main[31] = 0)

def spec_addw (rs2 rs1 rd : regidx) : SailM Unit := do
Sail.writeReg Register.nextPC ((← Sail.readReg Register.PC) + 4#64)
_ ← execute_RTYPEW rs2 rs1 rd ropw.ADDW
pure ()

def sp1_op_a : BitVec 5 :=
by
refine BitVec.ofNatLT Main[6] ?_
simp
show Main[6] < 32

have alu_cstrs := by
simp [SP1ConstraintList.allHold, constraints, SP1Constraint.toProp] at cstrs
exact cstrs.2.2.1

clear cstrs
simp [ALUTypeReader.constraints, h_is_real, Opcode.ofNat, Nat.ble, Nat.beq, SP1Constraint.toProp] at alu_cstrs

exact alu_cstrs.1.2.1

def sp1_op_b : BitVec 5 :=
by
refine BitVec.ofNatLT Main[14] ?_
simp
show Main[14] < 32

have alu_cstrs := by
simp [SP1ConstraintList.allHold, constraints, SP1Constraint.toProp] at cstrs
exact cstrs.2.2.1

clear cstrs
have : (Main[31] = 0 ∨ Main[31] = 1) := by
simp [ALUTypeReader.allHold_constraints_iff_is_real (h := h_is_real), Opcode.ofNat, Nat.ble] at alu_cstrs
aesop

simp [ALUTypeReader.allHold_constraints_iff_is_real (h := h_is_real), Opcode.ofNat, Nat.ble] at alu_cstrs
rcases this <;> simp_all

def sp1_op_c : BitVec 5 :=
by
refine BitVec.ofNatLT Main[21] ?_
simp
show Main[21] < 32

have alu_cstrs := by
simp [SP1ConstraintList.allHold, constraints, SP1Constraint.toProp] at cstrs
exact cstrs.2.2.1

clear cstrs
simp [ALUTypeReader.allHold_constraints_iff_is_real (h := h_is_real), Opcode.ofNat, Nat.ble, h_is_addw] at alu_cstrs

exact alu_cstrs.1.2.1

def sp1_addw : SailM Unit := do
let op_a := sp1_op_a Main cstrs h_is_real
-- TODO(gzgz): we can obtain this from the constraint compiler
-- This comes from the Interaction.state in CPUState
Sail.writeReg Register.nextPC (Word.toBitVec64 #v[Main[3], Main[4], Main[5], 0] + 4)
Sail.write_reg op_a (Word.toBitVec64 #v[Main[32], Main[33], Main[34] * 65535, Main[34] * 65535])

theorem correct_addw
(state_cstrs : (constraints Main).initialState s)
(h_is_addw : Main[31] = 0) :
let op_c := sp1_op_c Main cstrs h_is_real h_is_addw
let op_b := sp1_op_b Main cstrs h_is_real h_is_addw
let op_a := sp1_op_a Main cstrs h_is_real
(spec_addw (.Regidx op_c) (.Regidx op_b) (.Regidx op_a)).run s = (sp1_addw Main cstrs h_is_real).run s
:= by
-- Obtain and simplify state and pure constraints
simp [SP1ConstraintList.initialState, constraints, SP1Constraint.toStateProp, List.Forall, AddwOperation.constraints, CPUState.constraints, ALUTypeReader.constraints, U16MSBOperation.constraints, h_is_real] at state_cstrs
obtain ⟨read_pc, trusted_instr_state, read_op_a, read_op_b, read_op_c⟩ := state_cstrs
simp [constraints] at cstrs
obtain ⟨addw_op_cstrs, cpu_cstrs, alu_cstrs, _, _⟩ := cstrs
apply AddwOperation.correct (h_is_real := h_is_real) at addw_op_cstrs
rw [CPUState.allHold_constraints_iff_is_real h_is_real] at cpu_cstrs
rw [ALUTypeReader.allHold_constraints_iff_is_real h_is_real] at alu_cstrs
obtain ⟨ trusted_instr_prop, _, _, _, _, _, _, _, _, _, _, _, _, _, _, is_U64_a, is_U64_b, is_U64_c , _, _ ⟩ := alu_cstrs
simp [Opcode.ofNat, Nat.ble] at *
simp_all
obtain ⟨ _, _, is_U64_c ⟩ := is_U64_c
specialize addw_op_cstrs is_U64_b is_U64_c
obtain ⟨ is_U32_val, is_addw, is_msb ⟩ := addw_op_cstrs
simp_all

-- Now the monadic manipulation
simp [spec_addw, sp1_addw, execute, execute_RTYPEW']
rw [Sail.run_readReg, read_pc]
simp [sp1_op_a, sp1_op_b, sp1_op_c, read_op_b, read_op_c]
rw [exec_RTYPEW_pure_bv_to_w _ _ _ (by omega) (by omega)]
simp [execute_RTYPEW_pure_w]
rw [← is_addw] at is_msb

by_cases h_is_op_a_0 : Main[6] = 0 <;> simp_all
. simp [Word.toBitVec64, Word.toNat]
. rw [if_neg (by simpa [← BitVec.toNat_inj])]
rw [if_neg (by simpa [← BitVec.toNat_inj])]
simp [Word.toBitVec64, Word.toNat]
rw [← is_addw]; congr
rw [HalfWord.sign_extend_32_to_64_msb _ _ is_U32_val]
simp [Word.toBitVec64, Word.toNat]

end Addw

namespace Addiw

open Addw

variable
(Main : Vector (Fin BB) 37)
(s : SailState)
(cstrs : (constraints Main).allHold)
(h_is_real : Main[35] = 1)
(h_is_addiw : Main[31] = 1)

def spec_addiw (imm : BitVec 12) (rs1 rd : regidx) : SailM Unit := do
Sail.writeReg Register.nextPC ((← Sail.readReg Register.PC) + 4#64)
_ ← execute_ADDIW imm rs1 rd
pure ()

def sp1_op_a : BitVec 5 :=
by
refine BitVec.ofNatLT Main[6] ?_
simp
show Main[6] < 32

have alu_cstrs := by
simp [SP1ConstraintList.allHold, constraints, SP1Constraint.toProp] at cstrs
exact cstrs.2.2.1

clear cstrs
simp [ALUTypeReader.constraints, h_is_real, Opcode.ofNat, Nat.ble, Nat.beq, SP1Constraint.toProp] at alu_cstrs

exact alu_cstrs.1.2.1

def sp1_op_b : BitVec 5 :=
by
refine BitVec.ofNatLT Main[14] ?_
simp
show Main[14] < 32

have alu_cstrs := by
simp [SP1ConstraintList.allHold, constraints, SP1Constraint.toProp] at cstrs
exact cstrs.2.2.1

clear cstrs
have : (Main[31] = 0 ∨ Main[31] = 1) := by
simp [ALUTypeReader.allHold_constraints_iff_is_real (h := h_is_real), Opcode.ofNat, Nat.ble] at alu_cstrs
aesop

simp [ALUTypeReader.allHold_constraints_iff_is_real (h := h_is_real), Opcode.ofNat, Nat.ble] at alu_cstrs
rcases this <;> simp_all

def sp1_op_c : BitVec 12 := BitVec.ofNat 12 Main[21].val

def sp1_addiw : SailM Unit := do
let op_a := sp1_op_a Main cstrs h_is_real
-- TODO(gzgz): we can obtain this from the constraint compiler
-- This comes from the Interaction.state in CPUState
Sail.writeReg Register.nextPC (Word.toBitVec64 #v[Main[3], Main[4], Main[5], 0] + 4)
Sail.write_reg op_a (Word.toBitVec64 #v[Main[32], Main[33], Main[34] * 65535, Main[34] * 65535])

set_option maxHeartbeats 1000000 in
theorem correct_addw
(state_cstrs : (constraints Main).initialState s) :
let op_c := sp1_op_c Main
let op_b := sp1_op_b Main cstrs h_is_real h_is_addiw
let op_a := sp1_op_a Main cstrs h_is_real
(spec_addiw op_c (.Regidx op_b) (.Regidx op_a)).run s = (sp1_addiw Main cstrs h_is_real).run s
:= by
-- Obtain and simplify state and pure constraints
simp [SP1ConstraintList.initialState, constraints, SP1Constraint.toStateProp, List.Forall, AddwOperation.constraints, CPUState.constraints, ALUTypeReader.constraints, U16MSBOperation.constraints, h_is_real] at state_cstrs
obtain ⟨read_pc, trusted_instr_state, read_op_a, read_op_b, read_op_c⟩ := state_cstrs
simp [constraints] at cstrs
obtain ⟨addw_op_cstrs, cpu_cstrs, alu_cstrs, _, _⟩ := cstrs
apply AddwOperation.correct (h_is_real := h_is_real) at addw_op_cstrs
rw [CPUState.allHold_constraints_iff_is_real h_is_real] at cpu_cstrs
rw [ALUTypeReader.allHold_constraints_iff_is_real h_is_real] at alu_cstrs
obtain ⟨ trusted_instr_prop, _, _, ⟨ c0, c1, c2, c3 ⟩, _, _, _, _, _, _, _, _, _, _, _, is_U64_a, is_U64_b, _, _, _ ⟩ := alu_cstrs
simp [Opcode.ofNat, Nat.ble] at *
simp_all
have is_U64_c : Word.isU64 #v[Main[21], Main[22], Main[23], Main[24]]
:= by apply Word.isU64_of_cases _ c0 c1 c2 c3
specialize addw_op_cstrs is_U64_b is_U64_c
obtain ⟨ is_U32_val, is_addw, is_msb ⟩ := addw_op_cstrs
simp_all
obtain ⟨ h_f, h_imm_c ⟩ := trusted_instr_prop
simp [h_is_addiw] at h_f h_imm_c
obtain ⟨ h_c, h_is_imm_c ⟩ := h_imm_c

-- Now the monadic manipulation
simp [spec_addiw, sp1_addiw, execute, execute_ADDIW']
rw [Sail.run_readReg, read_pc]
simp [sp1_op_a, sp1_op_b, sp1_op_c, read_op_b]
rw [← h_is_imm_c]
rw [exec_RTYPEW_pure_bv_to_w _ _ _ (by omega) (by omega)]
simp [execute_RTYPEW_pure_w]
rw [← is_addw] at is_msb

by_cases h_is_op_a_0 : Main[6] = 0 <;> simp_all
. simp [Word.toBitVec64, Word.toNat]
. rw [if_neg (by simpa [← BitVec.toNat_inj])]
rw [if_neg (by simpa [← BitVec.toNat_inj])]
simp [Word.toBitVec64, Word.toNat]
rw [← is_addw]; congr
rw [HalfWord.sign_extend_32_to_64_msb _ _ is_U32_val]
simp [Word.toBitVec64, Word.toNat]

end Addiw
Loading
Loading