mirror of
https://github.com/ilyakooo0/secp256k1-haskell.git
synced 2024-09-11 12:25:38 +03:00
Add tweak negation (#28)
This commit is contained in:
parent
5dac083f4e
commit
ae2c67e35e
@ -57,6 +57,7 @@ module Crypto.Secp256k1
|
||||
, tweakAddPubKey
|
||||
, tweakMulPubKey
|
||||
, combinePubKeys
|
||||
, tweakNegate
|
||||
|
||||
#ifdef ECDH
|
||||
-- * Diffie Hellman
|
||||
@ -480,6 +481,18 @@ recover (RecSig frg) (Msg fm) = withContext $ \ctx ->
|
||||
ret <- withForeignPtr fp $ \pp -> ecdsaRecover ctx pp prg pm
|
||||
if isSuccess ret then return $ Just $ PubKey fp else return Nothing
|
||||
|
||||
tweakNegate :: Tweak -> Maybe Tweak
|
||||
tweakNegate (Tweak fk) = withContext $ \ctx -> do
|
||||
fnew <- mallocForeignPtr
|
||||
peeked <- withForeignPtr fk peek
|
||||
ret <- withForeignPtr fnew $ \n -> do
|
||||
poke n peeked
|
||||
ecTweakNegate ctx n
|
||||
return $
|
||||
if isSuccess ret
|
||||
then Just (Tweak fnew)
|
||||
else Nothing
|
||||
|
||||
#ifdef ECDH
|
||||
-- | Compute Diffie-Hellman secret.
|
||||
ecdh :: PubKey -> SecKey -> ByteString
|
||||
|
@ -391,6 +391,13 @@ foreign import ccall
|
||||
-> Ptr Tweak32
|
||||
-> IO Ret
|
||||
|
||||
foreign import ccall
|
||||
"secp256k1.h secp256k1_ec_privkey_negate"
|
||||
ecTweakNegate
|
||||
:: Ptr Ctx
|
||||
-> Ptr Tweak32
|
||||
-> IO Ret
|
||||
|
||||
foreign import ccall
|
||||
"secp256k1.h secp256k1_ec_pubkey_tweak_add"
|
||||
ecPubKeyTweakAdd
|
||||
|
@ -52,6 +52,7 @@ spec = do
|
||||
it "add public key" $ property $ tweakAddPubKeyTest
|
||||
it "multiply public key" $ property $ tweakMulPubKeyTest
|
||||
it "combine public keys" $ property $ combinePubKeyTest
|
||||
it "negates tweak" $ property $ negateTweakTest
|
||||
#ifdef ECDH
|
||||
describe "ecdh" $ do
|
||||
it "computes dh secret" $ property $ computeDhSecret
|
||||
@ -239,6 +240,18 @@ combinePubKeyTest =
|
||||
expected = importPubKey $ fst $ B16.decode $ B8.pack
|
||||
"043d9a7ec70011efc23c33a7e62d2ea73cca87797e3b659d93bea6aa871aebde56c3bc6134ca82e324b0ab9c0e601a6d2933afe7fb5d9f3aae900f5c5dc6e362c8"
|
||||
|
||||
negateTweakTest :: Assertion
|
||||
negateTweakTest =
|
||||
assertEqual "can recover secret key 1 after adding tweak 1" oneKey subtracted
|
||||
where
|
||||
Just oneKey = secKey $ fst $ B16.decode $ B8.pack
|
||||
"0000000000000000000000000000000000000000000000000000000000000001"
|
||||
Just oneTwk = tweak $ fst $ B16.decode $ B8.pack
|
||||
"0000000000000000000000000000000000000000000000000000000000000001"
|
||||
Just minusOneTwk = tweakNegate oneTwk
|
||||
Just twoKey = tweakAddSecKey oneKey oneTwk
|
||||
Just subtracted = tweakAddSecKey twoKey minusOneTwk
|
||||
|
||||
#ifdef ECDH
|
||||
computeDhSecret :: Assertion
|
||||
computeDhSecret =
|
||||
|
Loading…
Reference in New Issue
Block a user