-- |
-- Module      : Crypto.PubKey.Internal
-- License     : BSD-style
-- Maintainer  : Vincent Hanquez <vincent@snarc.org>
-- Stability   : experimental
-- Portability : Good
--
module Crypto.PubKey.Internal
    ( and'
    , (&&!)
    , dsaTruncHash
    , dsaTruncHashDigest
    ) where

import Data.Bits (shiftR)
import Data.List (foldl')

import Crypto.Hash
import Crypto.Internal.ByteArray (ByteArrayAccess)
import Crypto.Number.Basic (numBits)
import Crypto.Number.Serialize

-- | This is a strict version of and
and' :: [Bool] -> Bool
and' :: [Bool] -> Bool
and' [Bool]
l = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Bool -> Bool -> Bool
(&&!) Bool
True [Bool]
l

-- | This is a strict version of &&.
(&&!) :: Bool -> Bool -> Bool
Bool
True  &&! :: Bool -> Bool -> Bool
&&! Bool
True  = Bool
True
Bool
True  &&! Bool
False = Bool
False
Bool
False &&! Bool
True  = Bool
False
Bool
False &&! Bool
False = Bool
False

-- | Truncate and hash for DSA and ECDSA.
dsaTruncHash :: (ByteArrayAccess msg, HashAlgorithm hash) => hash -> msg -> Integer -> Integer
dsaTruncHash :: forall msg hash.
(ByteArrayAccess msg, HashAlgorithm hash) =>
hash -> msg -> Integer -> Integer
dsaTruncHash hash
hashAlg = forall hash.
HashAlgorithm hash =>
Digest hash -> Integer -> Integer
dsaTruncHashDigest forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ba alg.
(ByteArrayAccess ba, HashAlgorithm alg) =>
alg -> ba -> Digest alg
hashWith hash
hashAlg

-- | Truncate a digest for DSA and ECDSA.
dsaTruncHashDigest :: HashAlgorithm hash => Digest hash -> Integer -> Integer
dsaTruncHashDigest :: forall hash.
HashAlgorithm hash =>
Digest hash -> Integer -> Integer
dsaTruncHashDigest Digest hash
digest Integer
n
    | Int
d forall a. Ord a => a -> a -> Bool
> Int
0 = forall a. Bits a => a -> Int -> a
shiftR Integer
e Int
d
    | Bool
otherwise = Integer
e
  where e :: Integer
e = forall ba. ByteArrayAccess ba => ba -> Integer
os2ip Digest hash
digest
        d :: Int
d = forall a. HashAlgorithm a => a -> Int
hashDigestSize (forall hash. Digest hash -> hash
getHashAlg Digest hash
digest) forall a. Num a => a -> a -> a
* Int
8 forall a. Num a => a -> a -> a
- Integer -> Int
numBits Integer
n

getHashAlg :: Digest hash -> hash
getHashAlg :: forall hash. Digest hash -> hash
getHashAlg Digest hash
_ = forall a. HasCallStack => a
undefined