Skip to content

Commit

Permalink
Add RIPMED60 to syscall in ApplicationEngine.Crypto (neo-project#1694)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
2 people authored and KickSeason committed Jun 15, 2020
1 parent 7fe2736 commit e5507b1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/neo/Cryptography/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ public static byte[] RIPEMD160(this byte[] value)
return ripemd160.ComputeHash(value);
}

public static byte[] RIPEMD160(this ReadOnlySpan<byte> value)
{
byte[] source = value.ToArray();
return source.RIPEMD160();
}

public static uint Murmur32(this byte[] value, uint seed)
{
using (Murmur3 murmur = new Murmur3(seed))
Expand Down
12 changes: 12 additions & 0 deletions src/neo/SmartContract/ApplicationEngine.Crypto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte> value = item switch
{
InteropInterface _interface => _interface.GetInterface<IVerifiable>().GetHashData(),
Null _ => ScriptContainer.GetHashData(),
_ => item.GetSpan()
};
return value.RIPEMD160();
}

internal byte[] Sha256(StackItem item)
{
ReadOnlySpan<byte> value = item switch
Expand Down
9 changes: 9 additions & 0 deletions tests/neo.UnitTests/Cryptography/UT_Cryptography_Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ public void TestSha256()
resultStr.Should().Be("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9");
}

[TestMethod]
public void TestRIPEMD160()
{
ReadOnlySpan<byte> value = Encoding.ASCII.GetBytes("hello world");
byte[] result = value.RIPEMD160();
string resultStr = result.ToHexString();
resultStr.Should().Be("98c615784ccb5fe5936fbc0cbe9dfdb408d92f0f");
}

[TestMethod]
public void TestTest()
{
Expand Down

0 comments on commit e5507b1

Please sign in to comment.