-
Notifications
You must be signed in to change notification settings - Fork 5
/
tree_index.go
208 lines (177 loc) · 3.97 KB
/
tree_index.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package indexed
import (
"github.com/kiambogo/go-hypercore/bitfield"
ft "github.com/kiambogo/go-hypercore/flattree"
)
type Verification struct {
node uint64
top uint64
}
type tree struct {
bitfield *bitfield.Bitfield
}
func NewTree(bitfield *bitfield.Bitfield) tree {
return tree{
bitfield: bitfield,
}
}
func NewDefaultTree() tree {
return tree{
bitfield: bitfield.NewBitfield(0),
}
}
func (t tree) Get(index uint64) bool {
return t.bitfield.GetBit(index)
}
func (t *tree) Set(index uint64) bool {
// update the element in the tree at index
if !t.bitfield.SetBit(int(index), true) {
return false
}
// iteratively update the tree, setting the parent of index to true if the sibling is also set
for t.bitfield.GetBit(ft.Sibling(index)) {
index = ft.Parent(index)
if !t.bitfield.SetBit(int(index), true) {
break
}
}
return true
}
func (t tree) Proof(index, digest uint64, remoteTree tree) (proof Proof, verified bool, err error) {
var roots []uint64
if !t.Get(index) {
return proof, false, nil
}
nodes := []uint64{index}
if digest == 1 {
return Proof{
index: index,
verifiedBy: 0,
nodes: nodes,
}, true, nil
}
next := index
hasRoot := digest & 1
sibling := uint64(0)
digest >>= 1
for digest > 0 {
if digest == 1 && hasRoot != 0 {
if t.Get(next) {
_ = remoteTree.Set(next)
}
nextSibling := ft.Sibling(next)
if nextSibling < next {
next = nextSibling
}
roots, err = ft.FullRoots(ft.RightSpan(next) + 2)
if err != nil {
return
}
for _, root := range roots {
if t.Get(root) {
_ = remoteTree.Set(root)
}
}
break
}
sibling := ft.Sibling(next)
if !isEven(digest) && t.Get(sibling) {
remoteTree.Set(sibling)
}
next = ft.Parent(next)
digest >>= 1
}
next = index
for !remoteTree.Get(next) {
sibling = ft.Sibling(next)
if !t.Get(sibling) {
verifiedBy := t.VerifiedBy(next)
roots, err = ft.FullRoots(verifiedBy.node)
if err != nil {
return
}
for _, root := range roots {
if root != next && !remoteTree.Get(root) {
nodes = append(nodes, root)
}
}
return Proof{
index: index,
verifiedBy: verifiedBy.node,
nodes: nodes,
}, true, err
} else if !remoteTree.Get(sibling) {
nodes = append(nodes, sibling)
}
next = ft.Parent(next)
}
return Proof{
index: index,
verifiedBy: 0,
nodes: nodes,
}, true, nil
}
// Digest will calculate the digest of the data at a particular index
// It does this by checking the uncles in the merkle tree
func (t tree) Digest(index uint64) (digest uint64) {
if t.Get(index) {
return 1
}
depthBit := 2
nextIndex := ft.Sibling(index)
parentIndex := ft.Parent(index)
maxTreeIndex := max(nextIndex+2, t.bitfield.Len())
rightNodesLeftToConsider := func(next uint64) bool { return ft.RightSpan(next) < maxTreeIndex }
leftNodesLeftToConsider := func(parent uint64) bool { return ft.LeftSpan(parent) > 0 }
for (rightNodesLeftToConsider(nextIndex)) || (leftNodesLeftToConsider(parentIndex)) {
if t.Get(nextIndex) {
digest |= uint64(depthBit)
}
if t.Get(parentIndex) {
digest |= uint64(2*depthBit + 1)
if digest+uint64(1) == uint64(4*depthBit) {
return 1
}
}
nextIndex = ft.Sibling(parentIndex)
parentIndex = ft.Parent(nextIndex)
depthBit *= 2
}
return
}
func (t tree) VerifiedBy(index uint64) (verification Verification) {
if !t.Get(index) {
return
}
depth := ft.Depth(index)
top := index
parent := ft.Parent(index)
depth += 1
for t.Get(parent) && t.Get(ft.Sibling(top)) {
top = parent
parent = ft.Parent(top)
depth += 1
}
depth -= 1
for depth != 0 {
top, _ = ft.LeftChild(ft.Index(depth, ft.Offset(top)+1))
depth -= 1
for !t.Get(top) && depth > 0 {
top, _ = ft.LeftChild(top)
depth -= 1
}
}
if t.Get(top) {
return Verification{node: top + 2, top: top}
}
return Verification{node: top, top: top}
}
func max(x, y uint64) uint64 {
if x >= y {
return x
}
return y
}
func isEven(n uint64) bool {
return n%2 == 0
}