-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfv12.py
61 lines (49 loc) · 1.61 KB
/
fv12.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
import numpy as np
from dataclasses import dataclass
from utils.fv import secret_keygen, public_keygen, evaluate_keygen, encrypt, decrypt, add, mul, relin
@dataclass
class Context:
n: int
d: int
T: int
t: int
c_q: int
p_q: np.poly1d
class Fv12:
def __init__(self, ctx):
self.ctx = ctx
self.sk = secret_keygen(ctx.d)
self.pk = public_keygen(self.sk, ctx.d, ctx.c_q, ctx.p_q)
self.rlks = evaluate_keygen(self.sk, ctx.d, ctx.T, ctx.c_q, ctx.p_q)
def encrypt(self, message):
ciphertext = encrypt(self.pk, message, self.ctx.d, self.ctx.t, self.ctx.c_q, self.ctx.p_q)
return Ciphertext(ciphertext, self.rlks, self.ctx.T, self.ctx.t, self.ctx.c_q, self.ctx.p_q)
def decrypt(self, ciphertext):
return decrypt(self.sk, ciphertext.inner, self.ctx.t, self.ctx.c_q, self.ctx.p_q)
class Ciphertext:
def __init__(self, inner, rlks, T, t, c_q, p_q):
self.inner = inner
self.rlks = rlks
self.T = T
self.t = t
self.c_q = c_q
self.p_q = p_q
def __add__(self, other):
return Ciphertext(
add(self.inner, other.inner, self.c_q, self.p_q),
self.rlks,
self.T,
self.t,
self.c_q,
self.p_q
)
def __mul__(self, other):
result = mul(self.inner, other.inner, self.t, self.c_q, self.p_q)
return Ciphertext(
relin(self.rlks, result[0], result[1], result[2], self.T, self.c_q, self.p_q),
self.rlks,
self.T,
self.t,
self.c_q,
self.p_q
)