-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy patheval.go
115 lines (94 loc) · 1.86 KB
/
eval.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
//
// Copyright (c) 2019-2021 Markku Rossi
//
// All rights reserved.
//
package circuit
import (
"crypto/aes"
"fmt"
"github.com/markkurossi/mpc/ot"
)
// Eval evaluates the circuit.
func (c *Circuit) Eval(key []byte, wires []ot.Label,
garbled [][]ot.Label) error {
alg, err := aes.NewCipher(key)
if err != nil {
return err
}
var data ot.LabelData
var id uint32
for i := 0; i < len(c.Gates); i++ {
gate := &c.Gates[i]
var a, b, c ot.Label
switch gate.Op {
case XOR, XNOR, AND, OR:
a = wires[gate.Input0]
b = wires[gate.Input1]
case INV:
a = wires[gate.Input0]
default:
return fmt.Errorf("invalid operation %s", gate.Op)
}
var output ot.Label
switch gate.Op {
case XOR, XNOR:
a.Xor(b)
output = a
case AND:
row := garbled[i]
if len(row) != 2 {
return fmt.Errorf("corrupted ciruit: AND row length: %d",
len(row))
}
sa := a.S()
sb := b.S()
j0 := id
j1 := id + 1
id += 2
tg := row[0]
te := row[1]
wg := encryptHalf(alg, a, j0, &data)
if sa {
wg.Xor(tg)
}
we := encryptHalf(alg, b, j1, &data)
if sb {
we.Xor(te)
we.Xor(a)
}
output = wg
output.Xor(we)
case OR:
row := garbled[i]
index := idx(a, b)
if index > 0 {
// First row is zero and not transmitted.
index--
if index >= len(row) {
return fmt.Errorf("corrupted circuit: index %d >= row %d",
index, len(row))
}
c = row[index]
}
output = decrypt(alg, a, b, id, c, &data)
id++
case INV:
row := garbled[i]
index := idxUnary(a)
if index > 0 {
// First row is zero and not transmitted.
index--
if index >= len(row) {
return fmt.Errorf("corrupted circuit: index %d >= row %d",
index, len(row))
}
c = row[index]
}
output = decrypt(alg, a, ot.Label{}, id, c, &data)
id++
}
wires[gate.Output] = output
}
return nil
}