status-im / nim-websock

Websocket for Nim
83 stars 15 forks source link

align RNG use with `nim-eth` #144

Closed etan-status closed 1 year ago

etan-status commented 1 year ago

Update rand with the implementation from nim-eth (no unused var x) and change use to have ref explicitly for the Rng context, similar to how it is done in the other Status libraries. Further align name to SecureRngContext (https://github.com/status-im/nim-eth/pull/617)

etan-status commented 1 year ago

Splitting this into multiple PRs

arnetheduck commented 1 year ago

one thing we could do is create a nim-randoms library that provides utilities on top of RNG:s - the most common one is to reduce random bytes to an integer range but there are others as well (for example different distributions etc, like C++ does it). We ended up deciding that bearssl is not the right place for these utilities but we don't really have a good place to put them so at some point, it probably will make sense to write a new library - until then, copy-pasting is not a bad second option (it's trivial enough for most of our use cases)

etan-status commented 1 year ago

Yes, that's the logical next step. Just because it is a small library doesn't mean it can't exist.

copy-pasting is not a bad second option (it's trivial enough for most of our use cases)

We have quite a jungle already.

I think nim-randoms is the way to go, for both the newRng function, and the tiny helpers. When sysrand becomes viable, simply change the typealias to be an empty object instead and it will propagate through all Status libs. It may still be nice to have the concept of a RNG context for environments that actually need to do things, e.g., embedded, that may have to turn on some hardware.

arnetheduck commented 1 year ago

a RNG context for environments

yes, like any other code, relying on globals makes the code less flexible for a number of reasons - I don't see sysrand becoming a viable option any time soon also because it complicates security analysis in that each sysrand must be evaluated separately on different os's and hardware. bearssl (or any other cryptoprng) isolates us from that complexity (keeping in mind that most of the cryptography we use completely breaks down if the randomness source is broken, ie all the way from leaking private key details to allowing invalid signatures to pass etc etc)

etan-status commented 1 year ago

nim-randoms : https://github.com/status-im/nim-randoms/pull/2