Skip to content

Commit e3ff8bb

Browse files
committed
factor out isValidSubtree
1 parent 4443702 commit e3ff8bb

2 files changed

Lines changed: 6 additions & 11 deletions

File tree

proof/proof.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ func SubtreeInclusion(index, start, end uint64) (Nodes, error) {
6565
if index < start || index >= end {
6666
return Nodes{}, fmt.Errorf("index %d out of bounds for subtree [%d, %d)", index, start, end)
6767
}
68-
if err := checkSubtreeAlignment(start, end); err != nil {
69-
return Nodes{}, err
68+
if !isValidSubtree(start, end) {
69+
return Nodes{}, fmt.Errorf("start %d not a multiple of bit_ceil(end - start) = %d", start, end-start)
7070
}
7171

7272
// Shift the subtree to the left, such that it starts at 0.
@@ -219,14 +219,10 @@ func reverse(ids []compact.NodeID) {
219219
}
220220
}
221221

222-
func checkSubtreeAlignment(start, end uint64) error {
222+
func isValidSubtree(start, end uint64) bool {
223223
shift := bits.Len64(end - start - 1)
224224
if shift >= 64 {
225-
if start != 0 {
226-
return fmt.Errorf("start %d not a multiple of bit_ceil(end - start)", start)
227-
}
228-
} else if bc := uint64(1) << shift; start%bc != 0 {
229-
return fmt.Errorf("start %d not a multiple of bit_ceil(end - start) = %d", start, bc)
225+
return start == 0
230226
}
231-
return nil
227+
return start%(uint64(1)<<shift) == 0
232228
}

proof/proof_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package proof
1616

1717
import (
1818
"fmt"
19-
"math/bits"
2019
"testing"
2120

2221
"github.com/google/go-cmp/cmp"
@@ -426,7 +425,7 @@ func TestInclusionSubtreeSucceedsUpToTreeSize(t *testing.T) {
426425
const maxSize = uint64(555)
427426
for sbe := uint64(1); sbe <= maxSize; sbe++ {
428427
for sbs := uint64(0); sbs < sbe; sbs++ {
429-
if bc := uint64(1) << bits.Len64(sbe-sbs-1); sbs%bc != 0 {
428+
if !isValidSubtree(sbs, sbe) {
430429
continue
431430
}
432431
for i := sbs; i < sbe; i++ {

0 commit comments

Comments
 (0)