-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathhasher.go
324 lines (281 loc) · 11.1 KB
/
hasher.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package nmt
import (
"bytes"
"errors"
"fmt"
"hash"
"github.com/celestiaorg/nmt/namespace"
)
const (
LeafPrefix = 0
NodePrefix = 1
)
var _ hash.Hash = (*NmtHasher)(nil)
var (
ErrUnorderedSiblings = errors.New("NMT sibling nodes should be ordered lexicographically by namespace IDs")
ErrInvalidNodeLen = errors.New("invalid NMT node size")
ErrInvalidLeafLen = errors.New("invalid NMT leaf size")
ErrInvalidNodeNamespaceOrder = errors.New("invalid NMT node namespace order")
)
// Hasher describes the interface nmts use to hash leafs and nodes.
//
// Note: it is not advised to create alternative hashers if following the
// specification is desired. The main reason this exists is to not follow the
// specification for testing purposes.
type Hasher interface {
IsMaxNamespaceIDIgnored() bool
NamespaceSize() namespace.IDSize
HashLeaf(data []byte) ([]byte, error)
HashNode(leftChild, rightChild []byte) ([]byte, error)
EmptyRoot() []byte
}
var _ Hasher = &NmtHasher{}
// NmtHasher is the default hasher. It follows the description of the original
// hashing function described in the LazyLedger white paper.
type NmtHasher struct { //nolint:revive
baseHasher hash.Hash
NamespaceLen namespace.IDSize
// The "ignoreMaxNs" flag influences the calculation of the namespace ID
// range for intermediate nodes in the tree i.e., HashNode method. This flag
// signals that, when determining the upper limit of the namespace ID range
// for a tree node, the maximum possible namespace ID (equivalent to
// "NamespaceLen" bytes of 0xFF, or 2^NamespaceLen-1) should be omitted if
// feasible. For a more in-depth understanding of this field, refer to the
// "HashNode".
ignoreMaxNs bool
precomputedMaxNs namespace.ID
tp byte // keeps type of NMT node to be hashed
data []byte // written data of the NMT node
}
func (n *NmtHasher) IsMaxNamespaceIDIgnored() bool {
return n.ignoreMaxNs
}
func (n *NmtHasher) NamespaceSize() namespace.IDSize {
return n.NamespaceLen
}
func NewNmtHasher(baseHasher hash.Hash, nidLen namespace.IDSize, ignoreMaxNamespace bool) *NmtHasher {
return &NmtHasher{
baseHasher: baseHasher,
NamespaceLen: nidLen,
ignoreMaxNs: ignoreMaxNamespace,
precomputedMaxNs: bytes.Repeat([]byte{0xFF}, int(nidLen)),
}
}
// Size returns the number of bytes Sum will return.
func (n *NmtHasher) Size() int {
return n.baseHasher.Size() + int(n.NamespaceLen)*2
}
// Write writes the namespaced data to be hashed.
//
// Requires data of fixed size to match leaf or inner NMT nodes. Only a single
// write is allowed.
// It panics if more than one single write is attempted.
// If the data does not match the format of an NMT non-leaf node or leaf node, an error will be returned.
func (n *NmtHasher) Write(data []byte) (int, error) {
if n.data != nil {
panic("only a single Write is allowed")
}
ln := len(data)
switch ln {
// inner nodes are made up of the nmt hashes of the left and right children
case n.Size() * 2:
// check the format of the data
leftChild := data[:n.Size()]
rightChild := data[n.Size():]
if err := n.ValidateNodes(leftChild, rightChild); err != nil {
return 0, err
}
n.tp = NodePrefix
// leaf nodes contain the namespace length and a share
default:
// validate the format of the leaf
if err := n.ValidateLeaf(data); err != nil {
return 0, err
}
n.tp = LeafPrefix
}
n.data = data
return ln, nil
}
// Sum computes the hash. Does not append the given suffix, violating the
// interface.
// It may panic if the data being hashed is invalid. This should never happen since the Write method refuses an invalid data and errors out.
func (n *NmtHasher) Sum([]byte) []byte {
switch n.tp {
case LeafPrefix:
res, err := n.HashLeaf(n.data)
if err != nil {
panic(err) // this should never happen since the data is already validated in the Write method
}
return res
case NodePrefix:
flagLen := int(n.NamespaceLen) * 2
sha256Len := n.baseHasher.Size()
leftChild := n.data[:flagLen+sha256Len]
rightChild := n.data[flagLen+sha256Len:]
res, err := n.HashNode(leftChild, rightChild)
if err != nil {
panic(err) // this should never happen since the data is already validated in the Write method
}
return res
default:
panic("nmt node type wasn't set")
}
}
// Reset resets the Hash to its initial state.
func (n *NmtHasher) Reset() {
n.tp, n.data = 255, nil // reset with an invalid node type, as zero value is a valid Leaf
n.baseHasher.Reset()
}
// BlockSize returns the hash's underlying block size.
func (n *NmtHasher) BlockSize() int {
return n.baseHasher.BlockSize()
}
func (n *NmtHasher) EmptyRoot() []byte {
n.baseHasher.Reset()
// make returns a zeroed slice, exactly what we need for the (nID || nID)
zeroSize := int(n.NamespaceLen) * 2
fullSize := zeroSize + n.baseHasher.Size()
digest := make([]byte, zeroSize, fullSize)
return n.baseHasher.Sum(digest)
}
// ValidateLeaf verifies if data is namespaced and returns an error if not.
func (n *NmtHasher) ValidateLeaf(data []byte) (err error) {
nidSize := int(n.NamespaceSize())
lenData := len(data)
if lenData < nidSize {
return fmt.Errorf("%w: got: %v, want >= %v", ErrInvalidLeafLen, lenData, nidSize)
}
return nil
}
// HashLeaf computes namespace hash of the namespaced data item `ndata` as
// ns(ndata) || ns(ndata) || hash(leafPrefix || ndata), where ns(ndata) is the
// namespaceID inside the data item namely leaf[:n.NamespaceLen]). Note that for
// leaves minNs = maxNs = ns(leaf) = leaf[:NamespaceLen]. HashLeaf can return the ErrInvalidNodeLen error if the input is not namespaced.
func (n *NmtHasher) HashLeaf(ndata []byte) ([]byte, error) {
h := n.baseHasher
h.Reset()
if err := n.ValidateLeaf(ndata); err != nil {
return nil, err
}
nID := ndata[:n.NamespaceLen]
resLen := int(2*n.NamespaceLen) + n.baseHasher.Size()
minMaxNIDs := make([]byte, 0, resLen)
minMaxNIDs = append(minMaxNIDs, nID...) // nID
minMaxNIDs = append(minMaxNIDs, nID...) // nID || nID
h.Write([]byte{LeafPrefix})
h.Write(ndata)
// compute h(LeafPrefix || ndata) and append it to the minMaxNIDs
nameSpacedHash := h.Sum(minMaxNIDs) // nID || nID || h(LeafPrefix || ndata)
return nameSpacedHash, nil
}
// MustHashLeaf is a wrapper around HashLeaf that panics if an error is
// encountered. The ndata must be a valid leaf node.
func (n *NmtHasher) MustHashLeaf(ndata []byte) []byte {
res, err := n.HashLeaf(ndata)
if err != nil {
panic(err)
}
return res
}
// nsIDRange represents the range of namespace IDs with minimum and maximum values.
type nsIDRange struct {
Min, Max namespace.ID
}
// tryFetchNodeNSRange attempts to return the min and max namespace ids.
// It will return an ErrInvalidNodeLen | ErrInvalidNodeNamespaceOrder
// if the supplied node does not conform to the namespaced hash format.
func (n *NmtHasher) tryFetchNodeNSRange(node []byte) (nsIDRange, error) {
expectedNodeLen := n.Size()
nodeLen := len(node)
if nodeLen != expectedNodeLen {
return nsIDRange{}, fmt.Errorf("%w: got: %v, want %v", ErrInvalidNodeLen, nodeLen, expectedNodeLen)
}
// check the namespace order
minNID := namespace.ID(MinNamespace(node, n.NamespaceSize()))
maxNID := namespace.ID(MaxNamespace(node, n.NamespaceSize()))
if maxNID.Less(minNID) {
return nsIDRange{}, fmt.Errorf("%w: max namespace ID %d is less than min namespace ID %d ", ErrInvalidNodeNamespaceOrder, maxNID, minNID)
}
return nsIDRange{Min: minNID, Max: maxNID}, nil
}
// ValidateNodeFormat checks whether the supplied node conforms to the
// namespaced hash format and returns an error if not.
func (n *NmtHasher) ValidateNodeFormat(node []byte) error {
_, err := n.tryFetchNodeNSRange(node)
return err
}
// tryFetchLeftAndRightNSRange attempts to return the min/max namespace ids of both
// the left and right nodes. It verifies whether left
// and right comply by the namespace hash format, and are correctly ordered
// according to their namespace IDs.
func (n *NmtHasher) tryFetchLeftAndRightNSRanges(left, right []byte) (
nsIDRange,
nsIDRange,
error,
) {
var lNsRange nsIDRange
var rNsRange nsIDRange
var err error
lNsRange, err = n.tryFetchNodeNSRange(left)
if err != nil {
return lNsRange, rNsRange, err
}
rNsRange, err = n.tryFetchNodeNSRange(right)
if err != nil {
return lNsRange, rNsRange, err
}
// check the namespace range of the left and right children
if rNsRange.Min.Less(lNsRange.Max) {
err = fmt.Errorf("%w: the min namespace ID of the right child %d is less than the max namespace ID of the left child %d", ErrUnorderedSiblings, rNsRange.Min, lNsRange.Max)
}
return lNsRange, rNsRange, err
}
// ValidateNodes is a helper function to verify the
// validity of the inputs of HashNode. It verifies whether left
// and right comply by the namespace hash format, and are correctly ordered
// according to their namespace IDs.
func (n *NmtHasher) ValidateNodes(left, right []byte) error {
_, _, err := n.tryFetchLeftAndRightNSRanges(left, right)
return err
}
// HashNode calculates a namespaced hash of a node using the supplied left and
// right children. The input values, `left` and `right,` are namespaced hash
// values with the format `minNID || maxNID || hash.`
// The HashNode function returns an error if the provided inputs are invalid. Specifically, it returns the ErrInvalidNodeLen error if the left and right inputs are not in the namespaced hash format,
// and the ErrUnorderedSiblings error if left.maxNID is greater than right.minNID.
// By default, the normal namespace hash calculation is
// followed, which is `res = min(left.minNID, right.minNID) || max(left.maxNID,
// right.maxNID) || H(NodePrefix, left, right)`. `res` refers to the return
// value of the HashNode. However, if the `ignoreMaxNs` property of the Hasher
// is set to true, the calculation of the namespace ID range of the node
// slightly changes. Let MAXNID be the maximum possible namespace ID value i.e., 2^NamespaceIDSize-1.
// If the namespace range of the right child is start=end=MAXNID, indicating that it represents the root of a subtree whose leaves all have the namespace ID of `MAXNID`, then exclude the right child from the namespace range calculation. Instead,
// assign the namespace range of the left child as the parent's namespace range.
func (n *NmtHasher) HashNode(left, right []byte) ([]byte, error) {
// validate the inputs & fetch the namespace ranges
lRange, rRange, err := n.tryFetchLeftAndRightNSRanges(left, right)
if err != nil {
return nil, err
}
h := n.baseHasher
h.Reset()
// compute the namespace range of the parent node
minNs, maxNs := computeNsRange(lRange.Min, lRange.Max, rRange.Min, rRange.Max, n.ignoreMaxNs, n.precomputedMaxNs)
res := make([]byte, 0, len(minNs)+len(maxNs)+h.Size())
res = append(res, minNs...)
res = append(res, maxNs...)
h.Write([]byte{NodePrefix})
h.Write(left)
h.Write(right)
return h.Sum(res), nil
}
// computeNsRange computes the namespace range of the parent node based on the namespace ranges of its left and right children.
func computeNsRange(leftMinNs, leftMaxNs, rightMinNs, rightMaxNs []byte, ignoreMaxNs bool, precomputedMaxNs namespace.ID) (minNs []byte, maxNs []byte) {
minNs = leftMinNs
maxNs = rightMaxNs
if ignoreMaxNs && bytes.Equal(precomputedMaxNs, rightMinNs) {
maxNs = leftMaxNs
}
return minNs, maxNs
}