|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2016 Paul Sokolovsky |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <assert.h> |
| 28 | +#include <string.h> |
| 29 | + |
| 30 | +//#include "py/nlr.h" |
| 31 | +#include "py/runtime.h" |
| 32 | + |
| 33 | +#if MICROPY_PY_URANDOM |
| 34 | + |
| 35 | +// Yasmarang random number generator |
| 36 | +// by Ilya Levin |
| 37 | +// http://www.literatecode.com/yasmarang |
| 38 | +// Public Domain |
| 39 | + |
| 40 | +STATIC uint32_t yasmarang_pad = 0xeda4baba, yasmarang_n = 69, yasmarang_d = 233; |
| 41 | +STATIC uint8_t yasmarang_dat = 0; |
| 42 | + |
| 43 | +uint32_t yasmarang(void) |
| 44 | +{ |
| 45 | + yasmarang_pad += yasmarang_dat + yasmarang_d * yasmarang_n; |
| 46 | + yasmarang_pad = (yasmarang_pad<<3) + (yasmarang_pad>>29); |
| 47 | + yasmarang_n = yasmarang_pad | 2; |
| 48 | + yasmarang_d ^= (yasmarang_pad<<31) + (yasmarang_pad>>1); |
| 49 | + yasmarang_dat ^= (char) yasmarang_pad ^ (yasmarang_d>>8) ^ 1; |
| 50 | + |
| 51 | + return (yasmarang_pad^(yasmarang_d<<5)^(yasmarang_pad>>18)^(yasmarang_dat<<1)); |
| 52 | +} /* yasmarang */ |
| 53 | + |
| 54 | +// End of Yasmarang |
| 55 | + |
| 56 | +STATIC mp_obj_t mod_urandom_getrandbits(mp_obj_t num_in) { |
| 57 | + int n = mp_obj_get_int(num_in); |
| 58 | + if (n > 32 || n == 0) { |
| 59 | + nlr_raise(mp_obj_new_exception(&mp_type_ValueError)); |
| 60 | + } |
| 61 | + uint32_t mask = ~0; |
| 62 | + // Beware of C undefined behavior when shifting by >= than bit size |
| 63 | + mask >>= (32 - n); |
| 64 | + return mp_obj_new_int_from_uint(yasmarang() & mask); |
| 65 | +} |
| 66 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_urandom_getrandbits_obj, mod_urandom_getrandbits); |
| 67 | + |
| 68 | +STATIC mp_obj_t mod_urandom_seed(mp_obj_t seed_in) { |
| 69 | + mp_uint_t seed = mp_obj_get_int_truncated(seed_in); |
| 70 | + yasmarang_pad = seed; |
| 71 | + yasmarang_n = 69; |
| 72 | + yasmarang_d = 233; |
| 73 | + yasmarang_dat = 0; |
| 74 | + return mp_const_none; |
| 75 | +} |
| 76 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_urandom_seed_obj, mod_urandom_seed); |
| 77 | + |
| 78 | +STATIC const mp_rom_map_elem_t mp_module_urandom_globals_table[] = { |
| 79 | + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_urandom) }, |
| 80 | + { MP_ROM_QSTR(MP_QSTR_getrandbits), MP_ROM_PTR(&mod_urandom_getrandbits_obj) }, |
| 81 | + { MP_ROM_QSTR(MP_QSTR_seed), MP_ROM_PTR(&mod_urandom_seed_obj) }, |
| 82 | +}; |
| 83 | + |
| 84 | +STATIC MP_DEFINE_CONST_DICT(mp_module_urandom_globals, mp_module_urandom_globals_table); |
| 85 | + |
| 86 | +const mp_obj_module_t mp_module_urandom = { |
| 87 | + .base = { &mp_type_module }, |
| 88 | + .name = MP_QSTR_urandom, |
| 89 | + .globals = (mp_obj_dict_t*)&mp_module_urandom_globals, |
| 90 | +}; |
| 91 | + |
| 92 | +#endif //MICROPY_PY_URANDOM |
0 commit comments