From 8f0bd765cacb241b93ea596cd8017b051b738288 Mon Sep 17 00:00:00 2001 From: Sun Yimin Date: Thu, 29 May 2025 08:31:25 +0800 Subject: [PATCH] mldsa: add benchmark test --- mldsa/mldsa44_test.go | 31 +++++++++++++++++++++++++++++++ mldsa/mldsa65_test.go | 31 +++++++++++++++++++++++++++++++ mldsa/mldsa87_test.go | 31 +++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) diff --git a/mldsa/mldsa44_test.go b/mldsa/mldsa44_test.go index 86b0f0f..6339395 100644 --- a/mldsa/mldsa44_test.go +++ b/mldsa/mldsa44_test.go @@ -8,6 +8,7 @@ package mldsa import ( "bytes" + "crypto/rand" "encoding/asn1" "encoding/hex" "testing" @@ -308,3 +309,33 @@ func (zr) Read(dst []byte) (n int, err error) { } 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) + } + } +} diff --git a/mldsa/mldsa65_test.go b/mldsa/mldsa65_test.go index 4a69474..453feaf 100644 --- a/mldsa/mldsa65_test.go +++ b/mldsa/mldsa65_test.go @@ -8,6 +8,7 @@ package mldsa import ( "bytes" + "crypto/rand" "encoding/asn1" "encoding/hex" "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) + } + } +} diff --git a/mldsa/mldsa87_test.go b/mldsa/mldsa87_test.go index 74c5476..f2d55ca 100644 --- a/mldsa/mldsa87_test.go +++ b/mldsa/mldsa87_test.go @@ -8,6 +8,7 @@ package mldsa import ( "bytes" + "crypto/rand" "encoding/asn1" "encoding/hex" "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) + } + } +}