Skip to content

Commit 897ba8f

Browse files
authored
Add AppContext switch to allow hash seed to be deterministic (#79)
1 parent 18416cc commit 897ba8f

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

src/Directory.Build.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
<PropertyGroup Label="ProductInfo">
1313
<Authors>Nethermind</Authors>
1414
<Copyright>Demerzel Solutions Limited</Copyright>
15-
<VersionPrefix>1.4.0</VersionPrefix>
16-
<VersionSuffix></VersionSuffix>
15+
<VersionPrefix>1.5.0</VersionPrefix>
16+
<VersionSuffix>preview.1</VersionSuffix>
1717
</PropertyGroup>
1818

1919
</Project>

src/Nethermind.Int256.Tests/UInt256Tests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,4 +852,14 @@ public void ComparisonOperators((BigInteger A, BigInteger B) test)
852852
(uint256a == b).Should().Be(test.A == b);
853853
}
854854
}
855+
856+
[NonParallelizable]
857+
[TestCase(false)]
858+
[TestCase(true)]
859+
public void Shoud_respect_appcontext_switch(bool useHashCodeRandomizer)
860+
{
861+
AppContext.SetSwitch("Nethermind.Int256.UseHashCodeRandomizer", useHashCodeRandomizer);
862+
863+
Assert.That(UInt256.UseHashCodeRandomizer, Is.EqualTo(useHashCodeRandomizer));
864+
}
855865
}

src/Nethermind.Int256/UInt256.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Runtime.Intrinsics;
1111
using System.Runtime.Intrinsics.Arm;
1212
using System.Runtime.Intrinsics.X86;
13+
using System.Security.Cryptography;
1314

1415
[assembly: InternalsVisibleTo("Nethermind.Int256.Tests")]
1516

@@ -22,7 +23,13 @@ namespace Nethermind.Int256;
2223
// Ensure that hashes are different for every run of the node and every node, so if are any hash collisions on
2324
// one node they will not be the same on another node or across a restart so hash collision cannot be used to degrade
2425
// the performance of the network as a whole.
25-
private static readonly uint s_instanceRandom = (uint)System.Security.Cryptography.RandomNumberGenerator.GetInt32(int.MinValue, int.MaxValue);
26+
// Constant by default for zkVM-compatibility -- subject to change in the near feature
27+
private static readonly uint _hashSeed = UseHashCodeRandomizer
28+
? (uint)RandomNumberGenerator.GetInt32(int.MinValue, int.MaxValue)
29+
: 2098026241U; // just a random prime number
30+
31+
[FeatureSwitchDefinition("Nethermind.Int256.UseHashCodeRandomizer")]
32+
public static bool UseHashCodeRandomizer => AppContext.TryGetSwitch("Nethermind.Int256.UseHashCodeRandomizer", out var useHashCodeRandomizer) && useHashCodeRandomizer;
2633

2734
public static readonly UInt256 Zero = 0ul;
2835
public static readonly UInt256 One = 1ul;
@@ -1396,7 +1403,7 @@ public override int GetHashCode()
13961403
// Very fast hardware accelerated non-cryptographic hash function
13971404

13981405
// Start with instance random, length and first ulong as seed
1399-
uint hash = BitOperations.Crc32C(s_instanceRandom, u0);
1406+
uint hash = BitOperations.Crc32C(_hashSeed, u0);
14001407

14011408
// Crc32C is 3 cycle latency, 1 cycle throughput
14021409
// So we use same initial 3 times to not create a dependency chain

0 commit comments

Comments
 (0)