-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBrea.py
More file actions
280 lines (228 loc) · 7.41 KB
/
Brea.py
File metadata and controls
280 lines (228 loc) · 7.41 KB
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
import random
import numpy
import numpy as np
import learning.federated_main as fl
import learning.models_helper as mhelper
from Turbo import generateLagrangePolynomial
np.set_printoptions(threshold=np.inf, linewidth=np.inf)
# make N theta_i
# [호출] : 서버
# [인자] : n(사용자수), p
# [리턴] : th(각 client들에 대한 theta list)
def make_theta(n, p):
th = []
for i in range(n):
th.append(random.randint(1, p))
return th
# Secret polynomium coefficients r_ij
# [호출] : 클라이언트
# [인자] : T(threshold), p
# [리턴] : rij(사용자 i가 랜덤 생성한 계수)(j: 1~T)
def generate_rij(T, p):
rij = [0, ] # not using first index (0)
for j in range(T):
rij.append(random.randint(1, p))
return rij
# share를 생성하기 위한 다항식 f
# [인자] : theta(자신의 theta), w(weights. quantization한 값), T(threshold), rij(get_rij()의 리턴값)
# [리턴] : y
def f(theta, w, T, rij, p):
y = np.array(w)
for j in range(1, T + 1):
y = y + (rij[j] * (mod(theta, j, p)))
return y.tolist()
# make shares
# [호출] : 클라이언트
# [인자] : w, theta_list(서버가 알려준 theta list), T, rij_list, g, p
# [리턴] : shares (다른 사용자들에게 보낼 share list)
def make_shares(flatten_weights, theta_list, T, rij_list, p):
shares = []
for theta in theta_list:
shares.append(f(theta, flatten_weights, T, rij_list, p))
return shares
# make commitment
# [호출] : 클라이언트
# [인자] : flatten_weights, rij_list, q, p
# [리턴] : commitments (verify를 위한 commitment list)
def generate_commitments(flatten_weights, rij_list, g, p):
commitments = []
for i, rij in enumerate(rij_list):
if i == 0:
c = []
for k in flatten_weights:
c.append(mod(g, int(np.max(k)), p))
commitments.append(c)
continue
commitments.append(mod(g, rij, p))
return commitments
# verify commitments
# [호출] : 클라이언트
# [인자] : g, share(i 에게서 받은 share), commitments(i 가 생성한 commitment list), theta(자신의 theta), p
# [리턴] : boolean
def verify(g, share, commitments, theta, p):
tmp_x = []
for s in share:
tmp_x.append(mod(g, int(np.max(s)), p))
x = np.array(tmp_x)
y = 1
for i, c in enumerate(commitments):
if i == 0:
m = mod(theta, i, p)
y = y * mod(np.array(c, dtype=numpy.int32), m, p)
else:
m = mod(theta, i, p)
y = y * mod(c, m, p)
y = y % p
# f = open('result.txt', 'w')
# f.write(str(x))
# f.close()
# f = open('result2.txt', 'w')
# f.write(str(y))
# f.close()
if np.allclose(x, y) == True:
result = True
else:
result = False
return result
def mod(theta, i, p):
ret = 1
for idx in range(i):
ret = ret * int(theta) % p
return ret
# [호출] : 클라이언트
# [인자] : share1, share2(사용자 1과 사용자 2의 거리를 계산하기 위해 1에게 받은 share와 2에게 받은 share를 인자로)
# [리턴] : distance(계산한 거리)
def calculate_distance(shares):
distance = []
for i in range(len(shares)):
for j in range(len(shares)):
distance.append((i, j, (abs(np.array(shares[i]) - np.array(shares[j])) ** 2).tolist()))
return distance
# def calculate_distance(shares, n):
# distances = {}
# for j in range(n):
# distances[j] = {}
# for k in range(n):
# dis = abs(np.array(shares[j]) - np.array(shares[k])) ** 2
# distances[j][k] = dis
# return distances
#[호출] : 서버
#[인자] : theta(theta_list), distances(djk_list)
#[리턴] :_djk(hjk(0))
def calculate_djk_from_h_polynomial(theta, distances):
h = generateLagrangePolynomial(theta, distances)
djk = np.polyval(h,0)
return djk
#[호출] : 서버
#[인자] : _djk(hjk(0)), p, q
#[리턴] : 실수 djk
def real_domain_djk(_djk, p, q):
if ((p - 1) / 2) <= _djk < p:
_djk = _djk - p
djk = _djk / (q ** 2)
return djk
def multi_krum(n, m, djk):
"""
n = All user
m = selected user
djk = distances between users
a = Reed Solomon max number of error
Sk = selected index set S(k)
_set = range of adding dju
skj = list of added dju for each users
dis = temporary copy array for one row in skj
user = selected user's index
"""
k = 1
a = n / 2
Sk = []
while True:
_set = (n - k + 1) - a - 2
skj = {}
for key, value in djk.items():
sum_dis = 0
for idx, val in djk[key].items():
if idx not in Sk:
sum_dis += sum(val)
print("SUM_DIS" + str(sum_dis))
skj[key] = sum_dis
index = min(skj, key=skj.get)
print("INDEX" + str(index))
Sk.append(index)
if k == m:
break
k += 1
djk.pop(index)
return Sk
#[호출] : 서버
#[인자] : djk (실수 djk), _range: (N−k+1)−A−2 (범위 값)
#[리턴] : skj
def calcutate_skj_from_djk(djk, _range):
djk.sort()
skj = 0
for i in range(_range):
skj += djk[i]
return skj
#[호출] : 서버
#[인자] : skj
#[리턴] : 선택된 유저의 skj, 선택된 유저의 인덱스 값
def select_one_user_among_skj(skj):
tmp = skj[0]
user = 0
for i in range(skj):
if(tmp > skj[i]):
tmp = skj[i]
user = i
return skj[user], user
def aggregate_share(shares, selected_user, u):
si = [0, ]
for i in selected_user:
if i != u:
si = shares[i] + si
return si
def update_weight(_wj, model, p, q, n):
"""
_wj = weight from user
demap_wj = wj with demapping function
model = global model
para = paramater using leaning rate and q
"""
demap_wj = _wj
learning_rate = 0.01
para = (q * n)
for idx, val in enumerate(demap_wj):
if ((p - 1) / 2) <= val < p:
demap_wj[idx] = (val - p) / para
else:
demap_wj[idx] = val / para
return [m - d for m, d in zip(model, demap_wj)]
if __name__ == "__main__":
p = 7 # q -> p
q = 3 # g -> q
n = 4 # N = 40
T = 3 # T = 7
# print(multi_krum(5, 3, [[0, 3, 9, 2, 1], [3, 0, 5, 2, 1], [9, 5, 0, 6, 1], [2, 2, 6, 0, 3], [1, 1, 1, 3, 0]]))
'''
model = fl.setup()
my_model = fl.get_user_dataset(n)
local_model, local_weight, local_loss = fl.local_update(model, my_model[0], 0)
model_weights_list = mhelper.weights_to_dic_of_list(local_weight)
weights_info, flatten_weights = mhelper.flatten_list(model_weights_list)
bar_w = stochasticQuantization(np.array(flatten_weights), q, p)
theta_list = make_theta(n, p)
rij_list1 = generate_rij(T, p)
rij_list2 = generate_rij(T, p)
# print("rij_list2: ", rij_list2)
shares2 = make_shares(bar_w, theta_list, T, rij_list2, q, p)
#commitments1 = generate_commitments(bar_w1, rij_list1, g, q)
commitments2 = generate_commitments(bar_w, rij_list2, q, p)
print(shares2)
print(commitments2)
result = verify(q, shares2[0], commitments2, theta_list[0], p)
print("result: ", result)
distance = calculate_distance(shares2[0], shares2[1])
print("distance: ", distance)
print(multi_krum(4, 3,distance))
# print(calculate_djk_from_h_polynomial(theta_list, distance))
print(calculate_djk_from_h_polynomial([0,1,2],[1,2,3]))
'''