-
Notifications
You must be signed in to change notification settings - Fork 0
/
sig_test.go
264 lines (216 loc) · 7.7 KB
/
sig_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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright (c) 2023 Yawning Angel
//
// SPDX-License-Identifier: SSPL-1.0
package musig2
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func testSignVectors(t *testing.T) {
var testVectors struct {
tvHeader
SignError []tvErrorCase `json:"sign_error_test_cases"`
}
unpackTestVectors(t, "testdata/sign_verify_vectors.json", &testVectors)
for i := range testVectors.Valid {
vec := &testVectors.Valid[i]
t.Run(fmt.Sprintf("Valid %d", i), func(t *testing.T) {
if vec.Comment != "" {
t.Log(vec.Comment)
}
sk := testVectors.SecKey(t)
pkAggregator := testVectors.PublicKeyAggregator(t, vec.KeyIndices)
aggPk, err := pkAggregator.Aggregate()
require.NoError(t, err)
msg := testVectors.Messages()[vec.MsgIndex]
require.NotNil(t, msg)
// WARNING: Don't ever do this. Tbh, including this in test
// vectors/test cases is fucking moronic, despite the warning
// in the reference python code.
//
// I get that it's the crypto winter, but I didn't know that
// things were so tight that they can't spare (at most)
// 97-bytes per valid test case.
secNonces, _ := testVectors.SecNonces()
secNonce := secNonces[0].UnsafeClone()
nonceAggregator := testVectors.PublicNonceAggregator(t, vec.NonceIndices)
aggNonce, err := nonceAggregator.Aggregate()
require.NoError(t, err)
aggNonces, _ := testVectors.AggNonces()
expectedAggNonce := aggNonces[vec.AggNonceIndex]
require.EqualValues(t, expectedAggNonce.Bytes(), aggNonce.Bytes())
partialSig, err := Sign(sk, aggPk, secNonce, aggNonce, msg)
require.NoError(t, err, "Sign")
require.EqualValues(t, vec.Expected(), partialSig.Bytes())
// This is done implicitly, but do it explicitly as well.
ok := partialSig.Verify(sk.PublicKey(), secNonce.PublicNonce(), aggPk, aggNonce, msg)
require.True(t, ok)
// Check that the secNonce got invalidated.
require.False(t, secNonce.IsValid())
})
}
for i := range testVectors.SignError {
vec := &testVectors.SignError[i]
t.Run(fmt.Sprintf("Error %d", i), func(t *testing.T) {
if vec.Comment != "" {
t.Log(vec.Comment)
}
sk := testVectors.SecKey(t)
if vec.Error.Is(typeInvalidContribution, contribPubKey) {
pubKeys, errs := testVectors.PubKeys()
t.Log(pubKeys)
idx := vec.KeyIndices[*vec.Error.Signer]
require.Nil(t, pubKeys[idx])
require.Error(t, errs[idx])
return
}
aggregator := testVectors.PublicKeyAggregator(t, vec.KeyIndices)
aggPk, err := aggregator.Aggregate()
require.NoError(t, err)
msg := testVectors.Messages()[vec.MsgIndex]
require.NotNil(t, msg)
// Sigh, just... no (See the warning in the successful case).
secNonces, errs := testVectors.SecNonces()
secNonce := secNonces[vec.SecNonceIndex]
if secNonce == nil {
require.Equal(t, typeValue, vec.Error.Type)
require.ErrorIs(t, errs[vec.SecNonceIndex], errKIsZero)
return
}
secNonce = secNonce.UnsafeClone()
aggNonces, errs := testVectors.AggNonces()
if vec.Error.Is(typeInvalidContribution, contribAggNonce) {
idx := vec.AggNonceIndex
require.Nil(t, aggNonces[idx])
require.Error(t, errs[idx])
return
}
aggNonce := aggNonces[vec.AggNonceIndex]
partialSig, err := Sign(sk, aggPk, secNonce, aggNonce, msg)
require.Error(t, err, "Sign")
require.Nil(t, partialSig)
// Check that the secNonce got invalidated, the fast way.
require.False(t, secNonce.IsValid())
})
}
}
func testPartialSigVerifyVectors(t *testing.T) {
// We are only concerned about the various failures, as the
// successful case is covered by the Sign tests.
var testVectors struct {
tvHeader
Fail []tvErrorCase `json:"verify_fail_test_cases"`
Error []tvErrorCase `json:"verify_error_test_cases"`
}
unpackTestVectors(t, "testdata/sign_verify_vectors.json", &testVectors)
pubKeys, pubKeysErrs := testVectors.PubKeys()
pubNonces, pubNoncesErrs := testVectors.PubNonces()
for i := range testVectors.Fail {
vec := &testVectors.Fail[i]
t.Run(fmt.Sprintf("Fail %d", i), func(t *testing.T) {
if vec.Comment != "" {
t.Log(vec.Comment)
}
aggregator := testVectors.PublicKeyAggregator(t, vec.KeyIndices)
aggPk, err := aggregator.Aggregate()
require.NoError(t, err)
msg := testVectors.Messages()[vec.MsgIndex]
require.NotNil(t, msg)
nonceAggregator := testVectors.PublicNonceAggregator(t, vec.NonceIndices)
aggNonce, err := nonceAggregator.Aggregate()
require.NoError(t, err)
idx := vec.SignerIndex
pubKey, pubNonce := pubKeys[vec.KeyIndices[idx]], pubNonces[vec.NonceIndices[idx]]
partialSig, err := NewPartialSignature(mustUnhex(vec.Sig))
// Is it too much to ask for test cases to consistently include
// the "error" sub-structure.
switch i {
case 2:
// Signature fails to deserialize.
require.Nil(t, partialSig)
require.ErrorIs(t, err, errInvalidPartialSig)
default:
require.NoError(t, err)
ok := partialSig.Verify(pubKey, pubNonce, aggPk, aggNonce, msg)
require.False(t, ok)
}
})
}
for i := range testVectors.Error {
vec := &testVectors.Error[i]
t.Run(fmt.Sprintf("Error %d", i), func(t *testing.T) {
if vec.Comment != "" {
t.Log(vec.Comment)
}
// These are just deserialization failures.
require.Equal(t, typeInvalidContribution, vec.Error.Type)
idx := vec.SignerIndex
pubKeyErr, pubNonceErr := pubKeysErrs[vec.KeyIndices[idx]], pubNoncesErrs[vec.NonceIndices[idx]]
t.Log(pubKeyErr)
t.Log(pubNonceErr)
switch vec.Error.Contrib {
case contribPubKey:
require.Error(t, pubKeyErr)
case contribPubNonce:
require.Error(t, pubNonceErr)
default:
t.Fatalf("unsupported test case contrib: '%s'", vec.Error.Contrib)
}
})
}
}
func testPartialSigAggVectors(t *testing.T) {
var testVectors tvHeader
unpackTestVectors(t, "testdata/sig_agg_vectors.json", &testVectors)
msg := testVectors.Msg()
for i := range testVectors.Valid {
vec := &testVectors.Valid[i]
t.Run(fmt.Sprintf("Valid %d", i), func(t *testing.T) {
if vec.Comment != "" {
t.Log(vec.Comment)
}
pkAggregator := testVectors.PublicKeyAggregator(t, vec.KeyIndices)
aggPk, err := pkAggregator.Aggregate()
require.NoError(t, err)
tweaks := testVectors.Tweaks()
for ii, idx := range vec.TweakIndices {
tweak, isXOnly := tweaks[idx], vec.IsXOnly[ii]
err := aggPk.ApplyTweak(tweak, isXOnly)
require.NoError(t, err, "ApplyTweak")
}
nonceAggregator := testVectors.PublicNonceAggregator(t, vec.NonceIndices)
aggNonce, err := nonceAggregator.Aggregate()
require.NoError(t, err)
expectedAggNonce := vec.AggNonce(t)
require.EqualValues(t, expectedAggNonce.Bytes(), aggNonce.Bytes())
sigAggregator := NewPartialSignatureAggregator(aggPk, aggNonce, msg)
hdrPSigs, errs := testVectors.PartialSigs()
for _, idx := range vec.PSigIndices {
require.NoError(t, errs[idx])
err = sigAggregator.Add(hdrPSigs[idx])
require.NoError(t, err)
}
sig, err := sigAggregator.Aggregate()
require.NoError(t, err)
// So, the claim/whole point of this MuSig2 nonsense is
// that aggregated signatures are compatible with BIP-340
// Schnorr signatures, so test that as well.
ok := aggPk.Schnorr().Verify(msg, sig)
require.True(t, ok, "SchnorrPublicKey.Verify")
})
}
for i := range testVectors.Error {
vec := &testVectors.Error[i]
t.Run(fmt.Sprintf("Error %d", i), func(t *testing.T) {
// There's only one test case, and it's signature s11n
// related. Naturally, unlike the other test cases,
// the `error` is missing `contrib`, because fuck you.
require.True(t, vec.Error.Is(typeInvalidContribution, ""))
hdrPSigs, errs := testVectors.PartialSigs()
idx := vec.PSigIndices[*vec.Error.Signer]
require.Nil(t, hdrPSigs[idx])
require.Error(t, errs[idx])
})
}
}