Bitcoin Forum
May 01, 2024, 09:14:20 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: BIP39 foreign language wordlists not sorted  (Read 1484 times)
HeadsOrTails (OP)
Full Member
***
Offline Offline

Activity: 233
Merit: 100



View Profile
July 04, 2015, 05:09:20 PM
 #1

http://stackoverflow.com/a/31156743/3936486

Note the Japanese word list: https://github.com/trezor/python-mnemonic/raw/master/mnemonic/wordlist/japanese.txt

It's been raised here that a binary search (instead of using a built in list index in Python) relies on a sorted list of words. That's not an issue for English, but Japanese would fail for the test vectors.

BIP39 specifies wordlists should be sorted.

Any insights?
1714598060
Hero Member
*
Offline Offline

Posts: 1714598060

View Profile Personal Message (Offline)

Ignore
1714598060
Reply with quote  #2

1714598060
Report to moderator
1714598060
Hero Member
*
Offline Offline

Posts: 1714598060

View Profile Personal Message (Offline)

Ignore
1714598060
Reply with quote  #2

1714598060
Report to moderator
BitcoinCleanup.com: Learn why Bitcoin isn't bad for the environment
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1714598060
Hero Member
*
Offline Offline

Posts: 1714598060

View Profile Personal Message (Offline)

Ignore
1714598060
Reply with quote  #2

1714598060
Report to moderator
1714598060
Hero Member
*
Offline Offline

Posts: 1714598060

View Profile Personal Message (Offline)

Ignore
1714598060
Reply with quote  #2

1714598060
Report to moderator
johoe
Full Member
***
Offline Offline

Activity: 217
Merit: 238


View Profile
July 04, 2015, 06:18:34 PM
 #2

Could it be a problem of localization?  Every language has its own sorting rules and often this is implemented in the library.  Maybe if you have japanese localization the list is sorted correctly.

Looking into the official bip39 wordlists it seems other languages have the same problem, e.g., french:

Code:
belette
bélier
belote
bénéfice
berceau

I'm not sure what the right solution is.  Sorting the word list with LANG=C would change the indices of the words and break compatibility.  Requiring that localization setting must match the language of the word list make automatic tests difficult.  Especially if you don't have support for the language installed.

Donations to 1CF62UFWXiKqFUmgQMUby9DpEW5LXjypU3
tspacepilot
Legendary
*
Offline Offline

Activity: 1456
Merit: 1076


I may write code in exchange for bitcoins.


View Profile
July 05, 2015, 05:27:25 PM
 #3

Looking at that japanese.txt wordlist file, they seem sorted to me.  What order did you want them to be sorted in?
johoe
Full Member
***
Offline Offline

Activity: 217
Merit: 238


View Profile
July 05, 2015, 08:44:02 PM
 #4

Code:
>LANG=C sort japanese.txt | diff japanese.txt -
 あたる
+あっしゅく
 あつい
 あつかう
-あっしゅく
 あつまり
 あつめる
 あてな
 あてはまる
 あひる
+あふれる
 あぶら
 あぶる
-あふれる
 あまい
...

So in the word list っ and つ are sorted as the same letter.  This may be standard for japanese localization, but if you don't have this localization installed you get a different order.  The bad thing is that the binary-search method in the bip39 mnemonics tool doesn't work if the list is not sorted.  Thus, for example, the unit tests of python-mnemonics fail.

Donations to 1CF62UFWXiKqFUmgQMUby9DpEW5LXjypU3
tspacepilot
Legendary
*
Offline Offline

Activity: 1456
Merit: 1076


I may write code in exchange for bitcoins.


View Profile
July 06, 2015, 03:16:50 PM
 #5

That's very interesting, thanks for showing me that, johoe.   I looked up those characters and the smaller one is used almost as a sort of diacritical mark to double the following consonant:

https://en.wikipedia.org/wiki/Sokuon

So it looks to me like you wouldn't expect to sort them in the same order even in japanese localizations.  I wonder if there really is some sort of mistake that was done in the sorting of this text file.  (also, I just learned something cool about japanese orthography, so thanks at least for that you guys!)
HeadsOrTails (OP)
Full Member
***
Offline Offline

Activity: 233
Merit: 100



View Profile
July 07, 2015, 06:13:44 AM
 #6

So it looks to me like you wouldn't expect to sort them in the same order even in japanese localizations.  I wonder if there really is some sort of mistake that was done in the sorting of this text file.

I don't think there has been sorting. Or there was initially, and over time it's crept towards unsorted. Because here's the thing: the Japanese list only fails for ~50% of test vectors. So if you run the aforementioned JSON vectors, the first test passes, whereas the second does not.




So in the word list っ and つ are sorted as the same letter.  This may be standard for japanese localization, but if you don't have this localization installed you get a different order.

Yes, but the end user never sees the list. Cheesy

Word lists are created and utilised solely by developers, so it shouldn't matter what locale it is, because the code is looking to find a sorted (by codepoint) word list.

Perhaps a BIP39 failsafe should be checking that
Code:
sorted(wordlist) == wordlist.txt

Quote
The bad thing is that the binary-search method in the bip39 mnemonics tool doesn't work if the list is not sorted.  Thus, for example, the unit tests of python-mnemonics fail.

See above. It's more insidious. Only some of the vectors fail. In Python, a fail will return a binary value of (-)1. Also, locale is an issue again as Japanese vectors fail entropy==>mnemonic unless you return a mnemonic with ideographic spaces



Looking into the official bip39 wordlists it seems other languages have the same problem, e.g., french:

Code:
belette
bélier
belote
bénéfice
berceau

I'm not sure what the right solution is.  Sorting the word list with LANG=C would change the indices of the words and break compatibility.  Requiring that localization setting must match the language of the word list make automatic tests difficult.  Especially if you don't have support for the language installed.

Another issue with French (though the 2 languages for Chinese are worst).... The French wordlist shares 100 words with English. So vector with entropy of 32 nullbytes, for eg, can only be differentiated as French (English = abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about) by the checksum, since abandon is both French and English (different index too IIRC). But this isn't always possible, since the mnemonic can occasionally be all shared words. Not the same issue, but indexes will differ for each language.

Between Electrum doing it's own thing, and each bip39 language being unsorted, there's major issues that need ironing out sooner rather than later..

The weird thing is, only you 2 seem to agree there's an issue in having locale sorted words!
johoe
Full Member
***
Offline Offline

Activity: 217
Merit: 238


View Profile
July 07, 2015, 10:11:33 AM
 #7


Another issue with French (though the 2 languages for Chinese are worst).... The French wordlist shares 100 words with English. So vector with entropy of 32 nullbytes, for eg, can only be differentiated as French (English = abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about) by the checksum, since abandon is both French and English (different index too IIRC). But this isn't always possible, since the mnemonic can occasionally be all shared words. Not the same issue, but indexes will differ for each language.

For Chinese the words that are in both lists have the same numeric ID.  If I understand it correctly they contain exactly the same words, it's just that they use different variants of Chinese symbols (simplified vs. traditional).   This may be bad, because the traditional Chinese seed produces different keys than the simplified Chinese seed, even though they both match the checksum.

For French vs. English, the situation is not so bad.  You have some seeds where you can't say whether they are French or English, sometimes the checksum is okay for French, sometimes for English and sometimes for both languages.  But the keys produced are at least the same (unless the French and English seeds differ by accents).  The 128/256 bit seed from which the passphrases were produced is not used for anything, so it is not so bad if they differ.

Changing the word lists opens new problems.  We can't know how many French/Spanish/Chinese/Japanese seeds are already in use.  I think, the most practical solution is to just drop the requirement that the word lists are sorted and require a linear search.  When the language is not unique, check all supported languages and accept the seed if it has the right checksum for at least one language.  Auto correction requires that the user chooses the language before entering the seed. In the end the private keys are computed from the seed, ignoring the language for which it was computed. The checksum is only there to prevent people from doing stupid things like inventing their own seed with low entropy.

Donations to 1CF62UFWXiKqFUmgQMUby9DpEW5LXjypU3
dabura667
Sr. Member
****
Offline Offline

Activity: 475
Merit: 252


View Profile
September 20, 2015, 05:21:45 AM
 #8

So in the word list っ and つ are sorted as the same letter.  This may be standard for japanese localization, but if you don't have this localization installed you get a different order.  The bad thing is that the binary-search method in the bip39 mnemonics tool doesn't work if the list is not sorted.  Thus, for example, the unit tests of python-mnemonics fail.

This is my fault:
I sorted the list BEFORE NFKD normalizing it.
Sorting again AFTER NFKD normalizing it will produce the results mentioned.

When I first made the pull request, I was under the mistaken impression that the lists were not to be NFKD normalized, and all NFKD normalization would occur in the apps... but someone corrected me, and I fixed the NFKD normalization of the list... however, it seems the order should have been changed too.

Unfortunately, there are people using phrases generated with this word order.

BIP39's weakness: the checksum depends on the order of the wordlist... therefore requiring the wordlist... but the BIP says "not require wordlist" while at the same time "require check the checksum" (which requires knowing the wordlist lol)

I can understand why ThomasV removed BIP39 functionality from Electrum now...

My Tip Address:
1DXcHTJS2DJ3xDoxw22wCt11FeAsgfzdBU
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!