2016-06-01 15:48:32 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) Pavol Rusnak, SatoshiLabs
|
|
|
|
*
|
|
|
|
* Licensed under TREZOR License
|
|
|
|
* see LICENSE file for details
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "py/objstr.h"
|
|
|
|
|
|
|
|
#include "rand.h"
|
|
|
|
|
2017-06-20 15:45:27 +00:00
|
|
|
/// def uniform(n: int) -> int:
|
2017-06-14 15:40:50 +00:00
|
|
|
/// '''
|
2017-06-20 15:45:27 +00:00
|
|
|
/// Compute uniform random number from interval 0 ... n - 1.
|
2017-06-14 15:40:50 +00:00
|
|
|
/// '''
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC mp_obj_t mod_trezorcrypto_random_uniform(mp_obj_t n) {
|
2016-09-25 13:26:05 +00:00
|
|
|
uint32_t nn = mp_obj_get_int_truncated(n);
|
2016-06-01 15:48:32 +00:00
|
|
|
if (nn == 0) {
|
2016-10-07 10:09:05 +00:00
|
|
|
mp_raise_ValueError("Maximum can't be zero");
|
2016-06-01 15:48:32 +00:00
|
|
|
}
|
2016-09-21 16:06:34 +00:00
|
|
|
return mp_obj_new_int_from_uint(random_uniform(nn));
|
2016-06-01 15:48:32 +00:00
|
|
|
}
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_random_uniform_obj, mod_trezorcrypto_random_uniform);
|
2016-06-01 15:48:32 +00:00
|
|
|
|
2017-06-20 15:45:27 +00:00
|
|
|
/// def bytes(len: int) -> bytes:
|
2016-06-06 08:18:55 +00:00
|
|
|
/// '''
|
2017-06-20 15:45:27 +00:00
|
|
|
/// Generate random bytes sequence of length len.
|
2016-06-06 08:18:55 +00:00
|
|
|
/// '''
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC mp_obj_t mod_trezorcrypto_random_bytes(mp_obj_t len) {
|
2016-06-01 15:48:32 +00:00
|
|
|
uint32_t l = mp_obj_get_int(len);
|
2018-01-03 20:53:58 +00:00
|
|
|
if (l > 1024) {
|
|
|
|
mp_raise_ValueError("Maximum requested size is 1024");
|
2016-10-07 11:57:21 +00:00
|
|
|
}
|
2018-01-05 12:13:20 +00:00
|
|
|
vstr_t vstr;
|
|
|
|
vstr_init_len(&vstr, l);
|
|
|
|
random_buffer((uint8_t *)vstr.buf, l);
|
2018-01-07 12:56:18 +00:00
|
|
|
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
2016-06-01 15:48:32 +00:00
|
|
|
}
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_random_bytes_obj, mod_trezorcrypto_random_bytes);
|
2016-06-01 15:48:32 +00:00
|
|
|
|
2017-06-20 15:45:27 +00:00
|
|
|
/// def shuffle(data: list) -> None:
|
2016-06-06 08:18:55 +00:00
|
|
|
/// '''
|
2017-06-20 15:45:27 +00:00
|
|
|
/// Shuffles items of given list (in-place).
|
2016-06-06 08:18:55 +00:00
|
|
|
/// '''
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC mp_obj_t mod_trezorcrypto_random_shuffle(mp_obj_t data) {
|
2017-12-15 15:56:08 +00:00
|
|
|
size_t count;
|
2016-06-01 15:48:32 +00:00
|
|
|
mp_obj_t *items;
|
2017-12-15 15:56:08 +00:00
|
|
|
mp_obj_get_array(data, &count, &items);
|
|
|
|
if (count > 256) {
|
2016-10-07 10:09:05 +00:00
|
|
|
mp_raise_ValueError("Maximum list size is 256 items");
|
2016-06-01 15:48:32 +00:00
|
|
|
}
|
2017-12-15 15:56:08 +00:00
|
|
|
if (count <= 1) {
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
// Fisher-Yates shuffle
|
|
|
|
mp_obj_t t;
|
|
|
|
for (size_t i = count - 1; i >= 1; i--) {
|
|
|
|
size_t j = random_uniform(i + 1);
|
|
|
|
t = items[i];
|
|
|
|
items[i] = items[j];
|
|
|
|
items[j] = t;
|
|
|
|
}
|
2016-06-01 15:48:32 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_random_shuffle_obj, mod_trezorcrypto_random_shuffle);
|
2016-06-01 15:48:32 +00:00
|
|
|
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC const mp_rom_map_elem_t mod_trezorcrypto_random_globals_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_random) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_uniform), MP_ROM_PTR(&mod_trezorcrypto_random_uniform_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_bytes), MP_ROM_PTR(&mod_trezorcrypto_random_bytes_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_shuffle), MP_ROM_PTR(&mod_trezorcrypto_random_shuffle_obj) },
|
2016-06-01 15:48:32 +00:00
|
|
|
};
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_random_globals, mod_trezorcrypto_random_globals_table);
|
2016-06-01 15:48:32 +00:00
|
|
|
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC const mp_obj_module_t mod_trezorcrypto_random_module = {
|
|
|
|
.base = { &mp_type_module },
|
|
|
|
.globals = (mp_obj_dict_t*)&mod_trezorcrypto_random_globals,
|
2016-06-01 15:48:32 +00:00
|
|
|
};
|