|
| 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