from os import sys
from pygroupsig import groupsig
from pygroupsig import signature
from pygroupsig import memkey
from pygroupsig import grpkey
from pygroupsig import mgrkey
from pygroupsig import constants
from time import time

message = "Hello world"

total_setup_time = 0
total_join_time = 0
total_sig_time = 0
total_ver_time = 0
total_time = 0
#total_open_time = 0

for i in range (20):
    keys = []
    t = time()
    
    # Setup
    t_setup = time()
    _gl19 = groupsig.setup(constants.GL19_CODE)
    _gpk = _gl19['grpkey']
    isk = _gl19['mgrkey']
    gl19 = groupsig.setup(constants.GL19_CODE, _gpk)
    csk = gl19['mgrkey']
    gpk = gl19['grpkey']
    total_setup_time += time() - t_setup

    # Join
    t_join = time()
    for i in range(100):
        msg1 = groupsig.join_mgr(0, isk, gpk)
        msg2 = groupsig.join_mem(1, gpk, msgin = msg1)
        usk = msg2['memkey']
        msg3 = groupsig.join_mgr(2, isk, gpk, msg2['msgout'])
        msg4 = groupsig.join_mem(3, gpk, msgin = msg3, memkey = usk)
        usk = msg4['memkey']
        keys.append(usk)
    total_join_time += time() - t_join

    # Sign
    t_sig = time()
    sig = groupsig.sign(message, keys[8], gpk)
    total_sig_time += time() - t_sig

    # Verify
    t_ver = time()
    ver = groupsig.verify(sig, message, gpk)
    total_ver_time += time() - t_ver

    # Open
    #t_open = time()
    #open = groupsig.open(sig,isk,gpk)
    #total_open_time += time() - t_open

    total_time += time() - t

    if ver == True:
        print ("VALID signature.")
        print("")
    else:
        print ("WRONG signature.")
        print("")
        sys.exit()

total_setup_time /= 20
total_join_time /= 20
total_sig_time /= 20
total_ver_time /= 20
#total_open_time /= 20
total_time /= 20

print("Total setup: "+str(total_setup_time).replace(".",","))

print("Total join: "+str(total_join_time).replace(".",","))

print("Total sig: "+str(total_sig_time).replace(".",","))

print("Total ver: "+str(total_ver_time).replace(".",","))

#print("Total open: "+str(total_open_time).replace(".",","))

print("==================")
print("Total added: "+str(total_setup_time+total_join_time+total_sig_time+total_ver_time).replace(".",","))
print("Total: "+str(total_time).replace(".",","))


groupkey = grpkey.grpkey_export(gpk)
print("GPK")
print(len(groupkey))
print("")

userkey=memkey.memkey_export(keys[55])
print("GSK")
print(len(userkey))
print("")

ssig = signature.signature_export(sig)
print("SIG")
print(len(ssig))
print("")

print("MES")
print(len(message))
print("")

#groupsig.clear(constants.GL19_CODE, gl19['config'])
#groupsig.clear(constants.GL19_CODE, _gl19['config'])