-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathstream_garble.go
440 lines (368 loc) · 8.31 KB
/
stream_garble.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
440
//
// Copyright (c) 2020-2021, 2023 Markku Rossi
//
// All rights reserved.
//
package circuit
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"fmt"
"time"
"github.com/markkurossi/mpc/ot"
"github.com/markkurossi/mpc/p2p"
)
const (
// StreamDebug controls the debugging output of the streaming
// garbling.
StreamDebug = false
)
// Streaming is a streaming garbled circuit garbler.
type Streaming struct {
conn *p2p.Conn
key []byte
alg cipher.Block
r ot.Label
wires []ot.Wire
tmp []ot.Wire
in []Wire
out []Wire
firstTmp Wire
firstOut Wire
}
// NewStreaming creates a new streaming garbled circuit garbler.
func NewStreaming(key []byte, inputs []Wire, conn *p2p.Conn) (
*Streaming, error) {
r, err := ot.NewLabel(rand.Reader)
if err != nil {
return nil, err
}
r.SetS(true)
alg, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
stream := &Streaming{
conn: conn,
key: key,
alg: alg,
r: r,
}
stream.ensureWires(maxWire(0, inputs))
// Assing all input wires.
for i := 0; i < len(inputs); i++ {
w, err := makeLabels(stream.r)
if err != nil {
return nil, err
}
stream.wires[inputs[i]] = w
}
return stream, nil
}
func maxWire(max Wire, wires []Wire) Wire {
for _, w := range wires {
if w > max {
max = w
}
}
return max
}
func (stream *Streaming) ensureWires(max Wire) {
// Verify that wires is big enough.
if len(stream.wires) <= int(max) {
var i int
for i = 65536; i <= int(max); i <<= 1 {
}
n := make([]ot.Wire, i)
copy(n, stream.wires)
stream.wires = n
}
}
func (stream *Streaming) initCircuit(c *Circuit, in, out []Wire) {
stream.ensureWires(maxWire(maxWire(0, in), out))
if len(stream.tmp) < c.NumWires {
stream.tmp = make([]ot.Wire, c.NumWires)
}
stream.in = in
stream.out = out
stream.firstTmp = Wire(len(in))
stream.firstOut = Wire(c.NumWires - len(out))
}
// GetInput gets the value of the input wire.
func (stream *Streaming) GetInput(w Wire) ot.Wire {
return stream.wires[w]
}
// GetInputs gets the specified input wire range.
func (stream *Streaming) GetInputs(offset, count int) []ot.Wire {
return stream.wires[offset : offset+count]
}
// Get gets the value of the wire.
func (stream *Streaming) Get(w Wire) (ot.Wire, Wire, bool) {
if w < stream.firstTmp {
index := stream.in[w]
return stream.wires[index], index, false
} else if w >= stream.firstOut {
index := stream.out[w-stream.firstOut]
return stream.wires[index], index, false
} else {
return stream.tmp[w], w, true
}
}
// Set sets the value of the wire.
func (stream *Streaming) Set(w Wire, val ot.Wire) (index Wire, tmp bool) {
if w < stream.firstTmp {
index = stream.in[w]
stream.wires[index] = val
} else if w >= stream.firstOut {
index = stream.out[w-stream.firstOut]
stream.wires[index] = val
} else {
index = w
tmp = true
stream.tmp[w] = val
}
return index, tmp
}
// Garble garbles the circuit and streams the garbled tables into the
// stream.
func (stream *Streaming) Garble(c *Circuit, in, out []Wire) (
time.Duration, time.Duration, error) {
if StreamDebug {
fmt.Printf(" - Streaming.Garble: in=%v, out=%v\n", in, out)
}
start := time.Now()
stream.initCircuit(c, in, out)
// Garble gates.
var data ot.LabelData
var id uint32
var table [4]ot.Label
mid := time.Now()
for i := 0; i < len(c.Gates); i++ {
gate := &c.Gates[i]
err := stream.conn.NeedSpace(512)
if err != nil {
return 0, 0, err
}
err = stream.garbleGate(gate, &id, table[:], &data,
stream.conn.WriteBuf, &stream.conn.WritePos)
if err != nil {
return 0, 0, err
}
}
return mid.Sub(start), time.Now().Sub(mid), nil
}
// GarbleGate garbles the gate and streams it to the stream.
func (stream *Streaming) garbleGate(g *Gate, idp *uint32,
table []ot.Label, data *ot.LabelData, buf []byte, bufpos *int) error {
var a, b, c ot.Wire
var aIndex, bIndex, cIndex Wire
var aTmp, bTmp, cTmp bool
table = table[0:4]
var tableStart, tableCount, wireCount int
// Inputs.
switch g.Op {
case XOR, XNOR, AND, OR:
b, bIndex, bTmp = stream.Get(g.Input1)
fallthrough
case INV:
a, aIndex, aTmp = stream.Get(g.Input0)
default:
return fmt.Errorf("invalid gate type %s", g.Op)
}
// Output.
switch g.Op {
case XOR:
l0 := a.L0
l0.Xor(b.L0)
l1 := l0
l1.Xor(stream.r)
c = ot.Wire{
L0: l0,
L1: l1,
}
case XNOR:
l0 := a.L0
l0.Xor(b.L0)
l1 := l0
l1.Xor(stream.r)
c = ot.Wire{
L0: l1,
L1: l0,
}
case AND:
pa := a.L0.S()
pb := b.L0.S()
j0 := *idp
j1 := *idp + 1
*idp = *idp + 2
// First half gate.
tg := encryptHalf(stream.alg, a.L0, j0, data)
tg.Xor(encryptHalf(stream.alg, a.L1, j0, data))
if pb {
tg.Xor(stream.r)
}
wg0 := encryptHalf(stream.alg, a.L0, j0, data)
if pa {
wg0.Xor(tg)
}
// Second half gate.
te := encryptHalf(stream.alg, b.L0, j1, data)
te.Xor(encryptHalf(stream.alg, b.L1, j1, data))
te.Xor(a.L0)
we0 := encryptHalf(stream.alg, b.L0, j1, data)
if pb {
we0.Xor(te)
we0.Xor(a.L0)
}
// Combine halves
l0 := wg0
l0.Xor(we0)
l1 := l0
l1.Xor(stream.r)
c = ot.Wire{
L0: l0,
L1: l1,
}
table[0] = tg
table[1] = te
tableCount = 2
case OR, INV:
// Row reduction creates labels below so that the first row is
// all zero.
default:
panic("invalid gate type")
}
switch g.Op {
case XOR, XNOR:
// Free XOR.
wireCount = 3
case AND:
// Half AND garbled above.
wireCount = 3
case OR:
// a b c
// -----
// 0 0 0
// 0 1 1
// 1 0 1
// 1 1 1
id := *idp
*idp = *idp + 1
table[idx(a.L0, b.L0)] = encrypt(stream.alg, a.L0, b.L0, c.L0, id, data)
table[idx(a.L0, b.L1)] = encrypt(stream.alg, a.L0, b.L1, c.L1, id, data)
table[idx(a.L1, b.L0)] = encrypt(stream.alg, a.L1, b.L0, c.L1, id, data)
table[idx(a.L1, b.L1)] = encrypt(stream.alg, a.L1, b.L1, c.L1, id, data)
// Row reduction. Make first table all zero so we don't have
// to transmit it.
l0Index := idx(a.L0, b.L0)
c.L0 = table[0]
c.L1 = table[0]
if l0Index == 0 {
c.L1.Xor(stream.r)
} else {
c.L0.Xor(stream.r)
}
for i := 0; i < 4; i++ {
if i == l0Index {
table[i].Xor(c.L0)
} else {
table[i].Xor(c.L1)
}
}
tableStart = 1
tableCount = 3
wireCount = 3
case INV:
// a b c
// -----
// 0 1
// 1 0
zero := ot.Label{}
id := *idp
*idp = *idp + 1
table[idxUnary(a.L0)] = encrypt(stream.alg, a.L0, zero, c.L1, id, data)
table[idxUnary(a.L1)] = encrypt(stream.alg, a.L1, zero, c.L0, id, data)
l0Index := idxUnary(a.L0)
c.L0 = table[0]
c.L1 = table[0]
if l0Index == 0 {
c.L0.Xor(stream.r)
} else {
c.L1.Xor(stream.r)
}
for i := 0; i < 2; i++ {
if i == l0Index {
table[i].Xor(c.L1)
} else {
table[i].Xor(c.L0)
}
}
tableStart = 1
tableCount = 1
wireCount = 2
default:
return fmt.Errorf("invalid operand %s", g.Op)
}
if g.Output < stream.firstTmp {
cIndex = stream.in[g.Output]
stream.wires[cIndex] = c
} else if g.Output >= stream.firstOut {
cIndex = stream.out[g.Output-stream.firstOut]
stream.wires[cIndex] = c
} else {
cIndex = g.Output
cTmp = true
stream.tmp[g.Output] = c
}
op := byte(g.Op)
if aTmp {
op |= 0b10000000
}
if bTmp {
op |= 0b01000000
}
if cTmp {
op |= 0b00100000
}
if aIndex <= 0xffff && bIndex <= 0xffff && cIndex <= 0xffff {
op |= 0b00010000
buf[*bufpos] = op
*bufpos = *bufpos + 1
switch wireCount {
case 3:
bo.PutUint16(buf[*bufpos+0:], uint16(aIndex))
bo.PutUint16(buf[*bufpos+2:], uint16(bIndex))
bo.PutUint16(buf[*bufpos+4:], uint16(cIndex))
*bufpos = *bufpos + 6
case 2:
bo.PutUint16(buf[*bufpos+0:], uint16(aIndex))
bo.PutUint16(buf[*bufpos+2:], uint16(cIndex))
*bufpos = *bufpos + 4
default:
panic(fmt.Sprintf("invalid wire count: %d", wireCount))
}
} else {
buf[*bufpos] = op
*bufpos = *bufpos + 1
switch wireCount {
case 3:
bo.PutUint32(buf[*bufpos+0:], uint32(aIndex))
bo.PutUint32(buf[*bufpos+4:], uint32(bIndex))
bo.PutUint32(buf[*bufpos+8:], uint32(cIndex))
*bufpos = *bufpos + 12
case 2:
bo.PutUint32(buf[*bufpos+0:], uint32(aIndex))
bo.PutUint32(buf[*bufpos+4:], uint32(cIndex))
*bufpos = *bufpos + 8
default:
panic(fmt.Sprintf("invalid wire count: %d", wireCount))
}
}
for i := 0; i < tableCount; i++ {
bytes := table[tableStart+i].Bytes(data)
copy(buf[*bufpos:], bytes)
*bufpos = *bufpos + len(bytes)
}
return nil
}