{-# LANGUAGE DataKinds #-}
{-# OPTIONS_GHC -Wall -Werror #-}
module Documentation.SBV.Examples.Existentials.CRCPolynomial where
import Data.SBV
import Data.SBV.Tools.Polynomial
crc_48_16 :: SWord 48 -> SWord16 -> [SBool]
crc_48_16 :: SWord 48 -> SWord16 -> [SBool]
crc_48_16 SWord 48
msg SWord16
poly = Int -> [SBool] -> [SBool] -> [SBool]
crcBV Int
16 (forall a. SFiniteBits a => SBV a -> [SBool]
blastBE SWord 48
msg) (forall a. SFiniteBits a => SBV a -> [SBool]
blastBE SWord16
poly)
diffCount :: (SWord 48, [SBool]) -> (SWord 48, [SBool]) -> SWord8
diffCount :: (SWord 48, [SBool]) -> (SWord 48, [SBool]) -> SWord8
diffCount (SWord 48
d1, [SBool]
crc1) (SWord 48
d2, [SBool]
crc2) = forall {a}. (Num a, Mergeable a) => [SBool] -> a
count [SBool]
xorBits
where bits1 :: [SBool]
bits1 = forall a. SFiniteBits a => SBV a -> [SBool]
blastBE SWord 48
d1 forall a. [a] -> [a] -> [a]
++ [SBool]
crc1
bits2 :: [SBool]
bits2 = forall a. SFiniteBits a => SBV a -> [SBool]
blastBE SWord 48
d2 forall a. [a] -> [a] -> [a]
++ [SBool]
crc2
xorBits :: [SBool]
xorBits = forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith SBool -> SBool -> SBool
(.<+>) [SBool]
bits1 [SBool]
bits2
count :: [SBool] -> a
count [] = a
0
count (SBool
b:[SBool]
bs) = let r :: a
r = [SBool] -> a
count [SBool]
bs in forall a. Mergeable a => SBool -> a -> a -> a
ite SBool
b (a
1forall a. Num a => a -> a -> a
+a
r) a
r
crcGood :: SWord8 -> SWord16 -> SWord 48 -> SWord 48 -> SBool
crcGood :: SWord8 -> SWord16 -> SWord 48 -> SWord 48 -> SBool
crcGood SWord8
hd SWord16
poly SWord 48
sent SWord 48
received =
SWord 48
sent forall a. EqSymbolic a => a -> a -> SBool
./= SWord 48
received SBool -> SBool -> SBool
.=> (SWord 48, [SBool]) -> (SWord 48, [SBool]) -> SWord8
diffCount (SWord 48
sent, [SBool]
crcSent) (SWord 48
received, [SBool]
crcReceived) forall a. OrdSymbolic a => a -> a -> SBool
.>= SWord8
hd
where crcSent :: [SBool]
crcSent = SWord 48 -> SWord16 -> [SBool]
crc_48_16 SWord 48
sent SWord16
poly
crcReceived :: [SBool]
crcReceived = SWord 48 -> SWord16 -> [SBool]
crc_48_16 SWord 48
received SWord16
poly
genPoly :: SWord8 -> Int -> IO ()
genPoly :: SWord8 -> Int -> IO ()
genPoly SWord8
hd Int
maxCnt = do AllSatResult
res <- forall a. Provable a => SMTConfig -> a -> IO AllSatResult
allSatWith SMTConfig
defaultSMTCfg{allSatMaxModelCount :: Maybe Int
allSatMaxModelCount = forall a. a -> Maybe a
Just Int
maxCnt} forall a b. (a -> b) -> a -> b
$ do
SWord16
p <- forall a. SymVal a => String -> Symbolic (SBV a)
sbvExists String
"polynomial"
SWord 48
s <- forall a. SymVal a => String -> Symbolic (SBV a)
sbvForall String
"sent"
SWord 48
r <- forall a. SymVal a => String -> Symbolic (SBV a)
sbvForall String
"received"
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. SFiniteBits a => SBV a -> Int -> SBool
sTestBit SWord16
p Int
0 SBool -> SBool -> SBool
.&& SWord8 -> SWord16 -> SWord 48 -> SWord 48 -> SBool
crcGood SWord8
hd SWord16
p SWord 48
s SWord 48
r
Int
cnt <- forall a.
SatModel a =>
([(Bool, a)] -> [(Bool, a)])
-> (Int -> (Bool, a) -> IO ()) -> AllSatResult -> IO Int
displayModels forall a. a -> a
id Int -> (Bool, Word16) -> IO ()
disp AllSatResult
res
String -> IO ()
putStrLn forall a b. (a -> b) -> a -> b
$ String
"Found: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
cnt forall a. [a] -> [a] -> [a]
++ String
" polynomail(s)."
where disp :: Int -> (Bool, Word16) -> IO ()
disp :: Int -> (Bool, Word16) -> IO ()
disp Int
n (Bool
_, Word16
s) = String -> IO ()
putStrLn forall a b. (a -> b) -> a -> b
$ String
"Polynomial #" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
n forall a. [a] -> [a] -> [a]
++ String
". x^16 + " forall a. [a] -> [a] -> [a]
++ forall a. Polynomial a => Bool -> a -> String
showPolynomial Bool
False Word16
s
findHD4Polynomials :: IO ()
findHD4Polynomials :: IO ()
findHD4Polynomials = SWord8 -> Int -> IO ()
genPoly SWord8
4 Int
cnt
where cnt :: Int
cnt = Int
5
{-# ANN crc_48_16 ("HLint: ignore Use camelCase" :: String) #-}