Skip to content

Commit

Permalink
Murmur128: process data in Little-Endian fashion (#2541)
Browse files Browse the repository at this point in the history
  • Loading branch information
fyrchik authored Jul 15, 2021
1 parent 3a8b15d commit 00805f6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/neo/Cryptography/Murmur128.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected override void HashCore(byte[] array, int ibStart, int cbSize)
int alignedLength = ibStart + (cbSize - remainder);
for (int i = ibStart; i < alignedLength; i += 16)
{
ulong k1 = BinaryPrimitives.ReadUInt64BigEndian(array.AsSpan(i));
ulong k1 = BinaryPrimitives.ReadUInt64LittleEndian(array.AsSpan(i));
k1 *= c1;
k1 = RotateLeft(k1, r1);
k1 *= c2;
Expand All @@ -52,7 +52,7 @@ protected override void HashCore(byte[] array, int ibStart, int cbSize)
H1 += H2;
H1 = H1 * m + n1;

ulong k2 = BinaryPrimitives.ReadUInt64BigEndian(array.AsSpan(i + 8));
ulong k2 = BinaryPrimitives.ReadUInt64LittleEndian(array.AsSpan(i + 8));
k2 *= c2;
k2 = RotateLeft(k2, r2);
k2 *= c1;
Expand Down
3 changes: 3 additions & 0 deletions tests/neo.UnitTests/Cryptography/UT_Murmur128.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public void TestHashCore()

array = Encoding.ASCII.GetBytes("hello world");
array.Murmur128(123u).ToHexString().ToString().Should().Be("e0a0632d4f51302c55e3b3e48d28795d");

array = "718f952132679baa9c5c2aa0d329fd2a".HexToBytes();
array.Murmur128(123u).ToHexString().ToString().Should().Be("9b4aa747ff0cf4e41b3d96251551c8ae");
}
}
}
20 changes: 10 additions & 10 deletions tests/neo.UnitTests/SmartContract/UT_ApplicationEngine.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public void TestGetRandomSameBlock()
var rand_9 = engine_2.GetRandom();
var rand_10 = engine_2.GetRandom();

rand_1.Should().Be(BigInteger.Parse("318788182164858154199132431451757857059"));
rand_2.Should().Be(BigInteger.Parse("282447758760362720671055450131457608779"));
rand_3.Should().Be(BigInteger.Parse("135807514681881442643162663784616300855"));
rand_4.Should().Be(BigInteger.Parse("312803243085521724655455828201034417069"));
rand_5.Should().Be(BigInteger.Parse("188525996996387253649513686803230761335"));
rand_1.Should().Be(BigInteger.Parse("225932872514876835587448704843370203748"));
rand_2.Should().Be(BigInteger.Parse("190129535548110356450238097068474508661"));
rand_3.Should().Be(BigInteger.Parse("48930406787011198493485648810190184269"));
rand_4.Should().Be(BigInteger.Parse("66199389469641263539889463157823839112"));
rand_5.Should().Be(BigInteger.Parse("217172703763162599519098299724476526911"));

rand_1.Should().Be(rand_6);
rand_2.Should().Be(rand_7);
Expand Down Expand Up @@ -79,11 +79,11 @@ public void TestGetRandomDifferentBlock()
var rand_9 = engine_2.GetRandom();
var rand_10 = engine_2.GetRandom();

rand_1.Should().Be(BigInteger.Parse("318788182164858154199132431451757857059"));
rand_2.Should().Be(BigInteger.Parse("282447758760362720671055450131457608779"));
rand_3.Should().Be(BigInteger.Parse("135807514681881442643162663784616300855"));
rand_4.Should().Be(BigInteger.Parse("312803243085521724655455828201034417069"));
rand_5.Should().Be(BigInteger.Parse("188525996996387253649513686803230761335"));
rand_1.Should().Be(BigInteger.Parse("225932872514876835587448704843370203748"));
rand_2.Should().Be(BigInteger.Parse("190129535548110356450238097068474508661"));
rand_3.Should().Be(BigInteger.Parse("48930406787011198493485648810190184269"));
rand_4.Should().Be(BigInteger.Parse("66199389469641263539889463157823839112"));
rand_5.Should().Be(BigInteger.Parse("217172703763162599519098299724476526911"));

rand_1.Should().NotBe(rand_6);
rand_2.Should().NotBe(rand_7);
Expand Down

0 comments on commit 00805f6

Please sign in to comment.