native: implement CryptoLib contract

This commit is contained in:
Evgeniy Stratonikov 2021-02-15 18:43:10 +03:00 committed by Anna Shaleva
parent 19a23a36e4
commit 100f2db3fb
22 changed files with 240 additions and 172 deletions

View file

@ -1,39 +0,0 @@
package crypto
import (
"github.com/nspcc-dev/neo-go/pkg/core/interop"
"github.com/nspcc-dev/neo-go/pkg/crypto"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)
// Sha256 returns sha256 hash of the data.
func Sha256(ic *interop.Context) error {
h, err := getMessageHash(ic, ic.VM.Estack().Pop().Item())
if err != nil {
return err
}
ic.VM.Estack().PushVal(h.BytesBE())
return nil
}
// RipeMD160 returns RipeMD160 hash of the data.
func RipeMD160(ic *interop.Context) error {
var msg []byte
item := ic.VM.Estack().Pop().Item()
switch val := item.(type) {
case *stackitem.Interop:
msg = val.Value().(crypto.Verifiable).GetSignedPart()
case stackitem.Null:
msg = ic.Container.GetSignedPart()
default:
var err error
if msg, err = val.TryBytes(); err != nil {
return err
}
}
h := hash.RipeMD160(msg).BytesBE()
ic.VM.Estack().PushVal(h)
return nil
}

View file

@ -1,69 +0,0 @@
package crypto
import (
"encoding/hex"
"testing"
"github.com/nspcc-dev/neo-go/pkg/core/interop"
"github.com/nspcc-dev/neo-go/pkg/crypto"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/stretchr/testify/require"
)
type testVerifiable []byte
var _ crypto.Verifiable = testVerifiable{}
func (v testVerifiable) GetSignedPart() []byte {
return v
}
func (v testVerifiable) GetSignedHash() util.Uint256 {
return hash.Sha256(v)
}
func testHash0100(t *testing.T, result string, interopFunc func(*interop.Context) error) {
t.Run("good", func(t *testing.T) {
bs := []byte{1, 0}
checkGood := func(t *testing.T, ic *interop.Context) {
require.NoError(t, interopFunc(ic))
require.Equal(t, 1, ic.VM.Estack().Len())
require.Equal(t, result, hex.EncodeToString(ic.VM.Estack().Pop().Bytes()))
}
t.Run("raw bytes", func(t *testing.T) {
ic := &interop.Context{VM: vm.New()}
ic.VM.Estack().PushVal(bs)
checkGood(t, ic)
})
t.Run("interop", func(t *testing.T) {
ic := &interop.Context{VM: vm.New()}
ic.VM.Estack().PushVal(stackitem.NewInterop(testVerifiable(bs)))
checkGood(t, ic)
})
t.Run("container", func(t *testing.T) {
ic := &interop.Context{VM: vm.New(), Container: testVerifiable(bs)}
ic.VM.Estack().PushVal(stackitem.Null{})
checkGood(t, ic)
})
})
t.Run("bad message", func(t *testing.T) {
ic := &interop.Context{VM: vm.New()}
ic.VM.Estack().PushVal(stackitem.NewArray(nil))
require.Error(t, interopFunc(ic))
})
}
func TestSHA256(t *testing.T) {
// 0x0100 hashes to 47dc540c94ceb704a23875c11273e16bb0b8a87aed84de911f2133568115f254
res := "47dc540c94ceb704a23875c11273e16bb0b8a87aed84de911f2133568115f254"
testHash0100(t, res, Sha256)
}
func TestRIPEMD160(t *testing.T) {
// 0x0100 hashes to 213492c0c6fc5d61497cf17249dd31cd9964b8a3
res := "213492c0c6fc5d61497cf17249dd31cd9964b8a3"
testHash0100(t, res, RipeMD160)
}

View file

@ -10,8 +10,6 @@ var (
ecdsaSecp256k1VerifyID = interopnames.ToID([]byte(interopnames.NeoCryptoVerifyWithECDsaSecp256k1))
ecdsaSecp256r1CheckMultisigID = interopnames.ToID([]byte(interopnames.NeoCryptoCheckMultisigWithECDsaSecp256r1))
ecdsaSecp256k1CheckMultisigID = interopnames.ToID([]byte(interopnames.NeoCryptoCheckMultisigWithECDsaSecp256k1))
sha256ID = interopnames.ToID([]byte(interopnames.NeoCryptoSHA256))
ripemd160ID = interopnames.ToID([]byte(interopnames.NeoCryptoRIPEMD160))
)
var cryptoInterops = []interop.Function{
@ -19,8 +17,6 @@ var cryptoInterops = []interop.Function{
{ID: ecdsaSecp256k1VerifyID, Func: ECDSASecp256k1Verify},
{ID: ecdsaSecp256r1CheckMultisigID, Func: ECDSASecp256r1CheckMultisig},
{ID: ecdsaSecp256k1CheckMultisigID, Func: ECDSASecp256k1CheckMultisig},
{ID: sha256ID, Func: Sha256},
{ID: ripemd160ID, Func: RipeMD160},
}
func init() {

View file

@ -51,8 +51,6 @@ const (
NeoCryptoVerifyWithECDsaSecp256k1 = "Neo.Crypto.VerifyWithECDsaSecp256k1"
NeoCryptoCheckMultisigWithECDsaSecp256r1 = "Neo.Crypto.CheckMultisigWithECDsaSecp256r1"
NeoCryptoCheckMultisigWithECDsaSecp256k1 = "Neo.Crypto.CheckMultisigWithECDsaSecp256k1"
NeoCryptoSHA256 = "Neo.Crypto.SHA256"
NeoCryptoRIPEMD160 = "Neo.Crypto.RIPEMD160"
)
var names = []string{
@ -105,6 +103,4 @@ var names = []string{
NeoCryptoVerifyWithECDsaSecp256k1,
NeoCryptoCheckMultisigWithECDsaSecp256r1,
NeoCryptoCheckMultisigWithECDsaSecp256k1,
NeoCryptoSHA256,
NeoCryptoRIPEMD160,
}