-
Notifications
You must be signed in to change notification settings - Fork 0
/
module_lwe.py
56 lines (39 loc) · 1.17 KB
/
module_lwe.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
from ring_lwe import polyadd, polymul
import numpy as np
def parameters():
# polynomial modulus degree
n = 2**2
# ciphertext modulus
q = 67
# polynomial modulus
poly_mod = np.array([1] + [0] * (n - 1) + [1])
#module rank
k = 2
return (n,q,poly_mod,k)
def add_vec(v0, v1, q, f):
assert(len(v0) == len(v1)) # sizes need to be the same
result = []
for i in range(len(v0)):
result.append(polyadd(v0[i], v1[i], q, f))
return result
def mul_vec_simple(v0, v1, f, q):
assert(len(v0) == len(v1)) # sizes need to be the same
degree_f = len(f) - 1
result = [0 for i in range(degree_f - 1)]
# textbook vector inner product
for i in range(len(v0)):
result = polyadd(result, polymul(v0[i], v1[i], q, f), q, f)
return result
def mul_mat_vec_simple(m, a, f, q):
result = []
# textbook matrix-vector multiplication
for i in range(len(m)):
result.append(mul_vec_simple(m[i], a, f, q))
return result
#transpose of a matrix
def transpose(m):
result = [[None for i in range(len(m))] for j in range(len(m[0]))]
for i in range(len(m)):
for j in range(len(m[0])):
result[j][i] = m[i][j]
return result