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

[Randomness part 4] Update math/rand usage in crypto module and improve randomness tests #4111

Merged
merged 8 commits into from
Mar 31, 2023

Conversation

tarakby
Copy link
Contributor

@tarakby tarakby commented Mar 28, 2023

( part of this PR is a result of splitting #4052 into several PRs)

  • prepare for Go1.20 update by removing deprecated math/rand functions Seed and Read.
  • improve randomness tests in (crypto/random, utils/rand and fvm/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

@github-actions
Copy link
Contributor

github-actions bot commented Mar 28, 2023

FVM Benchstat comparison

This branch with compared with the base branch onflow:master commit fc5b515

The command (for i in {1..7}; do go test ./fvm ./engine/execution/computation --bench . --tags relic -shuffle=on --benchmem --run ^$; done) was used.

Collapsed results for better readability

old.txtnew.txt
time/opdelta
ComputeBlock/16/cols/128/txes-26.43s ± 0%6.34s ± 0%~(p=1.000 n=1+1)
 
us/txdelta
ComputeBlock/16/cols/128/txes-23.14k ± 0%3.09k ± 0%~(p=1.000 n=1+1)
 
alloc/opdelta
ComputeBlock/16/cols/128/txes-21.21GB ± 0%1.21GB ± 0%~(p=1.000 n=1+1)
 
allocs/opdelta
ComputeBlock/16/cols/128/txes-217.4M ± 0%17.4M ± 0%~(p=1.000 n=1+1)
 

"gonum.org/v1/gonum/stat"
)

// BasicDistributionTest is a test function to run a basic statistic test on `randf` output.
Copy link
Contributor Author

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)) {
Copy link
Contributor Author

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)) {
Copy link
Contributor Author

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-commenter
Copy link

codecov-commenter commented Mar 28, 2023

Codecov Report

Merging #4111 (c24395a) into master (814e98d) will decrease coverage by 2.33%.
The diff coverage is 48.18%.

@@            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     
Flag Coverage Δ
unittests 51.18% <48.18%> (-2.33%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
cmd/bootstrap/cmd/keys.go 53.60% <0.00%> (ø)
cmd/scaffold.go 14.75% <0.00%> (-0.34%) ⬇️
cmd/util/ledger/reporters/account_reporter.go 0.00% <0.00%> (ø)
...nsensus/hotstuff/committees/consensus_committee.go 69.72% <ø> (ø)
fvm/derived/derived_block_data.go 33.33% <0.00%> (-0.96%) ⬇️
fvm/derived/table_invalidator.go 100.00% <ø> (ø)
fvm/environment/account_key_updater.go 19.48% <0.00%> (ø)
fvm/environment/facade_env.go 87.11% <0.00%> (ø)
fvm/errors/accounts.go 0.00% <0.00%> (ø)
fvm/errors/codes.go 100.00% <ø> (ø)
... and 43 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.

@tarakby tarakby marked this pull request as ready for review March 28, 2023 19:53
@tarakby tarakby changed the title [Crypto] Update math/rand usage in crypto module and improve randomness tests [Randomness part 4] Update math/rand usage in crypto module and improve randomness tests Mar 28, 2023
crypto/hash/hash_test.go Outdated Show resolved Hide resolved
@@ -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-- {
Copy link
Contributor

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

Copy link
Contributor Author

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.

Copy link
Contributor

@peterargue peterargue left a 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

crypto/bls12381_utils_test.go Outdated Show resolved Hide resolved
crypto/bls12381_utils_test.go Outdated Show resolved Hide resolved
crypto/bls_test.go Outdated Show resolved Hide resolved
crypto/bls_thresholdsign_test.go Outdated Show resolved Hide resolved
crypto/ecdsa_test.go Show resolved Hide resolved
@tarakby tarakby merged commit 72a1e42 into master Mar 31, 2023
@tarakby tarakby deleted the tarak/randomness-part4-crypto branch March 31, 2023 16:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants