Skip to content

Commit 90cf9e5

Browse files
authored
Lemmas about BabyBear and Lean version bump (#39)
* chore: common baby bear field equations * lean version breaking changes and baby bear lemmas
1 parent 33896e2 commit 90cf9e5

File tree

15 files changed

+218
-71
lines changed

15 files changed

+218
-71
lines changed

LeanRV32D/PreludeMem.lean

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ def physaddrbits_zero_extend (xs : (BitVec 34)) : (BitVec 64) :=
256256
(zero_extend (m := 64) xs)
257257

258258
/-- Type quantifiers: width : Nat, 0 < width ∧ width ≤ max_mem_access -/
259-
def write_ram (wk : write_kind) (app_1 : physaddr) (width : Nat) (data : (BitVec (8 * width))) (meta : Unit) : SailM Bool := do
259+
def write_ram (wk : write_kind) (app_1 : physaddr) (width : Nat) (data : (BitVec (8 * width))) (meta_ : Unit) : SailM Bool := do
260260
let .Physaddr addr := app_1
261261
let request : (Mem_write_request width 64 physaddrbits Unit RISCV_strong_access) :=
262262
{ access_kind := match wk with
@@ -286,7 +286,7 @@ def write_ram (wk : write_kind) (app_1 : physaddr) (width : Nat) (data : (BitVec
286286
tag := none }
287287
match (← (sail_mem_write request)) with
288288
| .Ok _ =>
289-
(let _ : Unit := (__WriteRAM_Meta addr width meta)
289+
(let _ : Unit := (__WriteRAM_Meta addr width meta_)
290290
(pure true))
291291
| .Err () => (pure false)
292292

@@ -298,7 +298,7 @@ def write_ram_ea (wk : write_kind) (app_1 : physaddr) (width : Nat) : Unit :=
298298
/-- Type quantifiers: k_ex370025# : Bool, width : Nat, 0 < width ∧ width ≤ max_mem_access -/
299299
def read_ram (rk : read_kind) (app_1 : physaddr) (width : Nat) (read_meta : Bool) : SailM ((BitVec (8 * width)) × Unit) := do
300300
let .Physaddr addr := app_1
301-
let meta :=
301+
let meta_ :=
302302
bif read_meta
303303
then (__ReadRAM_Meta addr width)
304304
else default_meta
@@ -329,6 +329,5 @@ def read_ram (rk : read_kind) (app_1 : physaddr) (width : Nat) (read_meta : Bool
329329
size := width
330330
tag := false }
331331
match (← (sail_mem_read request)) with
332-
| .Ok (value, _) => (pure (value, meta))
332+
| .Ok (value, _) => (pure (value, meta_))
333333
| .Err () => throw Error.Exit
334-

LeanRV32D/PreludeMemMetadata.lean

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,9 @@ open AccessType
171171
def default_meta : mem_meta := ()
172172

173173
/-- Type quantifiers: width : Nat, 1 ≤ width ∧ width ≤ 4096 -/
174-
def __WriteRAM_Meta (addr : (BitVec 34)) (width : Nat) (meta : Unit) : Unit :=
174+
def __WriteRAM_Meta (addr : (BitVec 34)) (width : Nat) (meta_ : Unit) : Unit :=
175175
()
176176

177177
/-- Type quantifiers: width : Nat, 1 ≤ width ∧ width ≤ 4096 -/
178178
def __ReadRAM_Meta (addr : (BitVec 34)) (width : Nat) : Unit :=
179179
default_meta
180-

LeanRV32D/RiscvMem.lean

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,10 @@ def write_kind_of_flags (aq : Bool) (rl : Bool) (con : Bool) : SailM write_kind
211211

212212
/-- Type quantifiers: k_ex375202# : Bool, k_ex375201# : Bool, k_ex375200# : Bool, k_ex375199# : Bool, width
213213
: Nat, 0 < width ∧ width ≤ max_mem_access -/
214-
def phys_mem_read (t : (AccessType Unit)) (paddr : physaddr) (width : Nat) (aq : Bool) (rl : Bool) (res : Bool) (meta : Bool) : SailM (Result ((BitVec (8 * width)) × Unit) ExceptionType) := do
214+
def phys_mem_read (t : (AccessType Unit)) (paddr : physaddr) (width : Nat) (aq : Bool) (rl : Bool) (res : Bool) (meta_ : Bool) : SailM (Result ((BitVec (8 * width)) × Unit) ExceptionType) := do
215215
let result ← do
216216
match (read_kind_of_flags aq rl res) with
217-
| .some rk => (pure (some (← (read_ram rk paddr width meta))))
217+
| .some rk => (pure (some (← (read_ram rk paddr width meta_))))
218218
| none => (pure none)
219219
match (t, result) with
220220
| (.InstructionFetch (), none) => (pure (Err (E_Fetch_Access_Fault ())))
@@ -230,7 +230,7 @@ def phys_access_check (t : (AccessType Unit)) (p : Privilege) (paddr : physaddr)
230230

231231
/-- Type quantifiers: k_ex375222# : Bool, k_ex375221# : Bool, k_ex375220# : Bool, k_ex375219# : Bool, width
232232
: Nat, 0 < width ∧ width ≤ max_mem_access -/
233-
def checked_mem_read (t : (AccessType Unit)) (priv : Privilege) (paddr : physaddr) (width : Nat) (aq : Bool) (rl : Bool) (res : Bool) (meta : Bool) : SailM (Result ((BitVec (8 * width)) × Unit) ExceptionType) := do
233+
def checked_mem_read (t : (AccessType Unit)) (priv : Privilege) (paddr : physaddr) (width : Nat) (aq : Bool) (rl : Bool) (res : Bool) (meta_ : Bool) : SailM (Result ((BitVec (8 * width)) × Unit) ExceptionType) := do
234234
match (← (phys_access_check t priv paddr width)) with
235235
| .some e => (pure (Err e))
236236
| none =>
@@ -242,8 +242,8 @@ def checked_mem_read (t : (AccessType Unit)) (priv : Privilege) (paddr : physadd
242242
bif (← (within_phys_mem paddr width))
243243
then
244244
(do
245-
match (ext_check_phys_mem_read t paddr width aq rl res meta) with
246-
| .Ext_PhysAddr_OK () => (phys_mem_read t paddr width aq rl res meta)
245+
match (ext_check_phys_mem_read t paddr width aq rl res meta_) with
246+
| .Ext_PhysAddr_OK () => (phys_mem_read t paddr width aq rl res meta_)
247247
| .Ext_PhysAddr_Error e => (pure (Err e)))
248248
else
249249
(match t with
@@ -253,7 +253,7 @@ def checked_mem_read (t : (AccessType Unit)) (priv : Privilege) (paddr : physadd
253253

254254
/-- Type quantifiers: k_ex375232# : Bool, k_ex375231# : Bool, k_ex375230# : Bool, k_ex375229# : Bool, width
255255
: Nat, 0 < width ∧ width ≤ max_mem_access -/
256-
def mem_read_priv_meta (typ : (AccessType Unit)) (priv : Privilege) (paddr : physaddr) (width : Nat) (aq : Bool) (rl : Bool) (res : Bool) (meta : Bool) : SailM (Result ((BitVec (8 * width)) × Unit) ExceptionType) := do
256+
def mem_read_priv_meta (typ : (AccessType Unit)) (priv : Privilege) (paddr : physaddr) (width : Nat) (aq : Bool) (rl : Bool) (res : Bool) (meta_ : Bool) : SailM (Result ((BitVec (8 * width)) × Unit) ExceptionType) := do
257257
let result ← (( do
258258
bif ((aq || res) && (not (is_aligned_paddr paddr width)))
259259
then (pure (Err (E_Load_Addr_Align ())))
@@ -262,7 +262,7 @@ def mem_read_priv_meta (typ : (AccessType Unit)) (priv : Privilege) (paddr : phy
262262
match (aq, rl, res) with
263263
| (false, true, false) => sailThrow ((Error_not_implemented "load.rl"))
264264
| (false, true, true) => sailThrow ((Error_not_implemented "lr.rl"))
265-
| (_, _, _) => (checked_mem_read typ priv paddr width aq rl res meta)) ) : SailM
265+
| (_, _, _) => (checked_mem_read typ priv paddr width aq rl res meta_)) ) : SailM
266266
(MemoryOpResult ((BitVec (8 * width)) × mem_meta)) )
267267
let _ : Unit :=
268268
match result with
@@ -273,10 +273,10 @@ def mem_read_priv_meta (typ : (AccessType Unit)) (priv : Privilege) (paddr : phy
273273

274274
/-- Type quantifiers: k_ex375287# : Bool, k_ex375286# : Bool, k_ex375285# : Bool, k_ex375284# : Bool, width
275275
: Nat, 0 < width ∧ width ≤ max_mem_access -/
276-
def mem_read_meta (typ : (AccessType Unit)) (paddr : physaddr) (width : Nat) (aq : Bool) (rl : Bool) (res : Bool) (meta : Bool) : SailM (Result ((BitVec (8 * width)) × Unit) ExceptionType) := do
276+
def mem_read_meta (typ : (AccessType Unit)) (paddr : physaddr) (width : Nat) (aq : Bool) (rl : Bool) (res : Bool) (meta_ : Bool) : SailM (Result ((BitVec (8 * width)) × Unit) ExceptionType) := do
277277
(mem_read_priv_meta typ
278278
(← (effectivePrivilege typ (← readReg mstatus) (← readReg cur_privilege))) paddr width aq
279-
rl res meta)
279+
rl res meta_)
280280

281281
/-- Type quantifiers: k_ex375290# : Bool, k_ex375289# : Bool, k_ex375288# : Bool, width : Nat, 0 <
282282
width ∧ width ≤ max_mem_access -/
@@ -298,12 +298,12 @@ def mem_write_ea (addr : physaddr) (width : Nat) (aq : Bool) (rl : Bool) (con :
298298
else (pure (Ok (write_ram_ea (← (write_kind_of_flags aq rl con)) addr width)))
299299

300300
/-- Type quantifiers: width : Nat, 0 < width ∧ width ≤ max_mem_access -/
301-
def phys_mem_write (wk : write_kind) (paddr : physaddr) (width : Nat) (data : (BitVec (8 * width))) (meta : Unit) : SailM (Result Bool ExceptionType) := do
302-
(pure (Ok (← (write_ram wk paddr width data meta))))
301+
def phys_mem_write (wk : write_kind) (paddr : physaddr) (width : Nat) (data : (BitVec (8 * width))) (meta_ : Unit) : SailM (Result Bool ExceptionType) := do
302+
(pure (Ok (← (write_ram wk paddr width data meta_))))
303303

304304
/-- Type quantifiers: k_ex375311# : Bool, k_ex375310# : Bool, k_ex375309# : Bool, width : Nat, 0 <
305305
width ∧ width ≤ max_mem_access -/
306-
def checked_mem_write (paddr : physaddr) (width : Nat) (data : (BitVec (8 * width))) (typ : (AccessType Unit)) (priv : Privilege) (meta : Unit) (aq : Bool) (rl : Bool) (con : Bool) : SailM (Result Bool ExceptionType) := do
306+
def checked_mem_write (paddr : physaddr) (width : Nat) (data : (BitVec (8 * width))) (typ : (AccessType Unit)) (priv : Privilege) (meta_ : Unit) (aq : Bool) (rl : Bool) (con : Bool) : SailM (Result Bool ExceptionType) := do
307307
match (← (phys_access_check typ priv paddr width)) with
308308
| .some e => (pure (Err e))
309309
| none =>
@@ -316,19 +316,19 @@ def checked_mem_write (paddr : physaddr) (width : Nat) (data : (BitVec (8 * widt
316316
then
317317
(do
318318
let wk ← do (write_kind_of_flags aq rl con)
319-
match (ext_check_phys_mem_write wk paddr width data meta) with
320-
| .Ext_PhysAddr_OK () => (phys_mem_write wk paddr width data meta)
319+
match (ext_check_phys_mem_write wk paddr width data meta_) with
320+
| .Ext_PhysAddr_OK () => (phys_mem_write wk paddr width data meta_)
321321
| .Ext_PhysAddr_Error e => (pure (Err e)))
322322
else (pure (Err (E_SAMO_Access_Fault ())))))
323323

324324
/-- Type quantifiers: k_ex375325# : Bool, k_ex375324# : Bool, k_ex375323# : Bool, width : Nat, 0 <
325325
width ∧ width ≤ max_mem_access -/
326-
def mem_write_value_priv_meta (paddr : physaddr) (width : Nat) (value : (BitVec (8 * width))) (typ : (AccessType Unit)) (priv : Privilege) (meta : Unit) (aq : Bool) (rl : Bool) (con : Bool) : SailM (Result Bool ExceptionType) := do
326+
def mem_write_value_priv_meta (paddr : physaddr) (width : Nat) (value : (BitVec (8 * width))) (typ : (AccessType Unit)) (priv : Privilege) (meta_ : Unit) (aq : Bool) (rl : Bool) (con : Bool) : SailM (Result Bool ExceptionType) := do
327327
bif ((rl || con) && (not (is_aligned_paddr paddr width)))
328328
then (pure (Err (E_SAMO_Addr_Align ())))
329329
else
330330
(do
331-
let result ← do (checked_mem_write paddr width value typ priv meta aq rl con)
331+
let result ← do (checked_mem_write paddr width value typ priv meta_ aq rl con)
332332
let _ : Unit :=
333333
match result with
334334
| .Ok _ => (mem_write_callback (accessType_to_str typ) (bits_of_physaddr paddr) width value)
@@ -342,13 +342,12 @@ def mem_write_value_priv (paddr : physaddr) (width : Nat) (value : (BitVec (8 *
342342

343343
/-- Type quantifiers: k_ex375341# : Bool, k_ex375340# : Bool, k_ex375339# : Bool, width : Nat, 0 <
344344
width ∧ width ≤ max_mem_access -/
345-
def mem_write_value_meta (paddr : physaddr) (width : Nat) (value : (BitVec (8 * width))) (ext_acc : Unit) (meta : Unit) (aq : Bool) (rl : Bool) (con : Bool) : SailM (Result Bool ExceptionType) := do
345+
def mem_write_value_meta (paddr : physaddr) (width : Nat) (value : (BitVec (8 * width))) (ext_acc : Unit) (meta_ : Unit) (aq : Bool) (rl : Bool) (con : Bool) : SailM (Result Bool ExceptionType) := do
346346
let typ := (Write ext_acc)
347347
let ep ← do (effectivePrivilege typ (← readReg mstatus) (← readReg cur_privilege))
348-
(mem_write_value_priv_meta paddr width value typ ep meta aq rl con)
348+
(mem_write_value_priv_meta paddr width value typ ep meta_ aq rl con)
349349

350350
/-- Type quantifiers: k_ex375344# : Bool, k_ex375343# : Bool, k_ex375342# : Bool, width : Nat, 0 <
351351
width ∧ width ≤ max_mem_access -/
352352
def mem_write_value (paddr : physaddr) (width : Nat) (value : (BitVec (8 * width))) (aq : Bool) (rl : Bool) (con : Bool) : SailM (Result Bool ExceptionType) := do
353353
(mem_write_value_meta paddr width value default_write_acc default_meta aq rl con)
354-

SP1Chips/AddChip.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def specAdd (rs2 rs1 rd : regidx) : StateM SP1State Unit := do
7474

7575
-- TODO(gzgz): this should be auto-generate-able from our constraints.
7676
def sp1Add (Main : Vector BabyBear 23) : StateM SP1State Unit := do
77-
set_pc (Main[3].val + 4) -- dt: This should actually be coming from `CPUState.constraints` send
77+
setPC (Main[3].val + 4) -- dt: This should actually be coming from `CPUState.constraints` send
7878
let op_a := regidx.Regidx Main[4].val
7979
update_reg op_a (BitVec.ofNat 32 (↑Main[20] + ↑Main[21] * 65536))
8080

SP1Chips/BitwiseChip.lean

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,10 @@ theorem SP1BitwiseChip_xor_correct (Main : Vector BabyBear 33)
196196

197197
-- Suffices to show the new register map with cases on it being destination register
198198
refine congr_arg (fun out => pure (_, (_, out))) (funext fun reg => ?_)
199-
by_cases hreg : regidx.Regidx (BitVec.ofNat 5 Main[4].val) = reg
200-
· simp [hreg, hmem₁, hmem₂, hxor0, hxor1, hxor2, hxor3, h27, h29,
201-
Nat.mod_eq_of_lt h1218, Nat.mod_eq_of_lt h1117,
202-
bitVec_helper_xor _ _ _ _ h11 h12 h17 h18,
203-
Fin.xor_val, ofNat_add, ofNat_mul, ofNat_xor]
204-
· rw [Function.update_of_ne (Ne.symm hreg), Function.update_of_ne (Ne.symm hreg)]
205-
199+
simp [hmem₁, hmem₂, hxor0, hxor1, hxor2, hxor3, h27, h29,
200+
Nat.mod_eq_of_lt h1218, Nat.mod_eq_of_lt h1117,
201+
bitVec_helper_xor _ _ _ _ h11 h12 h17 h18,
202+
Fin.xor_val, ofNat_add, ofNat_mul, ofNat_xor]
206203

207204
/-- If the constraints all hold, `is_or` is set to true, and `op_b` and `op_c` are loaded
208205
into the proper registers, then the bitwise chip conforms to the or spec. -/
@@ -261,14 +258,11 @@ theorem SP1BitwiseChip_or_correct (Main : Vector BabyBear 33)
261258
rw [hxor0, hxor1] at h27
262259
rw [hxor2, hxor3] at h29
263260

264-
-- Suffices to show the new register map with cases on it being destination register
265261
refine congr_arg (fun out => pure (_, (_, out))) (funext fun reg => ?_)
266-
by_cases hreg : regidx.Regidx (BitVec.ofNat 5 Main[4].val) = reg
267-
· simp [hreg, hmem₁, hmem₂, hxor0, hxor1, hxor2, hxor3, h27, h29,
268-
Nat.mod_eq_of_lt h1218, Nat.mod_eq_of_lt h1117,
269-
bitVec_helper_or _ _ _ _ h11 h12 h17 h18,
270-
Fin.or_val, ofNat_add, ofNat_mul, ofNat_or]
271-
· rw [Function.update_of_ne (Ne.symm hreg), Function.update_of_ne (Ne.symm hreg)]
262+
simp [hmem₁, hmem₂, hxor0, hxor1, hxor2, hxor3, h27, h29,
263+
Nat.mod_eq_of_lt h1218, Nat.mod_eq_of_lt h1117,
264+
bitVec_helper_or _ _ _ _ h11 h12 h17 h18,
265+
Fin.or_val, ofNat_add, ofNat_mul, ofNat_or]
272266

273267
-- dt: could just hardcode "and" also, would be nice to avoid that
274268

SP1Chips/JalChip.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ def specJal (rd : regidx) (imm : BitVec 21) : StateM SP1State Unit := do
5959
let old_pc := (← get).1
6060
incrementPC
6161
update_reg rd (old_pc + 4#32)
62-
set_pc (old_pc + imm)
62+
setPC (old_pc + imm)
6363

6464
-- TODO(gzgz): this should be auto-generate-able from our constraints.
6565
def sp1Jal (Main : Vector BabyBear 15) : StateM SP1State Unit := do
6666
let rd := regidx.Regidx Main[4].val
6767
let new_pc := BitVec.ofNat 32 (Main[10] + Main[11] * 65536)
6868
let old_pc := BitVec.ofNat 32 (Main[12] + Main[13] * 65536)
69-
if Main[14] = 1 then set_pc new_pc
69+
if Main[14] = 1 then setPC new_pc
7070
if Main[14] = 1 then update_reg rd old_pc
7171

7272
theorem SP1JAL_correct (Main : Vector BabyBear 15)

0 commit comments

Comments
 (0)