-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
51 lines (37 loc) · 1.29 KB
/
server.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
from MachineLearningModel import LinModel
import phe as paillier
import json
"""
The homomorphic properties of the Paillier crypto system are:
1. Encrypted numbers can be multiplied by a non encrypted scalar.
2. Encrypted numbers can be added together.
3. Encrypted numbers can be added to non encrypted scalars.
"""
def getData():
with open('data.json', 'r') as file:
d = json.load(file)
data = json.loads(d)
return data
def computeData():
data = getData()
mycoef = LinModel().getCoef()
pk = data['public_key']
pubkey = paillier.PaillierPublicKey(n=int(pk['n']))
enc_nums_rec = [paillier.EncryptedNumber(pubkey, int(x[0], int(x[1]))) for x in data['values']]
results = sum([mycoef[i] * enc_nums_rec[i] for i in range(len(mycoef))])
return results, pubkey
def serializeData():
results, pubkey = computeData()
encrypted_data = {}
encrypted_data['pubkey'] = {'n': pubkey.n}
encrypted_data['values'] = (str(results.ciphertext()), results.exponent)
serialized = json.dumps(encrypted_data)
return serialized
print(sum([data[i] * mycoef[i] for i in range(len(data))]))
def main():
datafile = serializeData()
with open('answer.json', 'w') as file:
json.dump(datafile, file)
# Driver function
if __name__ == '__main__':
main()