summaryrefslogtreecommitdiff
path: root/tools/certtools.py
diff options
context:
space:
mode:
Diffstat (limited to 'tools/certtools.py')
-rw-r--r--tools/certtools.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/tools/certtools.py b/tools/certtools.py
index 16c2105..b132caa 100644
--- a/tools/certtools.py
+++ b/tools/certtools.py
@@ -226,3 +226,27 @@ def timing_point(timer_dict=None, name=None):
else:
timer_dict = {"deltatimes":[], "lasttime":t}
return timer_dict
+
+def internal_hash(pair):
+ if len(pair) == 1:
+ return pair[0]
+ else:
+ hash = hashlib.sha256()
+ hash.update(struct.pack(">b", 1))
+ hash.update(pair[0])
+ hash.update(pair[1])
+ return hash.digest()
+
+def chunks(l, n):
+ return [l[i:i+n] for i in range(0, len(l), n)]
+
+def next_merkle_layer(layer):
+ return [internal_hash(pair) for pair in chunks(layer, 2)]
+
+def build_merkle_tree(layer0):
+ layers = []
+ current_layer = layer0
+ while len(current_layer) > 1:
+ current_layer = next_merkle_layer(current_layer)
+ layers.append(current_layer)
+ return layers