Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Squares RNG 64bit implementation differs from paper #25

Open
tridao opened this issue Jan 11, 2025 · 2 comments
Open

Squares RNG 64bit implementation differs from paper #25

tridao opened this issue Jan 11, 2025 · 2 comments

Comments

@tridao
Copy link

tridao commented Jan 11, 2025

Hi,

I was just reading the Squares RNG implementation here and it seems to differ from the paper.
Screenshot 2025-01-11 at 6 23 39 PM
In round 4, the paper has

t = x * x + z;
x = t;
x = (x >> 32) | (x << 32);
// t is then used to xor with the result of round 5

However, in the code here,

x = round(x, z);

you have

x = round(x, z);  // i.e. x = x * x + z; x = (x >> 32) | (x << 32);
// x is then used to xor with the result of round 5

So the difference is that the paper uses t to xor while the code uses (t >> 32) | (t << 32) to xor.

@Shihab-Shahriar
Copy link
Collaborator

Hi @tridao, thanks for bringing this to our attention. And you're absolutely correct, this is a bug. I think it's a bit interesting that this RNG still managed to pass practrand and testU01 battery of statistical tests.

One thing to note, and perhaps I should make it more explicit in the doc, please don't use 64bit seed with this particular implementation of Squares. Squares algorithm requires the key (i.e. seed here) to have certain properties (e.g. "The key should be an irregular bit pattern with roughly half ones and
half zeros"), whereas we wanted to allow developers to use any unique integer as seed, and the hashing we use to solve this forced us to trim down seed.

@tridao
Copy link
Author

tridao commented Jan 18, 2025

Yup I saw that the seed is uint32_t and you need extra processing. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants