Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Murmur3 should not be cryptographic hash algorithm #3668

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/Neo/Cryptography/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using Org.BouncyCastle.Crypto.Modes;
using Org.BouncyCastle.Crypto.Parameters;
using System;
using System.Buffers.Binary;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -63,8 +62,7 @@ public static byte[] RIPEMD160(this ReadOnlySpan<byte> value)
/// <returns>The computed hash code.</returns>
public static uint Murmur32(this byte[] value, uint seed)
{
using Murmur32 murmur = new(seed);
return BinaryPrimitives.ReadUInt32LittleEndian(murmur.ComputeHash(value));
return Neo.Cryptography.Murmur32.HashToUInt32(value, seed);
}

/// <summary>
Expand All @@ -75,10 +73,7 @@ public static uint Murmur32(this byte[] value, uint seed)
/// <returns>The computed hash code.</returns>
public static uint Murmur32(this ReadOnlySpan<byte> value, uint seed)
{
Span<byte> buffer = stackalloc byte[sizeof(uint)];
using Murmur32 murmur = new(seed);
murmur.TryComputeHash(value, buffer, out _);
return BinaryPrimitives.ReadUInt32LittleEndian(buffer);
return Neo.Cryptography.Murmur32.HashToUInt32(value, seed);
}

/// <summary>
Expand Down
75 changes: 50 additions & 25 deletions src/Neo/Cryptography/Murmur32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@

using System;
using System.Buffers.Binary;
using System.Runtime.CompilerServices;

namespace Neo.Cryptography
{
/// <summary>
/// Computes the murmur hash for the input data.
/// <remarks>Murmur32 is a non-cryptographic hash function.</remarks>
/// </summary>
public sealed class Murmur32 : System.Security.Cryptography.HashAlgorithm
public sealed class Murmur32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use NonCryptographicHashAlgorithm as inherit class

Copy link
Contributor Author

@nan01ab nan01ab Jan 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More updates are needed, because Append not supported.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean? You can still use append.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean? You can still use append.

The current murmur32 impl in neo doesn’t support multiple Append operations

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you can't use it in NonCryptographicHashAlgorithm?

Copy link
Member

@shargon shargon Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the current logic does not allow this interface, feel free to create a new PR for this purpose

Copy link
Member

@cschuchardt88 cschuchardt88 Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well then I don't find this as a fix. Using these interfaces makes the hash a lot faster to process data. I want to see a benchmark of this new implementation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic is the same, should be faster but never slower, why do you think it will be slower?

Copy link
Member

@cschuchardt88 cschuchardt88 Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic is the same, should be faster but never slower, why do you think it will be slower?

Because of the class interfaces use an engine for fast hashing for huge amounts of data up to 2GB.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well then I don't find this as a fix. Using these interfaces makes the hash a lot faster to process data. I want to see a benchmark of this new implementation.

No significant performance difference.

{
private const uint c1 = 0xcc9e2d51;
private const uint c2 = 0x1b873593;
Expand All @@ -31,7 +33,7 @@ public sealed class Murmur32 : System.Security.Cryptography.HashAlgorithm
private int length;

public const int HashSizeInBits = 32;
public override int HashSize => HashSizeInBits;
public int HashSize => HashSizeInBits;

/// <summary>
/// Initializes a new instance of the <see cref="Murmur32"/> class with the specified seed.
Expand All @@ -40,16 +42,10 @@ public sealed class Murmur32 : System.Security.Cryptography.HashAlgorithm
public Murmur32(uint seed)
{
this.seed = seed;
HashSizeValue = HashSizeInBits;
Initialize();
}

protected override void HashCore(byte[] array, int ibStart, int cbSize)
{
HashCore(array.AsSpan(ibStart, cbSize));
}

protected override void HashCore(ReadOnlySpan<byte> source)
private void HashCore(ReadOnlySpan<byte> source)
{
length += source.Length;
for (; source.Length >= 4; source = source[4..])
Expand Down Expand Up @@ -78,30 +74,59 @@ protected override void HashCore(ReadOnlySpan<byte> source)
}
}

protected override byte[] HashFinal()
private uint GetCurrentHashUInt32()
{
byte[] buffer = new byte[sizeof(uint)];
TryHashFinal(buffer, out _);
return buffer;
var state = hash ^ (uint)length;
state ^= state >> 16;
state *= 0x85ebca6b;
state ^= state >> 13;
state *= 0xc2b2ae35;
state ^= state >> 16;
return state;
}

protected override bool TryHashFinal(Span<byte> destination, out int bytesWritten)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Initialize()
nan01ab marked this conversation as resolved.
Show resolved Hide resolved
{
hash ^= (uint)length;
hash ^= hash >> 16;
hash *= 0x85ebca6b;
hash ^= hash >> 13;
hash *= 0xc2b2ae35;
hash ^= hash >> 16;
hash = seed;
length = 0;
}

bytesWritten = Math.Min(destination.Length, sizeof(uint));
return BinaryPrimitives.TryWriteUInt32LittleEndian(destination, hash);
/// <summary>
/// Computes the murmur hash for the input data and resets the state.
/// </summary>
/// <param name="data">The input to compute the hash code for.</param>
/// <returns>The computed hash code in byte[4].</returns>
public byte[] ComputeHash(ReadOnlySpan<byte> data)
{
var buffer = new byte[HashSizeInBits / 8];
BinaryPrimitives.WriteUInt32LittleEndian(buffer, ComputeHashUInt32(data));
return buffer;
}

public override void Initialize()
/// <summary>
/// Computes the murmur hash for the input data and resets the state.
/// </summary>
/// <param name="data">The input to compute the hash code for.</param>
/// <returns>The computed hash code in uint.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint ComputeHashUInt32(ReadOnlySpan<byte> data)
{
hash = seed;
length = 0;
HashCore(data);

var state = GetCurrentHashUInt32();
Initialize();
return state;
}

/// <summary>
/// Computes the murmur hash for the input data.
/// </summary>
/// <param name="data">The input to compute the hash code for.</param>
/// <param name="seed">The seed used by the murmur algorithm.</param>
/// <returns>The computed hash code in uint.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint HashToUInt32(ReadOnlySpan<byte> data, uint seed)
=> new Murmur32(seed).ComputeHashUInt32(data);
}
}
2 changes: 1 addition & 1 deletion src/Neo/SmartContract/Native/CryptoLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static byte[] Sha256(byte[] data)
[ContractMethod(CpuFee = 1 << 13)]
public static byte[] Murmur32(byte[] data, uint seed)
{
using Murmur32 murmur = new(seed);
Murmur32 murmur = new(seed);
AnnaShaleva marked this conversation as resolved.
Show resolved Hide resolved
return murmur.ComputeHash(data);
}

Expand Down
18 changes: 11 additions & 7 deletions tests/Neo.UnitTests/Cryptography/UT_Murmur32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,28 @@ public void TestGetHashSize()
}

[TestMethod]
public void TestHashCore()
public void TestHashToUInt32()
{
byte[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1 };
array.Murmur32(10u).Should().Be(378574820u);
}

[TestMethod]
public void TestTryComputeHash()
public void TestComputeHash()
{
var murmur3 = new Murmur32(10u);
var buffer = new byte[murmur3.HashSize / 8];
var data = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1 };

var ok = murmur3.TryComputeHash(data, buffer, out _);
ok.Should().BeTrue();

var buffer = murmur3.ComputeHash(data);
var hash = BinaryPrimitives.ReadUInt32LittleEndian(buffer);
hash.Should().Be(378574820u);
}

[TestMethod]
public void TestComputeHashUInt32()
{
var murmur3 = new Murmur32(10u);
var hash = murmur3.ComputeHashUInt32("hello worldhello world"u8.ToArray());
hash.Should().Be(60539726u);
}
}
}
Loading