Skip to content

Commit

Permalink
drop binary search
Browse files Browse the repository at this point in the history
  • Loading branch information
matejcik committed Apr 28, 2022
1 parent 941c296 commit e3b8830
Showing 1 changed file with 2 additions and 21 deletions.
23 changes: 2 additions & 21 deletions src/mnemonic/mnemonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

import bisect
import hashlib
import hmac
import itertools
import os
import secrets
import unicodedata
from typing import AnyStr, List, Optional, Sequence, TypeVar, Union
from typing import AnyStr, List, TypeVar, Union

_T = TypeVar("_T")
PBKDF2_ROUNDS = 2048
Expand All @@ -37,18 +36,6 @@ class ConfigurationError(Exception):
pass


# From <https://stackoverflow.com/questions/212358/binary-search-bisection-in-python/2233940#2233940>
def binary_search(
a: Sequence[_T],
x: _T,
lo: int = 0,
hi: Optional[int] = None, # can't use a to specify default for hi
) -> int:
hi = hi if hi is not None else len(a) # hi defaults to len(a)
pos = bisect.bisect_left(a, x, lo, hi) # find insertion position
return pos if pos != hi and a[pos] == x else -1 # don't walk off the end


# Refactored code segments from <https://github.com/keis/base58>
def b58encode(v: bytes) -> str:
alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
Expand Down Expand Up @@ -79,8 +66,6 @@ def __init__(self, language: str = "english"):
)
else:
raise ConfigurationError("Language not detected")
# We can use binary search for English language
self.use_binary_search = language == "english"
# Japanese must be joined by ideographic space
self.delimiter = "\u3000" if language == "japanese" else " "

Expand Down Expand Up @@ -156,11 +141,7 @@ def to_entropy(self, words: Union[List[str], str]) -> bytearray:
wordindex = 0
for word in words:
# Find the words index in the wordlist
ndx = (
binary_search(self.wordlist, word)
if self.use_binary_search
else self.wordlist.index(word)
)
ndx = self.wordlist.index(word)
if ndx < 0:
raise LookupError('Unable to find "%s" in word list.' % word)
# Set the next 11 bits to the value of the index.
Expand Down

0 comments on commit e3b8830

Please sign in to comment.