From a58a91eb04c50daafb31228a79f6752374338c5e Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 17 Jan 2016 12:10:28 +0200 Subject: [PATCH] extmod/modurandom: Add "urandom" module. Seedable and reproducible pseudo-random number generator. Implemented functions are getrandbits(n) (n <= 32) and seed(). The algorithm used is Yasmarang by Ilya Levin: http://www.literatecode.com/yasmarang --- extmod/modurandom.c | 92 +++++++++++++++++++++++++++++++++++++++++++ py/builtin.h | 1 + py/mpconfig.h | 4 ++ py/objmodule.c | 3 ++ py/py.mk | 1 + py/qstrdefs.h | 6 +++ stmhal/mpconfigport.h | 1 + unix/mpconfigport.h | 1 + 8 files changed, 109 insertions(+) create mode 100644 extmod/modurandom.c diff --git a/extmod/modurandom.c b/extmod/modurandom.c new file mode 100644 index 0000000000..fb85eddcb3 --- /dev/null +++ b/extmod/modurandom.c @@ -0,0 +1,92 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Paul Sokolovsky + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include + +//#include "py/nlr.h" +#include "py/runtime.h" + +#if MICROPY_PY_URANDOM + +// Yasmarang random number generator +// by Ilya Levin +// http://www.literatecode.com/yasmarang +// Public Domain + +STATIC uint32_t yasmarang_pad = 0xeda4baba, yasmarang_n = 69, yasmarang_d = 233; +STATIC uint8_t yasmarang_dat = 0; + +uint32_t yasmarang(void) +{ + yasmarang_pad += yasmarang_dat + yasmarang_d * yasmarang_n; + yasmarang_pad = (yasmarang_pad<<3) + (yasmarang_pad>>29); + yasmarang_n = yasmarang_pad | 2; + yasmarang_d ^= (yasmarang_pad<<31) + (yasmarang_pad>>1); + yasmarang_dat ^= (char) yasmarang_pad ^ (yasmarang_d>>8) ^ 1; + + return (yasmarang_pad^(yasmarang_d<<5)^(yasmarang_pad>>18)^(yasmarang_dat<<1)); +} /* yasmarang */ + +// End of Yasmarang + +STATIC mp_obj_t mod_urandom_getrandbits(mp_obj_t num_in) { + int n = mp_obj_get_int(num_in); + if (n > 32 || n == 0) { + nlr_raise(mp_obj_new_exception(&mp_type_ValueError)); + } + uint32_t mask = ~0; + // Beware of C undefined behavior when shifting by >= than bit size + mask >>= (32 - n); + return mp_obj_new_int_from_uint(yasmarang() & mask); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_urandom_getrandbits_obj, mod_urandom_getrandbits); + +STATIC mp_obj_t mod_urandom_seed(mp_obj_t seed_in) { + mp_uint_t seed = mp_obj_get_int_truncated(seed_in); + yasmarang_pad = seed; + yasmarang_n = 69; + yasmarang_d = 233; + yasmarang_dat = 0; + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_urandom_seed_obj, mod_urandom_seed); + +STATIC const mp_rom_map_elem_t mp_module_urandom_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_urandom) }, + { MP_ROM_QSTR(MP_QSTR_getrandbits), MP_ROM_PTR(&mod_urandom_getrandbits_obj) }, + { MP_ROM_QSTR(MP_QSTR_seed), MP_ROM_PTR(&mod_urandom_seed_obj) }, +}; + +STATIC MP_DEFINE_CONST_DICT(mp_module_urandom_globals, mp_module_urandom_globals_table); + +const mp_obj_module_t mp_module_urandom = { + .base = { &mp_type_module }, + .name = MP_QSTR_urandom, + .globals = (mp_obj_dict_t*)&mp_module_urandom_globals, +}; + +#endif //MICROPY_PY_URANDOM diff --git a/py/builtin.h b/py/builtin.h index 261e5a2311..162835cd4f 100644 --- a/py/builtin.h +++ b/py/builtin.h @@ -101,6 +101,7 @@ extern const mp_obj_module_t mp_module_ure; extern const mp_obj_module_t mp_module_uheapq; extern const mp_obj_module_t mp_module_uhashlib; extern const mp_obj_module_t mp_module_ubinascii; +extern const mp_obj_module_t mp_module_urandom; extern const mp_obj_module_t mp_module_ussl; extern const mp_obj_module_t mp_module_machine; extern const mp_obj_module_t mp_module_lwip; diff --git a/py/mpconfig.h b/py/mpconfig.h index 5e717ad0f6..72af343ddf 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -783,6 +783,10 @@ typedef double mp_float_t; #define MICROPY_PY_UBINASCII (0) #endif +#ifndef MICROPY_PY_URANDOM +#define MICROPY_PY_URANDOM (0) +#endif + #ifndef MICROPY_PY_MACHINE #define MICROPY_PY_MACHINE (0) #endif diff --git a/py/objmodule.c b/py/objmodule.c index 32ded89196..1034d00f60 100644 --- a/py/objmodule.c +++ b/py/objmodule.c @@ -184,6 +184,9 @@ STATIC const mp_rom_map_elem_t mp_builtin_module_table[] = { #if MICROPY_PY_UBINASCII { MP_ROM_QSTR(MP_QSTR_ubinascii), MP_ROM_PTR(&mp_module_ubinascii) }, #endif +#if MICROPY_PY_URANDOM + { MP_ROM_QSTR(MP_QSTR_urandom), MP_ROM_PTR(&mp_module_urandom) }, +#endif #if MICROPY_PY_USSL { MP_ROM_QSTR(MP_QSTR_ussl), MP_ROM_PTR(&mp_module_ussl) }, #endif diff --git a/py/py.mk b/py/py.mk index 4574ec9a68..63770e2d24 100644 --- a/py/py.mk +++ b/py/py.mk @@ -168,6 +168,7 @@ PY_O_BASENAME = \ ../extmod/modubinascii.o \ ../extmod/machine_mem.o \ ../extmod/modussl.o \ + ../extmod/modurandom.o \ ../extmod/fsusermount.o \ ../extmod/moduos_dupterm.o \ diff --git a/py/qstrdefs.h b/py/qstrdefs.h index 50f2b3b385..7e21090063 100644 --- a/py/qstrdefs.h +++ b/py/qstrdefs.h @@ -671,3 +671,9 @@ Q(count) #if MICROPY_PY_OS_DUPTERM Q(dupterm) #endif + +#if MICROPY_PY_URANDOM +Q(urandom) +Q(getrandbits) +Q(seed) +#endif diff --git a/stmhal/mpconfigport.h b/stmhal/mpconfigport.h index c83d02b741..47ad0a4156 100644 --- a/stmhal/mpconfigport.h +++ b/stmhal/mpconfigport.h @@ -79,6 +79,7 @@ #define MICROPY_PY_IO (1) #define MICROPY_PY_IO_FILEIO (1) #define MICROPY_PY_UBINASCII (1) +#define MICROPY_PY_URANDOM (1) #define MICROPY_PY_UCTYPES (1) #define MICROPY_PY_UZLIB (1) #define MICROPY_PY_UJSON (1) diff --git a/unix/mpconfigport.h b/unix/mpconfigport.h index 0eaa84d15c..2f992fdf03 100644 --- a/unix/mpconfigport.h +++ b/unix/mpconfigport.h @@ -104,6 +104,7 @@ #define MICROPY_PY_UHEAPQ (1) #define MICROPY_PY_UHASHLIB (1) #define MICROPY_PY_UBINASCII (1) +#define MICROPY_PY_URANDOM (1) #ifndef MICROPY_PY_USELECT #define MICROPY_PY_USELECT (1) #endif