From 8935a48fe53526b9d55e843237510d11bc4c6c83 Mon Sep 17 00:00:00 2001 From: Philippe Boneff Date: Tue, 9 Jun 2026 10:58:09 +0000 Subject: [PATCH] use range where possible --- cmd/proofgen/main.go | 4 ++-- compact/node_fuzz_test.go | 4 +--- compact/nodes_test.go | 2 +- compact/range_test.go | 32 ++++++++++++++++---------------- proof/proof_test.go | 2 +- rfc6962/rfc6962_test.go | 2 +- testonly/tree_fuzz_test.go | 24 +++++++++++------------- testonly/tree_test.go | 15 ++++++++------- 8 files changed, 41 insertions(+), 44 deletions(-) diff --git a/cmd/proofgen/main.go b/cmd/proofgen/main.go index f3ebc8c..f607a71 100644 --- a/cmd/proofgen/main.go +++ b/cmd/proofgen/main.go @@ -178,7 +178,7 @@ func invalidInclusionProof(leafIdx, treeSize uint64, proof [][]byte, root, leafH ln := len(proof) // Modify single bit in an element of the proof. - for i := 0; i < ln; i++ { + for i := range ln { wrongProof := prepend(proof) // Copy the proof slice. wrongProof[i] = append([]byte(nil), wrongProof[i]...) // But also the modified data. wrongProof[i][0] ^= 8 // Flip the bit. @@ -360,7 +360,7 @@ func invalidConsistencyProof(size1, size2 uint64, root1, root2 []byte, proof [][ } // Modify single bit in an element of the proof. - for i := 0; i < ln; i++ { + for i := range ln { wrongProof := prepend(proof) // Copy the proof slice. wrongProof[i] = append([]byte(nil), wrongProof[i]...) // But also the modified data. wrongProof[i][0] ^= 16 // Flip the bit. diff --git a/compact/node_fuzz_test.go b/compact/node_fuzz_test.go index ce2a7fe..2417bd1 100644 --- a/compact/node_fuzz_test.go +++ b/compact/node_fuzz_test.go @@ -1,5 +1,3 @@ -//go:build go1.18 - package compact import ( @@ -9,7 +7,7 @@ import ( // Test that RangeNodes returns a slice of nodes with contiguous coverage. // https://github.com/transparency-dev/merkle/blob/main/docs/compact_ranges.md#definition func FuzzRangeNodes(f *testing.F) { - for begin := 0; begin <= 10; begin++ { + for begin := range 10 + 1 { for end := begin; end <= 20; end++ { f.Add(uint64(end), uint64(end)) } diff --git a/compact/nodes_test.go b/compact/nodes_test.go index c9981ef..8139571 100644 --- a/compact/nodes_test.go +++ b/compact/nodes_test.go @@ -93,7 +93,7 @@ func TestRangeNodesAppend(t *testing.T) { func TestGenRangeNodes(t *testing.T) { const size = uint64(512) - for begin := uint64(0); begin <= size; begin++ { + for begin := range size + 1 { for end := begin; end <= size; end++ { got := RangeNodes(begin, end, nil) want := refRangeNodes(NewNodeID(63, 0), begin, end) diff --git a/compact/range_test.go b/compact/range_test.go index de58ef2..e6d0c8c 100644 --- a/compact/range_test.go +++ b/compact/range_test.go @@ -66,7 +66,7 @@ func newTree(t *testing.T, size uint64) (*tree, compact.VisitFn) { nodes[lvl] = make([]treeNode, size>>uint(lvl)) } // Compute leaf hashes. - for i := uint64(0); i < size; i++ { + for i := range size { nodes[0][i].hash = hashLeaf(leafData(i)) } // Compute internal node hashes. @@ -121,7 +121,7 @@ func (tr *tree) verifyRange(t *testing.T, r *compact.Range, wantMatch bool) { // Naively build the expected list of hashes comprising the compact range. left, right := compact.Decompose(pos, r.End()) var hashes [][]byte - for lvl := uint(0); lvl < 64; lvl++ { + for lvl := range uint(64) { if left&(1<>lvl].hash) pos += 1 << lvl @@ -162,7 +162,7 @@ func (tr *tree) verifyAllVisited(t *testing.T, r *compact.Range) { func TestAppend(t *testing.T) { var sizes []uint64 - for size := uint64(0); size <= 256; size++ { + for size := range uint64(256) + 1 { sizes = append(sizes, size) } sizes = append(sizes, 555, 1040, 5431) @@ -172,7 +172,7 @@ func TestAppend(t *testing.T) { tree, visit := newTree(t, size) cr := factory.NewEmptyRange(0) tree.verifyRange(t, cr, true) - for i := uint64(0); i < size; i++ { + for i := range size { if err := cr.Append(tree.leaf(i), visit); err != nil { t.Errorf("Append()=%v", err) } @@ -188,10 +188,10 @@ func TestGoldenRanges(t *testing.T) { roots := testonly.RootHashes() hashes := testonly.CompactTrees() - for size, ln := 0, len(inputs); size <= ln; size++ { + for size := range len(inputs) + 1 { t.Run(fmt.Sprintf("size:%d", size), func(t *testing.T) { cr := factory.NewEmptyRange(0) - for i := 0; i < size; i++ { + for i := range size { if err := cr.Append(hashLeaf(inputs[i]), nil); err != nil { t.Fatalf("Append: %v", err) } @@ -308,7 +308,7 @@ func TestNewRange(t *testing.T) { const numNodes = uint64(123) tree, visit := newTree(t, numNodes) rng := factory.NewEmptyRange(0) - for i := uint64(0); i < numNodes; i++ { + for i := range numNodes { if err := rng.Append(tree.leaf(i), visit); err != nil { t.Errorf("Append()=%v", err) } @@ -358,7 +358,7 @@ func TestNewRangeWithStorage(t *testing.T) { } cr := factory.NewEmptyRange(0) - for i := uint64(0); i < numNodes; i++ { + for i := range numNodes { nodes[compact.NewNodeID(0, i)] = tree.leaf(i) if err := cr.Append(tree.leaf(i), func(id compact.NodeID, hash []byte) { nodes[id] = hash @@ -382,11 +382,11 @@ func TestNewRangeWithStorage(t *testing.T) { } func TestGetRootHash(t *testing.T) { - for size := uint64(0); size < 16; size++ { + for size := range uint64(16) { t.Run(fmt.Sprintf("size:%d", size), func(t *testing.T) { tree, _ := newTree(t, size) rng := factory.NewEmptyRange(0) - for i := uint64(0); i < size; i++ { + for i := range size { if err := rng.Append(tree.leaf(i), nil); err != nil { t.Errorf("Append=%v", err) } @@ -468,7 +468,7 @@ func TestGetRootHashGolden(t *testing.T) { } { t.Run(fmt.Sprintf("size:%v", tc.size), func(t *testing.T) { rng := factory.NewEmptyRange(0) - for i := 0; i < tc.size; i++ { + for i := range tc.size { data := []byte{byte(i & 0xff), byte((i >> 8) & 0xff)} hash := hashLeaf(data) if err := rng.Append(hash, nil); err != nil { @@ -528,7 +528,7 @@ func verifyDecompose(begin, end uint64) error { } pos := begin - for lvl := uint(0); lvl < 64; lvl++ { + for lvl := range uint(64) { if size := uint64(1) << lvl; left&size != 0 { if pos%size != 0 { return fmt.Errorf("left: level %d not aligned", lvl) @@ -552,7 +552,7 @@ func verifyDecompose(begin, end uint64) error { func TestDecompose(t *testing.T) { const n = uint64(100) - for i := uint64(0); i <= n; i++ { + for i := range n + 1 { for j := i; j <= n; j++ { if err := verifyDecompose(i, j); err != nil { t.Fatalf("verifyDecompose(%d,%d): %v", i, j, err) @@ -562,7 +562,7 @@ func TestDecompose(t *testing.T) { } func TestDecomposePow2(t *testing.T) { - for p := 0; p < 64; p++ { + for p := range 64 { t.Run(fmt.Sprintf("2^%d", p), func(t *testing.T) { end := uint64(1) << uint(p) if err := verifyDecompose(0, end); err != nil { @@ -578,9 +578,9 @@ func TestDecomposePow2(t *testing.T) { func BenchmarkAppend(b *testing.B) { const size = 1024 - for n := 0; n < b.N; n++ { + for range b.N { cr := factory.NewEmptyRange(0) - for i := 0; i < size; i++ { + for i := range size { l := []byte{byte(i & 0xff), byte((i >> 8) & 0xff)} hash := hashLeaf(l) if err := cr.Append(hash, nil); err != nil { diff --git a/proof/proof_test.go b/proof/proof_test.go index d063b6b..2984a70 100644 --- a/proof/proof_test.go +++ b/proof/proof_test.go @@ -424,7 +424,7 @@ func TestInclusionSucceedsUpToTreeSize(t *testing.T) { func TestInclusionSubtreeSucceedsUpToTreeSize(t *testing.T) { const maxSize = uint64(555) for sbe := uint64(1); sbe <= maxSize; sbe++ { - for sbs := uint64(0); sbs < sbe; sbs++ { + for sbs := range sbe { if err := isSubtreeValid(sbs, sbe); err != nil { continue } diff --git a/rfc6962/rfc6962_test.go b/rfc6962/rfc6962_test.go index f7aa721..015983c 100644 --- a/rfc6962/rfc6962_test.go +++ b/rfc6962/rfc6962_test.go @@ -101,7 +101,7 @@ func BenchmarkHashChildren(b *testing.B) { h := DefaultHasher l := h.HashLeaf([]byte("one")) r := h.HashLeaf([]byte("or other")) - for i := 0; i < b.N; i++ { + for range b.N { _ = h.HashChildren(l, r) } } diff --git a/testonly/tree_fuzz_test.go b/testonly/tree_fuzz_test.go index 4428bf1..92edfd6 100644 --- a/testonly/tree_fuzz_test.go +++ b/testonly/tree_fuzz_test.go @@ -1,5 +1,3 @@ -//go:build go1.18 - package testonly import ( @@ -49,8 +47,8 @@ func FuzzConsistencyProofAndVerify(f *testing.F) { // Compute and verify inclusion proofs func FuzzInclusionProofAndVerify(f *testing.F) { - for size := 0; size <= 8; size++ { - for index := 0; index <= size; index++ { + for size := range 8 + 1 { + for index := range size + 1 { f.Add(uint64(index), uint64(size)) } } @@ -77,8 +75,8 @@ func FuzzInclusionProofAndVerify(f *testing.F) { // Compute and verify inclusion proofs func FuzzSubtreeInclusionProofAndVerify(f *testing.F) { - for end := 0; end <= 8; end++ { - for start := 0; start <= end; start++ { + for end := range 8 + 1 { + for start := range end + 1 { for index := start; index <= end; index++ { f.Add(uint64(index), uint64(start), uint64(end)) } @@ -115,8 +113,8 @@ func FuzzSubtreeInclusionProofAndVerify(f *testing.F) { } func FuzzHashAtAgainstReferenceImplementation(f *testing.F) { - for size := 0; size <= 8; size++ { - for index := 0; index <= size; index++ { + for size := range 8 + 1 { + for index := range size + 1 { f.Add(uint64(index), uint64(size)) } } @@ -139,8 +137,8 @@ func FuzzHashAtAgainstReferenceImplementation(f *testing.F) { } func FuzzInclusionProofAgainstReferenceImplementation(f *testing.F) { - for size := 0; size <= 8; size++ { - for index := 0; index <= size; index++ { + for size := range 8 + 1 { + for index := range size + 1 { f.Add(uint64(index), uint64(size)) } } @@ -167,9 +165,9 @@ func FuzzInclusionProofAgainstReferenceImplementation(f *testing.F) { } func FuzzConsistencyProofAgainstReferenceImplementation(f *testing.F) { - for size := 0; size <= 8; size++ { - for end := 0; end <= size; end++ { - for begin := 0; begin <= end; begin++ { + for size := range 8 + 1 { + for end := range size + 1 { + for begin := range end + 1 { f.Add(uint64(size), uint64(begin), uint64(end)) } } diff --git a/testonly/tree_test.go b/testonly/tree_test.go index 6a73261..bfa669b 100644 --- a/testonly/tree_test.go +++ b/testonly/tree_test.go @@ -35,7 +35,7 @@ func validateTree(t *testing.T, mt *Tree, size uint64) { if got, want := mt.Hash(), roots[size]; !bytes.Equal(got, want) { t.Errorf("Hash(%d): %x, want %x", size, got, want) } - for s := uint64(0); s <= size; s++ { + for s := range size + 1 { if got, want := mt.HashAt(s), roots[s]; !bytes.Equal(got, want) { t.Errorf("HashAt(%d/%d): %x, want %x", s, size, got, want) } @@ -70,7 +70,7 @@ func TestTreeHashAt(t *testing.T) { test := func(desc string, entries [][]byte) { t.Run(desc, func(t *testing.T) { mt := newTree(entries) - for size := 0; size <= len(entries); size++ { + for size := range len(entries) + 1 { got := mt.HashAt(uint64(size)) want := refRootHash(entries[:size], mt.hasher) if !bytes.Equal(got, want) { @@ -81,7 +81,7 @@ func TestTreeHashAt(t *testing.T) { } entries := LeafInputs() - for size := 0; size <= len(entries); size++ { + for size := range len(entries) + 1 { test(fmt.Sprintf("size:%d", size), entries[:size]) } test("generated", genEntries(256)) @@ -91,7 +91,8 @@ func TestTreeInclusionProof(t *testing.T) { test := func(desc string, entries [][]byte) { t.Run(desc, func(t *testing.T) { mt := newTree(entries) - for index, size := uint64(0), uint64(len(entries)); index < size; index++ { + size := uint64(len(entries)) + for index := range size { got, err := mt.InclusionProof(index, size) if err != nil { t.Fatalf("InclusionProof(%d, %d): %v", index, size, err) @@ -106,7 +107,7 @@ func TestTreeInclusionProof(t *testing.T) { test("generated", genEntries(256)) entries := LeafInputs() - for size := 0; size < len(entries); size++ { + for size := range len(entries) { test(fmt.Sprintf("golden:%d", size), entries[:size]) } } @@ -120,7 +121,7 @@ func TestTreeConsistencyProof(t *testing.T) { t.Error("ConsistencyProof(6, 3) succeeded unexpectedly") } - for size1 := uint64(0); size1 <= 8; size1++ { + for size1 := range uint64(8) + 1 { for size2 := size1; size2 <= 8; size2++ { t.Run(fmt.Sprintf("%d:%d", size1, size2), func(t *testing.T) { got, err := mt.ConsistencyProof(size1, size2) @@ -142,7 +143,7 @@ func TestTreeConsistencyProofFuzz(t *testing.T) { for treeSize := uint64(1); treeSize <= 256; treeSize++ { mt := newTree(entries[:treeSize]) - for i := 0; i < 8; i++ { + for range 8 { size2 := rand.Uint64N(treeSize + 1) size1 := rand.Uint64N(size2 + 1)