From 358cf1150474ec651751bda3de677ae2f6847d63 Mon Sep 17 00:00:00 2001 From: lichen <48947753+Lichen9618@users.noreply.github.com> Date: Thu, 11 Jun 2020 23:03:05 +0800 Subject: [PATCH] Add RIPMED60 to syscall in ApplicationEngine.Crypto (#1694) * Add Hash160 and Hash256 to syscall * remove unnecessary reference * format fix * provide RIPMED160 instead of Hash256 and Hash160 Add RIPMED160 UT Co-authored-by: Erik Zhang --- src/neo/Cryptography/Helper.cs | 6 ++++++ src/neo/SmartContract/ApplicationEngine.Crypto.cs | 12 ++++++++++++ .../Cryptography/UT_Cryptography_Helper.cs | 9 +++++++++ 3 files changed, 27 insertions(+) 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 9cb3a9abf4..70533d900c 100644 --- a/src/neo/SmartContract/ApplicationEngine.Crypto.cs +++ b/src/neo/SmartContract/ApplicationEngine.Crypto.cs @@ -12,12 +12,24 @@ partial class ApplicationEngine { public const long ECDsaVerifyPrice = 0_01000000; + 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[] RIPEMD160(StackItem item) + { + ReadOnlySpan value = item switch + { + InteropInterface _interface => _interface.GetInterface().GetHashData(), + Null _ => ScriptContainer.GetHashData(), + _ => item.GetSpan() + }; + return value.RIPEMD160(); + } + internal byte[] Sha256(StackItem item) { ReadOnlySpan value = item switch 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() {