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

Helpers on @ppad-secp256k1@ points: the BIP-327 encodings and scalar
multiplication with the right timing properties.

Not part of the public API.
-}
module Crypto.Curve.Secp256k1.MuSig2.Internal.Point (
  -- * Predicates
  isInfinity,
  isValidPoint,

  -- * Encodings
  cbytes,
  cbytesExt,
  hasEvenYBytes,
  infinityBytes,

  -- * Multiplication
  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)

-- | Whether a point is the point at infinity.
isInfinity :: Projective -> Bool
isInfinity :: Projective -> Bool
isInfinity Projective
p = Projective
p Projective -> Projective -> Bool
forall a. Eq a => a -> a -> Bool
== Projective
_CURVE_ZERO

-- | A finite point on the curve (ppad's 'Projective' constructor is exported, so values may be anything).
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@: 33-byte compressed encoding of a finite point.

Precondition: the point is finite ('serialize_point' encodes infinity as
@02 || 0^32@).
-}
cbytes :: Projective -> ByteString
cbytes :: Projective -> ByteString
cbytes = Projective -> ByteString
serialize_point

-- | @cbytes_ext@: like 'cbytes', but infinity is 33 zero bytes.
cbytesExt :: Projective -> ByteString
cbytesExt :: Projective -> ByteString
cbytesExt Projective
p
  | Projective -> Bool
isInfinity Projective
p = ByteString
infinityBytes
  | Bool
otherwise = Projective -> ByteString
serialize_point Projective
p

-- | The @cbytes_ext@ encoding of infinity: 33 zero bytes.
infinityBytes :: ByteString
infinityBytes :: ByteString
infinityBytes = Int -> Word8 -> ByteString
BS.replicate Int
33 Word8
0

-- | Whether a 33-byte compressed encoding has the even-y prefix @0x02@.
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

{- | Variable-time multiplication by a /public/ scalar, reduced modulo \(n\);
\(0\) gives infinity.
-}
mulPublic :: Projective -> Integer -> Projective
mulPublic :: Projective -> Integer -> Projective
mulPublic Projective
p Integer
k =
  -- mul_vartime only fails for scalars >= n, which modQ rules out.
  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)))

-- | Precomputed multiples of the generator, built once on first use.
gContext :: Context
gContext :: Context
gContext = Context
precompute
{-# NOINLINE gContext #-}

{- | Multiplication of the generator by a public scalar, reduced modulo
\(n\); \(0\) gives infinity.
-}
mulGPublic :: Integer -> Projective
mulGPublic :: Integer -> Projective
mulGPublic Integer
k = case Integer -> Integer
modQ Integer
k of
  Integer
0 -> Projective
_CURVE_ZERO
  -- mul_wnaf only fails for scalars 0 or >= n, which are ruled out.
  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'))

{- | Constant-time multiplication of the generator by a /secret/ scalar
(ppad's timing-safe wNAF over precomputed multiples); 'Nothing' for zero.
The scalar goes to ppad as a t'Data.Word.Wider.Wider' word, never through
'Integer'.
-}
mulGSecret :: SecScalar -> Maybe Projective
mulGSecret :: SecScalar -> Maybe Projective
mulGSecret SecScalar
k = Context -> Wider -> Maybe Projective
mul_wnaf Context
gContext (SecScalar -> Wider
secScalarWider SecScalar
k)

-- | Strict sum of points.
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