...
1 package schemes_test
2
3 import (
4 "fmt"
5 "testing"
6
7 "github.com/cloudflare/circl/sign"
8 "github.com/cloudflare/circl/sign/schemes"
9 )
10
11 func TestCaseSensitivity(t *testing.T) {
12 if schemes.ByName("ed25519") != schemes.ByName("Ed25519") {
13 t.Fatal()
14 }
15 }
16
17 func TestApi(t *testing.T) {
18 allSchemes := schemes.All()
19 for _, scheme := range allSchemes {
20 scheme := scheme
21 t.Run(scheme.Name(), func(t *testing.T) {
22 if scheme == nil {
23 t.Fatal()
24 }
25
26 pk, sk, err := scheme.GenerateKey()
27 if err != nil {
28 t.Fatal()
29 }
30
31 packedPk, err := pk.MarshalBinary()
32 if err != nil {
33 t.Fatal()
34 }
35
36 if len(packedPk) != scheme.PublicKeySize() {
37 t.Fatal()
38 }
39
40 packedSk, err := sk.MarshalBinary()
41 if err != nil {
42 t.Fatal(err)
43 }
44
45 if len(packedSk) != scheme.PrivateKeySize() {
46 t.Fatal()
47 }
48
49 pk2, err := scheme.UnmarshalBinaryPublicKey(packedPk)
50 if err != nil {
51 t.Fatal(err)
52 }
53
54 sk2, err := scheme.UnmarshalBinaryPrivateKey(packedSk)
55 if err != nil {
56 t.Fatal(err)
57 }
58
59 if !sk.Equal(sk2) {
60 t.Fatal()
61 }
62
63 if !pk.Equal(pk2) {
64 t.Fatal()
65 }
66
67 msg := []byte(fmt.Sprintf("Signing with %s", scheme.Name()))
68 opts := &sign.SignatureOpts{}
69 if scheme.SupportsContext() {
70 opts.Context = "A context"
71 }
72 sig := scheme.Sign(sk, msg, opts)
73
74 if scheme.SignatureSize() != len(sig) {
75 t.Fatal()
76 }
77
78 if !scheme.Verify(pk2, msg, sig, opts) {
79 t.Fatal()
80 }
81
82 if scheme.SupportsContext() {
83 opts2 := opts
84 opts2.Context = "Wrong context"
85 if scheme.Verify(pk2, msg, sig, opts2) {
86 t.Fatal()
87 }
88 }
89
90 sig[0]++
91 if scheme.Verify(pk2, msg, sig, opts) {
92 t.Fatal()
93 }
94
95 scheme2 := schemes.ByName(scheme.Name())
96 if scheme2 == nil || scheme2 != scheme {
97 t.Fatal()
98 }
99
100 if pk.Scheme() != scheme {
101 t.Fatal()
102 }
103
104 if sk.Scheme() != scheme {
105 t.Fatal()
106 }
107 })
108 }
109 }
110
111 func Example() {
112 for _, sch := range schemes.All() {
113 fmt.Println(sch.Name())
114 }
115
116
117
118
119
120 }
121
122 func BenchmarkGenerateKeyPair(b *testing.B) {
123 allSchemes := schemes.All()
124 for _, scheme := range allSchemes {
125 scheme := scheme
126 b.Run(scheme.Name(), func(b *testing.B) {
127 for i := 0; i < b.N; i++ {
128 _, _, _ = scheme.GenerateKey()
129 }
130 })
131 }
132 }
133
134 func BenchmarkSign(b *testing.B) {
135 allSchemes := schemes.All()
136 opts := &sign.SignatureOpts{}
137 for _, scheme := range allSchemes {
138 msg := []byte(fmt.Sprintf("Signing with %s", scheme.Name()))
139 scheme := scheme
140 _, sk, _ := scheme.GenerateKey()
141 b.Run(scheme.Name(), func(b *testing.B) {
142 for i := 0; i < b.N; i++ {
143 _ = scheme.Sign(sk, msg, opts)
144 }
145 })
146 }
147 }
148
149 func BenchmarkVerify(b *testing.B) {
150 allSchemes := schemes.All()
151 opts := &sign.SignatureOpts{}
152 for _, scheme := range allSchemes {
153 msg := []byte(fmt.Sprintf("Signing with %s", scheme.Name()))
154 scheme := scheme
155 pk, sk, _ := scheme.GenerateKey()
156 sig := scheme.Sign(sk, msg, opts)
157 b.Run(scheme.Name(), func(b *testing.B) {
158 for i := 0; i < b.N; i++ {
159 _ = scheme.Verify(pk, msg, sig, opts)
160 }
161 })
162 }
163 }
164
View as plain text