def eval_reenc(timing_rounds):
    tp = tc.ThresholdParameters(5, 10)

    old_pub_key, old_shares = tc.create_public_key_and_shares_centralized(
        GLOBAL_CP, tp)
    new_pub_key, new_shares = tc.create_public_key_and_shares_centralized(
        GLOBAL_CP, tp)
    t_old_shares = old_shares[:tp.t]
    t_new_shares = new_shares[:tp.t]
    t_old_shares_x = [share.x for share in t_old_shares]
    t_new_shares_x = [share.x for share in t_new_shares]
    old_lc = [
        tc.lagrange_coefficient_for_key_share_indices(t_old_shares_x, s,
                                                      GLOBAL_CP)
        for s in t_old_shares_x
    ]
    new_lc = [
        tc.lagrange_coefficient_for_key_share_indices(t_new_shares_x, s,
                                                      GLOBAL_CP)
        for s in t_new_shares_x
    ]
    prek = []
    for os, olc, ns, nlc in zip(t_old_shares, old_lc, t_new_shares, new_lc):
        prek.append(tc.compute_partial_re_encryption_key(os, olc, ns, nlc))
    re_encryption_key = tc.combine_partial_re_encryption_keys(
        prek, old_pub_key, new_pub_key, tp, tp)

    em = tc.encrypt_message("a", old_pub_key)

    eval_performance("ReEncrypt",
                     "",
                     tc.re_encrypt_message,
                     em=em,
                     re_key=re_encryption_key,
                     timing_rounds=timing_rounds)
def eval_rek():
    # for t, n in GLOBAL_VAR_TP_PARAMS:
    t, n = 3, 5
    tp = tc.ThresholdParameters(t, n)
    _, old_shares = tc.create_public_key_and_shares_centralized(GLOBAL_CP, tp)
    _, new_shares = tc.create_public_key_and_shares_centralized(GLOBAL_CP, tp)
    t_old_shares = old_shares[:t]
    t_new_shares = new_shares[:t]
    t_old_shares_x = [share.x for share in t_old_shares]
    t_new_shares_x = [share.x for share in t_new_shares]
    old_lc = [
        tc.lagrange_coefficient_for_key_share_indices(t_old_shares_x, s,
                                                      GLOBAL_CP)
        for s in t_old_shares_x
    ]
    new_lc = [
        tc.lagrange_coefficient_for_key_share_indices(t_new_shares_x, s,
                                                      GLOBAL_CP)
        for s in t_new_shares_x
    ]
    prek = []
    for os, olc, ns, nlc in zip(t_old_shares, old_lc, t_new_shares, new_lc):
        prek.append(tc.compute_partial_re_encryption_key(os, olc, ns, nlc))

    eval_performance("ReEncryptionKeyCombination",
                     "({}, {})".format(t, n),
                     tc.combine_partial_re_encryption_keys,
                     partial_keys=prek,
                     old_threshold_params=tp,
                     new_threshold_params=tp)
def eval_dkg(timing_rounds):
    for t, n in GLOBAL_VAR_TP_PARAMS:
        tp = tc.ThresholdParameters(t, n)
        eval_performance("DKG",
                         "({}, {})".format(t, n),
                         run_dkg_centralized,
                         timing_rounds=timing_rounds,
                         thresh_params=tp,
                         curve_params=GLOBAL_CP)
def eval_ckg(timing_rounds):
    for t, n in GLOBAL_VAR_TP_PARAMS:
        tp = tc.ThresholdParameters(t, n)

        eval_performance("CKG",
                         "({}, {})".format(t, n),
                         tc.create_public_key_and_shares_centralized,
                         curve_params=GLOBAL_CP,
                         threshold_params=tp,
                         timing_rounds=timing_rounds)
def eval_pd(timing_rounds):
    tp = tc.ThresholdParameters(3, 5)
    pub_key, shares = tc.create_public_key_and_shares_centralized(
        GLOBAL_CP, tp)
    em = tc.encrypt_message("a", pub_key)
    eval_performance("PartialDecryption",
                     "",
                     tc.compute_partial_decryption,
                     encrypted_message=em,
                     key_share=shares[0],
                     timing_rounds=timing_rounds)
def eval_enc(timing_rounds):
    tp = tc.ThresholdParameters(3, 5)
    pub_key, shares = tc.create_public_key_and_shares_centralized(
        GLOBAL_CP, tp)

    for msg_size in MESSAGE_BYTE_SIZES:
        msg = "a" * msg_size  # since encryption uses utf-8 encoding, this leads to messages of size msg_size
        eval_performance("Encrypt",
                         "{}".format(msg_size),
                         tc.encrypt_message,
                         message=msg,
                         public_key=pub_key,
                         timing_rounds=timing_rounds)
def eval_prek():
    tp = tc.ThresholdParameters(5, 10)
    _, old_shares = tc.create_public_key_and_shares_centralized(GLOBAL_CP, tp)
    _, new_shares = tc.create_public_key_and_shares_centralized(GLOBAL_CP, tp)
    t_old_shares_x = [share.x for share in old_shares[:tp.t]]
    t_new_shares_x = [share.x for share in new_shares[:tp.t]]
    old_lc = tc.lagrange_coefficient_for_key_share_indices(
        t_old_shares_x, t_old_shares_x[0], GLOBAL_CP)
    new_lc = tc.lagrange_coefficient_for_key_share_indices(
        t_new_shares_x, t_new_shares_x[0], GLOBAL_CP)

    eval_performance("PartialReEncryptionKey",
                     "",
                     tc.compute_partial_re_encryption_key,
                     old_share=old_shares[0],
                     old_lc=old_lc,
                     new_share=new_shares[0],
                     new_lc=new_lc)
def eval_dec_msg_size(timing_rounds, t=2, n=3):
    tp = tc.ThresholdParameters(t, n)
    pub_key, shares = tc.create_public_key_and_shares_centralized(
        GLOBAL_CP, tp)

    for msg_size in MESSAGE_BYTE_SIZES:
        msg = "a" * msg_size
        enc_msg = tc.encrypt_message(msg, pub_key)
        pds = [
            tc.compute_partial_decryption(enc_msg, share)
            for share in shares[:t]
        ]

        eval_performance("Decrypt" + str(t) + str(n),
                         "{}".format(msg_size),
                         tc.decrypt_message,
                         partial_decryptions=pds,
                         encrypted_message=enc_msg,
                         threshold_params=tp,
                         timing_rounds=timing_rounds)
def eval_dec(timing_rounds):
    msg_size = 1024
    msg = "a" * msg_size

    for (t, n) in GLOBAL_VAR_TP_PARAMS:
        tp = tc.ThresholdParameters(t, n)
        pub_key, shares = tc.create_public_key_and_shares_centralized(
            GLOBAL_CP, tp)
        enc_msg = tc.encrypt_message(msg, pub_key)
        pds = [
            tc.compute_partial_decryption(enc_msg, share)
            for share in shares[:t]
        ]

        eval_performance("DecryptCombine",
                         "({}, {})".format(t, n),
                         tc.decrypt_message,
                         partial_decryptions=pds,
                         encrypted_message=enc_msg,
                         threshold_params=tp,
                         timing_rounds=timing_rounds)