-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathgarbler.go
241 lines (207 loc) · 4.57 KB
/
garbler.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
//
// garbler.go
//
// Copyright (c) 2019 Markku Rossi
//
// All rights reserved.
//
package circuit
import (
"crypto/rand"
"fmt"
"math/big"
"time"
"github.com/markkurossi/mpc/ot"
"github.com/markkurossi/mpc/p2p"
)
const (
OP_OT = iota
OP_RESULT
)
type FileSize uint64
func (s FileSize) String() string {
if s > 1000*1000*1000*1000 {
return fmt.Sprintf("%dTB", s/(1000*1000*1000*1000))
} else if s > 1000*1000*1000 {
return fmt.Sprintf("%dGB", s/(1000*1000*1000))
} else if s > 1000*1000 {
return fmt.Sprintf("%dMB", s/(1000*1000))
} else if s > 1000 {
return fmt.Sprintf("%dkB", s/1000)
} else {
return fmt.Sprintf("%dB", s)
}
}
func Garbler(conn *p2p.Conn, circ *Circuit, inputs *big.Int, verbose bool) (
[]*big.Int, error) {
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)
// Send program info.
if verbose {
fmt.Printf(" - Sending garbled circuit...\n")
}
if err := conn.SendData(key[:]); err != nil {
return nil, err
}
// Send garbled tables.
if err := conn.SendUint32(len(garbled.Gates)); err != nil {
return nil, err
}
for _, data := range garbled.Gates {
if err := conn.SendUint32(len(data)); err != nil {
return nil, err
}
for _, d := range data {
if err := conn.SendLabel(d); err != nil {
return nil, err
}
}
}
// Select our inputs.
var n1 []ot.Label
var w int
for i := 0; i < circ.Inputs[0].Size; i++ {
wire := garbled.Wires[w]
w++
var n ot.Label
if inputs.Bit(i) == 1 {
n = wire.L1
} else {
n = wire.L0
}
n1 = append(n1, n)
}
// Send our inputs.
for idx, i := range n1 {
if verbose && false {
fmt.Printf("N1[%d]:\t%s\n", idx, i)
}
if err := conn.SendLabel(i); err != nil {
return nil, err
}
}
ioStats := conn.Stats
timing.Sample("Xfer", []string{FileSize(ioStats.Sum()).String()})
if verbose {
fmt.Printf(" - Processing messages...\n")
}
// Init oblivious transfer.
sender, err := ot.NewSender(2048)
if err != nil {
return nil, err
}
// Send our public key.
pub := sender.PublicKey()
data := pub.N.Bytes()
if err := conn.SendData(data); err != nil {
return nil, err
}
if err := conn.SendUint32(pub.E); err != nil {
return nil, err
}
conn.Flush()
ioStats = conn.Stats.Sub(ioStats)
timing.Sample("OT Init", []string{FileSize(ioStats.Sum()).String()})
// Init wires the peer is allowed to OT.
allowedOTs := make(map[int]bool)
for bit := 0; bit < circ.Inputs[1].Size; bit++ {
allowedOTs[circ.Inputs[0].Size+bit] = true
}
// Process messages.
var xfer *ot.SenderXfer
lastOT := time.Now()
done := false
result := big.NewInt(0)
for !done {
op, err := conn.ReceiveUint32()
if err != nil {
return nil, err
}
switch op {
case OP_OT:
bit, err := conn.ReceiveUint32()
if err != nil {
return nil, err
}
if !allowedOTs[bit] {
return nil, fmt.Errorf("peer can't OT wire %d", bit)
}
allowedOTs[bit] = false
wire := garbled.Wires[bit]
m0Data := wire.L0.Data()
m1Data := wire.L1.Data()
xfer, err = sender.NewTransfer(m0Data[:], m1Data[:])
if err != nil {
return nil, err
}
x0, x1 := xfer.RandomMessages()
if err := conn.SendData(x0); err != nil {
return nil, err
}
if err := conn.SendData(x1); err != nil {
return nil, err
}
conn.Flush()
v, err := conn.ReceiveData()
if err != nil {
return nil, err
}
xfer.ReceiveV(v)
m0p, m1p, err := xfer.Messages()
if err != nil {
return nil, err
}
if err := conn.SendData(m0p); err != nil {
return nil, err
}
if err := conn.SendData(m1p); err != nil {
return nil, err
}
conn.Flush()
lastOT = time.Now()
case OP_RESULT:
for i := 0; i < circ.Outputs.Size(); i++ {
label, err := conn.ReceiveLabel()
if err != nil {
return nil, err
}
wire := garbled.Wires[circ.NumWires-circ.Outputs.Size()+i]
var bit uint
if label.Equal(wire.L0) {
bit = 0
} else if label.Equal(wire.L1) {
bit = 1
} else {
return nil, fmt.Errorf("Unknown label %s for result %d",
label, i)
}
result = big.NewInt(0).SetBit(result, i, bit)
}
data := result.Bytes()
if err := conn.SendData(data); err != nil {
return nil, err
}
conn.Flush()
done = true
}
}
ioStats = conn.Stats.Sub(ioStats)
timing.Sample("Eval", []string{FileSize(ioStats.Sum()).String()}).
SubSample("OT", lastOT)
if verbose {
timing.Print()
}
return circ.Outputs.Split(result), nil
}