8 ms·
Designing a New PRNG
- guimplen 5y agoIt is funny how the LCG is getting overlooked. As far as I know 128 bit state Lehmerthat outputs higher 64 bits works for every application. The problem with the old LCG wath that they had extremely small state (16 bit) and they just gave it right away. The 128 bit Lehmer is dead simple, very well studied and should work fast on any modern CPU (one 128 bit multiplication).
- tptacek 5y agoThis is all pretty confusing to me, because it's discussing a non-cryptographic RNG but talking about invertibility and "having been cracked" as a motivation for replacing that RNG. If you need a secure RNG, you use a CSPRNG. For all other cases: why is "having been cracked" motivating? This was always my complaint with PCG, that it talked about "difficulty to predict" and its suitability for... some kinds? of security applications? I don't know what those applications are, but somehow PCG targets them? If you care about this stuff, you should simply be using getrandom(2). For what it's worth: I get why you'd want a high-quality generator of insecure random data (for simulations and stuff), and that there's an interesting discussion to have about what generator does the best job at handling that workload. I'm just saying it seems to have nothing to do with invertibility and crackability.
- jcranmer 5y agoFrom what I understand of the matter, the crux of it is that there are two modern PRNG families whose creators are running around telling you to use their PRNG over the other's because <technical reasons>, where a) both of them are correct, and b) the <technical reasons> don't elucidate the tradeoffs being made to people whose prior understanding amounts to "I just want a source of random numbers!" This is contrast to something like a linear congruential generator, where the failure mode of the random data is pretty easy to explain.
- masklinn 5y agoI think you misread tptacek's line of questioning. It's not about the quality / superiority of what over what, but the initial motivation for rand switching away from PCG: > This was done after some concerns were raised by the author of xoshiro, who posted a blog entry on PCG and also raised an issue on the possibility of PCG being invertible. PCG and the XOR family are both non-cryptographic RNGs, how is predictability or invertibility relevant then? If those are concerns, surely a CSPRNG should be used? The "quality" of randomness is very much a concern in non-secure contexts e.g. patterns in an RNG can bias your simulation, but it doesn't seem like it'd matter if somebody can recover even your simulation seed.
- deleted 5y ago[deleted]
- sobriquet9 5y agoI think the allure here is the same as with ciphers. Everyone can create a cipher they cannot break, but cryptographers can break easily. Likewise, it's easy to invent another PRNG, but not so easy to figure out that, say, its output always falls on one of seventeen parallel planes when treated as consecutive (x, y, z) coordinates. If you want random numbers that are fast and almost-cryptographically-strong, you can take some ARX block cipher and reduce number of rounds as you see fit. But it's not nearly as much fun.
- zokier 5y agoSeems that vigna (author of the xoshiro family of rngs) mostly agrees with you > Personally, I don't believe in the "middle ground" of "difficult-to-predict-but-not-crypto": usually it just means it is so easy to break that nobody https://github.com/rust-random/rand/issues/905#issuecomment-551841670 https://github.com/rust-random/rand/issues/905#issuecomment-...
- omoikane 5y agoI wish that issue would also link to the rebuttal: https://www.pcg-random.org/posts/on-vignas-pcg-critique.html https://www.pcg-random.org/posts/on-vignas-pcg-critique.html
- tptacek 5y agoDo you find that rebuttal persuasive? For someone who doesn't care at all about anyone's "tone", and just wants to know what the hell is going on with these constructions, I didn't find much there.
- omoikane 5y agoMy reading of both: - Vigna claims small differences in internal states don't have avalanche effects, and the rebuttal says those small differences in internal states don't occur normally, not with the proper seeding steps. - Vigna claims to be able to predict one particular variant of PCG, and the rebuttal said that that was one of the weaker variants that specifically has "insecure" in the name. It's persuasive to me in suggesting that Vigna's claims are perhaps not that clear cut. I am not an expert in PRNG and I wouldn't say this makes PCG win over Xorshiro or not, I just think it's unfortunate that the issue above linked only one side of the argument, especially since I find the other side more pleasant to read.
- ttybird2 5y agohttps://www.pcg-random.org/ https://www.pcg-random.org/ PCG's statistical quality is "excellent" but chacha20's just "good"?
- loeg 5y agoI agree that the motivation is pretty confused. One of the straightforward goals is to be faster than PCG-64 or xoshiro256++ -- that seems reasonable.
- ilhamsgenius 5y ago
- ilhamsgenius 5y ago
- failrate 5y agoI've used noise functions as good noncryptographic PRNG. E.g. Squirrel3 hash Fast and cheap with no apparent periods.
- nullc 5y agoEvery RNG with a fixed size state has a period. If the period is not known, it's probably seed dependent and there may be some seeds with very small period. If 'very small' is small enough (such as 2 or 5) then those seeds will cause some ordinary uses to fail badly in obvious ways. If bad seeds are common enough (say 1 in 2^64) then you could actually see rare failures in production. (Cryptographic RNGs tend to avoid this risk just by having a pretty gigantic and well mixed state, so any bad seed-- if one exists-- would be extremely unlikely to be hit. They also usually have structures that are 'close' to permutations which makes it hard for them to have small periods.) For insecure small state RNGs the risk of small periods makes it useful to build RNGs based on structures that make it easy to know the period. Unfortunately, those same structures also tend to do things like make the RNG efficiently invertible. Invertibility itself shouldn't be a problem since these sorts of non-cryptographic RNGs should never be used where security is a factor, but the existence of invertibility has a bad smell even when you're just concerned that the pattern of the RNG may trigger pathological behavior in the underlying system (as the structure of LCGs often does). If it's not obvious to you why invertibility (or predictability in general) is a bad smell, think of it this way: Using the inversion I can create a randomness test that the RNG will fail (by recovering the state and checking it) and I can create an application that will fail using it. The application would be contrived, for sure, but it would exist. If we don't know how to predict the RNG then we also probably don't know how to create an application that will fail with it, even a contrived one (assuming the RNG otherwise has good statistical properties). Though this only goes so far, because with a small state every RNG is going to be invertible (up to seeds with equal output) by an exhaustive test.
- kittywav 5y agoJust a small comment: you can do things to ensure that the period of a PRNG is longer than a certain lower bound for any input seed. For example, if you make the state-mixing function depend on a 64-bit counter, you'll ensure that the period is at least 2^64 (assuming the state-mixing function is reversible).
- spekcular 5y agoDumb question: Why not just use AES? Most platforms have hardware acceleration so it should be pretty fast. Then there's no worrying about output quality. (Is there an application for which AES is not fast enough? Has it been benchmarked for speed against the various PRNG options?)
- jandrewrogers 5y agoPortability, most likely. I've seen AES-NI used as a convenient and fast PRNG but it would be a pretty sub-optimal choice for hardware platforms that don't have hardware acceleration.
- pcwalton 5y agoUsing AES-NI for small integer hashing in the Rust compiler was found to result in a measurable slowdown.
- oconnor663 5y agoI think the biggest gains from AES-NI are when you can use the vectorized versions. So random byte generation would benefit, but small integer hashing probably not? (Unless you could arrange to hash a vector of integers at once.) But this isn't something I've tried myself.
- matthewdgreen 5y agoSeriously. Cut this shit out. Design a cryptographic RNG or decide precisely how you intend to justify a non-cryptographic RNG that has cryptographic security properties (but like, not all of them.)
- foxfluff 5y agoIf you want to design a cryptographic RNG that falls between LCGs and PCG in speed, please have a go at it. Meanwhile we'll use our small and fast PRNGs and strive to make them as good as they can possibly be for non-cryptographic uses.
- matthewdgreen 5y agoI beg you to define what “good” means in a way that doesn’t imply security properties. And if you must demand security properties, god help you if you designed something that can’t provide them.
- 317070 5y agoSay I want to numerically approximate a high dimensional integral with MC sampling, say in a computational fluid dynamics simulation, or in a ray traced render. For this, I need consequent samples from a PRNG to be really uncorrelated and fast. But there is no adversary relationship, so I don't need cryptographic strength. I just cannot have any spurious correlations between samples, as they might throw off convergence speed or worse, introduce error.
- foxfluff 5y agoYou don't need to define good in an absolute sense to see how one thing can be better than another. The flaws of an MCG or LCG are easily witnessed, so it's totally fair for someone to go find out if it's possible to do better (i.e. remove obvious patterns, fix low period of low order bits, improve statistical quality) without sacrificing much performance; and indeed perhaps making it faster than a large-state LCG of comparable quality would be. If you admit that there are any legitimate uses for non-cryptographic PRNGs then I guess a 32 bit MCG (or "42") is just perfect for you (otherwise you'd know what's better and you don't need to ask anyone to define good). You're easy to please. If you think there are no legitimate use cases for these fast generators at all, then I'm sorry but a lot of people disagree and they will never rewrite their sims, noise generators, world generators, etcetra to use a CSPRNG no matter how much you whine about it.
- oconnor663 5y ago> After some minor optimizing, on my laptop here are benchmarks on filling a 1kb byte array: > > Mwc-256-X-X-A-64 (This algorithm): 100.98 ns That comes to 10 GB/s. For comparison, I think ChaCha8 implemented with AVX-512 should run at a similar speed. Maybe a bit faster in short-ish, single-threaded benchmarks. To be clear, there are plenty of caveats in making this comparison: ChaCha8 won't hit that throughput on any output size smaller than 1 KiB. AVX-512 requires a very recent processor. Most library implementations don't include AVX-512 support. Power usage will be high. Downclocking will be an issue, especially with multithreading. Etc. etc. But in exchange, you get all the security properties. I think one takeaway here is that the gap between cryptographic and non-cryptographic is smaller than many people think, which argues in favor of secure defaults. (Though to be clear, I don't think OP or anyone in this thread is opposed to secure defaults.) Another takeaway is that a lot of these blockers, like hardware and library support, will continue to improve over time. So if you find that a CSPRNG isn't fast enough for your use case today, make sure to check again in a few years. And of course, as always, it's important to benchmark your specific use case if you're worried about performance.
- loeg 5y agoChacha generates much bigger outputs (64 byte blocks vs 8 byte) and has somewhat bigger state (512 bits vs 256 or 128 bits). And AVX-512 is Intel-only. To make spinning up the AVX units worth it, you probably need to generate quite a lot of output in larger batches (ie, even bigger state required). It’s still interesting, but less competitive than looking at the GB/s for MB-sized outputs makes it seem.
- kevingadd 5y agoIf you actually care about performance you should be generating batches of output and pulling entropy out of it, not invoking the PRNG every time you need a random byte. The icache and dcache impact of running all that prng code is going to eat into the performance of your actual software in a way that microbenchmarks won't show. So the need to do that with chacha isn't necessarily a problem, it may be pushing you towards a better design.
- berkut 5y agoYeah, I really don't understand why security properties are that important for a non-cryptographic PRNG anyway either... Also, I'd personally argue that 'k-Dimensional Equidistribution' *is* actually fairly useful for a PRNG - certainly I'd argue it's more useful than secure properties in the use cases of simulation or MC...