| // Copyright 2024 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| |
| package nistec_test |
| |
| import ( |
| "crypto/internal/fips140/nistec" |
| "crypto/rand" |
| "testing" |
| ) |
| |
| type nistPoint[T any] interface { |
| Bytes() []byte |
| SetGenerator() T |
| SetBytes([]byte) (T, error) |
| Add(T, T) T |
| Double(T) T |
| ScalarMult(T, []byte) (T, error) |
| ScalarBaseMult([]byte) (T, error) |
| } |
| |
| func BenchmarkScalarMult(b *testing.B) { |
| b.Run("P224", func(b *testing.B) { |
| benchmarkScalarMult(b, nistec.NewP224Point().SetGenerator(), 28) |
| }) |
| b.Run("P256", func(b *testing.B) { |
| benchmarkScalarMult(b, nistec.NewP256Point().SetGenerator(), 32) |
| }) |
| b.Run("P384", func(b *testing.B) { |
| benchmarkScalarMult(b, nistec.NewP384Point().SetGenerator(), 48) |
| }) |
| b.Run("P521", func(b *testing.B) { |
| benchmarkScalarMult(b, nistec.NewP521Point().SetGenerator(), 66) |
| }) |
| } |
| |
| func benchmarkScalarMult[P nistPoint[P]](b *testing.B, p P, scalarSize int) { |
| scalar := make([]byte, scalarSize) |
| rand.Read(scalar) |
| b.ReportAllocs() |
| b.ResetTimer() |
| for i := 0; i < b.N; i++ { |
| p.ScalarMult(p, scalar) |
| } |
| } |
| |
| func BenchmarkScalarBaseMult(b *testing.B) { |
| b.Run("P224", func(b *testing.B) { |
| benchmarkScalarBaseMult(b, nistec.NewP224Point().SetGenerator(), 28) |
| }) |
| b.Run("P256", func(b *testing.B) { |
| benchmarkScalarBaseMult(b, nistec.NewP256Point().SetGenerator(), 32) |
| }) |
| b.Run("P384", func(b *testing.B) { |
| benchmarkScalarBaseMult(b, nistec.NewP384Point().SetGenerator(), 48) |
| }) |
| b.Run("P521", func(b *testing.B) { |
| benchmarkScalarBaseMult(b, nistec.NewP521Point().SetGenerator(), 66) |
| }) |
| } |
| |
| func benchmarkScalarBaseMult[P nistPoint[P]](b *testing.B, p P, scalarSize int) { |
| scalar := make([]byte, scalarSize) |
| rand.Read(scalar) |
| b.ReportAllocs() |
| b.ResetTimer() |
| for i := 0; i < b.N; i++ { |
| p.ScalarBaseMult(scalar) |
| } |
| } |