[Gen] make sure it's harder to confuse bits and bytes, and add safer display operation
This commit is contained in:
parent
3fc6dd17a9
commit
f51fdf23ca
51
gen/Gen.hs
51
gen/Gen.hs
@ -1,3 +1,4 @@
|
|||||||
|
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
||||||
module Main where
|
module Main where
|
||||||
|
|
||||||
import System.FilePath
|
import System.FilePath
|
||||||
@ -9,25 +10,45 @@ import Template
|
|||||||
readTemplate templateFile = parseTemplate <$> readFile templateFile
|
readTemplate templateFile = parseTemplate <$> readFile templateFile
|
||||||
writeTemplate file vars multi template = writeFile file (renderTemplate template vars multi)
|
writeTemplate file vars multi template = writeFile file (renderTemplate template vars multi)
|
||||||
|
|
||||||
{-
|
-- print a loud error if the conversion is losing information
|
||||||
|
divSafe :: Int -> Int -> Int
|
||||||
|
divSafe a b
|
||||||
|
| r == 0 = d
|
||||||
|
| otherwise = error ("cannot safely convert values: trying to divide " ++ show a ++ " by " ++ show b ++ " remainder: " ++ show r)
|
||||||
|
where
|
||||||
|
(d,r) = a `divMod` b
|
||||||
|
|
||||||
newtype Bits = Bits Int
|
newtype Bits = Bits Int
|
||||||
deriving (Show,Eq,Num)
|
deriving (Show,Eq,Num)
|
||||||
newtype Bytes = Bytes Int
|
newtype Bytes = Bytes Int
|
||||||
deriving (Show,Eq,Num)
|
deriving (Show,Eq,Num)
|
||||||
-}
|
|
||||||
|
class SizedNum a where
|
||||||
|
showBytes :: a -> String
|
||||||
|
showBits :: a -> String
|
||||||
|
showW64 :: a -> String
|
||||||
|
|
||||||
|
instance SizedNum Bytes where
|
||||||
|
showBits (Bytes b) = show (b * 8)
|
||||||
|
showBytes (Bytes b) = show b
|
||||||
|
showW64 (Bytes b) = show (b `divSafe` 8)
|
||||||
|
instance SizedNum Bits where
|
||||||
|
showBits (Bits b) = show b
|
||||||
|
showBytes (Bits b) = show (b `divSafe` 8)
|
||||||
|
showW64 (Bits b) = show (b `divSafe` 64)
|
||||||
|
|
||||||
data GenHashModule = GenHashModule
|
data GenHashModule = GenHashModule
|
||||||
{ ghmModuleName :: String
|
{ ghmModuleName :: String
|
||||||
, ghmHeaderFile :: String
|
, ghmHeaderFile :: String
|
||||||
, ghmHashName :: String
|
, ghmHashName :: String
|
||||||
, ghmContextSize :: Int -- in bytes
|
, ghmContextSize :: Bytes
|
||||||
, ghmCustomizable :: HashCustom
|
, ghmCustomizable :: HashCustom
|
||||||
} deriving (Show,Eq)
|
} deriving (Show,Eq)
|
||||||
|
|
||||||
data HashCustom =
|
data HashCustom =
|
||||||
HashSimple Int -- digest size in bits
|
HashSimple Bits -- digest size in bits
|
||||||
Int -- block length in bytes
|
Bytes -- block length in bytes
|
||||||
| HashMulti [(Int, Int)] -- list of (digest output size in *bits*, block size in bytes)
|
| HashMulti [(Bits, Bytes)] -- list of (digest output size in *bits*, block size in bytes)
|
||||||
deriving (Show,Eq)
|
deriving (Show,Eq)
|
||||||
|
|
||||||
hashModules =
|
hashModules =
|
||||||
@ -62,8 +83,8 @@ renderHashModules genOpts = do
|
|||||||
let baseVars = [ ("MODULENAME" , ghmModuleName ghm)
|
let baseVars = [ ("MODULENAME" , ghmModuleName ghm)
|
||||||
, ("HEADER_FILE" , ghmHeaderFile ghm)
|
, ("HEADER_FILE" , ghmHeaderFile ghm)
|
||||||
, ("HASHNAME" , ghmHashName ghm)
|
, ("HASHNAME" , ghmHashName ghm)
|
||||||
, ("CTX_SIZE_BYTES" , show (ghmContextSize ghm))
|
, ("CTX_SIZE_BYTES" , showBytes (ghmContextSize ghm))
|
||||||
, ("CTX_SIZE_WORD64" , show (ghmContextSize ghm `div` 8))
|
, ("CTX_SIZE_WORD64" , showW64 (ghmContextSize ghm))
|
||||||
] :: Attrs
|
] :: Attrs
|
||||||
let mainDir = "Crypto/Hash"
|
let mainDir = "Crypto/Hash"
|
||||||
mainName = mainDir </> (ghmModuleName ghm ++ ".hs")
|
mainName = mainDir </> (ghmModuleName ghm ++ ".hs")
|
||||||
@ -74,19 +95,19 @@ renderHashModules genOpts = do
|
|||||||
case ghmCustomizable ghm of
|
case ghmCustomizable ghm of
|
||||||
HashSimple digestSize blockLength ->
|
HashSimple digestSize blockLength ->
|
||||||
(hashTemplate,
|
(hashTemplate,
|
||||||
[ ("DIGEST_SIZE_BITS" , show digestSize)
|
[ ("DIGEST_SIZE_BITS" , showBits digestSize)
|
||||||
, ("DIGEST_SIZE_BYTES", show (digestSize`div` 8))
|
, ("DIGEST_SIZE_BYTES", showBytes digestSize)
|
||||||
, ("BLOCK_SIZE_BYTES" , show blockLength)
|
, ("BLOCK_SIZE_BYTES" , showBytes blockLength)
|
||||||
]
|
]
|
||||||
, []
|
, []
|
||||||
)
|
)
|
||||||
HashMulti customSizes ->
|
HashMulti customSizes ->
|
||||||
(hashLenTemplate, [],
|
(hashLenTemplate, [],
|
||||||
[ ("CUSTOMIZABLE", map (\(outputSizeBits, customBlockSize) ->
|
[ ("CUSTOMIZABLE", map (\(outputSizeBits, customBlockSize) ->
|
||||||
[ ("CUSTOM_BITSIZE", show outputSizeBits)
|
[ ("CUSTOM_BITSIZE", showBits outputSizeBits)
|
||||||
, ("CUSTOM_DIGEST_SIZE_BITS", show outputSizeBits)
|
, ("CUSTOM_DIGEST_SIZE_BITS", showBits outputSizeBits)
|
||||||
, ("CUSTOM_DIGEST_SIZE_BYTES", show (outputSizeBits `div` 8))
|
, ("CUSTOM_DIGEST_SIZE_BYTES", showBytes outputSizeBits)
|
||||||
, ("CUSTOM_BLOCK_SIZE_BYTES", show customBlockSize)
|
, ("CUSTOM_BLOCK_SIZE_BYTES", showBytes customBlockSize)
|
||||||
]) customSizes
|
]) customSizes
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user