Skip to content

Commit

Permalink
Add hash256 and hash160, update ApplicationEngine (#293)
Browse files Browse the repository at this point in the history
* add hash256 and hash160, update ApplicationEngine

* Apply suggestions from code review

Co-authored-by: Luchuan <luchuan@ngd.neo.org>

* Fix UT

* Clean code

Co-authored-by: Vitor Nazário Coelho <vncoelho@gmail.com>
Co-authored-by: Erik Zhang <erik@neo.org>
Co-authored-by: Luchuan <luchuan@ngd.neo.org>
Co-authored-by: Shargon <shargon@gmail.com>
  • Loading branch information
5 people authored Jun 17, 2020
1 parent 3a7c670 commit 98c29df
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 38 deletions.
6 changes: 3 additions & 3 deletions src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ private int ConvertCall(OpCode src, NeoMethod to)
else if (calltype == 4)
{
ConvertPushDataArray(callhash, src, to);
Insert1(VM.OpCode.SYSCALL, "", to, BitConverter.GetBytes(InteropService.Contract.Call));
Insert1(VM.OpCode.SYSCALL, "", to, BitConverter.GetBytes(ApplicationEngine.System_Contract_Call));
}
else if (calltype == 5)
{
Expand All @@ -843,7 +843,7 @@ private int ConvertCall(OpCode src, NeoMethod to)

//a syscall
{
var bytes = BitConverter.GetBytes(InteropService.Runtime.Notify);
var bytes = BitConverter.GetBytes(ApplicationEngine.System_Runtime_Notify);
//byte[] outbytes = new byte[bytes.Length + 1];
//outbytes[0] = (byte)bytes.Length;
//Array.Copy(bytes, 0, outbytes, 1, bytes.Length);
Expand All @@ -855,7 +855,7 @@ private int ConvertCall(OpCode src, NeoMethod to)
{
ConvertPushNumber(callpcount, src, to);
Convert1by1(VM.OpCode.ROLL, null, to);
Convert1by1(VM.OpCode.SYSCALL, null, to, BitConverter.GetBytes(InteropService.Contract.Call));
Convert1by1(VM.OpCode.SYSCALL, null, to, BitConverter.GetBytes(ApplicationEngine.System_Contract_Call));
}
else if (calltype == 3)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.Compiler.MSIL/Neo.Compiler.MSIL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic" Version="3.4.0" />
<PackageReference Include="Mono.Cecil" Version="0.11.2" />
<PackageReference Include="Neo" Version="3.0.0-CI00926" />
<PackageReference Include="Neo" Version="3.0.0-CI00938" />
</ItemGroup>

<ItemGroup>
Expand Down
19 changes: 15 additions & 4 deletions src/Neo.SmartContract.Framework/Services/Neo/Crypto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,34 @@ public static class Crypto
[Syscall("Neo.Crypto.SHA256")]
public extern static byte[] SHA256(byte[] value);

[Syscall("Neo.Crypto.RIPEMD160")]
public extern static byte[] RIPEMD160(byte[] value);

[Syscall("Neo.Crypto.SHA256")]
[Syscall("Neo.Crypto.RIPEMD160")]
public extern static byte[] Hash160(byte[] value);

[Syscall("Neo.Crypto.SHA256")]
[Syscall("Neo.Crypto.SHA256")]
public extern static byte[] Hash256(byte[] value);

public static class ECDsa
{
public static class Secp256r1
{
[Syscall("Neo.Crypto.ECDsa.Secp256r1.Verify")]
[Syscall("Neo.Crypto.VerifyWithECDsaSecp256r1")]
public extern static bool Verify(byte[] message, byte[] pubkey, byte[] signature);

[Syscall("Neo.Crypto.ECDsa.Secp256r1.CheckMultiSig")]
[Syscall("Neo.Crypto.CheckMultisigWithECDsaSecp256r1")]
public extern static bool CheckMultiSig(byte[] message, byte[][] pubkey, byte[][] signature);
}

public static class Secp256k1
{
[Syscall("Neo.Crypto.ECDsa.Secp256k1.Verify")]
[Syscall("Neo.Crypto.VerifyWithECDsaSecp256k1")]
public extern static bool Verify(byte[] message, byte[] pubkey, byte[] signature);

[Syscall("Neo.Crypto.ECDsa.Secp256k1.CheckMultiSig")]
[Syscall("Neo.Crypto.CheckMultisigWithECDsaSecp256k1")]
public extern static bool CheckMultiSig(byte[] message, byte[][] pubkey, byte[][] signature);
}
}
Expand Down
12 changes: 2 additions & 10 deletions tests/Neo.Compiler.MSIL.UnitTests/Utils/TestEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Neo.VM.Types;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Neo.Compiler.MSIL.UnitTests.Utils
{
Expand All @@ -22,16 +21,9 @@ static TestEngine()
{
// Extract Native deploy syscall

Native_Deploy = (InteropDescriptor)typeof(InteropService)
.GetNestedType("Native", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static)
.GetField("Deploy")
.GetValue(null);
Native_Deploy = Neo_Native_Deploy;
}


public const int MaxStorageKeySize = 64;
public const int MaxStorageValueSize = ushort.MaxValue;

static readonly IDictionary<string, BuildScript> scriptsAll = new Dictionary<string, BuildScript>();

public readonly IDictionary<string, BuildScript> Scripts;
Expand Down Expand Up @@ -197,7 +189,7 @@ protected override bool OnSysCall(uint method)
{
{ Native_Deploy.Hash , Native_Deploy }
};
foreach (var m in InteropService.SupportedMethods())
foreach (var m in ApplicationEngine.Services)
{
callmethod[m] = m;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ public void Test_CreateCallDestroy()
Assert.AreEqual(1, result.Count);

var item = result.Pop();
Assert.IsTrue(item.Type == VM.Types.StackItemType.InteropInterface);
var ledger = (item as InteropInterface).GetInterface<Ledger.ContractState>();
Assert.AreEqual(manifest.Hash, ledger.ScriptHash);
Assert.IsInstanceOfType(item, typeof(Array));
var itemArray = item as Array;
Assert.AreEqual(script.finalNEF, itemArray[0]); // Script
Assert.AreEqual(false, itemArray[1]); // HasStorage
Assert.AreEqual(false, itemArray[2]); // Payable

// Call

Expand Down Expand Up @@ -104,9 +106,11 @@ public void Test_Update()
Assert.AreEqual(1, result.Count);

var item = result.Pop();
Assert.IsTrue(item.Type == VM.Types.StackItemType.InteropInterface);
var ledger = (item as InteropInterface).GetInterface<Ledger.ContractState>();
Assert.AreEqual(manifest.Hash, ledger.ScriptHash);
Assert.IsInstanceOfType(item, typeof(Array));
var itemArray = item as Array;
Assert.AreEqual(script.finalNEF, itemArray[0]); // Script
Assert.AreEqual(false, itemArray[1]); // HasStorage
Assert.AreEqual(false, itemArray[2]); // Payable

// Call & Update

Expand Down Expand Up @@ -164,7 +168,7 @@ public void Test_CreateStandardAccount()

var item = result.Pop();
Assert.IsTrue(item.Type == StackItemType.ByteString);
Assert.AreEqual("8b67a062c232ce87dc65cc69391ea909e721cd98", item.GetSpan().ToHexString());
Assert.AreEqual("3ae15fc83b48d9bb5c327e578e2f1d2100ba1b89", item.GetSpan().ToHexString());

// Good pubKey (uncompressed)

Expand All @@ -175,7 +179,7 @@ public void Test_CreateStandardAccount()

item = result.Pop();
Assert.IsTrue(item.Type == StackItemType.ByteString);
Assert.AreEqual("8b67a062c232ce87dc65cc69391ea909e721cd98", item.GetSpan().ToHexString());
Assert.AreEqual("3ae15fc83b48d9bb5c327e578e2f1d2100ba1b89", item.GetSpan().ToHexString());
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void Test_SHA256()
{
var data = _engine.ScriptContainer.GetHashData();
_engine.Reset();
var result = _engine.ExecuteTestCaseStandard("sHA256", data);
var result = _engine.ExecuteTestCaseStandard("SHA256", data);
Assert.AreEqual(VMState.HALT, _engine.State);
Assert.AreEqual(1, result.Count);

Expand All @@ -60,6 +60,48 @@ public void Test_SHA256()
Assert.AreEqual("293ba9cd0c05e23da15e39d29bcb8edfa5b2eeb29163a325c3229e81feed3d11", item.GetSpan().ToArray().ToHexString());
}

[TestMethod]
public void Test_RIPEMD160()
{
_engine.Reset();
var str = System.Text.Encoding.Default.GetBytes("hello world");
var result = _engine.ExecuteTestCaseStandard("RIPEMD160", str);
Assert.AreEqual(VMState.HALT, _engine.State);
Assert.AreEqual(1, result.Count);

var item = result.Pop();
Assert.IsInstanceOfType(item, typeof(ByteString));
Assert.AreEqual("98c615784ccb5fe5936fbc0cbe9dfdb408d92f0f", item.GetSpan().ToArray().ToHexString());
}

[TestMethod]
public void Test_HASH160()
{
_engine.Reset();
var str = System.Text.Encoding.Default.GetBytes("hello world");
var result = _engine.ExecuteTestCaseStandard("hash160", str);
Assert.AreEqual(VMState.HALT, _engine.State);
Assert.AreEqual(1, result.Count);

var item = result.Pop();
Assert.IsInstanceOfType(item, typeof(ByteString));
Assert.AreEqual("d7d5ee7824ff93f94c3055af9382c86c68b5ca92", item.GetSpan().ToArray().ToHexString());
}

[TestMethod]
public void Test_HASH256()
{
_engine.Reset();
var str = System.Text.Encoding.Default.GetBytes("hello world");
var result = _engine.ExecuteTestCaseStandard("hash256", str);
Assert.AreEqual(VMState.HALT, _engine.State);
Assert.AreEqual(1, result.Count);

var item = result.Pop();
Assert.IsInstanceOfType(item, typeof(ByteString));
Assert.AreEqual("bc62d4b80d9e36da29c16c5d4d9f11731f36052c72401a76c23c0fb5a9b74423", item.GetSpan().ToArray().ToHexString());
}

[TestMethod]
public void Test_VerifySignature()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ public void ScriptContainerTest()
Assert.AreEqual(1, result.Count);

var item = result.Pop() as InteropInterface;
var ver = item.GetInterface<IVerifiable>();
Assert.IsTrue(ver != null);
Assert.AreEqual(_engine.ScriptContainer, ver);
Assert.AreEqual(null, item);
}
}
}
16 changes: 8 additions & 8 deletions tests/Neo.SmartContract.Framework.UnitTests/SyscallTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ public void TestAllSyscalls()

var notFound = new List<string>();

foreach (var syscall in InteropService.SupportedMethods())
foreach (var syscall in ApplicationEngine.Services)
{
if (syscall.Method == "Neo.Native.Deploy") continue;
if (syscall.Method == "Neo.Native.Tokens.NEO") continue;
if (syscall.Method == "Neo.Native.Tokens.GAS") continue;
if (syscall.Method == "Neo.Native.Policy") continue;
if (syscall.Method == "Neo.Native.Call") continue;
if (syscall.Name == "Neo.Native.Deploy") continue;
if (syscall.Name == "Neo.Native.Tokens.NEO") continue;
if (syscall.Name == "Neo.Native.Tokens.GAS") continue;
if (syscall.Name == "Neo.Native.Policy") continue;
if (syscall.Name == "Neo.Native.Call") continue;

if (list.Remove(syscall.Method)) continue;
if (list.Remove(syscall.Name)) continue;

notFound.Add(syscall.Method);
notFound.Add(syscall.Name);
}

if (list.Count > 0)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
using Neo.SmartContract.Framework.Services.Neo;
using System.ComponentModel;

namespace Neo.Compiler.MSIL.TestClasses
{
public class Contract_Crypto : SmartContract.Framework.SmartContract
{
[DisplayName("SHA256")]
public static byte[] SHA256(byte[] value)
{
return Crypto.SHA256(value);
}

[DisplayName("RIPEMD160")]
public static byte[] RIPEMD160(byte[] value)
{
return Crypto.RIPEMD160(value);
}

public static byte[] Hash160(byte[] value)
{
return Crypto.Hash160(value);
}

public static byte[] Hash256(byte[] value)
{
return Crypto.Hash256(value);
}

public static bool Secp256r1VerifySignature(byte[] pubkey, byte[] signature)
{
return Crypto.ECDsa.Secp256r1.Verify(null, pubkey, signature);
Expand Down

0 comments on commit 98c29df

Please sign in to comment.