-
Notifications
You must be signed in to change notification settings - Fork 22
/
player.go
439 lines (366 loc) · 9.45 KB
/
player.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
//
// garbler.go
//
// Copyright (c) 2019-2023 Markku Rossi
//
// All rights reserved.
//
package circuit
import (
"crypto/rand"
"fmt"
"math/big"
"github.com/markkurossi/mpc/ot"
"github.com/markkurossi/mpc/p2p"
)
// Player runs the BMR protocol client on the P2P network.
func Player(nw *p2p.Network, circ *Circuit, inputs *big.Int, verbose bool) (
[]*big.Int, error) {
numPlayers := len(nw.Peers) + 1
player := nw.ID
timing := NewTiming()
if verbose {
fmt.Printf(" - Garbling...\n")
}
var key [32]byte
_, err := rand.Read(key[:])
if err != nil {
return nil, err
}
garbled, err := circ.Garble(key[:])
if err != nil {
return nil, err
}
timing.Sample("Garble", nil)
// The protocol for Fgc (Protocol 3.1)
// Step 1: Compute Lambda{u,v} for each non-XOR gate.
if verbose {
fmt.Printf(" - Step 1: compute Luv\n")
}
lu := new(big.Int)
lv := new(big.Int)
for g, gate := range circ.Gates {
switch gate.Op {
case XOR, XNOR:
case INV:
default:
lu.SetBit(lu, g, garbled.Lambda(gate.Input0))
lv.SetBit(lv, g, garbled.Lambda(gate.Input1))
}
}
// OTs with peers.
lambdaResults := make(chan OTLambdaResult)
for peerID, peer := range nw.Peers {
go func(peerID int, peer *p2p.Peer) {
x1, result, err := func(peer *p2p.Peer) (
*big.Int, *big.Int, error) {
// Random X1
buf := make([]byte, len(circ.Gates)/8+1)
_, err := rand.Read(buf[:])
if err != nil {
return nil, nil, err
}
x1 := new(big.Int).SetBytes(buf)
shift := len(buf)*8 - len(circ.Gates)
x1.Rsh(x1, uint(shift))
// X2.
x2 := new(big.Int).Xor(lu, x1)
result, err := peer.OTLambda(len(circ.Gates), lv, x1, x2)
if err != nil {
return nil, nil, err
}
return x1, result, nil
}(peer)
lambdaResults <- OTLambdaResult{
peerID: peerID,
x1: x1,
result: result,
err: err,
}
}(peerID, peer)
}
// Compute lu AND lv.
luv := new(big.Int)
luv.And(lu, lv)
for i := 0; i < len(nw.Peers); i++ {
result := <-lambdaResults
if result.err != nil {
return nil, fmt.Errorf("OT-Lambda with peer %d failed: %s",
result.peerID, result.err)
}
luv.Xor(luv, result.x1)
luv.Xor(luv, result.result)
}
fmt.Printf("Luv: %s\n", luv.Text(2))
ioStats := nw.Stats().Sum()
timing.Sample("Fgc Step 1", []string{FileSize(ioStats).String()})
// Step 2: generate XOR shares of Luvw
if verbose {
fmt.Printf(" - Step 2: generate XOR shares of Luvw\n")
}
// Init new gate values.
Gs := NewGateValues(circ.NumGates, numPlayers, nw.ID)
Ag := new(big.Int)
Bg := new(big.Int)
Cg := new(big.Int)
for g, gate := range circ.Gates {
switch gate.Op {
case XOR, XNOR:
case INV:
default:
tmp := luv.Bit(g) ^ garbled.Lambda(gate.Output)
Ag.SetBit(Ag, g, tmp)
if tmp != 0 {
Gs.Ag[player][g].Xor(garbled.R)
Gs.Dg[player][g].Xor(garbled.R)
}
Bg.SetBit(Bg, g, tmp^garbled.Lambda(gate.Input0))
if tmp^garbled.Lambda(gate.Input0) != 0 {
Gs.Bg[player][g].Xor(garbled.R)
Gs.Dg[player][g].Xor(garbled.R)
}
Cg.SetBit(Cg, g, tmp^garbled.Lambda(gate.Input1))
if tmp^garbled.Lambda(gate.Input1) != 0 {
Gs.Cg[player][g].Xor(garbled.R)
Gs.Dg[player][g].Xor(garbled.R)
}
Gs.Dg[player][g].Xor(garbled.R)
}
}
X1LongAg := make([][]ot.Label, numPlayers)
X1LongBg := make([][]ot.Label, numPlayers)
X1LongCg := make([][]ot.Label, numPlayers)
X2LongAg := make([][]ot.Label, numPlayers)
X2LongBg := make([][]ot.Label, numPlayers)
X2LongCg := make([][]ot.Label, numPlayers)
for peerID := range nw.Peers {
X1LongAg[peerID] = make([]ot.Label, len(circ.Gates))
X1LongBg[peerID] = make([]ot.Label, len(circ.Gates))
X1LongCg[peerID] = make([]ot.Label, len(circ.Gates))
X2LongAg[peerID] = make([]ot.Label, len(circ.Gates))
X2LongBg[peerID] = make([]ot.Label, len(circ.Gates))
X2LongCg[peerID] = make([]ot.Label, len(circ.Gates))
for g, gate := range circ.Gates {
switch gate.Op {
case XOR, XNOR:
case INV:
default:
rand1, err := ot.NewLabel(rand.Reader)
if err != nil {
return nil, err
}
X1LongAg[peerID][g] = rand1
Gs.Ag[player][g].Xor(rand1)
rand2, err := ot.NewLabel(rand.Reader)
if err != nil {
return nil, err
}
X1LongBg[peerID][g] = rand2
Gs.Bg[player][g].Xor(rand2)
rand3, err := ot.NewLabel(rand.Reader)
if err != nil {
return nil, err
}
X1LongCg[peerID][g] = rand3
Gs.Cg[player][g].Xor(rand3)
Gs.Dg[player][g].Xor(rand1)
Gs.Dg[player][g].Xor(rand2)
Gs.Dg[player][g].Xor(rand3)
X2LongAg[peerID][g] = garbled.R
X2LongAg[peerID][g].Xor(rand1)
X2LongBg[peerID][g] = garbled.R
X2LongBg[peerID][g].Xor(rand2)
X2LongCg[peerID][g] = garbled.R
X2LongCg[peerID][g].Xor(rand3)
}
}
}
timing.Sample("Fgc Step 2", nil)
// Step 3: generate XOR shares of Rj
if verbose {
fmt.Printf(" - Step 3: generate XOR shares or Rj\n")
}
// OTs with peers.
rResults := make(chan OTRResult)
for peerID, peer := range nw.Peers {
go func(peerID int, peer *p2p.Peer) {
ra, rb, rc, err := peer.OTR(Ag, Bg, Cg,
X1LongAg[peerID], X2LongAg[peerID],
X1LongBg[peerID], X2LongBg[peerID],
X1LongCg[peerID], X2LongCg[peerID])
rResults <- OTRResult{
peerID: peerID,
Ra: ra,
Rb: rb,
Rc: rc,
err: err,
}
}(peerID, peer)
}
for i := 0; i < len(nw.Peers); i++ {
result := <-rResults
if result.err != nil {
return nil, fmt.Errorf("OT-R with peer %d failed: %s",
result.peerID, result.err)
}
for g, gate := range circ.Gates {
switch gate.Op {
case XOR, XNOR:
case INV:
default:
Gs.Ag[result.peerID][g].Xor(result.Ra[g])
Gs.Bg[result.peerID][g].Xor(result.Rb[g])
Gs.Cg[result.peerID][g].Xor(result.Rc[g])
Gs.Dg[result.peerID][g].Xor(result.Ra[g])
Gs.Dg[result.peerID][g].Xor(result.Rb[g])
Gs.Dg[result.peerID][g].Xor(result.Rc[g])
}
}
}
xfer := nw.Stats().Sum() - ioStats
ioStats = nw.Stats().Sum()
timing.Sample("Fgc Step 3", []string{FileSize(xfer).String()})
// Step 4: generate final secrets
if verbose {
fmt.Printf(" - Step 4: exchange gates\n")
}
// Output wire lambdas.
Lo := new(big.Int)
for w := 0; w < circ.Outputs.Size(); w++ {
Lo.SetBit(Lo, w,
garbled.Lambda(Wire(circ.NumWires-circ.Outputs.Size()+w)))
}
// Exchange gates with peers.
gResultsC := make(chan GateResults)
for peerID, peer := range nw.Peers {
go func(peerID int, peer *p2p.Peer) {
ra, rb, rc, rd, ro, err := peer.ExchangeGates(
Gs.Ag, Gs.Bg, Gs.Cg, Gs.Dg, Lo)
gResultsC <- GateResults{
peerID: peerID,
Ra: ra,
Rb: rb,
Rc: rc,
Rd: rd,
Ro: ro,
err: err,
}
}(peerID, peer)
}
var gResults []GateResults
for i := 0; i < len(nw.Peers); i++ {
gResults = append(gResults, <-gResultsC)
}
for _, result := range gResults {
if result.err != nil {
return nil, fmt.Errorf("gate exchange with peer %d failed: %s",
result.peerID, result.err)
}
for p := 0; p < numPlayers; p++ {
for g, gate := range circ.Gates {
switch gate.Op {
case XOR, XNOR:
case INV:
default:
Gs.Ag[p][g].Xor(result.Ra[p][g])
Gs.Bg[p][g].Xor(result.Rb[p][g])
Gs.Cg[p][g].Xor(result.Rc[p][g])
Gs.Dg[p][g].Xor(result.Rd[p][g])
}
}
}
for w := 0; w < circ.Outputs.Size(); w++ {
index := circ.NumWires - circ.Outputs.Size() + w
// XOR peer lambda bit with our lambda bit i.e. only
// result 1 changes our value.
if result.Ro.Bit(index) == 1 {
if garbled.Lambda(Wire(index)) == 0 {
garbled.SetLambda(Wire(index), 1)
} else {
garbled.SetLambda(Wire(index), 0)
}
}
}
}
for i := 0; i < numPlayers; i++ {
gates:
for g, gate := range circ.Gates {
switch gate.Op {
case XOR, XNOR:
case INV:
default:
fmt.Printf("%d:%d: Ag: %s\n", i, g, Gs.Ag[i][g])
fmt.Printf("%d:%d: Bg: %s\n", i, g, Gs.Bg[i][g])
fmt.Printf("%d:%d: Cg: %s\n", i, g, Gs.Cg[i][g])
fmt.Printf("%d:%d: Dg: %s\n", i, g, Gs.Dg[i][g])
break gates
}
}
}
xfer = nw.Stats().Sum() - ioStats
timing.Sample("Result", []string{FileSize(xfer).String()})
if verbose {
timing.Print(nw.Stats())
}
fmt.Printf("player not implemented yet\n")
return []*big.Int{new(big.Int)}, nil
}
// OTLambdaResult contain oblivious transfer lambda results.
type OTLambdaResult struct {
peerID int
x1 *big.Int
result *big.Int
err error
}
// OTRResult contain oblivious transfer results.
type OTRResult struct {
peerID int
Ra []ot.Label
Rb []ot.Label
Rc []ot.Label
err error
}
// GateResults contain gate exchange results.
type GateResults struct {
peerID int
Ra [][]ot.Label
Rb [][]ot.Label
Rc [][]ot.Label
Rd [][]ot.Label
Ro *big.Int
err error
}
// GateValues specify player's gate values.
type GateValues struct {
Ag [][]ot.Label
Bg [][]ot.Label
Cg [][]ot.Label
Dg [][]ot.Label
}
// NewGateValues creates GateValues for a player.
func NewGateValues(numGates, numPlayers, we int) *GateValues {
v := &GateValues{
Ag: make([][]ot.Label, numPlayers),
Bg: make([][]ot.Label, numPlayers),
Cg: make([][]ot.Label, numPlayers),
Dg: make([][]ot.Label, numPlayers),
}
for p := 0; p < numPlayers; p++ {
v.Ag[p] = arrayOfLabels(numGates)
v.Bg[p] = arrayOfLabels(numGates)
v.Cg[p] = arrayOfLabels(numGates)
v.Dg[p] = arrayOfLabels(numGates)
}
return v
}
func arrayOfLabels(count int) []ot.Label {
result := make([]ot.Label, count)
for i := 0; i < count; i++ {
l, err := ot.NewLabel(rand.Reader)
if err != nil {
panic(err)
}
result[i] = l
}
return result
}