1
2
3
4
5 package nistec_test
6
7 import (
8 "crypto/internal/fips140/nistec"
9 "crypto/rand"
10 "testing"
11 )
12
13 type nistPoint[T any] interface {
14 Bytes() []byte
15 SetGenerator() T
16 SetBytes([]byte) (T, error)
17 Add(T, T) T
18 Double(T) T
19 ScalarMult(T, []byte) (T, error)
20 ScalarBaseMult([]byte) (T, error)
21 }
22
23 func BenchmarkScalarMult(b *testing.B) {
24 b.Run("P224", func(b *testing.B) {
25 benchmarkScalarMult(b, nistec.NewP224Point().SetGenerator(), 28)
26 })
27 b.Run("P256", func(b *testing.B) {
28 benchmarkScalarMult(b, nistec.NewP256Point().SetGenerator(), 32)
29 })
30 b.Run("P384", func(b *testing.B) {
31 benchmarkScalarMult(b, nistec.NewP384Point().SetGenerator(), 48)
32 })
33 b.Run("P521", func(b *testing.B) {
34 benchmarkScalarMult(b, nistec.NewP521Point().SetGenerator(), 66)
35 })
36 }
37
38 func benchmarkScalarMult[P nistPoint[P]](b *testing.B, p P, scalarSize int) {
39 scalar := make([]byte, scalarSize)
40 rand.Read(scalar)
41 b.ReportAllocs()
42 b.ResetTimer()
43 for i := 0; i < b.N; i++ {
44 p.ScalarMult(p, scalar)
45 }
46 }
47
48 func BenchmarkScalarBaseMult(b *testing.B) {
49 b.Run("P224", func(b *testing.B) {
50 benchmarkScalarBaseMult(b, nistec.NewP224Point().SetGenerator(), 28)
51 })
52 b.Run("P256", func(b *testing.B) {
53 benchmarkScalarBaseMult(b, nistec.NewP256Point().SetGenerator(), 32)
54 })
55 b.Run("P384", func(b *testing.B) {
56 benchmarkScalarBaseMult(b, nistec.NewP384Point().SetGenerator(), 48)
57 })
58 b.Run("P521", func(b *testing.B) {
59 benchmarkScalarBaseMult(b, nistec.NewP521Point().SetGenerator(), 66)
60 })
61 }
62
63 func benchmarkScalarBaseMult[P nistPoint[P]](b *testing.B, p P, scalarSize int) {
64 scalar := make([]byte, scalarSize)
65 rand.Read(scalar)
66 b.ReportAllocs()
67 b.ResetTimer()
68 for i := 0; i < b.N; i++ {
69 p.ScalarBaseMult(scalar)
70 }
71 }
72
View as plain text