Add tweak negation (#28)

This commit is contained in:
Janus Troelsen 2020-04-09 06:16:10 -05:00 committed by GitHub
parent 5dac083f4e
commit ae2c67e35e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -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 =