1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-26 00:08:10 +00:00

chore: rename BIP39_WORDS to BIP39_WORD_COUNT

This commit is contained in:
matejcik 2022-06-01 13:19:12 +02:00 committed by matejcik
parent dfa4b1d9a1
commit 38a36a131f
6 changed files with 16 additions and 16 deletions

View File

@ -2,12 +2,12 @@ use core::cmp::Ordering;
use cstr_core::CStr; use cstr_core::CStr;
// TODO: expose from trezor-crypto via build.rs // TODO: expose from trezor-crypto via build.rs
const BIP39_WORDS: usize = 2048; const BIP39_WORD_COUNT: usize = 2048;
extern "C" { extern "C" {
// trezor-crypto/bip39.h // trezor-crypto/bip39.h
fn mnemonic_word_completion_mask(prefix: *const cty::c_char, len: cty::c_int) -> u32; fn mnemonic_word_completion_mask(prefix: *const cty::c_char, len: cty::c_int) -> u32;
pub static BIP39_WORDLIST_ENGLISH: [*const cty::c_char; BIP39_WORDS]; pub static BIP39_WORDLIST_ENGLISH: [*const cty::c_char; BIP39_WORD_COUNT];
} }
unsafe fn from_utf8_unchecked<'a>(word: *const cty::c_char) -> &'a str { unsafe fn from_utf8_unchecked<'a>(word: *const cty::c_char) -> &'a str {
@ -136,9 +136,9 @@ mod tests {
#[test] #[test]
fn test_filter_prefix_empty() { fn test_filter_prefix_empty() {
let words = Wordlist::all().filter_prefix(""); let words = Wordlist::all().filter_prefix("");
assert_eq!(words.len(), BIP39_WORDS); assert_eq!(words.len(), BIP39_WORD_COUNT);
let iter = words.iter(); let iter = words.iter();
assert_eq!(iter.size_hint(), (BIP39_WORDS, Some(BIP39_WORDS))); assert_eq!(iter.size_hint(), (BIP39_WORD_COUNT, Some(BIP39_WORD_COUNT)));
} }
#[test] #[test]
@ -171,9 +171,9 @@ mod tests {
fn test_wordlist_get() { fn test_wordlist_get() {
let words = Wordlist::all(); let words = Wordlist::all();
assert_eq!(words.get(0), Some("abandon")); assert_eq!(words.get(0), Some("abandon"));
assert_eq!(words.get(BIP39_WORDS - 1), Some("zoo")); assert_eq!(words.get(BIP39_WORD_COUNT - 1), Some("zoo"));
assert_eq!(words.get(BIP39_WORDS), None); assert_eq!(words.get(BIP39_WORD_COUNT), None);
assert_eq!(words.get(BIP39_WORDS + 1), None); assert_eq!(words.get(BIP39_WORD_COUNT + 1), None);
let filtered = words.filter_prefix("str"); let filtered = words.filter_prefix("str");
assert_eq!(filtered.get(0), Some("strategy")); assert_eq!(filtered.get(0), Some("strategy"));

View File

@ -242,7 +242,7 @@ void mnemonic_to_seed(const char *mnemonic, const char *passphrase,
// binary search for finding the word in the wordlist // binary search for finding the word in the wordlist
int mnemonic_find_word(const char *word) { int mnemonic_find_word(const char *word) {
int lo = 0, hi = BIP39_WORDS - 1; int lo = 0, hi = BIP39_WORD_COUNT - 1;
while (lo <= hi) { while (lo <= hi) {
int mid = lo + (hi - lo) / 2; int mid = lo + (hi - lo) / 2;
int cmp = strcmp(word, BIP39_WORDLIST_ENGLISH[mid]); int cmp = strcmp(word, BIP39_WORDLIST_ENGLISH[mid]);
@ -261,7 +261,7 @@ int mnemonic_find_word(const char *word) {
const char *mnemonic_complete_word(const char *prefix, int len) { const char *mnemonic_complete_word(const char *prefix, int len) {
// we need to perform linear search, // we need to perform linear search,
// because we want to return the first match // because we want to return the first match
for (int i = 0; i < BIP39_WORDS; i++) { for (int i = 0; i < BIP39_WORD_COUNT; i++) {
if (strncmp(BIP39_WORDLIST_ENGLISH[i], prefix, len) == 0) { if (strncmp(BIP39_WORDLIST_ENGLISH[i], prefix, len) == 0) {
return BIP39_WORDLIST_ENGLISH[i]; return BIP39_WORDLIST_ENGLISH[i];
} }
@ -270,7 +270,7 @@ const char *mnemonic_complete_word(const char *prefix, int len) {
} }
const char *mnemonic_get_word(int index) { const char *mnemonic_get_word(int index) {
if (index >= 0 && index < BIP39_WORDS) { if (index >= 0 && index < BIP39_WORD_COUNT) {
return BIP39_WORDLIST_ENGLISH[index]; return BIP39_WORDLIST_ENGLISH[index];
} else { } else {
return NULL; return NULL;
@ -282,7 +282,7 @@ uint32_t mnemonic_word_completion_mask(const char *prefix, int len) {
return 0x3ffffff; // all letters (bits 1-26 set) return 0x3ffffff; // all letters (bits 1-26 set)
} }
uint32_t res = 0; uint32_t res = 0;
for (int i = 0; i < BIP39_WORDS; i++) { for (int i = 0; i < BIP39_WORD_COUNT; i++) {
const char *word = BIP39_WORDLIST_ENGLISH[i]; const char *word = BIP39_WORDLIST_ENGLISH[i];
if (strncmp(word, prefix, len) == 0 && word[len] >= 'a' && if (strncmp(word, prefix, len) == 0 && word[len] >= 'a' &&
word[len] <= 'z') { word[len] <= 'z') {

View File

@ -29,14 +29,14 @@
#include "options.h" #include "options.h"
#define BIP39_WORDS 2048 #define BIP39_WORD_COUNT 2048
#define BIP39_PBKDF2_ROUNDS 2048 #define BIP39_PBKDF2_ROUNDS 2048
#if USE_BIP39_CACHE #if USE_BIP39_CACHE
void bip39_cache_clear(void); void bip39_cache_clear(void);
#endif #endif
extern const char *const BIP39_WORDLIST_ENGLISH[BIP39_WORDS]; extern const char *const BIP39_WORDLIST_ENGLISH[BIP39_WORD_COUNT];
const char *mnemonic_generate(int strength); // strength in bits const char *mnemonic_generate(int strength); // strength in bits
const char *mnemonic_from_data(const uint8_t *data, int len); const char *mnemonic_from_data(const uint8_t *data, int len);

View File

@ -23,7 +23,7 @@
#include "bip39.h" #include "bip39.h"
const char* const BIP39_WORDLIST_ENGLISH[BIP39_WORDS] = { const char* const BIP39_WORDLIST_ENGLISH[BIP39_WORD_COUNT] = {
"abandon", "ability", "able", "about", "above", "absent", "abandon", "ability", "able", "about", "above", "absent",
"absorb", "abstract", "absurd", "abuse", "access", "accident", "absorb", "abstract", "absurd", "abuse", "access", "accident",
"account", "accuse", "achieve", "acid", "acoustic", "acquire", "account", "accuse", "achieve", "acid", "acoustic", "acquire",

View File

@ -5603,7 +5603,7 @@ END_TEST
START_TEST(test_mnemonic_find_word) { START_TEST(test_mnemonic_find_word) {
ck_assert_int_eq(-1, mnemonic_find_word("aaaa")); ck_assert_int_eq(-1, mnemonic_find_word("aaaa"));
ck_assert_int_eq(-1, mnemonic_find_word("zzzz")); ck_assert_int_eq(-1, mnemonic_find_word("zzzz"));
for (int i = 0; i < BIP39_WORDS; i++) { for (int i = 0; i < BIP39_WORD_COUNT; i++) {
const char *word = mnemonic_get_word(i); const char *word = mnemonic_get_word(i);
int index = mnemonic_find_word(word); int index = mnemonic_find_word(word);
ck_assert_int_eq(i, index); ck_assert_int_eq(i, index);

View File

@ -451,7 +451,7 @@ void next_word(void) {
oledDrawStringCenter(OLED_WIDTH / 2, 8, _("Please enter"), FONT_STANDARD); oledDrawStringCenter(OLED_WIDTH / 2, 8, _("Please enter"), FONT_STANDARD);
word_pos = word_order[word_index]; word_pos = word_order[word_index];
if (word_pos == 0) { if (word_pos == 0) {
strlcpy(fake_word, mnemonic_get_word(random_uniform(BIP39_WORDS)), strlcpy(fake_word, mnemonic_get_word(random_uniform(BIP39_WORD_COUNT)),
sizeof(fake_word)); sizeof(fake_word));
oledDrawStringCenter(OLED_WIDTH / 2, 24, fake_word, oledDrawStringCenter(OLED_WIDTH / 2, 24, fake_word,
FONT_FIXED | FONT_DOUBLE); FONT_FIXED | FONT_DOUBLE);