Repository navigation
bip39: add optional compact word-list storage - #111
benma-agent wants to merge 2 commits into
Conversation
English word storage is expensive in small firmware: the default table keeps a pointer and a length for each of 2,048 words. Add the optional compact-wordlist feature to reduce this overhead. In a no_std ARM test program built with size optimization and LTO, code and constant data shrink from 27,818 to 12,574 bytes, saving 15,244 bytes. Store all English words in one 11,068-byte string with a 1,024-byte index. A small macro generates this string and the original word array from the same list, so the words only need to be maintained in one place. Each index entry covers 16 words: a 16-bit starting offset followed by 16 three-bit lengths, encoded as length minus three. Lookup adds at most 15 lengths and returns a slice of the string. The index is checked at compile time; lookup needs no allocation, unsafe code or UTF-8 conversion. Add Language::word_at and use it for mnemonic and prefix iteration. English word lookup uses binary search. Other languages keep their existing storage. Keep all existing APIs and leave the feature disabled by default. word_list and the old slice-based prefix API still need the original table. Using them alongside compact storage can increase binary size; the savings depend on the linker removing the unused original table. The default minimum Rust version stays at 1.41.1. The optional feature requires Rust 1.46 for compile-time index generation. Cover the feature in CI, including its minimum compiler, and test word contents, indices, prefix order, bounds and invalid lookups.
|
Hi @tnull @stevenroose. We use this dep in the BitBox02 firmware and would really benefit from this optimization. I'd appreciate it a lot if you could take a look and merge/release this. |
tnull
left a comment
There was a problem hiding this comment.
Excuse the delay here.
I see the benefit, but honestly don't like the idea of adding a feature that only works for one of the supported languages (english). If we support multiple languages, the features we add should support all of them, otherwise we'll end up with a matrix of what works for which combinations, which results in an error-prone/unexpected API for the users and an increasingly complex test matrix. Do you see any way to generalize support? FWIW, Claude is the opinion that it's rather straightforward.
|
@tnull thank you. I pushed a fixup commit (to be squashed before merge) to implement the generalization to all languages. Please check. |
English word storage is expensive in small firmware: the default table
keeps a pointer and a length for each of 2,048 words. Add the optional
compact-wordlist feature to reduce this overhead. In a no_std ARM test
program built with size optimization and LTO, code and constant data
shrink from 27,818 to 12,574 bytes, saving 15,244 bytes.
Store all English words in one 11,068-byte string with a 1,024-byte index.
A small macro generates this string and the original word array from the
same list, so the words only need to be maintained in one place.
Each index entry covers 16 words: a 16-bit starting offset followed by
16 three-bit lengths, encoded as length minus three. Lookup adds at most
15 lengths and returns a slice of the string. The index is checked at
compile time; lookup needs no allocation, unsafe code or UTF-8 conversion.
Add Language::word_at and use it for mnemonic and prefix iteration.
English word lookup uses binary search. Other languages keep their
existing storage.
Keep all existing APIs and leave the feature disabled by default.
word_list and the old slice-based prefix API still need the original
table. Using them alongside compact storage can increase binary size;
the savings depend on the linker removing the unused original table.
The default minimum Rust version stays at 1.41.1. The optional feature
requires Rust 1.46 for compile-time index generation. Cover the feature
in CI, including its minimum compiler, and test word contents, indices,
prefix order, bounds and invalid lookups.