From 8dd3dfe522bf1b5f2042e941ffe7b4301b6ccfd6 Mon Sep 17 00:00:00 2001 From: John Adler Date: Tue, 8 Jun 2021 09:30:17 -0400 Subject: [PATCH] Move loop-invariant offset calculation outside loop. (#35) --- smt.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/smt.go b/smt.go index 8692bc0..26ea6a1 100644 --- a/smt.go +++ b/smt.go @@ -281,13 +281,13 @@ func (smt *SparseMerkleTree) updateWithSideNodes(path []byte, value []byte, side currentData = currentHash } + // The offset from the bottom of the tree to the start of the side nodes. + // Note: i-offsetOfSideNodes is the index into sideNodes[] + offsetOfSideNodes := smt.depth() - len(sideNodes) + for i := 0; i < smt.depth(); i++ { sideNode := make([]byte, smt.th.pathSize()) - // The offset from the bottom of the tree to the start of the side nodes - // i-offsetOfSideNodes is the index into sideNodes[] - offsetOfSideNodes := smt.depth() - len(sideNodes) - if i-offsetOfSideNodes < 0 || sideNodes[i-offsetOfSideNodes] == nil { if commonPrefixCount != smt.depth() && commonPrefixCount > smt.depth()-1-i { // If there are no sidenodes at this height, but the number of