mldsa: add benchmark test

This commit is contained in:
Sun Yimin 2025-05-29 08:31:25 +08:00 committed by GitHub
parent 67ac5da71e
commit 8f0bd765ca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 93 additions and 0 deletions

View File

@ -8,6 +8,7 @@ package mldsa
import ( import (
"bytes" "bytes"
"crypto/rand"
"encoding/asn1" "encoding/asn1"
"encoding/hex" "encoding/hex"
"testing" "testing"
@ -308,3 +309,33 @@ func (zr) Read(dst []byte) (n int, err error) {
} }
var zeroReader = zr{} var zeroReader = zr{}
func BenchmarkKeyGen44(b *testing.B) {
var d [32]byte
rand.Read(d[:])
b.ResetTimer()
for b.Loop() {
if _, err := NewKey44(d[:]); err != nil {
b.Fatalf("NewKey44 failed: %v", err)
}
}
}
func BenchmarkSign44(b *testing.B) {
var seed [32]byte
c := sigGen44InternalProjectionCases[0]
sk, _ := hex.DecodeString(c.sk)
mu, _ := hex.DecodeString(c.mu)
priv, err := NewPrivateKey44(sk)
if err != nil {
b.Fatalf("NewPrivateKey44 failed: %v", err)
}
b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
_, err := priv.signInternal(seed[:], mu)
if err != nil {
b.Fatalf("signInternal failed: %v", err)
}
}
}

View File

@ -8,6 +8,7 @@ package mldsa
import ( import (
"bytes" "bytes"
"crypto/rand"
"encoding/asn1" "encoding/asn1"
"encoding/hex" "encoding/hex"
"testing" "testing"
@ -298,3 +299,33 @@ func TestVerify65(t *testing.T) {
} }
} }
} }
func BenchmarkKeyGen65(b *testing.B) {
var d [32]byte
rand.Read(d[:])
b.ResetTimer()
for b.Loop() {
if _, err := NewKey65(d[:]); err != nil {
b.Fatalf("NewKey44 failed: %v", err)
}
}
}
func BenchmarkSign65(b *testing.B) {
var seed [32]byte
c := sigGen65InternalProjectionCases[0]
sk, _ := hex.DecodeString(c.sk)
mu, _ := hex.DecodeString(c.mu)
priv, err := NewPrivateKey65(sk)
if err != nil {
b.Fatalf("NewPrivateKey65 failed: %v", err)
}
b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
_, err := priv.signInternal(seed[:], mu)
if err != nil {
b.Fatalf("signInternal failed: %v", err)
}
}
}

View File

@ -8,6 +8,7 @@ package mldsa
import ( import (
"bytes" "bytes"
"crypto/rand"
"encoding/asn1" "encoding/asn1"
"encoding/hex" "encoding/hex"
"testing" "testing"
@ -258,3 +259,33 @@ func TestVerify87(t *testing.T) {
} }
} }
} }
func BenchmarkKeyGen87(b *testing.B) {
var d [32]byte
rand.Read(d[:])
b.ResetTimer()
for b.Loop() {
if _, err := NewKey87(d[:]); err != nil {
b.Fatalf("NewKey44 failed: %v", err)
}
}
}
func BenchmarkSign87(b *testing.B) {
var seed [32]byte
c := sigGen87InternalProjectionCases[0]
sk, _ := hex.DecodeString(c.sk)
mu, _ := hex.DecodeString(c.mu)
priv, err := NewPrivateKey87(sk)
if err != nil {
b.Fatalf("NewPrivateKey87 failed: %v", err)
}
b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
_, err := priv.signInternal(seed[:], mu)
if err != nil {
b.Fatalf("signInternal failed: %v", err)
}
}
}