from Crypto.Util.number import getPrime, bytes_to_long
from gmpy2 import *
import os
import sys
from hashlib import sha256,sha512
flag =os.getenv("GZCTF_FLAG").encode()
class LIANGZHU:
    def __init__(self):
        p, q = getPrime(512), getPrime(512)
        self.e = 65537
        self.d = invert(self.e,(p-1)*(q-1))
        self.n = p * q
    def listen(self, m): return pow(m, self.e, self.n)
    def say(self, c): return pow(c, self.d, self.n)

def my_print(message):
    sys.stdout.write(f'{message}\n')
    sys.stdout.flush()
    sys.stderr.flush()

def read_str():
    return sys.stdin.readline().strip()

def proof_of_work():
    s = hex(bytes_to_long(os.urandom(10)))[2:]
    digest = sha256(s.encode()).hexdigest()
    print(f"sha256(XXXXXX + {s[6:]})=={digest}")
    my_print("Give me XXXXXX: ")
    #print(s,s[:6])
    x = read_str()
    if len(x) != 6 or x != s[:6]:
        print("Wrong!")
        return False
    return True

def PoW():
    if not proof_of_work():
        exit(-1)

PoW()
Liangshanbo, Zhuyingtai = LIANGZHU(), LIANGZHU()
temp = os.urandom(32)
print('the flag and some noise:', Liangshanbo.listen(bytes_to_long(temp[:16] + flag + temp[16:])))

for _ in temp:
    print('Zhuyingtai listen:', Zhuyingtai.listen(Liangshanbo.say(int(input('Liangshanbo say: ')))))
    print('Liangshanbo listen:', Liangshanbo.listen(Zhuyingtai.say(int(input('Zhuyingtai say: ')))))
