-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Add random number to neo #2477
Merged
Merged
Add random number to neo #2477
Changes from 56 commits
Commits
Show all changes
65 commits
Select commit
Hold shift + click to select a range
b3680dc
Add Verifiable Random Function and test cases
Jim8y 6579546
update the code format.
Jim8y 192e553
Merge branch 'master' into master
shargon bcb863a
Remove empty lines
shargon a525e77
Remove empty lines
shargon 7b99fad
fix TODO by throwing exception
Jim8y e47e565
Merge branch 'master' of github.com:Liaojinghui/neo
Jim8y 1391184
add a `nonce` field to the header
Jim8y 80fff9d
Add comment to GetRandom
Jim8y cc1b9f7
update comment
Jim8y 1b2f912
change pubkey type to ECPoint
Jim8y 10e7b6d
Add exception
Jim8y 4ecdc48
Remove VRF from neo core to dBFT plugin
Jim8y d8f2bb2
Remove empty line
erikzhang d20bd0d
add suppoort to multiple random numbers
Jim8y 110db72
update test
Jim8y 0126393
Add extra random check
Jim8y bb3e29d
fix mistake
Jim8y cd23177
update random algorithm
Jim8y d58a3ce
Clean code
shargon 99fa4b7
Add extra check to next_nonce
Jim8y 5ed2940
fix error
Jim8y f52c2b2
Add more test to test random number
Jim8y 60086e3
Add extra check to make sure the next_once will be reinitialized when…
Jim8y 798cafc
Variable name style consistency
Jim8y 91a8836
Remove `nonce` from the header.
Jim8y 6a1ba3d
update test
Jim8y 11ba5b7
fix test error. So weird the error does not exist in my local environ…
Jim8y 1ba655f
get the nonce_tx at index 0
Jim8y 9470311
Set next_nonce to null if there is no valid getrandom call in the per…
Jim8y 728f479
update runtime test
Jim8y 62eda32
Set the ECCurve.N public since VRFin the dBFT need to use this value.
Jim8y a0ee63e
Merge branch 'master' into vrf
shargon e62b56b
add nonce to header
Jim8y 303bbbb
nextnonce : ulong to replace byte[]
Jim8y e5aa633
format
erikzhang d0c81f7
add nonce to `trimmedblock`
Jim8y c48adbc
Merge branch 'vrf' of https://github.com/Liaojinghui/neo into vrf
Jim8y 491df4a
update comment
Jim8y 2cec716
add `Murmur128`
Jim8y 73b989c
add more test to `Murmur128`
Jim8y 3087243
fix format
Jim8y e43c150
`ulong` to `hexstring` in `ToJson`
Jim8y 714bac5
update nonce.
Jim8y 3d3d3ed
update format
Jim8y 4a508ae
update `getrandom`
Jim8y 9359dff
update comment
Jim8y 4360d00
Update src/neo/Network/P2P/Payloads/Header.cs
Jim8y 8a059bb
update unite test
Jim8y 2fab87f
Update src/neo/Network/P2P/Payloads/Header.cs
Jim8y 866ffe4
update test
Jim8y bcd6b27
Merge branch 'vrf' of https://github.com/Liaojinghui/neo into vrf
Jim8y 874e633
Merge branch 'master' into vrf
Qiao-Jin f5b3bf1
Update `GetRandom`
Jim8y 1ca4244
update comments
Jim8y 8f89321
update comments and format
Jim8y 654e37b
Improve
erikzhang 5258a1b
More improve
erikzhang ef41ef2
Fix UT
erikzhang daa642b
update `nonce` test cases
Jim8y ac35197
Merge branch 'vrf' of https://github.com/Liaojinghui/neo into vrf
Jim8y 460f0a6
update comments
Jim8y 77c7775
Format UT
shargon 2c6be15
Merge branch 'master' into vrf
shargon 30f33d0
Merge branch 'master' into vrf
erikzhang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
using System; | ||
using System.Buffers.Binary; | ||
using System.Runtime.CompilerServices; | ||
using System.Security.Cryptography; | ||
|
||
namespace Neo.Cryptography | ||
{ | ||
/// <summary> | ||
/// Computes the 128 bits murmur hash for the input data. | ||
/// </summary> | ||
public sealed class Murmur128 : HashAlgorithm | ||
{ | ||
private const ulong c1 = 0x87c37b91114253d5; | ||
private const ulong c2 = 0x4cf5ad432745937f; | ||
private const int r1 = 31; | ||
private const int r2 = 33; | ||
private const uint m = 5; | ||
private const uint n1 = 0x52dce729; | ||
private const uint n2 = 0x38495ab5; | ||
|
||
private readonly uint seed; | ||
private int length; | ||
|
||
public override int HashSize => 128; | ||
|
||
private ulong H1 { get; set; } | ||
private ulong H2 { get; set; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="Murmur128"/> class with the specified seed. | ||
/// </summary> | ||
/// <param name="seed">The seed to be used.</param> | ||
public Murmur128(uint seed) | ||
{ | ||
this.seed = seed; | ||
Initialize(); | ||
} | ||
|
||
protected override void HashCore(byte[] array, int ibStart, int cbSize) | ||
{ | ||
length += cbSize; | ||
int remainder = cbSize & 15; | ||
int alignedLength = ibStart + (cbSize - remainder); | ||
for (int i = ibStart; i < alignedLength; i += 16) | ||
{ | ||
ulong k1 = BinaryPrimitives.ReadUInt64BigEndian(array.AsSpan(i)); | ||
k1 *= c1; | ||
k1 = RotateLeft(k1, r1); | ||
k1 *= c2; | ||
H1 ^= k1; | ||
H1 = RotateLeft(H1, 27); | ||
H1 += H2; | ||
H1 = H1 * m + n1; | ||
|
||
ulong k2 = BinaryPrimitives.ReadUInt64BigEndian(array.AsSpan(i + 8)); | ||
k2 *= c2; | ||
k2 = RotateLeft(k2, r2); | ||
k2 *= c1; | ||
H2 ^= k2; | ||
H2 = RotateLeft(H2, 31); | ||
H2 += H1; | ||
H2 = H2 * m + n2; | ||
} | ||
|
||
if (remainder > 0) | ||
{ | ||
ulong remainingBytesL = 0, remainingBytesH = 0; | ||
switch (remainder) | ||
{ | ||
case 15: remainingBytesH ^= (ulong)array[alignedLength + 14] << 48; goto case 14; | ||
case 14: remainingBytesH ^= (ulong)array[alignedLength + 13] << 40; goto case 13; | ||
case 13: remainingBytesH ^= (ulong)array[alignedLength + 12] << 32; goto case 12; | ||
case 12: remainingBytesH ^= (ulong)array[alignedLength + 11] << 24; goto case 11; | ||
case 11: remainingBytesH ^= (ulong)array[alignedLength + 10] << 16; goto case 10; | ||
case 10: remainingBytesH ^= (ulong)array[alignedLength + 9] << 8; goto case 9; | ||
case 9: remainingBytesH ^= (ulong)array[alignedLength + 8] << 0; goto case 8; | ||
case 8: remainingBytesL ^= (ulong)array[alignedLength + 7] << 56; goto case 7; | ||
case 7: remainingBytesL ^= (ulong)array[alignedLength + 6] << 48; goto case 6; | ||
case 6: remainingBytesL ^= (ulong)array[alignedLength + 5] << 40; goto case 5; | ||
case 5: remainingBytesL ^= (ulong)array[alignedLength + 4] << 32; goto case 4; | ||
case 4: remainingBytesL ^= (ulong)array[alignedLength + 3] << 24; goto case 3; | ||
case 3: remainingBytesL ^= (ulong)array[alignedLength + 2] << 16; goto case 2; | ||
case 2: remainingBytesL ^= (ulong)array[alignedLength + 1] << 8; goto case 1; | ||
case 1: remainingBytesL ^= (ulong)array[alignedLength] << 0; break; | ||
} | ||
|
||
H2 ^= RotateLeft(remainingBytesH * c2, r2) * c1; | ||
H1 ^= RotateLeft(remainingBytesL * c1, r1) * c2; | ||
} | ||
} | ||
|
||
protected override byte[] HashFinal() | ||
{ | ||
ulong len = (ulong)length; | ||
H1 ^= len; H2 ^= len; | ||
|
||
H1 += H2; | ||
H2 += H1; | ||
|
||
H1 = FMix(H1); | ||
H2 = FMix(H2); | ||
|
||
H1 += H2; | ||
H2 += H1; | ||
|
||
var buffer = new byte[16]; | ||
Array.Copy(BitConverter.GetBytes(H1), 0, buffer, 0, 8); | ||
Array.Copy(BitConverter.GetBytes(H2), 0, buffer, 8, 8); | ||
|
||
return buffer; | ||
} | ||
|
||
public override void Initialize() | ||
{ | ||
H1 = H2 = seed; | ||
length = 0; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static ulong RotateLeft(ulong x, byte n) | ||
{ | ||
return (x << n) | (x >> (64 - n)); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static ulong FMix(ulong h) | ||
{ | ||
// pipelining friendly algorithm | ||
h = (h ^ (h >> 33)) * 0xff51afd7ed558ccd; | ||
h = (h ^ (h >> 33)) * 0xc4ceb9fe1a85ec53; | ||
|
||
return (h ^ (h >> 33)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Neo.Cryptography; | ||
using System.Text; | ||
namespace Neo.UnitTests.Cryptography | ||
{ | ||
[TestClass] | ||
public class UT_Murmur128 | ||
{ | ||
|
||
[TestMethod] | ||
public void TestGetHashSize() | ||
{ | ||
Murmur128 murmur128 = new Murmur128(1); | ||
murmur128.HashSize.Should().Be(128); | ||
} | ||
|
||
[TestMethod] | ||
public void TestHashCore() | ||
{ | ||
byte[] array = Encoding.ASCII.GetBytes("hello"); | ||
array.Murmur128(123u).ToHexString().ToString().Should().Be("0bc59d0ad25fde2982ed65af61227a0e"); | ||
|
||
array = Encoding.ASCII.GetBytes("world"); | ||
array.Murmur128(123u).ToHexString().ToString().Should().Be("3d3810fed480472bd214a14023bb407f"); | ||
|
||
array = Encoding.ASCII.GetBytes("hello world"); | ||
array.Murmur128(123u).ToHexString().ToString().Should().Be("e0a0632d4f51302c55e3b3e48d28795d"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any meaning?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nop, we dont really need any meaningful nonce here, it is just a placeholder, so i picked the nonce from the bitcoin genesis block to show our respect to Satoshi Nakamoto.