From 6177901591aee17caa8ae52f3440fc800af596b6 Mon Sep 17 00:00:00 2001 From: Rafael Fourquet Date: Tue, 8 Jun 2021 11:45:20 +0200 Subject: [PATCH] [WIP] Xoshiro: allow any non-negative integer as a seed, via SHA2_256 [ci skip] --- stdlib/Random/Project.toml | 1 + stdlib/Random/src/Random.jl | 1 + stdlib/Random/src/Xoshiro.jl | 49 ++++++------------------------------ 3 files changed, 9 insertions(+), 42 deletions(-) diff --git a/stdlib/Random/Project.toml b/stdlib/Random/Project.toml index 6958e618d3ea8..199dcab940c86 100644 --- a/stdlib/Random/Project.toml +++ b/stdlib/Random/Project.toml @@ -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" diff --git a/stdlib/Random/src/Random.jl b/stdlib/Random/src/Random.jl index 45aa6442eed7e..05fe872eb6ebc 100644 --- a/stdlib/Random/src/Random.jl +++ b/stdlib/Random/src/Random.jl @@ -13,6 +13,7 @@ include("DSFMT.jl") using .DSFMT using Base.GMP.MPZ using Base.GMP: Limb +import SHA using Base: BitInteger, BitInteger_types, BitUnsigned, require_one_based_indexing diff --git a/stdlib/Random/src/Xoshiro.jl b/stdlib/Random/src/Xoshiro.jl index 40da3c5ff1722..2418c045059b4 100644 --- a/stdlib/Random/src/Xoshiro.jl +++ b/stdlib/Random/src/Xoshiro.jl @@ -117,7 +117,7 @@ end # Shared implementation between Xoshiro and TaskLocalRNG -- seeding -function seed!(x::Union{TaskLocalRNG,Xoshiro}) +function seed!(rng::Union{TaskLocalRNG,Xoshiro}) # as we get good randomness from RandomDevice, we can skip hashing parent = RandomDevice() # Constants have nothing up their sleeve, see task.c @@ -125,57 +125,22 @@ function seed!(x::Union{TaskLocalRNG,Xoshiro}) # 0x5a94851fb48a6e05 == hash(UInt(2))|0x01 # 0x3688cf5d48899fa7 == hash(UInt(3))|0x01 # 0x867b4bb4c42e5661 == hash(UInt(4))|0x01 - setstate!(x, + setstate!(rng, 0x02011ce34bce797f * rand(parent, UInt64), 0x5a94851fb48a6e05 * rand(parent, UInt64), 0x3688cf5d48899fa7 * rand(parent, UInt64), 0x867b4bb4c42e5661 * rand(parent, UInt64)) end -function seed!(rng::Union{TaskLocalRNG,Xoshiro}, seed::NTuple{4,UInt64}) - # TODO: Consider a less ad-hoc construction - # We can afford burning a handful of cycles here, and we don't want any - # surprises with respect to bad seeds / bad interactions. - - s0 = s = Base.hash_64_64(seed[1]) - s1 = s += Base.hash_64_64(seed[2]) - s2 = s += Base.hash_64_64(seed[3]) - s3 = s += Base.hash_64_64(seed[4]) - +function seed!(rng::Union{TaskLocalRNG,Xoshiro}, seed::Vector{UInt32}) + c = SHA.SHA2_256_CTX() + SHA.update!(c, reinterpret(UInt8, seed)) + s0, s1, s2, s3 = reinterpret(UInt64, SHA.digest!(c)) setstate!(rng, s0, s1, s2, s3) - - rand(rng, UInt64) - rand(rng, UInt64) - rand(rng, UInt64) - rand(rng, UInt64) - rng end -function seed!(rng::Union{TaskLocalRNG, Xoshiro}, seed::UInt128) - seed0 = seed % UInt64 - seed1 = (seed>>>64) % UInt64 - seed!(rng, (seed0, seed1, zero(UInt64), zero(UInt64))) -end -seed!(rng::Union{TaskLocalRNG, Xoshiro}, seed::Integer) = seed!(rng, UInt128(seed)) - -function seed!(rng::Union{TaskLocalRNG, Xoshiro}, seed::AbstractVector{UInt64}) - if length(seed) > 4 - throw(ArgumentError("seed should have no more than 256 bits")) - end - seed0 = length(seed)>0 ? seed[1] : UInt64(0) - seed1 = length(seed)>1 ? seed[2] : UInt64(0) - seed2 = length(seed)>2 ? seed[3] : UInt64(0) - seed3 = length(seed)>3 ? seed[4] : UInt64(0) - seed!(rng, (seed0, seed1, seed2, seed3)) -end +seed!(rng::Union{TaskLocalRNG, Xoshiro}, seed::Integer) = seed!(rng, make_seed(seed)) -function seed!(rng::Union{TaskLocalRNG, Xoshiro}, seed::AbstractVector{UInt32}) - if iseven(length(seed)) - seed!(rng, reinterpret(UInt64, seed)) - else - seed!(rng, UInt64[reinterpret(UInt64, @view(seed[begin:end-1])); seed[end] % UInt64]) - end -end @inline function rand(rng::Union{TaskLocalRNG, Xoshiro}, ::SamplerType{UInt128}) first = rand(rng, UInt64)