Giter Site home page Giter Site logo

Comments (2)

ctarbide avatar ctarbide commented on August 25, 2024 1

Thank you, learned a lot from your answer. Closing the issue, best regards.
--ctarbide

from monocypher-rng.

LoupVaillant avatar LoupVaillant commented on August 25, 2024

Hi,

Actually, arc4random(3) and what I do here have very different use cases.

  • arc4random(3) is a system RNG, that you can use safely without asking any question, if it is available on your system. If it’s not, you can usually re-implement it by using whatever system call you have available on your OS.
  • Monocypher-RNG user space RNG with no dependencies (save Monocypher, which really has no dependencies). That makes it extremely portable, but the flip side is that’s it’s a freaking footgun: it’s not thread safe, it’s not fork safe, and basically it’s very easy to catastrophically misuse.

My advice: Monocypher-RNG, like any userspace RNG, should only be used as an absolute last resort. If you can use arc4random(3), use it. If you have another system RNG, implement arc4random(3) on top of it. If your RNG has limitations, get around them: retry as often as you need to, don’t hesitate to panic upon unrecoverable errors, and request at most 32 bytes at a time (use Chacha20 to expand those bytes into a stream of the required length). If you don’t have an RNG at all, then you can consider a user space RNG, but then you need to think hard about how you’re going to seed it.

Now, users pace RNGs do have one advantage over system RNG: once seeded, if used correctly, they cannot fail. That means you can request 32 random bytes when you initialise your program, and if that step succeeds, your RNG will never ever be a source of failure or crash. That is, if you use it correctly: you need to think about thread safety and fork safety: you need to spawn a separate RNG state for each thread, times the number of forks. It can be done, but personally I wouldn’t risk it except for simple programs like a single threaded batch program, or low stakes, such as a video game’s gameplay RNG.

Now there’s a really big advantage to user space RNG, that often apply to video games, but can be nice for simulations as well: it’s deterministic. If you remember the seed, you can re-generate the entire random stream, which can help reproduce exactly what happened on the first run. This is excellent for debugging (just replay the input logs), or if you want a replay system in your game.


If we’re talking about internal design, well… the biggest advantage of Monocypher-RNG is that it’s extremely short, and has no dependency besides Monocypher. Though when you look at it, arc4random(3) isn’t that big either, and given its role as a system RNG, its dependencies don’t look unjustified.

from monocypher-rng.

Related Issues (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.