-
Notifications
You must be signed in to change notification settings - Fork 64
/
share_arithmetic.py
161 lines (114 loc) · 4.46 KB
/
share_arithmetic.py
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
from asyncio import gather
from honeybadgermpc.progs.mixins.base import AsyncMixin
from honeybadgermpc.progs.mixins.constants import MixinConstants
from honeybadgermpc.progs.mixins.dataflow import Share, ShareArray
from honeybadgermpc.utils.typecheck import TypeCheck
class BeaverMultiply(AsyncMixin):
from honeybadgermpc.mpc import Mpc, Share
name = MixinConstants.MultiplyShare
@staticmethod
@TypeCheck()
async def _prog(context: Mpc, x: Share, y: Share):
a, b, ab = context.preproc.get_triples(context)
d, e = await gather(*[(x - a).open(), (y - b).open()])
xy = d * e + d * b + e * a + ab
return xy
class BeaverMultiplyArrays(AsyncMixin):
from honeybadgermpc.mpc import Mpc
name = MixinConstants.MultiplyShareArray
@staticmethod
@TypeCheck()
async def _prog(context: Mpc, j: ShareArray, k: ShareArray):
assert len(j) == len(k)
a, b, ab = [], [], []
for _ in range(len(j)):
p, q, pq = context.preproc.get_triples(context)
a.append(p)
b.append(q)
ab.append(pq)
u, v = context.ShareArray(a), context.ShareArray(b)
f, g = await gather(*[(j - u).open(), (k - v).open()])
xy = [d * e + d * q + e * p + pq for (p, q, pq, d, e) in zip(a, b, ab, f, g)]
return context.ShareArray(xy)
class DoubleSharingMultiply(AsyncMixin):
from honeybadgermpc.mpc import Mpc
name = MixinConstants.MultiplyShare
@staticmethod
@TypeCheck()
async def reduce_degree_share(context: Mpc, x_2t: Share):
assert x_2t.t == context.t * 2
r_t, r_2t = context.preproc.get_double_shares(context)
diff = await (x_2t - r_2t).open()
return r_t + diff
@staticmethod
@TypeCheck()
async def _prog(context: Mpc, x: Share, y: Share):
xy_2t = context.Share(x.v * y.v, context.t * 2)
xy_t = await DoubleSharingMultiply.reduce_degree_share(context, xy_2t)
return xy_t
class DoubleSharingMultiplyArrays(AsyncMixin):
from honeybadgermpc.mpc import Mpc
name = MixinConstants.MultiplyShareArray
@staticmethod
@TypeCheck()
async def reduce_degree_share_array(context: Mpc, x_2t: ShareArray):
assert x_2t.t == context.t * 2
r_t, r_2t = [], []
for _ in range(len(x_2t)):
r_t_, r_2t_ = context.preproc.get_double_shares(context)
r_t.append(r_t_)
r_2t.append(r_2t_)
q_t = context.ShareArray(r_t)
q_2t = context.ShareArray(r_2t, 2 * context.t)
diff = await (x_2t - q_2t).open()
return q_t + diff
@staticmethod
@TypeCheck()
async def _prog(context: Mpc, x: ShareArray, y: ShareArray):
assert len(x) == len(y)
xy_2t = context.ShareArray(
[j.v * k.v for j, k in zip(x._shares, y._shares)], context.t * 2
)
xy_t = await DoubleSharingMultiplyArrays.reduce_degree_share_array(
context, xy_2t
)
return xy_t
class InvertShare(AsyncMixin):
from honeybadgermpc.mpc import Mpc
name = MixinConstants.InvertShare
@staticmethod
@TypeCheck()
async def _prog(context: Mpc, x: Share):
r = context.preproc.get_rand(context)
sig = await (x * r).open()
return r * (1 / sig)
class InvertShareArray(AsyncMixin):
from honeybadgermpc.mpc import Mpc
name = MixinConstants.InvertShareArray
@staticmethod
@TypeCheck()
async def _prog(context: Mpc, xs: ShareArray):
rs = context.ShareArray(
[context.preproc.get_rand(context) for _ in range(len(xs))]
)
sigs = await (await (xs * rs)).open()
sig_invs = context.ShareArray([1 / sig for sig in sigs])
return await (rs * sig_invs)
class DivideShares(AsyncMixin):
from honeybadgermpc.mpc import Mpc
name = MixinConstants.DivideShare
dependencies = [MixinConstants.InvertShare]
@staticmethod
@TypeCheck()
async def _prog(context: Mpc, x: Share, y: Share):
y_inv = await context.config[MixinConstants.InvertShare](context, y)
return await (x * y_inv)
class DivideShareArrays(AsyncMixin):
from honeybadgermpc.mpc import Mpc
name = MixinConstants.DivideShareArray
dependencies = [MixinConstants.InvertShareArray]
@staticmethod
@TypeCheck()
async def _prog(context: Mpc, xs: ShareArray, ys: ShareArray):
y_invs = await context.config[MixinConstants.InvertShareArray](context, ys)
return await (xs * y_invs)