chore(core): Improve naming of SLIP39's T9 mask lookup.

poc/andrew-t1-keyboard
Andrew Kozlik 3 years ago committed by Andrew Kozlik
parent 7fc1851279
commit aeb021b159

@ -24,7 +24,7 @@
/// package: trezorcrypto.slip39
/// def compute_mask(prefix: int) -> int:
/// def word_completion_mask(prefix: int) -> int:
/// """
/// Calculates which buttons still can be pressed after some already were.
/// Returns a 9-bit bitmask, where each bit specifies which buttons
@ -34,17 +34,18 @@
/// Example: 110000110 - second, third, eighth and ninth button still can be
/// pressed.
/// """
STATIC mp_obj_t mod_trezorcrypto_slip39_compute_mask(mp_obj_t _prefix) {
STATIC mp_obj_t mod_trezorcrypto_slip39_word_completion_mask(mp_obj_t _prefix) {
uint16_t prefix = mp_obj_get_int(_prefix);
if (prefix < 1 || prefix > 9999) {
mp_raise_ValueError(
"Invalid button prefix (range between 1 and 9999 is allowed)");
}
return mp_obj_new_int_from_uint(compute_mask(prefix));
return mp_obj_new_int_from_uint(slip39_word_completion_mask(prefix));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_slip39_compute_mask_obj,
mod_trezorcrypto_slip39_compute_mask);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(
mod_trezorcrypto_slip39_word_completion_mask_obj,
mod_trezorcrypto_slip39_word_completion_mask);
/// def button_sequence_to_word(prefix: int) -> str:
/// """
@ -90,12 +91,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_slip39_word_index_obj,
STATIC mp_obj_t mod_trezorcrypto_slip39_get_word(mp_obj_t _index) {
uint16_t index = mp_obj_get_int(_index);
if (index > 1023) {
const char *word = get_word(index);
if (word == NULL) {
mp_raise_ValueError(
"Invalid wordlist index (range between 0 and 1024 is allowed)");
}
const char *word = get_word(index);
return mp_obj_new_str_copy(&mp_type_str, (const uint8_t *)word, strlen(word));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_slip39_get_word_obj,
@ -103,8 +104,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_slip39_get_word_obj,
STATIC const mp_rom_map_elem_t mod_trezorcrypto_slip39_globals_table[] = {
{MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_slip39)},
{MP_ROM_QSTR(MP_QSTR_compute_mask),
MP_ROM_PTR(&mod_trezorcrypto_slip39_compute_mask_obj)},
{MP_ROM_QSTR(MP_QSTR_word_completion_mask),
MP_ROM_PTR(&mod_trezorcrypto_slip39_word_completion_mask_obj)},
{MP_ROM_QSTR(MP_QSTR_button_sequence_to_word),
MP_ROM_PTR(&mod_trezorcrypto_slip39_button_sequence_to_word_obj)},
{MP_ROM_QSTR(MP_QSTR_word_index),

@ -2,7 +2,7 @@ from typing import *
# extmod/modtrezorcrypto/modtrezorcrypto-slip39.h
def compute_mask(prefix: int) -> int:
def word_completion_mask(prefix: int) -> int:
"""
Calculates which buttons still can be pressed after some already were.
Returns a 9-bit bitmask, where each bit specifies which buttons

@ -171,7 +171,7 @@ class Slip39Keyboard(ui.Layout):
# find the completions
word = ""
self.mask = slip39.compute_mask(self.button_sequence)
self.mask = slip39.word_completion_mask(self.button_sequence)
if self.is_input_final():
word = slip39.button_sequence_to_word(self.button_sequence)

@ -114,10 +114,10 @@ KEYBOARD_FULL_MASK = const(0x1FF)
"""All buttons are allowed. 9-bit bitmap all set to 1."""
def compute_mask(prefix: str) -> int:
def word_completion_mask(prefix: str) -> int:
if not prefix:
return KEYBOARD_FULL_MASK
return slip39.compute_mask(int(prefix))
return slip39.word_completion_mask(int(prefix))
def button_sequence_to_word(prefix: str) -> str:

Loading…
Cancel
Save