2019-08-27 13:29:42 +00:00
|
|
|
package keys
|
|
|
|
|
|
|
|
import (
|
2020-07-13 09:59:41 +00:00
|
|
|
"crypto/ecdsa"
|
2020-07-25 07:56:58 +00:00
|
|
|
"math/big"
|
2019-08-27 13:29:42 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-11-08 14:59:59 +00:00
|
|
|
"github.com/decred/dcrd/dcrec/secp256k1/v4"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
2019-08-27 13:29:42 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-07-13 09:59:41 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-08-27 13:29:42 +00:00
|
|
|
)
|
|
|
|
|
2020-07-25 07:56:58 +00:00
|
|
|
func TestIssue1223(t *testing.T) {
|
|
|
|
var d, x, y big.Int
|
|
|
|
d.SetString("75066030006596498716801752450216843918658392116070031536027203512060270094427", 10)
|
|
|
|
x.SetString("56810139335762307690884151098712528235297095596167964448512639328424930082240", 10)
|
|
|
|
y.SetString("108055740278314806025442297642651169427004858252141003070998851291610422839293", 10)
|
|
|
|
|
2022-11-08 14:59:59 +00:00
|
|
|
privateKey := PrivateKey{
|
|
|
|
PrivateKey: ecdsa.PrivateKey{
|
|
|
|
PublicKey: ecdsa.PublicKey{
|
|
|
|
Curve: secp256k1.S256(),
|
|
|
|
X: &x,
|
|
|
|
Y: &y,
|
|
|
|
},
|
|
|
|
D: &d,
|
2020-07-25 07:56:58 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
pubKey := PublicKey(ecdsa.PublicKey{
|
2022-11-08 14:59:59 +00:00
|
|
|
Curve: secp256k1.S256(),
|
2020-07-25 07:56:58 +00:00
|
|
|
X: privateKey.X,
|
|
|
|
Y: privateKey.Y,
|
|
|
|
})
|
|
|
|
|
|
|
|
hashedData := hash.Sha256([]byte("sample"))
|
2022-11-08 14:59:59 +00:00
|
|
|
signature := privateKey.SignHash(hashedData)
|
|
|
|
require.True(t, pubKey.Verify(signature, hashedData.BytesBE()))
|
2020-07-25 07:56:58 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 14:06:32 +00:00
|
|
|
func TestPubKeyVerify(t *testing.T) {
|
|
|
|
var data = []byte("sample")
|
2019-08-27 13:29:42 +00:00
|
|
|
hashedData := hash.Sha256(data)
|
|
|
|
|
2020-07-13 09:59:41 +00:00
|
|
|
t.Run("Secp256r1", func(t *testing.T) {
|
|
|
|
privKey, err := NewPrivateKey()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
signedData := privKey.Sign(data)
|
|
|
|
pubKey := privKey.PublicKey()
|
|
|
|
result := pubKey.Verify(signedData, hashedData.BytesBE())
|
|
|
|
expected := true
|
|
|
|
assert.Equal(t, expected, result)
|
2019-11-24 15:40:07 +00:00
|
|
|
|
2020-09-29 10:54:15 +00:00
|
|
|
// Small signature, no panic.
|
|
|
|
assert.False(t, pubKey.Verify([]byte{1, 2, 3}, hashedData.BytesBE()))
|
|
|
|
|
2020-07-13 09:59:41 +00:00
|
|
|
pubKey = &PublicKey{}
|
|
|
|
assert.False(t, pubKey.Verify(signedData, hashedData.BytesBE()))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Secp256k1", func(t *testing.T) {
|
2022-11-08 14:59:59 +00:00
|
|
|
privateKey, err := NewSecp256k1PrivateKey()
|
2020-07-13 09:59:41 +00:00
|
|
|
assert.Nil(t, err)
|
2022-11-08 14:59:59 +00:00
|
|
|
signedData := privateKey.SignHash(hashedData)
|
|
|
|
pubKey := privateKey.PublicKey()
|
2020-07-13 09:59:41 +00:00
|
|
|
require.True(t, pubKey.Verify(signedData, hashedData.BytesBE()))
|
|
|
|
|
2022-11-08 14:59:59 +00:00
|
|
|
pubKey = &PublicKey{}
|
2020-07-13 09:59:41 +00:00
|
|
|
assert.False(t, pubKey.Verify(signedData, hashedData.BytesBE()))
|
|
|
|
})
|
2019-08-27 13:29:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWrongPubKey(t *testing.T) {
|
|
|
|
sample := []byte("sample")
|
|
|
|
hashedData := hash.Sha256(sample)
|
|
|
|
|
2020-07-13 09:59:41 +00:00
|
|
|
t.Run("Secp256r1", func(t *testing.T) {
|
|
|
|
privKey, _ := NewPrivateKey()
|
|
|
|
signedData := privKey.Sign(sample)
|
|
|
|
|
|
|
|
secondPrivKey, _ := NewPrivateKey()
|
|
|
|
wrongPubKey := secondPrivKey.PublicKey()
|
|
|
|
|
|
|
|
actual := wrongPubKey.Verify(signedData, hashedData.BytesBE())
|
|
|
|
expcted := false
|
|
|
|
assert.Equal(t, expcted, actual)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Secp256k1", func(t *testing.T) {
|
2022-11-08 14:59:59 +00:00
|
|
|
privateKey, err := NewSecp256k1PrivateKey()
|
2020-07-13 09:59:41 +00:00
|
|
|
assert.Nil(t, err)
|
2022-11-08 14:59:59 +00:00
|
|
|
signedData := privateKey.SignHash(hashedData)
|
2020-07-13 09:59:41 +00:00
|
|
|
|
2022-11-08 14:59:59 +00:00
|
|
|
secondPrivKey, err := NewSecp256k1PrivateKey()
|
2020-07-13 09:59:41 +00:00
|
|
|
assert.Nil(t, err)
|
2022-11-08 14:59:59 +00:00
|
|
|
wrongPubKey := secondPrivKey.PublicKey()
|
2019-08-27 13:29:42 +00:00
|
|
|
|
2020-07-13 09:59:41 +00:00
|
|
|
assert.False(t, wrongPubKey.Verify(signedData, hashedData.BytesBE()))
|
|
|
|
})
|
2019-08-27 13:29:42 +00:00
|
|
|
}
|