forked from celestiaorg/rsmt2d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.go
52 lines (42 loc) · 1.03 KB
/
tree.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
package rsmt2d
import (
"crypto/sha256"
"github.com/celestiaorg/merkletree"
)
// TreeConstructorFn creates a fresh Tree instance to be used as the Merkle inside of rsmt2d.
type TreeConstructorFn = func() Tree
// SquareIndex contains all information needed to identify the cell that is being
// pushed
type SquareIndex struct {
Axis, Cell uint
}
// Tree wraps Merkle tree implementations to work with rsmt2d
type Tree interface {
Push(data []byte, idx SquareIndex)
Root() []byte
}
var _ Tree = &DefaultTree{}
type DefaultTree struct {
*merkletree.Tree
leaves [][]byte
root []byte
}
func NewDefaultTree() Tree {
return &DefaultTree{
Tree: merkletree.New(sha256.New()),
leaves: make([][]byte, 0, 128),
}
}
func (d *DefaultTree) Push(data []byte, _idx SquareIndex) {
// ignore the idx, as this implementation doesn't need that info
d.leaves = append(d.leaves, data)
}
func (d *DefaultTree) Root() []byte {
if d.root == nil {
for _, l := range d.leaves {
d.Tree.Push(l)
}
d.root = d.Tree.Root()
}
return d.root
}