{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}
module Crypto.Curve.Secp256k1.MuSig2.Internal.Scalar (
curveOrder,
fieldSize,
modQ,
bytesToInteger,
integerToBytes32,
hashToScalar,
SecScalar,
secScalarFromBytes,
secScalarFromBytesModQ,
secScalarToBytes,
secScalarWider,
signScalar,
widerFromBytes,
widerToBytes,
) where
import Control.DeepSeq (NFData (..))
import Crypto.Curve.Secp256k1.MuSig2.Internal.Error (DecodeError (..))
import Data.Bits (shiftR, unsafeShiftL, unsafeShiftR, (.|.))
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Internal as BI
import qualified Data.ByteString.Unsafe as BU
import qualified Data.Choice as C
import Data.Word (Word8)
import Data.Word.Limb (Limb (..))
import Data.Word.Wider (Wider (..))
import qualified Data.Word.Wider as W
import Foreign.Ptr (Ptr)
import Foreign.Storable (pokeByteOff)
import GHC.Exts (Word (..))
import qualified Numeric.Montgomery.Secp256k1.Scalar as S
curveOrder :: Integer
curveOrder :: Integer
curveOrder = Integer
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
fieldSize :: Integer
fieldSize :: Integer
fieldSize = Integer
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
modQ :: Integer -> Integer
modQ :: Integer -> Integer
modQ Integer
x = Integer
x Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
curveOrder
bytesToInteger :: ByteString -> Integer
bytesToInteger :: ByteString -> Integer
bytesToInteger = (Integer -> Word8 -> Integer) -> Integer -> ByteString -> Integer
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
BS.foldl' (\Integer
acc Word8
b -> Integer
acc Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
256 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Word8 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
b) Integer
0
integerToBytes32 :: Integer -> ByteString
integerToBytes32 :: Integer -> ByteString
integerToBytes32 Integer
i = (ByteString, Maybe Int) -> ByteString
forall a b. (a, b) -> a
fst (Int
-> (Int -> Maybe (Word8, Int)) -> Int -> (ByteString, Maybe Int)
forall a.
Int -> (a -> Maybe (Word8, a)) -> a -> (ByteString, Maybe a)
BS.unfoldrN Int
32 Int -> Maybe (Word8, Int)
step Int
31)
where
step :: Int -> Maybe (Word8, Int)
step :: Int -> Maybe (Word8, Int)
step Int
j = (Word8, Int) -> Maybe (Word8, Int)
forall a. a -> Maybe a
Just (Integer -> Word8
forall a. Num a => Integer -> a
fromInteger (Integer
i Integer -> Int -> Integer
forall a. Bits a => a -> Int -> a
`shiftR` (Int
8 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
j)), Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
hashToScalar :: ByteString -> Integer
hashToScalar :: ByteString -> Integer
hashToScalar = Integer -> Integer
modQ (Integer -> Integer)
-> (ByteString -> Integer) -> ByteString -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Integer
bytesToInteger
newtype SecScalar = SecScalar S.Montgomery
instance NFData SecScalar where
rnf :: SecScalar -> ()
rnf (SecScalar Montgomery
m) = Montgomery -> ()
forall a. NFData a => a -> ()
rnf Montgomery
m
orderWider :: Wider
orderWider :: Wider
orderWider = Word -> Word -> Word -> Word -> Wider
W.wider Word
0xBFD25E8CD0364141 Word
0xBAAEDCE6AF48A03B Word
0xFFFFFFFFFFFFFFFE Word
0xFFFFFFFFFFFFFFFF
zeroWider :: Wider
zeroWider :: Wider
zeroWider = Word -> Word -> Word -> Word -> Wider
W.wider Word
0 Word
0 Word
0 Word
0
secScalarFromBytes :: ByteString -> Either DecodeError SecScalar
secScalarFromBytes :: ByteString -> Either DecodeError SecScalar
secScalarFromBytes ByteString
b
| ByteString -> Int
BS.length ByteString
b Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
32 = DecodeError -> Either DecodeError SecScalar
forall a b. a -> Either a b
Left (Int -> Int -> DecodeError
InvalidLength Int
32 (ByteString -> Int
BS.length ByteString
b))
| Choice -> Bool
C.decide (Choice -> Choice -> Choice
C.and (Wider -> Wider -> Choice
W.gt Wider
w Wider
zeroWider) (Wider -> Wider -> Choice
W.lt Wider
w Wider
orderWider)) = SecScalar -> Either DecodeError SecScalar
forall a b. b -> Either a b
Right (SecScalar -> Either DecodeError SecScalar)
-> SecScalar -> Either DecodeError SecScalar
forall a b. (a -> b) -> a -> b
$! Montgomery -> SecScalar
SecScalar (Wider -> Montgomery
S.to Wider
w)
| Choice -> Bool
C.decide (Wider -> Wider -> Choice
W.gt Wider
w Wider
zeroWider) = DecodeError -> Either DecodeError SecScalar
forall a b. a -> Either a b
Left DecodeError
ScalarOutOfRange
| Bool
otherwise = DecodeError -> Either DecodeError SecScalar
forall a b. a -> Either a b
Left DecodeError
ScalarZero
where
w :: Wider
w = ByteString -> Wider
widerFromBytes ByteString
b
secScalarFromBytesModQ :: ByteString -> SecScalar
secScalarFromBytesModQ :: ByteString -> SecScalar
secScalarFromBytesModQ ByteString
b = Montgomery -> SecScalar
SecScalar (Wider -> Montgomery
S.to (ByteString -> Wider
widerFromBytes ByteString
b))
secScalarToBytes :: SecScalar -> ByteString
secScalarToBytes :: SecScalar -> ByteString
secScalarToBytes = Wider -> ByteString
widerToBytes (Wider -> ByteString)
-> (SecScalar -> Wider) -> SecScalar -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SecScalar -> Wider
secScalarWider
secScalarWider :: SecScalar -> Wider
secScalarWider :: SecScalar -> Wider
secScalarWider (SecScalar Montgomery
m) = Montgomery -> Wider
S.retr Montgomery
m
publicMontgomery :: Integer -> S.Montgomery
publicMontgomery :: Integer -> Montgomery
publicMontgomery Integer
x = Wider -> Montgomery
S.to (Integer -> Wider
W.to_vartime (Integer -> Integer
modQ Integer
x))
choice :: Bool -> C.Choice
choice :: Bool -> Choice
choice Bool
True = () -> Choice
C.true# ()
choice Bool
False = () -> Choice
C.false# ()
signScalar ::
SecScalar ->
SecScalar ->
SecScalar ->
Bool ->
Bool ->
Integer ->
Integer ->
Integer
signScalar :: SecScalar
-> SecScalar
-> SecScalar
-> Bool
-> Bool
-> Integer
-> Integer
-> Integer
signScalar (SecScalar Montgomery
k1) (SecScalar Montgomery
k2) (SecScalar Montgomery
d) Bool
negateNonce Bool
negateKey Integer
b Integer
ea =
let k :: Montgomery
k = Montgomery -> Montgomery -> Montgomery
S.add Montgomery
k1 (Montgomery -> Montgomery -> Montgomery
S.mul (Integer -> Montgomery
publicMontgomery Integer
b) Montgomery
k2)
k' :: Montgomery
k' = Montgomery -> Montgomery -> Choice -> Montgomery
S.select Montgomery
k (Montgomery -> Montgomery
S.neg Montgomery
k) (Bool -> Choice
choice Bool
negateNonce)
d' :: Montgomery
d' = Montgomery -> Montgomery -> Choice -> Montgomery
S.select Montgomery
d (Montgomery -> Montgomery
S.neg Montgomery
d) (Bool -> Choice
choice Bool
negateKey)
in Wider -> Integer
W.from_vartime (Montgomery -> Wider
S.retr (Montgomery -> Montgomery -> Montgomery
S.add Montgomery
k' (Montgomery -> Montgomery -> Montgomery
S.mul (Integer -> Montgomery
publicMontgomery Integer
ea) Montgomery
d')))
widerFromBytes :: ByteString -> Wider
widerFromBytes :: ByteString -> Wider
widerFromBytes ByteString
b
| ByteString -> Int
BS.length ByteString
b Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
32 = [Char] -> Wider
forall a. HasCallStack => [Char] -> a
error [Char]
"widerFromBytes: fewer than 32 bytes"
| Bool
otherwise = Word -> Word -> Word -> Word -> Wider
W.wider (Int -> Word
limbAt Int
24) (Int -> Word
limbAt Int
16) (Int -> Word
limbAt Int
8) (Int -> Word
limbAt Int
0)
where
limbAt :: Int -> Word
limbAt :: Int -> Word
limbAt Int
off =
Int -> Int -> Word
byteAt Int
off Int
56
Word -> Word -> Word
forall a. Bits a => a -> a -> a
.|. Int -> Int -> Word
byteAt (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
48
Word -> Word -> Word
forall a. Bits a => a -> a -> a
.|. Int -> Int -> Word
byteAt (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2) Int
40
Word -> Word -> Word
forall a. Bits a => a -> a -> a
.|. Int -> Int -> Word
byteAt (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
3) Int
32
Word -> Word -> Word
forall a. Bits a => a -> a -> a
.|. Int -> Int -> Word
byteAt (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4) Int
24
Word -> Word -> Word
forall a. Bits a => a -> a -> a
.|. Int -> Int -> Word
byteAt (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
5) Int
16
Word -> Word -> Word
forall a. Bits a => a -> a -> a
.|. Int -> Int -> Word
byteAt (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
6) Int
8
Word -> Word -> Word
forall a. Bits a => a -> a -> a
.|. Int -> Int -> Word
byteAt (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
7) Int
0
byteAt :: Int -> Int -> Word
byteAt :: Int -> Int -> Word
byteAt Int
i Int
s = Word8 -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int -> Word8
BU.unsafeIndex ByteString
b Int
i) Word -> Int -> Word
forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
s
widerToBytes :: Wider -> ByteString
widerToBytes :: Wider -> ByteString
widerToBytes (Wider (# Limb Word#
w0, Limb Word#
w1, Limb Word#
w2, Limb Word#
w3 #)) = Int -> (Ptr Word8 -> IO ()) -> ByteString
BI.unsafeCreate Int
32 ((Ptr Word8 -> IO ()) -> ByteString)
-> (Ptr Word8 -> IO ()) -> ByteString
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
p -> do
Ptr Word8 -> Int -> Word -> IO ()
pokeLimb Ptr Word8
p Int
0 (Word# -> Word
W# Word#
w3)
Ptr Word8 -> Int -> Word -> IO ()
pokeLimb Ptr Word8
p Int
8 (Word# -> Word
W# Word#
w2)
Ptr Word8 -> Int -> Word -> IO ()
pokeLimb Ptr Word8
p Int
16 (Word# -> Word
W# Word#
w1)
Ptr Word8 -> Int -> Word -> IO ()
pokeLimb Ptr Word8
p Int
24 (Word# -> Word
W# Word#
w0)
where
pokeLimb :: Ptr Word8 -> Int -> Word -> IO ()
pokeLimb :: Ptr Word8 -> Int -> Word -> IO ()
pokeLimb Ptr Word8
p Int
off Word
w = do
Ptr Word8 -> Int -> Word -> IO ()
pokeByte Ptr Word8
p Int
off (Word
w Word -> Int -> Word
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
56)
Ptr Word8 -> Int -> Word -> IO ()
pokeByte Ptr Word8
p (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Word
w Word -> Int -> Word
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
48)
Ptr Word8 -> Int -> Word -> IO ()
pokeByte Ptr Word8
p (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2) (Word
w Word -> Int -> Word
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
40)
Ptr Word8 -> Int -> Word -> IO ()
pokeByte Ptr Word8
p (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
3) (Word
w Word -> Int -> Word
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
32)
Ptr Word8 -> Int -> Word -> IO ()
pokeByte Ptr Word8
p (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4) (Word
w Word -> Int -> Word
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
24)
Ptr Word8 -> Int -> Word -> IO ()
pokeByte Ptr Word8
p (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
5) (Word
w Word -> Int -> Word
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
16)
Ptr Word8 -> Int -> Word -> IO ()
pokeByte Ptr Word8
p (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
6) (Word
w Word -> Int -> Word
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
8)
Ptr Word8 -> Int -> Word -> IO ()
pokeByte Ptr Word8
p (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
7) Word
w
pokeByte :: Ptr Word8 -> Int -> Word -> IO ()
pokeByte :: Ptr Word8 -> Int -> Word -> IO ()
pokeByte Ptr Word8
p Int
off Word
x = Ptr Word8 -> Int -> Word8 -> IO ()
forall b. Ptr b -> Int -> Word8 -> IO ()
forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr Word8
p Int
off (Word -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
x :: Word8)