Skip to content
This repository was archived by the owner on Apr 17, 2024. It is now read-only.

Commit 43c17d4

Browse files
juergwcopybara-github
authored andcommitted
Add Benchmark tests for PRFs in Golang.
BenchmarkComputePRF/HMAC_SHA256_PRF_16-8 285105 3884 ns/op 528 B/op 5 allocs/op BenchmarkComputePRF/HMAC_SHA256_PRF_16k-8 19966 59463 ns/op 528 B/op 5 allocs/op BenchmarkComputePRF/HMAC_SHA512_PRF_16-8 256862 4397 ns/op 768 B/op 5 allocs/op BenchmarkComputePRF/HMAC_SHA512_PRF_16k-8 28039 42464 ns/op 768 B/op 5 allocs/op BenchmarkComputePRF/HKDF_SHA256_16-8 133785 8502 ns/op 1361 B/op 15 allocs/op BenchmarkComputePRF/HKDF_SHA256_16k-8 18424 65048 ns/op 1361 B/op 15 allocs/op BenchmarkComputePRF/AES_CMAC_PRF_16-8 3682260 323.0 ns/op 48 B/op 3 allocs/op BenchmarkComputePRF/AES_CMAC_PRF_16k-8 10000 114050 ns/op 48 B/op 3 allocs/op PiperOrigin-RevId: 615416650
1 parent cebe5e7 commit 43c17d4

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

go/prf/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ go_test(
4141
"aes_cmac_prf_key_manager_test.go",
4242
"hkdf_prf_key_manager_test.go",
4343
"hmac_prf_key_manager_test.go",
44+
"prf_benchmark_test.go",
4445
"prf_key_templates_test.go",
4546
"prf_set_factory_test.go",
4647
"prf_test.go",

go/prf/prf_benchmark_test.go

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
// //////////////////////////////////////////////////////////////////////////////
16+
17+
package prf_test
18+
19+
import (
20+
"testing"
21+
22+
"github.com/google/tink/go/keyset"
23+
"github.com/google/tink/go/prf"
24+
"github.com/google/tink/go/subtle/random"
25+
tinkpb "github.com/google/tink/go/proto/tink_go_proto"
26+
)
27+
28+
// Benchmarks for PRF algorithms.
29+
30+
func BenchmarkComputePRF(b *testing.B) {
31+
const (
32+
outputLength = 16
33+
)
34+
testCases := []struct {
35+
name string
36+
template *tinkpb.KeyTemplate
37+
dataSize uint32
38+
}{
39+
{
40+
name: "HMAC_SHA256_PRF_16",
41+
template: prf.HMACSHA256PRFKeyTemplate(),
42+
dataSize: 16,
43+
}, {
44+
name: "HMAC_SHA256_PRF_16k",
45+
template: prf.HMACSHA256PRFKeyTemplate(),
46+
dataSize: 16 * 1024,
47+
}, {
48+
name: "HMAC_SHA512_PRF_16",
49+
template: prf.HMACSHA512PRFKeyTemplate(),
50+
dataSize: 16,
51+
}, {
52+
name: "HMAC_SHA512_PRF_16k",
53+
template: prf.HMACSHA512PRFKeyTemplate(),
54+
dataSize: 16 * 1024,
55+
}, {
56+
name: "HKDF_SHA256_16",
57+
template: prf.HKDFSHA256PRFKeyTemplate(),
58+
dataSize: 16,
59+
}, {
60+
name: "HKDF_SHA256_16k",
61+
template: prf.HKDFSHA256PRFKeyTemplate(),
62+
dataSize: 16 * 1024,
63+
}, {
64+
name: "AES_CMAC_PRF_16",
65+
template: prf.AESCMACPRFKeyTemplate(),
66+
dataSize: 16,
67+
}, {
68+
name: "AES_CMAC_PRF_16k",
69+
template: prf.AESCMACPRFKeyTemplate(),
70+
dataSize: 16 * 1024,
71+
},
72+
}
73+
for _, tc := range testCases {
74+
b.Run(tc.name, func(b *testing.B) {
75+
b.ReportAllocs()
76+
77+
handle, err := keyset.NewHandle(tc.template)
78+
if err != nil {
79+
b.Fatal(err)
80+
}
81+
primitive, err := prf.NewPRFSet(handle)
82+
if err != nil {
83+
b.Fatal(err)
84+
}
85+
data := random.GetRandomBytes(tc.dataSize)
86+
b.ResetTimer()
87+
for i := 0; i < b.N; i++ {
88+
_, err := primitive.ComputePrimaryPRF(data, outputLength)
89+
if err != nil {
90+
b.Fatal(err)
91+
}
92+
}
93+
})
94+
}
95+
}

0 commit comments

Comments
 (0)