I am given a set of $(u, v)$ values, matrix $A$, primary key vector, private key vector, error vector and prime $q$. I wanted to recover the binary value of each $(u, v)$ pairs using LWE decryption.
The formula I used to get that was: $\mbox{result} = v - s u$, where $s$ is the private key. I then compared the result with $q//2$. If result is more than $q//2$, the output is 1. If the result is less than $q//2$. However, I am unable to recover the correct binary values. This is the Python code snippet that I used to get the binary value.
import numpy as np
import json
m = ''
def computeM(u, v, q, s):
global m
res=(np.subtract(v, np.dot(s, u))) % q
if (res > q//2):
m += '1'
else:
m += '0'
# retrieve u and v from JSON
f = open('CarlDavisJSON.json')
data = json.load(f)
# Extract the u and v values from the messages array
u_json = []
v_json = []
s = ""
for message in data['exercise']['messages'][0]['cipher']:
u_json.append(message['u'])
v_json.append(message['v'])
s = data['exercise']['alice']['sk']
q = data['exercise']['alice']['q']
# store u and v in numpy array
u_list = np.array(u_json)
v_list = np.array(v_json)
# compute each binary from u and v
for i in range(len(u_list)):
u = np.array(u_list[i])
v = np.array(v_list[i])
# print(v)
computeM(u, v, q, s)
print(m)
I have checked that the correct values are used. What do you think have gone wrong with the method?
Update: I have edited this question with the full codes and the JSON file for this implementation.
These are my JSON file containing prime q, secret key sk and (u, v) ciphertext pairs for decryption.
{
"srn": "887766554",
"name": "Carl Davis",
"exercise": {
"alice": {
"pk": [ 8, 9, 6, 5, 4, 15, 21 ],
"sk": [ 2, 21, 3, 6, 23 ],
"e": [ -1, 1, 1, -1, 0, 0, 0 ],
"q": 29,
"a":
[[ 7, 21, 26, 19, 0 ],
[ 0, 14, 1, 5, 0 ],
[ 13, 21, 11, 18, 28 ],
[ 16, 5, 15, 13, 23 ],
[ 21, 22, 3, 1, 23 ],
[ 4, 21, 21, 1, 21 ],
[ 18, 0, 22, 16, 15 ]]
},
"messages": [
{
"cipher": [
{ "u": [ 1, 19, 3, 2, 24 ], "v": 16 },
{ "u": [ 3, 20, 22, 26, 15 ], "v": 21 },
{ "u": [ 7, 3, 24, 26, 22 ], "v": 13 },
{ "u": [ 9, 20, 7, 25, 14 ], "v": 5 },
{ "u": [ 28, 11, 26, 22, 16 ], "v": 23 },
{ "u": [ 18, 11, 20, 23, 8 ], "v": 26 },
{ "u": [ 16, 27, 3, 10, 14 ], "v": 18 },
{ "u": [ 15, 4, 16, 9, 17 ], "v": 11 },
{ "u": [ 15, 4, 16, 9, 17 ], "v": 26 },
{ "u": [ 12, 4, 11, 20, 9 ], "v": 18 },
{ "u": [ 9, 27, 2, 0, 14 ], "v": 0 },
{ "u": [ 12, 11, 6, 24, 9 ], "v": 14 },
{ "u": [ 12, 20, 12, 14, 22 ], "v": 27 },
{ "u": [ 3, 20, 22, 26, 15 ], "v": 7 },
{ "u": [ 24, 19, 1, 14, 20 ], "v": 9 },
{ "u": [ 9, 11, 1, 6, 1 ], "v": 6 },
{ "u": [ 9, 20, 7, 25, 14 ], "v": 5 },
{ "u": [ 1, 19, 3, 2, 24 ], "v": 1 },
{ "u": [ 21, 4, 1, 8, 16 ], "v": 9 },
{ "u": [ 0, 27, 12, 12, 7 ], "v": 24 },
{ "u": [ 27, 6, 28, 7, 0 ], "v": 2 },
{ "u": [ 13, 5, 22, 25, 6 ], "v": 6 },
{ "u": [ 9, 11, 1, 6, 1 ], "v": 21 },
{ "u": [ 16, 18, 26, 20, 1 ], "v": 5 },
{ "u": [ 16, 18, 26, 20, 1 ], "v": 20 },
{ "u": [ 28, 11, 26, 22, 16 ], "v": 8 },
{ "u": [ 16, 27, 3, 10, 14 ], "v": 18 },
{ "u": [ 12, 20, 12, 14, 22 ], "v": 27 },
{ "u": [ 12, 4, 11, 20, 9 ], "v": 4 },
{ "u": [ 13, 5, 22, 25, 6 ], "v": 6 },
{ "u": [ 15, 4, 16, 9, 17 ], "v": 26 },
{ "u": [ 11, 10, 15, 22, 14 ], "v": 19 }
]
},
{
"text": "node"
}
]
},
"solution": {
"messages": [
{
"text": "game",
"cipher": [
{ "u": [ 1, 19, 3, 2, 24 ], "v": 16 },
{ "u": [ 3, 20, 22, 26, 15 ], "v": 21 },
{ "u": [ 7, 3, 24, 26, 22 ], "v": 13 },
{ "u": [ 9, 20, 7, 25, 14 ], "v": 5 },
{ "u": [ 28, 11, 26, 22, 16 ], "v": 23 },
{ "u": [ 18, 11, 20, 23, 8 ], "v": 26 },
{ "u": [ 16, 27, 3, 10, 14 ], "v": 18 },
{ "u": [ 15, 4, 16, 9, 17 ], "v": 11 },
{ "u": [ 15, 4, 16, 9, 17 ], "v": 26 },
{ "u": [ 12, 4, 11, 20, 9 ], "v": 18 },
{ "u": [ 9, 27, 2, 0, 14 ], "v": 0 },
{ "u": [ 12, 11, 6, 24, 9 ], "v": 14 },
{ "u": [ 12, 20, 12, 14, 22 ], "v": 27 },
{ "u": [ 3, 20, 22, 26, 15 ], "v": 7 },
{ "u": [ 24, 19, 1, 14, 20 ], "v": 9 },
{ "u": [ 9, 11, 1, 6, 1 ], "v": 6 },
{ "u": [ 9, 20, 7, 25, 14 ], "v": 5 },
{ "u": [ 1, 19, 3, 2, 24 ], "v": 1 },
{ "u": [ 21, 4, 1, 8, 16 ], "v": 9 },
{ "u": [ 0, 27, 12, 12, 7 ], "v": 24 },
{ "u": [ 27, 6, 28, 7, 0 ], "v": 2 },
{ "u": [ 13, 5, 22, 25, 6 ], "v": 6 },
{ "u": [ 9, 11, 1, 6, 1 ], "v": 21 },
{ "u": [ 16, 18, 26, 20, 1 ], "v": 5 },
{ "u": [ 16, 18, 26, 20, 1 ], "v": 20 },
{ "u": [ 28, 11, 26, 22, 16 ], "v": 8 },
{ "u": [ 16, 27, 3, 10, 14 ], "v": 18 },
{ "u": [ 12, 20, 12, 14, 22 ], "v": 27 },
{ "u": [ 12, 4, 11, 20, 9 ], "v": 4 },
{ "u": [ 13, 5, 22, 25, 6 ], "v": 6 },
{ "u": [ 15, 4, 16, 9, 17 ], "v": 26 },
{ "u": [ 11, 10, 15, 22, 14 ], "v": 19 }
]
},
{
"text": "node",
"cipher": [
{ "u": [ 7, 3, 24, 26, 22 ], "v": 28 },
{ "u": [ 12, 20, 12, 14, 22 ], "v": 12 },
{ "u": [ 27, 3, 5, 9, 15 ], "v": 22 },
{ "u": [ 17, 28, 23, 12, 9 ], "v": 13 },
{ "u": [ 27, 3, 5, 9, 15 ], "v": 22 },
{ "u": [ 4, 3, 19, 8, 14 ], "v": 20 },
{ "u": [ 25, 18, 16, 8, 8 ], "v": 25 },
{ "u": [ 18, 11, 20, 23, 8 ], "v": 12 },
{ "u": [ 24, 19, 1, 14, 20 ], "v": 9 },
{ "u": [ 10, 19, 22, 19, 2 ], "v": 21 },
{ "u": [ 9, 27, 2, 0, 14 ], "v": 0 },
{ "u": [ 11, 10, 15, 22, 14 ], "v": 5 },
{ "u": [ 9, 11, 1, 6, 1 ], "v": 6 },
{ "u": [ 28, 11, 26, 22, 16 ], "v": 8 },
{ "u": [ 9, 20, 7, 25, 14 ], "v": 19 },
{ "u": [ 28, 11, 26, 22, 16 ], "v": 8 },
{ "u": [ 10, 19, 22, 19, 2 ], "v": 7 },
{ "u": [ 4, 19, 8, 20, 3 ], "v": 23 },
{ "u": [ 12, 20, 12, 14, 22 ], "v": 12 },
{ "u": [ 12, 11, 6, 24, 9 ], "v": 14 },
{ "u": [ 22, 18, 11, 19, 0 ], "v": 18 },
{ "u": [ 26, 12, 12, 6, 3 ], "v": 24 },
{ "u": [ 9, 20, 7, 25, 14 ], "v": 5 },
{ "u": [ 11, 10, 15, 22, 14 ], "v": 5 },
{ "u": [ 16, 27, 3, 10, 14 ], "v": 4 },
{ "u": [ 1, 6, 4, 25, 8 ], "v": 24 },
{ "u": [ 13, 5, 22, 25, 6 ], "v": 6 },
{ "u": [ 14, 28, 18, 23, 1 ], "v": 20 },
{ "u": [ 25, 18, 16, 8, 8 ], "v": 11 },
{ "u": [ 19, 11, 7, 5, 9 ], "v": 17 },
{ "u": [ 12, 4, 11, 20, 9 ], "v": 4 },
{ "u": [ 26, 12, 12, 6, 3 ], "v": 24 }
]
}
]
}
}
The first group of (u, v) ciphertext pairs should encrypt to game which has the binary values of 01100111011000010110110101100101
.