diff options
author | Robert Kern <robert.kern@gmail.com> | 2020-06-09 11:58:49 -0400 |
---|---|---|
committer | Robert Kern <robert.kern@gmail.com> | 2020-06-09 11:58:49 -0400 |
commit | 7a82f14075eaf8f59ea3bb2594b71639e938611e (patch) | |
tree | 53d2db5c149bb83232cefd7475e70628cdfcc7b3 /numpy/random/tests | |
parent | ad30b31af0bb3fbfdc0f11486807edc76cec2680 (diff) | |
download | numpy-7a82f14075eaf8f59ea3bb2594b71639e938611e.tar.gz |
BUG: Ensure SeedSequence 0-padding does not collide with spawn keys
Fixes #16539
The implicit 0-padding that is done to small entropy inputs to make them the
size of the internal pool conflicts with the spawn keys, which start with an
appended 0.
In order to maintain stream compatibility with unspawned `SeedSequence`s, we
explicitly 0-pad short inputs out to the pool size only if the spawn key is
provided, and thus would trigger the bug. This should minimize the impact on
users that were not encountering the bug.
Diffstat (limited to 'numpy/random/tests')
-rw-r--r-- | numpy/random/tests/test_seed_sequence.py | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/numpy/random/tests/test_seed_sequence.py b/numpy/random/tests/test_seed_sequence.py index fe23680ed..f08cf80fa 100644 --- a/numpy/random/tests/test_seed_sequence.py +++ b/numpy/random/tests/test_seed_sequence.py @@ -1,5 +1,5 @@ import numpy as np -from numpy.testing import assert_array_equal +from numpy.testing import assert_array_equal, assert_array_compare from numpy.random import SeedSequence @@ -52,3 +52,29 @@ def test_reference_data(): assert_array_equal(state, expected) state64 = ss.generate_state(len(expected64), dtype=np.uint64) assert_array_equal(state64, expected64) + + +def test_zero_padding(): + """ Ensure that the implicit zero-padding does not cause problems. + """ + # Ensure that large integers are inserted in little-endian fashion to avoid + # trailing 0s. + ss0 = SeedSequence(42) + ss1 = SeedSequence(42 << 32) + assert_array_compare( + np.not_equal, + ss0.generate_state(4), + ss1.generate_state(4)) + + # Ensure backwards compatibility with the original 0.17 release for small + # integers and no spawn key. + expected42 = np.array([3444837047, 2669555309, 2046530742, 3581440988], + dtype=np.uint32) + assert_array_equal(SeedSequence(42).generate_state(4), expected42) + + # Regression test for gh-16539 to ensure that the implicit 0s don't + # conflict with spawn keys. + assert_array_compare( + np.not_equal, + SeedSequence(42, spawn_key=(0,)).generate_state(4), + expected42) |