electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit 6ebbaa60ef31f110300d3df1c3345e6624d83e48
parent bc4f22503f3c38a1c458db4ede364f0ea02dd9b1
Author: SomberNight <somber.night@protonmail.com>
Date:   Fri, 15 Nov 2019 12:08:06 +0100

old_mnemonic: speed up mn_decode

mn_decode is used by mnemonic.make_seed which now takes around 25% less time

Diffstat:
Melectrum/keystore.py | 2+-
Melectrum/old_mnemonic.py | 11++++++-----
Melectrum/tests/test_mnemonic.py | 5+++--
3 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/electrum/keystore.py b/electrum/keystore.py @@ -904,7 +904,7 @@ def from_seed(seed, passphrase, is_p2sh=False): xtype = 'p2wsh' if is_p2sh else 'p2wpkh' keystore.add_xprv_from_seed(bip32_seed, xtype, der) else: - raise BitcoinException('Unexpected seed type {}'.format(t)) + raise BitcoinException('Unexpected seed type {}'.format(repr(t))) return keystore def from_private_key_list(text): diff --git a/electrum/old_mnemonic.py b/electrum/old_mnemonic.py @@ -26,7 +26,7 @@ # list of words from http://en.wiktionary.org/wiki/Wiktionary:Frequency_lists/Contemporary_poetry -words = [ +words = ( "like", "just", "love", @@ -1653,7 +1653,8 @@ words = [ "unseen", "weapon", "weary", -] +) +_words_indexes = {w: i for i, w in enumerate(words)} n = len(words) assert n == 1626 @@ -1679,9 +1680,9 @@ def mn_decode( wlist ): out = '' for i in range(len(wlist)//3): word1, word2, word3 = wlist[3*i:3*i+3] - w1 = words.index(word1) - w2 = (words.index(word2))%n - w3 = (words.index(word3))%n + w1 = _words_indexes[word1] + w2 = (_words_indexes[word2]) % n + w3 = (_words_indexes[word3]) % n x = w1 +n*((w2-w1)%n) +n*n*((w3-w2)%n) out += '%08x'%x return out diff --git a/electrum/tests/test_mnemonic.py b/electrum/tests/test_mnemonic.py @@ -186,5 +186,6 @@ class Test_seeds(ElectrumTestCase): self.assertTrue(is_old_seed("0123456789ABCDEF" * 4)) def test_seed_type(self): - for seed_words, _type in self.mnemonics: - self.assertEqual(_type, seed_type(seed_words), msg=seed_words) + for idx, (seed_words, _type) in enumerate(self.mnemonics): + with self.subTest(msg=f"seed_type_subcase_{idx}", seed_words=seed_words): + self.assertEqual(_type, seed_type(seed_words), msg=seed_words)