forked from kwonalbert/xrd
/
verifier.go
245 lines (197 loc) · 4.79 KB
/
verifier.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
package mixnet
import (
"encoding/binary"
"errors"
"log"
"math/big"
"runtime"
"sync"
)
type Verifier interface {
NewRound(round int) error
EndRound(round int) error
SetPublicKey(round int, x, y *big.Int) error
PublicKey(round int) (*big.Int, *big.Int, error)
PrivateKey(round int) (*big.Int, error)
AddInnerCiphertexts(round int, msgs [][]byte) error
Finalize(round int, privateKeys [][]byte) ([][]byte, error)
}
type verifier struct {
round int
index int
groupSize int
smu sync.RWMutex
states map[int]*verifierRoundState
}
// internal per round state for verifier
type verifierRoundState struct {
sync.Mutex
round int
// private and public keys of this round
private *big.Int
publicX *big.Int
publicY *big.Int
innerCiphertexts [][]byte
decJobs chan decryptionJob
done bool
}
func NewVerifier(index int, groupSize int) Verifier {
v := &verifier{
round: 0,
index: index,
groupSize: groupSize,
states: make(map[int]*verifierRoundState),
}
return v
}
func (ver *verifier) NewRound(round int) error {
if round < ver.round {
return errors.New("Cannot start previous rounds")
}
ver.smu.Lock()
defer ver.smu.Unlock()
if _, ok := ver.states[round]; ok {
return errors.New("Round already started.")
}
ver.round = round // keep track of the lastest round number
priv, px, py := GenerateInnerKey()
state := &verifierRoundState{
round: round,
private: priv,
publicX: px,
publicY: py,
innerCiphertexts: nil,
decJobs: make(chan decryptionJob, runtime.NumCPU()*16),
done: false,
}
for i := 0; i < runtime.NumCPU()*2; i++ {
go decryptionWorker(round, state.decJobs)
}
ver.states[round] = state
return nil
}
func (ver *verifier) EndRound(round int) error {
if round > ver.round {
return errors.New("Cannot delete future rounds")
}
ver.smu.RLock()
state, ok := ver.states[round]
ver.smu.RUnlock()
if !ok {
return errors.New("Round already deleted")
}
state.Lock()
defer state.Unlock()
if !state.done {
log.Println("Couldn't delete", ver.index)
return errors.New("Cannot delete a round that has not finished")
}
close(state.decJobs)
ver.smu.Lock()
delete(ver.states, round)
ver.smu.Unlock()
return nil
}
func (ver *verifier) SetPublicKey(round int, x, y *big.Int) error {
ver.smu.RLock()
state, ok := ver.states[round]
ver.smu.RUnlock()
if !ok {
return errors.New("Looking for non-existing keys")
}
state.Lock()
state.publicX = x
state.publicY = y
state.Unlock()
return nil
}
func (ver *verifier) PublicKey(round int) (*big.Int, *big.Int, error) {
ver.smu.RLock()
state, ok := ver.states[round]
ver.smu.RUnlock()
if !ok {
return nil, nil, errors.New("Looking for non-existing keys")
}
return state.publicX, state.publicY, nil
}
type decryptionJob struct {
privateKey *big.Int
ciphertext []byte
idx int
errs []error
results [][]byte
wg *sync.WaitGroup
}
func decryptionWorker(round int, jobs chan decryptionJob) {
var nonce [24]byte
binary.PutUvarint(nonce[:], uint64(round))
x, y := new(big.Int), new(big.Int)
for job := range jobs {
ciphertext := job.ciphertext
xb, yb := ciphertext[:32], ciphertext[32:64]
msg := ciphertext[64:]
rx, ry := x.SetBytes(xb), y.SetBytes(yb)
plaintext, err := Decrypt(job.privateKey, &nonce, rx, ry, msg)
job.results[job.idx] = plaintext
job.errs[job.idx] = err
job.wg.Done()
}
}
func (ver *verifier) PrivateKey(round int) (*big.Int, error) {
ver.smu.RLock()
state, ok := ver.states[round]
ver.smu.RUnlock()
if !ok {
return nil, errors.New("Round not yet started")
}
return state.private, nil
}
func (ver *verifier) AddInnerCiphertexts(round int, inners [][]byte) error {
ver.smu.RLock()
state, ok := ver.states[round]
ver.smu.RUnlock()
if !ok {
return errors.New("Round not yet started")
}
state.innerCiphertexts = inners
return nil
}
func (ver *verifier) Finalize(round int, privateKeys [][]byte) ([][]byte, error) {
ver.smu.RLock()
state, ok := ver.states[round]
ver.smu.RUnlock()
if !ok {
return nil, errors.New("Round not yet started")
}
if state.innerCiphertexts == nil {
return nil, errors.New("Cannot finalize without inner ciphertexts")
}
aggKey := big.NewInt(0)
for _, priv := range privateKeys {
aggKey = aggKey.Add(aggKey, new(big.Int).SetBytes(priv))
}
wg := new(sync.WaitGroup)
wg.Add(len(state.innerCiphertexts))
errs := make([]error, len(state.innerCiphertexts))
plaintexts := make([][]byte, len(state.innerCiphertexts))
for c := range state.innerCiphertexts {
state.decJobs <- decryptionJob{
privateKey: aggKey,
ciphertext: state.innerCiphertexts[c],
idx: c,
errs: errs,
results: plaintexts,
wg: wg,
}
}
wg.Wait()
for _, err := range errs {
if err != nil {
return nil, err
}
}
state.Lock()
state.done = true
state.Unlock()
return plaintexts, nil
}