[Python-Dev] RNG in the core (original) (raw)

Matthieu Brucher matthieu.brucher at gmail.com
Tue Jan 3 18:23:08 CET 2012


Hi,

I'm not a core Python developer, but it may be intesting to use a real Crush resistant RNG, as one from Random123 (a parallel random generator that is Crush resistant, contrary to the Mersenne Twister, and without a state).

Cheers,

Matthieu Brucher

2012/1/3 Christian Heimes <lists at cheimes.de>

Hello,

all proposed fixes for a randomized hashing function raise and fall with a good random number generator to feed the random seed. The seed must be created very early in the startup phase of the interpreter, preferable before the basic types are initialized. CPython already have multiple sources for random data (win32urandom in Modules/posixmodule.c, urandom in Lib/os.py, Mersenne twister in Modules/randommodule.c). However we can't use them because they are wrapped inside Python modules which require infrastructure like initialized base types. I propose an addition to the current Python C API: int PyOSURandom(char *buf, Pyssizet len) Read "len" chars from the OS's RNG into the pre-allocated buffer "buf". The RNG should be suitable for cryptography. In case of an error the function returns -1 and sets an exception, otherwise it returns 0. On Windows I can re-use most of the code of win32urandom(). For POSIX I have to implement os.urandom() in C in order to read data from /dev/urandom. That's simple and straight forward.

Since some platforms may not have /dev/urandom, we need a PRNG in the core, too. I therefore propose to move the Mersenne twister from randommodule.c into the core, too. typedef struct { unsigned long state[N]; int index; } PyMTRandomState; unsigned long PyMTGenRandInt32(PyMTRandomState *state); // genrandint32() double PyMTGenRandRes53(PyMTRandomState *state); // randomrandom() void PyMTGenRandInit(PyMTRandomState *state, unsigned long seed); // initgenrand() void PyMTGenRandInitArray(PyMTRandomState *state, unsigned long initkey[], unsigned long keylength); // initbyarray I suggest Python/random.c as source file and Python/pyrandom.h as header file. Comments? Christian


Python-Dev mailing list Python-Dev at python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/matthieu.brucher%40gmail.com

-- Information System Engineer, Ph.D. Blog: http://matt.eifelle.com LinkedIn: http://www.linkedin.com/in/matthieubrucher -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20120103/c022dd8e/attachment.html>



More information about the Python-Dev mailing list