/
demo.go
166 lines (147 loc) · 3.35 KB
/
demo.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
package main
import "C"
import (
"encoding/json"
"fmt"
tss "github.com/RockX-SG/bls-tss"
log "github.com/sirupsen/logrus"
"os"
"time"
)
func init() {
log.SetFormatter(&log.TextFormatter{})
//Set log output to standard output (default output is stderr, standard error)
//Log message output can be any io.writer type
log.SetOutput(os.Stdout)
log.SetLevel(log.DebugLevel)
}
func main() {
t := 1
n := 3
var (
ins []chan string
outs []chan string
kMachines []*tss.Keygen
sMachines []*tss.Sign
)
for i := 1; i < n+1; i++ {
in := make(chan string, n)
out := make(chan string, n)
keygen := tss.NewKeygen(i, t, n, in, out)
ins = append(ins, in)
outs = append(outs, out)
kMachines = append(kMachines, keygen)
}
defer func(machines []*tss.Keygen) {
for _, machine := range machines {
machine.Free()
}
}(kMachines)
go func(o1 <-chan string, o2 <-chan string, o3 <-chan string, i1 chan<- string, i2 chan<- string, i3 chan<- string) {
send := func(str string) {
msg := tss.ProtocolMessage{}
if err := json.Unmarshal([]byte(str), &msg); err != nil {
fmt.Printf("error: %v\n", err)
} else {
switch msg.Receiver {
case 0:
if msg.Sender != 1 {
i1 <- str
}
if msg.Sender != 2 {
i2 <- str
}
if msg.Sender != 3 {
i3 <- str
}
case 1:
i1 <- str
case 2:
i2 <- str
case 3:
i3 <- str
}
}
}
for {
select {
case str, ok := <-o1:
if ok {
send(str)
}
case str, ok := <-o2:
if ok {
send(str)
}
case str, ok := <-o3:
if ok {
send(str)
}
case <-time.After(1 * time.Second):
}
}
}(outs[0], outs[1], outs[2], ins[0], ins[1], ins[2])
log.Debug("Starting keygen")
go kMachines[0].ProcessLoop()
go kMachines[1].ProcessLoop()
go kMachines[2].ProcessLoop()
kMachines[0].Initialize()
kMachines[1].Initialize()
kMachines[2].Initialize()
log.Debug("KeygenSimple started")
var allFinished bool
for !allFinished {
select {
case <-time.After(1 * time.Second):
allFinished = true
for _, machine := range kMachines {
allFinished = allFinished && machine.Output() != nil
}
log.Tracef("keygen allFinished: %v\n", allFinished)
if allFinished {
break
}
}
}
log.Debug("KeygenSimple completed")
for _, machine := range kMachines {
log.WithFields(log.Fields{
"result": *machine.Output(),
}).Trace("keygen result")
}
msgHash := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" // 32 * "a"
n = 2
for i := 1; i < n+1; i++ {
sign := tss.NewSign(msgHash, i, n, *kMachines[i-1].Output(), ins[i-1], outs[i-1])
sMachines = append(sMachines, sign)
}
defer func(machines []*tss.Sign) {
for _, machine := range machines {
machine.Free()
}
}(sMachines)
log.Debug("Start signing")
go sMachines[0].ProcessLoop()
go sMachines[1].ProcessLoop()
sMachines[0].Initialize()
sMachines[1].Initialize()
//sMachines[2].Initialize()
log.Debug("Signing started")
allFinished = false
for !allFinished {
select {
case <-time.After(1 * time.Second):
allFinished = true
for _, machine := range sMachines {
allFinished = allFinished && machine.Output() != nil
}
log.Tracef("sign allFinished: %v\n", allFinished)
if allFinished {
break
}
}
}
log.Debug("Signing completed")
log.Infof("msgHash is: %v\n", msgHash)
log.Infof("signature is: %v\n", *sMachines[1].Output())
}