Skip to content

Commit

Permalink
primitives: Add inclusion proof benchmarks.
Browse files Browse the repository at this point in the history
BenchmarkGenerateInclusionProof
-------------------------------
64_leaves     31101     38577 ns/op    2240 B/op   2 allocs/op
128_leaves    15512     77140 ns/op    4320 B/op   2 allocs/op
256_leaves     7999    155127 ns/op    8448 B/op   2 allocs/op
2048_leaves     975   1237867 ns/op   65888 B/op   2 allocs/op

BenchmarkVerifyInclusionProof
-----------------------------
64_leaves     333319   3999 ns/op   0 B/op   0 allocs/op
128_leaves    287670   4187 ns/op   0 B/op   0 allocs/op
256_leaves    249007   4841 ns/op   0 B/op   0 allocs/op
2048_leaves   181994   6683 ns/op   0 B/op   0 allocs/op
  • Loading branch information
davecgh committed Dec 4, 2021
1 parent 5fabbb1 commit bcf5fca
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions internal/staging/primitives/inclusionproof_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) 2021 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package primitives

import (
"encoding/binary"
"fmt"
"testing"

"github.com/decred/dcrd/chaincfg/chainhash"
)

// BenchmarkGenerateInclusionProof benchmarks generating inclusion proofs for
// various numbers of leaves.
func BenchmarkGenerateInclusionProof(b *testing.B) {
for _, numLeaves := range []uint32{64, 128, 256, 2048} {
// Generate the specified number of leaves and choose a leaf index
// accordingly.
benchName := fmt.Sprintf("%d leaves", numLeaves)
leaves := make([]chainhash.Hash, 0, numLeaves)
for i := uint32(0); i < numLeaves; i++ {
var buf [4]byte
binary.LittleEndian.PutUint32(buf[:], i)
leaves = append(leaves, chainhash.HashH(buf[:]))
}
leafIndex := numLeaves / 2

b.Run(benchName, func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = GenerateInclusionProof(leaves, leafIndex)
}
})
}
}

// BenchmarkVerifyInclusionProof benchmarks verifying inclusion proofs for
// various numbers of leaves.
func BenchmarkVerifyInclusionProof(b *testing.B) {
for _, numLeaves := range []uint32{64, 128, 256, 2048} {
// Generate the specified number of leaves, calculate the merkle root,
// choose a leaf index, and generate a proof accordingly.
benchName := fmt.Sprintf("%d leaves", numLeaves)
leaves := make([]chainhash.Hash, 0, numLeaves)
for i := uint32(0); i < numLeaves; i++ {
var buf [4]byte
binary.LittleEndian.PutUint32(buf[:], i)
leaves = append(leaves, chainhash.HashH(buf[:]))
}
root := CalcMerkleRoot(leaves)
leafIndex := numLeaves / 2
leaf := leaves[leafIndex]
proof := GenerateInclusionProof(leaves, leafIndex)

b.Run(benchName, func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
verified := VerifyInclusionProof(&root, &leaf, leafIndex, proof)
if !verified {
b.Fatalf("%q: failed to verify proof", benchName)
}
}
})
}
}

0 comments on commit bcf5fca

Please sign in to comment.