From: Filippo Valsorda Date: Wed, 28 Apr 2021 06:12:17 +0000 (-0400) Subject: crypto/cipher: make AES-GCM benchmarks match ChaCha20Poly1305 ones X-Git-Tag: go1.17beta1~384 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=ad989c7dbad8227a0e6944802f0557e625807bb2;p=gostls13.git crypto/cipher: make AES-GCM benchmarks match ChaCha20Poly1305 ones It's useful to compare TLS AEADs. Here are the numbers on my MacBook with an Intel(R) Core(TM) i7-7920HQ CPU @ 3.10GHz. name speed AESGCM/Open-128-64-8 692MB/s ± 2% AESGCM/Seal-128-64-8 568MB/s ± 1% AESGCM/Open-128-1350-8 3.96GB/s ± 1% AESGCM/Seal-128-1350-8 3.17GB/s ± 4% AESGCM/Open-128-8192-8 5.46GB/s ± 2% AESGCM/Seal-128-8192-8 4.40GB/s ± 3% name speed AESGCM/Open-256-64-8 602MB/s ± 2% AESGCM/Seal-256-64-8 508MB/s ± 1% AESGCM/Open-256-1350-8 3.06GB/s ± 1% AESGCM/Seal-256-1350-8 2.65GB/s ± 2% AESGCM/Open-256-8192-8 4.02GB/s ± 3% AESGCM/Seal-256-8192-8 3.53GB/s ± 2% name speed Chacha20Poly1305/Open-64-8 385MB/s ± 3% Chacha20Poly1305/Seal-64-8 396MB/s ± 3% Chacha20Poly1305/Open-1350-8 1.67GB/s ± 2% Chacha20Poly1305/Seal-1350-8 1.62GB/s ± 1% Chacha20Poly1305/Open-8192-8 2.04GB/s ± 2% Chacha20Poly1305/Seal-8192-8 2.04GB/s ± 3% Change-Id: I9373ab85bf132b45b41078205259100fa2d46dda Reviewed-on: https://go-review.googlesource.com/c/go/+/314610 Trust: Filippo Valsorda Run-TryBot: Filippo Valsorda TryBot-Result: Go Bot Reviewed-by: Roland Shoemaker --- diff --git a/src/crypto/cipher/benchmark_test.go b/src/crypto/cipher/benchmark_test.go index 90d0cd7138..eb02cd08c5 100644 --- a/src/crypto/cipher/benchmark_test.go +++ b/src/crypto/cipher/benchmark_test.go @@ -7,28 +7,15 @@ package cipher_test import ( "crypto/aes" "crypto/cipher" + "strconv" "testing" ) -func benchmarkAESGCMSign(b *testing.B, buf []byte) { +func benchmarkAESGCMSeal(b *testing.B, buf []byte, keySize int) { + b.ReportAllocs() b.SetBytes(int64(len(buf))) - var key [16]byte - var nonce [12]byte - aes, _ := aes.NewCipher(key[:]) - aesgcm, _ := cipher.NewGCM(aes) - var out []byte - - b.ResetTimer() - for i := 0; i < b.N; i++ { - out = aesgcm.Seal(out[:0], nonce[:], nil, buf) - } -} - -func benchmarkAESGCMSeal(b *testing.B, buf []byte) { - b.SetBytes(int64(len(buf))) - - var key [16]byte + var key = make([]byte, keySize) var nonce [12]byte var ad [13]byte aes, _ := aes.NewCipher(key[:]) @@ -41,44 +28,41 @@ func benchmarkAESGCMSeal(b *testing.B, buf []byte) { } } -func benchmarkAESGCMOpen(b *testing.B, buf []byte) { +func benchmarkAESGCMOpen(b *testing.B, buf []byte, keySize int) { + b.ReportAllocs() b.SetBytes(int64(len(buf))) - var key [16]byte + var key = make([]byte, keySize) var nonce [12]byte var ad [13]byte aes, _ := aes.NewCipher(key[:]) aesgcm, _ := cipher.NewGCM(aes) var out []byte - out = aesgcm.Seal(out[:0], nonce[:], buf, ad[:]) + + ct := aesgcm.Seal(nil, nonce[:], buf[:], ad[:]) b.ResetTimer() for i := 0; i < b.N; i++ { - _, err := aesgcm.Open(buf[:0], nonce[:], out, ad[:]) - if err != nil { - b.Errorf("Open: %v", err) - } + out, _ = aesgcm.Open(out[:0], nonce[:], ct, ad[:]) } } -func BenchmarkAESGCMSeal1K(b *testing.B) { - benchmarkAESGCMSeal(b, make([]byte, 1024)) -} - -func BenchmarkAESGCMOpen1K(b *testing.B) { - benchmarkAESGCMOpen(b, make([]byte, 1024)) -} - -func BenchmarkAESGCMSign8K(b *testing.B) { - benchmarkAESGCMSign(b, make([]byte, 8*1024)) -} - -func BenchmarkAESGCMSeal8K(b *testing.B) { - benchmarkAESGCMSeal(b, make([]byte, 8*1024)) -} - -func BenchmarkAESGCMOpen8K(b *testing.B) { - benchmarkAESGCMOpen(b, make([]byte, 8*1024)) +func BenchmarkAESGCM(b *testing.B) { + for _, length := range []int{64, 1350, 8 * 1024} { + b.Run("Open-128-"+strconv.Itoa(length), func(b *testing.B) { + benchmarkAESGCMOpen(b, make([]byte, length), 128/8) + }) + b.Run("Seal-128-"+strconv.Itoa(length), func(b *testing.B) { + benchmarkAESGCMSeal(b, make([]byte, length), 128/8) + }) + + b.Run("Open-256-"+strconv.Itoa(length), func(b *testing.B) { + benchmarkAESGCMOpen(b, make([]byte, length), 256/8) + }) + b.Run("Seal-256-"+strconv.Itoa(length), func(b *testing.B) { + benchmarkAESGCMSeal(b, make([]byte, length), 256/8) + }) + } } func benchmarkAESStream(b *testing.B, mode func(cipher.Block, []byte) cipher.Stream, buf []byte) {