Skip to content

Commit

Permalink
chore: verify PK consistency in ECDSA PoP (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianElvis authored Oct 17, 2023
1 parent eeacd5f commit 41bc320
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions crypto/ecdsa/ecdsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package ecdsa

import (
"bytes"
"fmt"

"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/ecdsa"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
)
Expand Down Expand Up @@ -35,6 +37,14 @@ func Sign(sk *btcec.PrivateKey, msg string) ([]byte, error) {

func Verify(pk *btcec.PublicKey, msg string, sigBytes []byte) error {
msgHash := magicHash(msg)
_, _, err := ecdsa.RecoverCompact(sigBytes, msgHash[:])
return err
recoveredPK, _, err := ecdsa.RecoverCompact(sigBytes, msgHash[:])
if err != nil {
return err
}
pkBytes := schnorr.SerializePubKey(pk)
recoveredPKBytes := schnorr.SerializePubKey(recoveredPK)
if !bytes.Equal(pkBytes, recoveredPKBytes) {
return fmt.Errorf("the recovered PK does not match the given PK")
}
return nil
}

0 comments on commit 41bc320

Please sign in to comment.