Avoid abort in combinePubKeys (#31)

Fixes #27.
This commit is contained in:
Janus Troelsen 2020-04-11 07:04:21 -05:00 committed by GitHub
parent 539999933e
commit 3307b6d5dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 9 deletions

View File

@ -482,15 +482,18 @@ tweakMulPubKey (PubKey fp) (Tweak ft) = withContext $ \ctx ->
-- | Add multiple public keys together.
combinePubKeys :: [PubKey] -> Maybe PubKey
combinePubKeys pubs = withContext $ \ctx -> pointers [] pubs $ \ps ->
allocaArray (length ps) $ \a -> do
pokeArray a ps
fp <- mallocForeignPtr
ret <- withForeignPtr fp $ \p ->
ecPubKeyCombine ctx p a (fromIntegral $ length ps)
if isSuccess ret
then return $ Just $ PubKey fp
else return Nothing
combinePubKeys pubs = withContext $ \ctx ->
if pubs == []
then return Nothing
else pointers [] pubs $ \ps ->
allocaArray (length ps) $ \a -> do
pokeArray a ps
fp <- mallocForeignPtr
ret <- withForeignPtr fp $ \p ->
ecPubKeyCombine ctx p a (fromIntegral $ length ps)
if isSuccess ret
then return $ Just $ PubKey fp
else return Nothing
where
pointers ps [] f = f ps
pointers ps (PubKey fp : pubs') f =

View File

@ -57,6 +57,7 @@ spec = do
it "add public key" $ property $ tweakAddPubKeyTest
it "multiply public key" $ property $ tweakMulPubKeyTest
it "combine public keys" $ property $ combinePubKeyTest
it "can't combine 0 public keys" $ property $ combinePubKeyEmptyListTest
it "negates tweak" $ property $ negateTweakTest
#ifdef ECDH
describe "ecdh" $ do
@ -277,6 +278,13 @@ combinePubKeyTest =
expected = importPubKey $ hexToBytes
"043d9a7ec70011efc23c33a7e62d2ea73cca87797e3b659d93bea6aa871aebde56c3bc6134ca82e324b0ab9c0e601a6d2933afe7fb5d9f3aae900f5c5dc6e362c8"
combinePubKeyEmptyListTest :: Assertion
combinePubKeyEmptyListTest =
assertEqual "empty pubkey list must return Nothing" expected combined
where
expected = Nothing
combined = combinePubKeys []
negateTweakTest :: Assertion
negateTweakTest =
assertEqual "can recover secret key 1 after adding tweak 1" oneKey subtracted