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 (Show, Eq)
generatePrimes :: MonadRandom m
=> Int
-> PrimeCondition
-> PrimeCondition
-> m (Integer, Integer)
generatePrimes size pCond qCond =
let pBits = (8*(size `div` 2))
qBits = (8*(size - (size `div` 2)))
in do
p <- generatePrime' pBits pCond
q <- generatePrime' qBits qCond
return (p, q)
where
generatePrime' bits cond = do
pr' <- generatePrime bits
let pr = findPrimeFromWith cond pr'
if numBits pr == bits then return pr
else generatePrime' bits cond