-
Notifications
You must be signed in to change notification settings - Fork 387
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(examples): Add a useful set of high quality pseudo-random number generators #2868
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅ 📢 Thoughts on this report? Let us know! |
7f3cc3f
to
4510d34
Compare
I ported a number of my pseudo-random number generator implementations from Ruby to Gno, building them be compatible with the standard Rand() implementation, so that any of these can be used as a drop-in replacement for the default PCG algorithm. All of these are faster than PCG, while still having competitive-to-superior statistical properties and predictability resistance. Further, the ISAAC family of generators are cryptographically secure, and when properly seeded, still have no known practical attack vectors.
Hey @wyhaines, can you update the PR branch with the master branch? 🙏 |
Revise all of the output to use ufmt.
Tidy gno.mods; add a Sprintf %f test.
…intf float formats a bit more to be closer to Go's support.
f56bcea
to
0fff418
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very cool work.
It would be valuable to decouple your uint64
and float64
work from the math/random
work. The changes you introduce there are generally useful and it would give a cleaner path for merging a PR.
This way the larger discussion on where the isaac
and isaac64
work should live can be done in isolation from this. I'm still learning about our namespacing conventions myself and @thehowl brings up valid points that maybe work like this should live in a personal namespace and maybe be considered for "promotion" to official namespaces over time.
I'm a bot that assists the Gno Core team in maintaining this repository. My role is to ensure that contributors understand and follow our guidelines, helping to streamline the development process. The following requirements must be fulfilled before a pull request can be merged. These requirements are defined in this configuration file. Automated Checks🟢 Maintainers must be able to edit this pull request Manual Checks
Debug
|
Fix typo. Thanks @thehowl. Co-authored-by: Morgan <[email protected]>
Fix a typo. Thanks @thehowl. Co-authored-by: Morgan <[email protected]>
That's fine with me. This was work that I did as much to make available some interesting things that I have experience with already as it was to just better learn Gno and some of the gnovm internals. So, my intention is to just make sure that it remains a useful set of packages that is available to people. I'll revise this PR to put this under |
OK. I have moved all of the PRNGs to
|
#3263 has been created. Without that PR, this one will need to retain it's changes to |
This PR includes the changes to `ufmt.Sprintf()` from #2868 as well as another branch where I had added support for `%v`. These changes add support for a variety of float formatting options to Sprintf, as well as support for the %v flag to automatically chose a default representation for a given type. Tests were added for these additions. This PR is needed for the PRNG PR (#2868) to be fully functional, as the generators include some built in statistical examples/tests which will not function without `ufmt.Sprintf()` support for floats. <details><summary>Contributors' checklist...</summary> - [x] Added new tests, or not needed, or not feasible - [x] Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory - [x] Updated the official documentation or not needed - [x] No breaking changes were made, or a `BREAKING CHANGE: xxx` message was included in the description - [x] Added references to related issues and PRs </details> --------- Co-authored-by: Nathan Toups <[email protected]>
This PR includes the changes to `ufmt.Sprintf()` from gnolang#2868 as well as another branch where I had added support for `%v`. These changes add support for a variety of float formatting options to Sprintf, as well as support for the %v flag to automatically chose a default representation for a given type. Tests were added for these additions. This PR is needed for the PRNG PR (gnolang#2868) to be fully functional, as the generators include some built in statistical examples/tests which will not function without `ufmt.Sprintf()` support for floats. <details><summary>Contributors' checklist...</summary> - [x] Added new tests, or not needed, or not feasible - [x] Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory - [x] Updated the official documentation or not needed - [x] No breaking changes were made, or a `BREAKING CHANGE: xxx` message was included in the description - [x] Added references to related issues and PRs </details> --------- Co-authored-by: Nathan Toups <[email protected]>
… generators (gnolang#2868) I ported a number of my pseudo-random number generator implementations from Ruby to gno while traveling to the retreat last weekend as an exercise in expanding my comfort level with gno code, and expanding my understanding of some of the code internals, while contributing code that others may find interesting or useful. I added two xorshift generators, xorshift64* and xorshiftr128+. These are both many times faster than the PCG generator that is the gno default, and produce high quality randomness with great statistical qualities. In addition to these, I added both the 32-bit ISAAC implementation (with an added function to return 64 bit values), and the 64-bit ISAAC implementation. ISAAC is a stellar pseudo-random number generator. Both implementations are significantly faster than PCG (though not near so fast as the xorshift algorithms), while producing extremely high quality, cryptographically secure randomness that can not be differentiated from real randomness. All of these were built to be compatible with the standard Rand() implementation. This means that any of these can be used as a drop-in replacement for the default PCG algorithm: ``` source = isaac.New() prng := rand.New(source) ``` All of these leverage the `gno.land/p/demo/entropy` package to assist with seeding if no seed is provided. In the case of the ISAAC algorithms, they require 256 uint values for their seed, so they leverage a combination of `entropy` and `xorshiftr128+` to generate any missing numbers in the provided seed. I also added a function to entropy to return uint64, to facilitate using it for seeding. I added tests to entropy, and wrote tests for the other generators, as well. There are a few other things that ended up in this PR. In order to make some fact based assertions about the performance of these generators, I included some code that can be ran via `gno run -expr`. i.e. `gno run -expr 'averageISAAC()' isaac.gno` that can be used to get some benchmarks and some very simple self-statistical-analysis on the results, and when I did so, I discovered that the current `ufmt.Sprintf` implementation didn't support any of the float output flags. I added float support to it's capabilities, which, in turn, required adding `FormatFloat` to the `strconv.gno/strconv.go` implementation in the standard library. I added a test to cover this. I also noticed that there is a test in `tm2/pkg/p2p` that is failing on both master and my branch. Specifically, there is a call to `sw.Logger.Error()` that passes a message and an error, but not `"err"` before the error. Adding that seemed to clear up the build failure. This, specifically, is line 222 of `switch.go`. Currently there is one failing test, which is the code coverage check on tm2, because it is non-obvious to me how to setup a test to properly exercise that one changed line. <details><summary>Contributors' checklist...</summary> - [X] Added new tests, or not needed, or not feasible - [X] Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory - [X] Updated the official documentation or not needed - [X] No breaking changes were made, or a `BREAKING CHANGE: xxx` message was included in the description - [X] Added references to related issues and PRs </details> --------- Co-authored-by: Morgan <[email protected]> Co-authored-by: Nathan Toups <[email protected]> Co-authored-by: Morgan <[email protected]>
This PR includes the changes to `ufmt.Sprintf()` from gnolang#2868 as well as another branch where I had added support for `%v`. These changes add support for a variety of float formatting options to Sprintf, as well as support for the %v flag to automatically chose a default representation for a given type. Tests were added for these additions. This PR is needed for the PRNG PR (gnolang#2868) to be fully functional, as the generators include some built in statistical examples/tests which will not function without `ufmt.Sprintf()` support for floats. <details><summary>Contributors' checklist...</summary> - [x] Added new tests, or not needed, or not feasible - [x] Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory - [x] Updated the official documentation or not needed - [x] No breaking changes were made, or a `BREAKING CHANGE: xxx` message was included in the description - [x] Added references to related issues and PRs </details> --------- Co-authored-by: Nathan Toups <[email protected]>
… generators (gnolang#2868) I ported a number of my pseudo-random number generator implementations from Ruby to gno while traveling to the retreat last weekend as an exercise in expanding my comfort level with gno code, and expanding my understanding of some of the code internals, while contributing code that others may find interesting or useful. I added two xorshift generators, xorshift64* and xorshiftr128+. These are both many times faster than the PCG generator that is the gno default, and produce high quality randomness with great statistical qualities. In addition to these, I added both the 32-bit ISAAC implementation (with an added function to return 64 bit values), and the 64-bit ISAAC implementation. ISAAC is a stellar pseudo-random number generator. Both implementations are significantly faster than PCG (though not near so fast as the xorshift algorithms), while producing extremely high quality, cryptographically secure randomness that can not be differentiated from real randomness. All of these were built to be compatible with the standard Rand() implementation. This means that any of these can be used as a drop-in replacement for the default PCG algorithm: ``` source = isaac.New() prng := rand.New(source) ``` All of these leverage the `gno.land/p/demo/entropy` package to assist with seeding if no seed is provided. In the case of the ISAAC algorithms, they require 256 uint values for their seed, so they leverage a combination of `entropy` and `xorshiftr128+` to generate any missing numbers in the provided seed. I also added a function to entropy to return uint64, to facilitate using it for seeding. I added tests to entropy, and wrote tests for the other generators, as well. There are a few other things that ended up in this PR. In order to make some fact based assertions about the performance of these generators, I included some code that can be ran via `gno run -expr`. i.e. `gno run -expr 'averageISAAC()' isaac.gno` that can be used to get some benchmarks and some very simple self-statistical-analysis on the results, and when I did so, I discovered that the current `ufmt.Sprintf` implementation didn't support any of the float output flags. I added float support to it's capabilities, which, in turn, required adding `FormatFloat` to the `strconv.gno/strconv.go` implementation in the standard library. I added a test to cover this. I also noticed that there is a test in `tm2/pkg/p2p` that is failing on both master and my branch. Specifically, there is a call to `sw.Logger.Error()` that passes a message and an error, but not `"err"` before the error. Adding that seemed to clear up the build failure. This, specifically, is line 222 of `switch.go`. Currently there is one failing test, which is the code coverage check on tm2, because it is non-obvious to me how to setup a test to properly exercise that one changed line. <details><summary>Contributors' checklist...</summary> - [X] Added new tests, or not needed, or not feasible - [X] Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory - [X] Updated the official documentation or not needed - [X] No breaking changes were made, or a `BREAKING CHANGE: xxx` message was included in the description - [X] Added references to related issues and PRs </details> --------- Co-authored-by: Morgan <[email protected]> Co-authored-by: Nathan Toups <[email protected]> Co-authored-by: Morgan <[email protected]>
I ported a number of my pseudo-random number generator implementations from Ruby to gno while traveling to the retreat last weekend as an exercise in expanding my comfort level with gno code, and expanding my understanding of some of the code internals, while contributing code that others may find interesting or useful.
I added two xorshift generators, xorshift64* and xorshiftr128+. These are both many times faster than the PCG generator that is the gno default, and produce high quality randomness with great statistical qualities. In addition to these, I added both the 32-bit ISAAC implementation (with an added function to return 64 bit values), and the 64-bit ISAAC implementation. ISAAC is a stellar pseudo-random number generator. Both implementations are significantly faster than PCG (though not near so fast as the xorshift algorithms), while producing extremely high quality, cryptographically secure randomness that can not be differentiated from real randomness.
All of these were built to be compatible with the standard Rand() implementation. This means that any of these can be used as a drop-in replacement for the default PCG algorithm:
All of these leverage the
gno.land/p/demo/entropy
package to assist with seeding if no seed is provided. In the case of the ISAAC algorithms, they require 256 uint values for their seed, so they leverage a combination ofentropy
andxorshiftr128+
to generate any missing numbers in the provided seed.I also added a function to entropy to return uint64, to facilitate using it for seeding.
I added tests to entropy, and wrote tests for the other generators, as well.
There are a few other things that ended up in this PR. In order to make some fact based assertions about the performance of these generators, I included some code that can be ran via
gno run -expr
. i.e.gno run -expr 'averageISAAC()' isaac.gno
that can be used to get some benchmarks and some very simple self-statistical-analysis on the results, and when I did so, I discovered that the currentufmt.Sprintf
implementation didn't support any of the float output flags.I added float support to it's capabilities, which, in turn, required adding
FormatFloat
to thestrconv.gno/strconv.go
implementation in the standard library. I added a test to cover this.I also noticed that there is a test in
tm2/pkg/p2p
that is failing on both master and my branch. Specifically, there is a call tosw.Logger.Error()
that passes a message and an error, but not"err"
before the error. Adding that seemed to clear up the build failure. This, specifically, is line 222 ofswitch.go
.Currently there is one failing test, which is the code coverage check on tm2, because it is non-obvious to me how to setup a test to properly exercise that one changed line.
Contributors' checklist...
BREAKING CHANGE: xxx
message was included in the description