@@ -154,10 +154,12 @@ func (n *NmtHasher) BlockSize() int {
154
154
155
155
func (n * NmtHasher ) EmptyRoot () []byte {
156
156
n .baseHasher .Reset ()
157
- h := n .baseHasher .Sum (nil )
158
- digest := append (make ([]byte , int (n .NamespaceLen )* 2 ), h ... )
157
+ // make returns a zeroed slice, exactly what we need for the (nID || nID)
158
+ zeroSize := int (n .NamespaceLen ) * 2
159
+ fullSize := zeroSize + n .baseHasher .Size ()
159
160
160
- return digest
161
+ digest := make ([]byte , zeroSize , fullSize )
162
+ return n .baseHasher .Sum (digest )
161
163
}
162
164
163
165
// ValidateLeaf verifies if data is namespaced and returns an error if not.
@@ -174,8 +176,6 @@ func (n *NmtHasher) ValidateLeaf(data []byte) (err error) {
174
176
// ns(ndata) || ns(ndata) || hash(leafPrefix || ndata), where ns(ndata) is the
175
177
// namespaceID inside the data item namely leaf[:n.NamespaceLen]). Note that for
176
178
// leaves minNs = maxNs = ns(leaf) = leaf[:NamespaceLen]. HashLeaf can return the ErrInvalidNodeLen error if the input is not namespaced.
177
- //
178
- //nolint:errcheck
179
179
func (n * NmtHasher ) HashLeaf (ndata []byte ) ([]byte , error ) {
180
180
h := n .baseHasher
181
181
h .Reset ()
@@ -190,11 +190,8 @@ func (n *NmtHasher) HashLeaf(ndata []byte) ([]byte, error) {
190
190
minMaxNIDs = append (minMaxNIDs , nID ... ) // nID
191
191
minMaxNIDs = append (minMaxNIDs , nID ... ) // nID || nID
192
192
193
- // add LeafPrefix to the ndata
194
- leafPrefixedNData := make ([]byte , 0 , len (ndata )+ 1 )
195
- leafPrefixedNData = append (leafPrefixedNData , LeafPrefix )
196
- leafPrefixedNData = append (leafPrefixedNData , ndata ... )
197
- h .Write (leafPrefixedNData )
193
+ h .Write ([]byte {LeafPrefix })
194
+ h .Write (ndata )
198
195
199
196
// compute h(LeafPrefix || ndata) and append it to the minMaxNIDs
200
197
nameSpacedHash := h .Sum (minMaxNIDs ) // nID || nID || h(LeafPrefix || ndata)
@@ -306,19 +303,13 @@ func (n *NmtHasher) HashNode(left, right []byte) ([]byte, error) {
306
303
// compute the namespace range of the parent node
307
304
minNs , maxNs := computeNsRange (lRange .Min , lRange .Max , rRange .Min , rRange .Max , n .ignoreMaxNs , n .precomputedMaxNs )
308
305
309
- res := make ([]byte , 0 , len (minNs )* 2 )
306
+ res := make ([]byte , 0 , len (minNs )+ len ( maxNs ) + h . Size () )
310
307
res = append (res , minNs ... )
311
308
res = append (res , maxNs ... )
312
309
313
- // Note this seems a little faster than calling several Write()s on the
314
- // underlying Hash function (see:
315
- // https://github.com/google/trillian/pull/1503):
316
- data := make ([]byte , 0 , 1 + len (left )+ len (right ))
317
- data = append (data , NodePrefix )
318
- data = append (data , left ... )
319
- data = append (data , right ... )
320
- //nolint:errcheck
321
- h .Write (data )
310
+ h .Write ([]byte {NodePrefix })
311
+ h .Write (left )
312
+ h .Write (right )
322
313
return h .Sum (res ), nil
323
314
}
324
315
0 commit comments