fig linting + improve docs + structure of hash optimization

This commit is contained in:
protolambda 2019-06-11 21:53:38 +02:00
parent f4814862fe
commit 9ec395c04f
No known key found for this signature in database
GPG Key ID: EC89FDBB2B4C7623

View File

@ -2,18 +2,27 @@ from hashlib import sha256
ZERO_BYTES32 = b'\x00' * 32
def _hash(x):
return sha256(x).digest()
# Minimal collection of (key, value) pairs, for fast hash-retrieval, to save on repetitive computation cost.
# Key = the hash input
# Value = the hash output
hash_cache = []
def add_zero_hashes_to_cache():
zerohashes = [(None, ZERO_BYTES32)]
for layer in range(1, 32):
k = zerohashes[layer - 1][1] + zerohashes[layer - 1][1]
zerohashes.append((k, _hash(k)))
zerohashes = zerohashes[1:]
hash_cache.extend(zerohashes[1:])
def hash(x):
for (k, h) in zerohashes:
for (k, h) in hash_cache:
if x == k:
return h
return _hash(x)