Debian Codegraph @

jwk/es256k_test.go

raw symbols SCIP
1 //go:build jwx_es256k
2
3 package jwk_test
4
5 import (
6 "crypto/ecdsa"
7 "encoding/base64"
8 "encoding/json"
9 "math/big"
10 "testing"
11
12 "github.com/decred/dcrd/dcrec/secp256k1/v4"
13 "github.com/lestrrat-go/jwx/v3/jwa"
14 "github.com/lestrrat-go/jwx/v3/jwk"
15 ourecdsa "github.com/lestrrat-go/jwx/v3/jwk/ecdsa"
16 "github.com/stretchr/testify/require"
17 )
18
19 func TestES256K(t *testing.T) {
20 require.True(t, ourecdsa.IsCurveAvailable(jwa.Secp256k1()), `jwa.Secp256k1 should be available`)
21 }
22
23 func BenchmarkKeyInstantiation(b *testing.B) {
24 const xb64 = "YAXIamcY9mIhcTp3BzxBKRzDq7_NA6pJVemytQ2_f5s"
25 const yb64 = "ZnLa0NRq3mHjgveYiKc-p4mdlBm-zx1snsIIfBGI-hg"
26
27 x, err := base64.RawURLEncoding.DecodeString(xb64)
28 require.NoError(b, err, `DecodeBase64 should succeed`)
29 y, err := base64.RawURLEncoding.DecodeString(yb64)
30 require.NoError(b, err, `DecodeBase64 should succeed`)
31
32 b.Run("Use json.Marshal/json.Unmarshal", func(b *testing.B) {
33 for i := 0; i < b.N; i++ {
34 serialized, err := json.Marshal(map[string]any{
35 "kty": "EC",
36 "crv": "secp256k1",
37 "x": xb64,
38 "y": yb64,
39 })
40 if err != nil {
41 panic(err)
42 }
43
44 key, err := jwk.Parse(serialized)
45 if err != nil {
46 panic(err)
47 }
48 _ = key
49 }
50 })
51 b.Run("Use jwk.Import", func(b *testing.B) {
52 for i := 0; i < b.N; i++ {
53 var raw ecdsa.PublicKey
54 raw.Curve = secp256k1.S256()
55 raw.X = &big.Int{}
56 raw.Y = &big.Int{}
57 raw.X.SetBytes(x)
58 raw.Y.SetBytes(y)
59
60 key, err := jwk.Import(&raw)
61 if err != nil {
62 panic(err)
63 }
64 _ = key
65 }
66 })
67 }