forked from ldsec/CS523-Project1
-
Notifications
You must be signed in to change notification settings - Fork 1
/
operations.go
329 lines (265 loc) · 6.51 KB
/
operations.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
package main
import (
"crypto/rand"
"errors"
"github.com/ldsec/lattigo/ring"
"math/big"
)
type WireID uint64
type GateID uint64
type BeaverTriplet struct {
a *big.Int
b *big.Int
c *big.Int
}
type Operation interface {
Output() WireID
Eval(*Protocol) // computes the operation of the wire and stores the result in the WireOutput map
BeaverTriplet(int) []BeaverTriplet // If necessary, returns the sahres for a Beaver triplet, otherwise nil
IsMult() bool // returns true if and only if the gate is a multiplication
}
// Given an input, split it between the peers and send them their share
func (io Input) generateShares(cep *Protocol) {
sum := big.NewInt(0)
for _, peer := range cep.Peers {
if peer.ID != cep.ID {
share, err := rand.Int(rand.Reader, q)
check(err)
sum.Add(sum, share)
peer.SendingChan <- Message{MPCMessage: &MPCMessage{io.Out, share.Uint64()}}
}
}
s := big.NewInt(int64(cep.Input))
cep.WireOutput[io.Out] = new(big.Int).Sub(s, sum)
cep.WireOutput[io.Out].Mod(cep.WireOutput[io.Out], q)
}
type Input struct {
Party PartyID
Out WireID
}
func (io Input) Output() WireID {
return io.Out
}
// If the input is our, split it using the method 'generateShares', otherwise receive our share from the concerned peer
func (io Input) Eval(cep *Protocol) {
if io.Party == cep.ID {
io.generateShares(cep)
} else {
m := <-cep.Peers[io.Party].ReceiveChan
if m.MPCMessage == nil {
check(errors.New("BeaverMessage received instead of MPCMessage"))
}
cep.WireOutput[io.Out] = big.NewInt(int64(m.MPCMessage.Value))
}
}
func (io Input) BeaverTriplet(count int) []BeaverTriplet {
return nil
}
func (io Input) IsMult() bool {
return false
}
type Add struct {
In1 WireID
In2 WireID
Out WireID
}
func (a Add) IsMult() bool {
return false
}
func (ao Add) Output() WireID {
return ao.Out
}
func (ao Add) Eval(cep *Protocol) {
cep.WireOutput[ao.Out] = new(big.Int).Add(cep.WireOutput[ao.In1], cep.WireOutput[ao.In2])
}
func (ao Add) BeaverTriplet(count int) []BeaverTriplet {
return nil
}
type AddCst struct {
In WireID
CstValue uint64
Out WireID
}
func (a AddCst) IsMult() bool {
return false
}
func (aco AddCst) Output() WireID {
return aco.Out
}
func (aco AddCst) Eval(cep *Protocol) {
cep.WireOutput[aco.Out] = big.NewInt(cep.WireOutput[aco.In].Int64())
if cep.ID == 0 {
cep.WireOutput[aco.Out].Add(cep.WireOutput[aco.Out], big.NewInt(int64(aco.CstValue)))
}
}
func (aco AddCst) BeaverTriplet(count int) []BeaverTriplet {
return nil
}
type Sub struct {
In1 WireID
In2 WireID
Out WireID
}
func (so Sub) IsMult() bool {
return false
}
func (so Sub) Output() WireID {
return so.Out
}
func (so Sub) Eval(cep *Protocol) {
cep.WireOutput[so.Out] = new(big.Int).Sub(cep.WireOutput[so.In1], cep.WireOutput[so.In2])
}
func (so Sub) BeaverTriplet(count int) []BeaverTriplet {
return nil
}
type Mult struct {
In1 WireID
In2 WireID
Out WireID
}
func (mo Mult) IsMult() bool {
return true
}
func (mo Mult) Output() WireID {
return mo.Out
}
// Executes a multiplication using the Beaver triplet that were already generated
func (mo Mult) Eval(cep *Protocol) {
x := cep.WireOutput[mo.In1]
y := cep.WireOutput[mo.In2]
a := cep.BeaverTriplets[mo.Output()].a
b := cep.BeaverTriplets[mo.Output()].b
c := cep.BeaverTriplets[mo.Output()].c
X_a := big.NewInt(0)
X_a.Sub(x, a).Mod(X_a, q)
Y_b := big.NewInt(0)
Y_b.Sub(y, b).Mod(Y_b, q)
for _, peer := range cep.Peers {
if peer.ID != cep.ID {
peer.SendingChan <- Message{MPCMessage: &MPCMessage{
Out: mo.Output(),
Value: X_a.Uint64(),
}}
peer.SendingChan <- Message{MPCMessage: &MPCMessage{
Out: mo.Output(),
Value: Y_b.Uint64(),
}}
}
}
for _, peer := range cep.Peers {
if peer.ID != cep.ID {
m := <-peer.ReceiveChan
if m.MPCMessage == nil {
check(errors.New("BeaverMessage received instead of MPCMessage"))
}
X_a.Add(X_a, big.NewInt(int64(m.MPCMessage.Value)))
m = <-peer.ReceiveChan
if m.MPCMessage == nil {
check(errors.New("BeaverMessage received instead of MPCMessage"))
}
Y_b.Add(Y_b, big.NewInt(int64(m.MPCMessage.Value)))
}
}
z := big.NewInt(0)
z.Add(z, c)
x_y_b := big.NewInt(0).Mul(x, Y_b)
z.Add(z, x_y_b)
y_x_a := big.NewInt(0).Mul(y, X_a)
z.Add(z, y_x_a)
if cep.ID == 0 {
x_a_y_b := big.NewInt(0).Mul(X_a, Y_b)
z.Sub(z, x_a_y_b)
}
cep.WireOutput[mo.Output()] = z
}
func (mo Mult) BeaverTriplet(count int) []BeaverTriplet {
triplets := make([]BeaverTriplet, count)
sum_a := big.NewInt(0)
sum_b := big.NewInt(0)
sum_c := big.NewInt(0)
a := ring.RandInt(q)
b := ring.RandInt(q)
c := big.NewInt(0).Mul(a, b)
c.Mod(c, q)
for i := 0; i < len(triplets)-1; i++ {
a_share := ring.RandInt(q)
b_share := ring.RandInt(q)
c_share := ring.RandInt(q)
sum_a.Add(a_share, sum_a)
sum_b.Add(b_share, sum_b)
sum_c.Add(c_share, sum_c)
triplets[i] = BeaverTriplet{
a: a_share,
b: b_share,
c: c_share,
}
}
a_share := big.NewInt(0)
a_share.Sub(a, sum_a).Mod(a_share, q)
b_share := big.NewInt(0)
b_share.Sub(b, sum_b).Mod(b_share, q)
c_share := big.NewInt(0)
c_share.Sub(c, sum_c).Mod(c_share, q)
triplets[len(triplets)-1] = BeaverTriplet{
a: a_share,
b: b_share,
c: c_share,
}
return triplets
}
type MultCst struct {
In WireID
CstValue uint64
Out WireID
}
func (mco MultCst) IsMult() bool {
return false
}
func (mco MultCst) Output() WireID {
return mco.Out
}
func (mco MultCst) Eval(cep *Protocol) {
cep.WireOutput[mco.Out] = new(big.Int).Mul(cep.WireOutput[mco.In], big.NewInt(int64(mco.CstValue)))
}
func (mco MultCst) BeaverTriplet(count int) []BeaverTriplet {
return nil
}
type Reveal struct {
In WireID
Out WireID
}
func (ro Reveal) IsMult() bool {
return false
}
func (ro Reveal) Output() WireID {
return ro.Out
}
// Reveal the output by adding all the shares together
func (ro Reveal) Eval(cep *Protocol) {
inputShare := cep.WireOutput[ro.In]
inputShare.Mod(inputShare, q)
for _, peer := range cep.Peers {
if peer.ID != cep.ID {
peer.SendingChan <- Message{MPCMessage: &MPCMessage{
Out: ro.Output(),
Value: inputShare.Uint64(),
}}
}
}
sum := big.NewInt(0)
sum.Add(sum, inputShare)
for _, peer := range cep.Peers {
if peer.ID != cep.ID {
m := <-peer.ReceiveChan
if m.MPCMessage == nil {
check(errors.New("BeaverMessage received instead of MPCMessage"))
}
sum.Add(sum, big.NewInt(int64(m.MPCMessage.Value)))
}
}
sum.Mod(sum, q)
cep.WireOutput[ro.Output()] = sum
}
func (ro Reveal) BeaverTriplet(count int) []BeaverTriplet {
return nil
}