From e85b68ef53e80eb66c7ab394c57e9eb146a60b91 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Wed, 6 Dec 2017 16:07:08 +0100 Subject: crypto: add DecompressPubkey, VerifySignature (#15615) We need those operations for p2p/enr. Also upgrade github.com/btcsuite/btcd/btcec to the latest version and improve BenchmarkSha3. The benchmark printed extra output that confused tools like benchstat and ignored N. --- crypto/signature_cgo.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'crypto/signature_cgo.go') diff --git a/crypto/signature_cgo.go b/crypto/signature_cgo.go index feec5e7be..381d8a1bb 100644 --- a/crypto/signature_cgo.go +++ b/crypto/signature_cgo.go @@ -27,10 +27,12 @@ import ( "github.com/ethereum/go-ethereum/crypto/secp256k1" ) +// Ecrecover returns the uncompressed public key that created the given signature. func Ecrecover(hash, sig []byte) ([]byte, error) { return secp256k1.RecoverPubkey(hash, sig) } +// SigToPub returns the public key that created the given signature. func SigToPub(hash, sig []byte) (*ecdsa.PublicKey, error) { s, err := Ecrecover(hash, sig) if err != nil { @@ -58,6 +60,22 @@ func Sign(hash []byte, prv *ecdsa.PrivateKey) (sig []byte, err error) { return secp256k1.Sign(hash, seckey) } +// VerifySignature checks that the given public key created signature over hash. +// The public key should be in compressed (33 bytes) or uncompressed (65 bytes) format. +// The signature should have the 64 byte [R || S] format. +func VerifySignature(pubkey, hash, signature []byte) bool { + return secp256k1.VerifySignature(pubkey, hash, signature) +} + +// DecompressPubkey parses a public key in the 33-byte compressed format. +func DecompressPubkey(pubkey []byte) (*ecdsa.PublicKey, error) { + x, y := secp256k1.DecompressPubkey(pubkey) + if x == nil { + return nil, fmt.Errorf("invalid public key") + } + return &ecdsa.PublicKey{X: x, Y: y, Curve: S256()}, nil +} + // S256 returns an instance of the secp256k1 curve. func S256() elliptic.Curve { return secp256k1.S256() -- cgit v1.2.3