forked from Avalanche-io/c4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.go
218 lines (191 loc) · 4.2 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
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
209
210
211
212
213
214
215
216
217
218
package c4
import (
"bytes"
"crypto/sha512"
"io"
"math/bits"
"strings"
)
// `Tree` implements an ID tree as used for calculating IDs of non-contiguous
// sets of data. A C4 ID Tree is a type of merkle tree except that the list
// of IDs is sorted. According to the standard this is done to insure that
// two identical lists of IDs always resolve to the same ID.
type Tree []byte
// NewTree creates a new Tree from a DigestSlice, and copies the digests into
// the tree. However, it does not compute the tree.
func NewTree(s []ID) Tree {
size := 1
for l := len(s); l > 1; l = (l + 1) / 2 {
size += l
}
data := make([]byte, size*64)
offset := len(data) - len(s)*64
for i, id := range s {
copy(data[offset+i*64:], id[:])
}
return Tree(data)
}
func ReadTree(r io.Reader) (Tree, error) {
tree := make(Tree, 3*64)
// If the first 192 bytes are not 3 valid digests this is not a tree.
n, err := r.Read(tree)
if err != nil {
return nil, err
}
if n != len(tree) {
return nil, errInvalidTree{}
}
head := make([]ID, 3)
for i := range head {
copy(head[i][:], tree[i*64:])
}
root := head[1].Sum(head[2])
if root.Cmp(head[0]) != 0 {
return nil, errInvalidTree{}
}
buffer := make([]byte, 4096)
for err != io.EOF {
n, err = r.Read(buffer)
if err != nil && err != io.EOF {
return nil, err
}
tree = append(tree, buffer[:n]...)
}
// tree.rows = buildRows(tree)
return tree, nil
}
// Compute resolves all Digests in the tree, and returns the root Digest
func (t Tree) compute() (id ID) {
h := sha512.New()
rows := buildRows(t)
for i := len(rows) - 2; i >= 0; i-- {
l := len(rows[i+1])
for j := 0; j < l; j += 64 * 2 {
jj := j / 2
if j+64 >= l {
copy(rows[i][jj:], rows[i+1][j:j+64])
} else {
h.Reset()
if bytes.Compare(rows[i+1][j:j+64], rows[i+1][j+64:j+64*2]) == 1 {
h.Write(rows[i+1][j+64 : j+64*2])
h.Write(rows[i+1][j : j+64])
} else {
h.Write(rows[i+1][j : j+64*2])
}
copy(rows[i][jj:], h.Sum(nil)[0:64])
}
}
}
copy(id[:], t)
return id
}
func (t Tree) String() string {
var b strings.Builder
var id ID
if !t.valid() {
t.compute()
}
for i := 0; i < len(t); i += 64 {
copy(id[:], t[i:])
b.WriteString(id.String())
}
return b.String()
}
func (t Tree) valid() bool {
for _, b := range t[:64] {
if b != 0 {
return true
}
}
return false
}
// Bytes returns the tree as a slice of bytes.
func (t Tree) Bytes() []byte {
if !t.valid() {
t.compute()
}
return t
}
// Number of IDs in the list (i.e. the length of the bottom row of the tree).
func (t Tree) Len() int {
return listSize(len(t) / 64)
}
// The ID of the list (i.e. the level 0 of the tree).
func (t Tree) ID() (id ID) {
if !t.valid() {
return t.compute()
}
copy(id[:], t)
return id
}
func buildRows(data []byte) [][]byte {
length := len(data) / 64
w := listSize(length)
s := length - w
r := 1
for l := w; l > 1; l = (l + 1) / 2 {
r++
}
rows := make([][]byte, r)
i := len(rows) - 1
for range rows {
row := data[s*64 : (s+w)*64]
rows[i] = row
w = (w + 1) / 2
s -= w
i--
}
return rows
}
// listSize computes the length of the list represented by a
// tree given `total` number of branchs in the tree.
func listSize(total int) int {
// Given that:
// total >= 2*len(list)-1
// and
// total <= 2*len(list)-1+log2(len(list))
// The range of possible values for length are:
max := (total + 1) / 2
// min := max - log2(total)
min := max - bits.Len(uint(total))
if treeSize(min) == total {
return min
}
if treeSize(max) == total {
return max
}
// If not min or max, then we simply binary search for the matching size.
for {
length := (min + max) / 2
t := treeSize(length)
// fmt.Printf("listSize min, max, length, t: %d, %d, %d, %d\n", min, max, length, t)
if t == total {
return length
}
if t > total {
max = length
continue
}
if t < total {
min = length
continue
}
return length
}
}
// treeSize computes the total number of branchs required to represent
// a list of length `l` elements.
func treeSize(l int) int {
// Account for the root branch
total := 1
for ; l > 1; l = (l + 1) / 2 {
total = total + l
}
return total
}
// Left
// r = row + 1
// i = i * 2
// Right
// r = row + 1
// i = (i+1)*2