feat(crypto): expose BIP39 wordlist

pull/2314/head
matejcik 2 years ago committed by matejcik
parent e98464d2c8
commit 61adb0a62b

@ -25,7 +25,6 @@
#include <string.h> #include <string.h>
#include "bip39.h" #include "bip39.h"
#include "bip39_english.h"
#include "hmac.h" #include "hmac.h"
#include "memzero.h" #include "memzero.h"
#include "options.h" #include "options.h"
@ -87,8 +86,8 @@ const char *mnemonic_from_data(const uint8_t *data, int len) {
idx <<= 1; idx <<= 1;
idx += (bits[(i * 11 + j) / 8] & (1 << (7 - ((i * 11 + j) % 8)))) > 0; idx += (bits[(i * 11 + j) / 8] & (1 << (7 - ((i * 11 + j) % 8)))) > 0;
} }
strcpy(p, wordlist[idx]); strcpy(p, BIP39_WORDLIST_ENGLISH[idx]);
p += strlen(wordlist[idx]); p += strlen(BIP39_WORDLIST_ENGLISH[idx]);
*p = (i < mlen - 1) ? ' ' : 0; *p = (i < mlen - 1) ? ' ' : 0;
p++; p++;
} }
@ -144,10 +143,11 @@ int mnemonic_to_bits(const char *mnemonic, uint8_t *bits) {
} }
k = 0; k = 0;
for (;;) { for (;;) {
if (!wordlist[k]) { // word not found if (!BIP39_WORDLIST_ENGLISH[k]) { // word not found
return 0; return 0;
} }
if (strcmp(current_word, wordlist[k]) == 0) { // word found on index k if (strcmp(current_word, BIP39_WORDLIST_ENGLISH[k]) ==
0) { // word found on index k
for (ki = 0; ki < 11; ki++) { for (ki = 0; ki < 11; ki++) {
if (k & (1 << (10 - ki))) { if (k & (1 << (10 - ki))) {
result[bi / 8] |= 1 << (7 - (bi % 8)); result[bi / 8] |= 1 << (7 - (bi % 8));
@ -245,7 +245,7 @@ int mnemonic_find_word(const char *word) {
int lo = 0, hi = BIP39_WORDS - 1; int lo = 0, hi = BIP39_WORDS - 1;
while (lo <= hi) { while (lo <= hi) {
int mid = lo + (hi - lo) / 2; int mid = lo + (hi - lo) / 2;
int cmp = strcmp(word, wordlist[mid]); int cmp = strcmp(word, BIP39_WORDLIST_ENGLISH[mid]);
if (cmp == 0) { if (cmp == 0) {
return mid; return mid;
} }
@ -261,9 +261,9 @@ 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 (const char *const *w = wordlist; *w != 0; w++) { for (int i = 0; i < BIP39_WORDS; i++) {
if (strncmp(*w, prefix, len) == 0) { if (strncmp(BIP39_WORDLIST_ENGLISH[i], prefix, len) == 0) {
return *w; return BIP39_WORDLIST_ENGLISH[i];
} }
} }
return NULL; return NULL;
@ -271,7 +271,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_WORDS) {
return wordlist[index]; return BIP39_WORDLIST_ENGLISH[index];
} else { } else {
return NULL; return NULL;
} }
@ -282,8 +282,8 @@ 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 (const char *const *w = wordlist; *w != 0; w++) { for (int i = 0; i < BIP39_WORDS; i++) {
const char *word = *w; 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') {
res |= 1 << (word[len] - 'a'); res |= 1 << (word[len] - 'a');

@ -36,6 +36,8 @@
void bip39_cache_clear(void); void bip39_cache_clear(void);
#endif #endif
extern const char *const BIP39_WORDLIST_ENGLISH[BIP39_WORDS];
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);
void mnemonic_clear(void); void mnemonic_clear(void);

@ -21,7 +21,9 @@
* OTHER DEALINGS IN THE SOFTWARE. * OTHER DEALINGS IN THE SOFTWARE.
*/ */
static const char* const wordlist[] = { #include "bip39.h"
const char* const BIP39_WORDLIST_ENGLISH[BIP39_WORDS] = {
"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",
@ -363,5 +365,5 @@ static const char* const wordlist[] = {
"work", "world", "worry", "worth", "wrap", "wreck", "work", "world", "worry", "worth", "wrap", "wreck",
"wrestle", "wrist", "write", "wrong", "yard", "year", "wrestle", "wrist", "write", "wrong", "yard", "year",
"yellow", "you", "young", "youth", "zebra", "zero", "yellow", "you", "young", "youth", "zebra", "zero",
"zone", "zoo", 0, "zone", "zoo",
}; };
Loading…
Cancel
Save