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

misc random #429

Open
timotheecour opened this issue Dec 7, 2020 · 12 comments
Open

misc random #429

timotheecour opened this issue Dec 7, 2020 · 12 comments
Assignees
Labels

Comments

@timotheecour
Copy link
Owner

timotheecour commented Dec 7, 2020

D20201208T190246

links

@timotheecour timotheecour changed the title oids should use randomize oids should use randomize Dec 7, 2020
@timotheecour timotheecour changed the title oids should use randomize misc random Dec 7, 2020
@timotheecour
Copy link
Owner Author

timotheecour commented Dec 7, 2020

  • we should port urandom/getentropy/arc4random + friends
    in particular, this would provide better seeding for std/random.randomize

see man urandom and man getentropy

/dev/urandom is a compatibility nod to Linux. On Linux, /dev/urandom will produce lower quality output if the entropy pool drains, while /dev/random will prefer to block and wait for additional entropy
     to be collected.  With Fortuna, this choice and distinction is not necessary, and the two devices behave identically. You may use either.

     The random device implements the Fortuna pseudo random number generator algorithm and maintains its entropy pool.  The kernel automatically seeds the algorithm with additional entropy during normal exe-
     cution.

etc

EDIT: would help properly fix nim-lang#8588

@timotheecour
Copy link
Owner Author

timotheecour commented Dec 7, 2020

  • port SecureRandom and PseudoRandom

see https://github.com/mongodb/mongo/blob/eb2b72cf9c0269f086223d499ac9be8a270d268c/src/mongo/platform/random.h

* More secure random numbers
 * Suitable for nonce/crypto
 * Slower than PseudoRandom, so only use when really need

@timotheecour
Copy link
Owner Author

@ringabout ringabout self-assigned this Dec 8, 2020
@ringabout
Copy link
Collaborator

ringabout commented Dec 8, 2020

  • port SecureRandom and PseudoRandom

see https://github.com/mongodb/mongo/blob/eb2b72cf9c0269f086223d499ac9be8a270d268c/src/mongo/platform/random.h

* More secure random numbers
 * Suitable for nonce/crypto
 * Slower than PseudoRandom, so only use when really need

This is really useful.
https://docs.python.org/3/library/secrets.html
https://github.com/cheatfate/nimcrypto/blob/master/nimcrypto/sysrand.nim

@timotheecour timotheecour added random and removed stdlib labels Dec 8, 2020
@timotheecour
Copy link
Owner Author

timotheecour commented Dec 8, 2020

  • some bugs with rand(float) corner cases:
when true:
  #[
  D20201208T123017:here
  ]#
  import std/random
  import std/fenv
  proc main()=
    echo rand(-Inf .. Inf) # bug: nan
    echo rand(0.0 .. Inf) # ok: inf
    echo rand(0.0 .. float.maximumPositiveValue) # ok: 5.992393389465399e+307
    echo rand(-float.maximumPositiveValue .. float.maximumPositiveValue) # bug: inf
    echo rand(float.maximumPositiveValue) # ok: 7.976840732228883e+307
    echo rand(Inf) # ok: inf
  main()

@timotheecour
Copy link
Owner Author

timotheecour commented Dec 8, 2020

  • make random threadsafe if there is no meaningful overhead

This proc uses the default random number generator. Thus, it is not thread-safe.

(either using threadvar or using locks)

@timotheecour
Copy link
Owner Author

timotheecour commented Dec 9, 2020

@timotheecour
Copy link
Owner Author

timotheecour commented Dec 9, 2020

@timotheecour
Copy link
Owner Author

wyhash and wyrand are the ideal 64-bit hash function and PRNG respectively:
solid: wyhash passed SMHasher, wyrand passed BigCrush, practrand.
portable: 64-bit/32-bit system, big/little endian.
fastest: Efficient on 64-bit machines, especially for short keys.
simplest: In the sense of code size.

(suggested by @xflywind)

@ringabout
Copy link
Collaborator

ringabout commented Dec 28, 2020

@ringabout
Copy link
Collaborator

  • rand(typedesc) / next doesn't work in JS backend
    rand(int8) => JS

@timotheecour
Copy link
Owner Author

  • add sampleBuffer
  • changing randomize() to use the fresh new std/sysrandom, then users can choose depending on how they call:
# CSPRNG
var r = initRand(opt = useSysrand)
sampleBuffer(r, buf, alphabet)

# PRNG
var r = initRand(opt = useTime)
sampleBuffer(r, buf, alphabet)

# CSPRNG
randomize() # could be changed to: randState = initRand(opt = useSysrand)
sampleBuffer(randState, buf, alphabet) # uses default RNG

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants