aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYann Herklotz <git@yannherklotz.com>2020-04-07 15:11:51 +0100
committerYann Herklotz <git@yannherklotz.com>2020-04-07 15:11:51 +0100
commit4a3f2b3448851fe9ac017ff397f9b9ae2babdce3 (patch)
treeabf392585210eee89691ebcbc135dce1ca93e51b
parenta6cf1079e40061196d84cfc186857f379249db2c (diff)
downloadverismith-4a3f2b3448851fe9ac017ff397f9b9ae2babdce3.tar.gz
verismith-4a3f2b3448851fe9ac017ff397f9b9ae2babdce3.zip
Remove DRBG dependency
-rw-r--r--src/Verismith.hs19
-rw-r--r--src/Verismith/Fuzz.hs17
-rw-r--r--src/Verismith/Utils.hs29
-rw-r--r--verismith.cabal4
4 files changed, 36 insertions, 33 deletions
diff --git a/src/Verismith.hs b/src/Verismith.hs
index 61a56f2..578b7ac 100644
--- a/src/Verismith.hs
+++ b/src/Verismith.hs
@@ -39,7 +39,6 @@ where
import Control.Concurrent
import Control.Lens hiding ((<.>))
import Control.Monad.IO.Class (liftIO)
-import qualified Crypto.Random.DRBG as C
import Data.ByteString (ByteString)
import Data.ByteString.Builder (byteStringHex, toLazyByteString)
import qualified Data.ByteString.Lazy as L
@@ -71,19 +70,7 @@ import Verismith.Tool
import Verismith.Tool.Internal
import Verismith.Verilog
import Verismith.Verilog.Parser (parseSourceInfoFile)
-
--- | Generate a specific number of random bytestrings of size 256.
-randomByteString :: C.CtrDRBG -> Int -> [ByteString] -> [ByteString]
-randomByteString gen n bytes
- | n == 0 = ranBytes : bytes
- | otherwise = randomByteString newGen (n - 1) $ ranBytes : bytes
- where Right (ranBytes, newGen) = C.genBytes 32 gen
-
--- | generates the specific number of bytestring with a random seed.
-generateByteString :: Int -> IO [ByteString]
-generateByteString n = do
- gen <- C.newGenIO :: IO C.CtrDRBG
- return $ randomByteString gen n []
+import Verismith.Utils (generateByteString)
toFP :: String -> FilePath
toFP = fromText . T.pack
@@ -256,7 +243,7 @@ runSimulation = do
-- shelly $ run_ "dot" ["-Tpng", "-o", "file.png", "file.dot"]
-- let circ =
-- head $ (nestUpTo 30 . generateAST $ Circuit gr) ^.. getVerilog . traverse . getDescription
- rand <- generateByteString 20
+ rand <- generateByteString Nothing 32 20
rand2 <- Hog.sample (randomMod 10 100)
val <- shelly . runResultT $ runSim defaultIcarus (makeSrcInfo rand2) rand
case val of
@@ -304,7 +291,7 @@ runEquivalence
runEquivalence seed gm t d k i = do
(_, m) <- shelly $ sampleSeed seed gm
let srcInfo = SourceInfo "top" m
- rand <- generateByteString 20
+ rand <- generateByteString Nothing 32 20
datadir <- getDataDir
shellyFailDir $ do
mkdir_p (fromText d </> fromText n)
diff --git a/src/Verismith/Fuzz.hs b/src/Verismith/Fuzz.hs
index 10fbfa8..20dc13c 100644
--- a/src/Verismith/Fuzz.hs
+++ b/src/Verismith/Fuzz.hs
@@ -36,7 +36,6 @@ import Control.Monad.IO.Class
import Control.Monad.Reader
import Control.Monad.State.Strict
import Control.Monad.Trans.Control (MonadBaseControl)
-import qualified Crypto.Random.DRBG as C
import Data.ByteString (ByteString)
import Data.List (nubBy, sort)
import Data.Maybe (catMaybes, fromMaybe, isNothing)
@@ -64,6 +63,7 @@ import Verismith.Tool.Internal
import Verismith.Tool.Yosys
import Verismith.Verilog.AST
import Verismith.Verilog.CodeGen
+import Verismith.Utils (generateByteString)
data FuzzOpts = FuzzOpts { _fuzzOptsOutput :: !(Maybe FilePath)
, _fuzzOptsForced :: !Bool
@@ -308,7 +308,7 @@ simulation src = do
datadir <- fmap _fuzzDataDir askOpts
synth <- passedSynthesis
counterEgs <- failEquivWithIdentityCE
- vals <- liftIO $ generateByteString 20
+ vals <- liftIO $ generateByteString Nothing 32 20
ident <- liftSh $ sim datadir vals Nothing defaultIdentitySynth
resTimes <- liftSh $ mapM (sim datadir vals (justPass $ snd ident)) synth
resTimes2 <- liftSh $ mapM (simCounterEg datadir) counterEgs
@@ -338,19 +338,6 @@ simulation src = do
runSimIcEC datadir defaultIcarus a src b (Just ident)
where dir = fromText $ "countereg_sim_" <> toText a
--- | Generate a specific number of random bytestrings of size 256.
-randomByteString :: C.CtrDRBG -> Int -> [ByteString] -> [ByteString]
-randomByteString gen n bytes
- | n == 0 = ranBytes : bytes
- | otherwise = randomByteString newGen (n - 1) $ ranBytes : bytes
- where Right (ranBytes, newGen) = C.genBytes 32 gen
-
--- | generates the specific number of bytestring with a random seed.
-generateByteString :: Int -> IO [ByteString]
-generateByteString n = do
- gen <- C.newGenIO :: IO C.CtrDRBG
- return $ randomByteString gen n []
-
failEquivWithIdentity :: (MonadSh m) => Fuzz m [SynthResult]
failEquivWithIdentity = filter withIdentity . _fuzzSynthResults <$> get
where
diff --git a/src/Verismith/Utils.hs b/src/Verismith/Utils.hs
new file mode 100644
index 0000000..1f5dd01
--- /dev/null
+++ b/src/Verismith/Utils.hs
@@ -0,0 +1,29 @@
+{-|
+Module : Verismith
+Description : Verismith
+Copyright : (c) 2020, Yann Herklotz
+License : GPL-3
+Maintainer : yann [at] yannherklotz [dot] com
+Stability : experimental
+Portability : POSIX
+-}
+
+module Verismith.Utils
+ (generateByteString)
+where
+
+import System.Random (mkStdGen, newStdGen, randoms)
+import Data.ByteString (ByteString, pack)
+
+generateByteString :: (Maybe Int) -> Int -> Int -> IO [ByteString]
+generateByteString mseed size n = do
+ fmap pack . chunksOf size . take (size * n) . randoms <$>
+ case mseed of
+ Just seed' -> return $ mkStdGen seed'
+ Nothing -> newStdGen
+ where
+ chunksOf i _ | i <= 0 = error $ "chunksOf, number must be positive, got " ++ show i
+ chunksOf i xs = repeatedly (splitAt i) xs
+ repeatedly _ [] = []
+ repeatedly f as = b : repeatedly f as'
+ where (b, as') = f as
diff --git a/verismith.cabal b/verismith.cabal
index 43280d5..f272bac 100644
--- a/verismith.cabal
+++ b/verismith.cabal
@@ -59,6 +59,7 @@ library
, Verismith.Tool.Vivado
, Verismith.Tool.XST
, Verismith.Tool.Yosys
+ , Verismith.Utils
, Verismith.Verilog
, Verismith.Verilog.AST
, Verismith.Verilog.BitVec
@@ -71,8 +72,7 @@ library
, Verismith.Verilog.Preprocess
, Verismith.Verilog.Quote
, Verismith.Verilog.Token
- build-depends: DRBG >=0.5 && <0.6
- , array >=0.5 && <0.6
+ build-depends: array >=0.5 && <0.6
, base >=4.7 && <5
, binary >= 0.8.5.1 && <0.9
, blaze-html >=0.9.0.1 && <0.10