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

Conversation

nan01ab
Copy link
Contributor

@nan01ab nan01ab commented Jan 12, 2025

Description

Murmur3 is not a secure hash algorithm, so it should not implement System.Security.Cryptography.HashAlgorithm, as this can lead to misunderstanding and misuse.

Murmur3 probably shouldn't be under the namespace Neo.Cryptography , but this PR doesn't change that.

Type of change

  • Optimization (the change is only an optimization)
  • Style (the change is only a code style for better maintenance or standard purpose)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@nan01ab nan01ab changed the title Murmur3 should not no cryptographic hash algorithm Murmur3 should not be cryptographic hash algorithm Jan 12, 2025
Copy link
Member

@shargon shargon left a comment

Choose a reason for hiding this comment

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

What's the problem?

@cschuchardt88
Copy link
Member

The class has nothing to do with security.

Hash functions are fundamental to modern cryptography. These functions map binary strings of an arbitrary length to small binary strings of a fixed length, known as hash values. A cryptographic hash function has the property that it is computationally infeasible to find two distinct inputs that hash to the same value. Hash functions are commonly used with digital signatures and for data integrity.

The hash is used as a unique value of fixed size representing a large amount of data. Hashes of two sets of data should match if the corresponding data also matches. Small changes to the data result in large unpredictable changes in the hash.

Dictionary:
cryp·tog·ra·phy
/kripˈtäɡrəfē/

the art of writing or solving codes.

@nan01ab
Copy link
Contributor Author

nan01ab commented Jan 13, 2025

The class has nothing to do with security.

Hash functions are fundamental to modern cryptography. These functions map binary strings of an arbitrary length to small binary strings of a fixed length, known as hash values. A cryptographic hash function has the property that it is computationally infeasible to find two distinct inputs that hash to the same value. Hash functions are commonly used with digital signatures and for data integrity.

The hash is used as a unique value of fixed size representing a large amount of data. Hashes of two sets of data should match if the corresponding data also matches. Small changes to the data result in large unpredictable changes in the hash.

Dictionary: cryp·tog·ra·phy /kripˈtäɡrəfē/

the art of writing or solving codes.

Great explanation.

But:

Hash functions are fundamental to modern cryptography.

These hash functions refer to the secure hash functions in cryptography(SHA -> Secure Hash Algorithm).
Such as SHA2, SHA3, RIPMD-160.

However, murmur32(as well as murmur128) is a NonCryptographicHashAlgorithm(https://learn.microsoft.com/en-us/dotnet/api/system.io.hashing.noncryptographichashalgorithm?view=net-9.0-pp, https://www.nuget.org/packages/System.IO.Hashing/ ).

It is wrong to implement murmur32 as System.Security.Cryptography.HashAlgorithm.

@nan01ab
Copy link
Contributor Author

nan01ab commented Jan 13, 2025

What's the problem?

Explained above.

shargon
shargon previously approved these changes Jan 13, 2025
/// Append data to murmur computation
/// </summary>
/// <param name="source">Source</param>
public void Append(ReadOnlySpan<byte> source)
Copy link
Contributor Author

@nan01ab nan01ab Jan 13, 2025

Choose a reason for hiding this comment

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

There is a problem with the implementation of murmur3 in neo.
So append is not supported yet.

Copy link
Member

Choose a reason for hiding this comment

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

Then we can remove also Initialize

Copy link
Contributor Author

@nan01ab nan01ab Jan 14, 2025

Choose a reason for hiding this comment

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

Then we can remove also Initialize

Yes. Initialize can be removed.
I keep Initialize because ComputeHash can be called multiple times for a Murmur3 instance.

Jim8y
Jim8y previously approved these changes Jan 14, 2025
@nan01ab nan01ab dismissed stale reviews from shargon and Jim8y via dfbfb95 January 14, 2025 05:51
Jim8y
Jim8y previously approved these changes Jan 14, 2025
shargon
shargon previously approved these changes Jan 14, 2025
src/Neo/Cryptography/Murmur32.cs Outdated Show resolved Hide resolved
@nan01ab nan01ab dismissed stale reviews from shargon and Jim8y via 4894dfc January 14, 2025 07:42
/// </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.

@shargon shargon requested a review from Jim8y January 26, 2025 10:14
@shargon shargon merged commit 806091d into neo-project:master Jan 27, 2025
6 of 7 checks passed
Jim8y added a commit to Jim8y/neo that referenced this pull request Jan 31, 2025
* master: (43 commits)
  Fix `GetAndChange` warnings (neo-project#3702)
  `Murmur3` should not be cryptographic hash algorithm (neo-project#3668)
  Test: add tests for native contract id (neo-project#3697)
  Update nugets (neo-project#3692)
  [Core P2P] fix the bug (neo-project#3695)
  Add hardfork HF_Echidna (neo-project#3454)
  Fix: add lock for RocksDbStore.Snapshot to keep same behavior as MemoryStore and LevelDbStore (neo-project#3689)
  Nullable rocks db (neo-project#3686)
  Nullable leveldb (neo-project#3685)
  Enforcement Compiler Warnings (neo-project#3687)
  [`Update`] Dotnet & Compiler Version (neo-project#3684)
  [`Add`]: LevelDB Benchmarks (neo-project#3667)
  [`Fix`]: Behavior when `keyPrefix` is null in different `IStore.Seek` impls. (neo-project#3682)
  Improve calculatenetworkfee (neo-project#3674)
  more 2025 (neo-project#3678)
  Nullable in Storage classes (neo-project#3670)
  readonly (neo-project#3676)
  [Fix] Set max entries for `VerifyProof` in `statePlugin` (neo-project#3675)
  Neo.json.benchmarks (neo-project#3673)
  Happy new year 2025 (neo-project#3677)
  ...

# Conflicts:
#	src/Neo/Neo.csproj
#	src/Neo/ProtocolSettings.cs
#	src/Neo/SmartContract/ApplicationEngine.cs
#	src/Neo/SmartContract/Native/NeoToken.cs
#	src/Neo/SmartContract/Native/RoleManagement.cs
#	tests/Neo.UnitTests/SmartContract/Native/UT_NativeContract.cs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants