Which method is used for verifying secp256k1 signatures in Go's btcec library?

155 Views Asked by At

I am working with secp256k1 signatures in Go using the btcec library. However, I couldn't find a clear method to verify signatures in the official documentation.btcec There is a link to a "Verify Signature" example in the documentation, but it doesn’t seem to provide the example code directly.

I am wondering, which method in the btcec library is used for verifying secp256k1 signatures? It would be great if someone could provide a simple code example. Thank you!

1

There are 1 best solutions below

0
David Walton On BEST ANSWER

Here you Go ;-)

https://github.com/btcsuite/btcd/blob/master/btcec/ecdsa/example_test.go


// This example demonstrates verifying a secp256k1 signature against a public
// key that is first parsed from raw bytes.  The signature is also parsed from
// raw bytes.
func Example_verifySignature() {
    // Decode hex-encoded serialized public key.
    pubKeyBytes, err := hex.DecodeString("02a673638cb9587cb68ea08dbef685c" +
        "6f2d2a751a8b3c6f2a7e9a4999e6e4bfaf5")
    if err != nil {
        fmt.Println(err)
        return
    }
    pubKey, err := btcec.ParsePubKey(pubKeyBytes)
    if err != nil {
        fmt.Println(err)
        return
    }

    // Decode hex-encoded serialized signature.
    sigBytes, err := hex.DecodeString("30450220090ebfb3690a0ff115bb1b38b" +
        "8b323a667b7653454f1bccb06d4bbdca42c2079022100ec95778b51e707" +
        "1cb1205f8bde9af6592fc978b0452dafe599481c46d6b2e479")

    if err != nil {
        fmt.Println(err)
        return
    }
    signature, err := ecdsa.ParseSignature(sigBytes)
    if err != nil {
        fmt.Println(err)
        return
    }

    // Verify the signature for the message using the public key.
    message := "test message"
    messageHash := chainhash.DoubleHashB([]byte(message))
    verified := signature.Verify(messageHash, pubKey)
    fmt.Println("Signature Verified?", verified)

    // Output:
    // Signature Verified? true
}