You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
2.4 KiB
69 lines
2.4 KiB
![]()
8 years ago
|
/*
|
||
|
* Copyright (c) Pavol Rusnak, SatoshiLabs
|
||
|
*
|
||
|
* Licensed under TREZOR License
|
||
|
* see LICENSE file for details
|
||
|
*/
|
||
|
|
||
|
#include "py/objstr.h"
|
||
|
|
||
|
#include "rand.h"
|
||
|
|
||
![]()
7 years ago
|
/// def uniform(n: int) -> int:
|
||
![]()
7 years ago
|
/// '''
|
||
![]()
7 years ago
|
/// Compute uniform random number from interval 0 ... n - 1.
|
||
![]()
7 years ago
|
/// '''
|
||
![]()
7 years ago
|
STATIC mp_obj_t mod_trezorcrypto_random_uniform(mp_obj_t n) {
|
||
![]()
8 years ago
|
uint32_t nn = mp_obj_get_int_truncated(n);
|
||
![]()
8 years ago
|
if (nn == 0) {
|
||
![]()
8 years ago
|
mp_raise_ValueError("Maximum can't be zero");
|
||
![]()
8 years ago
|
}
|
||
![]()
8 years ago
|
return mp_obj_new_int_from_uint(random_uniform(nn));
|
||
![]()
8 years ago
|
}
|
||
![]()
7 years ago
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_random_uniform_obj, mod_trezorcrypto_random_uniform);
|
||
![]()
8 years ago
|
|
||
![]()
7 years ago
|
/// def bytes(len: int) -> bytes:
|
||
![]()
8 years ago
|
/// '''
|
||
![]()
7 years ago
|
/// Generate random bytes sequence of length len.
|
||
![]()
8 years ago
|
/// '''
|
||
![]()
7 years ago
|
STATIC mp_obj_t mod_trezorcrypto_random_bytes(mp_obj_t len) {
|
||
![]()
8 years ago
|
uint32_t l = mp_obj_get_int(len);
|
||
![]()
8 years ago
|
if (l > 8192) {
|
||
|
mp_raise_ValueError("Maximum requested size is 8192");
|
||
|
}
|
||
![]()
8 years ago
|
vstr_t vstr;
|
||
|
vstr_init_len(&vstr, l);
|
||
|
random_buffer((uint8_t *)vstr.buf, l);
|
||
|
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||
|
}
|
||
![]()
7 years ago
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_random_bytes_obj, mod_trezorcrypto_random_bytes);
|
||
![]()
8 years ago
|
|
||
![]()
7 years ago
|
/// def shuffle(data: list) -> None:
|
||
![]()
8 years ago
|
/// '''
|
||
![]()
7 years ago
|
/// Shuffles items of given list (in-place).
|
||
![]()
8 years ago
|
/// '''
|
||
![]()
7 years ago
|
STATIC mp_obj_t mod_trezorcrypto_random_shuffle(mp_obj_t data) {
|
||
![]()
8 years ago
|
size_t item_cnt;
|
||
![]()
8 years ago
|
mp_obj_t *items;
|
||
![]()
8 years ago
|
mp_obj_get_array(data, &item_cnt, &items);
|
||
![]()
8 years ago
|
if (item_cnt > 256) {
|
||
![]()
8 years ago
|
mp_raise_ValueError("Maximum list size is 256 items");
|
||
![]()
8 years ago
|
}
|
||
|
random_permute(items, sizeof(mp_obj_t *), item_cnt);
|
||
|
return mp_const_none;
|
||
|
}
|
||
![]()
7 years ago
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_random_shuffle_obj, mod_trezorcrypto_random_shuffle);
|
||
![]()
8 years ago
|
|
||
![]()
7 years ago
|
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) },
|
||
![]()
8 years ago
|
};
|
||
![]()
7 years ago
|
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_random_globals, mod_trezorcrypto_random_globals_table);
|
||
![]()
8 years ago
|
|
||
![]()
7 years ago
|
STATIC const mp_obj_module_t mod_trezorcrypto_random_module = {
|
||
|
.base = { &mp_type_module },
|
||
|
.globals = (mp_obj_dict_t*)&mod_trezorcrypto_random_globals,
|
||
![]()
8 years ago
|
};
|