Skip to content

Commit

Permalink
Merge branch 'master' into Branch_lights_trycatch
Browse files Browse the repository at this point in the history
  • Loading branch information
lightszero committed May 2, 2020
2 parents 3089c6d + 5c488d9 commit 8d7a6e5
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 35 deletions.
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.1" />
<PackageReference Include="Neo" Version="3.0.0-CI00897" />
<PackageReference Include="Neo" Version="3.0.0-CI00901" />
</ItemGroup>

<ItemGroup>
Expand Down
25 changes: 21 additions & 4 deletions src/Neo.SmartContract.Framework/Services/Neo/Crypto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,28 @@ namespace Neo.SmartContract.Framework.Services.Neo
{
public static class Crypto
{
[Syscall("Neo.Crypto.SHA256")]
public extern static byte[] SHA256(byte[] value);

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

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

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

[Syscall("Neo.Crypto.ECDsa.Secp256k1.CheckMultiSig")]
public extern static bool CheckMultiSig(byte[] message, byte[][] pubkey, byte[][] signature);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public void Test_CreateStandardAccount()

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

// Good pubKey (uncompressed)
_engine.Reset();
Expand All @@ -175,15 +175,15 @@ public void Test_CreateStandardAccount()

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

[TestMethod]
public void Test_GetCallFlags()
{
_engine.Reset();
var result = _engine.ExecuteTestCaseStandard("getCallFlags").Pop();
StackItem wantResult = 0b00000111;
StackItem wantResult = 0b00001111;
Assert.AreEqual(wantResult, result);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ public static KeyPair GenerateKey(int privateKeyLength)
return new KeyPair(privateKey);
}

[TestMethod]
public void Test_SHA256()
{
var data = _engine.ScriptContainer.GetHashData();
_engine.Reset();
var result = _engine.ExecuteTestCaseStandard("sHA256", data);
Assert.AreEqual(VMState.HALT, _engine.State);
Assert.AreEqual(1, result.Count);

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

[TestMethod]
public void Test_VerifySignature()
{
Expand All @@ -56,7 +70,7 @@ public void Test_VerifySignature()
// False

_engine.Reset();
var result = _engine.ExecuteTestCaseStandard("verifySignature",
var result = _engine.ExecuteTestCaseStandard("secp256r1VerifySignature",
new ByteString(_key.PublicKey.EncodePoint(true)), new ByteString(new byte[64]));
Assert.AreEqual(VMState.HALT, _engine.State);
Assert.AreEqual(1, result.Count);
Expand All @@ -68,7 +82,7 @@ public void Test_VerifySignature()
// True

_engine.Reset();
result = _engine.ExecuteTestCaseStandard("verifySignature",
result = _engine.ExecuteTestCaseStandard("secp256r1VerifySignature",
new ByteString(_key.PublicKey.EncodePoint(true)), new ByteString(signature));
Assert.AreEqual(VMState.HALT, _engine.State);
Assert.AreEqual(1, result.Count);
Expand All @@ -87,7 +101,7 @@ public void Test_VerifySignatures()
// False

_engine.Reset();
var result = _engine.ExecuteTestCaseStandard("verifySignatures",
var result = _engine.ExecuteTestCaseStandard("secp256r1VerifySignatures",
new Array(new StackItem[] { new ByteString(_key.PublicKey.EncodePoint(true)) }),
new Array(new StackItem[] { new ByteString(new byte[64]) }));
Assert.AreEqual(VMState.HALT, _engine.State);
Expand All @@ -100,7 +114,7 @@ public void Test_VerifySignatures()
// True

_engine.Reset();
result = _engine.ExecuteTestCaseStandard("verifySignatures",
result = _engine.ExecuteTestCaseStandard("secp256r1VerifySignatures",
new Array(new StackItem[] { new ByteString(_key.PublicKey.EncodePoint(true)) }),
new Array(new StackItem[] { new ByteString(signature) }));
Assert.AreEqual(VMState.HALT, _engine.State);
Expand All @@ -120,7 +134,7 @@ public void Test_VerifySignaturesWithMessage()
// False

_engine.Reset();
var result = _engine.ExecuteTestCaseStandard("verifySignaturesWithMessage",
var result = _engine.ExecuteTestCaseStandard("secp256r1VerifySignaturesWithMessage",
new ByteString(new byte[0]),
new Array(new StackItem[] { new ByteString(_key.PublicKey.EncodePoint(true)) }),
new Array(new StackItem[] { new ByteString(new byte[64]) }));
Expand All @@ -134,7 +148,7 @@ public void Test_VerifySignaturesWithMessage()
// True

_engine.Reset();
result = _engine.ExecuteTestCaseStandard("verifySignaturesWithMessage",
result = _engine.ExecuteTestCaseStandard("secp256r1VerifySignaturesWithMessage",
new ByteString(_engine.ScriptContainer.GetHashData()),
new Array(new StackItem[] { new ByteString(_key.PublicKey.EncodePoint(true)) }),
new Array(new StackItem[] { new ByteString(signature) }));
Expand All @@ -155,7 +169,7 @@ public void Test_VerifySignatureWithMessage()
// False

_engine.Reset();
var result = _engine.ExecuteTestCaseStandard("verifySignatureWithMessage",
var result = _engine.ExecuteTestCaseStandard("secp256r1VerifySignatureWithMessage",
new ByteString(new byte[0]),
new ByteString(_key.PublicKey.EncodePoint(true)), new ByteString(signature));
Assert.AreEqual(VMState.HALT, _engine.State);
Expand All @@ -168,7 +182,7 @@ public void Test_VerifySignatureWithMessage()
// True

_engine.Reset();
result = _engine.ExecuteTestCaseStandard("verifySignatureWithMessage",
result = _engine.ExecuteTestCaseStandard("secp256r1VerifySignatureWithMessage",
new ByteString(_engine.ScriptContainer.GetHashData()),
new ByteString(_key.PublicKey.EncodePoint(true)), new ByteString(signature));
Assert.AreEqual(VMState.HALT, _engine.State);
Expand Down
33 changes: 22 additions & 11 deletions tests/Neo.SmartContract.Framework.UnitTests/SyscallTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Mono.Cecil;
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -22,17 +23,7 @@ public void TestAllSyscalls()

foreach (var type in module.Types)
{
foreach (var method in type.Methods)
{
foreach (var attr in method.CustomAttributes)
{
if (attr.AttributeType.FullName == expectedType)
{
var syscall = attr.ConstructorArguments[0].Value.ToString();
if (!list.Contains(syscall)) list.Add(syscall);
}
}
}
CheckType(type, expectedType, list);
}
}

Expand Down Expand Up @@ -62,5 +53,25 @@ public void TestAllSyscalls()
Assert.Fail($"Not implemented syscalls: {string.Join("\n-", notFound)}");
}
}

private void CheckType(TypeDefinition type, string expectedType, List<string> list)
{
foreach (var nested in type.NestedTypes)
{
CheckType(nested, expectedType, list);
}

foreach (var method in type.Methods)
{
foreach (var attr in method.CustomAttributes)
{
if (attr.AttributeType.FullName == expectedType)
{
var syscall = attr.ConstructorArguments[0].Value.ToString();
if (!list.Contains(syscall)) list.Add(syscall);
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,49 @@ namespace Neo.Compiler.MSIL.TestClasses
{
public class Contract_Crypto : SmartContract.Framework.SmartContract
{
public static bool VerifySignature(byte[] pubkey, byte[] signature)
public static byte[] SHA256(byte[] value)
{
return Crypto.ECDsaVerify(null, pubkey, signature);
return Crypto.SHA256(value);
}

public static bool VerifySignatureWithMessage(byte[] message, byte[] pubkey, byte[] signature)
public static bool Secp256r1VerifySignature(byte[] pubkey, byte[] signature)
{
return Crypto.ECDsaVerify(message, pubkey, signature);
return Crypto.ECDsa.Secp256r1.Verify(null, pubkey, signature);
}

public static bool VerifySignatures(byte[][] pubkeys, byte[][] signatures)
public static bool Secp256r1VerifySignatureWithMessage(byte[] message, byte[] pubkey, byte[] signature)
{
return Crypto.ECDsaCheckMultiSig(null, pubkeys, signatures);
return Crypto.ECDsa.Secp256r1.Verify(message, pubkey, signature);
}

public static bool VerifySignaturesWithMessage(byte[] message, byte[][] pubkeys, byte[][] signatures)
public static bool Secp256r1VerifySignatures(byte[][] pubkeys, byte[][] signatures)
{
return Crypto.ECDsaCheckMultiSig(message, pubkeys, signatures);
return Crypto.ECDsa.Secp256r1.CheckMultiSig(null, pubkeys, signatures);
}

public static bool Secp256r1VerifySignaturesWithMessage(byte[] message, byte[][] pubkeys, byte[][] signatures)
{
return Crypto.ECDsa.Secp256r1.CheckMultiSig(message, pubkeys, signatures);
}

public static bool Secp256k1VerifySignature(byte[] pubkey, byte[] signature)
{
return Crypto.ECDsa.Secp256k1.Verify(null, pubkey, signature);
}

public static bool Secp256k1VerifySignatureWithMessage(byte[] message, byte[] pubkey, byte[] signature)
{
return Crypto.ECDsa.Secp256k1.Verify(message, pubkey, signature);
}

public static bool Secp256k1VerifySignatures(byte[][] pubkeys, byte[][] signatures)
{
return Crypto.ECDsa.Secp256k1.CheckMultiSig(null, pubkeys, signatures);
}

public static bool Secp256k1VerifySignaturesWithMessage(byte[] message, byte[][] pubkeys, byte[][] signatures)
{
return Crypto.ECDsa.Secp256k1.CheckMultiSig(message, pubkeys, signatures);
}
}
}

0 comments on commit 8d7a6e5

Please sign in to comment.