Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit c396a35

Browse files
committed
Added optiized RLP decoding routines
1 parent 70a653a commit c396a35

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

ethereum/fast_rlp.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,25 @@ def consume_length_prefix(rlp, start):
6767
l = big_endian_to_int(rlp[start + 1:start + 1 + ll])
6868
return (list, l, start + 1 + ll)
6969

70+
def optimized_decode_single(x, pos):
71+
z = safe_ord(x[pos])
72+
if z < 128:
73+
return x[pos: pos + 1], 1
74+
elif z < 184:
75+
return x[pos + 1: pos + z - 127], z - 127
76+
else:
77+
ll = big_endian_to_int(x[pos + 1: pos + z - 182])
78+
return x[pos + z - 182: pos + z - 182 + ll], z - 182 + ll
79+
80+
def optimized_decode_list(rlp):
81+
o, pos = [], 0
82+
_typ, _len, pos = consume_length_prefix(rlp, pos)
83+
while pos < len(rlp):
84+
x, inc = optimized_decode_single(rlp, pos)
85+
pos += inc
86+
o.append(x)
87+
return o
88+
7089
#
7190
if sys.version_info.major == 2:
7291
encode_optimized = _encode_optimized

ethereum/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def bytes_to_int(value):
8585

8686

8787
def ecrecover_to_pub(rawhash, v, r, s):
88-
if secp256k1:
88+
if secp256k1 and hasattr(secp256k1, "PublicKey"):
8989
# Legendre symbol check; the secp256k1 library does not seem to do this
9090
pk = secp256k1.PublicKey(flags=secp256k1.ALL_FLAGS)
9191
xc = r * r * r + 7

0 commit comments

Comments
 (0)