from Crypto.Util.number import *
import sys

def terminate(*args):
    print(*args)
    quit()


def receive():
    return sys.stdin.buffer.readline()

def main():
    NBIT = 32
    STEP = 40

    current_step = 1
    while current_step <= STEP:
        nbit = NBIT * current_step
        p = getPrime(nbit)
        print(f'The modulus p is: {p}')
        print('First, send the base g: ')
        g = receive().strip().decode()
        print('pow(g, x + y, p) = x * y, as x and y:')
        xy = receive().strip().decode().split(',')
        try:
            g, x, y = [int(_) for _ in [g] + xy]
        except:
            print('fail')
            exit(0)
        if (x >= p-1 or x <= 1) or (y >= p-1 or y <= 1) or x == y:
            exit(0)
        if g <= 2**24 or g >= p-1:
            exit(0)
        if pow(g, x + y, p) ==  x * y % p:
            if current_step == STEP:
                fp = open("/flag","r")
                print(fp.read())
                fp.close()
            else:
                print("good")
                current_step += 1


if __name__ == '__main__':
    main()
