Skip to content

Commit b2eec9d

Browse files
authored
Use path from node to root for inclusion proofs (#292)
Removes duplication and rehashing can be implemented once as well
1 parent 8004e35 commit b2eec9d

File tree

1 file changed

+1
-40
lines changed

1 file changed

+1
-40
lines changed

merkle/merkle_path.go

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -30,46 +30,7 @@ func CalcInclusionProofNodeAddresses(treeSize, index int64, maxBitLen int) ([]No
3030
return []NodeFetch{}, fmt.Errorf("invalid params ts: %d index: %d, bitlen:%d", treeSize, index, maxBitLen)
3131
}
3232

33-
proof := make([]NodeFetch, 0, bitLen(treeSize)+1)
34-
35-
sizeLessOne := treeSize - 1
36-
37-
if bitLen(treeSize) == 0 || index > sizeLessOne {
38-
return proof, nil
39-
}
40-
41-
node := index
42-
depth := 0
43-
lastNodeAtLevel := sizeLessOne
44-
45-
for depth < bitLen(sizeLessOne) {
46-
sibling := node ^ 1
47-
if sibling < lastNodeAtLevel {
48-
// Tree must be completely filled in up to this node index
49-
n, err := storage.NewNodeIDForTreeCoords(int64(depth), sibling, maxBitLen)
50-
if err != nil {
51-
return nil, err
52-
}
53-
proof = append(proof, NodeFetch{NodeID: n})
54-
} else if sibling == lastNodeAtLevel {
55-
// We're working in the same node coordinate space as the C++ reference implementation
56-
// (depth, index) but intermediate nodes with only one child are not written by our storage.
57-
// In these cases the value that we want is a copy of a node further down (multiple levels
58-
// may be skipped).
59-
l, sibling := skipMissingLevels(treeSize, lastNodeAtLevel, depth, node)
60-
n, err := storage.NewNodeIDForTreeCoords(int64(l), sibling, maxBitLen)
61-
if err != nil {
62-
return nil, err
63-
}
64-
proof = append(proof, NodeFetch{NodeID: n})
65-
}
66-
67-
node = node >> 1
68-
lastNodeAtLevel = lastNodeAtLevel >> 1
69-
depth++
70-
}
71-
72-
return proof, nil
33+
return pathFromNodeToRootAtSnapshot(index, 0, treeSize, maxBitLen)
7334
}
7435

7536
// CalcConsistencyProofNodeAddresses returns the tree node IDs needed to

0 commit comments

Comments
 (0)