Skip to content

Commit

Permalink
core: add murmur32 to CryptoLib native contract
Browse files Browse the repository at this point in the history
Close #2415.
  • Loading branch information
AnnaShaleva committed Apr 4, 2022
1 parent efe6d81 commit 8ca4ca3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
20 changes: 20 additions & 0 deletions pkg/core/native/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package native

import (
"crypto/elliptic"
"encoding/binary"
"errors"
"fmt"

Expand All @@ -14,6 +15,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/twmb/murmur3"
)

// Crypto represents CryptoLib contract.
Expand Down Expand Up @@ -46,6 +48,12 @@ func newCrypto() *Crypto {
md = newMethodAndPrice(c.ripemd160, 1<<15, callflag.NoneFlag)
c.AddMethod(md, desc)

desc = newDescriptor("murmur32", smartcontract.ByteArrayType,
manifest.NewParameter("data", smartcontract.ByteArrayType),
manifest.NewParameter("seed", smartcontract.IntegerType))
md = newMethodAndPrice(c.murmur32, 1<<13, callflag.NoneFlag)
c.AddMethod(md, desc)

desc = newDescriptor("verifyWithECDsa", smartcontract.BoolType,
manifest.NewParameter("message", smartcontract.ByteArrayType),
manifest.NewParameter("pubkey", smartcontract.ByteArrayType),
Expand All @@ -72,6 +80,18 @@ func (c *Crypto) ripemd160(_ *interop.Context, args []stackitem.Item) stackitem.
return stackitem.NewByteArray(hash.RipeMD160(bs).BytesBE())
}

func (c *Crypto) murmur32(_ *interop.Context, args []stackitem.Item) stackitem.Item {
bs, err := args[0].TryBytes()
if err != nil {
panic(err)
}
seed := toUint32(args[1])
h := murmur3.SeedSum32(seed, bs)
result := make([]byte, 4)
binary.LittleEndian.PutUint32(result, h)
return stackitem.NewByteArray(result)
}

func (c *Crypto) verifyWithECDsa(_ *interop.Context, args []stackitem.Item) stackitem.Item {
msg, err := args[0].TryBytes()
if err != nil {
Expand Down
21 changes: 21 additions & 0 deletions pkg/core/native/crypto_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package native

import (
"encoding/binary"
"encoding/hex"
"math"
"math/big"
Expand Down Expand Up @@ -43,6 +44,26 @@ func TestRIPEMD160(t *testing.T) {
})
}

func TestMurmur32(t *testing.T) {
c := newCrypto()
ic := &interop.Context{VM: vm.New()}

t.Run("bad arg type", func(t *testing.T) {
require.Panics(t, func() {
c.murmur32(ic, []stackitem.Item{stackitem.NewInterop(nil), stackitem.Make(5)})
})
})
t.Run("good", func(t *testing.T) {
// Example from the C# node:
// https://github.com/neo-project/neo/blob/2a64c1cc809d1ff4b3a573c7c22bffbbf69a738b/tests/neo.UnitTests/Cryptography/UT_Murmur32.cs#L18
data := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1}
seed := 10
expected := make([]byte, 4)
binary.LittleEndian.PutUint32(expected, 378574820)
require.Equal(t, expected, c.murmur32(ic, []stackitem.Item{stackitem.NewByteArray(data), stackitem.Make(seed)}).Value().([]byte))
})
}

func TestCryptoLibVerifyWithECDsa(t *testing.T) {
t.Run("R1", func(t *testing.T) {
testECDSAVerify(t, Secp256r1)
Expand Down
6 changes: 6 additions & 0 deletions pkg/interop/native/crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ func Ripemd160(b []byte) interop.Hash160 {
return neogointernal.CallWithToken(Hash, "ripemd160", int(contract.NoneFlag), b).(interop.Hash160)
}

// Murmur32 calls `murmur32` method of native CryptoLib contract and computes Murmur32 hash of b
// using the given seed.
func Murmur32(b []byte, seed int) []byte {
return neogointernal.CallWithToken(Hash, "murmur32", int(contract.NoneFlag), b, seed).([]byte)
}

// VerifyWithECDsa calls `verifyWithECDsa` method of native CryptoLib contract and checks that sig is
// correct msg's signature for a given pub (serialized public key on a given curve).
func VerifyWithECDsa(msg []byte, pub interop.PublicKey, sig interop.Signature, curve NamedCurve) bool {
Expand Down

0 comments on commit 8ca4ca3

Please sign in to comment.