core: use Neo.Crypto.CheckSig for standard signature verification
This commit is contained in:
parent
4e6c1092b8
commit
cdaca7be3e
49 changed files with 404 additions and 322 deletions
|
@ -2,9 +2,12 @@ package native
|
|||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"math"
|
||||
"math/big"
|
||||
"testing"
|
||||
|
||||
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
||||
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
||||
"github.com/nspcc-dev/neo-go/pkg/vm"
|
||||
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
@ -39,3 +42,81 @@ func TestRIPEMD160(t *testing.T) {
|
|||
require.Equal(t, "213492c0c6fc5d61497cf17249dd31cd9964b8a3", hex.EncodeToString(c.ripemd160(ic, []stackitem.Item{stackitem.NewByteArray([]byte{1, 0})}).Value().([]byte)))
|
||||
})
|
||||
}
|
||||
|
||||
func TestCryptoLibVerifyWithECDsa(t *testing.T) {
|
||||
t.Run("R1", func(t *testing.T) {
|
||||
testECDSAVerify(t, Secp256r1)
|
||||
})
|
||||
t.Run("K1", func(t *testing.T) {
|
||||
testECDSAVerify(t, Secp256k1)
|
||||
})
|
||||
}
|
||||
|
||||
func testECDSAVerify(t *testing.T, curve NamedCurve) {
|
||||
var (
|
||||
priv *keys.PrivateKey
|
||||
err error
|
||||
c = newCrypto()
|
||||
ic = &interop.Context{VM: vm.New()}
|
||||
actual stackitem.Item
|
||||
)
|
||||
switch curve {
|
||||
case Secp256k1:
|
||||
priv, err = keys.NewSecp256k1PrivateKey()
|
||||
case Secp256r1:
|
||||
priv, err = keys.NewPrivateKey()
|
||||
default:
|
||||
t.Fatal("unknown curve")
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
runCase := func(t *testing.T, isErr bool, result interface{}, args ...interface{}) {
|
||||
argsArr := make([]stackitem.Item, len(args))
|
||||
for i := range args {
|
||||
argsArr[i] = stackitem.Make(args[i])
|
||||
}
|
||||
if isErr {
|
||||
require.Panics(t, func() {
|
||||
_ = c.verifyWithECDsa(ic, argsArr)
|
||||
})
|
||||
} else {
|
||||
require.NotPanics(t, func() {
|
||||
actual = c.verifyWithECDsa(ic, argsArr)
|
||||
})
|
||||
require.Equal(t, stackitem.Make(result), actual)
|
||||
}
|
||||
}
|
||||
|
||||
msg := []byte("test message")
|
||||
sign := priv.Sign(msg)
|
||||
|
||||
t.Run("bad message item", func(t *testing.T) {
|
||||
runCase(t, true, false, stackitem.NewInterop("cheburek"), priv.PublicKey().Bytes(), sign, int64(curve))
|
||||
})
|
||||
t.Run("bad pubkey item", func(t *testing.T) {
|
||||
runCase(t, true, false, msg, stackitem.NewInterop("cheburek"), sign, int64(curve))
|
||||
})
|
||||
t.Run("bad pubkey bytes", func(t *testing.T) {
|
||||
runCase(t, true, false, msg, []byte{1, 2, 3}, sign, int64(curve))
|
||||
})
|
||||
t.Run("bad signature item", func(t *testing.T) {
|
||||
runCase(t, true, false, msg, priv.PublicKey().Bytes(), stackitem.NewInterop("cheburek"), int64(curve))
|
||||
})
|
||||
t.Run("bad curve item", func(t *testing.T) {
|
||||
runCase(t, true, false, msg, priv.PublicKey().Bytes(), sign, stackitem.NewInterop("cheburek"))
|
||||
})
|
||||
t.Run("bad curve value", func(t *testing.T) {
|
||||
runCase(t, true, false, msg, priv.PublicKey().Bytes(), sign, new(big.Int).Add(big.NewInt(math.MaxInt64), big.NewInt(1)))
|
||||
})
|
||||
t.Run("unknown curve", func(t *testing.T) {
|
||||
runCase(t, true, false, msg, priv.PublicKey().Bytes(), sign, int64(123))
|
||||
})
|
||||
t.Run("invalid signature", func(t *testing.T) {
|
||||
s := priv.Sign(msg)
|
||||
s[0] = ^s[0]
|
||||
runCase(t, false, false, s, priv.PublicKey().Bytes(), msg, int64(curve))
|
||||
})
|
||||
t.Run("success", func(t *testing.T) {
|
||||
runCase(t, false, true, msg, priv.PublicKey().Bytes(), sign, int64(curve))
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue