sm2: refine BenchmarkEncrypt test

This commit is contained in:
Sun Yimin 2024-05-15 10:17:15 +08:00 committed by GitHub
parent 105331f164
commit 7fb729f4a8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 30 deletions

View File

@ -14,11 +14,10 @@ import (
func BenchmarkSM4BCEncrypt1K(b *testing.B) { func BenchmarkSM4BCEncrypt1K(b *testing.B) {
var key [16]byte var key [16]byte
c, _ := sm4.NewCipher(key[:]) c, _ := sm4.NewCipher(key[:])
benchmarkBCEncrypt1K(b, c) benchmarkBCEncrypt(b, c, make([]byte, 1024))
} }
func benchmarkBCEncrypt1K(b *testing.B, block cipher.Block) { func benchmarkBCEncrypt(b *testing.B, block cipher.Block, buf []byte) {
buf := make([]byte, 1024)
b.SetBytes(int64(len(buf))) b.SetBytes(int64(len(buf)))
var iv [16]byte var iv [16]byte
@ -31,11 +30,10 @@ func benchmarkBCEncrypt1K(b *testing.B, block cipher.Block) {
func BenchmarkSM4BCDecrypt1K(b *testing.B) { func BenchmarkSM4BCDecrypt1K(b *testing.B) {
var key [16]byte var key [16]byte
c, _ := sm4.NewCipher(key[:]) c, _ := sm4.NewCipher(key[:])
benchmarkBCDecrypt1K(b, c) benchmarkBCDecrypt(b, c, make([]byte, 1024))
} }
func benchmarkBCDecrypt1K(b *testing.B, block cipher.Block) { func benchmarkBCDecrypt(b *testing.B, block cipher.Block, buf []byte) {
buf := make([]byte, 1024)
b.SetBytes(int64(len(buf))) b.SetBytes(int64(len(buf)))
var iv [16]byte var iv [16]byte
@ -59,8 +57,7 @@ func BenchmarkSM4HCTREncrypt1K(b *testing.B) {
} }
} }
func benchmarkECBEncrypt1K(b *testing.B, block cipher.Block) { func benchmarkECBEncrypt(b *testing.B, block cipher.Block, buf []byte) {
buf := make([]byte, 1024)
b.SetBytes(int64(len(buf))) b.SetBytes(int64(len(buf)))
ecb := smcipher.NewECBEncrypter(block) ecb := smcipher.NewECBEncrypter(block)
@ -72,17 +69,16 @@ func benchmarkECBEncrypt1K(b *testing.B, block cipher.Block) {
func BenchmarkSM4ECBEncrypt1K(b *testing.B) { func BenchmarkSM4ECBEncrypt1K(b *testing.B) {
var key [16]byte var key [16]byte
c, _ := sm4.NewCipher(key[:]) c, _ := sm4.NewCipher(key[:])
benchmarkECBEncrypt1K(b, c) benchmarkECBEncrypt(b, c, make([]byte, 1024))
} }
func BenchmarkAES128ECBEncrypt1K(b *testing.B) { func BenchmarkAES128ECBEncrypt1K(b *testing.B) {
var key [16]byte var key [16]byte
c, _ := aes.NewCipher(key[:]) c, _ := aes.NewCipher(key[:])
benchmarkECBEncrypt1K(b, c) benchmarkECBEncrypt(b, c, make([]byte, 1024))
} }
func benchmarkCBCEncrypt1K(b *testing.B, block cipher.Block) { func benchmarkCBCEncrypt(b *testing.B, block cipher.Block, buf []byte) {
buf := make([]byte, 1024)
b.SetBytes(int64(len(buf))) b.SetBytes(int64(len(buf)))
var iv [16]byte var iv [16]byte
@ -95,17 +91,22 @@ func benchmarkCBCEncrypt1K(b *testing.B, block cipher.Block) {
func BenchmarkAESCBCEncrypt1K(b *testing.B) { func BenchmarkAESCBCEncrypt1K(b *testing.B) {
var key [16]byte var key [16]byte
c, _ := aes.NewCipher(key[:]) c, _ := aes.NewCipher(key[:])
benchmarkCBCEncrypt1K(b, c) benchmarkCBCEncrypt(b, c, make([]byte, 1024))
} }
func BenchmarkSM4CBCEncrypt1K(b *testing.B) { func BenchmarkSM4CBCEncrypt1K(b *testing.B) {
var key [16]byte var key [16]byte
c, _ := sm4.NewCipher(key[:]) c, _ := sm4.NewCipher(key[:])
benchmarkCBCEncrypt1K(b, c) benchmarkCBCEncrypt(b, c, make([]byte, 1024))
} }
func benchmarkCBCDecrypt1K(b *testing.B, block cipher.Block) { func BenchmarkSM4CBCEncrypt8K(b *testing.B) {
buf := make([]byte, 1024) var key [16]byte
c, _ := sm4.NewCipher(key[:])
benchmarkCBCEncrypt(b, c, make([]byte, 8*1024))
}
func benchmarkCBCDecrypt(b *testing.B, block cipher.Block, buf []byte) {
b.SetBytes(int64(len(buf))) b.SetBytes(int64(len(buf)))
var iv [16]byte var iv [16]byte
@ -118,13 +119,13 @@ func benchmarkCBCDecrypt1K(b *testing.B, block cipher.Block) {
func BenchmarkAESCBCDecrypt1K(b *testing.B) { func BenchmarkAESCBCDecrypt1K(b *testing.B) {
var key [16]byte var key [16]byte
c, _ := aes.NewCipher(key[:]) c, _ := aes.NewCipher(key[:])
benchmarkCBCDecrypt1K(b, c) benchmarkCBCDecrypt(b, c, make([]byte, 1024))
} }
func BenchmarkSM4CBCDecrypt1K(b *testing.B) { func BenchmarkSM4CBCDecrypt1K(b *testing.B) {
var key [16]byte var key [16]byte
c, _ := sm4.NewCipher(key[:]) c, _ := sm4.NewCipher(key[:])
benchmarkCBCDecrypt1K(b, c) benchmarkCBCDecrypt(b, c, make([]byte, 1024))
} }
func benchmarkStream(b *testing.B, block cipher.Block, mode func(cipher.Block, []byte) cipher.Stream, buf []byte) { func benchmarkStream(b *testing.B, block cipher.Block, mode func(cipher.Block, []byte) cipher.Stream, buf []byte) {

View File

@ -823,12 +823,13 @@ func BenchmarkVerify_SM2(b *testing.B) {
} }
} }
func benchmarkEncrypt(b *testing.B, curve elliptic.Curve, plaintext string) { func benchmarkEncrypt(b *testing.B, curve elliptic.Curve, plaintext []byte) {
r := bufio.NewReaderSize(rand.Reader, 1<<15) r := bufio.NewReaderSize(rand.Reader, 1<<15)
priv, err := ecdsa.GenerateKey(curve, r) priv, err := ecdsa.GenerateKey(curve, r)
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)
} }
b.SetBytes(int64(len(plaintext)))
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
@ -836,26 +837,30 @@ func benchmarkEncrypt(b *testing.B, curve elliptic.Curve, plaintext string) {
} }
} }
func BenchmarkLessThan32_P256(b *testing.B) { func BenchmarkEncryptNoMoreThan32_P256(b *testing.B) {
benchmarkEncrypt(b, elliptic.P256(), "encryption standard") benchmarkEncrypt(b, elliptic.P256(), make([]byte, 31))
} }
func BenchmarkLessThan32_SM2(b *testing.B) { func BenchmarkEncryptNoMoreThan32_SM2(b *testing.B) {
benchmarkEncrypt(b, P256(), "encryption standard") benchmarkEncrypt(b, P256(), make([]byte, 31))
} }
func BenchmarkMoreThan32_P256(b *testing.B) { func BenchmarkEncrypt128_P256(b *testing.B) {
benchmarkEncrypt(b, elliptic.P256(), "encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard") benchmarkEncrypt(b, elliptic.P256(), make([]byte, 128))
} }
func BenchmarkMoreThan32_SM2(b *testing.B) { func BenchmarkEncrypt128_SM2(b *testing.B) {
benchmarkEncrypt(b, P256(), "encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard") benchmarkEncrypt(b, P256(), make([]byte, 128))
} }
func BenchmarkEncrypt512_SM2(b *testing.B) { func BenchmarkEncrypt512_SM2(b *testing.B) {
benchmarkEncrypt(b, P256(), "encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption s") benchmarkEncrypt(b, P256(), make([]byte, 512))
} }
func BenchmarkEncrypt1024_SM2(b *testing.B) { func BenchmarkEncrypt1K_SM2(b *testing.B) {
benchmarkEncrypt(b, P256(), "encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption sencryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption s") benchmarkEncrypt(b, P256(), make([]byte, 1024))
}
func BenchmarkEncrypt8K_SM2(b *testing.B) {
benchmarkEncrypt(b, P256(), make([]byte, 8*1024))
} }