feat(subtree): RootHashPadded accepts non-power-of-two leaf counts#127
Merged
Conversation
The pow2 precondition on RootHashPadded was overly strict. The natural
root produced by BuildMerkleTreeStoreFromBytes already lives at height
ceil(log2(length)) for any length >= 1 because the duplicate-when-odd
rule is applied at every internal level. Self-hashing from there up to
targetHeight composes correctly with the canonical flat merkle tree.
- replace int(math.Log2(float64(length))) with bits.Len(uint(length-1))
so the height calculation is exact for non-power-of-two lengths
- drop the IsPowerOfTwo guard and the now-unused
ErrNotPowerOfTwoLeafCount sentinel
- convert the prior error-path test into an acceptance test plus a
sweep over r in {1,2,3,5,6,7,9,11,13,15,17,21,23,27,31} that proves
equivalence with the canonical flat root
CI's gosec doesn't flag the uint(length-1) conversion because length >= 1 is provable from the preceding early return. nolintlint then reports the //nolint directive as unused. Drop the annotation — the conversion is safe and CI confirms it doesn't need suppression.
|
ordishs
added a commit
to ordishs/teranode
that referenced
this pull request
May 20, 2026
go-subtree v1.4.2 relaxes RootHashPadded to accept non-power-of-two leaf counts (bsv-blockchain/go-subtree#127), making the local liftSubtreeRootToTargetHeight / liftRootForTest helpers redundant. - bump github.com/bsv-blockchain/go-subtree from v1.4.1 to v1.4.2 - delete liftSubtreeRootToTargetHeight in model/Block.go; CheckMerkleRoot calls last.RootHashPadded(targetHeight) directly - delete liftRootForTest in util/subtree_merkle_root_equivalence_test.go; the test calls right.RootHashPadded(left.Height) directly - drop the now-unused crypto/sha256 and math/bits imports in both files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
The pow2 precondition on
RootHashPaddedwas overly strict. The natural root produced byBuildMerkleTreeStoreFromBytesalready lives at heightceil(log2(length))for anylength >= 1because the duplicate-when-odd rule is applied at every internal level. Self-hashing from there up totargetHeightcomposes correctly with the canonical flat merkle tree.Changes
int(math.Log2(float64(length)))withbits.Len(uint(length-1))so the height calculation is exact for non-power-of-two lengths.IsPowerOfTwoguard and the now-unusedErrNotPowerOfTwoLeafCountsentinel from the public API.TestRootHashPadded_NotPowerOfTwoErrors) into an acceptance test that asserts the lifted root matches both the natural root (whentargetHeight == ceil(log2(length))) and the expected self-hashed value one level above.TestRootHashPadded_MatchesFlatTreeForNonPowerOfTwoFinal, a sweep acrossr ∈ {1,2,3,5,6,7,9,11,13,15,17,21,23,27,31}that proves the composed root (complete left + lifted right) equals the canonical flat merkle root for each remainder size.Motivation
This enables teranode PR #909 to delete duplicate lift-helper code (~40 lines across two files) and call
RootHashPaddeddirectly for the legacy-block partitioner's final subtree of arbitrary length.Breaking change?
ErrNotPowerOfTwoLeafCountis removed from the public API. A grep across this repo and teranode shows no other callers; the only test using it has been converted. External consumers (if any) catching this sentinel would need to drop that branch.Test plan
go test -race ./...(all green, including the new sweep test)go vet ./...golangci-lint run ./...(no new issues introduced)