Live data from Hacker News

Need a PRNG? Use a CSPRNG

sortingsearching.com

61–70 of 105 posts

Re: Need a PRNG? Use a CSPRNG

#61
post #4

Even simpler than implementing a cryptographic PRNG yourself, just use /dev/urandom, which will provide an infinite, non-blocking stream of cryptographically-strong pseudorandom bytes. All platforms currently have known-good implementations of /dev/urandom -- Linux uses ChaCha and MacOS and BSDs use Bruce Schneier's Fortuna.

[deleted]

Re: Need a PRNG? Use a CSPRNG

#62
post #53

Earlier quoted context omitted.

The same is true for CSPRNG. Same seed -> same output.

I'm confused; if you can predict the output, then isn't it by definition not secure? Or is the seed so hard to brute force that it's equivalent to breaking the underlying cryptography.

Guessing the seed is impossible using current knowledge of information theory and predictable breakthroughs in new computer hardware. That may or may not include quantum.

Entropy sources in for instance Linux constantly receive new data, but there are CSPRNGs that only receive truly random data at construction time, and you can intentionally expose that data in your testing code. Or to be precise, since this is security we are talking about: You can set your bootstrapping code to take a seed as input, and your tests can generate and log that seed, while the real code picks its own (secret) value and keeps it safely away from storage.

I don't know how Java works now but it worked this way about ten years ago.

It takes a lot of work to get people to use randomized testing properly. Seed reproduction is table stakes IMO and yet I've had to introduce it a couple of times because it was missing. Getting people to look at test failures instead of clicking 'run again' is a whole psycho-social tarpit.

In fact I suspect whoever invented Property Based Testing was trying to route around this tarpit - do random tests, then provide concrete repro steps, so people can't ignore them and hope.

Re: Need a PRNG? Use a CSPRNG

#63
post #54
post #29

Earlier quoted context omitted.

What are you talking about? Jason Donenfeld is the author of WireGuard, the extraordinarily popular VPN protocol that cannot use NIST cryptography (it does no negotiation, and is built on a version of Noise that uses ChaPoly and 25519). The change that was just described to you was a shift from NIST cryptography to non-NIST cryptography.

> that cannot use NIST cryptography Do you mean as a matter of Donenfeld's engineering decisions (that those algorithms are unavailable in WireGuard)?

Yes: they use, for lack of a better term, DJB cryptography, and like many modern cryptosystems they eschew negotiation, so it's not straightforward to fit NIST algorithms in.

Re: Need a PRNG? Use a CSPRNG

#64
post #53

Earlier quoted context omitted.

The same is true for CSPRNG. Same seed -> same output.

I'm confused; if you can predict the output, then isn't it by definition not secure? Or is the seed so hard to brute force that it's equivalent to breaking the underlying cryptography.

People talking about seedable CSPRNGs for Monte Carlo work are proposing that you use CSPRNG algorithms, but in userland, as a library; instead of periodically rekeying them with unpredictable data collected from timing and hardware, you'd key it just once, with a known key, and thus generate a predictable sequence of "random" bits for your simulation.

Re: Need a PRNG? Use a CSPRNG

#65
post #53

Earlier quoted context omitted.

The same is true for CSPRNG. Same seed -> same output.

I'm confused; if you can predict the output, then isn't it by definition not secure? Or is the seed so hard to brute force that it's equivalent to breaking the underlying cryptography.

If you have the key to a ciphertext, you can decrypt it, but that doesn't make the cipher insecure. It is the same principle for the seed of a CSPRNG. In fact, a common cipher construction (ChaCha20, for instance) relies on a random stream seeded by the key, generated by the sender to encrypt data, which the recipient re-generates from that same key to decrypt the data.

Re: Need a PRNG? Use a CSPRNG

#66
post #44
post #17

An alternative to the post is to take any PRNG's output and calculate the SHA3 hash of it, then use the hash as the random number. If SHA3 is not available, use AES. In either case, all statistical weaknesses and side-channel attacks will be eliminated. The platform is far more likely to have support for either of those in the system libraries, than a bespoke CSPRNG, which would need to be packaged with your app/game…

The sponge design of SHA-3 makes it a CSPRNG basically. You can keep draining it after you fed the data in.

You can do something similar with any cryptographic hash; just feed a (say) 128 bit counter through the hash function, and collect the outputs.

Re: Need a PRNG? Use a CSPRNG

#67

But if you need a PRNG in a big hurry , an LFSR is probably fine. https://en.m.wikipedia.org/wiki/Linear-feedback_shift_regist... I use one on my NES projects that can compute a new 8bit PRNG value in something like 65 cycles

Xorshift has the best bang for the buck, easily. Literally three xors and shifts per generated number, no extra state, long period, reasonably high quality. But admittedly it would be trickier on a 8-bit platform…

Re: Need a PRNG? Use a CSPRNG

#68
post #53

Earlier quoted context omitted.

The same is true for CSPRNG. Same seed -> same output.

I'm confused; if you can predict the output, then isn't it by definition not secure? Or is the seed so hard to brute force that it's equivalent to breaking the underlying cryptography.

There is no such thing as a true random generator when we are talking about software - hence the P in PRNG. The seed value to any of these algorithms is meant to be the "true" random part - or as close to that as it can be.

The idea here is that given a truely random seed, the algorithm should subsequently generate random bits that aren't predicable from its last output. So given the first 128 bits of output you shouldn't be able to predict the next bit for example.

Given the same seed a PRNG, and a CSPRNG will always output the same bits.

Hopefully that explains it?

Re: Need a PRNG? Use a CSPRNG

#69
post #13

Earlier quoted context omitted.

I was curious. The rust rand library has implementations for a lot of rngs - prngs and csrngs. All with high quality implementations as far as I can tell. They agree with your numbers: their fast, high quality prng implementations are about 8gb/sec and chacha is about 2gb/sec: https://rust-random.github.io/book/guide-rngs.html

There are ways to make much faster PRNGs; I designed one that reaches 53 GB/s[0] (0.06 cycles per byte) with better quality than those listed in the article. But I agree with tczajka: good engineering practice ought to be to start with a CSPRNG. First make it work, then make it fast. With truly random data, you know your algorithm works, and can then check if there is a quality loss when switching away. The performan…

This is a very informative repo and associated blog posts.
Post reply on HN