[Python-Dev] RNG in the core

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


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


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 (win32_urandom 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 PyOS_URandom(char *buf, Py_ssize_t 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 win32_urandom(). 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;
> } _Py_MT_RandomState;
> unsigned long _Py_MT_GenRand_Int32(_Py_MT_RandomState *state); //
> genrand_int32()
> double _Py_MT_GenRand_Res53(_Py_MT_RandomState *state); // random_random()
> void _Py_MT_GenRand_Init(_Py_MT_RandomState *state, unsigned long seed);
> // init_genrand()
> void _Py_MT_GenRand_InitArray(_Py_MT_RandomState *state, unsigned long
> init_key[], unsigned long key_length); // init_by_array
> 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