/
floating_point.py
95 lines (80 loc) · 2.34 KB
/
floating_point.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
import paillier
class FloatingPoint:
def __init__(self, mantissa, exponent):
self.mantissa = mantissa
self.exponent = exponent
def __repr__(self):
return "(%s, %s)" %(self.mantissa,self.exponent)
def encryptFP(pb, x):
"""
x = 3.54 or 23.12 or 23
returns an obj of type FloatingPoint, after encrypting x with pb
"""
f = 15
e = -f
x = int(x * (10 ** f))
while x % 10 == 0:
x = x // 10
e += 1
return FloatingPoint(paillier.encrypt(pb, x), e)
def getValue(pr, pb, x):
"""
Returns the decryptred value of x in floating point type
"""
ret = paillier.decrypt(pr, pb, x.mantissa)
for _ in range(abs(x.exponent)):
if x.exponent > 0:
ret = ret * 10
else:
ret = ret / 10
return ret
def addEncEnc(pb,x, y):
"""
Adds 2 FP numbers x, y in their encryptred form & returns the result
in encrypted form as well.
"""
mantissa_x = x.mantissa
mantissa_y = y.mantissa
res_exponenet = min(y.exponent, x.exponent)
if x.exponent < y.exponent:
mantissa_y = paillier.homomorphicMul(pb,mantissa_y, 10 ** (y.exponent - x.exponent))
else:
mantissa_x = paillier.homomorphicMul(pb,mantissa_x, 10 ** (x.exponent - y.exponent))
res_mantissa = paillier.homomorphicAddC(pb,mantissa_x, mantissa_y)
return FloatingPoint(res_mantissa, res_exponenet)
def multiplyEncPlain(pb,x, y):
"""
Multiplies encrypted x with plain text FP y
"""
# rewriting y
f = 15
e = -f
y = int(y * (10 ** f))
while y % 10 == 0:
y = y // 10
e += 1
return FloatingPoint(paillier.homomorphicMul(pb, x.mantissa, y) , x.exponent + e)
def subtractEncEnc(pb,x,y):
"""
Unsafe
Subtract encrypted y from encrypted x
Do only when the result is positive
"""
# rewriting y
mantissa_x = x.mantissa
mantissa_y = y.mantissa
res_exponenet = min(y.exponent, x.exponent)
if x.exponent < y.exponent:
mantissa_y = paillier.homomorphicMul(pb,mantissa_y, 10 ** (y.exponent - x.exponent))
else:
mantissa_x = paillier.homomorphicMul(pb,mantissa_x, 10 ** (x.exponent - y.exponent))
res_mantissa = paillier.homomorphicSubCC(pb,mantissa_x, mantissa_y)
return FloatingPoint(res_mantissa, res_exponenet)
# pr,pb = paillier.generateKeypair(9)
# x = encryptFP(pb, 5)
# y = encryptFP(pb, 2)
# print(getValue(pr, pb, x))
# print(getValue(pr, pb, y))
# print(getValue(pr,pb,subtractEncEnc(pb,y,x)))
# print(getValue(pr,pb,addEncEnc(pb,x,y)))
# print(getValue(pr, pb, multiplyEncPlain(pb,x, 3)))