-- Copyright 2026 Biglup Labs
-- SPDX-License-Identifier: Apache-2.0
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     https://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

-- | Ed25519 signatures as immutable 64-byte values. Import checks encoded
-- length, not whether a signature verifies against a particular key/message.
--
-- @
-- import qualified Cometa.Crypto.Ed25519.Signature as Signature
--
-- roundTrip :: Signature.Signature -> Bool
-- roundTrip value = Signature.fromBytes (Signature.toBytes value) == Right value
-- @
module Cometa.Crypto.Ed25519.Signature
  ( Signature,

    -- * Import
    fromBytes,
    fromHex,

    -- * Inspection
    toBytes,
    toHex,
    byteLength,
    hexLength,
  )
where

-- IMPORTS --------------------------------------------------------------------

import Cometa.Error (Error (..))
import qualified Cometa.Internal.Hex as Hex
import Data.ByteString (ByteString)
import qualified Data.ByteString as ByteString
import Data.Text (Text)

-- TYPES ----------------------------------------------------------------------

-- | An immutable 64-byte Ed25519 signature. Equality compares bytes;
-- ordering is lexicographic byte ordering. 'Show' is diagnostic; use 'toHex'
-- for a stable textual representation.
newtype Signature = Signature ByteString
  deriving (Signature -> Signature -> Bool
(Signature -> Signature -> Bool)
-> (Signature -> Signature -> Bool) -> Eq Signature
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Signature -> Signature -> Bool
== :: Signature -> Signature -> Bool
$c/= :: Signature -> Signature -> Bool
/= :: Signature -> Signature -> Bool
Eq, Eq Signature
Eq Signature =>
(Signature -> Signature -> Ordering)
-> (Signature -> Signature -> Bool)
-> (Signature -> Signature -> Bool)
-> (Signature -> Signature -> Bool)
-> (Signature -> Signature -> Bool)
-> (Signature -> Signature -> Signature)
-> (Signature -> Signature -> Signature)
-> Ord Signature
Signature -> Signature -> Bool
Signature -> Signature -> Ordering
Signature -> Signature -> Signature
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Signature -> Signature -> Ordering
compare :: Signature -> Signature -> Ordering
$c< :: Signature -> Signature -> Bool
< :: Signature -> Signature -> Bool
$c<= :: Signature -> Signature -> Bool
<= :: Signature -> Signature -> Bool
$c> :: Signature -> Signature -> Bool
> :: Signature -> Signature -> Bool
$c>= :: Signature -> Signature -> Bool
>= :: Signature -> Signature -> Bool
$cmax :: Signature -> Signature -> Signature
max :: Signature -> Signature -> Signature
$cmin :: Signature -> Signature -> Signature
min :: Signature -> Signature -> Signature
Ord, Int -> Signature -> ShowS
[Signature] -> ShowS
Signature -> String
(Int -> Signature -> ShowS)
-> (Signature -> String)
-> ([Signature] -> ShowS)
-> Show Signature
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Signature -> ShowS
showsPrec :: Int -> Signature -> ShowS
$cshow :: Signature -> String
show :: Signature -> String
$cshowList :: [Signature] -> ShowS
showList :: [Signature] -> ShowS
Show)

-- DEFINITIONS ----------------------------------------------------------------

-- | Import exactly 64 bytes. Other lengths return 'InvalidEd25519SignatureLength'.
-- No cryptographic verification is performed.
fromBytes :: ByteString -> Either Error Signature
fromBytes :: ByteString -> Either Error Signature
fromBytes ByteString
bytes
  | ByteString -> Int
ByteString.length ByteString
bytes Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
64 = Error -> Either Error Signature
forall a b. a -> Either a b
Left (Int -> Error
InvalidEd25519SignatureLength (ByteString -> Int
ByteString.length ByteString
bytes))
  | Bool
otherwise = Signature -> Either Error Signature
forall a b. b -> Either a b
Right (ByteString -> Signature
Signature ByteString
bytes)

-- | Import 128 ASCII hexadecimal digits, accepting either case. No whitespace
-- or prefix is accepted. Empty, odd-length or nonhexadecimal text returns
-- 'InvalidHex'; valid hex of another byte length returns 'InvalidEd25519SignatureLength'.
fromHex :: Text -> Either Error Signature
fromHex :: Text -> Either Error Signature
fromHex Text
text = do
  ByteString
bytes <- Either Error ByteString
-> (ByteString -> Either Error ByteString)
-> Maybe ByteString
-> Either Error ByteString
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Error -> Either Error ByteString
forall a b. a -> Either a b
Left Error
InvalidHex) ByteString -> Either Error ByteString
forall a b. b -> Either a b
Right (Text -> Maybe ByteString
Hex.decodeHex Text
text)
  if ByteString -> Bool
ByteString.null ByteString
bytes then Error -> Either Error Signature
forall a b. a -> Either a b
Left Error
InvalidHex else ByteString -> Either Error Signature
fromBytes ByteString
bytes

-- | Retrieve the exact encoded bytes.
toBytes :: Signature -> ByteString
toBytes :: Signature -> ByteString
toBytes (Signature ByteString
bytes) = ByteString
bytes

-- | Render lowercase ASCII hexadecimal digits without a prefix or whitespace.
toHex :: Signature -> Text
toHex :: Signature -> Text
toHex = ByteString -> Text
Hex.encodeHex (ByteString -> Text)
-> (Signature -> ByteString) -> Signature -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Signature -> ByteString
toBytes

-- | Encoded byte length, always 64.
byteLength :: Signature -> Int
byteLength :: Signature -> Int
byteLength = ByteString -> Int
ByteString.length (ByteString -> Int)
-> (Signature -> ByteString) -> Signature -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Signature -> ByteString
toBytes

-- | Hexadecimal digit count, always 128. No terminator is included.
hexLength :: Signature -> Int
hexLength :: Signature -> Int
hexLength Signature
value = Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Signature -> Int
byteLength Signature
value