github.com/zntrio/harp/v2@v2.0.9/pkg/sdk/security/crypto/deterministicecdsa/ecdsa_test.go (about) 1 package deterministicecdsa 2 3 import ( 4 "crypto/elliptic" 5 "crypto/rand" 6 "testing" 7 ) 8 9 func testAllCurves(t *testing.T, f func(*testing.T, elliptic.Curve)) { 10 t.Helper() 11 12 tests := []struct { 13 name string 14 curve elliptic.Curve 15 }{ 16 {"P256", elliptic.P256()}, 17 {"P224", elliptic.P224()}, 18 {"P384", elliptic.P384()}, 19 {"P521", elliptic.P521()}, 20 } 21 if testing.Short() { 22 tests = tests[:1] 23 } 24 for _, test := range tests { 25 curve := test.curve 26 t.Run(test.name, func(t *testing.T) { 27 t.Parallel() 28 f(t, curve) 29 }) 30 } 31 } 32 33 func TestKeyGeneration(t *testing.T) { 34 t.Parallel() 35 36 testAllCurves(t, testKeyGeneration) 37 } 38 39 func testKeyGeneration(t *testing.T, c elliptic.Curve) { 40 t.Helper() 41 42 priv, err := GenerateKey(c, rand.Reader) 43 if err != nil { 44 t.Fatal(err) 45 } 46 if !c.IsOnCurve(priv.PublicKey.X, priv.PublicKey.Y) { 47 t.Errorf("public key invalid: %s", err) 48 } 49 } 50 51 func benchmarkAllCurves(b *testing.B, f func(*testing.B, elliptic.Curve)) { 52 tests := []struct { 53 name string 54 curve elliptic.Curve 55 }{ 56 {"P256", elliptic.P256()}, 57 {"P224", elliptic.P224()}, 58 {"P384", elliptic.P384()}, 59 {"P521", elliptic.P521()}, 60 } 61 for _, test := range tests { 62 curve := test.curve 63 b.Run(test.name, func(b *testing.B) { 64 f(b, curve) 65 }) 66 } 67 } 68 69 func BenchmarkGenerateKey(b *testing.B) { 70 benchmarkAllCurves(b, func(b *testing.B, curve elliptic.Curve) { 71 b.ReportAllocs() 72 b.ResetTimer() 73 for i := 0; i < b.N; i++ { 74 if _, err := GenerateKey(curve, rand.Reader); err != nil { 75 b.Fatal(err) 76 } 77 } 78 }) 79 }