-
Notifications
You must be signed in to change notification settings - Fork 340
/
htree.go
196 lines (155 loc) · 3.73 KB
/
htree.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
/*
Copyright 2021 CodeNotary, Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package htree
import (
"crypto/sha256"
"errors"
"math/bits"
)
var ErrMaxWidthExceeded = errors.New("max width exceeded")
var ErrIllegalArguments = errors.New("illegal arguments")
var ErrIllegalState = errors.New("illegal state")
const LeafPrefix = byte(0)
const NodePrefix = byte(1)
type HTree struct {
levels [][][sha256.Size]byte
maxWidth int
width int
root [sha256.Size]byte
}
type InclusionProof struct {
Leaf int
Width int
Terms [][sha256.Size]byte
}
func New(maxWidth int) (*HTree, error) {
if maxWidth < 1 {
return nil, ErrIllegalArguments
}
lw := 1
for lw < maxWidth {
lw = lw << 1
}
height := bits.Len64(uint64(maxWidth-1)) + 1
levels := make([][][sha256.Size]byte, height)
for l := 0; l < height; l++ {
levels[l] = make([][sha256.Size]byte, lw>>l)
}
return &HTree{
levels: levels,
maxWidth: maxWidth,
}, nil
}
func (t *HTree) BuildWith(digests [][sha256.Size]byte) error {
if len(digests) > t.maxWidth {
return ErrMaxWidthExceeded
}
if len(digests) == 0 {
return ErrIllegalArguments
}
for i, d := range digests {
leaf := [1 + sha256.Size]byte{LeafPrefix}
copy(leaf[1:], d[:])
t.levels[0][i] = sha256.Sum256(leaf[:])
}
l := 0
w := len(digests)
for w > 1 {
b := [1 + 2*sha256.Size]byte{NodePrefix}
wn := 0
for i := 0; i+1 < w; i += 2 {
copy(b[1:], t.levels[l][i][:])
copy(b[1+sha256.Size:], t.levels[l][i+1][:])
t.levels[l+1][wn] = sha256.Sum256(b[:])
wn++
}
if w%2 == 1 {
t.levels[l+1][wn] = t.levels[l][w-1]
wn++
}
l++
w = wn
}
t.width = len(digests)
t.root = t.levels[l][0]
return nil
}
func (t *HTree) Root() (root [sha256.Size]byte, err error) {
if t.width == 0 {
err = ErrIllegalState
return
}
return t.root, nil
}
// InclusionProof returns the shortest list of additional nodes required to compute the root
// It's an adaption from the algorithm for proof construction at github.com/codenotary/merkletree
func (t *HTree) InclusionProof(i int) (proof *InclusionProof, err error) {
if i >= t.width {
return nil, ErrIllegalArguments
}
m := i
n := t.width
var offset int
var l int
var r int
proof = &InclusionProof{
Leaf: i,
Width: t.width,
}
if t.width == 1 {
return
}
for {
d := bits.Len(uint(n - 1))
k := 1 << (d - 1)
if m < k {
l, r = offset+k, offset+n-1
n = k
} else {
l, r = offset, offset+k-1
m = m - k
n = n - k
offset += k
}
layer := bits.Len(uint(r - l))
index := l / (1 << layer)
proof.Terms = append([][sha256.Size]byte{t.levels[layer][index]}, proof.Terms...)
if n < 1 || (n == 1 && m == 0) {
return
}
}
}
func VerifyInclusion(proof *InclusionProof, digest, root [sha256.Size]byte) bool {
if proof == nil {
return false
}
leaf := [1 + sha256.Size]byte{LeafPrefix}
copy(leaf[1:], digest[:])
calcRoot := sha256.Sum256(leaf[:])
i := proof.Leaf
r := proof.Width - 1
for _, t := range proof.Terms {
b := [1 + 2*sha256.Size]byte{NodePrefix}
if i%2 == 0 && i != r {
copy(b[1:], calcRoot[:])
copy(b[1+sha256.Size:], t[:])
} else {
copy(b[1:], t[:])
copy(b[1+sha256.Size:], calcRoot[:])
}
calcRoot = sha256.Sum256(b[:])
i /= 2
r /= 2
}
return i == r && root == calcRoot
}