Skip to content

Commit f310eda

Browse files
who2: Fix rol/ror bug, reinstate property tests
1 parent c7e77b5 commit f310eda

File tree

4 files changed

+49
-22
lines changed

4 files changed

+49
-22
lines changed

who2/src/Who2/Builder/Ops/BV.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,7 @@ bvRol alloc x y
10491049
-- test: bvrol-const
10501050
| Just (wx, bvx) <- asBVLit x
10511051
, Just (_, bvy) <- asBVLit y =
1052-
bvLit alloc wx (BV.rotateL wx bvx (fromInteger (BV.asUnsigned bvy)))
1052+
bvLit alloc wx (BV.rotateL wx bvx (fromInteger (BV.asUnsigned bvy `mod` NR.intValue wx)))
10531053
| otherwise = do
10541054
let w = EBV.width x
10551055
-- Cryptol> :prove \(x : [8]) -> x <<< 8 == x
@@ -1075,7 +1075,7 @@ bvRor alloc x y
10751075
-- test: bvror-const
10761076
| Just (wx, bvx) <- asBVLit x
10771077
, Just (_, bvy) <- asBVLit y =
1078-
bvLit alloc wx (BV.rotateR wx bvx (fromInteger (BV.asUnsigned bvy)))
1078+
bvLit alloc wx (BV.rotateR wx bvx (fromInteger (BV.asUnsigned bvy `mod` NR.intValue wx)))
10791079
| otherwise = do
10801080
let w = EBV.width x
10811081
-- Cryptol> :prove \(x : [8]) -> x >>> 8 == x

who2/test/Main.hs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,15 @@ hashConsedTests =
116116
simplificationCorrectnessTests :: TestTree
117117
simplificationCorrectnessTests =
118118
testGroup "Simplification Correctness"
119-
[ -- TODO: These all fail :-(
120-
-- [ testProperty "General simplifications (depth 5)" $
121-
-- Hedgehog.withTests 32 Props.propSimplificationCorrect -- TODO: increase
122-
-- , testProperty "Deep expressions (depth 10)" $
123-
-- Hedgehog.withTests 32 Props.propDeepSimplifications -- TODO: increase
124-
-- testProperty "Boolean-only expressions (100 tests)" $
125-
-- Hedgehog.withTests 128 Props.propBoolSimplifications
126-
-- , testProperty "BV arithmetic expressions (100 tests)" $
127-
-- Hedgehog.withTests 128 Props.propBvArithSimplifications
128-
testProperty "No variables reduces to literal" $
119+
[ testProperty "General simplifications (depth 5)" $
120+
Hedgehog.withTests 32 Props.propSimplificationCorrect -- TODO: increase
121+
, testProperty "Deep expressions (depth 10)" $
122+
Hedgehog.withTests 32 Props.propDeepSimplifications -- TODO: increase
123+
, testProperty "Boolean-only expressions (100 tests)" $
124+
Hedgehog.withTests 128 Props.propBoolSimplifications
125+
, testProperty "BV arithmetic expressions (100 tests)" $
126+
Hedgehog.withTests 128 Props.propBvArithSimplifications
127+
, testProperty "No variables reduces to literal" $
129128
Hedgehog.withTests 1000 Props.propNoVariablesReducesToLiteral
130129
]
131130

who2/test/Who2/Builder/Simplification.hs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module Who2.Builder.Simplification
2020

2121
import Control.Exception (catch, SomeException, evaluate)
2222
import Control.Monad.IO.Class (liftIO)
23+
import Data.List (isInfixOf)
2324
import qualified Data.Text as Text
2425
import qualified Data.Text.Lazy as Text.Lazy
2526
import qualified Data.Text.Lazy.Builder as Builder
@@ -29,6 +30,7 @@ import System.Process (readProcessWithExitCode)
2930
import System.Timeout (timeout)
3031

3132
import Data.Parameterized.Nonce (withIONonceGenerator)
33+
import qualified Hedgehog as H
3234
import Hedgehog (Property, PropertyT, property, forAll, annotate, failure, assert)
3335
import qualified Hedgehog.Gen as Gen
3436

@@ -106,25 +108,31 @@ checkSimplificationPreserved expr = do
106108
case result of
107109
(Right simpleRes, Right naiveRes) | simpleRes == naiveRes ->
108110
pure () -- Equisatisfiable
109-
(Left "term rendering timeout", Left _) -> do
110-
annotate "Skipped - naive term too large to render"
111-
pure () -- Pass the test, don't fail on huge terms
112111
(Right simpleRes, Right naiveRes) -> do
113112
annotate $ "Expression: " ++ show expr
114113
annotate $ "Simplified result: " ++ Text.unpack simpleRes
115114
annotate $ "Naive result: " ++ Text.unpack naiveRes
116115
annotate "Results differ - not equisatisfiable"
117116
failure
118-
(Left err, _) | "term rendering timeout" `elem` words err -> do
117+
(Left "term rendering timeout", Left _) -> do
118+
annotate "Skipped - naive term too large to render"
119+
H.discard
120+
(Left err, _) | "term rendering timeout" `isInfixOf` err -> do
121+
annotate "Skipped - term too large to render"
122+
H.discard
123+
(Left err, _) | "z3 timeout" `isInfixOf` err -> do
124+
annotate "Skipped - Z3 timeout (query too complex)"
125+
H.discard
126+
(_, Left err) | "term rendering timeout" `isInfixOf` err -> do
119127
annotate "Skipped - term too large to render"
120-
pure () -- Pass the test
128+
H.discard
129+
(_, Left err) | "z3 timeout" `isInfixOf` err -> do
130+
annotate "Skipped - Z3 timeout (query too complex)"
131+
H.discard
121132
(Left err, _) -> do
122133
annotate $ "Expression: " ++ show expr
123134
annotate $ "Z3 error (simplified): " ++ err
124135
failure
125-
(_, Left err) | "term rendering timeout" `elem` words err -> do
126-
annotate "Skipped - term too large to render"
127-
pure () -- Pass the test
128136
(_, Left err) -> do
129137
annotate $ "Expression: " ++ show expr
130138
annotate $ "Z3 error (naive): " ++ err

who2/test/Who2/Builder/TestBuilder.hs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module Who2.Builder.TestBuilder
1414

1515
import Data.Parameterized.Nonce (NonceGenerator, Nonce)
1616
import qualified Data.Parameterized.Nonce as Nonce
17+
import Data.Parameterized.NatRepr (natValue)
1718
import qualified Data.BitVector.Sized as BVS
1819

1920
import What4.BaseTypes (BaseBoolType)
@@ -33,6 +34,7 @@ import Who2.Expr.SymExpr (SymExpr(..))
3334
import qualified Who2.Builder.Ops.BV as BV
3435
import qualified Who2.Expr.BV as EBV
3536
import qualified Who2.Expr.Logic as EL
37+
import qualified Who2.Expr.Views as EV
3638
import qualified Who2.Expr.Bloom.Polarized as PBS
3739
import qualified Who2.Expr.Bloom.SemiRing.Product as SRP
3840
import qualified Who2.Expr.Bloom.SemiRing.Sum as SRS
@@ -282,13 +284,31 @@ instance IsExprBuilder (TestBuilder t) where
282284
bvRol tb x y = do
283285
let SymExpr ex = x
284286
SymExpr ey = y
285-
result <- naiveAlloc tb (BVApp (EBV.BVRol (EBV.width ex) ex ey))
287+
w = EBV.width ex
288+
-- Normalize rotation amount modulo width (same as Builder)
289+
-- But only if it's not already a literal < width
290+
y' <- case EV.asBVLit ey of
291+
Just (_, bv) | BVS.asUnsigned bv < toInteger (natValue w) -> pure y -- Already normalized
292+
_ -> do
293+
widthLit <- bvLit tb w (BVS.mkBV w (toInteger (natValue w)))
294+
bvUrem tb y widthLit
295+
let SymExpr ey' = y'
296+
result <- naiveAlloc tb (BVApp (EBV.BVRol w ex ey'))
286297
pure (SymExpr result)
287298

288299
bvRor tb x y = do
289300
let SymExpr ex = x
290301
SymExpr ey = y
291-
result <- naiveAlloc tb (BVApp (EBV.BVRor (EBV.width ex) ex ey))
302+
w = EBV.width ex
303+
-- Normalize rotation amount modulo width (same as Builder)
304+
-- But only if it's not already a literal < width
305+
y' <- case EV.asBVLit ey of
306+
Just (_, bv) | BVS.asUnsigned bv < toInteger (natValue w) -> pure y -- Already normalized
307+
_ -> do
308+
widthLit <- bvLit tb w (BVS.mkBV w (toInteger (natValue w)))
309+
bvUrem tb y widthLit
310+
let SymExpr ey' = y'
311+
result <- naiveAlloc tb (BVApp (EBV.BVRor w ex ey'))
292312
pure (SymExpr result)
293313

294314
bvConcat tb x y = do

0 commit comments

Comments
 (0)