2020-04-20 08:52:05 +00:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2020-12-02 12:52:57 +00:00
|
|
|
"fmt"
|
2020-04-20 08:52:05 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-12-02 12:52:57 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/dao"
|
2021-02-05 08:25:22 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/fee"
|
2020-04-20 08:52:05 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
2020-12-02 12:52:57 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/storage"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2020-04-20 08:52:05 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
|
2021-03-09 15:11:21 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-04-20 08:52:05 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2020-06-03 12:55:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-04-20 08:52:05 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2021-03-09 14:38:41 +00:00
|
|
|
func initCHECKMULTISIG(msg []byte, n int) ([]stackitem.Item, []stackitem.Item, map[string]*keys.PublicKey, error) {
|
2020-04-20 08:52:05 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
keyMap := make(map[string]*keys.PublicKey)
|
|
|
|
pkeys := make([]*keys.PrivateKey, n)
|
2020-06-03 12:55:06 +00:00
|
|
|
pubs := make([]stackitem.Item, n)
|
2020-04-20 08:52:05 +00:00
|
|
|
for i := range pubs {
|
2021-03-09 14:38:41 +00:00
|
|
|
pkeys[i], err = keys.NewPrivateKey()
|
2020-04-20 08:52:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pk := pkeys[i].PublicKey()
|
|
|
|
data := pk.Bytes()
|
2020-06-03 12:55:06 +00:00
|
|
|
pubs[i] = stackitem.NewByteArray(data)
|
2020-04-20 08:52:05 +00:00
|
|
|
keyMap[string(data)] = pk
|
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
sigs := make([]stackitem.Item, n)
|
2020-04-20 08:52:05 +00:00
|
|
|
for i := range sigs {
|
|
|
|
sig := pkeys[i].Sign(msg)
|
2020-06-03 12:55:06 +00:00
|
|
|
sigs[i] = stackitem.NewByteArray(sig)
|
2020-04-20 08:52:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return pubs, sigs, keyMap, nil
|
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
func subSlice(arr []stackitem.Item, indices []int) []stackitem.Item {
|
2020-04-20 08:52:05 +00:00
|
|
|
if indices == nil {
|
|
|
|
return arr
|
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
result := make([]stackitem.Item, len(indices))
|
2020-04-20 08:52:05 +00:00
|
|
|
for i, j := range indices {
|
|
|
|
result[i] = arr[j]
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-03-09 15:11:21 +00:00
|
|
|
func initCheckMultisigVMNoArgs(container *transaction.Transaction) *vm.VM {
|
2020-04-20 08:52:05 +00:00
|
|
|
buf := make([]byte, 5)
|
|
|
|
buf[0] = byte(opcode.SYSCALL)
|
2021-03-09 15:11:21 +00:00
|
|
|
binary.LittleEndian.PutUint32(buf[1:], neoCryptoCheckMultisigID)
|
2020-04-20 08:52:05 +00:00
|
|
|
|
2021-03-09 15:11:21 +00:00
|
|
|
ic := &interop.Context{
|
|
|
|
Trigger: trigger.Verification,
|
|
|
|
Container: container,
|
|
|
|
}
|
2020-07-28 13:38:00 +00:00
|
|
|
Register(ic)
|
|
|
|
v := ic.SpawnVM()
|
2020-04-20 08:52:05 +00:00
|
|
|
v.LoadScript(buf)
|
2020-12-02 12:52:57 +00:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2021-03-09 14:38:41 +00:00
|
|
|
func initCHECKMULTISIGVM(t *testing.T, n int, ik, is []int) *vm.VM {
|
2021-03-09 15:11:21 +00:00
|
|
|
tx := transaction.New(netmode.UnitTestNet, []byte("NEO - An Open Network For Smart Economy"), 10)
|
|
|
|
tx.Signers = []transaction.Signer{{Account: util.Uint160{1, 2, 3}}}
|
|
|
|
tx.Scripts = []transaction.Witness{{}}
|
2020-04-20 08:52:05 +00:00
|
|
|
|
2021-03-09 15:11:21 +00:00
|
|
|
v := initCheckMultisigVMNoArgs(tx)
|
|
|
|
|
|
|
|
pubs, sigs, _, err := initCHECKMULTISIG(tx.GetSignedPart(), n)
|
2020-04-20 08:52:05 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
pubs = subSlice(pubs, ik)
|
|
|
|
sigs = subSlice(sigs, is)
|
|
|
|
|
|
|
|
v.Estack().PushVal(sigs)
|
|
|
|
v.Estack().PushVal(pubs)
|
|
|
|
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2021-03-09 14:38:41 +00:00
|
|
|
func testCHECKMULTISIGGood(t *testing.T, n int, is []int) {
|
|
|
|
v := initCHECKMULTISIGVM(t, n, nil, is)
|
2020-04-20 08:52:05 +00:00
|
|
|
|
|
|
|
require.NoError(t, v.Run())
|
|
|
|
assert.Equal(t, 1, v.Estack().Len())
|
|
|
|
assert.True(t, v.Estack().Pop().Bool())
|
|
|
|
}
|
|
|
|
|
2020-12-02 12:52:57 +00:00
|
|
|
func TestECDSASecp256r1CheckMultisigGood(t *testing.T) {
|
2021-03-09 14:38:41 +00:00
|
|
|
testCurveCHECKMULTISIGGood(t)
|
2020-12-02 12:52:57 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 14:38:41 +00:00
|
|
|
func testCurveCHECKMULTISIGGood(t *testing.T) {
|
|
|
|
t.Run("3_1", func(t *testing.T) { testCHECKMULTISIGGood(t, 3, []int{1}) })
|
|
|
|
t.Run("2_2", func(t *testing.T) { testCHECKMULTISIGGood(t, 2, []int{0, 1}) })
|
|
|
|
t.Run("3_3", func(t *testing.T) { testCHECKMULTISIGGood(t, 3, []int{0, 1, 2}) })
|
|
|
|
t.Run("3_2", func(t *testing.T) { testCHECKMULTISIGGood(t, 3, []int{0, 2}) })
|
|
|
|
t.Run("4_2", func(t *testing.T) { testCHECKMULTISIGGood(t, 4, []int{0, 2}) })
|
|
|
|
t.Run("10_7", func(t *testing.T) { testCHECKMULTISIGGood(t, 10, []int{2, 3, 4, 5, 6, 8, 9}) })
|
|
|
|
t.Run("12_9", func(t *testing.T) { testCHECKMULTISIGGood(t, 12, []int{0, 1, 4, 5, 6, 7, 8, 9}) })
|
2020-12-02 12:52:57 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 14:38:41 +00:00
|
|
|
func testCHECKMULTISIGBad(t *testing.T, isErr bool, n int, ik, is []int) {
|
|
|
|
v := initCHECKMULTISIGVM(t, n, ik, is)
|
2020-04-20 08:52:05 +00:00
|
|
|
|
2020-07-25 09:16:56 +00:00
|
|
|
if isErr {
|
|
|
|
require.Error(t, v.Run())
|
|
|
|
return
|
|
|
|
}
|
2020-04-20 08:52:05 +00:00
|
|
|
require.NoError(t, v.Run())
|
|
|
|
assert.Equal(t, 1, v.Estack().Len())
|
|
|
|
assert.False(t, v.Estack().Pop().Bool())
|
|
|
|
}
|
|
|
|
|
2020-12-02 12:52:57 +00:00
|
|
|
func TestECDSASecp256r1CheckMultisigBad(t *testing.T) {
|
2021-03-09 14:38:41 +00:00
|
|
|
testCurveCHECKMULTISIGBad(t)
|
2020-12-02 12:52:57 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 14:38:41 +00:00
|
|
|
func testCurveCHECKMULTISIGBad(t *testing.T) {
|
|
|
|
t.Run("1_1 wrong signature", func(t *testing.T) { testCHECKMULTISIGBad(t, false, 2, []int{0}, []int{1}) })
|
|
|
|
t.Run("3_2 wrong order", func(t *testing.T) { testCHECKMULTISIGBad(t, false, 3, []int{0, 2}, []int{2, 0}) })
|
|
|
|
t.Run("3_2 duplicate sig", func(t *testing.T) { testCHECKMULTISIGBad(t, false, 3, nil, []int{0, 0}) })
|
|
|
|
t.Run("1_2 too many signatures", func(t *testing.T) { testCHECKMULTISIGBad(t, true, 2, []int{0}, []int{0, 1}) })
|
2020-07-25 09:16:56 +00:00
|
|
|
t.Run("gas limit exceeded", func(t *testing.T) {
|
2021-03-09 14:38:41 +00:00
|
|
|
v := initCHECKMULTISIGVM(t, 1, []int{0}, []int{0})
|
2021-02-05 08:25:22 +00:00
|
|
|
v.GasLimit = fee.ECDSAVerifyPrice - 1
|
2020-07-25 09:16:56 +00:00
|
|
|
require.Error(t, v.Run())
|
|
|
|
})
|
2020-12-02 12:52:57 +00:00
|
|
|
|
|
|
|
msg := []byte("NEO - An Open Network For Smart Economy")
|
2021-03-09 14:38:41 +00:00
|
|
|
pubs, sigs, _, err := initCHECKMULTISIG(msg, 1)
|
2020-12-02 12:52:57 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
arr := stackitem.NewArray([]stackitem.Item{stackitem.NewArray(nil)})
|
2021-03-09 15:11:21 +00:00
|
|
|
tx := transaction.New(netmode.UnitTestNet, []byte("NEO - An Open Network For Smart Economy"), 10)
|
|
|
|
tx.Signers = []transaction.Signer{{Account: util.Uint160{1, 2, 3}}}
|
|
|
|
tx.Scripts = []transaction.Witness{{}}
|
2020-12-02 12:52:57 +00:00
|
|
|
|
|
|
|
t.Run("invalid public keys", func(t *testing.T) {
|
2021-03-09 15:11:21 +00:00
|
|
|
v := initCheckMultisigVMNoArgs(tx)
|
2020-12-02 12:52:57 +00:00
|
|
|
v.Estack().PushVal(sigs)
|
|
|
|
v.Estack().PushVal(arr)
|
|
|
|
require.Error(t, v.Run())
|
|
|
|
})
|
|
|
|
t.Run("invalid signatures", func(t *testing.T) {
|
2021-03-09 15:11:21 +00:00
|
|
|
v := initCheckMultisigVMNoArgs(tx)
|
2020-12-02 12:52:57 +00:00
|
|
|
v.Estack().PushVal(arr)
|
|
|
|
v.Estack().PushVal(pubs)
|
|
|
|
require.Error(t, v.Run())
|
|
|
|
})
|
2020-04-20 08:52:05 +00:00
|
|
|
}
|
2021-03-04 17:56:54 +00:00
|
|
|
|
|
|
|
func TestCheckSig(t *testing.T) {
|
|
|
|
priv, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
verifyFunc := ECDSASecp256r1CheckSig
|
|
|
|
d := dao.NewSimple(storage.NewMemoryStore(), netmode.UnitTestNet, false)
|
|
|
|
ic := &interop.Context{DAO: dao.NewCached(d)}
|
|
|
|
runCase := func(t *testing.T, isErr bool, result interface{}, args ...interface{}) {
|
|
|
|
ic.SpawnVM()
|
|
|
|
for i := range args {
|
|
|
|
ic.VM.Estack().PushVal(args[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
func() {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
err = fmt.Errorf("panic: %v", r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
err = verifyFunc(ic)
|
|
|
|
}()
|
|
|
|
|
|
|
|
if isErr {
|
|
|
|
require.Error(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, ic.VM.Estack().Len())
|
|
|
|
require.Equal(t, result, ic.VM.Estack().Pop().Value().(bool))
|
|
|
|
}
|
|
|
|
|
|
|
|
tx := transaction.New(netmode.UnitTestNet, []byte{0, 1, 2}, 1)
|
|
|
|
msg := tx.GetSignedPart()
|
|
|
|
ic.Container = tx
|
|
|
|
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
sign := priv.Sign(msg)
|
|
|
|
runCase(t, false, true, sign, priv.PublicKey().Bytes())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("missing argument", func(t *testing.T) {
|
|
|
|
runCase(t, true, false)
|
|
|
|
sign := priv.Sign(msg)
|
|
|
|
runCase(t, true, false, sign)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("invalid signature", func(t *testing.T) {
|
|
|
|
sign := priv.Sign(msg)
|
|
|
|
sign[0] = ^sign[0]
|
|
|
|
runCase(t, false, false, sign, priv.PublicKey().Bytes())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("invalid public key", func(t *testing.T) {
|
|
|
|
sign := priv.Sign(msg)
|
|
|
|
pub := priv.PublicKey().Bytes()
|
|
|
|
pub[0] = 0xFF // invalid prefix
|
|
|
|
runCase(t, true, false, sign, pub)
|
|
|
|
})
|
|
|
|
}
|