{-# OPTIONS_GHC -O2 -feager-blackholing #-}
{-# LANGUAGE Trustworthy, ScopedTypeVariables, PackageImports, NoImplicitPrelude #-}
module Crypto.ECC.Ed25519.Sign ( genkeys
, genkeysSimple
, publickey
, dsign
, sign
, dverify
, verify
, Message
, PubKey
, SecKey
, Signature
, SignedMessage
, SigOK(..)
, VerifyResult
)
where
import safe Crypto.ECC.Ed25519.Internal.Ed25519
import safe Prelude ((==),show,($),(<),IO,return,pure,Either(Left,Right),String,(&&))
import safe qualified Crypto.Fi as FP
import safe qualified Data.ByteString as BS
import qualified "crypto-api" Crypto.Random as CR
genkeysSimple :: IO (Either String (SecKey,PubKey))
genkeysSimple = do
(g :: CR.SystemRandom) <- CR.newGenIO
return $ genkeys g
genkeys :: (CR.CryptoRandomGen g) => g -> Either String (SecKey,PubKey)
genkeys g = case CR.genBytes 32 g of
Left e -> Left (show e)
Right (sk',_) -> let sk = SecKeyBytes sk'
derived = publickey sk
in case derived of
Left e -> Left e
Right pk -> Right (sk,pk)
publickey :: SecKey -> Either String PubKey
publickey (SecKeyBytes sk) = let mysk = BS.take 32 sk
secret = clamp $ BS.take 32 $ h mysk
in case secret of
Left e -> Left e
Right sec -> let aB = pmul bPoint sec
in if ison aB
then Right (pointtobs aB)
else Left "public key is not on curve"
sign :: SecKey -> Message -> Either String SignedMessage
sign sk m = case dsign sk m of
Left e -> Left e
Right sig -> Right (BS.append sig m)
verify :: PubKey -> SignedMessage -> VerifyResult
verify a_ sigm = let sig = BS.take 64 sigm
m = BS.drop 64 sigm
in dverify a_ sig m
dsign :: SecKey -> Message -> Either String Signature
dsign (SecKeyBytes sk) m = do
let mysk = BS.take 32 sk
hsk = h mysk
ahsk = BS.take 32 hsk
rhsk = BS.drop 32 hsk
r <- getFPrime64 $ h $ rhsk `BS.append ` m
let rB_ = pointtobs $ pmul bPoint (FP.redc l r)
a' <- clamp ahsk
let aB_ = pointtobs $ pmul bPoint a'
t' <- getFPrime64 (h $ rB_ `BS.append` aB_ `BS.append` ph m)
let s = FP.addr l r (FP.mulr l t' a')
let s_ = putFPrime s
pure $ BS.append rB_ s_
dverify :: PubKey -> Signature -> Message -> VerifyResult
dverify a_ sig m = do
let r_ = BS.take 32 sig
r <- bstopoint r_
a' <- bstopoint a_
s' <- getFPrime32 $ BS.drop 32 sig
t <- getFPrime64 $ h $ r_ `BS.append` a_ `BS.append` m
if (FP.toInteger s' < FP.toInteger l) && (scale $ pmul bPoint (FP.redc l s')) == (scale $ padd r $ pmul a' (FP.redc l t))
then Right SigOK
else Left "bad Signature"