33
44 SPDX-License-Identifier: Apache-2.0 OR MIT
55-/
6+ module
67
7- import Strata.Languages.Core.Verifier
8- import Strata.Languages.Core.Factory
9- import Strata.SimpleAPI
8+ meta import Strata.Languages.Core
9+ import StrataDDM.Integration.Lean.HashCommands
1010
1111/-!
12- End-to-end verification tests for the three Bv↔Int cast operators ,
13- exercised all the way through the SMT pipeline via `Strata. Core.verifyProgram `.
12+ End-to-end verification tests for the three Bv↔Int cast built-in functions ,
13+ exercised all the way through the SMT pipeline via `Core.verify `.
1414
15- Factory ops (`Bv{n}.ToUInt`, `Bv{n}.ToInt`, `Int.ToBv{n}`) cannot be called
16- from `#strata program Core;` text, so programs are constructed programmatically
17- using the Lean API.
18-
19- - `Bv{n}.ToUInt` ≙ SMT-LIB 2.7 `ubv_to_int` — unsigned bv → Int
20- - `Bv{n}.ToInt` ≙ SMT-LIB 2.7 `sbv_to_int` — signed bv → Int
21- - `Int.ToBv{n}` ≙ SMT-LIB 2.7 `(_ int_to_bv n)` — Int → bv
15+ - `as_uint(e)` ≙ SMT-LIB 2.7 `ubv_to_int` — unsigned bv → Int
16+ - `as_sint(e)` ≙ SMT-LIB 2.7 `sbv_to_int` — signed bv → Int
17+ - `as_bv8(e)` ≙ SMT-LIB 2.7 `(_ int_to_bv 8)` — Int → bv8
2218 -/
2319
24- namespace Core.BvIntCastVerify
25-
26- open Lambda Core
27-
28- private def xBv8 : Expression.Expr := .fvar () ⟨"x" , ()⟩ (.some (.bitvec 8 ))
29- private def bv8255 : Expression.Expr := .bitvecConst () 8 (255 : BitVec 8 )
30-
31- private def zero : Expression.Expr := .intConst () 0
32- private def i255 : Expression.Expr := .intConst () 255
33- private def i256 : Expression.Expr := .intConst () 256
34- private def negOne : Expression.Expr := .intConst () (-1 )
35-
36- private def applyGe (l r : Expression.Expr) : Expression.Expr :=
37- .app () (.app () intGeOp l) r
38-
39- private def mkProc (name : String) (postcond : Expression.Expr) : Decl :=
20+ meta section
21+ open Strata
22+ open StrataDDM (Program)
23+
24+ private def bvIntCastProgram : Program :=
25+ #strata
26+ program Core;
27+
28+ procedure test_ubv_nonneg(x : bv8)
29+ spec {
30+ ensures as_uint(x) >= 0 ;
31+ }
32+ {
33+ assume true ;
34+ };
35+
36+ procedure test_ubv_concrete()
37+ spec {
38+ ensures as_uint(bv{8 }(255 )) == 255 ;
39+ }
40+ {
41+ assume true ;
42+ };
43+
44+ procedure test_ubv_roundtrip(x : bv8)
45+ spec {
46+ ensures as_bv8(as_uint(x)) == x;
47+ }
48+ {
49+ assume true ;
50+ };
51+
52+ procedure test_sbv_concrete()
53+ spec {
54+ ensures as_sint(bv{8 }(255 )) == -1 ;
55+ }
56+ {
57+ assume true ;
58+ };
59+
60+ procedure test_ubv_impossible(x : bv8)
61+ spec {
62+ ensures as_uint(x) >= 256 ;
63+ }
64+ {
65+ assume true ;
66+ };
67+
68+ #end
69+
70+ private def mkProc (name : String) (postcond : Core.Expression.Expr) : Core.Decl :=
4071 .proc {
4172 header := {
4273 name := ⟨name, ()⟩
@@ -51,25 +82,6 @@ private def mkProc (name : String) (postcond : Expression.Expr) : Decl :=
5182 body := .structured [.assume "body" (.true ()) #[]]
5283 } #[]
5384
54- private def castVerifyProg : Core.Program :=
55- { decls := [
56- -- Provable: Bv8.ToUInt is always nonneg
57- mkProc "test_ubv_nonneg"
58- (applyGe (.app () bv8ToUIntFunc.opExpr xBv8) zero),
59- -- Provable: concrete value bv{8}(255) as unsigned == 255
60- mkProc "test_ubv_concrete"
61- (.eq () (.app () bv8ToUIntFunc.opExpr bv8255) i255),
62- -- Provable: unsigned round-trip Int.ToBv8(Bv8.ToUInt(x)) == x
63- mkProc "test_ubv_roundtrip"
64- (.eq () (.app () int8ToBvFunc.opExpr (.app () bv8ToUIntFunc.opExpr xBv8)) xBv8),
65- -- Provable: signed semantics bv{8}(255) as signed == -1
66- mkProc "test_sbv_concrete"
67- (.eq () (.app () bv8ToIntFunc.opExpr bv8255) negOne),
68- -- Failing: Bv8.ToUInt(x) >= 256 is impossible for 8-bit
69- mkProc "test_ubv_impossible"
70- (applyGe (.app () bv8ToUIntFunc.opExpr xBv8) i256),
71- ] }
72-
7385/--
7486info:
7587Obligation: test_ubv_nonneg_ensures_0
@@ -93,9 +105,4 @@ Property: assert
93105Result: ❌ fail
94106-/
95107#guard_msgs in
96- #eval show IO Unit from do
97- let results ← EIO.toIO (fun e => IO.Error.userError e)
98- (Strata.Core.verifyProgram castVerifyProg Core.VerifyOptions.quiet)
99- IO.println (toString results)
100-
101- end Core.BvIntCastVerify
108+ #eval Strata.Core.verify bvIntCastProgram (options := .quiet)
0 commit comments