module Crypto.PubKey.Rabin.Types
( Error(..)
, generatePrimes
) where
import Crypto.Number.Basic (numBits)
import Crypto.Number.Prime (generatePrime, findPrimeFromWith)
import Crypto.Random.Types
type PrimeCondition = Integer -> Bool
data Error = MessageTooLong
| MessageNotRecognized
| InvalidParameters
deriving (Int -> Error -> ShowS
[Error] -> ShowS
Error -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Error] -> ShowS
$cshowList :: [Error] -> ShowS
show :: Error -> String
$cshow :: Error -> String
showsPrec :: Int -> Error -> ShowS
$cshowsPrec :: Int -> Error -> ShowS
Show, Error -> Error -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Error -> Error -> Bool
$c/= :: Error -> Error -> Bool
== :: Error -> Error -> Bool
$c== :: Error -> Error -> Bool
Eq)
generatePrimes :: MonadRandom m
=> Int
-> PrimeCondition
-> PrimeCondition
-> m (Integer, Integer)
generatePrimes :: forall (m :: * -> *).
MonadRandom m =>
Int -> PrimeCondition -> PrimeCondition -> m (Integer, Integer)
generatePrimes Int
size PrimeCondition
pCond PrimeCondition
qCond =
let pBits :: Int
pBits = (Int
8forall a. Num a => a -> a -> a
*(Int
size forall a. Integral a => a -> a -> a
`div` Int
2))
qBits :: Int
qBits = (Int
8forall a. Num a => a -> a -> a
*(Int
size forall a. Num a => a -> a -> a
- (Int
size forall a. Integral a => a -> a -> a
`div` Int
2)))
in do
Integer
p <- forall {m :: * -> *}.
MonadRandom m =>
Int -> PrimeCondition -> m Integer
generatePrime' Int
pBits PrimeCondition
pCond
Integer
q <- forall {m :: * -> *}.
MonadRandom m =>
Int -> PrimeCondition -> m Integer
generatePrime' Int
qBits PrimeCondition
qCond
forall (m :: * -> *) a. Monad m => a -> m a
return (Integer
p, Integer
q)
where
generatePrime' :: Int -> PrimeCondition -> m Integer
generatePrime' Int
bits PrimeCondition
cond = do
Integer
pr' <- forall (m :: * -> *). MonadRandom m => Int -> m Integer
generatePrime Int
bits
let pr :: Integer
pr = PrimeCondition -> Integer -> Integer
findPrimeFromWith PrimeCondition
cond Integer
pr'
if Integer -> Int
numBits Integer
pr forall a. Eq a => a -> a -> Bool
== Int
bits then forall (m :: * -> *) a. Monad m => a -> m a
return Integer
pr
else Int -> PrimeCondition -> m Integer
generatePrime' Int
bits PrimeCondition
cond