Skip to content

Commit 4098e0c

Browse files
authored
use range where possible (transparency-dev#231)
1 parent ededf79 commit 4098e0c

8 files changed

Lines changed: 41 additions & 44 deletions

File tree

cmd/proofgen/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func invalidInclusionProof(leafIdx, treeSize uint64, proof [][]byte, root, leafH
178178
ln := len(proof)
179179

180180
// Modify single bit in an element of the proof.
181-
for i := 0; i < ln; i++ {
181+
for i := range ln {
182182
wrongProof := prepend(proof) // Copy the proof slice.
183183
wrongProof[i] = append([]byte(nil), wrongProof[i]...) // But also the modified data.
184184
wrongProof[i][0] ^= 8 // Flip the bit.
@@ -360,7 +360,7 @@ func invalidConsistencyProof(size1, size2 uint64, root1, root2 []byte, proof [][
360360
}
361361

362362
// Modify single bit in an element of the proof.
363-
for i := 0; i < ln; i++ {
363+
for i := range ln {
364364
wrongProof := prepend(proof) // Copy the proof slice.
365365
wrongProof[i] = append([]byte(nil), wrongProof[i]...) // But also the modified data.
366366
wrongProof[i][0] ^= 16 // Flip the bit.

compact/node_fuzz_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//go:build go1.18
2-
31
package compact
42

53
import (
@@ -9,7 +7,7 @@ import (
97
// Test that RangeNodes returns a slice of nodes with contiguous coverage.
108
// https://github.com/transparency-dev/merkle/blob/main/docs/compact_ranges.md#definition
119
func FuzzRangeNodes(f *testing.F) {
12-
for begin := 0; begin <= 10; begin++ {
10+
for begin := range 10 + 1 {
1311
for end := begin; end <= 20; end++ {
1412
f.Add(uint64(end), uint64(end))
1513
}

compact/nodes_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func TestRangeNodesAppend(t *testing.T) {
9393

9494
func TestGenRangeNodes(t *testing.T) {
9595
const size = uint64(512)
96-
for begin := uint64(0); begin <= size; begin++ {
96+
for begin := range size + 1 {
9797
for end := begin; end <= size; end++ {
9898
got := RangeNodes(begin, end, nil)
9999
want := refRangeNodes(NewNodeID(63, 0), begin, end)

compact/range_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func newTree(t *testing.T, size uint64) (*tree, compact.VisitFn) {
6666
nodes[lvl] = make([]treeNode, size>>uint(lvl))
6767
}
6868
// Compute leaf hashes.
69-
for i := uint64(0); i < size; i++ {
69+
for i := range size {
7070
nodes[0][i].hash = hashLeaf(leafData(i))
7171
}
7272
// Compute internal node hashes.
@@ -121,7 +121,7 @@ func (tr *tree) verifyRange(t *testing.T, r *compact.Range, wantMatch bool) {
121121
// Naively build the expected list of hashes comprising the compact range.
122122
left, right := compact.Decompose(pos, r.End())
123123
var hashes [][]byte
124-
for lvl := uint(0); lvl < 64; lvl++ {
124+
for lvl := range uint(64) {
125125
if left&(1<<lvl) != 0 {
126126
hashes = append(hashes, tr.nodes[lvl][pos>>lvl].hash)
127127
pos += 1 << lvl
@@ -162,7 +162,7 @@ func (tr *tree) verifyAllVisited(t *testing.T, r *compact.Range) {
162162

163163
func TestAppend(t *testing.T) {
164164
var sizes []uint64
165-
for size := uint64(0); size <= 256; size++ {
165+
for size := range uint64(256) + 1 {
166166
sizes = append(sizes, size)
167167
}
168168
sizes = append(sizes, 555, 1040, 5431)
@@ -172,7 +172,7 @@ func TestAppend(t *testing.T) {
172172
tree, visit := newTree(t, size)
173173
cr := factory.NewEmptyRange(0)
174174
tree.verifyRange(t, cr, true)
175-
for i := uint64(0); i < size; i++ {
175+
for i := range size {
176176
if err := cr.Append(tree.leaf(i), visit); err != nil {
177177
t.Errorf("Append()=%v", err)
178178
}
@@ -188,10 +188,10 @@ func TestGoldenRanges(t *testing.T) {
188188
roots := testonly.RootHashes()
189189
hashes := testonly.CompactTrees()
190190

191-
for size, ln := 0, len(inputs); size <= ln; size++ {
191+
for size := range len(inputs) + 1 {
192192
t.Run(fmt.Sprintf("size:%d", size), func(t *testing.T) {
193193
cr := factory.NewEmptyRange(0)
194-
for i := 0; i < size; i++ {
194+
for i := range size {
195195
if err := cr.Append(hashLeaf(inputs[i]), nil); err != nil {
196196
t.Fatalf("Append: %v", err)
197197
}
@@ -308,7 +308,7 @@ func TestNewRange(t *testing.T) {
308308
const numNodes = uint64(123)
309309
tree, visit := newTree(t, numNodes)
310310
rng := factory.NewEmptyRange(0)
311-
for i := uint64(0); i < numNodes; i++ {
311+
for i := range numNodes {
312312
if err := rng.Append(tree.leaf(i), visit); err != nil {
313313
t.Errorf("Append()=%v", err)
314314
}
@@ -358,7 +358,7 @@ func TestNewRangeWithStorage(t *testing.T) {
358358
}
359359

360360
cr := factory.NewEmptyRange(0)
361-
for i := uint64(0); i < numNodes; i++ {
361+
for i := range numNodes {
362362
nodes[compact.NewNodeID(0, i)] = tree.leaf(i)
363363
if err := cr.Append(tree.leaf(i), func(id compact.NodeID, hash []byte) {
364364
nodes[id] = hash
@@ -382,11 +382,11 @@ func TestNewRangeWithStorage(t *testing.T) {
382382
}
383383

384384
func TestGetRootHash(t *testing.T) {
385-
for size := uint64(0); size < 16; size++ {
385+
for size := range uint64(16) {
386386
t.Run(fmt.Sprintf("size:%d", size), func(t *testing.T) {
387387
tree, _ := newTree(t, size)
388388
rng := factory.NewEmptyRange(0)
389-
for i := uint64(0); i < size; i++ {
389+
for i := range size {
390390
if err := rng.Append(tree.leaf(i), nil); err != nil {
391391
t.Errorf("Append=%v", err)
392392
}
@@ -468,7 +468,7 @@ func TestGetRootHashGolden(t *testing.T) {
468468
} {
469469
t.Run(fmt.Sprintf("size:%v", tc.size), func(t *testing.T) {
470470
rng := factory.NewEmptyRange(0)
471-
for i := 0; i < tc.size; i++ {
471+
for i := range tc.size {
472472
data := []byte{byte(i & 0xff), byte((i >> 8) & 0xff)}
473473
hash := hashLeaf(data)
474474
if err := rng.Append(hash, nil); err != nil {
@@ -528,7 +528,7 @@ func verifyDecompose(begin, end uint64) error {
528528
}
529529

530530
pos := begin
531-
for lvl := uint(0); lvl < 64; lvl++ {
531+
for lvl := range uint(64) {
532532
if size := uint64(1) << lvl; left&size != 0 {
533533
if pos%size != 0 {
534534
return fmt.Errorf("left: level %d not aligned", lvl)
@@ -552,7 +552,7 @@ func verifyDecompose(begin, end uint64) error {
552552

553553
func TestDecompose(t *testing.T) {
554554
const n = uint64(100)
555-
for i := uint64(0); i <= n; i++ {
555+
for i := range n + 1 {
556556
for j := i; j <= n; j++ {
557557
if err := verifyDecompose(i, j); err != nil {
558558
t.Fatalf("verifyDecompose(%d,%d): %v", i, j, err)
@@ -562,7 +562,7 @@ func TestDecompose(t *testing.T) {
562562
}
563563

564564
func TestDecomposePow2(t *testing.T) {
565-
for p := 0; p < 64; p++ {
565+
for p := range 64 {
566566
t.Run(fmt.Sprintf("2^%d", p), func(t *testing.T) {
567567
end := uint64(1) << uint(p)
568568
if err := verifyDecompose(0, end); err != nil {
@@ -578,9 +578,9 @@ func TestDecomposePow2(t *testing.T) {
578578

579579
func BenchmarkAppend(b *testing.B) {
580580
const size = 1024
581-
for n := 0; n < b.N; n++ {
581+
for range b.N {
582582
cr := factory.NewEmptyRange(0)
583-
for i := 0; i < size; i++ {
583+
for i := range size {
584584
l := []byte{byte(i & 0xff), byte((i >> 8) & 0xff)}
585585
hash := hashLeaf(l)
586586
if err := cr.Append(hash, nil); err != nil {

proof/proof_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ func TestInclusionSucceedsUpToTreeSize(t *testing.T) {
424424
func TestInclusionSubtreeSucceedsUpToTreeSize(t *testing.T) {
425425
const maxSize = uint64(555)
426426
for sbe := uint64(1); sbe <= maxSize; sbe++ {
427-
for sbs := uint64(0); sbs < sbe; sbs++ {
427+
for sbs := range sbe {
428428
if err := isSubtreeValid(sbs, sbe); err != nil {
429429
continue
430430
}

rfc6962/rfc6962_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func BenchmarkHashChildren(b *testing.B) {
101101
h := DefaultHasher
102102
l := h.HashLeaf([]byte("one"))
103103
r := h.HashLeaf([]byte("or other"))
104-
for i := 0; i < b.N; i++ {
104+
for range b.N {
105105
_ = h.HashChildren(l, r)
106106
}
107107
}

testonly/tree_fuzz_test.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//go:build go1.18
2-
31
package testonly
42

53
import (
@@ -49,8 +47,8 @@ func FuzzConsistencyProofAndVerify(f *testing.F) {
4947

5048
// Compute and verify inclusion proofs
5149
func FuzzInclusionProofAndVerify(f *testing.F) {
52-
for size := 0; size <= 8; size++ {
53-
for index := 0; index <= size; index++ {
50+
for size := range 8 + 1 {
51+
for index := range size + 1 {
5452
f.Add(uint64(index), uint64(size))
5553
}
5654
}
@@ -77,8 +75,8 @@ func FuzzInclusionProofAndVerify(f *testing.F) {
7775

7876
// Compute and verify inclusion proofs
7977
func FuzzSubtreeInclusionProofAndVerify(f *testing.F) {
80-
for end := 0; end <= 8; end++ {
81-
for start := 0; start <= end; start++ {
78+
for end := range 8 + 1 {
79+
for start := range end + 1 {
8280
for index := start; index <= end; index++ {
8381
f.Add(uint64(index), uint64(start), uint64(end))
8482
}
@@ -115,8 +113,8 @@ func FuzzSubtreeInclusionProofAndVerify(f *testing.F) {
115113
}
116114

117115
func FuzzHashAtAgainstReferenceImplementation(f *testing.F) {
118-
for size := 0; size <= 8; size++ {
119-
for index := 0; index <= size; index++ {
116+
for size := range 8 + 1 {
117+
for index := range size + 1 {
120118
f.Add(uint64(index), uint64(size))
121119
}
122120
}
@@ -139,8 +137,8 @@ func FuzzHashAtAgainstReferenceImplementation(f *testing.F) {
139137
}
140138

141139
func FuzzInclusionProofAgainstReferenceImplementation(f *testing.F) {
142-
for size := 0; size <= 8; size++ {
143-
for index := 0; index <= size; index++ {
140+
for size := range 8 + 1 {
141+
for index := range size + 1 {
144142
f.Add(uint64(index), uint64(size))
145143
}
146144
}
@@ -167,9 +165,9 @@ func FuzzInclusionProofAgainstReferenceImplementation(f *testing.F) {
167165
}
168166

169167
func FuzzConsistencyProofAgainstReferenceImplementation(f *testing.F) {
170-
for size := 0; size <= 8; size++ {
171-
for end := 0; end <= size; end++ {
172-
for begin := 0; begin <= end; begin++ {
168+
for size := range 8 + 1 {
169+
for end := range size + 1 {
170+
for begin := range end + 1 {
173171
f.Add(uint64(size), uint64(begin), uint64(end))
174172
}
175173
}

testonly/tree_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func validateTree(t *testing.T, mt *Tree, size uint64) {
3535
if got, want := mt.Hash(), roots[size]; !bytes.Equal(got, want) {
3636
t.Errorf("Hash(%d): %x, want %x", size, got, want)
3737
}
38-
for s := uint64(0); s <= size; s++ {
38+
for s := range size + 1 {
3939
if got, want := mt.HashAt(s), roots[s]; !bytes.Equal(got, want) {
4040
t.Errorf("HashAt(%d/%d): %x, want %x", s, size, got, want)
4141
}
@@ -70,7 +70,7 @@ func TestTreeHashAt(t *testing.T) {
7070
test := func(desc string, entries [][]byte) {
7171
t.Run(desc, func(t *testing.T) {
7272
mt := newTree(entries)
73-
for size := 0; size <= len(entries); size++ {
73+
for size := range len(entries) + 1 {
7474
got := mt.HashAt(uint64(size))
7575
want := refRootHash(entries[:size], mt.hasher)
7676
if !bytes.Equal(got, want) {
@@ -81,7 +81,7 @@ func TestTreeHashAt(t *testing.T) {
8181
}
8282

8383
entries := LeafInputs()
84-
for size := 0; size <= len(entries); size++ {
84+
for size := range len(entries) + 1 {
8585
test(fmt.Sprintf("size:%d", size), entries[:size])
8686
}
8787
test("generated", genEntries(256))
@@ -91,7 +91,8 @@ func TestTreeInclusionProof(t *testing.T) {
9191
test := func(desc string, entries [][]byte) {
9292
t.Run(desc, func(t *testing.T) {
9393
mt := newTree(entries)
94-
for index, size := uint64(0), uint64(len(entries)); index < size; index++ {
94+
size := uint64(len(entries))
95+
for index := range size {
9596
got, err := mt.InclusionProof(index, size)
9697
if err != nil {
9798
t.Fatalf("InclusionProof(%d, %d): %v", index, size, err)
@@ -106,7 +107,7 @@ func TestTreeInclusionProof(t *testing.T) {
106107

107108
test("generated", genEntries(256))
108109
entries := LeafInputs()
109-
for size := 0; size < len(entries); size++ {
110+
for size := range len(entries) {
110111
test(fmt.Sprintf("golden:%d", size), entries[:size])
111112
}
112113
}
@@ -120,7 +121,7 @@ func TestTreeConsistencyProof(t *testing.T) {
120121
t.Error("ConsistencyProof(6, 3) succeeded unexpectedly")
121122
}
122123

123-
for size1 := uint64(0); size1 <= 8; size1++ {
124+
for size1 := range uint64(8) + 1 {
124125
for size2 := size1; size2 <= 8; size2++ {
125126
t.Run(fmt.Sprintf("%d:%d", size1, size2), func(t *testing.T) {
126127
got, err := mt.ConsistencyProof(size1, size2)
@@ -142,7 +143,7 @@ func TestTreeConsistencyProofFuzz(t *testing.T) {
142143

143144
for treeSize := uint64(1); treeSize <= 256; treeSize++ {
144145
mt := newTree(entries[:treeSize])
145-
for i := 0; i < 8; i++ {
146+
for range 8 {
146147
size2 := rand.Uint64N(treeSize + 1)
147148
size1 := rand.Uint64N(size2 + 1)
148149

0 commit comments

Comments
 (0)