forked from initc3/HoneyBadgerMPC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshare_arithmetic.py
235 lines (172 loc) · 6.55 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
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
from honeybadgermpc.progs.mixins.base import AsyncMixin
from honeybadgermpc.progs.mixins.constants import MixinConstants
from honeybadgermpc.utils.typecheck import TypeCheck
from honeybadgermpc.progs.mixins.dataflow import Share, ShareArray
from honeybadgermpc.field import GFElement
from asyncio import gather
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)
class Equality(AsyncMixin):
from honeybadgermpc.mpc import Mpc
name = MixinConstants.ShareEquality
@staticmethod
@TypeCheck()
def legendre_mod_p(a: GFElement):
"""Return the legendre symbol ``legendre(a, p)`` where *p* is the
order of the field of *a*.
"""
assert a.modulus % 2 == 1
b = a ** ((a.modulus - 1) // 2)
if b == 1:
return 1
elif b == a.modulus - 1:
return -1
return 0
@staticmethod
@TypeCheck()
async def _gen_test_bit(context: Mpc, diff: Share):
# # b \in {0, 1}
b = context.preproc.get_bit(context)
# # _b \in {5, 1}, for p = 1 mod 8, s.t. (5/p) = -1
# # so _b = -4 * b + 5
_b = (-4 * b) + context.Share(5)
_r = context.preproc.get_rand(context)
_rp = context.preproc.get_rand(context)
# c = a * r + b * rp * rp
# If b_i == 1, c_i is guaranteed to be a square modulo p if a is zero
# and with probability 1/2 otherwise (except if rp == 0).
# If b_i == -1 it will be non-square.
c = await ((diff * _r) + (_b * _rp * _rp)).open()
return c, _b
@staticmethod
@TypeCheck
async def gen_test_bit(context: Mpc, diff: Share):
cj, bj = await Equality._gen_test_bit(context, diff)
while cj == 0:
cj, bj = await Equality._gen_test_bit(context, diff)
legendre = Equality.legendre_mod_p(cj)
if legendre == 0:
return Equality.gen_test_bit(context, diff)
return (legendre / context.field(2)) * (bj + context.Share(legendre))
@staticmethod
@TypeCheck()
async def _prog(
context: Mpc, p_share: Share, q_share: Share, security_parameter: int = 32
):
diff = p_share - q_share
x = context.ShareArray(
await gather(
*[
Equality.gen_test_bit(context, diff)
for _ in range(security_parameter)
]
)
)
return await x.multiplicative_product()