Skip to content

Commit 4a0dad2

Browse files
mmcloughlinFiloSottile
authored andcommitted
crypto/cipher: 8K benchmarks for AES stream modes
Some parallelizable cipher modes may achieve peak performance for larger block sizes. For this reason the AES-GCM mode already has an 8K benchmark alongside the 1K version. This change introduces 8K benchmarks for additional AES stream cipher modes. Updates #20967 Change-Id: If97c6fbf31222602dcc200f8f418d95908ec1202 Reviewed-on: https://go-review.googlesource.com/136897 Reviewed-by: Brad Fitzpatrick <[email protected]> Reviewed-by: Filippo Valsorda <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 699da6b commit 4a0dad2

File tree

1 file changed

+23
-44
lines changed

1 file changed

+23
-44
lines changed

src/crypto/cipher/benchmark_test.go

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -81,70 +81,49 @@ func BenchmarkAESGCMOpen8K(b *testing.B) {
8181
benchmarkAESGCMOpen(b, make([]byte, 8*1024))
8282
}
8383

84-
// If we test exactly 1K blocks, we would generate exact multiples of
85-
// the cipher's block size, and the cipher stream fragments would
86-
// always be wordsize aligned, whereas non-aligned is a more typical
87-
// use-case.
88-
const almost1K = 1024 - 5
89-
90-
func BenchmarkAESCFBEncrypt1K(b *testing.B) {
91-
buf := make([]byte, almost1K)
84+
func benchmarkAESStream(b *testing.B, mode func(cipher.Block, []byte) cipher.Stream, buf []byte) {
9285
b.SetBytes(int64(len(buf)))
9386

9487
var key [16]byte
9588
var iv [16]byte
9689
aes, _ := aes.NewCipher(key[:])
97-
ctr := cipher.NewCFBEncrypter(aes, iv[:])
90+
stream := mode(aes, iv[:])
9891

9992
b.ResetTimer()
10093
for i := 0; i < b.N; i++ {
101-
ctr.XORKeyStream(buf, buf)
94+
stream.XORKeyStream(buf, buf)
10295
}
10396
}
10497

105-
func BenchmarkAESCFBDecrypt1K(b *testing.B) {
106-
buf := make([]byte, almost1K)
107-
b.SetBytes(int64(len(buf)))
108-
109-
var key [16]byte
110-
var iv [16]byte
111-
aes, _ := aes.NewCipher(key[:])
112-
ctr := cipher.NewCFBDecrypter(aes, iv[:])
98+
// If we test exactly 1K blocks, we would generate exact multiples of
99+
// the cipher's block size, and the cipher stream fragments would
100+
// always be wordsize aligned, whereas non-aligned is a more typical
101+
// use-case.
102+
const almost1K = 1024 - 5
103+
const almost8K = 8*1024 - 5
113104

114-
b.ResetTimer()
115-
for i := 0; i < b.N; i++ {
116-
ctr.XORKeyStream(buf, buf)
117-
}
105+
func BenchmarkAESCFBEncrypt1K(b *testing.B) {
106+
benchmarkAESStream(b, cipher.NewCFBEncrypter, make([]byte, almost1K))
118107
}
119108

120-
func BenchmarkAESOFB1K(b *testing.B) {
121-
buf := make([]byte, almost1K)
122-
b.SetBytes(int64(len(buf)))
109+
func BenchmarkAESCFBDecrypt1K(b *testing.B) {
110+
benchmarkAESStream(b, cipher.NewCFBDecrypter, make([]byte, almost1K))
111+
}
123112

124-
var key [16]byte
125-
var iv [16]byte
126-
aes, _ := aes.NewCipher(key[:])
127-
ctr := cipher.NewOFB(aes, iv[:])
113+
func BenchmarkAESCFBDecrypt8K(b *testing.B) {
114+
benchmarkAESStream(b, cipher.NewCFBDecrypter, make([]byte, almost8K))
115+
}
128116

129-
b.ResetTimer()
130-
for i := 0; i < b.N; i++ {
131-
ctr.XORKeyStream(buf, buf)
132-
}
117+
func BenchmarkAESOFB1K(b *testing.B) {
118+
benchmarkAESStream(b, cipher.NewOFB, make([]byte, almost1K))
133119
}
134120

135121
func BenchmarkAESCTR1K(b *testing.B) {
136-
buf := make([]byte, almost1K)
137-
b.SetBytes(int64(len(buf)))
138-
139-
var key [16]byte
140-
var iv [16]byte
141-
aes, _ := aes.NewCipher(key[:])
142-
ctr := cipher.NewCTR(aes, iv[:])
122+
benchmarkAESStream(b, cipher.NewCTR, make([]byte, almost1K))
123+
}
143124

144-
b.ResetTimer()
145-
for i := 0; i < b.N; i++ {
146-
ctr.XORKeyStream(buf, buf)
147-
}
125+
func BenchmarkAESCTR8K(b *testing.B) {
126+
benchmarkAESStream(b, cipher.NewCTR, make([]byte, almost8K))
148127
}
149128

150129
func BenchmarkAESCBCEncrypt1K(b *testing.B) {

0 commit comments

Comments
 (0)