Navigation Menu

Skip to content

Commit

Permalink
refactor checklows
Browse files Browse the repository at this point in the history
  • Loading branch information
machine-bot committed Nov 22, 2018
1 parent fca875a commit 13617ed
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 1,614 deletions.
16 changes: 1 addition & 15 deletions crypto/ecdsasignature.go
Expand Up @@ -2,7 +2,6 @@ package crypto

import (
"encoding/hex"
"github.com/copernet/copernicus/errcode"
"github.com/copernet/copernicus/log"
"github.com/copernet/secp256k1-go/secp256k1"
"github.com/pkg/errors"
Expand Down Expand Up @@ -53,20 +52,7 @@ func ParseDERSignature(signature []byte) (*Signature, error) {
return (*Signature)(sig), nil
}

func IsLowDERSignature(vchSig []byte) (bool, error) {
if !IsValidSignatureEncoding(vchSig) {
return false, errcode.New(errcode.ScriptErrSigDer)
}
var vchCopy []byte
ret := checkLowS(append(vchCopy, vchSig...))
if !ret {
return false, errcode.New(errcode.ScriptErrSigHighs)
}
return true, nil

}

func checkLowS(vchSig []byte) bool {
func CheckLowS(vchSig []byte) bool {
sig, err := ParseDERSignature(vchSig)
if err != nil {
log.Debug("ParseDERSignature failed, sig:%s", hex.EncodeToString(vchSig))
Expand Down
20 changes: 4 additions & 16 deletions crypto/ecdsasignature_test.go
Expand Up @@ -264,24 +264,15 @@ func TestParseSignature(t *testing.T) {
}

func TestIsLowDERSignature(t *testing.T) {
ret, err := IsLowDERSignature(validSig)
if err == nil {
t.Errorf("IsLowDERSignature error:%v", err)
}
ret := CheckLowS(validSig)
assert.Equal(t, ret, false)

InitSecp256()
ret, err = IsLowDERSignature(validSig)
if err == nil {
t.Errorf("IsLowDERSignature error:%v", err)
}
ret = CheckLowS(validSig)
assert.Equal(t, ret, false)

sig1 := []byte{0x30, 0x3a, 0xd1, 0x5c, 0x20, 0x92, 0x75, 0xca}
ret, err = IsLowDERSignature(sig1)
if err == nil {
t.Errorf("IsLowDERSignature error:%v", err)
}
ret = CheckLowS(sig1)
assert.Equal(t, ret, false)

sig2 := []byte{
Expand All @@ -299,9 +290,6 @@ func TestIsLowDERSignature(t *testing.T) {
0xb2, 0x8d, 0x33, 0x71, 0xe2, 0xa6, 0x7b, 0xf5, 0xbe, 0x29,
}

ret, err = IsLowDERSignature(sig2)
if err != nil {
t.Error(err)
}
ret = CheckLowS(sig2)
assert.Equal(t, true, ret)
}

0 comments on commit 13617ed

Please sign in to comment.