-
Notifications
You must be signed in to change notification settings - Fork 1k
[Add] GetRandom(MaxValue) to StdLib Contract
#3968
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
base: dev
Are you sure you want to change the base?
Changes from all commits
c5ebd50
68cea76
4327f69
92d641c
7779416
c960401
8125030
889b467
9a0de40
f6bb2ce
c4a2381
97646c0
ec31953
b7007df
231c36f
e251a4e
75304f4
aa42a91
456453f
b93b007
0d81431
b3222c8
9d3cf99
e27cc9f
1890a24
495bf4b
6327298
699412b
a9b3f57
6123bce
087e2e3
684ef49
5b81136
ba407a4
536240f
93b9147
d0ef022
23c6644
4055de0
10d3eea
892a19b
530bae9
239ac48
62565d2
cd46b38
71421be
7f11329
fe1da8a
8defdea
16ef3e4
578440f
7680004
67d9824
0663753
97bdd7d
67da3d2
c479b79
bbc1dc5
f80ac48
f57cd44
a8328c7
09ba018
19fea5d
cc5dfd2
61fc499
6cec193
20ebbfd
8db7e47
5dfd971
104d719
3399f33
b7b652e
8fe7543
beaa95c
aa34316
accbf12
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
| using System.IO; | ||
| using System.Linq; | ||
| using System.Numerics; | ||
| using System.Runtime.InteropServices; | ||
| using Array = Neo.VM.Types.Array; | ||
|
|
||
| namespace Neo.SmartContract | ||
|
|
@@ -309,10 +310,39 @@ protected internal int GetInvocationCounter() | |
| /// <returns>The next random number.</returns> | ||
| protected internal BigInteger GetRandom() | ||
| { | ||
| byte[] buffer; | ||
| Span<byte> buffer; | ||
| // In the unit of datoshi, 1 datoshi = 1e-8 GAS | ||
| long price; | ||
| if (IsHardforkEnabled(Hardfork.HF_Aspidochelone)) | ||
| if (IsHardforkEnabled(Hardfork.HF_Faun)) | ||
| { | ||
| buffer = Cryptography.Helper.Murmur128(nonceData, ProtocolSettings.Network + random_times++); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. where did it go? |
||
| var s0 = BitConverter.ToUInt64(buffer[0..8]); | ||
| var s1 = BitConverter.ToUInt64(buffer[8..16]); | ||
|
|
||
| buffer = Cryptography.Helper.Murmur128(buffer, ProtocolSettings.Network + random_times++); | ||
| var s2 = BitConverter.ToUInt64(buffer[0..8]); | ||
| var s3 = BitConverter.ToUInt64(buffer[8..16]); | ||
|
|
||
| // Update PRNG state. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure what this buys you. Either we have pseudorandom bits from murmur128 and trust it to have them or this doesn't change much. |
||
| ulong t = s1 << 17; | ||
| s2 ^= s0; | ||
| s3 ^= s1; | ||
| s1 ^= s2; | ||
| s0 ^= s3; | ||
| s2 ^= t; | ||
| s3 = BitOperations.RotateLeft(s3, 45); | ||
|
|
||
| buffer = new byte[32]; | ||
| MemoryMarshal.Write(buffer[0..8], s0); | ||
| MemoryMarshal.Write(buffer[8..16], s1); | ||
| MemoryMarshal.Write(buffer[16..24], s2); | ||
| MemoryMarshal.Write(buffer[24..32], s3); | ||
|
|
||
| price = 1 << 13; | ||
| AddFee(price * ExecFeeFactor); | ||
| return new BigInteger(buffer, isUnsigned: true) % (BigInteger.One << 255) - BigInteger.One; | ||
| } | ||
| else if (IsHardforkEnabled(Hardfork.HF_Aspidochelone)) | ||
| { | ||
| buffer = Cryptography.Helper.Murmur128(nonceData, ProtocolSettings.Network + randomTimes++); | ||
| price = 1 << 13; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.