Skip to content

Commit ff42c80

Browse files
committed
Add core functionality
1 parent a71d2c4 commit ff42c80

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

bra12.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from dataclasses import dataclass
2+
3+
from utils.bra import keygen, encrypt, decrypt, add, mult
4+
5+
6+
@dataclass
7+
class Context:
8+
n: int
9+
q: int
10+
L: int
11+
12+
13+
class Bra12:
14+
def __init__(self, ctx):
15+
self.ctx = ctx
16+
pk, evks, sk = keygen(ctx.L, ctx.n, ctx.q)
17+
self.pk = pk
18+
self.evks = evks
19+
self.sk = sk
20+
21+
def encrypt(self, message):
22+
ciphertext = encrypt(self.pk, message, self.ctx.n, self.ctx.q)
23+
return Ciphertext(ciphertext, self.evks, self.ctx.q)
24+
25+
def decrypt(self, ciphertext):
26+
return decrypt(self.sk, ciphertext.inner, self.ctx.q)
27+
28+
29+
class Ciphertext:
30+
def __init__(self, inner, evks, q):
31+
self.inner = inner
32+
self.evks = evks
33+
self.q = q
34+
35+
def __add__(self, other):
36+
evk = self.evks[-1]
37+
return Ciphertext(
38+
add(evk, self.inner, other.inner, self.q),
39+
# Remove the key we've used used above (NOTE: An operation might not equal one circuit level)
40+
self.evks[:-1],
41+
self.q
42+
)
43+
44+
def __mul__(self, other):
45+
evk = self.evks[-1]
46+
return Ciphertext(
47+
mult(evk, self.inner, other.inner, self.q),
48+
# Remove the key we've used used above (NOTE: An operation might not equal one circuit level)
49+
self.evks[:-1],
50+
self.q
51+
)

main.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import sys
2+
3+
from bra12 import Context, Bra12
4+
from utils.core import tests as tests_utils_core
5+
from utils.regev import tests as tests_utils_regev
6+
from utils.bra import tests as tests_utils_bra
7+
8+
9+
def main():
10+
n = 3
11+
q = 2 ** 16
12+
L = 1
13+
ctx = Context(n, q, L)
14+
bra = Bra12(ctx)
15+
# Addition 1
16+
m_1 = 0
17+
m_2 = 0
18+
c_1 = bra.encrypt(m_1)
19+
c_2 = bra.encrypt(m_2)
20+
res = bra.decrypt(c_1 + c_2)
21+
print(f'{m_1} + {m_2} = {res}')
22+
assert res == (m_1 + m_2) % 2
23+
# Addition 2
24+
m_1 = 0
25+
m_2 = 1
26+
c_1 = bra.encrypt(m_1)
27+
c_2 = bra.encrypt(m_2)
28+
res = bra.decrypt(c_1 + c_2)
29+
print(f'{m_1} + {m_2} = {res}')
30+
assert res == (m_1 + m_2) % 2
31+
# Multiplication 1
32+
m_1 = 1
33+
m_2 = 0
34+
c_1 = bra.encrypt(m_1)
35+
c_2 = bra.encrypt(m_2)
36+
res = bra.decrypt(c_1 * c_2)
37+
print(f'{m_1} * {m_2} = {res}')
38+
assert res == (m_1 * m_2) % 2
39+
# Multiplication 2
40+
m_1 = 1
41+
m_2 = 1
42+
c_1 = bra.encrypt(m_1)
43+
c_2 = bra.encrypt(m_2)
44+
res = bra.decrypt(c_1 * c_2)
45+
print(f'{m_1} * {m_2} = {res}')
46+
assert res == (m_1 * m_2) % 2
47+
# Multiplication 3
48+
m_1 = 0
49+
m_2 = 0
50+
c_1 = bra.encrypt(m_1)
51+
c_2 = bra.encrypt(m_2)
52+
res = bra.decrypt(c_1 * c_2)
53+
print(f'{m_1} * {m_2} = {res}')
54+
assert res == (m_1 * m_2) % 2
55+
56+
57+
if __name__ == '__main__':
58+
if len(sys.argv) == 1:
59+
main()
60+
if len(sys.argv) > 1 and 'test' in sys.argv[1]:
61+
tests_utils_core()
62+
tests_utils_regev()
63+
tests_utils_bra()
64+
print('Success: 3/3')

0 commit comments

Comments
 (0)