-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphe-bee-sandbox.py
66 lines (43 loc) · 1.32 KB
/
phe-bee-sandbox.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
from prime import *
import random
# encryption function
def encrypt(m, r, exponential=False):
c1 = pow(g, r, p)
if exponential is True:
c2 = (pow(g, m, p) * pow(y, r, p)) % p
else:
c2 = (m * pow(y, r, p)) % p
return c1, c2
# decryption function
def decrypt(c1, c2):
return (c2 * pow(c1, -1*x, p)) % p
# key generation
p = generate_random_prime_number(bits=1024)
g = random.randint(1, p-1)
x = generate_random_prime_number(bits=512) # private key
y = pow(g, x, p) # public key
print(x, y)
m = 100 # message to encrypt
r = random.randint(1, p-1) # random encryption number
print(m, r)
c1, c2 = encrypt(m, r)
p = decrypt(c1, c2)
print(c1, c2, p)
# multiplicative homomorphic
m1 = 9
m2 = 11
r1 = random.randint(1, p-1)
r2 = random.randint(1, p-1)
m1_encrypted = encrypt(m1, r1)
m2_encrypted = encrypt(m2, r2)
h1 = encrypt(m1*m2, r1+r2)
h2 = m1_encrypted[0]*m2_encrypted[0] % p, m1_encrypted[1]*m2_encrypted[1] % p
print(h1, h2)
assert h1 == h2
# additive homomorphic
m1_encrypted = encrypt(m1, r1, exponential=True)
m2_encrypted = encrypt(m2, r2, exponential=True)
h1 = encrypt(m1+m2, r1+r2, exponential=True)
h2 = m1_encrypted[0]*m2_encrypted[0] % p, m1_encrypted[1]*m2_encrypted[1] % p
print(h1, h2)
assert h1 == h2