descriptor.py: speed-up DescriptorChecksum a bit
This commit is contained in:
@@ -46,6 +46,11 @@ def PolyMod(c: int, val: int) -> int:
|
||||
c ^= 0x644d626ffd
|
||||
return c
|
||||
|
||||
|
||||
_INPUT_CHARSET = "0123456789()[],'/*abcdefgh@:$%{}IJKLMNOPQRSTUVWXYZ&+-.;<=>?!^_|~ijklmnopqrstuvwxyzABCDEFGH`#\"\\ "
|
||||
_INPUT_CHARSET_INV = {c: i for (i, c) in enumerate(_INPUT_CHARSET)}
|
||||
_CHECKSUM_CHARSET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"
|
||||
|
||||
def DescriptorChecksum(desc: str) -> str:
|
||||
"""
|
||||
Compute the checksum for a descriptor
|
||||
@@ -53,15 +58,13 @@ def DescriptorChecksum(desc: str) -> str:
|
||||
:param desc: The descriptor string to compute a checksum for
|
||||
:return: A checksum
|
||||
"""
|
||||
INPUT_CHARSET = "0123456789()[],'/*abcdefgh@:$%{}IJKLMNOPQRSTUVWXYZ&+-.;<=>?!^_|~ijklmnopqrstuvwxyzABCDEFGH`#\"\\ "
|
||||
CHECKSUM_CHARSET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"
|
||||
|
||||
c = 1
|
||||
cls = 0
|
||||
clscount = 0
|
||||
for ch in desc:
|
||||
pos = INPUT_CHARSET.find(ch)
|
||||
if pos == -1:
|
||||
try:
|
||||
pos = _INPUT_CHARSET_INV[ch]
|
||||
except KeyError:
|
||||
return ""
|
||||
c = PolyMod(c, pos & 31)
|
||||
cls = cls * 3 + (pos >> 5)
|
||||
@@ -78,7 +81,7 @@ def DescriptorChecksum(desc: str) -> str:
|
||||
|
||||
ret = [''] * 8
|
||||
for j in range(0, 8):
|
||||
ret[j] = CHECKSUM_CHARSET[(c >> (5 * (7 - j))) & 31]
|
||||
ret[j] = _CHECKSUM_CHARSET[(c >> (5 * (7 - j))) & 31]
|
||||
return ''.join(ret)
|
||||
|
||||
def AddChecksum(desc: str) -> str:
|
||||
|
||||
@@ -25,7 +25,7 @@ from enum import Enum
|
||||
from typing import Tuple, Optional, Sequence, NamedTuple, List
|
||||
|
||||
CHARSET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"
|
||||
_CHARSET_INVERSE = {x: CHARSET.find(x) for x in CHARSET}
|
||||
_CHARSET_INVERSE = {c: i for (i, c) in enumerate(CHARSET)}
|
||||
|
||||
BECH32_CONST = 1
|
||||
BECH32M_CONST = 0x2bc830a3
|
||||
|
||||
Reference in New Issue
Block a user