micropython / micropython

MicroPython - a lean and efficient Python implementation for microcontrollers and constrained systems
https://micropython.org
Other
19.5k stars 7.8k forks source link

Pseudo-random number generator #965

Closed pfalcon closed 8 years ago

pfalcon commented 10 years ago

It's nice that stmhal port has hw random generator, but we really should implement port-independent reproducible PRNG. I can think of following choices:

  1. Implement exact same algo as in CPython. This will help achieve 100% reproducibility for testing, etc.
  2. Implement the smallest (by code size) reasonable PRNG.
  3. Implement cryptographically-secure PRNG.
dpgeorge commented 10 years ago

CPython uses Mersenne Twister which has a large state (2496 bytes). This is pretty large for "micro". So -1 for CPython compat.

Smallest by code size would be to write no code, and this can be achieved by reusing AES cipher: your key is the seed and you generate raw AES blocks which are the pseudo random numbers. This has the advantage of being cryptographically secure. (I assume here we have implemented AES for SSL.)

dpgeorge commented 10 years ago

The AES implementation I wrote uses 208 bytes of state for AES 128, and 272 bytes for AES 256.

dpgeorge commented 9 years ago

I just learned that the Python ssl module has a set of functions for crypto-secure PRNG: https://docs.python.org/3/library/ssl.html#ssl.RAND_bytes

dpgeorge commented 8 years ago

Point 2 above (Implement the smallest (by code size) reasonable PRNG) was implemened in a58a91eb04c50daafb31228a79f6752374338c5e using Yasmarang generator.