Skip to content

Commit a58a91e

Browse files
committed
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
1 parent e7bee6b commit a58a91e

File tree

8 files changed

+109
-0
lines changed

8 files changed

+109
-0
lines changed

extmod/modurandom.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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

py/builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ extern const mp_obj_module_t mp_module_ure;
101101
extern const mp_obj_module_t mp_module_uheapq;
102102
extern const mp_obj_module_t mp_module_uhashlib;
103103
extern const mp_obj_module_t mp_module_ubinascii;
104+
extern const mp_obj_module_t mp_module_urandom;
104105
extern const mp_obj_module_t mp_module_ussl;
105106
extern const mp_obj_module_t mp_module_machine;
106107
extern const mp_obj_module_t mp_module_lwip;

py/mpconfig.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,10 @@ typedef double mp_float_t;
783783
#define MICROPY_PY_UBINASCII (0)
784784
#endif
785785

786+
#ifndef MICROPY_PY_URANDOM
787+
#define MICROPY_PY_URANDOM (0)
788+
#endif
789+
786790
#ifndef MICROPY_PY_MACHINE
787791
#define MICROPY_PY_MACHINE (0)
788792
#endif

py/objmodule.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ STATIC const mp_rom_map_elem_t mp_builtin_module_table[] = {
184184
#if MICROPY_PY_UBINASCII
185185
{ MP_ROM_QSTR(MP_QSTR_ubinascii), MP_ROM_PTR(&mp_module_ubinascii) },
186186
#endif
187+
#if MICROPY_PY_URANDOM
188+
{ MP_ROM_QSTR(MP_QSTR_urandom), MP_ROM_PTR(&mp_module_urandom) },
189+
#endif
187190
#if MICROPY_PY_USSL
188191
{ MP_ROM_QSTR(MP_QSTR_ussl), MP_ROM_PTR(&mp_module_ussl) },
189192
#endif

py/py.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ PY_O_BASENAME = \
168168
../extmod/modubinascii.o \
169169
../extmod/machine_mem.o \
170170
../extmod/modussl.o \
171+
../extmod/modurandom.o \
171172
../extmod/fsusermount.o \
172173
../extmod/moduos_dupterm.o \
173174

py/qstrdefs.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,3 +671,9 @@ Q(count)
671671
#if MICROPY_PY_OS_DUPTERM
672672
Q(dupterm)
673673
#endif
674+
675+
#if MICROPY_PY_URANDOM
676+
Q(urandom)
677+
Q(getrandbits)
678+
Q(seed)
679+
#endif

stmhal/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
#define MICROPY_PY_IO (1)
8080
#define MICROPY_PY_IO_FILEIO (1)
8181
#define MICROPY_PY_UBINASCII (1)
82+
#define MICROPY_PY_URANDOM (1)
8283
#define MICROPY_PY_UCTYPES (1)
8384
#define MICROPY_PY_UZLIB (1)
8485
#define MICROPY_PY_UJSON (1)

unix/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
#define MICROPY_PY_UHEAPQ (1)
105105
#define MICROPY_PY_UHASHLIB (1)
106106
#define MICROPY_PY_UBINASCII (1)
107+
#define MICROPY_PY_URANDOM (1)
107108
#ifndef MICROPY_PY_USELECT
108109
#define MICROPY_PY_USELECT (1)
109110
#endif

0 commit comments

Comments
 (0)