Fixes initialization of ISAAC PRNG without explicit seed#4882
Fixes initialization of ISAAC PRNG without explicit seed#4882mverzilli merged 1 commit intocrystal-lang:masterfrom
Conversation
src/random/isaac.cr
Outdated
|
|
||
| private def random_seeds | ||
| result = uninitialized Seeds | ||
| SecureRandom.random_bytes(result.to_unsafe.as(UInt8*).to_slice(sizeof(Seeds))) |
There was a problem hiding this comment.
Would unsafe_as(StaticArray(UInt8, 32)).to_slice work?
There was a problem hiding this comment.
Yeah, this way it looks cleaner 😄 . Is StaticArray(UInt8, sizeof(Seeds)) ok?
src/random/isaac.cr
Outdated
|
|
||
| private def random_seeds | ||
| result = uninitialized Seeds | ||
| SecureRandom.random_bytes(result.to_unsafe.as(UInt8*).to_slice(sizeof(Seeds))) |
src/random/isaac.cr
Outdated
|
|
||
| private def random_seeds | ||
| result = uninitialized Seeds | ||
| SecureRandom.random_bytes(result.unsafe_as(StaticArray(UInt8, sizeof(Seeds))).to_slice) |
There was a problem hiding this comment.
Please extract result.unsafe_as(StaticArray(UInt8, sizeof(Seeds))).to_slice to a result_slice variable. The line's getting quite messy.
|
Why requiring |
|
I don't understand what is the difference (I thought that |
That definitely is worth a blogpost or similar that describes what the stdlib should look like moving forward. /cc @mverzilli @matiasgarciaisaia @bcardiff |
|
@konovod a bit more of background (until someone can produce a condensed and concise response here): Cheers |
|
@luislavena i think i already read them (maybe not thoroughly). But when reading i thought that it was replacing So, if I understand correctly, the difference is that I've updated PR. |
|
This weird phobia against including The system random situation in crystal is also just crazy. There should be one class in crystal which uses And you know how I feel about All the current mess just serves to confuse people, as this thread continues to prove. |
I have no personal position on that, so I will suggest a RFC for that be reviewed by core and other contributors. Please do, so the RFC can be used as guidance and modifications be introduced to simply current approach. |
adds specs for such initialization
|
Thank you @konovod! I was about to say you're steadily becoming "Crystal's Random Guy" but realized it didn't sound so positive :P. |
#4789 introduced a problem - there is no spec ensuring that PRNG can be actually created without seed. So "seeding" it added for ISAAC was a piece of code that don't even compiles.
This PR fixes it and adds spec that ensures that initialization at least compiles. I don't know what else can be checked in spec (i think we can compare that
next_uof two created instances aren't equal, but this means that there will be 1/2^32 chance that they will match and spec fails).