1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-25 07:48:10 +00:00
trezor-firmware/micropython/extmod/modtrezorcrypto/modtrezorcrypto-random.h

87 lines
2.9 KiB
C
Raw Normal View History

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"
/// class Random:
/// '''
/// '''
2016-06-01 15:48:32 +00:00
typedef struct _mp_obj_Random_t {
mp_obj_base_t base;
} mp_obj_Random_t;
/// def __init__(self) -> None:
/// '''
/// '''
2017-06-14 16:47:38 +00:00
STATIC mp_obj_t mod_trezorcrypto_Random_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
2016-06-01 15:48:32 +00:00
mp_arg_check_num(n_args, n_kw, 0, 0, false);
mp_obj_Random_t *o = m_new_obj(mp_obj_Random_t);
o->base.type = type;
return MP_OBJ_FROM_PTR(o);
}
/// def uniform(self, n: int) -> int:
/// '''
/// Compute uniform random number from interval 0 ... n - 1
/// '''
2017-06-14 16:47:38 +00:00
STATIC mp_obj_t mod_trezorcrypto_Random_uniform(mp_obj_t self, mp_obj_t n) {
uint32_t nn = mp_obj_get_int_truncated(n);
2016-06-01 15:48:32 +00:00
if (nn == 0) {
mp_raise_ValueError("Maximum can't be zero");
2016-06-01 15:48:32 +00:00
}
return mp_obj_new_int_from_uint(random_uniform(nn));
2016-06-01 15:48:32 +00:00
}
2017-06-14 16:47:38 +00:00
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_Random_uniform_obj, mod_trezorcrypto_Random_uniform);
2016-06-01 15:48:32 +00:00
/// def bytes(self, len: int) -> bytes:
/// '''
/// Generate random bytes sequence of length len
/// '''
2017-06-14 16:47:38 +00:00
STATIC mp_obj_t mod_trezorcrypto_Random_bytes(mp_obj_t self, mp_obj_t len) {
2016-06-01 15:48:32 +00:00
uint32_t l = mp_obj_get_int(len);
if (l > 8192) {
mp_raise_ValueError("Maximum requested size is 8192");
}
2016-06-01 15:48:32 +00:00
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);
}
2017-06-14 16:47:38 +00:00
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_Random_bytes_obj, mod_trezorcrypto_Random_bytes);
2016-06-01 15:48:32 +00:00
/// def shuffle(self, data: list) -> None:
/// '''
/// Shuffles items of given list (in-place)
/// '''
2017-06-14 16:47:38 +00:00
STATIC mp_obj_t mod_trezorcrypto_Random_shuffle(mp_obj_t self, mp_obj_t data) {
size_t item_cnt;
2016-06-01 15:48:32 +00:00
mp_obj_t *items;
mp_obj_get_array(data, &item_cnt, &items);
2016-06-01 15:48:32 +00:00
if (item_cnt > 256) {
mp_raise_ValueError("Maximum list size is 256 items");
2016-06-01 15:48:32 +00:00
}
random_permute(items, sizeof(mp_obj_t *), item_cnt);
return mp_const_none;
}
2017-06-14 16:47:38 +00:00
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_Random_shuffle_obj, mod_trezorcrypto_Random_shuffle);
2016-06-01 15:48:32 +00:00
2017-06-14 16:47:38 +00:00
STATIC const mp_rom_map_elem_t mod_trezorcrypto_Random_locals_dict_table[] = {
{ 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-14 16:47:38 +00:00
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_Random_locals_dict, mod_trezorcrypto_Random_locals_dict_table);
2016-06-01 15:48:32 +00:00
2017-06-14 16:47:38 +00:00
STATIC const mp_obj_type_t mod_trezorcrypto_Random_type = {
2016-06-01 15:48:32 +00:00
{ &mp_type_type },
.name = MP_QSTR_Random,
2017-06-14 16:47:38 +00:00
.make_new = mod_trezorcrypto_Random_make_new,
.locals_dict = (void*)&mod_trezorcrypto_Random_locals_dict,
2016-06-01 15:48:32 +00:00
};