-
Notifications
You must be signed in to change notification settings - Fork 2
/
bra12.py
51 lines (40 loc) · 1.31 KB
/
bra12.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 dataclasses import dataclass
from utils.bra import keygen, encrypt, decrypt, add, mult
@dataclass
class Context:
n: int
q: int
L: int
class Bra12:
def __init__(self, ctx):
self.ctx = ctx
pk, evks, sk = keygen(ctx.L, ctx.n, ctx.q)
self.pk = pk
self.evks = evks
self.sk = sk
def encrypt(self, message):
ciphertext = encrypt(self.pk, message, self.ctx.n, self.ctx.q)
return Ciphertext(ciphertext, self.evks, self.ctx.q)
def decrypt(self, ciphertext):
return decrypt(self.sk, ciphertext.inner, self.ctx.q)
class Ciphertext:
def __init__(self, inner, evks, q):
self.inner = inner
self.evks = evks
self.q = q
def __add__(self, other):
evk = self.evks[-1]
return Ciphertext(
add(evk, self.inner, other.inner, self.q),
# Remove the key we've used used above (NOTE: An operation might not equal one circuit level)
self.evks[:-1],
self.q
)
def __mul__(self, other):
evk = self.evks[-1]
return Ciphertext(
mult(evk, self.inner, other.inner, self.q),
# Remove the key we've used used above (NOTE: An operation might not equal one circuit level)
self.evks[:-1],
self.q
)