Random/LFSR on P2
Ahle2
Posts: 1,179
As I understood it, the P2 Will have a built in LFSR random generator. Is that right?
Which bits are tapped? How long is the cycle?
I would like to run it trough the Dieharder and Testu01 randomness suites to see how well it performs.
IMHO, it must be good enough to be "indistinguishable" (according to these tests) from a TRNG for it to be usable for anything other than fun and games. (a pure LFSR never succeeds these tests, there needs to be a little bit more logic than that)
Even better would be if it the internal states of the PRNG never propagates outside the HW in combination with some way of seeding. Just wishful thinking because I know that a CSPRNG is a complex matter. ☺
/Johannes
Which bits are tapped? How long is the cycle?
I would like to run it trough the Dieharder and Testu01 randomness suites to see how well it performs.
IMHO, it must be good enough to be "indistinguishable" (according to these tests) from a TRNG for it to be usable for anything other than fun and games. (a pure LFSR never succeeds these tests, there needs to be a little bit more logic than that)
Even better would be if it the internal states of the PRNG never propagates outside the HW in combination with some way of seeding. Just wishful thinking because I know that a CSPRNG is a complex matter. ☺
/Johannes
Comments
Such a coincidence! I have not been active in the P2 community for a long time and just came to think of the P2 and PRNG when I had the needs for a better PRNG than whats available in the std C++ library. To be honest I don't think a simple LFSR is good enough even for robot application. My experience with pure LFSR is that they are much less good than what most people think. A simple random image generated this way will reveal obvious patterns. Even a PRNG that is considered "useless" by the "PRNG community" still produce images that the eye can't distinguish from a TRNG. Xoroshiro is dead simple and would make the randomness soooo endlessly much better.
Have a look at this link for a VHDL implementation.
http://jorisvr.nl/article/random-numbers-VHDL
If the LFSR in the P2 is available somewhere I would be happy to produce random images and run it trough some test suites.
That's all it takes to fool even the best randomness tests!
Have a search for the "real random" object. I'm sure it's in OBEX somewhere. http://forums.parallax.com/discussion/93061/real-random-number-generator-object
Downside is that it uses a whole COG to generate real random bits.
Late time I asked the P2 was also going to be capable of such real random generation.
So I created a version of the JKISS32 PRNG in Spin.
http://forums.parallax.com/discussion/136975/jkiss32-high-quality-prng-with-realrandom-seed-save-a-cog
JKISS32 is a high quality PRNG with a period of 2^121, or 2.6*10^36, by David Jones. It's a version of George Marsaglia's KISS32 PRNG that needs no multiply, divide or mod. Like so.
David Jones original paper here: http://www0.cs.ucl.ac.uk/staff/d.jones/GoodPracticeRNG.pdf
Note that the Diehard test suite was developed by G. Marsaglia. His orginal description of KISS is here: https://groups.google.com/forum/#!msg/comp.lang.fortran/5Bi8cFoYwPE/pSFU7NaK224J
These simple PRNG are wonderful because they are very small simple and fast, compared to the famous Mersenne Twister. They have massively long periods and do well in tests of randomness. They are not cryptographically strong though and should not be used for crypto.
Like so: http://www.cryogenius.com/hardware/rng/
Works a treat. My home made discrete schmit trigger following it does not though, seems to be too slow to keep up.
I was not aware of the TRNG object from Chip. Sooo cool! If we could dedicate one of the 16 cogs in the P2 to be a TRNG, that solves everything! I still think a random instruction in hardware should be better than this!
It's better to leave it out entirely if it's just a LFSR!
/Johannes
That KISS algorithm is neat!
Just ran it on my P2 and it works great.
You can clearly see the shift operation in action vertically.
/Johannes
There is a Spin-only version of realRandom, and there is a PASM engine that uses the RealRandom technique to generate random seed bits slowly, but continuously runs JKISS32 in the background, periodically updating the JKISS32 seed bits with the real random ones generated. All of that updates a long in Hub RAM as fast as possible. It's fast enough that you could do back-to-back assignments in Spin and get different random numbers, no wait needed.
Jonathan
The 'regscan' part (rndx) is the 32-bit LFSR which gets seeded with 32'b1 on reset.
'rndy' reorders those bits for eventual output. This gets rid of the obvious shift pattern.
'rnd' takes offsets of those 32 reordered bits and xor's them with different patterns for final output to the 16 cogs.
'pin_rnd' is the same data as 'rnd', but gets used as 64 bytes, one per smart pin.
This all means that each cog gets a different view of the LFSR, as well as each smart pin. We could change to the "xoroshiro" method, no problem.
In the Prop2, a smart pin's ADC could serve as a source for a TRNG. Just put the pin into ADC calibration mode and observe the LSB of output.
I don't get C. Is there some kind of initialization going on near the top?
That would make sense, but why is s0 assigned as 'const uint64_t' and s1 isn't?
Maybe it should look like this?
xoroshiro is designed for 64 bit machines. Where as JKISS is designed for 32 bitters. If that is of any consequence.
And the definition of s is missing. Presumably it's uint64_t s[2];
It can be cast away anywhere in the codebase, so it's not a reliable thing for the optimizing compiler anyway.
-Phil
They are notorious for being terrible PRNGs.
The graphic above may be exceptionally bad but I have seen the outputs of various common random functions plotted in various ways and patterns show up very clearly.
Sadly I can't find any examples just now.
The rotates could be optimized, but this was simplest to think about.
It looks pretty random, to me.
If we can be sure this is correct, I'll update the PRNG in the hub to use this, instead.
I made a video of it:
JKISS32 is especially optimized for 32 bit machines.
Admittedly it's "quality" may be a bit less but it's pretty damn good anyway.
Original source code for xorooshiro128+ is here:
http://xoroshiro.di.unimi.it/
http://xoroshiro.di.unimi.it/xoroshiro128plus.c
NOTE: I notice in the source it says:
"The state must be seeded so that it is not everywhere zero."
xoroshiro can never generate a zero output.
Do you think the smart pin LSB will act as a good TRNG? Maybe smart pins in combination with a zener diode and a amplifier!?
@Evanh
Have you tried your zener TRNG with some RNG test suites? It would be cool to see how well it performs. I guess It would pass with Flying colors!