{-# LANGUAGE MultiWayIf #-}
module Crypto.Curve.Secp256k1.MuSig2.Internal.Types (
Encode (..),
hexString,
PubKey (..),
mkPubKey,
pubKeyFromPoint,
pubKeyPoint,
pubKeyXOnly,
pubKeyHasEvenY,
XOnlyPubKey (..),
SecKey (..),
secKeyFromBytes,
secKeyToBytes,
secKeyPubKey,
Tweak (..),
tweakFromBytes,
PubNonce (..),
mkPubNonce,
AggNonce (..),
mkAggNonce,
PartialSignature (..),
Signature (..),
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)
class Encode a where
encode :: a -> ByteString
decode :: ByteString -> Either DecodeError a
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
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))
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))
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)
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
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
data PubKey = PubKey
{ PubKey -> Projective
pubKeyPt :: !Projective
, PubKey -> ByteString
pubKeyBytes :: !ByteString
}
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
mkPubKey :: Projective -> PubKey
mkPubKey :: Projective -> PubKey
mkPubKey Projective
p = Projective -> ByteString -> PubKey
PubKey Projective
p (Projective -> ByteString
cbytes Projective
p)
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)
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
data XOnlyPubKey = XOnlyPubKey
{ XOnlyPubKey -> Projective
xOnlyPoint :: !Projective
, XOnlyPubKey -> ByteString
xOnlyBytes :: !ByteString
}
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
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)
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))
data SecKey = SecKey
{ SecKey -> SecScalar
secKeyScalar :: !SecScalar
, SecKey -> PubKey
secKeyPub :: !PubKey
}
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
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))
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
newtype Tweak = Tweak
{ Tweak -> Integer
tweakScalar :: Integer
}
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
tweakFromBytes :: ByteString -> Either DecodeError Tweak
tweakFromBytes :: ByteString -> Either DecodeError Tweak
tweakFromBytes = ByteString -> Either DecodeError Tweak
forall a. Encode a => ByteString -> Either DecodeError a
decode
data PubNonce = PubNonce
{ PubNonce -> Projective
pubNonceR1 :: !Projective
, PubNonce -> Projective
pubNonceR2 :: !Projective
, PubNonce -> ByteString
pubNonceBytes :: !ByteString
}
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)
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)
data AggNonce = AggNonce
{ AggNonce -> Projective
aggNonceR1 :: !Projective
, AggNonce -> Projective
aggNonceR2 :: !Projective
, AggNonce -> ByteString
aggNonceBytes :: !ByteString
}
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)
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)
newtype PartialSignature = PartialSignature
{ PartialSignature -> Integer
partialSigScalar :: Integer
}
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
newtype Signature = Signature
{ Signature -> ByteString
signatureBytes :: 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)
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)