This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
key.py
80 lines (60 loc) · 2.41 KB
/
key.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
import numpy as np
from numpy.polynomial import polynomial
from typing import Tuple
from utils import coordinate_wise_random_rounding
class Key:
M: int
P: int
q: int
private_key: polynomial.Polynomial
public_key: Tuple[polynomial.Polynomial, polynomial.Polynomial]
evaluation_key: Tuple[polynomial.Polynomial, polynomial.Polynomial]
def __init__(self, M: int, P: int, q: int) -> None:
assert q > 0, "modulo q must be positive"
self.M = M
self.q = q
self.P = P
self.private_key = self.generate_private_key()
self.public_key = self.generate_public_key(self.private_key)
self.evaluation_key = self.generate_evaluation_key(
self.private_key,
self.P
)
def generate_private_key(self) -> polynomial.Polynomial:
N = self.M // 2
# Menghasilkan private key
coef = np.random.randint(0, self.q, N)
return polynomial.Polynomial(coef)
def generate_public_key(self, private_key: polynomial.Polynomial) -> Tuple[polynomial.Polynomial, polynomial.Polynomial]:
# Menghasilkan public key
N = self.M // 2
# Menghasilkan random polynomial
coefA = np.random.randint(0, self.q, N)
A = polynomial.Polynomial(coefA)
# Menghasilkan noise
coefE = coordinate_wise_random_rounding(np.random.normal(0, 1, N))
E = polynomial.Polynomial(coefE)
# Menghasilkan phi
phi = polynomial.Polynomial(
[1 if i == 0 or i == N else 0 for i in range(N + 1)]
)
# Menghasilkan public key
B = (-A * private_key + E) % phi
return B, A
def generate_evaluation_key(self, private_key: polynomial.Polynomial, P: int) -> Tuple[polynomial.Polynomial, polynomial.Polynomial]:
# Menghasilkan evaluation key
N = self.M // 2
# Menghasilkan random polynomial
coefA = np.random.randint(0, self.q, N)
A = polynomial.Polynomial(coefA)
# Menghasilkan noise
coefE = coordinate_wise_random_rounding(np.random.normal(0, 1, N))
E = polynomial.Polynomial(coefE)
# Menghasilkan phi
phi = polynomial.Polynomial(
[1 if i == 0 or i == N else 0 for i in range(N + 1)]
)
# Menghasilkan evaluation key
# With big integer support
B = (-A * private_key + E + P * private_key * private_key) % phi
return B, A