package merkle
import "hash"
type Subtree struct {
root *Node
left *Subtree right *Subtree
height int hashF hash.Hash
}
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
}
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}}
}