keys: fix failing Secp256k1 test
Pad R and S when computing signature. Fix #1223.
This commit is contained in:
parent
a45c160f10
commit
76fdbea331
2 changed files with 35 additions and 3 deletions
|
@ -136,8 +136,11 @@ func (p *PrivateKey) Sign(data []byte) []byte {
|
|||
)
|
||||
|
||||
r, s := rfc6979.SignECDSA(privateKey, digest[:], sha256.New)
|
||||
return getSignatureSlice(privateKey.Curve, r, s)
|
||||
}
|
||||
|
||||
params := privateKey.Curve.Params()
|
||||
func getSignatureSlice(curve elliptic.Curve, r, s *big.Int) []byte {
|
||||
params := curve.Params()
|
||||
curveOrderByteSize := params.P.BitLen() / 8
|
||||
rBytes, sBytes := r.Bytes(), s.Bytes()
|
||||
signature := make([]byte, curveOrderByteSize*2)
|
||||
|
|
|
@ -2,6 +2,7 @@ package keys
|
|||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"math/big"
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
|
@ -10,6 +11,34 @@ import (
|
|||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestIssue1223(t *testing.T) {
|
||||
var d, x, y big.Int
|
||||
d.SetString("75066030006596498716801752450216843918658392116070031536027203512060270094427", 10)
|
||||
x.SetString("56810139335762307690884151098712528235297095596167964448512639328424930082240", 10)
|
||||
y.SetString("108055740278314806025442297642651169427004858252141003070998851291610422839293", 10)
|
||||
|
||||
privateKey := &btcec.PrivateKey{
|
||||
PublicKey: ecdsa.PublicKey{
|
||||
Curve: btcec.S256(),
|
||||
X: &x,
|
||||
Y: &y,
|
||||
},
|
||||
D: &d,
|
||||
}
|
||||
pubKey := PublicKey(ecdsa.PublicKey{
|
||||
Curve: btcec.S256(),
|
||||
X: privateKey.X,
|
||||
Y: privateKey.Y,
|
||||
})
|
||||
|
||||
hashedData := hash.Sha256([]byte("sample"))
|
||||
signature, err := privateKey.Sign(hashedData.BytesBE())
|
||||
require.NoError(t, err)
|
||||
|
||||
signedData := getSignatureSlice(privateKey.Curve, signature.R, signature.S)
|
||||
require.True(t, pubKey.Verify(signedData, hashedData.BytesBE()))
|
||||
}
|
||||
|
||||
func TestPubKeyVerify(t *testing.T) {
|
||||
var data = []byte("sample")
|
||||
hashedData := hash.Sha256(data)
|
||||
|
@ -32,7 +61,7 @@ func TestPubKeyVerify(t *testing.T) {
|
|||
assert.Nil(t, err)
|
||||
signature, err := privateKey.Sign(hashedData.BytesBE())
|
||||
require.NoError(t, err)
|
||||
signedData := append(signature.R.Bytes(), signature.S.Bytes()...)
|
||||
signedData := getSignatureSlice(privateKey.Curve, signature.R, signature.S)
|
||||
pubKey := PublicKey(ecdsa.PublicKey{
|
||||
Curve: btcec.S256(),
|
||||
X: privateKey.X,
|
||||
|
@ -66,7 +95,7 @@ func TestWrongPubKey(t *testing.T) {
|
|||
assert.Nil(t, err)
|
||||
signature, err := privateKey.Sign(hashedData.BytesBE())
|
||||
assert.Nil(t, err)
|
||||
signedData := append(signature.R.Bytes(), signature.S.Bytes()...)
|
||||
signedData := getSignatureSlice(privateKey.Curve, signature.R, signature.S)
|
||||
|
||||
secondPrivKey, err := btcec.NewPrivateKey(btcec.S256())
|
||||
assert.Nil(t, err)
|
||||
|
|
Loading…
Reference in a new issue