-
Notifications
You must be signed in to change notification settings - Fork 0
/
securestatis.py
112 lines (90 loc) · 2.74 KB
/
securestatis.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
"""
Use basic secure function to compute statistics
"""
import torch
import syft as sy
import numpy as np
import securefe as sfe
import securefunc as sfunc
def secure_mean(shares, dim, num):
"""
"""
ret = []
if dim == 1:
for share in shares:
sum = share.sum()
mean = sum / num
return {mean}
elif dim == 2:
mean = []
for share in shares:
s = share.sum()
p = s
r = p / num
mean.append(r)
return mean
else:
raise AttributeError("Secure Statistics Computation only support less than 3 dims")
def secure_varience(shares, mean, N):
"""
"""
for m in mean:
for i in range(N):
mid = shares[i] - m
if i == 0:
ret = mid * mid
else:
ret += mid * mid
return ret / N
def secure_std():
"""
"""
pass
def secure_median(numList, hook, alice, bob, crypto_provider):
"""
"""
shares = numList.fix_precision().share(alice, bob, crypto_provider=crypto_provider)
print("Bits Vector Generation:")
sort_vector = []
N = len(numList)
for i in range(N):
sum = shares[i] > shares[i]
for j in range(N):
if j == i:
continue
sum += shares[i] > shares[j]
sort_vector.append(sum)
bit_vector = []
mid = torch.Tensor([N // 2]).fix_precision().share(alice, bob, crypto_provider=crypto_provider)
if N % 2 == 1:
for i in range(N):
z = sort_vector[i] == mid
bit_vector.append(z)
print("Print Bits Vector:")
b_vec_print = []
for i in range(N):
b_vec_print.append(bit_vector[i].copy().get().child.child.item())
print(b_vec_print)
median = torch.tensor([0]).fix_precision().share(alice, bob, crypto_provider=crypto_provider)
for i in range(N):
median += shares[i] * bit_vector[i]
return median
else:
for i in range(N):
mid_p = torch.Tensor([N // 2 - 1]).fix_precision().share(alice, bob, crypto_provider=crypto_provider)
z = (sort_vector[i] == mid) + (sort_vector[i] == mid_p)
bit_vector.append(z)
print("Print Bits Vector:")
b_vec_print = []
for i in range(N):
b_vec_print.append(bit_vector[i].copy().get().child.child.item())
print(b_vec_print)
median = torch.tensor([0]).fix_precision().share(alice, bob, crypto_provider=crypto_provider)
for i in range(N):
median += shares[i] * bit_vector[i]
median = median / 2
return median
def secure_mode():
"""
"""
pass