Skip to content

Commit

Permalink
Added more internal tests for Verify
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Oct 1, 2020
1 parent 161e43c commit 7770631
Showing 1 changed file with 83 additions and 16 deletions.
99 changes: 83 additions & 16 deletions verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package bitcoin
import (
"encoding/hex"
"fmt"
"strconv"
"testing"

"github.com/piotrnar/gocoin/lib/secp256k1"
)

// TestVerifyMessage will test the method VerifyMessage()
Expand Down Expand Up @@ -37,8 +40,32 @@ func TestVerifyMessage(t *testing.T) {
}
}

}

// ExampleVerifyMessage example using VerifyMessage()
func ExampleVerifyMessage() {
if err := VerifyMessage("1FiyJnrgwBc3Ff83V1yRWAkmXBdGrDQnXQ", "IBDscOd/Ov4yrd/YXantqajSAnW4fudpfr2KQy5GNo9pZybF12uNaal4KI822UpQLS/UJD+UK2SnNMn6Z3E4na8=", "Testing!"); err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
fmt.Printf("verification passed")
// Output:verification passed
}

// BenchmarkVerifyMessage benchmarks the method VerifyMessage()
func BenchmarkVerifyMessage(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = VerifyMessage("1FiyJnrgwBc3Ff83V1yRWAkmXBdGrDQnXQ", "IBDscOd/Ov4yrd/YXantqajSAnW4fudpfr2KQy5GNo9pZybF12uNaal4KI822UpQLS/UJD+UK2SnNMn6Z3E4na8=", "Testing!")
}
}

// TestVerifyMessageMessageHash will test the method messageHash()
func TestVerifyMessageMessageHash(t *testing.T) {

t.Parallel()

// Testing private methods
var messageTests = []struct {
var tests = []struct {
inputMessage string
inputHeader string
expectedHash string
Expand All @@ -52,7 +79,7 @@ func TestVerifyMessage(t *testing.T) {
}

// Run tests
for _, test := range messageTests {
for _, test := range tests {
if output, err := messageHash(test.inputMessage, test.inputHeader); err != nil && !test.expectedError {
t.Errorf("%s Failed: [%s] [%s] inputted and error not expected but got: %s", t.Name(), test.inputMessage, test.inputHeader, err.Error())
} else if err == nil && test.expectedError {
Expand All @@ -62,8 +89,15 @@ func TestVerifyMessage(t *testing.T) {
}
}

}

// TestVerifyMessageParseSignature will test the method parseSignature()
func TestVerifyMessageParseSignature(t *testing.T) {

t.Parallel()

// Testing private methods
var parseTests = []struct {
var tests = []struct {
inputSignature string
expectedID int
expectedError bool
Expand All @@ -78,7 +112,7 @@ func TestVerifyMessage(t *testing.T) {
}

// Run tests
for _, test := range parseTests {
for _, test := range tests {
if _, output, err := parseSignature(test.inputSignature); err != nil && !test.expectedError {
t.Errorf("%s Failed: [%s] inputted and error not expected but got: %s", t.Name(), test.inputSignature, err.Error())
} else if err == nil && test.expectedError {
Expand All @@ -90,19 +124,52 @@ func TestVerifyMessage(t *testing.T) {

}

// ExampleVerifyMessage example using VerifyMessage()
func ExampleVerifyMessage() {
if err := VerifyMessage("1FiyJnrgwBc3Ff83V1yRWAkmXBdGrDQnXQ", "IBDscOd/Ov4yrd/YXantqajSAnW4fudpfr2KQy5GNo9pZybF12uNaal4KI822UpQLS/UJD+UK2SnNMn6Z3E4na8=", "Testing!"); err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
// TestVerifyMessageSigRecover will test the method recoverSig()
//
// From: https://github.com/piotrnar/gocoin/blob/master/lib/secp256k1/sig_test.go
func TestVerifyMessageSigRecover(t *testing.T) {
var vs = [][6]string{
{
"6028b9e3a31c9e725fcbd7d5d16736aaaafcc9bf157dfb4be62bcbcf0969d488",
"036d4a36fa235b8f9f815aa6f5457a607f956a71a035bf0970d8578bf218bb5a",
"9cff3da1a4f86caf3683f865232c64992b5ed002af42b321b8d8a48420680487",
"0",
"56dc5df245955302893d8dda0677cc9865d8011bc678c7803a18b5f6faafec08",
"54b5fbdcd8fac6468dac2de88fadce6414f5f3afbb103753e25161bef77705a6",
},
{
"b470e02f834a3aaafa27bd2b49e07269e962a51410f364e9e195c31351a05e50",
"560978aed76de9d5d781f87ed2068832ed545f2b21bf040654a2daff694c8b09",
"9ce428d58e8e4caf619dc6fc7b2c2c28f0561654d1f80f322c038ad5e67ff8a6",
"1",
"15b7e7d00f024bffcd2e47524bb7b7d3a6b251e23a3a43191ed7f0a418d9a578",
"bf29a25e2d1f32c5afb18b41ae60112723278a8af31275965a6ec1d95334e840",
},
}
fmt.Printf("verification passed")
// Output:verification passed
}

// BenchmarkVerifyMessage benchmarks the method VerifyMessage()
func BenchmarkVerifyMessage(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = VerifyMessage("1FiyJnrgwBc3Ff83V1yRWAkmXBdGrDQnXQ", "IBDscOd/Ov4yrd/YXantqajSAnW4fudpfr2KQy5GNo9pZybF12uNaal4KI822UpQLS/UJD+UK2SnNMn6Z3E4na8=", "Testing!")
var sig secp256k1.Signature
var pubkey, exp secp256k1.XY
var msg secp256k1.Number

for i := range vs {
sig.R.SetHex(vs[i][0])
sig.S.SetHex(vs[i][1])
msg.SetHex(vs[i][2])
rid, _ := strconv.ParseInt(vs[i][3], 10, 32)
exp.X.SetHex(vs[i][4])
exp.Y.SetHex(vs[i][5])

success, _ := recoverSig(&sig, &pubkey, &msg, int(rid))

if success {
if !exp.X.Equals(&pubkey.X) {
t.Error("X mismatch at vector", i)
}
if !exp.Y.Equals(&pubkey.Y) {
t.Error("Y mismatch at vector", i)
}
} else {
t.Error("sig.recover failed")
}
}
}

0 comments on commit 7770631

Please sign in to comment.