Skip to content

Commit

Permalink
Added test cases for getBin and Verify
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Oct 1, 2020
1 parent 7770631 commit 2744ec1
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 3 deletions.
12 changes: 9 additions & 3 deletions verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,12 @@ func addressToString(byteCopy []byte) (s string, err error) {
// License is:
// https://github.com/piotrnar/gocoin/blob/master/lib/secp256k1/COPYING
func getBin(num *secp256k1.Number, le int) ([]byte, error) {
if num == nil {
return nil, errors.New("secp256k1.Number is nil")
}
bts := num.Bytes()
if len(bts) > le {
return nil, fmt.Errorf("buffer too small")
return nil, errors.New("buffer too small")
}
if len(bts) == le {
return bts, nil
Expand All @@ -146,6 +149,9 @@ func getBin(num *secp256k1.Number, le int) ([]byte, error) {
// License is:
// https://github.com/piotrnar/gocoin/blob/master/lib/secp256k1/COPYING
func recoverSig(sig *secp256k1.Signature, pubkey *secp256k1.XY, m *secp256k1.Number, recID int) (bool, error) {
if sig == nil {
return false, errors.New("sig is nil")
}
var theCurveP secp256k1.Number
theCurveP.SetBytes([]byte{
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
Expand All @@ -159,7 +165,7 @@ func recoverSig(sig *secp256k1.Signature, pubkey *secp256k1.XY, m *secp256k1.Num
if (recID & 2) != 0 {
rx.Add(&rx.Int, &secp256k1.TheCurve.Order.Int)
if rx.Cmp(&theCurveP.Int) >= 0 {
return false, nil // todo: is this actually an error?
return false, errors.New("error in recoverSig")
}
}

Expand All @@ -172,7 +178,7 @@ func recoverSig(sig *secp256k1.Signature, pubkey *secp256k1.XY, m *secp256k1.Num

X.SetXO(&fx, (recID&1) != 0)
if !X.IsValid() {
return false, nil // todo: is this actually an error?
return false, errors.New("x.IsValid failed")
}

xj.SetXY(&X)
Expand Down
99 changes: 99 additions & 0 deletions verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bitcoin
import (
"encoding/hex"
"fmt"
"math/big"
"strconv"
"testing"

Expand Down Expand Up @@ -173,3 +174,101 @@ func TestVerifyMessageSigRecover(t *testing.T) {
}
}
}

// TestVerifyMessageSigRecoverFailed will test the method recoverSig()
func TestVerifyMessageSigRecoverFailed(t *testing.T) {

// Bad signature cases
var vs = [][6]string{
{
"0",
"0",
"0",
"0",
"0",
"0",
},
{
"000",
"560978aed76de9d5d781f87ed2068832ed545f2b21bf040654a2daff694c8b09",
"9ce428d58e8e4caf619dc6fc7b2c2c28f0561654d1f80f322c038ad5e67ff8a6",
"1",
"000",
"bf29a25e2d1f32c5afb18b41ae60112723278a8af31275965a6ec1d95334e840",
},
{
"000",
"1234567",
"1234567",
"1",
"000",
"1234567",
},
{
"bf29a25e2d1f32c5afb18b41ae60112723278a8af31275965a6ec1d95334e840",
"bf29a25e2d1f32c5afb18b41ae60112723278a8af31275965a6ec1d95334e840",
"bf29a25e2d1f32c5afb18b41ae60112723278a8af31275965a6ec1d95334e840",
"1",
"bf29a25e2d1f32c5afb18b41ae60112723278a8af31275965a6ec1d95334e840",
"bf29a25e2d1f32c5afb18b41ae60112723278a8af31275965a6ec1d95334e840",
},
}

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, err := strconv.ParseInt(vs[i][3], 10, 32)
if err != nil {
t.Fatalf("failed in ParseInt: %s", err.Error())
}
exp.X.SetHex(vs[i][4])
exp.Y.SetHex(vs[i][5])

var success bool
success, err = recoverSig(&sig, &pubkey, &msg, int(rid))

if success {
t.Fatalf("sigRecover should have failed")
} else if err == nil {
t.Error("sigRecover should have thrown an error")
}
}

// Test nil case
if _, err := recoverSig(nil, &pubkey, &msg, 1); err == nil {
t.Fatalf("error was expected with nil sig")
}
}

// TestVerifyMessageGetBin will test the method getBin()
func TestVerifyMessageGetBin(t *testing.T) {
t.Parallel()

var (
// Testing private methods
tests = []struct {
inputNumber *secp256k1.Number
inputLength int
expectedError bool
}{
{nil, 1, true},
{&secp256k1.Number{Int: *big.NewInt(400)}, 1, true},
{&secp256k1.Number{Int: *big.NewInt(1)}, 1, false},
}
)

// Run tests
for _, test := range tests {
if _, err := getBin(test.inputNumber, test.inputLength); err != nil && !test.expectedError {
t.Errorf("%s Failed: [%v] [%d] inputted and error not expected but got: %s", t.Name(), test.inputNumber, test.inputLength, err.Error())
} else if err == nil && test.expectedError {
t.Log(len(test.inputNumber.Bytes()), test.inputLength)
t.Errorf("%s Failed: [%v] [%d] inputted and error was expected", t.Name(), test.inputNumber, test.inputLength)
}
}
}

0 comments on commit 2744ec1

Please sign in to comment.