from Crypto import Random
from Crypto.Random import random
from Crypto.PublicKey import ElGamal
from Crypto.Util.number import GCD
from Crypto.Hash import SHA
#import binascii



#d = open('plaintexts_1024_2.txt', 'r')
f = open('ciphertexts.txt', 'r')
t = open('private_key.txt', 'r')
w = open('solutions.txt', 'w')

key = []
key.extend(t.readlines())
g = long(key[1].split()[-1])
y = long(key[2].split()[-1])
p = long(key[3].split()[-1])
x = long(key[4].split()[-1])
key = ElGamal.construct((p,g,y,x))

cip = []
cip.extend(f.readlines())

enc_2 = []
for i in range(len(cip)):
	temp = cip[i].split(',')
	tup = (long(temp[0].strip('(')), long(temp[1].strip().strip(')')))
	enc_2.append(tup)


temp_1 = (enc_2[0][0], (6*enc_2[0][1])%p)
enc_2[0] = temp_1
temp_2 =(enc_2[2][0], (2*enc_2[2][1])%p)
enc_2[2]= temp_2
temp_3 =(enc_2[3][0], (enc_2[3][1]*(pow(4,p-2,p))%p))
enc_2[3] = temp_3
temp_4 = ((enc_2[0][0]*enc_2[3][0])%p, (enc_2[0][1]*enc_2[3][1])%p)
enc_2[5] = temp_4
temp_5 = ((enc_2[1][0]*enc_2[2][0]*enc_2[4][0])%p, (enc_2[1][1]*enc_2[2][1]*enc_2[4][1])%p)
enc_2[6] = temp_5
temp_6 = ((enc_2[3][0]*enc_2[6][0])%p, (enc_2[3][1]*enc_2[6][1])%p)
enc_2[7] = temp_6

dec_2 =[]
for i in enc_2:
	w.write(str(i).replace('L','')+'\n')
	dec_2.append(key.decrypt(i))

print dec_2
	