Elliptic Curve Digital Signature Algorithm (ECDSA) is used within Bitcoin and Ethereum. In this case we will use the \((x,y)\) point of Bob's public key, the \((R,S)\) value of the signature and the message (\(m\)) and will check that the signature is valid [ECDSA generation[.
ECDSA Verification |
Outline
The code is:
package main import ( "crypto/ecdsa" "crypto/sha256" "fmt" "math/big" "os" "strings" "github.com/dustinxie/ecc" ) func isHexString(s string) bool { return strings.ContainsAny(s, "abcdef") } func toBigInt(s string) *big.Int { x, _ := new(big.Int).SetString("0", 16) if isHexString(s) { x, _ = new(big.Int).SetString(s, 16) } else { x, _ = new(big.Int).SetString(s, 10) } return (x) } func main() { msg := "Hello 123" r := "2e63d239b3fc47433f19789843e30514b53dfd8773ebc915c0bc774e5368dbb6" s := "39b22fd14b77ae40399340f55fe0ceced6b3107a57bffc65c1dbc9b7a91a050a" bobpub_X := "46cab231f27992deef926ed08b4c8102505d8a29ed0b544777a7ded3996a059c" bobpub_Y := "cc86ca06058be27a7e61830f4e804a02ff072d59e78749b544640280aed73c53" argCount := len(os.Args[1:]) if argCount > 0 { msg = os.Args[1] } if argCount > 1 { r = os.Args[2] } if argCount > 2 { s = os.Args[3] } if argCount > 3 { bobpub_X = os.Args[4] } if argCount > 4 { bobpub_Y = os.Args[5] } m := []byte(msg) digest := sha256.Sum256(m) x := toBigInt(bobpub_X) y := toBigInt(bobpub_Y) R := toBigInt(r) S := toBigInt(s) publicKey := ecdsa.PublicKey{ Curve: ecc.P256k1(), //secp256k1 X: x, Y: y, } fmt.Printf("Message to sign: %s\n", msg) fmt.Printf("Bob Public Key: X:%s Y:%s\n", bobpub_X, bobpub_Y) fmt.Printf("Bob Signature: R:%s S:%s\n", r, s) fmt.Printf("\n=== Now checking with Bob's public key ===\n") rtn := ecdsa.Verify(&publicKey, digest[:], R, S) if rtn { fmt.Printf("Signature works. Yipee!") } else { fmt.Printf("Signature does not check out!!!") } }
A sample run:
Message to sign: Hello 123 Bob Public Key: X:316c6d59567275de266aa82a30ba3e1cd408246a988b431f13d244e34bc2efc1 Y:38a21f573ed9ad9c8dd9107d8954d24b10357beee5b9df470f289be9a28b96a1 Bob Signature: R:8d855e3135408528319c48b9e77bff82dfdf18bf9c65011df8725ab5823ae40c S:b5df557f90fd5697986806186497392d9bdf29fb6dc198775a381d900c399ee === Now checking with Bob's public key === Signature works. Yipee!