{-# LANGUAGE MultiWayIf #-}

{- |
Module: Crypto.Curve.Secp256k1.MuSig2.Internal.Types
Copyright: (c) 2025 Jose Storopoli
License: MIT
Maintainer: Jose Storopoli <jose@storopoli.com>

Keys, nonces, signatures and tweaks, with their BIP-327 byte encodings.

The constructors exported here bypass validation; only this library and its
tests may use them. Every value must satisfy the invariant documented on its
type.

Not part of the public API.
-}
module Crypto.Curve.Secp256k1.MuSig2.Internal.Types (
  -- * Encodings
  Encode (..),
  hexString,

  -- * Public keys
  PubKey (..),
  mkPubKey,
  pubKeyFromPoint,
  pubKeyPoint,
  pubKeyXOnly,
  pubKeyHasEvenY,
  XOnlyPubKey (..),

  -- * Secret keys
  SecKey (..),
  secKeyFromBytes,
  secKeyToBytes,
  secKeyPubKey,

  -- * Tweaks
  Tweak (..),
  tweakFromBytes,

  -- * Nonces
  PubNonce (..),
  mkPubNonce,
  AggNonce (..),
  mkAggNonce,

  -- * Signatures
  PartialSignature (..),
  Signature (..),

  -- * Decoding helpers
  decodeCPoint,
  decodeCPointExt,
  decodeScalar,
  expectLength,
) where

import Control.DeepSeq (NFData (..))
import Crypto.Curve.Secp256k1 (Projective, neg, parse_point, _CURVE_ZERO)
import Crypto.Curve.Secp256k1.MuSig2.Internal.Error (DecodeError (..))
import Crypto.Curve.Secp256k1.MuSig2.Internal.Point (cbytes, cbytesExt, hasEvenYBytes, infinityBytes, isInfinity, isValidPoint, mulGSecret)
import Crypto.Curve.Secp256k1.MuSig2.Internal.Scalar (SecScalar, bytesToInteger, curveOrder, fieldSize, integerToBytes32, secScalarFromBytes, secScalarToBytes)
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import Data.Word (Word8)

-- Encodings ------------------------------------------------------------------

{- | Types with a canonical BIP-327 byte encoding.

@'decode' . 'encode' = 'Right'@, and 'decode' accepts exactly the canonical
encodings: it rejects wrong lengths, invalid points and out-of-range scalars.
Secrets have no instance.
-}
class Encode a where
  -- | The canonical encoding.
  encode :: a -> ByteString

  -- | Parses and validates an encoding.
  decode :: ByteString -> Either DecodeError a

-- | Lower-case hexadecimal.
hexString :: ByteString -> String
hexString :: ByteString -> String
hexString = (Word8 -> String -> String) -> String -> ByteString -> String
forall a. (Word8 -> a -> a) -> a -> ByteString -> a
BS.foldr (\Word8
w String
acc -> Word8 -> Char
nibble (Word8
w Word8 -> Word8 -> Word8
forall a. Integral a => a -> a -> a
`div` Word8
16) Char -> String -> String
forall a. a -> [a] -> [a]
: Word8 -> Char
nibble (Word8
w Word8 -> Word8 -> Word8
forall a. Integral a => a -> a -> a
`mod` Word8
16) Char -> String -> String
forall a. a -> [a] -> [a]
: String
acc) []
 where
  nibble :: Word8 -> Char
  nibble :: Word8 -> Char
nibble Word8
n = String
"0123456789abcdef" String -> Int -> Char
forall a. HasCallStack => [a] -> Int -> a
!! Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
n

-- | Shows a value as its constructor name applied to its hex encoding.
showsHex :: String -> ByteString -> Int -> ShowS
showsHex :: String -> ByteString -> Int -> String -> String
showsHex String
name ByteString
bytes Int
d = Bool -> (String -> String) -> String -> String
showParen (Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
10) (String -> String -> String
showString String
name (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String -> String
showString String
" " (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String -> String
forall a. Show a => a -> String -> String
shows (ByteString -> String
hexString ByteString
bytes))

-- | Fails unless the input has the given length.
expectLength :: Int -> ByteString -> Either DecodeError ()
expectLength :: Int -> ByteString -> Either DecodeError ()
expectLength Int
n ByteString
b
  | ByteString -> Int
BS.length ByteString
b Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
n = () -> Either DecodeError ()
forall a b. b -> Either a b
Right ()
  | Bool
otherwise = DecodeError -> Either DecodeError ()
forall a b. a -> Either a b
Left (Int -> Int -> DecodeError
InvalidLength Int
n (ByteString -> Int
BS.length ByteString
b))

{- | @cpoint@: exactly 33 bytes, prefix @0x02@ or @0x03@, \(x < p\) on the
curve. Rejects 32-byte (x-only) and 65-byte (uncompressed) forms, which
'parse_point' would accept.
-}
decodeCPoint :: ByteString -> Either DecodeError Projective
decodeCPoint :: ByteString -> Either DecodeError Projective
decodeCPoint ByteString
b = do
  Int -> ByteString -> Either DecodeError ()
expectLength Int
33 ByteString
b
  if
    | ByteString
b ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
infinityBytes -> DecodeError -> Either DecodeError Projective
forall a b. a -> Either a b
Left DecodeError
PointAtInfinity
    | HasCallStack => ByteString -> Int -> Word8
ByteString -> Int -> Word8
BS.index ByteString
b Int
0 Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word8
0x02 Bool -> Bool -> Bool
&& HasCallStack => ByteString -> Int -> Word8
ByteString -> Int -> Word8
BS.index ByteString
b Int
0 Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word8
0x03 -> DecodeError -> Either DecodeError Projective
forall a b. a -> Either a b
Left DecodeError
InvalidPoint
    | Bool
otherwise -> Either DecodeError Projective
-> (Projective -> Either DecodeError Projective)
-> Maybe Projective
-> Either DecodeError Projective
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (DecodeError -> Either DecodeError Projective
forall a b. a -> Either a b
Left DecodeError
InvalidPoint) Projective -> Either DecodeError Projective
forall a b. b -> Either a b
Right (ByteString -> Maybe Projective
parse_point ByteString
b)

-- | @cpoint_ext@: like 'decodeCPoint', but 33 zero bytes is the point at infinity.
decodeCPointExt :: ByteString -> Either DecodeError Projective
decodeCPointExt :: ByteString -> Either DecodeError Projective
decodeCPointExt ByteString
b
  | ByteString
b ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
infinityBytes = Projective -> Either DecodeError Projective
forall a b. b -> Either a b
Right Projective
_CURVE_ZERO
  | Bool
otherwise = ByteString -> Either DecodeError Projective
decodeCPoint ByteString
b

-- | A 32-byte big-endian scalar below \(n\) (zero allowed).
decodeScalar :: ByteString -> Either DecodeError Integer
decodeScalar :: ByteString -> Either DecodeError Integer
decodeScalar ByteString
b = do
  Int -> ByteString -> Either DecodeError ()
expectLength Int
32 ByteString
b
  let x :: Integer
x = ByteString -> Integer
bytesToInteger ByteString
b
  if Integer
x Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
curveOrder then DecodeError -> Either DecodeError Integer
forall a b. a -> Either a b
Left DecodeError
ScalarOutOfRange else Integer -> Either DecodeError Integer
forall a b. b -> Either a b
Right Integer
x

-- Public keys ----------------------------------------------------------------

{- | A public key: a finite point on the curve.

Caches its 33-byte compressed encoding; 'Eq' and 'Ord' compare that encoding
(the order is the one BIP-327 @KeySort@ uses), and 'Show' prints it in hex.
-}
data PubKey = PubKey
  { PubKey -> Projective
pubKeyPt :: !Projective
  -- ^ The point (finite, on the curve).
  , PubKey -> ByteString
pubKeyBytes :: !ByteString
  -- ^ Its 33-byte compressed encoding.
  }

-- | The public key as a @ppad-secp256k1@ point.
pubKeyPoint :: PubKey -> Projective
pubKeyPoint :: PubKey -> Projective
pubKeyPoint = PubKey -> Projective
pubKeyPt

instance Eq PubKey where
  PubKey
a == :: PubKey -> PubKey -> Bool
== PubKey
b = PubKey -> ByteString
pubKeyBytes PubKey
a ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== PubKey -> ByteString
pubKeyBytes PubKey
b

instance Ord PubKey where
  compare :: PubKey -> PubKey -> Ordering
compare PubKey
a PubKey
b = ByteString -> ByteString -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (PubKey -> ByteString
pubKeyBytes PubKey
a) (PubKey -> ByteString
pubKeyBytes PubKey
b)

instance Show PubKey where
  showsPrec :: Int -> PubKey -> String -> String
showsPrec Int
d PubKey
x = String -> ByteString -> Int -> String -> String
showsHex String
"PubKey" (PubKey -> ByteString
pubKeyBytes PubKey
x) Int
d

instance NFData PubKey where
  rnf :: PubKey -> ()
rnf (PubKey Projective
p ByteString
b) = Projective
p Projective -> () -> ()
forall a b. a -> b -> b
`seq` ByteString -> ()
forall a. NFData a => a -> ()
rnf ByteString
b

instance Encode PubKey where
  encode :: PubKey -> ByteString
encode = PubKey -> ByteString
pubKeyBytes
  decode :: ByteString -> Either DecodeError PubKey
decode ByteString
b = (Projective -> ByteString -> PubKey
`PubKey` ByteString
b) (Projective -> PubKey)
-> Either DecodeError Projective -> Either DecodeError PubKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError Projective
decodeCPoint ByteString
b

-- | A t'PubKey' from a point already known to be finite and on the curve.
mkPubKey :: Projective -> PubKey
mkPubKey :: Projective -> PubKey
mkPubKey Projective
p = Projective -> ByteString -> PubKey
PubKey Projective
p (Projective -> ByteString
cbytes Projective
p)

-- | A t'PubKey' from any point: rejects infinity and points not on the curve.
pubKeyFromPoint :: Projective -> Either DecodeError PubKey
pubKeyFromPoint :: Projective -> Either DecodeError PubKey
pubKeyFromPoint Projective
p
  | Projective -> Bool
isInfinity Projective
p = DecodeError -> Either DecodeError PubKey
forall a b. a -> Either a b
Left DecodeError
PointAtInfinity
  | Bool -> Bool
not (Projective -> Bool
isValidPoint Projective
p) = DecodeError -> Either DecodeError PubKey
forall a b. a -> Either a b
Left DecodeError
InvalidPoint
  | Bool
otherwise = PubKey -> Either DecodeError PubKey
forall a b. b -> Either a b
Right (Projective -> PubKey
mkPubKey Projective
p)

-- | Whether the point has an even y-coordinate.
pubKeyHasEvenY :: PubKey -> Bool
pubKeyHasEvenY :: PubKey -> Bool
pubKeyHasEvenY = ByteString -> Bool
hasEvenYBytes (ByteString -> Bool) -> (PubKey -> ByteString) -> PubKey -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PubKey -> ByteString
pubKeyBytes

{- | A BIP-340 x-only public key: the finite point with the given
x-coordinate and an even y-coordinate.

'Eq', 'Ord' and 'Show' use the 32-byte encoding.
-}
data XOnlyPubKey = XOnlyPubKey
  { XOnlyPubKey -> Projective
xOnlyPoint :: !Projective
  -- ^ The point with even y.
  , XOnlyPubKey -> ByteString
xOnlyBytes :: !ByteString
  -- ^ Its 32-byte x-coordinate.
  }

instance Eq XOnlyPubKey where
  XOnlyPubKey
a == :: XOnlyPubKey -> XOnlyPubKey -> Bool
== XOnlyPubKey
b = XOnlyPubKey -> ByteString
xOnlyBytes XOnlyPubKey
a ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== XOnlyPubKey -> ByteString
xOnlyBytes XOnlyPubKey
b

instance Ord XOnlyPubKey where
  compare :: XOnlyPubKey -> XOnlyPubKey -> Ordering
compare XOnlyPubKey
a XOnlyPubKey
b = ByteString -> ByteString -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (XOnlyPubKey -> ByteString
xOnlyBytes XOnlyPubKey
a) (XOnlyPubKey -> ByteString
xOnlyBytes XOnlyPubKey
b)

instance Show XOnlyPubKey where
  showsPrec :: Int -> XOnlyPubKey -> String -> String
showsPrec Int
d XOnlyPubKey
x = String -> ByteString -> Int -> String -> String
showsHex String
"XOnlyPubKey" (XOnlyPubKey -> ByteString
xOnlyBytes XOnlyPubKey
x) Int
d

instance NFData XOnlyPubKey where
  rnf :: XOnlyPubKey -> ()
rnf (XOnlyPubKey Projective
p ByteString
b) = Projective
p Projective -> () -> ()
forall a b. a -> b -> b
`seq` ByteString -> ()
forall a. NFData a => a -> ()
rnf ByteString
b

instance Encode XOnlyPubKey where
  encode :: XOnlyPubKey -> ByteString
encode = XOnlyPubKey -> ByteString
xOnlyBytes
  decode :: ByteString -> Either DecodeError XOnlyPubKey
decode ByteString
b = do
    Int -> ByteString -> Either DecodeError ()
expectLength Int
32 ByteString
b
    -- 'parse_point' lifts 32 bytes as a BIP-340 key: x < p, on the curve, even y.
    p <- Either DecodeError Projective
-> (Projective -> Either DecodeError Projective)
-> Maybe Projective
-> Either DecodeError Projective
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (DecodeError -> Either DecodeError Projective
forall a b. a -> Either a b
Left DecodeError
InvalidPoint) Projective -> Either DecodeError Projective
forall a b. b -> Either a b
Right (ByteString -> Maybe Projective
parse_point ByteString
b)
    Right (XOnlyPubKey p b)

-- | The x-only key of a public key (negated to even y if needed).
pubKeyXOnly :: PubKey -> XOnlyPubKey
pubKeyXOnly :: PubKey -> XOnlyPubKey
pubKeyXOnly PubKey
pk =
  Projective -> ByteString -> XOnlyPubKey
XOnlyPubKey
    (if PubKey -> Bool
pubKeyHasEvenY PubKey
pk then PubKey -> Projective
pubKeyPoint PubKey
pk else Projective -> Projective
neg (PubKey -> Projective
pubKeyPoint PubKey
pk))
    (Int -> ByteString -> ByteString
BS.drop Int
1 (PubKey -> ByteString
pubKeyBytes PubKey
pk))

-- Secret keys ----------------------------------------------------------------

{- | A secret key \(0 < d < n\), with its public key.

The scalar is held as four 64-bit words in Montgomery form, never in an
'Integer': it is parsed, encoded and computed with in constant time.

Deliberately has no 'Show', 'Eq', 'Ord', 'Read', 'Num' or 'Encode' instance.
-}
data SecKey = SecKey
  { SecKey -> SecScalar
secKeyScalar :: !SecScalar
  -- ^ \(d\).
  , SecKey -> PubKey
secKeyPub :: !PubKey
  -- ^ \(d \cdot G\).
  }

-- | The public key \(d \cdot G\) of a secret key.
secKeyPubKey :: SecKey -> PubKey
secKeyPubKey :: SecKey -> PubKey
secKeyPubKey = SecKey -> PubKey
secKeyPub

instance NFData SecKey where
  rnf :: SecKey -> ()
rnf (SecKey SecScalar
d PubKey
p) = SecScalar -> ()
forall a. NFData a => a -> ()
rnf SecScalar
d () -> () -> ()
forall a b. a -> b -> b
`seq` PubKey -> ()
forall a. NFData a => a -> ()
rnf PubKey
p

{- | A secret key from 32 big-endian bytes in \([1, n)\). The range check
runs in constant time; only its outcome (valid, 'ScalarZero' or
'ScalarOutOfRange') is revealed.
-}
secKeyFromBytes :: ByteString -> Either DecodeError SecKey
secKeyFromBytes :: ByteString -> Either DecodeError SecKey
secKeyFromBytes ByteString
b = do
  d <- ByteString -> Either DecodeError SecScalar
secScalarFromBytes ByteString
b
  case mulGSecret d of
    Maybe Projective
Nothing -> DecodeError -> Either DecodeError SecKey
forall a b. a -> Either a b
Left DecodeError
ScalarZero
    Just Projective
p -> SecKey -> Either DecodeError SecKey
forall a b. b -> Either a b
Right (SecScalar -> PubKey -> SecKey
SecKey SecScalar
d (Projective -> PubKey
mkPubKey Projective
p))

-- | The 32-byte big-endian encoding of a secret key, in constant time.
secKeyToBytes :: SecKey -> ByteString
secKeyToBytes :: SecKey -> ByteString
secKeyToBytes = SecScalar -> ByteString
secScalarToBytes (SecScalar -> ByteString)
-> (SecKey -> SecScalar) -> SecKey -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SecKey -> SecScalar
secKeyScalar

-- Tweaks -------------------------------------------------------------------------

{- | A tweak \(0 \le t < n\) (zero allowed, as in BIP-327).

'Eq', 'Ord' and 'Show' use the 32-byte encoding.
-}
newtype Tweak = Tweak
  { Tweak -> Integer
tweakScalar :: Integer
  -- ^ \(t\).
  }
  deriving (Tweak -> Tweak -> Bool
(Tweak -> Tweak -> Bool) -> (Tweak -> Tweak -> Bool) -> Eq Tweak
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Tweak -> Tweak -> Bool
== :: Tweak -> Tweak -> Bool
$c/= :: Tweak -> Tweak -> Bool
/= :: Tweak -> Tweak -> Bool
Eq, Eq Tweak
Eq Tweak =>
(Tweak -> Tweak -> Ordering)
-> (Tweak -> Tweak -> Bool)
-> (Tweak -> Tweak -> Bool)
-> (Tweak -> Tweak -> Bool)
-> (Tweak -> Tweak -> Bool)
-> (Tweak -> Tweak -> Tweak)
-> (Tweak -> Tweak -> Tweak)
-> Ord Tweak
Tweak -> Tweak -> Bool
Tweak -> Tweak -> Ordering
Tweak -> Tweak -> Tweak
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 :: Tweak -> Tweak -> Ordering
compare :: Tweak -> Tweak -> Ordering
$c< :: Tweak -> Tweak -> Bool
< :: Tweak -> Tweak -> Bool
$c<= :: Tweak -> Tweak -> Bool
<= :: Tweak -> Tweak -> Bool
$c> :: Tweak -> Tweak -> Bool
> :: Tweak -> Tweak -> Bool
$c>= :: Tweak -> Tweak -> Bool
>= :: Tweak -> Tweak -> Bool
$cmax :: Tweak -> Tweak -> Tweak
max :: Tweak -> Tweak -> Tweak
$cmin :: Tweak -> Tweak -> Tweak
min :: Tweak -> Tweak -> Tweak
Ord)

instance Show Tweak where
  showsPrec :: Int -> Tweak -> String -> String
showsPrec Int
d Tweak
x = String -> ByteString -> Int -> String -> String
showsHex String
"Tweak" (Tweak -> ByteString
forall a. Encode a => a -> ByteString
encode Tweak
x) Int
d

instance NFData Tweak where
  rnf :: Tweak -> ()
rnf (Tweak Integer
t) = Integer -> ()
forall a. NFData a => a -> ()
rnf Integer
t

instance Encode Tweak where
  encode :: Tweak -> ByteString
encode = Integer -> ByteString
integerToBytes32 (Integer -> ByteString)
-> (Tweak -> Integer) -> Tweak -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tweak -> Integer
tweakScalar
  decode :: ByteString -> Either DecodeError Tweak
decode ByteString
b = Integer -> Tweak
Tweak (Integer -> Tweak)
-> Either DecodeError Integer -> Either DecodeError Tweak
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError Integer
decodeScalar ByteString
b

-- | 'decode' for t'Tweak'.
tweakFromBytes :: ByteString -> Either DecodeError Tweak
tweakFromBytes :: ByteString -> Either DecodeError Tweak
tweakFromBytes = ByteString -> Either DecodeError Tweak
forall a. Encode a => ByteString -> Either DecodeError a
decode

-- Nonces ---------------------------------------------------------------------

{- | A signer's public nonce \((R_1, R_2)\): two finite points.

'Eq', 'Ord' and 'Show' use the 66-byte encoding.
-}
data PubNonce = PubNonce
  { PubNonce -> Projective
pubNonceR1 :: !Projective
  -- ^ \(R_1\).
  , PubNonce -> Projective
pubNonceR2 :: !Projective
  -- ^ \(R_2\).
  , PubNonce -> ByteString
pubNonceBytes :: !ByteString
  -- ^ @cbytes(R1) || cbytes(R2)@.
  }

instance Eq PubNonce where
  PubNonce
a == :: PubNonce -> PubNonce -> Bool
== PubNonce
b = PubNonce -> ByteString
pubNonceBytes PubNonce
a ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== PubNonce -> ByteString
pubNonceBytes PubNonce
b

instance Ord PubNonce where
  compare :: PubNonce -> PubNonce -> Ordering
compare PubNonce
a PubNonce
b = ByteString -> ByteString -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (PubNonce -> ByteString
pubNonceBytes PubNonce
a) (PubNonce -> ByteString
pubNonceBytes PubNonce
b)

instance Show PubNonce where
  showsPrec :: Int -> PubNonce -> String -> String
showsPrec Int
d PubNonce
x = String -> ByteString -> Int -> String -> String
showsHex String
"PubNonce" (PubNonce -> ByteString
pubNonceBytes PubNonce
x) Int
d

instance NFData PubNonce where
  rnf :: PubNonce -> ()
rnf (PubNonce Projective
r1 Projective
r2 ByteString
b) = Projective
r1 Projective -> () -> ()
forall a b. a -> b -> b
`seq` Projective
r2 Projective -> () -> ()
forall a b. a -> b -> b
`seq` ByteString -> ()
forall a. NFData a => a -> ()
rnf ByteString
b

instance Encode PubNonce where
  encode :: PubNonce -> ByteString
encode = PubNonce -> ByteString
pubNonceBytes
  decode :: ByteString -> Either DecodeError PubNonce
decode ByteString
b = do
    Int -> ByteString -> Either DecodeError ()
expectLength Int
66 ByteString
b
    r1 <- ByteString -> Either DecodeError Projective
decodeCPoint (Int -> ByteString -> ByteString
BS.take Int
33 ByteString
b)
    r2 <- decodeCPoint (BS.drop 33 b)
    Right (PubNonce r1 r2 b)

-- | A t'PubNonce' from two points already known to be finite and on the curve.
mkPubNonce :: Projective -> Projective -> PubNonce
mkPubNonce :: Projective -> Projective -> PubNonce
mkPubNonce Projective
r1 Projective
r2 = Projective -> Projective -> ByteString -> PubNonce
PubNonce Projective
r1 Projective
r2 (Projective -> ByteString
cbytes Projective
r1 ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Projective -> ByteString
cbytes Projective
r2)

{- | An aggregate nonce \((R_1, R_2)\); either point may be infinity.

'Eq', 'Ord' and 'Show' use the 66-byte encoding (infinity as 33 zero bytes).
-}
data AggNonce = AggNonce
  { AggNonce -> Projective
aggNonceR1 :: !Projective
  -- ^ \(R_1\).
  , AggNonce -> Projective
aggNonceR2 :: !Projective
  -- ^ \(R_2\).
  , AggNonce -> ByteString
aggNonceBytes :: !ByteString
  -- ^ @cbytes_ext(R1) || cbytes_ext(R2)@.
  }

instance Eq AggNonce where
  AggNonce
a == :: AggNonce -> AggNonce -> Bool
== AggNonce
b = AggNonce -> ByteString
aggNonceBytes AggNonce
a ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== AggNonce -> ByteString
aggNonceBytes AggNonce
b

instance Ord AggNonce where
  compare :: AggNonce -> AggNonce -> Ordering
compare AggNonce
a AggNonce
b = ByteString -> ByteString -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (AggNonce -> ByteString
aggNonceBytes AggNonce
a) (AggNonce -> ByteString
aggNonceBytes AggNonce
b)

instance Show AggNonce where
  showsPrec :: Int -> AggNonce -> String -> String
showsPrec Int
d AggNonce
x = String -> ByteString -> Int -> String -> String
showsHex String
"AggNonce" (AggNonce -> ByteString
aggNonceBytes AggNonce
x) Int
d

instance NFData AggNonce where
  rnf :: AggNonce -> ()
rnf (AggNonce Projective
r1 Projective
r2 ByteString
b) = Projective
r1 Projective -> () -> ()
forall a b. a -> b -> b
`seq` Projective
r2 Projective -> () -> ()
forall a b. a -> b -> b
`seq` ByteString -> ()
forall a. NFData a => a -> ()
rnf ByteString
b

instance Encode AggNonce where
  encode :: AggNonce -> ByteString
encode = AggNonce -> ByteString
aggNonceBytes
  decode :: ByteString -> Either DecodeError AggNonce
decode ByteString
b = do
    Int -> ByteString -> Either DecodeError ()
expectLength Int
66 ByteString
b
    r1 <- ByteString -> Either DecodeError Projective
decodeCPointExt (Int -> ByteString -> ByteString
BS.take Int
33 ByteString
b)
    r2 <- decodeCPointExt (BS.drop 33 b)
    Right (AggNonce r1 r2 b)

-- | An t'AggNonce' from two points on the curve (or infinity).
mkAggNonce :: Projective -> Projective -> AggNonce
mkAggNonce :: Projective -> Projective -> AggNonce
mkAggNonce Projective
r1 Projective
r2 = Projective -> Projective -> ByteString -> AggNonce
AggNonce Projective
r1 Projective
r2 (Projective -> ByteString
cbytesExt Projective
r1 ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Projective -> ByteString
cbytesExt Projective
r2)

-- Signatures -----------------------------------------------------------------

{- | A partial signature \(0 \le s < n\).

'Eq', 'Ord' and 'Show' use the 32-byte encoding.
-}
newtype PartialSignature = PartialSignature
  { PartialSignature -> Integer
partialSigScalar :: Integer
  -- ^ \(s\).
  }
  deriving (PartialSignature -> PartialSignature -> Bool
(PartialSignature -> PartialSignature -> Bool)
-> (PartialSignature -> PartialSignature -> Bool)
-> Eq PartialSignature
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PartialSignature -> PartialSignature -> Bool
== :: PartialSignature -> PartialSignature -> Bool
$c/= :: PartialSignature -> PartialSignature -> Bool
/= :: PartialSignature -> PartialSignature -> Bool
Eq, Eq PartialSignature
Eq PartialSignature =>
(PartialSignature -> PartialSignature -> Ordering)
-> (PartialSignature -> PartialSignature -> Bool)
-> (PartialSignature -> PartialSignature -> Bool)
-> (PartialSignature -> PartialSignature -> Bool)
-> (PartialSignature -> PartialSignature -> Bool)
-> (PartialSignature -> PartialSignature -> PartialSignature)
-> (PartialSignature -> PartialSignature -> PartialSignature)
-> Ord PartialSignature
PartialSignature -> PartialSignature -> Bool
PartialSignature -> PartialSignature -> Ordering
PartialSignature -> PartialSignature -> PartialSignature
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 :: PartialSignature -> PartialSignature -> Ordering
compare :: PartialSignature -> PartialSignature -> Ordering
$c< :: PartialSignature -> PartialSignature -> Bool
< :: PartialSignature -> PartialSignature -> Bool
$c<= :: PartialSignature -> PartialSignature -> Bool
<= :: PartialSignature -> PartialSignature -> Bool
$c> :: PartialSignature -> PartialSignature -> Bool
> :: PartialSignature -> PartialSignature -> Bool
$c>= :: PartialSignature -> PartialSignature -> Bool
>= :: PartialSignature -> PartialSignature -> Bool
$cmax :: PartialSignature -> PartialSignature -> PartialSignature
max :: PartialSignature -> PartialSignature -> PartialSignature
$cmin :: PartialSignature -> PartialSignature -> PartialSignature
min :: PartialSignature -> PartialSignature -> PartialSignature
Ord)

instance Show PartialSignature where
  showsPrec :: Int -> PartialSignature -> String -> String
showsPrec Int
d PartialSignature
x = String -> ByteString -> Int -> String -> String
showsHex String
"PartialSignature" (PartialSignature -> ByteString
forall a. Encode a => a -> ByteString
encode PartialSignature
x) Int
d

instance NFData PartialSignature where
  rnf :: PartialSignature -> ()
rnf (PartialSignature Integer
s) = Integer -> ()
forall a. NFData a => a -> ()
rnf Integer
s

instance Encode PartialSignature where
  encode :: PartialSignature -> ByteString
encode = Integer -> ByteString
integerToBytes32 (Integer -> ByteString)
-> (PartialSignature -> Integer) -> PartialSignature -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PartialSignature -> Integer
partialSigScalar
  decode :: ByteString -> Either DecodeError PartialSignature
decode ByteString
b = Integer -> PartialSignature
PartialSignature (Integer -> PartialSignature)
-> Either DecodeError Integer
-> Either DecodeError PartialSignature
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError Integer
decodeScalar ByteString
b

{- | A 64-byte BIP-340 Schnorr signature @R.x || s@ with \(R.x < p\) and
\(s < n\).

'Eq', 'Ord' and 'Show' use the encoding.
-}
newtype Signature = Signature
  { Signature -> ByteString
signatureBytes :: ByteString
  -- ^ The 64 bytes.
  }
  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)

instance Show Signature where
  showsPrec :: Int -> Signature -> String -> String
showsPrec Int
d Signature
x = String -> ByteString -> Int -> String -> String
showsHex String
"Signature" (Signature -> ByteString
signatureBytes Signature
x) Int
d

instance NFData Signature where
  rnf :: Signature -> ()
rnf (Signature ByteString
b) = ByteString -> ()
forall a. NFData a => a -> ()
rnf ByteString
b

instance Encode Signature where
  encode :: Signature -> ByteString
encode = Signature -> ByteString
signatureBytes
  decode :: ByteString -> Either DecodeError Signature
decode ByteString
b = do
    Int -> ByteString -> Either DecodeError ()
expectLength Int
64 ByteString
b
    if ByteString -> Integer
bytesToInteger (Int -> ByteString -> ByteString
BS.take Int
32 ByteString
b) Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
fieldSize
      then DecodeError -> Either DecodeError Signature
forall a b. a -> Either a b
Left DecodeError
InvalidPoint
      else ByteString -> Signature
Signature ByteString
b Signature
-> Either DecodeError Integer -> Either DecodeError Signature
forall a b. a -> Either DecodeError b -> Either DecodeError a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ByteString -> Either DecodeError Integer
decodeScalar (Int -> ByteString -> ByteString
BS.drop Int
32 ByteString
b)