-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Code Health] refactor: random number generation (#618)
- Loading branch information
1 parent
1587a30
commit 6a41dd4
Showing
11 changed files
with
151 additions
and
135 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package rand | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"math/rand" | ||
|
||
"github.com/cometbft/cometbft/crypto" | ||
) | ||
|
||
// SeededFloat32 generates a deterministic float32 between 0 and 1 given a seed. | ||
// | ||
// TODO_MAINNET: To support other language implementations of the protocol, the | ||
// pseudo-random number generator used here should be language-agnostic (i.e. not | ||
// golang specific). | ||
func SeededFloat32(seedParts ...[]byte) (float32, error) { | ||
seedHashInputBz := bytes.Join(append([][]byte{}, seedParts...), nil) | ||
seedHash := crypto.Sha256(seedHashInputBz) | ||
seed, _ := binary.Varint(seedHash) | ||
|
||
// Construct a pseudo-random number generator with the seed. | ||
pseudoRand := rand.New(rand.NewSource(seed)) | ||
|
||
// Generate a random uint32. | ||
randUint32 := pseudoRand.Uint32() | ||
|
||
// Clamp the random float32 between [0,1]. This is achieved by dividing the random uint32 | ||
// by the most significant digit of a float32, which is 2^32, guaranteeing an output between | ||
// 0 and 1, inclusive. | ||
oneMostSignificantDigitFloat32 := float32(1 << 32) | ||
randClampedFloat32 := float32(randUint32) / oneMostSignificantDigitFloat32 | ||
|
||
return randClampedFloat32, nil | ||
} |
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,54 @@ | ||
package rand_test | ||
|
||
import ( | ||
"encoding/binary" | ||
"sync" | ||
"sync/atomic" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
poktrand "github.com/pokt-network/poktroll/pkg/crypto/rand" | ||
prooftypes "github.com/pokt-network/poktroll/x/proof/types" | ||
) | ||
|
||
func TestSeededFloat32(t *testing.T) { | ||
probability := prooftypes.DefaultProofRequestProbability | ||
tolerance := 0.01 | ||
confidence := 0.99 | ||
|
||
sampleSize := poktrand.RequiredSampleSize(float64(probability), tolerance, confidence) | ||
|
||
var numTrueSamples atomic.Int64 | ||
|
||
// Sample concurrently to save time. | ||
wg := sync.WaitGroup{} | ||
for idx := int64(0); idx < sampleSize; idx++ { | ||
wg.Add(1) | ||
go func() { | ||
idxBz := make([]byte, binary.MaxVarintLen64) | ||
binary.PutVarint(idxBz, idx) | ||
randFloat, err := poktrand.SeededFloat32(idxBz) | ||
require.NoError(t, err) | ||
|
||
if randFloat < 0 || randFloat > 1 { | ||
t.Fatalf("secureRandFloat64() returned out of bounds value: %f", randFloat) | ||
} | ||
|
||
if randFloat <= probability { | ||
numTrueSamples.Add(1) | ||
} | ||
wg.Done() | ||
}() | ||
} | ||
wg.Wait() | ||
|
||
expectedNumTrueSamples := float32(sampleSize) * probability | ||
expectedNumFalseSamples := float32(sampleSize) * (1 - probability) | ||
toleranceSamples := tolerance * float64(sampleSize) | ||
|
||
// Check that the number of samples for each outcome is within the expected range. | ||
numFalseSamples := sampleSize - numTrueSamples.Load() | ||
require.InDeltaf(t, expectedNumTrueSamples, numTrueSamples.Load(), toleranceSamples, "true samples") | ||
require.InDeltaf(t, expectedNumFalseSamples, numFalseSamples, toleranceSamples, "false samples") | ||
} |
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,23 @@ | ||
package rand | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"math/rand" | ||
|
||
"github.com/cometbft/cometbft/crypto" | ||
) | ||
|
||
// SeededInt63 generates a deterministic non-negative int64 by seeding a random | ||
// source with the hash of seedParts. | ||
// | ||
// TODO_MAINNET: To support other language implementations of the protocol, the | ||
// pseudo-random number generator used here should be language-agnostic (i.e. not | ||
// golang specific). | ||
func SeededInt63(seedParts ...[]byte) int64 { | ||
seedHashInputBz := bytes.Join(append([][]byte{}, seedParts...), nil) | ||
seedHash := crypto.Sha256(seedHashInputBz) | ||
seed, _ := binary.Varint(seedHash) | ||
|
||
return rand.NewSource(seed).Int63() | ||
} |
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
Oops, something went wrong.