from zlib import compress, decompress
from base64 import b64encode as b64e, b64decode as b64d
def __init__(self, q, n, n_bar, m_bar):
self.__S_a = np.matrix(np.random.randint(-1, 2, size = (self.n, self.n_bar)))
self.__E_a = np.matrix(np.random.randint(-1, 2, size = (self.n, self.n_bar)))
self.A = np.matrix(np.random.randint( 0, q, size = (self.n, self.n)))
self.B = np.mod(self.A * self.__S_a + self.__E_a, self.q)
return round((x / (self.q / 4)))
out = np.vectorize(recenter)(mat)
out = np.vectorize(mult_and_round)(out)
def __decaps(self, U, C):
key_a = self.__decode(np.mod(C - np.dot(U, self.__S_a), self.q))
def check_exchange(self, U, C, key_b):
key_a = self.__decaps(U, C)
return (key_a == key_b).all()
def check_sk(self, S_a, E_a):
return (S_a == self.__S_a).all() and (E_a == self.__E_a).all()
print("Possible actions:")
print(" [1] Key exchange")
return int(input(">>> "))
if __name__ == "__main__":
server = Server(q, n, n_bar, m_bar)
print("Here are the server public parameters:")
print("A = {}".format(b64e(compress(A.tobytes())).decode()))
print("B = {}".format(b64e(compress(B.tobytes())).decode()))
print("Key exchange #{}".format(nbQueries), file = sys.stderr)
U = np.reshape(np.frombuffer(decompress(b64d(input("U = "))), dtype = np.int64), (m_bar, n))
C = np.reshape(np.frombuffer(decompress(b64d(input("C = "))), dtype = np.int64), (m_bar, n_bar))
key_b = np.reshape(np.frombuffer(decompress(b64d(input("key_b = "))), dtype = np.int64), (m_bar, n_bar))
if server.check_exchange(U, C, key_b):
print("Success, the server and the client share the same key!")
S_a = np.reshape(np.frombuffer(decompress(b64d(input("S_a = "))), dtype = np.int64), (n, n_bar))
E_a = np.reshape(np.frombuffer(decompress(b64d(input("E_a = "))), dtype = np.int64), (n, n_bar))
if server.check_sk(S_a, E_a):
print("Correct key, congratulations! Here is the flag: {}".format(flag))
print("Sorry, this is not the correct key.")