-
Notifications
You must be signed in to change notification settings - Fork 0
/
subtree.go
62 lines (48 loc) · 1.49 KB
/
subtree.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package gomerkle
import "hash"
// subtrees always should have power of 2 number of elements.
// tree could contain few of subtrees.
// root hash calculates from right to left by summing subtree roots hashes.
type Subtree struct {
root *Node // root node
left *Subtree // left subtree from this one
right *Subtree // right subtree from this one
height int // height of subtree
// hash function to hash sum nodes and hash data
hashF hash.Hash
}
// get proofs for root of this subtree
func (t *Subtree) GetRootProofs() []Proof {
proofs := make([]Proof, 0)
proofs = append(proofs, t.getRightSubtreesProof()...)
proofs = append(proofs, t.getLeftSubtreesProof()...)
return proofs
}
func (t *Subtree) getLeftSubtreesProof() []Proof {
proofs := make([]Proof, 0)
current := t.left
for current != nil {
proofs = append(proofs, Proof{hash: current.root.hash, leftSide: false})
current = current.left
}
return proofs
}
// right proof is only one cause we have to sum all right subtrees
// we have to sum hashes from right to left
func (t *Subtree) getRightSubtreesProof() []Proof {
if t.right == nil {
return make([]Proof, 0)
}
hashesToSum := make([][]byte, 0)
rightTree := t.right
for rightTree != nil {
hashesToSum = append(hashesToSum, rightTree.root.hash)
rightTree = rightTree.right
}
n := len(hashesToSum) - 1
proofHash := hashesToSum[n]
for i := n - 1; i >= 0; i-- {
proofHash = sum(t.hashF, proofHash, hashesToSum[i])
}
return []Proof{{hash: proofHash, leftSide: true}}
}