forked from nghuyenthevinh2000/bitcoin-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frost_1_test.go
181 lines (164 loc) · 5.25 KB
/
frost_1_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
package main
import (
"log"
"sync"
"testing"
"time"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/nghuyenthevinh2000/bitcoin-playground/testhelper"
"github.com/stretchr/testify/assert"
)
func TestFrostSignature1(t *testing.T) {
suite := testhelper.TestSuite{}
suite.SetupStaticSimNetSuite(t)
// frost
n := 1000
threshold := 667
participants := make([]*testhelper.FrostParticipant, n)
logger := log.Default()
logger.Println("Starting")
timeNow := time.Now()
var wg sync.WaitGroup
for i := 0; i < n; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
participants[i] = testhelper.NewFrostParticipant(&suite, logger, n, threshold, i+1, nil)
}(i)
}
// Wait for all goroutines to complete
wg.Wait()
logger.Printf("Generating participants takes: %v\n", time.Since(timeNow))
timeNow = time.Now()
// update polynomial commitments
polynomialCommitments := make([][]*btcec.PublicKey, len(participants)+1)
for i := 0; i < len(participants); i++ {
polynomialCommitments[i+1] = participants[i].PolynomialCommitment
participants[i].PolynomialCommitments = polynomialCommitments
}
logger.Printf("UpdatePolynomialCommitments takes: %v\n", time.Since(timeNow))
// generate challenges
timeNow = time.Now()
for i := 0; i < len(participants); i++ {
participant := participants[i]
challenge := participant.CalculateSecretProofs([32]byte{})
participant.VerifySecretProofs([32]byte{}, challenge, i+1, participant.PolynomialCommitments[participant.Position][0])
}
logger.Printf("VerifySecretProofs takes: %v\n", time.Since(timeNow))
// calculate secret shares
timeNow = time.Now()
secretSharesMap := make([]map[int]*btcec.ModNScalar, len(participants)+1)
for i := 0; i < len(participants); i++ {
secretSharesMap[i+1] = make(map[int]*btcec.ModNScalar)
}
for i := 0; i < len(participants); i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
participants[i].CalculateSecretShares()
}(i)
}
wg.Wait()
logger.Printf("CalculateSecretShares takes: %v\n", time.Since(timeNow))
timeNow = time.Now()
for i := 0; i < len(participants); i++ {
for j := 0; j < len(participants); j++ {
secret := participants[i].GetSecretShares(j + 1)
secretSharesMap[j+1][i+1] = secret
}
}
logger.Printf("GetSecretShares takes: %v\n", time.Since(timeNow))
//timeNow = time.Now()
//for i := 0; i < len(participants); i++ {
// participant := participants[i]
// timeNow1 := time.Now()
// for j := 0; j < len(participants); j++ {
// wg.Add(1)
// go func(j int) {
// defer wg.Done()
// secret := secretSharesMap[participant.Position][j+1]
// participant.VerifyPublicSecretShares(secret, j+1)
// }(j)
// }
// wg.Wait()
// logger.Printf("VerifyPublicSecretShares takes: %v\n", time.Since(timeNow1))
//}
//logger.Printf("Total VerifyPublicSecretShares takes: %v\n", time.Since(timeNow))
timeNow = time.Now()
for i := 0; i < 1; i++ {
wg.Add(1)
participant := participants[i]
go func() {
defer wg.Done()
participant.VerifyBatchPublicSecretShares(secretSharesMap[participant.Position])
}()
}
wg.Wait()
logger.Printf("VerifyBatchPublicSecretShares takes: %v\n", time.Since(timeNow))
// calculate public signing shares
timeNow = time.Now()
signingSharesMap := make(map[int]*btcec.ModNScalar)
for i := 0; i < n; i++ {
participant := participants[i]
signingShares := new(btcec.ModNScalar)
for j := 0; j < n; j++ {
secret := secretSharesMap[participant.Position][j+1]
signingShares.Add(secret)
}
signingSharesMap[participant.Position] = signingShares
}
logger.Printf("signingSharesMap takes: %v\n", time.Since(timeNow))
// calculate public signing shares
timeNow = time.Now()
for i := 0; i < len(participants); i++ {
participant := participants[i]
wg.Add(1)
go func() {
defer wg.Done()
participant.CalculateInternalPublicSigningShares(signingSharesMap[participant.Position])
}()
}
wg.Wait()
logger.Printf("CalculateInternalPublicSigningShares takes: %v\n", time.Since(timeNow))
timeNow = time.Now()
aggregateCommitments := make([]*btcec.JacobianPoint, threshold+1)
for j := 0; j <= threshold; j++ {
wg.Add(1)
agg := new(btcec.JacobianPoint)
go func(j int) {
defer wg.Done()
// parallel computation
// \prod_{j=0}^{t} A_mj^i^j
for i := 1; i <= len(participants); i++ {
// A_mj
A_ij := new(btcec.JacobianPoint)
polynomialCommitments[i][j].AsJacobian(A_ij)
btcec.AddNonConst(agg, A_ij, agg)
}
}(j)
wg.Wait()
aggregateCommitments[j] = agg
}
logger.Printf("aggregateCommitments takes: %v\n", time.Since(timeNow))
// All the same for other participants
timeNow = time.Now()
for i := 0; i < 1; i++ {
participant := participants[i]
// calculate public signing shares of other participants
for j := 0; j < n; j++ {
participant.CalculatePublicSigningShares(aggregateCommitments, j+1)
}
}
logger.Printf("CalculatePublicSigningShares takes: %v\n", time.Since(timeNow))
// verify correct calculation of public signing shares
// All the same for other participants
timeNow = time.Now()
for i := 0; i < 1; i++ {
participant := participants[i]
// verify public signing shares of other participants
for j := 0; j < n; j++ {
assert.Equal(t, participant.PublicSigningShares[j+1], participants[j].PublicSigningShares[j+1])
}
}
logger.Printf("Comparing takes: %v\n", time.Since(timeNow))
}