Skip to content
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

Fix revertibleRandom with module test #3025

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4582,16 +4582,16 @@ func TestRuntimeRandom(t *testing.T) {

testTypes := func(t *testing.T, testType func(*testing.T, sema.Type)) {
for _, ty := range sema.AllFixedSizeUnsignedIntegerTypes {
tyCopy := ty
ty := ty
t.Run(ty.String(), func(t *testing.T) {
t.Parallel()

testType(t, tyCopy)
testType(t, ty)
})
}
}

typeToBytes := func(t *testing.T, ty sema.Type) int {
numericTypeByteSize := func(t *testing.T, ty sema.Type) int {
require.IsType(t, &sema.NumericType{}, ty)
return ty.(*sema.NumericType).ByteSize()
}
Expand Down Expand Up @@ -4646,7 +4646,7 @@ func TestRuntimeRandom(t *testing.T) {
require.NoError(t, err)

// prepare the expected value from the random source
expected := new(big.Int).SetBytes(randBuffer[:typeToBytes(t, ty)])
expected := new(big.Int).SetBytes(randBuffer[:numericTypeByteSize(t, ty)])
assert.Equal(t, expected.String(), loggedMessage)
}
testTypes(t, runValidCaseWithoutModulo)
Expand All @@ -4662,7 +4662,7 @@ func TestRuntimeRandom(t *testing.T) {
value, err := executeScript(ty, "", newReadFromBuffer(randBuffer))
require.NoError(t, err)
// prepare the expected value from the random source
expected := new(big.Int).SetBytes(randBuffer[:typeToBytes(t, ty)])
expected := new(big.Int).SetBytes(randBuffer[:numericTypeByteSize(t, ty)])
assert.Equal(t, expected.String(), value.String())
}
testTypes(t, runValidCaseWithoutModulo)
Expand All @@ -4673,11 +4673,31 @@ func TestRuntimeRandom(t *testing.T) {
t.Parallel()

runValidCaseWithModulo := func(t *testing.T, ty sema.Type) {
byteSize := numericTypeByteSize(t, ty)

moduloBuffer := newRandBuffer(t)

// ensure random buffer range used for modulo is not all zeros (modulo cannot be zero)
for {
var foundNonZero bool
for _, v := range moduloBuffer[:byteSize] {
if v != 0 {
foundNonZero = true
break
}
}
if foundNonZero {
break
}

// if all zeros, draw new random buffer
_, err := rand.Read(moduloBuffer[:byteSize])
require.NoError(t, err)
}

// build a big Int from the modulo buffer, with the required `ty` size
// big.Int are used as they cover all the tested types including the small ones (UInt8 ..)
modulo := new(big.Int).SetBytes(moduloBuffer[:typeToBytes(t, ty)])
modulo := new(big.Int).SetBytes(moduloBuffer[:byteSize])
Copy link
Contributor

@tarakby tarakby Jan 16, 2024

Choose a reason for hiding this comment

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

Nit: we could use this big.Int to check for zero (using Cmp) instead of the buffer byte check (no need to open a new PR but it can be added in future changes).

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea, could you please open a PR?

Copy link
Contributor

Choose a reason for hiding this comment

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

yep, will clean it up in a few days.


value, err := executeScript(ty, modulo.String(), readCryptoRandom)
require.NoError(t, err)
Expand All @@ -4701,7 +4721,7 @@ func TestRuntimeRandom(t *testing.T) {

// set modulo to the max value of the type: (1 << bitSize) - 1
// big.Int are used as they cover all the tested types including the small ones (UInt8 ..)
bitSize := typeToBytes(t, ty) << 3
bitSize := numericTypeByteSize(t, ty) << 3
one := big.NewInt(1)
modulo := new(big.Int).Lsh(one, uint(bitSize))
modulo.Sub(modulo, one)
Expand Down
Loading