2019-03-26 13:17:08 +00:00
|
|
|
from hashlib import sha256
|
2019-03-18 18:51:52 +00:00
|
|
|
|
2019-06-11 17:23:45 +00:00
|
|
|
ZERO_BYTES32 = b'\x00' * 32
|
2019-03-18 18:51:52 +00:00
|
|
|
|
2019-06-11 17:23:45 +00:00
|
|
|
def _hash(x):
|
2019-06-01 00:40:29 +00:00
|
|
|
return sha256(x).digest()
|
2019-06-11 17:23:45 +00:00
|
|
|
|
|
|
|
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:]
|
|
|
|
|
|
|
|
|
|
|
|
def hash(x):
|
|
|
|
for (k, h) in zerohashes:
|
|
|
|
if x == k:
|
|
|
|
return h
|
|
|
|
return _hash(x)
|