ui/trinary_input_string: merge two funcs to optimize speed
What changed, and why it matters
This is a harmless code cleanup in the BitBox02 hardware wallet's on-screen keyboard. The developer merged two functions that did similar work into one, so the device no longer loops through the BIP39 word list twice when typing a recovery word. The visible behavior is unchanged; only performance improved.
No security action needed. Treat as a normal performance refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch folds _set_can_confirm() into _set_alphabet() in src/ui/components/trinary_input_string.c. Previously _set_alphabet() computed the allowed next-character alphabet by scanning the BIP39 wordlist, and _set_can_confirm() then scanned the same wordlist again to decide whether the current input matches a complete word. The new code sets data->can_confirm = true initially, resets it to false when a wordlist is present, and sets it back to true inside the existing wordlist loop when STREQ(word, data->string). The removed function and its call sites are deleted. Logic is preserved; only redundant iterations are eliminated.
Changed components
src/ui/components/trinary_input_string.cInspect captured patch +8 / −26
diff --git a/src/ui/components/trinary_input_string.c b/src/ui/components/trinary_input_string.c
index d221610..2c3a9ba 100644
--- a/src/ui/components/trinary_input_string.c
+++ b/src/ui/components/trinary_input_string.c
@@ -286,7 +286,10 @@ static void _set_alphabet(component_t* trinary_input_string)
{
data_t* data = (data_t*)trinary_input_string->data;
component_t* trinary_char = data->trinary_char_component;
+ data->can_confirm = true;
if (data->wordlist != NULL) {
+ data->can_confirm = false;
+
// Restrict input charset based on the available words with.
// E.g. if the user entered "act", and the wordlist contains "actor", "actress", "action",
// the charset to select the next letter wil be "eio".
@@ -297,6 +300,11 @@ static void _set_alphabet(component_t* trinary_input_string)
if (!keystore_get_bip39_word_stack(data->wordlist[word_idx], word, sizeof(word))) {
Abort("keystore_get_bip39_word_stack");
}
+
+ if (STREQ(word, data->string)) {
+ data->can_confirm = true;
+ }
+
bool is_prefix = strncmp(data->string, word, data->string_index) == 0;
if (is_prefix) {
if (strlen(word) > data->string_index) {
@@ -333,28 +341,6 @@ static void _set_alphabet(component_t* trinary_input_string)
}
}
-static void _set_can_confirm(component_t* trinary_input_string)
-{
- data_t* data = (data_t*)trinary_input_string->data;
- if (data->wordlist == NULL) {
- data->can_confirm = true;
- return;
- }
- data->can_confirm = false;
- // Can only confirm if the entered word matches a word in the wordlist.
- for (size_t i = 0; i < data->wordlist_size; i++) {
- char word[10];
- if (!keystore_get_bip39_word_stack(data->wordlist[i], word, sizeof(word))) {
- Abort("keystore_get_bip39_word_stack");
- }
-
- if (STREQ(word, data->string)) {
- data->can_confirm = true;
- return;
- }
- }
-}
-
static void _on_event(const event_t* event, component_t* component)
{
data_t* data = (data_t*)component->data;
@@ -401,7 +387,6 @@ static void _on_event(const event_t* event, component_t* component)
}
}
_set_alphabet(component);
- _set_can_confirm(component);
break;
default:
break;
@@ -433,7 +418,6 @@ static void _letter_chosen(component_t* trinary_char, char chosen)
data->show_last_character = true;
_maybe_autocomplete(trinary_input_string);
_set_alphabet(trinary_input_string);
- _set_can_confirm(trinary_input_string);
UG_S16 string_width = _constant_string_width(trinary_input_string);
if (data->target_x + string_width > SCROLL_RIGHT_LIMIT) {
data->target_x = -string_width + SCROLL_LEFT_PAD;
@@ -522,7 +506,6 @@ component_t* trinary_input_string_create(
data->trinary_char_component = trinary_input_char_create(_letter_chosen, component);
ui_util_add_sub_component(component, data->trinary_char_component);
_set_alphabet(component);
- _set_can_confirm(component);
return component;
}
@@ -542,7 +525,6 @@ void trinary_input_string_set_input(component_t* trinary_input_string, const cha
if (STREQ(bip39_word, word)) {
data->string_index = snprintf(data->string, sizeof(data->string), "%s", word);
_set_alphabet(trinary_input_string);
- _set_can_confirm(trinary_input_string);
return;
}
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.