module Crypto.Curve.Secp256k1.MuSig2.Internal.Point (
isInfinity,
isValidPoint,
cbytes,
cbytesExt,
hasEvenYBytes,
infinityBytes,
gContext,
mulPublic,
mulGPublic,
mulGSecret,
sumPoints,
) where
import Crypto.Curve.Secp256k1 (Context, Projective, add, mul_vartime, mul_wnaf, precompute, serialize_point, valid, _CURVE_ZERO)
import Crypto.Curve.Secp256k1.MuSig2.Internal.Scalar (SecScalar, modQ, secScalarWider)
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Unsafe as BU
import Data.Maybe (fromMaybe)
isInfinity :: Projective -> Bool
isInfinity :: Projective -> Bool
isInfinity Projective
p = Projective
p Projective -> Projective -> Bool
forall a. Eq a => a -> a -> Bool
== Projective
_CURVE_ZERO
isValidPoint :: Projective -> Bool
isValidPoint :: Projective -> Bool
isValidPoint Projective
p = Bool -> Bool
not (Projective -> Bool
isInfinity Projective
p) Bool -> Bool -> Bool
&& Projective -> Bool
valid Projective
p
cbytes :: Projective -> ByteString
cbytes :: Projective -> ByteString
cbytes = Projective -> ByteString
serialize_point
cbytesExt :: Projective -> ByteString
cbytesExt :: Projective -> ByteString
cbytesExt Projective
p
| Projective -> Bool
isInfinity Projective
p = ByteString
infinityBytes
| Bool
otherwise = Projective -> ByteString
serialize_point Projective
p
infinityBytes :: ByteString
infinityBytes :: ByteString
infinityBytes = Int -> Word8 -> ByteString
BS.replicate Int
33 Word8
0
hasEvenYBytes :: ByteString -> Bool
hasEvenYBytes :: ByteString -> Bool
hasEvenYBytes ByteString
b = Bool -> Bool
not (ByteString -> Bool
BS.null ByteString
b) Bool -> Bool -> Bool
&& ByteString -> Word8
BU.unsafeHead ByteString
b Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0x02
mulPublic :: Projective -> Integer -> Projective
mulPublic :: Projective -> Integer -> Projective
mulPublic Projective
p Integer
k =
Projective -> Maybe Projective -> Projective
forall a. a -> Maybe a -> a
fromMaybe Projective
_CURVE_ZERO (Projective -> Wider -> Maybe Projective
mul_vartime Projective
p (Integer -> Wider
forall a. Num a => Integer -> a
fromInteger (Integer -> Integer
modQ Integer
k)))
gContext :: Context
gContext :: Context
gContext = Context
precompute
{-# NOINLINE gContext #-}
mulGPublic :: Integer -> Projective
mulGPublic :: Integer -> Projective
mulGPublic Integer
k = case Integer -> Integer
modQ Integer
k of
Integer
0 -> Projective
_CURVE_ZERO
Integer
k' -> Projective -> Maybe Projective -> Projective
forall a. a -> Maybe a -> a
fromMaybe Projective
_CURVE_ZERO (Context -> Wider -> Maybe Projective
mul_wnaf Context
gContext (Integer -> Wider
forall a. Num a => Integer -> a
fromInteger Integer
k'))
mulGSecret :: SecScalar -> Maybe Projective
mulGSecret :: SecScalar -> Maybe Projective
mulGSecret SecScalar
k = Context -> Wider -> Maybe Projective
mul_wnaf Context
gContext (SecScalar -> Wider
secScalarWider SecScalar
k)
sumPoints :: (Foldable f) => f Projective -> Projective
sumPoints :: forall (f :: * -> *). Foldable f => f Projective -> Projective
sumPoints = (Projective -> Projective -> Projective)
-> Projective -> f Projective -> Projective
forall b a. (b -> a -> b) -> b -> f a -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Projective -> Projective -> Projective
add Projective
_CURVE_ZERO