Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/proofgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 1 addition & 3 deletions compact/node_fuzz_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build go1.18

package compact

import (
Expand All @@ -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))
}
Expand Down
2 changes: 1 addition & 1 deletion compact/nodes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 16 additions & 16 deletions compact/range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) != 0 {
hashes = append(hashes, tr.nodes[lvl][pos>>lvl].hash)
pos += 1 << lvl
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion proof/proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion rfc6962/rfc6962_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
24 changes: 11 additions & 13 deletions testonly/tree_fuzz_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build go1.18

package testonly

import (
Expand Down Expand Up @@ -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))
}
}
Expand All @@ -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))
}
Expand Down Expand Up @@ -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))
}
}
Expand All @@ -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))
}
}
Expand All @@ -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))
}
}
Expand Down
15 changes: 8 additions & 7 deletions testonly/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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))
Expand All @@ -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)
Expand All @@ -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])
}
}
Expand All @@ -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)
Expand All @@ -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)

Expand Down
Loading