Live data from Hacker News

Inverting the Xorshift128 random number generator

littlemaninmyhead.wordpress.com

41–50 of 54 posts

Re: Inverting the Xorshift128 random number generator

#41
post #31
post #12

Earlier quoted context omitted.

Perhaps put a warning in the name since the folks who don’t read the docs are the ones you’re trying to protect? For example: Math.RandomNotCrypto() When someone uses that in production for cryptographic purposes (and, yes someone is going to do that), they have to wear a dunce cap to the office for a month.

People are likely to use it in security-relevant ways without being aware that the use case constitutes “crypto”.

Exactly - I'm just generating random session ids, I'm not encrypting anything (or using any bitcoins). There's no crypto here, right?

Re: Inverting the Xorshift128 random number generator

#42

Xorshift128+ is not a cryptographic rng though, so at least this isn't a cryptographic attack... Should programming languages use cryptographic rngs like a ChaCha20 based one in their standard libraries to stop accidental use of non cryptographic rngs for cryptographic purposes? But that comes at the cost of speed

I think some naming conventions could go a long way. If you want to import `fast_unsafe_random`, you might think twice.

funsafe_random

Re: Inverting the Xorshift128 random number generator

#43
post #8

I have recently replaced Lua's random for this implemetation https://nullonerror.org/2025/08/02/replacing-lua-s-math-rand...

A small note: Lua 5.4 changed the way random happens.

Its now based on xoshiro256**.

Thats still not crytographically secure, but it is vastly faster.

Re: Inverting the Xorshift128 random number generator

#44
post #34

Xorshift128+ is not a cryptographic rng though, so at least this isn't a cryptographic attack... Should programming languages use cryptographic rngs like a ChaCha20 based one in their standard libraries to stop accidental use of non cryptographic rngs for cryptographic purposes? But that comes at the cost of speed

> But that comes at the cost of speed That is mostly a myth. I mean... technically, yes. But the cost is so marginal that you will have a hard time even measuring it unless you generate gigabytes of data. For pretty much all common use cases like generation of ids, tokens, etc., you can use a secure random number generator and it will not impact your performance in any meaningful way.

It's also the exact same silly argument as for the memory unsafety.

Incorrect isn't faster, it's just wrong, I can have wrong instantly and you're not faster than that, or smaller, or cheaper, or easier to understand. So you're just much worse.

Re: Inverting the Xorshift128 random number generator

#45
> I was truly amazed to see ChatGPT understand my reasoning and even come up with its own ideas to help improve the research.

ChatGPT did nothing of the sort. The creators of ChatGPT happened to have in their corpus a sufficient amount of text from related research, forums and blogs discussing RNGs, and related math and programming topics to create a model that can generate plausible-sounding synthetic text.

Re: Inverting the Xorshift128 random number generator

#46
post #41
post #31

Earlier quoted context omitted.

People are likely to use it in security-relevant ways without being aware that the use case constitutes “crypto”.

Exactly - I'm just generating random session ids, I'm not encrypting anything (or using any bitcoins). There's no crypto here, right?

Anakin Padme 4 Panel "right?" meme.

Re: Inverting the Xorshift128 random number generator

#47
post #4

looking at the CVE report itself, Math.random() not being crypto-level seems to be known? - and vulnerability comes from Node.js using it for some crypto purpose so OP simply did a good exercise for himself recreating exact weakness of it

No, the post takes the attack from 5 observations down to 3.

no, the post takes a shoddy quickly-made implementation of an attack and improves it to its own better implementation of an attack

neither are professional frontline research, because said frontline work has already been done long loong ago when Xorshift was popularized and definitely when it became javascript's *default* rng

this is recreational cryptography, don't over-present it

Re: Inverting the Xorshift128 random number generator

#48
post #4

Earlier quoted context omitted.

No, the post takes the attack from 5 observations down to 3.

no, the post takes a shoddy quickly-made implementation of an attack and improves it to its own better implementation of an attack neither are professional frontline research, because said frontline work has already been done long loong ago when Xorshift was popularized and definitely when it became javascript's *default* rng this is recreational cryptography, don't over-present it

Please tell me more about what does and doesn't qualify as recreational cryptography.

Re: Inverting the Xorshift128 random number generator

#49
post #4

Earlier quoted context omitted.

No, the post takes the attack from 5 observations down to 3.

no, the post takes a shoddy quickly-made implementation of an attack and improves it to its own better implementation of an attack neither are professional frontline research, because said frontline work has already been done long loong ago when Xorshift was popularized and definitely when it became javascript's *default* rng this is recreational cryptography, don't over-present it

You can call it recreational cryptography. I am no longer a professional cryptographer: I used to be. Now I have a full time job in the software industry and a family with kids. I don't have a lot of free time to work on cryptography like professional cryptographers do.

The person you replied to is correct. To my knowledge, the best inversion of Math.random( ) is this one: https://github.com/PwnFunction/v8-randomness-predictor . It takes 5 outputs from Math.random( ) to determine the seed. My research included a 2^50 algorithm to get it with 3 outputs. If there is a better implementation out there that does it in less than 2^50 work for 3 outputs, could you please provide a link to the implementation?

Also, as I said in the blog, this is a first step. I think I can bring it down by a factor of 2^6 with another trick I am working on, but details are still being tested. As the saying goes, attacks always get better, never worse.

The blog is also to encourage the aspiring or amateur cryptographer to have a look themselves. Nothing in my research is particularly deep, so I hope it shows a wider audience that what cryptographers do doesn't always require complex mathematics. This is a simple attack and I thought it was worth blogging about.

I have another blog about why I left cryptography. Part of it is about being stuck in doing research that has no practical implications. To a real cryptographer, attacking XorShift128+ and Math.random( ) may seem uninteresting. I have a different view. Engineers make mistakes and use the wrong tools for the job all the time. We tell them it is wrong, but it is so much more powerful to prove it. When I looked at CVE-2025-7783, I just shook my head: the web security community is stuck using less than ideal tools (requiring 5 outputs to invert the algorithm) because the cryptographic community does not value producing tools to invert things that are not designed for cryptographic purposes. I think this attitude is doing a disfavour to the web security community.

Re: Inverting the Xorshift128 random number generator

#50
post #35
post #5

If you represent the state as a 128-long vector of GF(2) elements, you can model the state transition function as a matrix multiplication. This allows you to describe any output bit (at any offset in the output stream) as a function of the 128 initial state elements. Treating the initial state vector as 128 unknown variables, you can solve for them (via gaussian elimination) with any 128 bits from the output stream,…

Exactly. I've implemented a xorshift-based rng inverter previously, and here's the implementation for the algorithm in the article: https://github.com/m1el/samaras/blob/master/src/xorshift128....

The blog is not about going from one known internal state to the previous internal state.

It is about not knowing the internal state but figuring it out after having 2 (or 3) consecutive outputs of the random number generator.

Post reply on HN