From ffbf20e9f0430b95448bd66c6b1b530864397fb3 Mon Sep 17 00:00:00 2001 From: Evgenii Akentev Date: Wed, 15 Nov 2023 18:42:38 +0400 Subject: [PATCH] Fix recoverPublicKey --- src-secp256k1/Crypto/Secp256k1/Internal.hs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src-secp256k1/Crypto/Secp256k1/Internal.hs b/src-secp256k1/Crypto/Secp256k1/Internal.hs index 547c7b8..5ceb2be 100644 --- a/src-secp256k1/Crypto/Secp256k1/Internal.hs +++ b/src-secp256k1/Crypto/Secp256k1/Internal.hs @@ -666,13 +666,17 @@ recoverPublicKey -> Bool -- ^ is second key -> Maybe Point -recoverPublicKey e r s oddY secondKey = case nC *. rP of - O -> if validatePublicKey pk then Just pk else Nothing - _ -> error "something went wrong (probably the value for second key is incorrect)" +recoverPublicKey e r s oddY secondKey + | Just rP <- pointFromX x oddY = + case nC *. rP of + O -> + let pk = invM r .*. (s .*. rP .-. e .*. gC) + in if validatePublicKey pk then Just pk else Nothing + _ -> error "something went wrong (probably the value for second key is incorrect)" + | otherwise = Nothing where x = if secondKey then zConv r .+ fp nC else zConv r - Just rP = pointFromX x oddY -- FIXME this fails for an invalid @secondKey@ value - pk = invM r .*. (s .*. rP .-. e .*. gC) + -- -------------------------------------------------------------------------- -- -- Hexdecimal Representation