2020-04-20 08:52:05 +00:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"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/smartcontract/trigger"
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2020-06-03 12:55:06 +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 {
|
|
|
|
pkeys[i], err = keys.NewPrivateKey()
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func initCHECKMULTISIGVM(t *testing.T, n int, ik, is []int) *vm.VM {
|
|
|
|
buf := make([]byte, 5)
|
|
|
|
buf[0] = byte(opcode.SYSCALL)
|
|
|
|
binary.LittleEndian.PutUint32(buf[1:], ecdsaCheckMultisigID)
|
|
|
|
|
|
|
|
v := vm.New()
|
|
|
|
ic := &interop.Context{Trigger: trigger.Verification}
|
|
|
|
v.RegisterInteropGetter(GetInterop(ic))
|
|
|
|
v.LoadScript(buf)
|
|
|
|
msg := []byte("NEO - An Open Network For Smart Economy")
|
|
|
|
|
|
|
|
pubs, sigs, _, err := initCHECKMULTISIG(msg, n)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
pubs = subSlice(pubs, ik)
|
|
|
|
sigs = subSlice(sigs, is)
|
|
|
|
|
|
|
|
v.Estack().PushVal(sigs)
|
|
|
|
v.Estack().PushVal(pubs)
|
|
|
|
v.Estack().PushVal(msg)
|
|
|
|
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCHECKMULTISIGGood(t *testing.T, n int, is []int) {
|
|
|
|
v := initCHECKMULTISIGVM(t, n, nil, is)
|
|
|
|
|
|
|
|
require.NoError(t, v.Run())
|
|
|
|
assert.Equal(t, 1, v.Estack().Len())
|
|
|
|
assert.True(t, v.Estack().Pop().Bool())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCHECKMULTISIGGood(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}) })
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCHECKMULTISIGBad(t *testing.T, n int, ik, is []int) {
|
|
|
|
v := initCHECKMULTISIGVM(t, n, ik, is)
|
|
|
|
|
|
|
|
require.NoError(t, v.Run())
|
|
|
|
assert.Equal(t, 1, v.Estack().Len())
|
|
|
|
assert.False(t, v.Estack().Pop().Bool())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCHECKMULTISIGBad(t *testing.T) {
|
|
|
|
t.Run("1_1 wrong signature", func(t *testing.T) { testCHECKMULTISIGBad(t, 2, []int{0}, []int{1}) })
|
|
|
|
t.Run("3_2 wrong order", func(t *testing.T) { testCHECKMULTISIGBad(t, 3, []int{0, 2}, []int{2, 0}) })
|
|
|
|
t.Run("3_2 duplicate sig", func(t *testing.T) { testCHECKMULTISIGBad(t, 3, nil, []int{0, 0}) })
|
|
|
|
}
|