-
Notifications
You must be signed in to change notification settings - Fork 180
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
[Randomness part 4] Update math/rand usage in crypto module and improve randomness tests #4111
Conversation
FVM Benchstat comparisonThis branch with compared with the base branch onflow:master commit fc5b515 The command Collapsed results for better readability
|
"gonum.org/v1/gonum/stat" | ||
) | ||
|
||
// BasicDistributionTest is a test function to run a basic statistic test on `randf` output. |
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.
Randomness test tools consolidated here so that all flow-go
tests can use them.
Redundant code will be deleted once the new crypto version is tagged.
// TODO: these functions are copied from flow-go/crypto/rand | ||
// Once the new flow-go/crypto/ module version is tagged, flow-go would upgrade | ||
// to the new version and import these functions | ||
func BasicDistributionTest(t *testing.T, n uint64, classWidth uint64, randf func() (uint64, error)) { |
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.
redundant code till the new crypto tag is ready
// TODO: these functions are copied from flow-go/crypto/rand | ||
// Once the new flow-go/crypto/ module version is tagged, flow-go would upgrade | ||
// to the new version and import these functions | ||
func BasicDistributionTest(t *testing.T, n uint64, classWidth uint64, randf func() (uint64, error)) { |
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.
redundant code till the new crypto tag is ready
Codecov Report
@@ Coverage Diff @@
## master #4111 +/- ##
==========================================
- Coverage 53.51% 51.18% -2.33%
==========================================
Files 833 673 -160
Lines 77896 60340 -17556
==========================================
- Hits 41687 30886 -10801
+ Misses 32874 26971 -5903
+ Partials 3335 2483 -852
Flags with carried forward coverage won't be shown. Click here to find out more.
... and 166 files with indirect coverage changes Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
@@ -149,11 +149,7 @@ func (p *genericPRG) Shuffle(n int, swap func(i, j int)) error { | |||
if n < 0 { | |||
return fmt.Errorf("population size cannot be negative") | |||
} | |||
for i := n - 1; i > 0; i-- { |
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.
the algorithms in utils/rand/rand.go is basically identical to this. the only difference is the random source. can you refactor the code to share implementation?
btw, Permutation / SubPermutation should also just call Shuffle/Sample
maybe undo changes to this file in this PR (since it's unrelated to test updates), and make the changes in a separate PR
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.
Permutation / SubPermutation should also just call Shuffle/Sample
This is true, though it is slightly less efficient: in order to use Shuffle
, Permutation
needs to initialize an array firts, and then shuffle it. The current implementation of Permutation
initializes and permutes at the same time (it's an optimized variant of Fisher-Yates).
can you refactor the code to share implementation?
You are right that the core idea is the same. I did explore the idea of combining both implementations but I didn't find an elegant way to do that, given there are minor differences between the packages (returned errors, thread safety, integer types). For now I only combined the test tools, but we can discuss ways to combine the rest.
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.
looks good. a few nits
( part of this PR is a result of splitting #4052 into several PRs)
math/rand
functionsSeed
andRead
.crypto/random
,utils/rand
andfvm/environment
):fix test bugs in
utils/rand
is extremely rare cases.consolidate the statistic test tools in under crypto library - once the new crypto module is tagged, flow-go test would use the newly exported functions.
improve coverage of integer modulo-type random functions to cover small and large modulos.
improve the sample size to avoid bias of distribution and speed up tests when modulo is small.
side change: update math/rand usage in /integration and /insecure