{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE Safe #-}
module Crypto.NewHope.Internals where
import Control.DeepSeq
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BSC
data N = N512
| N1024
deriving (Eq)
value :: N -> Int
value N512 = 512
value N1024 = 1024
q :: Int
q = 12289
symBytes :: Int
symBytes = 32
seedBytes :: Int
seedBytes = symBytes
sharedSecretBytes :: Int
sharedSecretBytes = symBytes
newtype Seed = Seed BS.ByteString deriving Eq
instance NFData Seed
where
rnf (Seed seedData) = seq seedData ()
getSeedData :: Seed -> BS.ByteString
getSeedData (Seed seedData) = seedData
class Seedable a
where
makeSeed :: a -> Seed
instance Seedable BS.ByteString
where
makeSeed bs | not lengthOK = error $ "Invalid length for Seed. Have " ++ show len ++ " and require " ++ show symBytes ++ " bytes."
| otherwise = Seed bs
where
len = BS.length bs
lengthOK = len == symBytes
instance Seedable String
where
makeSeed s | not lengthOK = error $ "Invalid length for Seed. Have " ++ show len ++ " and require " ++ show symBytes ++ " bytes."
| otherwise = Seed $ BSC.pack s
where
len = length s
lengthOK = len == symBytes