This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
treehashers.go
193 lines (166 loc) · 5.04 KB
/
treehashers.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
package lazyledger
import (
"hash"
"io"
"gitlab.com/NebulousLabs/merkletree"
)
// MessageSubtreeHasher implements merkletree.SubtreeHasher by reading leaf data from an underlying slice of messages.
type MessageSubtreeHasher struct {
messages *[]Message
h hash.Hash
pos int
}
// NewMessageSubtreeHasher returns a new MessageSubtreeHasher for a pointer to a slice of messages.
func NewMessageSubtreeHasher(messages *[]Message, h hash.Hash) *MessageSubtreeHasher {
return &MessageSubtreeHasher{
messages: messages,
h: h,
}
}
// NextSubtreeRoot implements SubtreeHasher.
func (msh *MessageSubtreeHasher) NextSubtreeRoot(subtreeSize int) ([]byte, error) {
tree := merkletree.New(msh.h)
for i := 0; i < subtreeSize; i++ {
if msh.pos >= len(*msh.messages) {
break
}
tree.Push((*msh.messages)[msh.pos].Marshal())
msh.pos += 1
}
root := tree.Root()
if root == nil {
return nil, io.EOF
}
return root, nil
}
// Skip implements SubtreeHasher.
func (msh *MessageSubtreeHasher) Skip(n int) error {
if msh.pos + n > len(*msh.messages) {
msh.pos = len(*msh.messages)
return io.ErrUnexpectedEOF
}
msh.pos += n
return nil
}
// MessageLeafHasher implements the merkletree.LeafHasher interface by reading leaf data from an underlying slice of messages.
type MessageLeafHasher struct {
messages *[]Message
h hash.Hash
pos int
messageSize int
}
// NewMessageLeafHasher returns a new MessageLeafHasher for a pointer to a slice of messages.
func NewMessageLeafHasher(messages *[]Message, h hash.Hash) *MessageLeafHasher {
return &MessageLeafHasher{
messages: messages,
h: h,
}
}
// NewPaddedMessageLeafHasher returns a new MessageLeafHasher for a pointer to a slice of messages, where messages should be padded.
func NewPaddedMessageLeafHasher(messages *[]Message, h hash.Hash, messageSize int) *MessageLeafHasher {
return &MessageLeafHasher{
messages: messages,
h: h,
messageSize: messageSize,
}
}
// NextLeafHash implements LeafHasher
func (mlh *MessageLeafHasher) NextLeafHash() (leafHash []byte, err error) {
if mlh.pos >= len(*mlh.messages) {
return nil, io.EOF
}
if mlh.messageSize > 0 {
leafHash = leafSum(mlh.h, (*mlh.messages)[mlh.pos].MarshalPadded(mlh.messageSize))
} else {
leafHash = leafSum(mlh.h, (*mlh.messages)[mlh.pos].Marshal())
}
err = nil
mlh.pos += 1
return
}
// HashLeafHasher implements the merkletree.LeafHasher interface from a slice of hashes.
type HashLeafHasher struct {
hashes [][]byte
pos int
}
// NewHashLeafHasher returns a new MessageLeafHasher for a slice of hashes.
func NewHashLeafHasher(hashes [][]byte) *HashLeafHasher {
return &HashLeafHasher{
hashes: hashes,
}
}
// NextLeafHash implements LeafHasher
func (hlh *HashLeafHasher) NextLeafHash() (leafHash []byte, err error) {
if hlh.pos >= len(hlh.hashes) {
return nil, io.EOF
}
leafHash = hlh.hashes[hlh.pos]
err = nil
hlh.pos += 1
return
}
// BytesLeafHasher implements the merkletree.LeafHasher interface by reading a slice.
type BytesLeafHasher struct {
data [][]byte
h hash.Hash
pos int
}
// NewBytesLeafHasher returns a new BytesLeafHasher for a slice.
func NewBytesLeafHasher(data [][]byte, h hash.Hash) *BytesLeafHasher {
return &BytesLeafHasher{
data: data,
h: h,
}
}
// NextLeafHash implements LeafHasher
func (blh *BytesLeafHasher) NextLeafHash() (leafHash []byte, err error) {
if blh.pos >= len(blh.data) {
return nil, io.EOF
}
leafHash = leafSum(blh.h, blh.data[blh.pos])
err = nil
blh.pos += 1
return
}
//CodedAxisSubtreeHasher implements merkletree.SubtreeHasher by reading leaf data from an underlying slice representing a coded axis.
type CodedAxisSubtreeHasher struct {
data [][]byte
h hash.Hash
pos int
}
// NewCodedAxisSubtreeHasher returns a new CodedAxisSubtreeHasher for a pointer to a slice representing a coded axis.
func NewCodedAxisSubtreeHasher(data [][]byte, h hash.Hash) *CodedAxisSubtreeHasher {
return &CodedAxisSubtreeHasher{
data: data,
h: h,
}
}
// NextSubtreeRoot implements SubtreeHasher.
func (cash *CodedAxisSubtreeHasher) NextSubtreeRoot(subtreeSize int) ([]byte, error) {
tree := merkletree.New(cash.h)
for i := 0; i < subtreeSize; i++ {
if cash.pos >= len(cash.data) {
break
}
if cash.pos >= len(cash.data) / 2 {
cash.h.(*flagDigest).setCodedMode(true)
}
tree.Push(cash.data[cash.pos])
cash.h.(*flagDigest).setCodedMode(false)
cash.pos += 1
}
root := tree.Root()
if root == nil {
return nil, io.EOF
}
return root, nil
}
// Skip implements SubtreeHasher.
func (cash *CodedAxisSubtreeHasher) Skip(n int) error {
if cash.pos + n > len(cash.data) {
cash.pos = len(cash.data)
return io.ErrUnexpectedEOF
}
cash.pos += n
return nil
}