Fix Issue 18595 & Issue 18596: replace MindstdRand0 w/SplitMix & templatize unpredictableSeed!UIntType#6580
Conversation
|
Thanks for your pull request, @n8sh! Bugzilla references
Testing this PR locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub fetch digger
dub run digger -- build "master + phobos#6580" |
93e75f5 to
2ba81a4
Compare
2ba81a4 to
ccbbca5
Compare
ccbbca5 to
d6e2c1f
Compare
7f6358b to
038b0d8
Compare
|
@n8sh does this need a changelog entry? |
|
@thewilsonator I'll add one. |
|
Thanks! |
…latize unpredictableSeed!UIntType
038b0d8 to
c75d6f7
Compare
|
Added. |
| updateResult(cast(ulong) getpid()); | ||
| updateResult(cast(ulong) MonoTime.currTime.ticks); | ||
| result = (result ^ (result >>> 47)) * m; | ||
| return result ^ (result >>> 47); |
There was a problem hiding this comment.
With the LLVM optimizer, this whole function is optimized to calling Thread.getThis(), getpid() and MonoTime.currTime().ticks() (for potential side effects) and then returning an undefined value (i.e., no updateResult() calls, no final mixing) - due to the final value of result depending on its initial value, which is explicitly undefined (= void).
The intent probably was to make the bootstrap seed somewhat more random, but this doesn't work here. Would it be okay to initialize result with 0?
There was a problem hiding this comment.
Quoting Johan:
Using an uninitialized value is UB in D. https://dlang.org/spec/declaration.html#void_init
There was a problem hiding this comment.
Would it be okay to initialize
resultwith 0?
Yes.
Slicing the elephant into smaller pieces. This PR differs from #6388 in that it omits entirely system entropy.
The PR's visible change is that one may now write
unpredictableSeed!ulongand get aulong. The PR's under-the-hood change is that instead of thread-local MinstdRand0unpredictableSeeduses a global SplitMix.Q: Why prefer global shared (atomic) instance to thread-locals?
A: It gives us an easy guarantee that the same
ulongseed will not be produced by more than 1 thread except ifunpredictableSeed!ulongis called 2^^64 times during the life of the application. The question should rather be, what advantage would thread-local storage give us here?Q: What about cryptography?
A: This PR changes nothing about the current situation.
unpredictableSeedis not in any sense unpredictable to an adversary willing to use mathematics.Q: What about speed?
A: This shouldn't be a main consideration, but this implementation happens to be faster. The new
unpredictableSeedtakes about 1/3 the time withldc2 -Oand about 1/2 the time withdmd -O -inline. The story is even better if you compare the newunpredictableSeed!ulongto calling the oldunpredictableSeedtwice to produce a 64-bit value.