-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
feat: add rand funcs to math #15043
Merged
Merged
feat: add rand funcs to math #15043
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d572c26
add rand file to math
tac0turtle 63dcd65
remove from store
tac0turtle bcc5337
Merge branch 'main' into marko/14726_replace_rand
tac0turtle db638a4
move rand.go to unsafe package
tac0turtle dcb3a6b
fix build
tac0turtle 6cb929c
fix imports
tac0turtle 959ce49
fix imports v2
tac0turtle 1b30bbb
update go mod
tac0turtle d038d9f
Merge branch 'main' into marko/14726_replace_rand
tac0turtle de24a4f
fix cosmovisor build
tac0turtle bf2f20e
rosetta build
tac0turtle 968a096
tests build
tac0turtle a83f3a5
build and changelog
tac0turtle f74e129
Merge branch 'main' into marko/14726_replace_rand
tac0turtle 6457d49
build for the 100th time
tac0turtle 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
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,148 @@ | ||
package unsafe | ||
|
||
import ( | ||
crand "crypto/rand" | ||
mrand "math/rand" | ||
"sync" | ||
) | ||
|
||
const ( | ||
strChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" // 62 characters | ||
) | ||
|
||
// Rand is a prng, that is seeded with OS randomness. | ||
// The OS randomness is obtained from crypto/rand, however none of the provided | ||
// methods are suitable for cryptographic usage. | ||
// They all utilize math/rand's prng internally. | ||
// | ||
// All of the methods here are suitable for concurrent use. | ||
// This is achieved by using a mutex lock on all of the provided methods. | ||
type Rand struct { | ||
sync.Mutex | ||
rand *mrand.Rand | ||
} | ||
|
||
var grand *Rand | ||
|
||
func init() { | ||
grand = NewRand() | ||
grand.init() | ||
} | ||
|
||
func NewRand() *Rand { | ||
rand := &Rand{} | ||
rand.init() | ||
return rand | ||
} | ||
|
||
func (r *Rand) init() { | ||
bz := cRandBytes(8) | ||
var seed uint64 | ||
for i := 0; i < 8; i++ { | ||
seed |= uint64(bz[i]) | ||
seed <<= 8 | ||
} | ||
r.reset(int64(seed)) | ||
} | ||
|
||
func (r *Rand) reset(seed int64) { | ||
r.rand = mrand.New(mrand.NewSource(seed)) | ||
} | ||
|
||
//---------------------------------------- | ||
// Global functions | ||
|
||
func Seed(seed int64) { | ||
grand.Seed(seed) | ||
} | ||
|
||
func Str(length int) string { | ||
return grand.Str(length) | ||
} | ||
|
||
func Int63() int64 { | ||
return grand.Int63() | ||
} | ||
|
||
func Int() int { | ||
return grand.Int() | ||
} | ||
|
||
func Bytes(n int) []byte { | ||
return grand.Bytes(n) | ||
} | ||
|
||
//---------------------------------------- | ||
// Rand methods | ||
|
||
func (r *Rand) Seed(seed int64) { | ||
r.Lock() | ||
r.reset(seed) | ||
r.Unlock() | ||
} | ||
|
||
// Str constructs a random alphanumeric string of given length. | ||
func (r *Rand) Str(length int) string { | ||
if length <= 0 { | ||
return "" | ||
} | ||
|
||
chars := []byte{} | ||
MAIN_LOOP: | ||
for { | ||
val := r.Int63() | ||
for i := 0; i < 10; i++ { | ||
v := int(val & 0x3f) // rightmost 6 bits | ||
if v >= 62 { // only 62 characters in strChars | ||
val >>= 6 | ||
continue | ||
} else { | ||
chars = append(chars, strChars[v]) | ||
if len(chars) == length { | ||
break MAIN_LOOP | ||
} | ||
val >>= 6 | ||
} | ||
} | ||
} | ||
|
||
return string(chars) | ||
} | ||
|
||
func (r *Rand) Int63() int64 { | ||
r.Lock() | ||
i63 := r.rand.Int63() | ||
r.Unlock() | ||
return i63 | ||
} | ||
|
||
func (r *Rand) Int() int { | ||
r.Lock() | ||
i := r.rand.Int() | ||
r.Unlock() | ||
return i | ||
} | ||
|
||
// Bytes returns n random bytes generated from the internal | ||
// prng. | ||
func (r *Rand) Bytes(n int) []byte { | ||
// cRandBytes isn't guaranteed to be fast so instead | ||
// use random bytes generated from the internal PRNG | ||
bs := make([]byte, n) | ||
for i := 0; i < len(bs); i++ { | ||
bs[i] = byte(r.Int() & 0xFF) | ||
} | ||
return bs | ||
} | ||
|
||
// NOTE: This relies on the os's random number generator. | ||
// For real security, we should salt that with some seed. | ||
// See github.com/cometbft/cometbft/crypto for a more secure reader. | ||
func cRandBytes(numBytes int) []byte { | ||
b := make([]byte, numBytes) | ||
_, err := crand.Read(b) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return b | ||
} |
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
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
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.
Check failure
Code scanning / gosec
Use of weak random number generator (math/rand instead of crypto/rand)