forked from tnunamak/multisig-decrypt-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ibm.go
312 lines (254 loc) · 7.2 KB
/
ibm.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
package main
import (
"context"
"crypto/ecdsa"
"crypto/sha256"
"crypto/x509"
"encoding/hex"
"fmt"
"log"
"sync"
"time"
ecdsa_scheme "github.com/IBM/TSS/mpc/binance/ecdsa"
"github.com/IBM/TSS/threshold"
. "github.com/IBM/TSS/types"
)
// SimpleLogger implements the Logger interface
type SimpleLogger struct{}
func (l *SimpleLogger) Debugf(format string, a ...interface{}) {
// Disable this for now
//log.Printf("DEBUG: "+format, a...)
}
func (l *SimpleLogger) Infof(format string, a ...interface{}) {
log.Printf("INFO: "+format, a...)
}
func (l *SimpleLogger) Warnf(format string, a ...interface{}) {
log.Printf("WARN: "+format, a...)
}
func (l *SimpleLogger) Errorf(format string, a ...interface{}) {
log.Printf("ERROR: "+format, a...)
}
func (l *SimpleLogger) DebugEnabled() bool {
return true
}
// Global channel to simulate network communication
var messageChan = make(chan Message, 100)
type Message struct {
From uint16
To []uint16
MsgType uint8
Topic []byte
Data []byte
}
func setupScheme(id uint16, totalParties, partyThreshold int) MpcParty {
logger := &SimpleLogger{}
membership := func() map[UniversalID]PartyID {
m := make(map[UniversalID]PartyID)
for i := uint16(1); i <= uint16(totalParties); i++ {
m[UniversalID(i)] = PartyID(i)
}
return m
}
send := func(msgType uint8, topic []byte, msg []byte, to ...uint16) {
messageChan <- Message{
From: id,
To: to,
MsgType: msgType,
Topic: topic,
Data: msg,
}
}
kgf := func(id uint16) KeyGenerator {
return ecdsa_scheme.NewParty(id, logger)
}
sf := func(id uint16) Signer {
return ecdsa_scheme.NewParty(id, logger)
}
return threshold.LoudScheme(id, logger, kgf, sf, partyThreshold, send, membership)
}
func runDKG(scheme MpcParty, totalParties, threshold int) ([]byte, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
secretData, err := scheme.KeyGen(ctx, totalParties, threshold)
if err != nil {
return nil, fmt.Errorf("DKG failed: %v", err)
}
return secretData, nil
}
func sign(scheme MpcParty, message string) ([]byte, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
msgHash := sha256.Sum256([]byte(message))
// Use a unique topic for each signing session
topic := fmt.Sprintf("signing-topic-%s", hex.EncodeToString(msgHash[:8]))
signature, err := scheme.Sign(ctx, msgHash[:], topic)
if err != nil {
return nil, fmt.Errorf("signing failed: %v", err)
}
return signature, nil
}
func handleMessages(schemes []MpcParty) {
for msg := range messageChan {
for _, to := range msg.To {
if int(to) <= len(schemes) {
schemes[to-1].HandleMessage(&IncMessage{
Source: msg.From,
MsgType: msg.MsgType,
Topic: msg.Topic,
Data: msg.Data,
})
}
}
}
}
func verifySignature(scheme MpcParty, message string, signature []byte) bool {
msgHash := sha256.Sum256([]byte(message))
pubKeyBytes, err := scheme.ThresholdPK()
if err != nil {
log.Printf("Failed to get public key: %v", err)
return false
}
pubKey, err := x509.ParsePKIXPublicKey(pubKeyBytes)
if err != nil {
log.Printf("Failed to parse public key: %v", err)
return false
}
ecdsaPubKey, ok := pubKey.(*ecdsa.PublicKey)
if !ok {
log.Printf("Public key is not an ECDSA key")
return false
}
return ecdsa.VerifyASN1(ecdsaPubKey, msgHash[:], signature)
}
func refreshShares(schemes []MpcParty, totalParties, threshold int) error {
log.Println("Starting share refresh process")
var wg sync.WaitGroup
errorChan := make(chan error, len(schemes))
for i := range schemes {
wg.Add(1)
go func(i int) {
defer wg.Done()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
// Use a unique topic for each refresh session
topic := fmt.Sprintf("refresh-topic-%d", time.Now().UnixNano())
// Perform a new DKG to simulate refresh, using the unique topic
newShareData, err := schemes[i].KeyGen(ctx, totalParties, threshold)
if err != nil {
errorChan <- fmt.Errorf("share refresh failed for party %d: %v", i+1, err)
return
}
// Update the stored data with the new share
schemes[i].SetStoredData(newShareData)
log.Printf("Party %d completed share refresh with topic %s", i+1, topic)
}(i)
}
wg.Wait()
close(errorChan)
// Check for any errors
for err := range errorChan {
if err != nil {
return err
}
}
log.Println("Share refresh process completed successfully")
return nil
}
func main() {
totalParties := 3
threshold := 2
// Set up schemes for each party
schemes := make([]MpcParty, totalParties)
for i := 0; i < totalParties; i++ {
schemes[i] = setupScheme(uint16(i+1), totalParties, threshold)
}
// Start the message handler
go handleMessages(schemes)
// Run initial DKG
if err := runDKGForAllParties(schemes, totalParties, threshold); err != nil {
log.Fatalf("Initial DKG failed: %v", err)
}
// Sign a message
message := "Hello, World!"
signatures, err := signWithAllParties(schemes, message)
if err != nil {
log.Fatalf("Signing failed: %v", err)
}
// Verify signatures
for i, sig := range signatures {
isValid := verifySignature(schemes[0], message, sig)
log.Printf("Signature from party %d is valid: %v", i+1, isValid)
}
// Refresh shares
if err := refreshShares(schemes, totalParties, threshold); err != nil {
log.Fatalf("Share refresh failed: %v", err)
}
// Sign a new message after refresh to verify everything still works
newMessage := "Hello after refresh!"
newSignatures, err := signWithAllParties(schemes, newMessage)
if err != nil {
log.Fatalf("Signing after refresh failed: %v", err)
}
// Verify new signatures
for i, sig := range newSignatures {
isValid := verifySignature(schemes[0], newMessage, sig)
log.Printf("Signature after refresh from party %d is valid: %v", i+1, isValid)
}
}
// Helper function to run DKG for all parties
func runDKGForAllParties(schemes []MpcParty, totalParties, threshold int) error {
var wg sync.WaitGroup
errorChan := make(chan error, len(schemes))
for i := range schemes {
wg.Add(1)
go func(i int) {
defer wg.Done()
secretData, err := runDKG(schemes[i], totalParties, threshold)
if err != nil {
errorChan <- fmt.Errorf("DKG failed for party %d: %v", i+1, err)
return
}
schemes[i].SetStoredData(secretData)
log.Printf("Party %d completed DKG", i+1)
}(i)
}
wg.Wait()
close(errorChan)
for err := range errorChan {
if err != nil {
return err
}
}
return nil
}
func signWithAllParties(schemes []MpcParty, message string) ([][]byte, error) {
var wg sync.WaitGroup
signatureChan := make(chan []byte, len(schemes))
errorChan := make(chan error, len(schemes))
for i := range schemes {
wg.Add(1)
go func(i int) {
defer wg.Done()
signature, err := sign(schemes[i], message)
if err != nil {
errorChan <- fmt.Errorf("signing failed for party %d: %v", i+1, err)
return
}
signatureChan <- signature
log.Printf("Party %d completed signing", i+1)
}(i)
}
wg.Wait()
close(signatureChan)
close(errorChan)
var signatures [][]byte
for sig := range signatureChan {
signatures = append(signatures, sig)
}
for err := range errorChan {
if err != nil {
return nil, err
}
}
return signatures, nil
}