{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}

module Test.Crypto.Vector.Vectors (
  defaultSKey,
  defaultMessage,
  signAndVerifyTestVectors,
  wrongEcdsaVerKeyTestVector,
  wrongSchnorrVerKeyTestVector,
  wrongLengthMessageHashTestVectors,
  ecdsaVerKeyAndSigVerifyTestVectors,
  schnorrVerKeyAndSigVerifyTestVectors,
  ecdsaMismatchMessageAndSignature,
  schnorrMismatchMessageAndSignature,
  verKeyNotOnCurveTestVectorRaw,
  wrongLengthVerKeyTestVectorsRaw,
  ecdsaWrongLengthSigTestVectorsRaw,
  schnorrWrongLengthSigTestVectorsRaw,
  ecdsaNegSigTestVectors,
)
where

import Cardano.Binary (FromCBOR)
import Cardano.Crypto.DSIGN (
  DSIGNAlgorithm (SigDSIGN, SignKeyDSIGN, VerKeyDSIGN),
  EcdsaSecp256k1DSIGN,
  SchnorrSecp256k1DSIGN,
 )
import Data.ByteString (ByteString)
import Test.Crypto.Vector.SerializationUtils (
  HexStringInCBOR (..),
  sKeyParser,
  sigParser,
  vKeyParser,
 )

defaultSKey :: forall d. FromCBOR (SignKeyDSIGN d) => SignKeyDSIGN d
defaultSKey :: forall d. FromCBOR (SignKeyDSIGN d) => SignKeyDSIGN d
defaultSKey = forall d.
FromCBOR (SignKeyDSIGN d) =>
HexStringInCBOR -> SignKeyDSIGN d
sKeyParser HexStringInCBOR
"B7E151628AED2A6ABF7158809CF4F3C762E7160F38B4DA56A784D9045190CFEF"

defaultMessage :: ByteString
defaultMessage :: ByteString
defaultMessage = ByteString
"243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89"

-- These vectors contains secret key which first signs the given message and verifies using generated signature and derived vKey
signAndVerifyTestVectors :: forall d. FromCBOR (SignKeyDSIGN d) => [(SignKeyDSIGN d, ByteString)]
signAndVerifyTestVectors :: forall d.
FromCBOR (SignKeyDSIGN d) =>
[(SignKeyDSIGN d, ByteString)]
signAndVerifyTestVectors =
  forall a b. (a -> b) -> [a] -> [b]
map
    (\(HexStringInCBOR
sk, ByteString
m) -> (forall d.
FromCBOR (SignKeyDSIGN d) =>
HexStringInCBOR -> SignKeyDSIGN d
sKeyParser HexStringInCBOR
sk, ByteString
m))
    [
      ( HexStringInCBOR
"0000000000000000000000000000000000000000000000000000000000000003"
      , ByteString
"0000000000000000000000000000000000000000000000000000000000000000"
      )
    ,
      ( HexStringInCBOR
"B7E151628AED2A6ABF7158809CF4F3C762E7160F38B4DA56A784D9045190CFEF"
      , ByteString
"243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89"
      )
    ,
      ( HexStringInCBOR
"C90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B14E5C9"
      , ByteString
"7E2D58D8B3BCDF1ABADEC7829054F90DDA9805AAB56C77333024B9D0A508B75C"
      )
    ,
      ( HexStringInCBOR
"0B432B2677937381AEF05BB02A66ECD012773062CF3FA2549E44F58ED2401710"
      , ByteString
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
      )
    ]

-- It is used for testing already given message, signature and vKey so that Ver should be sucessful without needing secret key to sign the message for ecdsa.
ecdsaVerKeyAndSigVerifyTestVectors ::
  (VerKeyDSIGN EcdsaSecp256k1DSIGN, ByteString, SigDSIGN EcdsaSecp256k1DSIGN)
ecdsaVerKeyAndSigVerifyTestVectors :: (VerKeyDSIGN EcdsaSecp256k1DSIGN, ByteString,
 SigDSIGN EcdsaSecp256k1DSIGN)
ecdsaVerKeyAndSigVerifyTestVectors =
  ( forall d.
FromCBOR (VerKeyDSIGN d) =>
HexStringInCBOR -> VerKeyDSIGN d
vKeyParser HexStringInCBOR
"02599de3e582e2a3779208a210dfeae8f330b9af00a47a7fb22e9bb8ef596f301b"
  , ByteString
"0000000000000000000000000000000000000000000000000000000000000000"
  , forall d. FromCBOR (SigDSIGN d) => HexStringInCBOR -> SigDSIGN d
sigParser
      HexStringInCBOR
"354b868c757ef0b796003f7c23dd754d2d1726629145be2c7b7794a25fec80a06254f0915935f33b91bceb16d46ff2814f659e9b6791a4a21ff8764b78d7e114"
  )

ecdsaNegSigTestVectors ::
  (VerKeyDSIGN EcdsaSecp256k1DSIGN, ByteString, SigDSIGN EcdsaSecp256k1DSIGN)
ecdsaNegSigTestVectors :: (VerKeyDSIGN EcdsaSecp256k1DSIGN, ByteString,
 SigDSIGN EcdsaSecp256k1DSIGN)
ecdsaNegSigTestVectors =
  ( forall d.
FromCBOR (VerKeyDSIGN d) =>
HexStringInCBOR -> VerKeyDSIGN d
vKeyParser HexStringInCBOR
"02599de3e582e2a3779208a210dfeae8f330b9af00a47a7fb22e9bb8ef596f301b"
  , ByteString
"0000000000000000000000000000000000000000000000000000000000000000"
  , forall d. FromCBOR (SigDSIGN d) => HexStringInCBOR -> SigDSIGN d
sigParser
      HexStringInCBOR
"354b868c757ef0b796003f7c23dd754d2d1726629145be2c7b7794a25fec80a09dab0f6ea6ca0cc46e4314e92b900d7d6b493e4b47b6fb999fd9e841575e602d"
  )

-- It is used for testing already given message, signature and vKey so that Ver should be sucessful without needing secret key to sign the message for schnorr.
schnorrVerKeyAndSigVerifyTestVectors ::
  (VerKeyDSIGN SchnorrSecp256k1DSIGN, ByteString, SigDSIGN SchnorrSecp256k1DSIGN)
schnorrVerKeyAndSigVerifyTestVectors :: (VerKeyDSIGN SchnorrSecp256k1DSIGN, ByteString,
 SigDSIGN SchnorrSecp256k1DSIGN)
schnorrVerKeyAndSigVerifyTestVectors =
  ( forall d.
FromCBOR (VerKeyDSIGN d) =>
HexStringInCBOR -> VerKeyDSIGN d
vKeyParser HexStringInCBOR
"599de3e582e2a3779208a210dfeae8f330b9af00a47a7fb22e9bb8ef596f301b"
  , ByteString
"0000000000000000000000000000000000000000000000000000000000000000"
  , forall d. FromCBOR (SigDSIGN d) => HexStringInCBOR -> SigDSIGN d
sigParser
      HexStringInCBOR
"5a56da88e6fd8419181dec4d3dd6997bab953d2fc71ab65e23cfc9e7e3d1a310613454a60f6703819a39fdac2a410a094442afd1fc083354443e8d8bb4461a9b"
  )

-- Wrong length message hash are used to test ecdsa toMessageHash function
wrongLengthMessageHashTestVectors :: [ByteString]
wrongLengthMessageHashTestVectors :: [ByteString]
wrongLengthMessageHashTestVectors =
  [ ByteString
"0"
  , ByteString
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE"
  ]

wrongEcdsaVerKeyTestVector :: VerKeyDSIGN EcdsaSecp256k1DSIGN
wrongEcdsaVerKeyTestVector :: VerKeyDSIGN EcdsaSecp256k1DSIGN
wrongEcdsaVerKeyTestVector = forall d.
FromCBOR (VerKeyDSIGN d) =>
HexStringInCBOR -> VerKeyDSIGN d
vKeyParser HexStringInCBOR
"02D69C3509BB99E412E68B0FE8544E72837DFA30746D8BE2AA65975F29D22DC7B9"

wrongSchnorrVerKeyTestVector :: VerKeyDSIGN SchnorrSecp256k1DSIGN
wrongSchnorrVerKeyTestVector :: VerKeyDSIGN SchnorrSecp256k1DSIGN
wrongSchnorrVerKeyTestVector = forall d.
FromCBOR (VerKeyDSIGN d) =>
HexStringInCBOR -> VerKeyDSIGN d
vKeyParser HexStringInCBOR
"D69C3509BB99E412E68B0FE8544E72837DFA30746D8BE2AA65975F29D22DC7B9"

-- Raw string verification key that is not on the curve should result in ver key parse failed
verKeyNotOnCurveTestVectorRaw :: HexStringInCBOR
verKeyNotOnCurveTestVectorRaw :: HexStringInCBOR
verKeyNotOnCurveTestVectorRaw = HexStringInCBOR
"02EEFDEA4CDB677750A420FEE807EACF21EB9898AE79B9768766E4FAA04A2D4A34"

-- Parse these vectors and expect the errors on tests with parity bit
wrongLengthVerKeyTestVectorsRaw :: [HexStringInCBOR]
wrongLengthVerKeyTestVectorsRaw :: [HexStringInCBOR]
wrongLengthVerKeyTestVectorsRaw =
  [ -- Ver key of length 30 bytes
    HexStringInCBOR
"02DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B50"
  , -- Ver key of length 34 bytes
    HexStringInCBOR
"02DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659FF"
  ]

-- Raw hexstring to be used in invalid length signature parser tests for ecdsa
ecdsaWrongLengthSigTestVectorsRaw :: [HexStringInCBOR]
ecdsaWrongLengthSigTestVectorsRaw :: [HexStringInCBOR]
ecdsaWrongLengthSigTestVectorsRaw =
  [ HexStringInCBOR
"354b868c757ef0b796003f7c23dd754d2d1726629145be2c7b7794a25fec80a06254f0915935f33b91bceb16d46ff2814f659e9b6791a4a21ff8764b78d7e1"
  , HexStringInCBOR
"354b868c757ef0b796003f7c23dd754d2d1726629145be2c7b7794a25fec80a06254f0915935f33b91bceb16d46ff2814f659e9b6791a4a21ff8764b78d7e114FF"
  ]

-- Raw hexstring to be used in invalid length signature parser tests for schnorr
schnorrWrongLengthSigTestVectorsRaw :: [HexStringInCBOR]
schnorrWrongLengthSigTestVectorsRaw :: [HexStringInCBOR]
schnorrWrongLengthSigTestVectorsRaw =
  [ HexStringInCBOR
"5a56da88e6fd8419181dec4d3dd6997bab953d2fc71ab65e23cfc9e7e3d1a310613454a60f6703819a39fdac2a410a094442afd1fc083354443e8d8bb4461a"
  , HexStringInCBOR
"5a56da88e6fd8419181dec4d3dd6997bab953d2fc71ab65e23cfc9e7e3d1a310613454a60f6703819a39fdac2a410a094442afd1fc083354443e8d8bb4461a9bFF"
  ]

ecdsaMismatchMessageAndSignature ::
  [(ByteString, VerKeyDSIGN EcdsaSecp256k1DSIGN, SigDSIGN EcdsaSecp256k1DSIGN)]
ecdsaMismatchMessageAndSignature :: [(ByteString, VerKeyDSIGN EcdsaSecp256k1DSIGN,
  SigDSIGN EcdsaSecp256k1DSIGN)]
ecdsaMismatchMessageAndSignature =
  forall a b. (a -> b) -> [a] -> [b]
map
    (\(ByteString
vm, HexStringInCBOR
vKey, HexStringInCBOR
sig) -> (ByteString
vm, forall d.
FromCBOR (VerKeyDSIGN d) =>
HexStringInCBOR -> VerKeyDSIGN d
vKeyParser HexStringInCBOR
vKey, forall d. FromCBOR (SigDSIGN d) => HexStringInCBOR -> SigDSIGN d
sigParser HexStringInCBOR
sig))
    --  verifyMessage, vKey, signature
    [
      ( ByteString
"243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89"
      , HexStringInCBOR
"0325d1dff95105f5253c4022f628a996ad3a0d95fbf21d468a1b33f8c160d8f517"
      , HexStringInCBOR
"3dccc57be49991e95b112954217e8b4fe884d4d26843dfec794feb370981407b79151d1e5af85aba21721876896957adb2b35bcbb84986dcf82daa520a87a9f9" -- wrong verify message but right signature
      )
    ,
      ( ByteString
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
      , HexStringInCBOR
"0325d1dff95105f5253c4022f628a996ad3a0d95fbf21d468a1b33f8c160d8f517"
      , HexStringInCBOR
"5ef63d477c5d1572550016ccf72a2310c7368beeb843c85b1b5697290872222a09e7519702cb2c9a65bbce92d273080a0193b77588bc2eac6dbcbfc15c6dfefd" -- right verify message but wrong signature
      )
    ]

schnorrMismatchMessageAndSignature ::
  [(ByteString, VerKeyDSIGN SchnorrSecp256k1DSIGN, SigDSIGN SchnorrSecp256k1DSIGN)]
schnorrMismatchMessageAndSignature :: [(ByteString, VerKeyDSIGN SchnorrSecp256k1DSIGN,
  SigDSIGN SchnorrSecp256k1DSIGN)]
schnorrMismatchMessageAndSignature =
  forall a b. (a -> b) -> [a] -> [b]
map
    (\(ByteString
vm, HexStringInCBOR
vKey, HexStringInCBOR
sig) -> (ByteString
vm, forall d.
FromCBOR (VerKeyDSIGN d) =>
HexStringInCBOR -> VerKeyDSIGN d
vKeyParser HexStringInCBOR
vKey, forall d. FromCBOR (SigDSIGN d) => HexStringInCBOR -> SigDSIGN d
sigParser HexStringInCBOR
sig))
    -- verifyMessage, vKey, signature
    [
      ( ByteString
"243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89"
      , HexStringInCBOR
"599de3e582e2a3779208a210dfeae8f330b9af00a47a7fb22e9bb8ef596f301b"
      , HexStringInCBOR
"5a56da88e6fd8419181dec4d3dd6997bab953d2fc71ab65e23cfc9e7e3d1a310613454a60f6703819a39fdac2a410a094442afd1fc083354443e8d8bb4461a9b" -- wrong verify message but right signature
      )
    ,
      ( ByteString
"0000000000000000000000000000000000000000000000000000000000000000"
      , HexStringInCBOR
"599de3e582e2a3779208a210dfeae8f330b9af00a47a7fb22e9bb8ef596f301b"
      , HexStringInCBOR
"18a66fb829009a9df6312e1d7f4b53af0ac8a6aa17c2b7ff5941b57a27b24c23531f01bd11135dd844318f814241ea41040cc68958a6c47da489a32f0e22b805" -- right verify message but wrong signature
      )
    ]