ecdsa_bench_test.go raw

   1  package p256k1
   2  
   3  import (
   4  	"crypto/rand"
   5  	"testing"
   6  )
   7  
   8  var (
   9  	benchSeckey   []byte
  10  	benchPubkey   PublicKey
  11  	benchMsghash  []byte
  12  	benchSignature ECDSASignature
  13  )
  14  
  15  func initBenchmarkData() {
  16  	// Generate a fixed secret key for benchmarks
  17  	benchSeckey = []byte{
  18  		0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  19  		0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  20  		0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  21  		0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  22  	}
  23  	
  24  	// Ensure it's valid
  25  	var scalar Scalar
  26  	for !scalar.setB32Seckey(benchSeckey) {
  27  		if _, err := rand.Read(benchSeckey); err != nil {
  28  			panic(err)
  29  		}
  30  	}
  31  	
  32  	// Create public key
  33  	if err := ECPubkeyCreate(&benchPubkey, benchSeckey); err != nil {
  34  		panic(err)
  35  	}
  36  	
  37  	// Create message hash
  38  	benchMsghash = make([]byte, 32)
  39  	if _, err := rand.Read(benchMsghash); err != nil {
  40  		panic(err)
  41  	}
  42  	
  43  	// Create signature
  44  	if err := ECDSASign(&benchSignature, benchMsghash, benchSeckey); err != nil {
  45  		panic(err)
  46  	}
  47  }
  48  
  49  func BenchmarkECDSASign(b *testing.B) {
  50  	if benchSeckey == nil {
  51  		initBenchmarkData()
  52  	}
  53  	
  54  	b.ResetTimer()
  55  	for i := 0; i < b.N; i++ {
  56  		var sig ECDSASignature
  57  		ECDSASign(&sig, benchMsghash, benchSeckey)
  58  	}
  59  }
  60  
  61  func BenchmarkECDSAVerify(b *testing.B) {
  62  	if benchSeckey == nil {
  63  		initBenchmarkData()
  64  	}
  65  	
  66  	b.ResetTimer()
  67  	for i := 0; i < b.N; i++ {
  68  		ECDSAVerify(&benchSignature, benchMsghash, &benchPubkey)
  69  	}
  70  }
  71  
  72  func BenchmarkECDSASignCompact(b *testing.B) {
  73  	if benchSeckey == nil {
  74  		initBenchmarkData()
  75  	}
  76  	
  77  	b.ResetTimer()
  78  	for i := 0; i < b.N; i++ {
  79  		var compactSig ECDSASignatureCompact
  80  		ECDSASignCompact(&compactSig, benchMsghash, benchSeckey)
  81  	}
  82  }
  83  
  84  func BenchmarkECDSAVerifyCompact(b *testing.B) {
  85  	if benchSeckey == nil {
  86  		initBenchmarkData()
  87  	}
  88  	
  89  	var compactSig ECDSASignatureCompact
  90  	ECDSASignCompact(&compactSig, benchMsghash, benchSeckey)
  91  	
  92  	b.ResetTimer()
  93  	for i := 0; i < b.N; i++ {
  94  		ECDSAVerifyCompact(&compactSig, benchMsghash, &benchPubkey)
  95  	}
  96  }
  97  
  98  func BenchmarkECSeckeyGenerate(b *testing.B) {
  99  	b.ResetTimer()
 100  	for i := 0; i < b.N; i++ {
 101  		ECSeckeyGenerate()
 102  	}
 103  }
 104  
 105  func BenchmarkECKeyPairGenerate(b *testing.B) {
 106  	b.ResetTimer()
 107  	for i := 0; i < b.N; i++ {
 108  		ECKeyPairGenerate()
 109  	}
 110  }
 111  
 112  func BenchmarkSHA256(b *testing.B) {
 113  	data := make([]byte, 64)
 114  	rand.Read(data)
 115  	
 116  	b.ResetTimer()
 117  	for i := 0; i < b.N; i++ {
 118  		h := NewSHA256()
 119  		h.Write(data)
 120  		var result [32]byte
 121  		h.Finalize(result[:])
 122  		h.Clear()
 123  	}
 124  }
 125  
 126  func BenchmarkHMACSHA256(b *testing.B) {
 127  	key := make([]byte, 32)
 128  	data := make([]byte, 64)
 129  	rand.Read(key)
 130  	rand.Read(data)
 131  	
 132  	b.ResetTimer()
 133  	for i := 0; i < b.N; i++ {
 134  		hmac := NewHMACSHA256(key)
 135  		hmac.Write(data)
 136  		var result [32]byte
 137  		hmac.Finalize(result[:])
 138  		hmac.Clear()
 139  	}
 140  }
 141  
 142  func BenchmarkRFC6979(b *testing.B) {
 143  	key := make([]byte, 64)
 144  	rand.Read(key)
 145  	
 146  	b.ResetTimer()
 147  	for i := 0; i < b.N; i++ {
 148  		rng := NewRFC6979HMACSHA256(key)
 149  		var nonce [32]byte
 150  		rng.Generate(nonce[:])
 151  		rng.Finalize()
 152  		rng.Clear()
 153  	}
 154  }
 155  
 156  func BenchmarkTaggedHash(b *testing.B) {
 157  	tag := []byte("BIP0340/challenge")
 158  	data := make([]byte, 32)
 159  	rand.Read(data)
 160  	
 161  	b.ResetTimer()
 162  	for i := 0; i < b.N; i++ {
 163  		TaggedHash(tag, data)
 164  	}
 165  }
 166  
 167  
 168