skip leading zeros when computing root

This commit is contained in:
lukechampine 2020-02-02 16:40:32 -05:00
parent 617550f073
commit 6c1c802729
1 changed files with 3 additions and 3 deletions

View File

@ -298,14 +298,14 @@ type Hasher struct {
used uint64 // bit vector indicating which stack elems are valid; also number of chunks added used uint64 // bit vector indicating which stack elems are valid; also number of chunks added
} }
func (h *Hasher) hasSubtreeAtHeight(i uint64) bool { func (h *Hasher) hasSubtreeAtHeight(i int) bool {
return h.used&(1<<i) != 0 return h.used&(1<<i) != 0
} }
// addChunkChainingValue appends a chunk to the right edge of the Merkle tree. // addChunkChainingValue appends a chunk to the right edge of the Merkle tree.
func (h *Hasher) addChunkChainingValue(cv [8]uint32) { func (h *Hasher) addChunkChainingValue(cv [8]uint32) {
// seek to first open stack slot, merging subtrees as we go // seek to first open stack slot, merging subtrees as we go
i := uint64(0) i := 0
for ; h.hasSubtreeAtHeight(i); i++ { for ; h.hasSubtreeAtHeight(i); i++ {
cv = parentNode(h.stack[i], cv, h.key, h.flags).chainingValue() cv = parentNode(h.stack[i], cv, h.key, h.flags).chainingValue()
} }
@ -317,7 +317,7 @@ func (h *Hasher) addChunkChainingValue(cv [8]uint32) {
// chainStack. // chainStack.
func (h *Hasher) rootNode() node { func (h *Hasher) rootNode() node {
n := h.cs.node() n := h.cs.node()
for i := uint64(bits.TrailingZeros64(h.used)); i < 64; i++ { for i := bits.TrailingZeros64(h.used); i < bits.Len64(h.used); i++ {
if h.hasSubtreeAtHeight(i) { if h.hasSubtreeAtHeight(i) {
n = parentNode(h.stack[i], n.chainingValue(), h.key, h.flags) n = parentNode(h.stack[i], n.chainingValue(), h.key, h.flags)
} }