from gmerkle import g_merkle_GSS as GSS

def main():
    run = True

    print("Interactive mode for proof of concept G-Merkle group signature scheme.")

    try:
        members = int(input("Number of group members: "))
        signatures = int(input("Number of signatures per group member: "))
    except ValueError:
        print("Wrong parameters for the GSS. Exiting program.")
        return

    if (0 < members < 200) and (0 < signatures < 1025):
        if (members > 64 or signatures > 200):
            print("Selected GSS parameters are big, scheme setup might take a while.\n")
        gmerkle_gs = GSS(int(members), int(signatures))
        gpk = gmerkle_gs.gpk
    else:
        if members == 0 or signatures == 0:
            print("Parameters can't be zero.")
        else:
            print("Big GSS parameters - tree would take too long to build. Exiting program.")
        return
    
    while(run):     
        # signing member id
        print("GROUP SIGNATURE on scheme with given parameters:")

        print('=================== SIGNING PHASE ================================')
        try:
            id = int(input(f"Signing member id (1-{members}): "))
        except ValueError:
            print("Wrong parameters for the GSS. Exiting program.")
            return
        message = input("Message: ")
        signature = gmerkle_gs.sign(id, message)
 
        print('=================== VERIFICATION PHASE ===========================')
        message = input("Verify message: ")
        verification = gmerkle_gs.verify(signature, message, gpk)
        print("\n")
        print (f"Signature verification result: \t{verification}")
        
        print('=================== OPENING PHASE ================================')
        message = input("Open message: ")
        open = gmerkle_gs.open(signature, message)
        print (f"Signature opening result: \t{open}")
        print('==================================================================')
        print("\nPress Enter for more signatures. Type \"exit\" to quit the program.")
        cont = input()
        if (cont.lower()=="exit"):
            run = False

if __name__ == '__main__':
    main()