News for hackers. Stuff that matters.
Re:Math.random in V8 is broken by pygy_ on Friday November 20, 2015 @10:54PM (#10605487)
The v8 Math.random() code was changed yesterday[0], maybe in response to this. However, the update appears to be misguided[1]...

0. https://github.com/v8/v8/commit/623cbdc5432713badc9fe1d605c5...

1. https://codereview.chromium.org/1462293002/#msg12

Re:Math.random in V8 is broken by hacst on Saturday November 21, 2015 @10:48AM (#10607037)
I must be missing some communication medium / tracker reference or is the information listed in the code-review really all there is on this change?

Full commit message: "Tweak RNG." Reviewer comment: "LGTM" landed

No reason for the change, no source code comment, no discussion?

Re:Math.random in V8 is broken by deckar01 on Saturday November 21, 2015 @11:43AM (#10607195)
A commit that is 90% refactoring and 10% critical system changes is a sin. Don't hide the important stuff, separate refactoring into its own commit.
Re:Math.random in V8 is broken by je42 on Saturday November 21, 2015 @02:23AM (#10606052)
I don't understand why they don't go for mersenne twister ?!
Re:Math.random in V8 is broken by bhickey on Saturday November 21, 2015 @08:22AM (#10606732)
I haven't looked closely enough at their choice to evaluate it, but there's a good reason not to use Mersenne Twister. MT is a bad generator.

* The state size is 2kb

* It fails some rudimentary statistical tests

* The output is trivially predicted

* It isn't particularly fast

* It lacks multi-stream support

If you need a CSPRNG use ChaCha20 or AES. For simulation use PCG.

Re:Math.random in V8 is broken by SeanLuke on Saturday November 21, 2015 @12:14PM (#10607299)
I wouldn't touch PCG yet. It's based on a single paper submitted to a journal which hasn't even been reviewed yet. Has there been any independent testing of the algorithm? Not that I know of. And the entire PCG website appears to have been built by the paper author as a promotional tool, which is a bit spooky given that the paper hasn't even been published.

MT is far from a "bad generator". It doesn't pass a few stringent TestU01 tests, which is the case for a number of very well regarded generators.

Re:Math.random in V8 is broken by bhickey on Saturday November 21, 2015 @06:02PM (#10608388)
What failure mode are you worried about? Yes, the author has a definite incentive to self-promote. At the same time she's produced a testable artifact. I think her claims about predictability are bogus, but the rest of it we can trivially verify on the merits.

Simple PCG PRNGs are fast and pass TestU01 BigCrush. Someone could conceivably cook up a new battery of tests that it fails on, but we'd need to reevaluate the whole zoology of RNGs at this point.

Edit: Sorry, I was playing loose classifying MT's BigCrush failures as rudimentary. That said, I think failing any part of BigCrush should disqualify a PRNG from use.

Re:Math.random in V8 is broken by SeanLuke on Saturday November 21, 2015 @10:36PM (#10608972)
> That said, I think failing any part of BigCrush should disqualify a PRNG from use.

Would include the PCG family then?

Re:Math.random in V8 is broken by bhickey on Saturday November 21, 2015 @11:20PM (#10609064)
No, it isn't a reasonable comparison: PCG is a family while MT is particular Generalized Feedback Shift Register. Despite my suspicions I can't tell you off the top of my head if the entire GFSR class is dodgy, but we can point out flaws in a single generator. I buy O'Neill's arguments (other than those about security, which are a load of hooey) and I think added scrutiny will bear her out. Usually what I see is people flocking to MT because of its comically large period

When it comes down to it, PRNG quality gets assessed empirically. Why would you use ever a generator with 2kb of state when faster & smaller generators like PCG or Xorshift* pass the same statistical battery?

You can also write proofs about their behavior to ferret out the lousy ones. For example, we can demonstrate that PCG is full period. Consider the inner LCG:

    x1 = (ax0 + c) % m
The modulus term is equivalent to the size of `x`, so in practice you can skip it and rely on overflow. By requiring that `c` is odd and `m` is a power of two, we know that `c` and `m` are coprime. If you select `a` such that (a - 1) % 4 = 0, the inner LCG is full period.

If you look at the permutation function, it's also provably unbiased by counting. Since all possible bit sequences are fairly represented in the PCG state we know that all combinations of permuting bits appear with all possible output bits.

Re:Math.random in V8 is broken by adrae5df on Saturday November 21, 2015 @12:26PM (#10607350)
I was just researching PCG, and I didn't find anything relevant apart from the the author's paper and website. I was trying to find out if it has been peer reviewed since its release last year. I'm really curious, how did you check if this paper was peer reviewed?
Re:Math.random in V8 is broken by acqq on Saturday November 21, 2015 @09:59AM (#10606935)
The best advice of all I've seen in the comments here. The PCG page:

http://www.pcg-random.org/

contains a nice comparison table.

Re:Math.random in V8 is broken by adrae5df on Saturday November 21, 2015 @11:06AM (#10607082)
Can you name those rudimentary statistical tests that Mersenne Twister fails. I would like to test them out, since I regularly use MT. Thank you.
Re:Math.random in V8 is broken by acqq on Saturday November 21, 2015 @11:37AM (#10607175)
Apparently here: https://en.m.wikipedia.org/wiki/TestU01

Wikipedia entry for MT specifies some of the problems.

Re:Math.random in V8 is broken by mmalone on Saturday November 21, 2015 @01:25PM (#10607542)
MT only fails LinearComp systematically. It is not a bad generator, it is a very good generator. The biggest problem is the "escape from zero-land" stuff where it's biased towards zero for a while if its seed is mostly zeros, but other generators have the same problem (though not to the same extent) and it's still statistically very unlikely to be a problem and it self corrects as the sequence runs.

Comparing to ChaCha20, which is a CSPRNG, is a little apples/oranges.

Re:Math.random in V8 is broken by acqq on Saturday November 21, 2015 @03:10PM (#10607866)
The author (or authors?) of MT have some more recent generators that are faster, what's the reason for staying with the original MT?
Re:Math.random in V8 is broken by mmalone on Saturday November 21, 2015 @04:49PM (#10608162)
Just familiarity. Mostly I just can't recommend something I don't know much about. The SIMD variant of MT is architecture specific, I thought? Other newer variants of MT/WELL generators and PCG are probably all good options. Also xorshift*.

But I know that MT is a very good algorithm and using it has a certain pragmatic appeal - you make a lot of people's lives easier because when they Google to learn more about your generator they'll find good information quickly.

Re:Math.random in V8 is broken by adrae5df on Saturday November 21, 2015 @11:45AM (#10607203)
The entire TestU01? Wow, I did not know that.
Re:Math.random in V8 is broken by acqq on Saturday November 21, 2015 @11:50AM (#10607222)
I thought you were interested to find the tests. The tests that fail are there. MT doesn't fail all, far from it. But there are RNGs that don't fail these that MT fails that are faster. PCG was mentioned here.
Re:Math.random in V8 is broken by adrae5df on Saturday November 21, 2015 @12:16PM (#10607308)
The only information on PCG I can find is the author's own paper which also fails some TestU01 tests. I am much more inclined to trust an established generator than an unproved one.
Re:Math.random in V8 is broken by bhickey on Saturday November 21, 2015 @05:47PM (#10608348)
PCG is a family of PRNGs. Any sensible member of the family will trivially pass TestU01 BigCrush. If you use a 32-bit PCG it's going to necessarily fail BigCrush.

PRNG design is an empirical science. We can implement a generator and throw it into a statistical battery. If one passes, we've got a constructive proof of goodness. Any of us can run BigCrush and verify the results. Unlike CSPRNGs, we don't need to sit around and wait for the results of cryptanalysis.

Re:Math.random in V8 is broken by acqq on Saturday November 21, 2015 @06:49AM (#10606551)
MT is "on average" maybe "as fast" but the key is "on average:" depending on the usage pattern it can cost much more.

This specific RNG follows a traditional approach (that is, like in C or Java standard libraries) to have one algorithm in the standard library that is "as fast as possible, as bad as possible." Which meant even "just multiply with constant, add another constant, return. Bad? Yes we know." So it's still on the level "it's two multiplies and two-three adds."

Traditionally, those who expect some specific properties, like the author of the article, should have actually used the implementation they added to their code (for example MT if it fits) if they ultimately cared about the speed and wanted exactly the properties they know they needed, or if they just didn't care about the speed used some much "slower" but "much better" library.

Maybe the tradition can be broken in this specific case, I really don't know which benchmarks define what's acceptable here. Note also that the implementation must be verified, just the fact that there is somewhere some implementation available doesn't even mean that it does what is expected to do. The devil is in the details here.

Re:Math.random in V8 is broken by acqq on Saturday November 21, 2015 @06:42AM (#10606532)
Did anybody make any tests of the resulting streams and compared the period and the properties? The code which inspired the article is obviously able to discover certain weaknesses.