tbech32: another around 10% speedup for bech32_decode - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit e0cfb2179d108e79303223684646bc1d905f5a37 DIR parent b83f7159a9dd635a7a6f12d479e8704f6d7ae4b2 HTML Author: SomberNight <somber.night@protonmail.com> Date: Sun, 28 Feb 2021 18:13:06 +0100 bech32: another around 10% speedup for bech32_decode tturns out stdlib ord() is somewhat slow; also, only lookup data chars once benchmarked with: ``` import time import electrum from electrum.segwit_addr import bech32_decode electrum.constants.set_testnet() inv = "lntb4m1p00zfpppp597ely08ffhk8n3emeswukt0y3qfvt3sj3ufkhnaatlrswj2xvwuqsp5vu3ezu44ka8arvgda44yalysp3k3edlvg56cjkk5lvu4e4anmdssdq2v9ekgctnvscqzynxqyz5vq9qypqsqrzjqv8shunq4nda8mw2mpxhtz8v03wlgug7sln2yvqklxym35ayz3erqxct8vqqqcqqqqqqqqlgqqqqqqgq9qrzjqdxvvgt048y4htef7r63r4ha9kctz3d6l3za0053ahe597wgrkc4gxct8cqqqfsqqqqqqqlgqqqqqqgq9qrzjqwyx8nu2hygyvgc02cwdtvuxe0lcxz06qt3lpsldzcdr46my5epmjxct8vqqqdcqqqqqqqlgqqqqqqgq9qrzjqf56jn5txtqqtepnd0ahg0qg5m5mavfajsx403rem9wgu6rue0de7xct8vqqqtgqqqqqqqlgqqqq86qq9qrzjq027z73uyyl7fy8pkrpcn7x0el82pz3fw974p2052de4uz4j5lqqxx49tuqqqwgqqqqqqqqqqqqqqqqqpurzjqfj34n62wztqjxl59w4drxekg04rrrtf08mdestwhtky84ds7ja0yxct8sqqq3qqqqqqqqlgqqqqqqgq9qrzjqd872t5c5r5a8ssmwelpkdccsyn9mrr40rpp7khad4jr3kssxj9nvx49vgqqqnqqqqqqqqlgqqqq05qqgcxwu0ervh6atmqmqv7pmenhmc207gncyj0mcxedpwm8f56y2yl3qpq6mzjak37ddmeayd9unektmffv5rq8dvlpgq00rmmdalda73yhgqep0zuz" def f(): for _ in range(10000): addr = bech32_decode(inv, ignore_long_length=True) tt0 = time.time() f() tt1 = time.time() print(f"{t1-t0:.4f}") ``` Diffstat: M electrum/segwit_addr.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) --- DIR diff --git a/electrum/segwit_addr.py b/electrum/segwit_addr.py t@@ -61,19 +61,23 @@ def bech32_encode(hrp, data): return hrp + '1' + ''.join([CHARSET[d] for d in combined]) -def bech32_decode(bech, ignore_long_length=False): +def bech32_decode(bech: str, ignore_long_length=False): """Validate a Bech32 string, and determine HRP and data.""" - if ((any(ord(x) < 33 or ord(x) > 126 for x in bech)) or - (bech.lower() != bech and bech.upper() != bech)): + bech_lower = bech.lower() + if bech_lower != bech and bech.upper() != bech: return (None, None) - bech = bech.lower() pos = bech.rfind('1') if pos < 1 or pos + 7 > len(bech) or (not ignore_long_length and len(bech) > 90): return (None, None) - if not all(x in CHARSET for x in bech[pos+1:]): + # check that HRP only consists of sane ASCII chars + if any(ord(x) < 33 or ord(x) > 126 for x in bech[:pos+1]): return (None, None) + bech = bech_lower hrp = bech[:pos] - data = [_CHARSET_INVERSE[x] for x in bech[pos+1:]] + try: + data = [_CHARSET_INVERSE[x] for x in bech[pos+1:]] + except KeyError: + return (None, None) if not bech32_verify_checksum(hrp, data): return (None, None) return (hrp, data[:-6])