Last active
July 29, 2019 12:23
-
-
Save bquast/0f72bb910a3c6747c55eb73894b54f29 to your computer and use it in GitHub Desktop.
Adapted from: https://asecuritysite.com/encryption/lwe4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# adapted from: https://asecuritysite.com/encryption/lwe4 | |
import sys | |
import numpy as np | |
import random | |
import math | |
nvals=20 | |
B=[] | |
e=[] | |
s = 20 | |
M1 = 5 | |
M2 = 7 | |
q=97 | |
def get_uv(A,B,M,q): | |
u=0 | |
v=0 | |
sample= random.sample(range(nvals-1), 5) | |
print( sample) | |
for x in range(0,len(sample)): | |
print("[",A[sample[x]],B[sample[x]],"]",) | |
u=u+(A[sample[x]]) | |
v= v+B[sample[x]] | |
v=v+math.floor(q/2)*M | |
return u%q,v%q | |
def get_result(u1,v1,u2,v2,q): | |
res=((v1-s*u1) + (v2-s*u2)) % q | |
if (res>q/2): | |
return 1 | |
return 0 | |
def tobits(val): | |
l = [0]*(8) | |
l[0]=val & 0x1 | |
l[1]=(val & 0x2)>>1 | |
l[2]=(val & 0x4)>>2 | |
l[3]=(val & 0x8)>>3 | |
return l | |
A = random.sample(range(q), nvals) | |
for x in range(0,len(A)): | |
e.append(random.randint(1,3)) | |
B.append((A[x]*s+e[x])%q) | |
bits1 = tobits(M1) | |
bits2 = tobits(M2) | |
u1_1,v1_1=get_uv(A,B,bits1[0],q) | |
u2_1,v2_1=get_uv(A,B,bits1[1],q) | |
u3_1,v3_1=get_uv(A,B,bits1[2],q) | |
u4_1,v4_1=get_uv(A,B,bits1[3],q) | |
u1_2,v1_2=get_uv(A,B,bits2[0],q) | |
u2_2,v2_2=get_uv(A,B,bits2[1],q) | |
u3_2,v3_2=get_uv(A,B,bits2[2],q) | |
u4_2,v4_2=get_uv(A,B,bits2[3],q) | |
get_result(u1_1,v1_1,u1_2,v1_2,q) | |
get_result(u2_1,v2_1,u2_2,v2_2,q) | |
get_result(u2_1,v2_1,u2_2,v2_2,q) | |
get_result(u2_1,v2_1,u2_2,v2_2,q) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment