This repository has been archived by the owner on Sep 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 125
/
rounds_test.go
381 lines (339 loc) · 11.2 KB
/
rounds_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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
//
// Copyright Coinbase, Inc. All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
package gennaro
import (
"fmt"
"testing"
"github.com/btcsuite/btcd/btcec"
"github.com/stretchr/testify/require"
"github.com/coinbase/kryptology/pkg/core/curves"
v1 "github.com/coinbase/kryptology/pkg/sharing/v1"
)
func TestParticipantRound1Works(t *testing.T) {
p1, err := NewParticipant(1, 2, testGenerator, curves.NewK256Scalar(), 2)
require.NoError(t, err)
bcast, p2psend, err := p1.Round1(nil)
require.NoError(t, err)
require.NotNil(t, bcast)
require.NotNil(t, p2psend)
require.Equal(t, len(p2psend), 1)
require.Equal(t, len(bcast), 2)
require.NotNil(t, p1.pedersenResult)
require.Equal(t, p1.round, 2)
_, ok := p2psend[2]
require.True(t, ok)
}
func TestParticipantRound1RepeatCall(t *testing.T) {
p1, err := NewParticipant(1, 2, testGenerator, curves.NewK256Scalar(), 2)
require.NoError(t, err)
_, _, err = p1.Round1(nil)
require.NoError(t, err)
_, _, err = p1.Round1(nil)
require.Error(t, err)
}
func TestParticipantRound1BadSecret(t *testing.T) {
p1, err := NewParticipant(1, 2, testGenerator, curves.NewK256Scalar(), 2)
require.NoError(t, err)
// secret == 0
secret := []byte{0}
_, _, err = p1.Round1(secret)
require.Error(t, err)
// secret too big
secret = []byte{7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7}
_, _, err = p1.Round1(secret)
require.Error(t, err)
}
func PrepareRound2Input(t *testing.T) (*Participant, *Participant, Round1Bcast, Round1Bcast, Round1P2PSend) {
// Prepare round 1 output of 2 participants
p1, err := NewParticipant(1, 2, testGenerator, curves.NewK256Scalar(), 2)
require.NoError(t, err)
require.Equal(t, p1.otherParticipantShares[2].Id, uint32(2))
p2, err := NewParticipant(2, 2, testGenerator, curves.NewK256Scalar(), 1)
require.NoError(t, err)
require.Equal(t, p2.otherParticipantShares[1].Id, uint32(1))
bcast1, _, _ := p1.Round1(nil)
bcast2, p2psend2, _ := p2.Round1(nil)
return p1, p2, bcast1, bcast2, p2psend2
}
// Test Gennaro DKG round2 works
func TestParticipantRound2Works(t *testing.T) {
// Prepare Dkg Round 1 output
p1, _, bcast1, bcast2, p2psend2 := PrepareRound2Input(t)
// Actual Test
require.NotNil(t, bcast1)
require.NotNil(t, bcast2)
require.NotNil(t, p2psend2[1])
bcast := make(map[uint32]Round1Bcast)
p2p := make(map[uint32]*Round1P2PSendPacket)
bcast[1] = bcast1
bcast[2] = bcast2
p2p[2] = p2psend2[1]
round2Out, err := p1.Round2(bcast, p2p)
require.NoError(t, err)
require.NotNil(t, round2Out)
require.Equal(t, len(round2Out), 2)
require.NotNil(t, p1.skShare)
require.Equal(t, p1.round, 3)
require.NotNil(t, p1.otherParticipantShares)
}
// Test Gennaro DKG round 2 repeat call
func TestParticipantRound2RepeatCall(t *testing.T) {
// Prepare Dkg Round 1 output
p1, _, bcast1, bcast2, p2psend2 := PrepareRound2Input(t)
// Actual Test
require.NotNil(t, bcast1)
require.NotNil(t, bcast2)
require.NotNil(t, p2psend2[1])
bcast := make(map[uint32]Round1Bcast)
p2p := make(map[uint32]*Round1P2PSendPacket)
bcast[1] = bcast1
bcast[2] = bcast2
p2p[2] = p2psend2[1]
_, err := p1.Round2(bcast, p2p)
require.NoError(t, err)
_, err = p1.Round2(bcast, p2p)
require.Error(t, err)
}
// Test Gennaro Dkg Round 2 Bad Input
func TestParticipantRound2BadInput(t *testing.T) {
// Prepare Dkg Round 1 output
p1, _, _, _, _ := PrepareRound2Input(t)
bcast := make(map[uint32]Round1Bcast)
p2p := make(map[uint32]*Round1P2PSendPacket)
// Test empty bcast and p2p
_, err := p1.Round2(bcast, p2p)
require.Error(t, err)
// Test nil bcast and p2p
p1, _, _, _, _ = PrepareRound2Input(t)
_, err = p1.Round2(nil, nil)
require.Error(t, err)
// Test tampered input bcast and p2p
p1, _, bcast1, bcast2, p2psend2 := PrepareRound2Input(t)
bcast = make(map[uint32]Round1Bcast)
p2p = make(map[uint32]*Round1P2PSendPacket)
// Tamper bcast1 and p2psend2 by doubling their value
bcast1[1].Y = bcast1[1].Y.Add(bcast1[1].Y, bcast1[1].Y)
p2psend2[1].SecretShare.Value = p2psend2[1].SecretShare.Value.Add(p2psend2[1].SecretShare.Value)
bcast[1] = bcast1
bcast[2] = bcast2
p2p[2] = p2psend2[1]
_, err = p1.Round2(bcast, p2p)
require.Error(t, err)
}
func PrepareRound3Input(t *testing.T) (*Participant, *Participant, map[uint32]Round2Bcast) {
p1, _ := NewParticipant(1, 2, testGenerator, curves.NewK256Scalar(), 2)
p2, _ := NewParticipant(2, 2, testGenerator, curves.NewK256Scalar(), 1)
bcast1, p2psend1, _ := p1.Round1(nil)
bcast2, p2psend2, _ := p2.Round1(nil)
bcast := make(map[uint32]Round1Bcast)
p2p1 := make(map[uint32]*Round1P2PSendPacket)
p2p2 := make(map[uint32]*Round1P2PSendPacket)
bcast[1] = bcast1
bcast[2] = bcast2
p2p1[2] = p2psend2[1]
p2p2[1] = p2psend1[2]
round2Out1, _ := p1.Round2(bcast, p2p1)
round2Out2, _ := p2.Round2(bcast, p2p2)
round3Input := make(map[uint32]Round2Bcast)
round3Input[1] = round2Out1
round3Input[2] = round2Out2
return p1, p2, round3Input
}
// Test Gennaro Dkg Round 3 Works
func TestParticipantRound3Works(t *testing.T) {
// Prepare Gennaro Dkg Round 3 Input
p1, p2, round3Input := PrepareRound3Input(t)
// Actual Test
round3Out1, _, err := p1.Round3(round3Input)
require.NoError(t, err)
require.NotNil(t, round3Out1)
round3Out2, _, err := p2.Round3(round3Input)
require.NoError(t, err)
require.NotNil(t, round3Out2)
require.Equal(t, p1.round, 4)
require.Equal(t, p2.round, 4)
require.Equal(t, p1.verificationKey, p2.verificationKey)
// Test if shares recombine properly
s, _ := v1.NewShamir(2, 2, curves.NewField(btcec.S256().N))
sk, err := s.Combine(&v1.ShamirShare{Identifier: p1.id, Value: p1.skShare},
&v1.ShamirShare{Identifier: p2.id, Value: p2.skShare})
require.NoError(t, err)
// Test verification keys are G * sk
x, y := btcec.S256().ScalarBaseMult(sk)
tmp := &curves.EcPoint{
Curve: btcec.S256(),
X: x,
Y: y,
}
require.True(t, tmp.Equals(p1.verificationKey))
require.True(t, tmp.Equals(p2.verificationKey))
}
// Test Gennaro Dkg Round3 Repeat Call
func TestParticipantRound3RepeatCall(t *testing.T) {
// Prepare Round 3 Input
p1, _, round3Input := PrepareRound3Input(t)
// Actual Test
_, _, err := p1.Round3(round3Input)
require.NoError(t, err)
_, _, err = p1.Round3(round3Input)
require.Error(t, err)
}
// Test Gennaro DKG Round 3 Bad Input
func TestParticipantRound3BadInput(t *testing.T) {
// Test empty round 3 input
p1, _, _ := PrepareRound3Input(t)
emptyInput := make(map[uint32]Round2Bcast)
_, _, err := p1.Round3(emptyInput)
require.Error(t, err)
// Test nil round 3 input
p1, _, _ = PrepareRound3Input(t)
_, _, err = p1.Round3(nil)
require.Error(t, err)
// Test tampered round 3 input
p1, _, round3Input := PrepareRound3Input(t)
// Tamper participant2's broadcast
round3Input[2][0], _ = round3Input[2][0].Add(round3Input[2][1])
_, _, err = p1.Round3(round3Input)
require.Error(t, err)
}
// Test Gennaro Dkg Round 4 Works
func TestParticipantRound4Works(t *testing.T) {
// Prepare Gennaro Dkg Round 3 Input
p1, p2, round3Input := PrepareRound3Input(t)
round3Out1, _, err := p1.Round3(round3Input)
require.NoError(t, err)
require.NotNil(t, round3Out1)
round3Out2, _, err := p2.Round3(round3Input)
require.NoError(t, err)
require.NotNil(t, round3Out2)
// Actual test
publicShares1, err := p1.Round4()
require.NoError(t, err)
require.NotNil(t, publicShares1)
publicShares2, err := p2.Round4()
require.NoError(t, err)
require.NotNil(t, publicShares2)
require.Equal(t, publicShares1, publicShares2)
}
// Test Gennaro Dkg Round 4 Works
func TestParticipantRound4RepeatCall(t *testing.T) {
// Prepare Gennaro Dkg Round 3 Input
p1, p2, round3Input := PrepareRound3Input(t)
round3Out1, _, err := p1.Round3(round3Input)
require.NoError(t, err)
require.NotNil(t, round3Out1)
round3Out2, _, err := p2.Round3(round3Input)
require.NoError(t, err)
require.NotNil(t, round3Out2)
// Actual test
publicShares1, err := p1.Round4()
require.NoError(t, err)
require.NotNil(t, publicShares1)
publicShares2, err := p1.Round4()
require.NoError(t, err)
require.NotNil(t, publicShares2)
require.Equal(t, publicShares1, publicShares2)
}
// Test all Gennaro DKG rounds
func TestAllGennaroDkgRounds(t *testing.T) {
// Initiate two participants
p1, _ := NewParticipant(1, 2, testGenerator, curves.NewK256Scalar(), 2)
p2, _ := NewParticipant(2, 2, testGenerator, curves.NewK256Scalar(), 1)
// Running round 1
bcast1, p2psend1, _ := p1.Round1(nil)
bcast2, p2psend2, _ := p2.Round1(nil)
bcast := make(map[uint32]Round1Bcast)
p2p1 := make(map[uint32]*Round1P2PSendPacket)
p2p2 := make(map[uint32]*Round1P2PSendPacket)
bcast[1] = bcast1
bcast[2] = bcast2
p2p1[2] = p2psend2[1]
p2p2[1] = p2psend1[2]
// Running round 2
round2Out1, _ := p1.Round2(bcast, p2p1)
round2Out2, _ := p2.Round2(bcast, p2p2)
round3Input := make(map[uint32]Round2Bcast)
round3Input[1] = round2Out1
round3Input[2] = round2Out2
// Running round 3
round3Out1, _, _ := p1.Round3(round3Input)
round3Out2, _, _ := p2.Round3(round3Input)
require.NotNil(t, round3Out1)
require.NotNil(t, round3Out2)
// Running round 4
publicShares1, _ := p1.Round4()
publicShares2, _ := p2.Round4()
// Test output of all rounds
require.Equal(t, publicShares1, publicShares2)
s, _ := v1.NewShamir(2, 2, curves.NewField(btcec.S256().N))
sk, err := s.Combine(&v1.ShamirShare{Identifier: p1.id, Value: p1.skShare},
&v1.ShamirShare{Identifier: p2.id, Value: p2.skShare})
require.NoError(t, err)
x, y := btcec.S256().ScalarBaseMult(sk)
tmp := &curves.EcPoint{
Curve: btcec.S256(),
X: x,
Y: y,
}
require.True(t, tmp.Equals(p1.verificationKey))
require.True(t, tmp.Equals(p2.verificationKey))
}
// Ensure correct functioning when input is missing
func TestParticipant2BadInput(t *testing.T) {
//
// Setup
//
p1, _ := NewParticipant(1, 2, testGenerator, curves.NewK256Scalar(), 2)
p2, _ := NewParticipant(2, 2, testGenerator, curves.NewK256Scalar(), 1)
bcast1, _, _ := p1.Round1(nil)
bcast2, p2psend2, _ := p2.Round1(nil)
bcast := make(map[uint32]Round1Bcast)
p2p1 := make(map[uint32]*Round1P2PSendPacket)
bcast[1] = bcast1
bcast[2] = bcast2
// Exclude p2p 2>1
p2p1[4] = p2psend2[1]
// Run round 2
_, err := p1.Round2(bcast, p2p1)
require.Error(t, err)
}
func TestValidIDs(t *testing.T) {
err := fmt.Errorf("")
tests := []struct {
name string
in []uint32
expected error
}{
{"positive-1,2", []uint32{1, 2}, nil},
{"positive-2,1", []uint32{2, 1}, nil},
{"positive-1-10", []uint32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, nil},
{"positive-1-10-random", []uint32{10, 9, 6, 5, 3, 2, 8, 7, 1, 4}, nil},
{"negative-1,3", []uint32{1, 3}, err},
{"negative-1-10-missing-5", []uint32{10, 9, 6, 3, 2, 8, 7, 1, 4}, err},
}
// Run all the tests!
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := validIds(test.in)
if test.expected == nil {
require.NoError(t, err)
} else {
require.Error(t, err)
}
})
}
}
// Test newParticipant with arbitrary IDs
func TestParticipantArbitraryIds(t *testing.T) {
_, err := NewParticipant(3, 2, testGenerator, curves.NewK256Scalar(), 4)
require.Error(t, err)
_, err = NewParticipant(0, 2, testGenerator, curves.NewK256Scalar(), 1)
require.Error(t, err)
_, err = NewParticipant(2, 2, testGenerator, curves.NewK256Scalar(), 2, 3, 5)
require.Error(t, err)
_, err = NewParticipant(1, 2, testGenerator, curves.NewK256Scalar(), 4)
require.Error(t, err)
}