From 8b274e536bf06c1f7252afd3f7e2b374205cd243 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=A8=20=E9=BB=8E?= Date: Thu, 11 Jun 2020 10:10:01 +0800 Subject: [PATCH 1/4] Add Hash160 and Hash256 to syscall --- .../SmartContract/ApplicationEngine.Crypto.cs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/neo/SmartContract/ApplicationEngine.Crypto.cs b/src/neo/SmartContract/ApplicationEngine.Crypto.cs index 9cb3a9abf4..85ee202a1a 100644 --- a/src/neo/SmartContract/ApplicationEngine.Crypto.cs +++ b/src/neo/SmartContract/ApplicationEngine.Crypto.cs @@ -5,6 +5,7 @@ using Neo.VM; using Neo.VM.Types; using System; +using System.Collections; namespace Neo.SmartContract { @@ -12,12 +13,35 @@ partial class ApplicationEngine { public const long ECDsaVerifyPrice = 0_01000000; + public static readonly InteropDescriptor Neo_Crypto_Hash160 = Register("Neo.Crypto.Hash160", nameof(Hash160), 0_01000000, TriggerType.All, CallFlags.None); + public static readonly InteropDescriptor Neo_Crypto_Hash256 = Register("Neo.Crypto.Hash256", nameof(Hash256), 0_01000000, TriggerType.All, CallFlags.None); public static readonly InteropDescriptor Neo_Crypto_SHA256 = Register("Neo.Crypto.SHA256", nameof(Sha256), 0_01000000, TriggerType.All, CallFlags.None); public static readonly InteropDescriptor Neo_Crypto_VerifyWithECDsaSecp256r1 = Register("Neo.Crypto.VerifyWithECDsaSecp256r1", nameof(VerifyWithECDsaSecp256r1), ECDsaVerifyPrice, TriggerType.All, CallFlags.None); public static readonly InteropDescriptor Neo_Crypto_VerifyWithECDsaSecp256k1 = Register("Neo.Crypto.VerifyWithECDsaSecp256k1", nameof(VerifyWithECDsaSecp256k1), ECDsaVerifyPrice, TriggerType.All, CallFlags.None); public static readonly InteropDescriptor Neo_Crypto_CheckMultisigWithECDsaSecp256r1 = Register("Neo.Crypto.CheckMultisigWithECDsaSecp256r1", nameof(CheckMultisigWithECDsaSecp256r1), 0, TriggerType.All, CallFlags.None); public static readonly InteropDescriptor Neo_Crypto_CheckMultisigWithECDsaSecp256k1 = Register("Neo.Crypto.CheckMultisigWithECDsaSecp256k1", nameof(CheckMultisigWithECDsaSecp256k1), 0, TriggerType.All, CallFlags.None); + internal byte[] Hash160(StackItem item) + { + ReadOnlySpan value = item switch + { + InteropInterface _interface => _interface.GetInterface().GetHashData(), + Null _ => ScriptContainer.GetHashData(), + _ => item.GetSpan() + }; + return value.Sha256().RIPEMD160(); + } + + internal byte[] Hash256(StackItem item) + { + ReadOnlySpan value = item switch + { + InteropInterface _interface => _interface.GetInterface().GetHashData(), + Null _ => ScriptContainer.GetHashData(), + _ => item.GetSpan() + }; + return value.Sha256().Sha256(); + } internal byte[] Sha256(StackItem item) { ReadOnlySpan value = item switch From 25c08d968a2c19853e106e47eefecab214123647 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=A8=20=E9=BB=8E?= Date: Thu, 11 Jun 2020 10:21:18 +0800 Subject: [PATCH 2/4] remove unnecessary reference --- src/neo/SmartContract/ApplicationEngine.Crypto.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/neo/SmartContract/ApplicationEngine.Crypto.cs b/src/neo/SmartContract/ApplicationEngine.Crypto.cs index 85ee202a1a..3cef61153d 100644 --- a/src/neo/SmartContract/ApplicationEngine.Crypto.cs +++ b/src/neo/SmartContract/ApplicationEngine.Crypto.cs @@ -5,7 +5,6 @@ using Neo.VM; using Neo.VM.Types; using System; -using System.Collections; namespace Neo.SmartContract { From b269b91d98653838c0f1bd8b35718c9d1e434608 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=A8=20=E9=BB=8E?= Date: Thu, 11 Jun 2020 10:32:18 +0800 Subject: [PATCH 3/4] format fix --- src/neo/SmartContract/ApplicationEngine.Crypto.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/neo/SmartContract/ApplicationEngine.Crypto.cs b/src/neo/SmartContract/ApplicationEngine.Crypto.cs index 3cef61153d..360d79ed0c 100644 --- a/src/neo/SmartContract/ApplicationEngine.Crypto.cs +++ b/src/neo/SmartContract/ApplicationEngine.Crypto.cs @@ -41,6 +41,7 @@ internal byte[] Hash256(StackItem item) }; return value.Sha256().Sha256(); } + internal byte[] Sha256(StackItem item) { ReadOnlySpan value = item switch From f1b8b19be881294920f67c31896b6ff31cedfce8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=A8=20=E9=BB=8E?= Date: Thu, 11 Jun 2020 15:17:35 +0800 Subject: [PATCH 4/4] provide RIPMED160 instead of Hash256 and Hash160 Add RIPMED160 UT --- src/neo/Cryptography/Helper.cs | 6 ++++++ .../SmartContract/ApplicationEngine.Crypto.cs | 18 +++--------------- .../Cryptography/UT_Cryptography_Helper.cs | 9 +++++++++ 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/neo/Cryptography/Helper.cs b/src/neo/Cryptography/Helper.cs index 3cedb53236..1f0c460a94 100644 --- a/src/neo/Cryptography/Helper.cs +++ b/src/neo/Cryptography/Helper.cs @@ -75,6 +75,12 @@ public static byte[] RIPEMD160(this byte[] value) return ripemd160.ComputeHash(value); } + public static byte[] RIPEMD160(this ReadOnlySpan value) + { + byte[] source = value.ToArray(); + return source.RIPEMD160(); + } + public static uint Murmur32(this byte[] value, uint seed) { using (Murmur3 murmur = new Murmur3(seed)) diff --git a/src/neo/SmartContract/ApplicationEngine.Crypto.cs b/src/neo/SmartContract/ApplicationEngine.Crypto.cs index 360d79ed0c..70533d900c 100644 --- a/src/neo/SmartContract/ApplicationEngine.Crypto.cs +++ b/src/neo/SmartContract/ApplicationEngine.Crypto.cs @@ -12,15 +12,14 @@ partial class ApplicationEngine { public const long ECDsaVerifyPrice = 0_01000000; - public static readonly InteropDescriptor Neo_Crypto_Hash160 = Register("Neo.Crypto.Hash160", nameof(Hash160), 0_01000000, TriggerType.All, CallFlags.None); - public static readonly InteropDescriptor Neo_Crypto_Hash256 = Register("Neo.Crypto.Hash256", nameof(Hash256), 0_01000000, TriggerType.All, CallFlags.None); + public static readonly InteropDescriptor Neo_Crypto_RIPEMD160 = Register("Neo.Crypto.RIPEMD160", nameof(RIPEMD160), 0_01000000, TriggerType.All, CallFlags.None); public static readonly InteropDescriptor Neo_Crypto_SHA256 = Register("Neo.Crypto.SHA256", nameof(Sha256), 0_01000000, TriggerType.All, CallFlags.None); public static readonly InteropDescriptor Neo_Crypto_VerifyWithECDsaSecp256r1 = Register("Neo.Crypto.VerifyWithECDsaSecp256r1", nameof(VerifyWithECDsaSecp256r1), ECDsaVerifyPrice, TriggerType.All, CallFlags.None); public static readonly InteropDescriptor Neo_Crypto_VerifyWithECDsaSecp256k1 = Register("Neo.Crypto.VerifyWithECDsaSecp256k1", nameof(VerifyWithECDsaSecp256k1), ECDsaVerifyPrice, TriggerType.All, CallFlags.None); public static readonly InteropDescriptor Neo_Crypto_CheckMultisigWithECDsaSecp256r1 = Register("Neo.Crypto.CheckMultisigWithECDsaSecp256r1", nameof(CheckMultisigWithECDsaSecp256r1), 0, TriggerType.All, CallFlags.None); public static readonly InteropDescriptor Neo_Crypto_CheckMultisigWithECDsaSecp256k1 = Register("Neo.Crypto.CheckMultisigWithECDsaSecp256k1", nameof(CheckMultisigWithECDsaSecp256k1), 0, TriggerType.All, CallFlags.None); - internal byte[] Hash160(StackItem item) + internal byte[] RIPEMD160(StackItem item) { ReadOnlySpan value = item switch { @@ -28,18 +27,7 @@ internal byte[] Hash160(StackItem item) Null _ => ScriptContainer.GetHashData(), _ => item.GetSpan() }; - return value.Sha256().RIPEMD160(); - } - - internal byte[] Hash256(StackItem item) - { - ReadOnlySpan value = item switch - { - InteropInterface _interface => _interface.GetInterface().GetHashData(), - Null _ => ScriptContainer.GetHashData(), - _ => item.GetSpan() - }; - return value.Sha256().Sha256(); + return value.RIPEMD160(); } internal byte[] Sha256(StackItem item) diff --git a/tests/neo.UnitTests/Cryptography/UT_Cryptography_Helper.cs b/tests/neo.UnitTests/Cryptography/UT_Cryptography_Helper.cs index 0e17d0913c..b2c5775903 100644 --- a/tests/neo.UnitTests/Cryptography/UT_Cryptography_Helper.cs +++ b/tests/neo.UnitTests/Cryptography/UT_Cryptography_Helper.cs @@ -133,6 +133,15 @@ public void TestSha256() resultStr.Should().Be("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"); } + [TestMethod] + public void TestRIPEMD160() + { + ReadOnlySpan value = Encoding.ASCII.GetBytes("hello world"); + byte[] result = value.RIPEMD160(); + string resultStr = result.ToHexString(); + resultStr.Should().Be("98c615784ccb5fe5936fbc0cbe9dfdb408d92f0f"); + } + [TestMethod] public void TestTest() {