Skip to content

bip39: add optional compact word-list storage - #111

Open
benma-agent wants to merge 2 commits into
rust-bitcoin:masterfrom
benma-agent:benma-agent/compact-wordlist
Open

benma-agent wants to merge 2 commits into
rust-bitcoin:masterfrom
benma-agent:benma-agent/compact-wordlist

Conversation

@benma-agent

Copy link
Copy Markdown

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.

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.
@benma-agent
benma-agent marked this pull request as ready for review October 9, 2026 10:17
@benma

benma commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

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 tnull left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@benma

benma commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

@tnull thank you. I pushed a fixup commit (to be squashed before merge) to implement the generalization to all languages. Please check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants