summaryrefslogtreecommitdiff
path: root/tools/certtools.py
diff options
context:
space:
mode:
authorMagnus Ahltorp <map@kth.se>2015-02-20 01:58:17 +0100
committerMagnus Ahltorp <map@kth.se>2015-02-20 01:58:17 +0100
commit4ad45f4b58fe0432f0ee48d9e2fa3cb61792e765 (patch)
tree9d4d35b06bb17077765c27ad279906a839a5ce5c /tools/certtools.py
parentd01055be865c2f36100b123f66baf44c8a4d4557 (diff)
testcase1: Actually verify inclusion proof
Diffstat (limited to 'tools/certtools.py')
-rw-r--r--tools/certtools.py26
1 files changed, 24 insertions, 2 deletions
diff --git a/tools/certtools.py b/tools/certtools.py
index 11e09bb..fdff0e1 100644
--- a/tools/certtools.py
+++ b/tools/certtools.py
@@ -512,10 +512,11 @@ def path_as_string(pos, level, treesize):
return ""
return path
-def nodes_for_pos(pos, treesize):
+def nodes_for_subtree(subtreesize, treesize):
height = merkle_height(treesize)
nodes = []
level = 0
+ pos = subtreesize
while pos > 0 and pos & 1 == 0:
pos >>= 1
level += 1
@@ -532,8 +533,29 @@ def nodes_for_pos(pos, treesize):
level += 1
return nodes
+def nodes_for_index(pos, treesize):
+ height = merkle_height(treesize)
+ nodes = []
+ level = 0
+ pos ^= 1
+ #print pos, level
+ while level < height:
+ pos_level0 = pos * (2 ** level)
+ #print pos, level
+ if pos_level0 < treesize:
+ nodes.append((pos, level))
+ pos >>= 1
+ pos ^= 1
+ level += 1
+ return nodes
+
def verify_consistency_proof(consistency_proof, first, second):
- chain = zip(nodes_for_pos(first, second), consistency_proof)
+ chain = zip(nodes_for_subtree(first, second), consistency_proof)
(_, hash) = reduce(lambda e1, e2: combine_two_hashes(e1, e2, second), chain)
(_, oldhash) = reduce(lambda e1, e2: combine_two_hashes(e1, e2, first), chain)
return (oldhash, hash)
+
+def verify_inclusion_proof(inclusion_proof, index, treesize, leafhash):
+ chain = zip([(index, 0)] + nodes_for_index(index, treesize), [leafhash] + inclusion_proof)
+ (_, hash) = reduce(lambda e1, e2: combine_two_hashes(e1, e2, treesize), chain)
+ return hash