Skip to content

Conversation

@cschuchardt88
Copy link
Member

@cschuchardt88 cschuchardt88 commented May 31, 2025

Description

Adds two new methods to StdLib contract. Still uses syscall GetRandom for getting ranges. Updated GetRandom syscall to have 256 bit integer.

image

Methods

// A 256-bit unsigned integer that is greater than or equal to 0 and less than MaxValue.
BigInteger GetRandom(BigInteger maxValue); 

Fixes #3817

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

How Has This Been Tested?

  • Locally Unit Tested

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

@cschuchardt88
Copy link
Member Author

@shargon maybe you help me with

/_/tests/Neo.UnitTests/SmartContract/Native/UT_NativeContract.cs(256): error test failed: TestGenesisNativeState (32ms): Assert.AreEqual failed. Expected:

and setting up hardfork.

@shargon
Copy link
Member

shargon commented May 31, 2025

@shargon maybe you help me with

/_/tests/Neo.UnitTests/SmartContract/Native/UT_NativeContract.cs(256): error test failed: TestGenesisNativeState (32ms): Assert.AreEqual failed. Expected:

and setting up hardfork.

I will do it

@cschuchardt88 cschuchardt88 changed the base branch from master to dev June 7, 2025 03:34
var maxValueSize = BigInteger.Pow(2, maxValueBits);

var nthMask = (BigInteger.One << maxValueBits) - 1;
var randomProduct = maxValue * (engine.GetRandom() & nthMask);
Copy link
Contributor

@Wi1l-B0t Wi1l-B0t Jun 28, 2025

Choose a reason for hiding this comment

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

The engine.GetRandom() returns 128-bit filled buffers.
So there is another idea:

  1. If the maxValue is less than or equal to 128bit, generate a 128-bit random number(between [0, maxValue)) once. Like this: https://cs.opensource.google/go/go/+/master:src/math/rand/rand.go;l=120
  2. If the maxValue is greater than 128bit, generate a random number(between [0, maxValue >> 128bit), i.e. rand1) once, and engine.GetRandom() once, the result is engine.GetRandom() & (rand1 << 128).

The operation maxValue * is not needed in this way.

Copy link
Member Author

@cschuchardt88 cschuchardt88 Jun 29, 2025

Choose a reason for hiding this comment

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

The bigger the number (and these numbers can be huge, if you look at the tests) the longer it takes to loop. So in this case (so it doesnt take long time) we only want to take no bigger than maxValue bits. That could be a number of 10 or Pow(100, 100). In other words its faster this way or else you run out of gas or takes to long. Also can be up to 256-bits.

Copy link
Contributor

@roman-khimov roman-khimov left a comment

Choose a reason for hiding this comment

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

Still need some time, likely can be simplified a bit.

var s2 = BitConverter.ToUInt64(buffer[0..8]);
var s3 = BitConverter.ToUInt64(buffer[8..16]);

// Update PRNG state.
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

@cschuchardt88
Copy link
Member Author

Still need some time, likely can be simplified a bit.

@roman-khimov
image

if (IsHardforkEnabled(Hardfork.HF_Aspidochelone))
if (IsHardforkEnabled(Hardfork.HF_Faun))
{
buffer = Cryptography.Helper.Murmur128(nonceData, ProtocolSettings.Network + random_times++);
Copy link
Member

Choose a reason for hiding this comment

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

random_times is not defined

Copy link
Member Author

Choose a reason for hiding this comment

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

where did it go?

@cschuchardt88
Copy link
Member Author

@roman-khimov when are you going to review this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Core Feature Type: Large changes or new features Hardfork

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Update VM GetRandom

9 participants