├── README.md └── rlwe_kex.py /README.md: -------------------------------------------------------------------------------- 1 | # RLWE-KEX 2 | 3 | Simple RLWE-KEX example using Python. 4 | -------------------------------------------------------------------------------- /rlwe_kex.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from numpy.polynomial import polynomial as p 3 | 4 | n = 1024 5 | q = 2**32-1 6 | hlpr = [1] + [0] * (n-1) + [1] 7 | 8 | 9 | def gen_poly(n,q): 10 | global hlpr 11 | l = 0 #Gamma Distribution Location (Mean "center" of dist.) 12 | poly = np.floor(np.random.normal(l,size=(n))) 13 | while (len(poly) != n): 14 | poly = np.floor(np.random.normal(l,size=(n))) 15 | poly = np.floor(p.polydiv(poly,hlpr)[1]%q) 16 | return poly 17 | 18 | 19 | 20 | #Generate A 21 | A = np.floor(np.random.random(size=(n))*q)%q 22 | 23 | A = np.floor(p.polydiv(A,hlpr)[1]) 24 | 25 | #Alice (Secret & Error) 26 | sA = gen_poly(n,q) 27 | eA = gen_poly(n,q) 28 | 29 | bA = p.polymul(A,sA)%q 30 | bA = np.floor(p.polydiv(sA,hlpr)[1]) 31 | bA = p.polyadd(bA,eA)%q 32 | 33 | 34 | #Bob 35 | sB = gen_poly(n,q) 36 | eB = gen_poly(n,q) 37 | 38 | 39 | bB = p.polymul(A,sB)%q 40 | bB = np.floor(p.polydiv(sB,hlpr)[1]) 41 | bB = p.polyadd(bB,eB)%q 42 | 43 | 44 | #Shared Secret 45 | #Alice 46 | sharedAlice = np.floor(p.polymul(sA,bB)%q) 47 | sharedAlice = np.floor(p.polydiv(sharedAlice,hlpr)[1])%q #TODO FIX THIS HAS TO BE DIVED BY HELPER 48 | sharedBob = np.floor(p.polymul(sB,bA)%q) 49 | sharedBob = np.floor(p.polydiv(sharedBob,hlpr)[1])%q 50 | 51 | #Error Rounding 52 | #--Bob 53 | u = np.asarray([0] * n) 54 | i = 0 55 | 56 | while (i < len(u)): 57 | if (len(bB) <= i): break; 58 | if (int(bB[i]/(q/4)) == 0): u[i] = 0 59 | elif (int(bB[i]/(q/2)) == 0): u[i] = 1 60 | elif (int(bB[i]/(3*q/4)) == 0): u[i] = 0 61 | elif (int(bB[i]/(q)) == 0): u[i] = 1 62 | else: 63 | print "error! (1)" 64 | i+=1 65 | 66 | 67 | i = 0 68 | while (i < len(u)): 69 | #Region 0 (0 --- q/4 and q/2 --- 3q/4) 70 | if (u[i] == 0): 71 | if (sharedBob[i] >= q*0.125 and sharedBob[i] < q*0.625): 72 | sharedBob[i] = 1 73 | else: 74 | sharedBob[i] = 0 75 | 76 | 77 | #Region 1 (q/4 --- q/2 and 3q/4 --- q) 78 | elif (u[i] == 1): 79 | if (sharedBob[i] >= q*0.875 and sharedBob[i] < q*0.375): 80 | sharedBob[i] = 0 81 | else: 82 | sharedBob[i] = 1 83 | 84 | else: 85 | print "error! (2)" 86 | 87 | i += 1 88 | 89 | #--Alice 90 | i = 0 91 | while (i < len(u)): 92 | #Region 0 (0 --- q/4 and q/2 --- 3q/4) 93 | if (u[i] == 0): 94 | if (sharedAlice[i] >= q*0.125 and sharedAlice[i] < q*0.625): 95 | sharedAlice[i] = 1 96 | else: 97 | sharedAlice[i] = 0 98 | 99 | 100 | #Region 1 (q/4 --- q/2 and 3q/4 --- q) 101 | elif (u[i] == 1): 102 | if (sharedAlice[i] >= q*0.875 and sharedAlice[i] < q*0.375): 103 | sharedAlice[i] = 0 104 | else: 105 | sharedAlice[i] = 1 106 | 107 | else: 108 | print "error! (3)" 109 | i += 1 110 | 111 | 112 | 113 | 114 | 115 | # 116 | print "A:",len(A),"|",A 117 | print "\n-Alice---" 118 | print " s:",len(sA),"|",sA 119 | print " e:",len(eA),"|",eA 120 | print " b:",len(bA),"|",bA 121 | print "\n-Bob---" 122 | print " s':",len(sB),"|",sB 123 | print " e':",len(eB),"|",eB 124 | print " b':",len(bB),"|",bB 125 | print " u :",len(u),"|",u 126 | print "\n" 127 | print "Shared Secret Alice:",len(sharedAlice),"|",sharedAlice 128 | print "Shared Secret Bob:",len(sharedBob),"|",sharedBob 129 | 130 | 131 | print "\n\n--Verification--" 132 | i = 0 133 | while (i < len(sharedBob)): 134 | if (sharedAlice[i] != sharedBob[i]): 135 | print "Error at index",i 136 | i+=1 --------------------------------------------------------------------------------