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

use SHA512 when seeding MersenneTwister #37766

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions stdlib/Random/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[deps]
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
2 changes: 1 addition & 1 deletion stdlib/Random/src/DSFMT.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function dsfmt_init_gen_rand(s::DSFMT_state, seed::UInt32)
s.val, seed)
end

function dsfmt_init_by_array(s::DSFMT_state, seed::Vector{UInt32})
function dsfmt_init_by_array(s::DSFMT_state, seed::AbstractVector{UInt32})
ccall((:dsfmt_init_by_array,:libdSFMT),
Cvoid,
(Ptr{Cvoid}, Ptr{UInt32}, Int32),
Expand Down
12 changes: 10 additions & 2 deletions stdlib/Random/src/RNGs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,17 @@ end

#### seed!()

function seed!(r::MersenneTwister, seed::Vector{UInt32})
function seed!(r::MersenneTwister, seed::Vector{UInt32}; hash=true)
copyto!(resize!(r.seed, length(seed)), seed)
dsfmt_init_by_array(r.state, r.seed)
if hash # hash the seed to make it defensively more robust
c = SHA.SHA2_512_CTX()
# hash VERSION to help people not rely on stream stability between minor releases
SHA.update!(c, reinterpret(UInt8, [VERSION.major, VERSION.minor]))
SHA.update!(c, reinterpret(UInt8, seed))
dsfmt_init_by_array(r.state, reinterpret(UInt32, SHA.digest!(c)))
else
dsfmt_init_by_array(r.state, r.seed)
end
mt_setempty!(r)
mt_setempty!(r, UInt128)
fillcache_zeros!(r)
Expand Down
2 changes: 2 additions & 0 deletions stdlib/Random/src/Random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ using .DSFMT
using Base.GMP.MPZ
using Base.GMP: Limb

import SHA
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As SHA is accessed, I guess yes? I prefer using qualifed names like SHA.update! here. Or did I misunderstand the question?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I see: it's used in RNG.jl but not in this file. Might be clearer to move the import SHA into the file that uses it, but this is valid as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ok I understand. it's just that Random.jl already happens to have all the import/using directives necessary for the other files it includes.


using Base: BitInteger, BitInteger_types, BitUnsigned, require_one_based_indexing

import Base: copymutable, copy, copy!, ==, hash, convert,
Expand Down