-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy paththreshold_test.go
482 lines (446 loc) · 13.4 KB
/
threshold_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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
package goar
import (
"crypto"
"crypto/rsa"
"crypto/sha256"
"encoding/json"
"os"
"testing"
"github.com/everFinance/goar/utils"
tcrsa "github.com/everFinance/ttcrsa"
"github.com/stretchr/testify/assert"
)
// TestCreateKeyPair Secret key creation and threshold signature
func TestCreateTcKeyPair(t *testing.T) {
exampleData := []byte("aaabbbcccddd112233")
signHashed := sha256.Sum256(exampleData)
salt := sha256.Sum256([]byte("everHash salt aaa"))
/* -------------------------- Key pair that generates RSA threshold signature on the server side ----------------------------*/
bitSize := 1024 // If the values are 2048 and 4096, then the generation functions below will perform minute-level times, and we need 4096 bits as the maximum safety level for production environments.
l := 5
k := 3
// Keyshares are distributed to each signer, and KeyMeta stores publicKey, k, l and other public information, which should be sent to the signer together
keyShares, keyMeta, err := CreateTcKeyPair(bitSize, k, l)
if err != nil {
panic(err)
}
ts, err := NewTcSign(keyMeta, exampleData, salt[:])
if err != nil {
panic(err)
}
/* -------------------------- Distribute KeyShare to the signatories ----------------------------*/
signer01 := keyShares[0]
signer02 := keyShares[1]
signer03 := keyShares[2]
signer04 := keyShares[3]
signer05 := keyShares[4]
/* -------------------------- Each signer signs the data received and submits it to the server ----------------------------*/
// sign data
signedData01, err := ts.ThresholdSign(signer01)
if err != nil {
panic(err)
}
signedData02, err := ts.ThresholdSign(signer02)
if err != nil {
panic(err)
}
signedData03, err := ts.ThresholdSign(signer03)
if err != nil {
panic(err)
}
signedData04, err := ts.ThresholdSign(signer04)
if err != nil {
panic(err)
}
signedData05, err := ts.ThresholdSign(signer05)
if err != nil {
panic(err)
}
/* -------------------------- After receiving the signature data submitted by the signers, the server verifies the signature and assembles the signature ----------------------------*/
signedShares := tcrsa.SigShareList{
signedData01,
signedData02,
signedData03,
signedData04,
signedData05,
}
ts, err = NewTcSign(keyMeta, exampleData, salt[:])
assert.NoError(t, err)
signature, err := ts.AssembleSigShares(signedShares)
if err != nil {
panic(err)
}
err = utils.Verify(exampleData, keyMeta.PublicKey, signature)
if err != nil {
panic(err)
}
/* --------------------------------------------------------------------------------------------------------------*/
/* -------------------------- Next, we will test the threshold based on the above environment ----------------------------*/
// As can be seen from the above, L =5, K =3, there are 5 signers, and the threshold is 3.
// However, in the above process, we have submitted the signatures of all 5 signers, and the goal of threshold test has not been reached.
// The threshold test is as follows
// 1. Submit the signature data of signer1,2,3 and verify it
signedShares123 := tcrsa.SigShareList{
signedData01,
signedData02,
signedData03,
}
// assemble
signature123, err := ts.AssembleSigShares(signedShares123)
if err != nil {
panic(err)
}
// verify
err = rsa.VerifyPSS(keyMeta.PublicKey, crypto.SHA256, signHashed[:], signature123, nil)
if err != nil {
panic(err)
}
// 2. Submit signer 3,2,1
signedShares321 := tcrsa.SigShareList{
signedData03,
signedData02,
signedData01,
}
// assemble
signature321, err := ts.AssembleSigShares(signedShares321)
if err != nil {
panic(err)
}
// verify
err = rsa.VerifyPSS(keyMeta.PublicKey, crypto.SHA256, signHashed[:], signature321, nil)
if err != nil {
panic(err)
}
// 3. Submit signer 3,1,2
signedShares312 := tcrsa.SigShareList{
signedData03,
signedData01,
signedData02,
}
// assemble
signature312, err := ts.AssembleSigShares(signedShares312)
if err != nil {
panic(err)
}
// verify
err = rsa.VerifyPSS(keyMeta.PublicKey, crypto.SHA256, signHashed[:], signature312, nil)
if err != nil {
panic(err)
}
// 4. Submit signer 1,3,5
signedShares135 := tcrsa.SigShareList{
signedData01,
signedData03,
signedData05,
}
// assemble
signature135, err := ts.AssembleSigShares(signedShares135)
if err != nil {
panic(err)
}
// verify
err = rsa.VerifyPSS(keyMeta.PublicKey, crypto.SHA256, signHashed[:], signature135, nil)
if err != nil {
panic(err)
}
// 5. Submit signer 5, 1, 4
signedShares514 := tcrsa.SigShareList{
signedData05,
signedData01,
signedData04,
}
// assemble
signature514, err := ts.AssembleSigShares(signedShares514)
if err != nil {
panic(err)
}
// verify
err = rsa.VerifyPSS(keyMeta.PublicKey, crypto.SHA256, signHashed[:], signature514, nil)
if err != nil {
panic(err)
}
// 6. Submit signer 2,3,4,5
signedShares2345 := tcrsa.SigShareList{
signedData02,
signedData03,
signedData04,
signedData05,
}
// assemble
signature2345, err := ts.AssembleSigShares(signedShares2345)
if err != nil {
panic(err)
}
// verify
err = rsa.VerifyPSS(keyMeta.PublicKey, crypto.SHA256, signHashed[:], signature2345, nil)
if err != nil {
panic(err)
}
// 7. Submit signer 5,4,2,3
signedShares5423 := tcrsa.SigShareList{
signedData05,
signedData04,
signedData02,
signedData03,
}
// assemble
signature5423, err := ts.AssembleSigShares(signedShares5423)
if err != nil {
panic(err)
}
// verify
err = rsa.VerifyPSS(keyMeta.PublicKey, crypto.SHA256, signHashed[:], signature5423, nil)
if err != nil {
panic(err)
}
// 8. Submit 5, 4, 3,2,1
signedShares54321 := tcrsa.SigShareList{
signedData05,
signedData04,
signedData03,
signedData02,
signedData01,
}
// assemble
signature54321, err := ts.AssembleSigShares(signedShares54321)
if err != nil {
panic(err)
}
// verify
err = rsa.VerifyPSS(keyMeta.PublicKey, crypto.SHA256, signHashed[:], signature54321, nil)
if err != nil {
panic(err)
}
// 9. Submit 4,3
signedShares43 := tcrsa.SigShareList{
signedData04,
signedData03,
}
// assemble
_, err = ts.AssembleSigShares(signedShares43)
assert.EqualError(t, err, "insufficient number of signature shares. provided: 2, needed: 3")
/*
Verify that the refactored signature data is committed.
So let's start with the conclusion:
threshold k=3, Pass the test [a,b,c,x,x,x]; [a,b,c,d,x,x];
Fail test [a,a,b,c,d,e,x,x,x]; [a,b,b,c,x,x,x];
why: No matter how many signatures you submit, the Join method only takes the first 3(k) pieces of the signedShares array to assemble the final signature.
*/
// 10. Submit 1,1,3,4; Is the signature of the assembly data signer01 signer01, signer03, so only two valid signatures, below the threshold
signedShares1134 := tcrsa.SigShareList{
signedData01,
signedData01,
signedData03,
signedData04,
}
// assemble
signature1134, err := ts.AssembleSigShares(signedShares1134)
assert.EqualError(t, err, "crypto/rsa: verification error")
// verify
err = rsa.VerifyPSS(keyMeta.PublicKey, crypto.SHA256, signHashed[:], signature1134, nil)
assert.EqualError(t, err, "crypto/rsa: verification error")
// 11. submit 1,2,2,3; Same thing as above
signedShares1223 := tcrsa.SigShareList{
signedData01,
signedData02,
signedData02,
signedData03,
}
// assemble
signature1223, err := ts.AssembleSigShares(signedShares1223)
assert.EqualError(t, err, "crypto/rsa: verification error")
// verify
err = rsa.VerifyPSS(keyMeta.PublicKey, crypto.SHA256, signHashed[:], signature1223, nil)
assert.EqualError(t, err, "crypto/rsa: verification error")
// 12. submit 3,2,5,5,5,4,2,1,3;Can get 3, 2 and 5 signature data to meet the threshold value
signedShares325554213 := tcrsa.SigShareList{
signedData03,
signedData02,
signedData05,
signedData05,
signedData05,
signedData04,
signedData02,
signedData01,
signedData03,
}
// assemble
signature325554213, err := ts.AssembleSigShares(signedShares325554213)
if err != nil {
panic(err)
}
// verify
err = rsa.VerifyPSS(keyMeta.PublicKey, crypto.SHA256, signHashed[:], signature325554213, nil)
if err != nil {
panic(err)
}
}
// GetKeyPairByLocal
func GetKeyPairFormLocalFile() (shares tcrsa.KeyShareList, meta *tcrsa.KeyMeta, err error) {
dd, err := os.ReadFile("keyMeta.json")
if err != nil {
return nil, nil, err
}
ee, err := os.ReadFile("keyShares.json")
if err != nil {
return nil, nil, err
}
keyMeta := &tcrsa.KeyMeta{}
err = json.Unmarshal(dd, keyMeta)
if err != nil {
return nil, nil, err
}
keyShares := tcrsa.KeyShareList{}
err = json.Unmarshal(ee, &keyShares)
if err != nil {
return nil, nil, err
}
return keyShares, keyMeta, nil
}
// TestCreateKeyPair3 get address
// func TestCreateKeyPair3(t *testing.T) {
// keyMeta := &tcrsa.KeyMeta{}
// keyMetaBy, err := os.ReadFile("keyMeta.json") // replace your key
// assert.NoError(t, err)
// err = json.Unmarshal(keyMetaBy, keyMeta)
// assert.NoError(t, err)
// addr := sha256.Sum256(keyMeta.PublicKey.N.Bytes())
// t.Log("address: ", utils.Base64Encode(addr[:])) // KKzL8og7VFLNwxbwW6cpUY_WkE5jFjWL26cTvKfWYms
// }
// TestCreateKeyPair2 send ar tx by threshold signature keypair
func TestCreateKeyPair2(t *testing.T) {
// cli := client.New("https://arweave.net")
//
// target := "Ii5wAMlLNz13n26nYY45mcZErwZLjICmYd46GZvn4ck"
// reward, err := cli.GetTransactionPrice(nil, &target)
// assert.NoError(t, err)
// // anchor, err := cli.GetTransactionAnchor() // for test
// anchor, err := cli.GetLastTransactionID("KKzL8og7VFLNwxbwW6cpUY_WkE5jFjWL26cTvKfWYms")
// assert.NoError(t, err)
// t.Log("lastTx: ", anchor)
// // read created threshold keypair for local file; need to be generated ahead of time;
// keyMeta := &tcrsa.KeyMeta{}
// keyMetaBy, err := os.ReadFile("keyMeta.json")
// assert.NoError(t, err)
// err = json.Unmarshal(keyMetaBy, keyMeta)
// assert.NoError(t, err)
//
// owner := utils.Base64Encode(keyMeta.PublicKey.N.Bytes())
//
// amount := big.NewInt(140000) // transfer amount
// tags := []types.Tag{{Name: "Content-Type", Value: "application/json"}, {Name: "tcrsa", Value: "sandyTest"}}
// tx := &types.Transaction{
// Format: 2,
// ID: "",
// LastTx: anchor,
// Owner: owner,
// Tags: types.TagsEncode(tags),
// Target: target,
// Quantity: amount.String(),
// Data: "",
// DataSize: "0",
// DataRoot: "",
// Reward: fmt.Sprintf("%d", reward),
// Signature: "",
// Chunks: nil,
// }
// signData, err := types.GetSignatureData(tx)
// assert.NoError(t, err)
// t.Log("signData: ", signData)
//
// // signature
// keyShares := tcrsa.KeyShareList{}
// keySharesBy, err := os.ReadFile("keyShares.json")
// assert.NoError(t, err)
// err = json.Unmarshal(keySharesBy, &keyShares)
// assert.NoError(t, err)
//
// ts, err := NewTcSign(keyMeta, signData)
// assert.NoError(t, err)
//
// /* --------------------------distribute keyShares to the signers ----------------------------*/
// signer01 := keyShares[0]
// signer02 := keyShares[1]
// signer03 := keyShares[2]
// signer04 := keyShares[3]
// signer05 := keyShares[4]
//
// /* -------------------------- signers to sign data ----------------------------*/
// signedData01, err := ts.ThresholdSign(signer01)
// if err != nil {
// panic(err)
// }
// t.Log(signedData01.Id)
// bb, _ := json.Marshal(signedData01)
// t.Log(hex.EncodeToString(bb))
//
// signedData02, err := ts.ThresholdSign(signer02)
// if err != nil {
// panic(err)
// }
// t.Log(signedData02.Id)
// bb, _ = json.Marshal(signedData02)
// t.Log(hex.EncodeToString(bb))
//
// signedData03, err := ts.ThresholdSign(signer03)
// if err != nil {
// panic(err)
// }
// t.Log(signedData03.Id)
// bb, _ = json.Marshal(signedData03)
// t.Log(hex.EncodeToString(bb))
//
// signedData04, err := ts.ThresholdSign(signer04)
// if err != nil {
// panic(err)
// }
// t.Log(signedData04.Id)
// bb, _ = json.Marshal(signedData04)
// t.Log(hex.EncodeToString(bb))
//
// signedData05, err := ts.ThresholdSign(signer05)
// if err != nil {
// panic(err)
// }
// t.Log(signedData05.Id)
// bb, _ = json.Marshal(signedData05)
// t.Log(hex.EncodeToString(bb))
//
// /* -------------------------- After receiving the signature data submitted by the signers, the server verifies the signature and assembles the signature ----------------------------*/
// // Collect the signer's signature data into an array
// signedShares := tcrsa.SigShareList{
// // signedData01,
// signedData02,
// signedData03,
// signedData04,
// // signedData05,
// }
//
// // Verify the signature of each collected signer. And what happens in practice is that the server receives the signature submitted by the signer and then it verifies it and then it puts it in the array above
// for _, sd := range signedShares {
// err = sd.Verify(ts.pssData, keyMeta)
// if err != nil {
// panic(err)
// }
// }
//
// // assemble signatures
// signature, err := ts.AssembleSigShares(signedShares)
// if err != nil {
// panic(err)
// }
// // Finally, RSA native PSS verification signature method is used to verify the aggregated signature
// signHashed := sha256.Sum256(signData)
// err = rsa.VerifyPSS(keyMeta.PublicKey, crypto.SHA256, signHashed[:], signature, nil)
// if err != nil {
// panic(err)
// }
// // assemble tx and send to ar chain
// tx.AddSignature(signature)
// t.Log("txHash: ", tx.ID)
//
// status, code, err := cli.SubmitTransaction(tx)
// assert.NoError(t, err)
// t.Log("status: ", status)
// t.Log("code: ", code)
}