-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathOKCN-AKCN-RLWE-CODE.py
70 lines (55 loc) · 2.61 KB
/
OKCN-AKCN-RLWE-CODE.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
"""
Released under the MIT License; see LICENSE.txt for details.
"""
from math import sqrt, exp, log, ceil
from discrete_distr import pdf_product, std_modulo, nfoldconvolution, convolution, sym_binomial, sym_uniform, dgauss
def okcn_get_d(q, n, m, g):
d = max(filter(lambda x: m*(x*2 + 1) < q*(1. - 1./g), range(q)))
return d
def akcn_get_d(q, n, m, g):
d = max(filter(lambda x: m*(x*2 + 1) < q*(1. - m*1./g), range(q)))
return d
def getVariance(v, q):
return sum(v[i] * min(i, q - i)**2 for i in v.keys())
def main():
print "RLWE with OKCN, AKCN"
bandwith = lambda q, n, g : (ceil(log(q, 2)) * n * 2 + ceil(log(g, 2)) * n + 256)*1. / 8
q, n, m = 12289, 1024, 2
reclen = n;
noise = sym_binomial(32)
noise_sqr = pdf_product(noise, noise, q)
v = nfoldconvolution(2*n, noise_sqr, q)
v = convolution(v, noise, q) # v = 2*n * (noise * noise) + noise
print getVariance(v, q), 2.*n*getVariance(noise, q)**2 + getVariance(noise, q)
g = 2**2
d = okcn_get_d(q, n, m, g)
failure_pr = sum(map(lambda x: v[x], filter(lambda x: min(x, q - x) > d, v.keys())))
print "OKCN q = {}, n = {}, m = {}, g = {}, d = {}, bandwith = {}".format(q, n, m, g, d, bandwith(q, n, g))
print "per bit pr of failure = 2^{:.2f}".format(log(failure_pr, 2))
print
g = 2**3
d = okcn_get_d(q, n, m, g)
failure_pr = sum(map(lambda x: v[x], filter(lambda x: min(x, q - x) > d, v.keys())))
print "OKCN q = {}, n = {}, m = {}, g = {}, d = {}, bandwith = {}".format(q, n, m, g, d, bandwith(q, n, g))
print "per bit pr of failure = 2^{:.2f}".format(log(failure_pr, 2))
print
g = 2**4
d = okcn_get_d(q, n, m, g)
failure_pr = sum(map(lambda x: v[x], filter(lambda x: min(x, q - x) > d, v.keys())))
print "OKCN q = {}, n = {}, m = {}, g = {}, d = {}, bandwith = {}".format(q, n, m, g, d, bandwith(q, n, g))
print "per bit pr of failure = 2^{:.2f}".format(log(failure_pr, 2))
print
g = 2**4
d = akcn_get_d(q, n, m, g)
failure_pr = sum(map(lambda x: v[x], filter(lambda x: min(x, q - x) > d, v.keys())))
print "AKCN q = {}, n = {}, m = {}, g = {}, d = {}, bandwith = {}".format(q, n, m, g, d, bandwith(q, n, g))
print "per bit pr of failure = 2^{:.2f}".format(log(failure_pr, 2))
print
g = 2**6
d = akcn_get_d(q, n, m, g)
failure_pr = sum(map(lambda x: v[x], filter(lambda x: min(x, q - x) > d, v.keys())))
print "AKCN q = {}, n = {}, m = {}, g = {}, d = {}, bandwith = {}".format(q, n, m, g, d, bandwith(q, n, g))
print "per bit pr of failure = 2^{:.2f}".format(log(failure_pr, 2))
print
if __name__ == "__main__":
main()