-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwots_test.go
172 lines (139 loc) · 4.16 KB
/
wots_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package wotsp
import (
"bytes"
"crypto/rand"
"fmt"
"testing"
"github.com/lentus/wotsp/testdata"
// ensure our crypto is available. This is part of the tests, but not of the
// library itself, to avoid including more packages than the library's user
// will actually need.
_ "crypto/sha256"
)
// noerr is a helper that triggers t.Fatal[f] if the error is non-nil.
func noerr(t *testing.T, err error) {
if err != nil {
t.Fatalf("error occurred: [%s]", err.Error())
}
}
// TestGenPublicKey verifies the public key generation algorithm by comparing
// the resulting public key to a public key obtained from the reference
// implementation of RFC 8391.
func TestGenPublicKey(t *testing.T) {
var opts Opts
opts.Mode = W16 // explicit, in case the default ever changes
pubKey := GenPublicKey(testdata.Seed, testdata.PubSeed, opts)
if !bytes.Equal(pubKey, testdata.PubKey) {
t.Error("Wrong key")
}
}
// TestSign verifies the signing algorithm by comparing the resulting signature
// to a signature obtained from the reference implementation of RFC 8391.
func TestSign(t *testing.T) {
var opts Opts
opts.Mode = W16 // explicit, in case the default ever changes
signature := Sign(testdata.Message, testdata.Seed, testdata.PubSeed, opts)
if !bytes.Equal(signature, testdata.Signature) {
t.Error("Wrong signature")
}
}
// TestPkFromSig verifies the public key from signature algorithm by comparing
// the resulting public key to a public key obtained from the reference
// implementation of RFC 8391.
func TestPkFromSig(t *testing.T) {
var opts Opts
opts.Mode = W16 // explicit, in case the default ever changes
pubKey := PublicKeyFromSig(testdata.Signature, testdata.Message, testdata.PubSeed, opts)
if !bytes.Equal(pubKey, testdata.PubKey) {
t.Error("Wrong public key")
}
}
func TestVerify(t *testing.T) {
var opts Opts
opts.Mode = W16 // explicit, in case the default ever changes
ok := Verify(testdata.PubKey, testdata.Signature, testdata.Message, testdata.PubSeed, opts)
if !ok {
t.Error("Wrong public key")
}
}
// TestAll verifies the three signature scheme algorithms for all parameter
// sets by generating a public key and a signature, and verifying the signature
// for that public key.
func TestAll(t *testing.T) {
for _, mode := range []Mode{W4, W16, W256} {
var opts Opts
opts.Mode = mode
seed := make([]byte, 32)
_, err := rand.Read(seed)
noerr(t, err)
pubSeed := make([]byte, 32)
_, err = rand.Read(pubSeed)
noerr(t, err)
msg := make([]byte, 32)
_, err = rand.Read(msg)
noerr(t, err)
t.Run(fmt.Sprintf("TestAll-%s", opts.Mode),
func(t *testing.T) {
pubKey := GenPublicKey(seed, pubSeed, opts)
signed := Sign(msg, seed, pubSeed, opts)
valid := Verify(pubKey, signed, msg, pubSeed, opts)
if !valid {
t.Fail()
}
})
}
}
func BenchmarkWOTSP(b *testing.B) {
for _, mode := range []Mode{W4, W16, W256} {
runBenches(b, mode)
}
}
// runBenches runs the set of main benchmarks
func runBenches(b *testing.B, mode Mode) {
// test setup
var signature []byte
switch mode {
case W4:
signature = testdata.SignatureW4
case W256:
signature = testdata.SignatureW256
default:
signature = testdata.Signature
}
// create opts
var opts Opts
opts.Mode = mode
opts.Concurrency = -1
var maxRoutines = opts.routines()
// for each level of concurrency, run the benchmarks on this set of options.
for i := 1; i <= maxRoutines; i++ {
opts.Concurrency = i
b.Run(fmt.Sprintf("GenPublicKey-%s-%d", opts.Mode, i),
func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = GenPublicKey(testdata.Seed, testdata.PubSeed, opts)
}
})
}
for i := 1; i <= maxRoutines; i++ {
opts.Concurrency = i
b.Run(fmt.Sprintf("Sign-%s-%d", opts.Mode, i),
func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = Sign(testdata.Message, testdata.Seed, testdata.PubSeed, opts)
}
})
}
for i := 1; i <= maxRoutines; i++ {
opts.Concurrency = i
b.Run(fmt.Sprintf("PublicKeyFromSig-%s-%d", opts.Mode, i),
func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = PublicKeyFromSig(signature, testdata.Message, testdata.PubSeed, opts)
}
})
}
}