-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicSA.py
More file actions
50 lines (43 loc) · 1.36 KB
/
BasicSA.py
File metadata and controls
50 lines (43 loc) · 1.36 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
# Lab-SA Basic SA for Federated Learning
import random, math
import SecureProtocol as sp
# Get common values for server set-up: n, t, ...
def getCommonValues():
# R: domain
# g: generator, p: prime
R = 100 #temp
commonValues = {"g": sp.g, "p": sp.p, "R": R}
return commonValues
# Generate two key pairs
def generateKeyPairs():
c_pk, c_sk = sp.generateKeyPair(sp.g, sp.p)
s_pk, s_sk = sp.generateKeyPair(sp.g, sp.p)
return (c_pk, c_sk), (s_pk, s_sk)
def stochasticQuantization(weights, q, p):
# weights = local model of user
# q = quantization level
# p = large prime
quantized = []
for x in weights:
floor_qx = math.floor(q * x)
selected = int(random.choices(
population = [floor_qx / q, (floor_qx + 1) / q],
weights = [1 - (q * x - floor_qx), q * x - floor_qx],
k = 1 # select one
)[0] * q)
if selected < 0:
selected = selected + p
quantized.append(selected)
return quantized
def convertToRealDomain(weights, q, p):
# weights = local model of user
# q = quantization level
# p = large prime
real_numbers = []
m = (p - 1) / 2
for x in weights:
if 0 <= x and x < m:
real_numbers.append(x / q)
else: # (p-1)/2 <= x < p
real_numbers.append((x - p) / q)
return real_numbers