-
Notifications
You must be signed in to change notification settings - Fork 10
/
utility.py
106 lines (89 loc) · 3.47 KB
/
utility.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
import numpy as np
import os
SCALE = 1000000000
def secure_aggregate_laplace(vectors, noise_scale, useMPC=False):
if useMPC == False:
return np.mean(vectors, axis=0) + noise_scale * np.random.laplace(0, 1, vectors.shape[1])
vectors = (vectors*SCALE).astype(int)
vectors_mask = np.random.randint(-SCALE, SCALE, vectors.shape)
random_vals = np.random.randint(-SCALE, SCALE, vectors.shape)
fp1 = open('Inputs/beta1.txt', 'w')
fp2 = open('Inputs/beta2.txt', 'w')
fp3 = open('Inputs/random_vals.txt', 'w')
for i in range(vectors.shape[0]):
for j in range(vectors.shape[1]):
fp1.write(str(vectors[i,j]^vectors_mask[i,j])+ ' ')
fp2.write(str(vectors_mask[i,j])+ ' ')
fp3.write(str(random_vals[i,j])+ ' ')
fp1.close()
fp2.close()
fp3.close()
port = 1234
# Note: Currently, M, D, lambda, epsilon and chunk size for each party are all hard coded in modelAggregate.c and modelAggregate.oh files.
os.system("./cycle 'model_aggregate_laplace/a.out "+str(port)+" -- dualex | model_aggregate_laplace/a.out "+str(port)+" localhost dualex'")
fp = open('Output/beta_avg.txt', 'r')
beta = []
for line in fp:
beta = [float(val) for val in line.split(' ')[:-1]]
fp.close()
return np.array(beta)
def ratio_of_uniforms():
u1 = np.random.rand()
v2 = np.random.rand()
u2 = (2*v2 - 1) * np.sqrt(2*np.exp(-1))
x = u2 / u1
while x*x > -4 * np.log(u1):
u1 = np.random.rand()
v2 = np.random.rand()
u2 = (2*v2 - 1) * np.sqrt(2*np.exp(-1))
x = u2 / u1
return x
def secure_aggregate_gaussian(vectors, noise_scale, useMPC=False):
if useMPC == False:
return np.mean(vectors, axis=0) + noise_scale * np.random.normal(0, 1, vectors.shape[1])
#return np.mean(vectors, axis=0) + noise_scale * [ratio_of_uniforms() for i in np.arange(vectors.shape[1])]
vectors = (vectors*SCALE).astype(int)
vectors_mask = np.random.randint(-SCALE, SCALE, vectors.shape)
noise_scale = int(noise_scale*SCALE)
noise_scale_mask = np.random.randint(-SCALE, SCALE)
fp1 = open('Inputs/beta1.txt', 'w')
fp2 = open('Inputs/beta2.txt', 'w')
fp3 = open('Inputs/noise1.txt', 'w')
fp4 = open('Inputs/noise2.txt', 'w')
for i in range(vectors.shape[0]):
for j in range(vectors.shape[1]):
fp1.write(str(vectors[i,j]^vectors_mask[i,j])+ ' ')
fp2.write(str(vectors_mask[i,j])+ ' ')
fp3.write(str(noise_scale^noise_scale_mask)+ ' ')
fp4.write(str(noise_scale_mask)+ ' ')
fp1.close()
fp2.close()
fp3.close()
fp4.close()
port = 1234
ret = 1
while ret != 0:
random1 = (np.random.rand(vectors.shape[1])*SCALE).astype(int)
random2 = (np.random.rand(vectors.shape[1])*SCALE).astype(int)
mask1 = np.random.randint(-SCALE, SCALE, vectors.shape[1])
mask2 = np.random.randint(-SCALE, SCALE, vectors.shape[1])
fp1 = open('Inputs/random11.txt', 'w')
fp2 = open('Inputs/random12.txt', 'w')
fp3 = open('Inputs/random21.txt', 'w')
fp4 = open('Inputs/random22.txt', 'w')
for i in range(vectors.shape[1]):
fp1.write(str(random1[i]^mask1[i])+ ' ')
fp2.write(str(mask1[i])+ ' ')
fp3.write(str(random2[i]^mask2[i])+ ' ')
fp4.write(str(mask2[i])+ ' ')
fp1.close()
fp2.close()
fp3.close()
fp4.close()
ret = os.system("./cycle 'model_aggregate_gaussian/a.out "+str(port)+" -- dualex "+str(vectors.shape[0])+" "+str(vectors.shape[1])+" | model_aggregate_gaussian/a.out "+str(port)+" localhost dualex "+str(vectors.shape[0])+" "+str(vectors.shape[1])+"'")
fp = open('Output/beta_avg.txt', 'r')
beta = []
for line in fp:
beta = [float(val) for val in line.split(' ')[:-1]]
fp.close()
return np.array(beta)