def set_veu11_operands(): if app.config["paillier_priv"] or app.config["gm_priv"]: print("Key inconsistency") return Response("", status=404) app.config["data"]['veu11_operand1'] = paillier.encrypt( mpz(request.json['val1']), app.config["paillier_pub"]) app.config["data"]['veu11_operand2'] = paillier.encrypt( mpz(request.json['val2']), app.config["paillier_pub"]) return Response(status=200)
def b_handler(mx_rand, ai_rand, l, enc_bit, idx, pubk, privk): qr_privk = CMP.get_GM_privk_B() isless = bool(gm.decrypt([enc_bit], qr_privk)) if isless: global private_b_var private_b_var = idx # print("wegweg",idx,private_b_var) vi = refresh_b(ai_rand, pubk, privk) bit_paillier = paillier.encrypt(1, pubk) else: vi = refresh_b(mx_rand, pubk, privk) bit_paillier = paillier.encrypt(0, pubk) return bit_paillier, vi
def compare_A(encrypted_a, encrypted_b, l, pk, privk): # KEY REQUEST TO BE CORRECTED ALL ACROSS global qr_pk x = encrypted_b + paillier.encrypt(pow(2, l), pk) - encrypted_a r = random.getrandbits(l + 2) z = x + paillier.encrypt(mpz(r), pk) c = r % pow(2, l) if MSB(r, l): r_l = gm.encrypt(1, qr_pk) else: r_l = gm.encrypt(0, qr_pk) ask_B_tocompute = step1_B(z, privk, l, r_l, c) return ask_B_tocompute
def handler_A(inp, l, pubk, privk): perm = np.arange(0, len(inp)) random.shuffle(perm) shuf_inp = inp[perm] mxval = shuf_inp[0] global private_b_var private_b_var = 0 for i in range(1, len(shuf_inp)): ai = shuf_inp[i] enc_bit = CMP.compare_A(mxval, ai, l, pubk, privk) r = random.getrandbits(l + 1) s = random.getrandbits(l + 1) enc_r = paillier.encrypt(r, pubk) enc_s = paillier.encrypt(s, pubk) mx_rand = mxval + enc_r ai_rand = ai + enc_s bi, vi = b_handler(mx_rand, ai_rand, l, enc_bit, i, pubk, privk) one_enc = paillier.encrypt(mpz(1), pubk) remove_r = (bi - one_enc) * r remove_s = bi * s vi = vi + remove_r vi = vi - remove_s # print(paillier.decrypt(mxval,privk),paillier.decrypt(ai,privk),gm.decrypt([enc_bit],jj),private_b_var,end=' ') mxval = vi # print(paillier.decrypt(mxval,privk)) shuf_idx = askB_for_index() return perm[shuf_idx]
def serializeANDenc_arr(my_inp, pubk): mx = max(my_inp) l = int(math.log2(abs(mx))) l += 10 gg = [] for i in range(len(my_inp)): x = paillier.encrypt(mpz(my_inp[i]), pubk) gg.append(x) s1 = str(gg[0].c) s2 = str(gg[0].n) for i in range(1, len(gg)): s1 = s1 + ";" + str(gg[i].c) s2 = s2 + ";" + str(gg[i].n) dic = {"a1": s1, "a2": s2, "a3": l} return dic
def dot(a, b): vector_b_encrypt = [] for each in b: each = mpz(each) c1 = paillier.encrypt(each, pub) vector_b_encrypt.append(c1) fval = 0 for ind, each in enumerate(vector_b_encrypt): c2 = mpz(a[ind]) c1 = each x = paillier.decrypt(c1 * c2, priv) fval += x return fval
def compare(encrypted_a, encrypted_b, l, pk, privk): # -------------------A----------------------------- x = encrypted_b + paillier.encrypt(pow(2, l), pk) - encrypted_a r = random.getrandbits(l + 2) z = x + paillier.encrypt(mpz(r), pk) c = r % pow(2, l) #---------------------B---------------------------- # print(x) z_dec = paillier.decrypt(z, privk) d = z_dec % pow(2, l) #-----------------A and B compute encrypted t ----- kp = gm.generate_key(1024) qr_pk = kp["pub"] qr_privk = kp["priv"] t = dgk_comp.compare(c, d, l, qr_pk) # -------------------A----------------------------- if MSB(r, l): r_l = gm.encrypt(1, qr_pk) else: r_l = gm.encrypt(0, qr_pk) # -------------------B----------------------------- if MSB(z_dec, l): z_l = gm.encrypt(1, qr_pk) else: z_l = gm.encrypt(0, qr_pk) # -------------------A----------------------------- N, _ = qr_pk t_ = (z_l[0] % N * r_l[0] % N) % N t = (t_ % N * t % N) % N return t, bool(gm.decrypt([t], qr_privk))
def getRandomInp(): key = paillier.keygen() privk = key.private_key pubk = key.public_key a = [ random.randint(-20000, 20000), random.randint(-200000, 20000), random.randint(-20000, 20000) ] # a = [1915,1383] # for i in range(15): # x = random.randint(-31,-1) # a.append(x) # a = np.array(a) ans = np.argmax(np.array(a)) # print(a) inp = [] for num in a: inp.append(paillier.encrypt(mpz(num), pubk)) inp = np.array(inp) return inp, pubk, privk, ans, a
def paillier_enc(x, pub_key): return paillier.encrypt(mpz(x), pub_key)
def refresh_b(num, pubk, privk): # global pubk , privk plainval = paillier.decrypt(num, privk) re_enc = paillier.encrypt(plainval, pubk) return re_enc
from paillierlib import paillier as pal from gmpy2 import mpz m1 = mpz(10) m2 = mpz(1) m3 = mpz(0) m4 = mpz(2000) m5 = mpz(4000) m6 = mpz(999) key_pair = pal.keygen() c1 = pal.encrypt(m1, key_pair.public_key) c2 = pal.encrypt(m2, key_pair.public_key) c3 = pal.encrypt(m3, key_pair.public_key) c4 = pal.encrypt(m4, key_pair.public_key) c5 = pal.encrypt(m5, key_pair.public_key) c6 = pal.encrypt(m6, key_pair.public_key) def test_additive(): assert pal.decrypt(c1 + c2, key_pair.private_key) == 11 assert pal.decrypt(c1 + c2 + c1, key_pair.private_key) == 21 assert pal.decrypt(c1 + c2 + c1 + c2, key_pair.private_key) == 22 assert pal.decrypt(c6 + c2, key_pair.private_key) == 1000 assert pal.decrypt(c5 + c6 + c5, key_pair.private_key) == 8999 assert pal.decrypt(c3 + c3 + c3 + c3 + c3, key_pair.private_key) == 0 def test_sub(): assert pal.decrypt(c2 - c3, key_pair.private_key) == 1 assert pal.decrypt(c2 - c3 - c3 - c3, key_pair.private_key) == 1
import random from gmpy2 import mpz from tqdm import tqdm from veu11 import compare from paillierlib import paillier key_pair = paillier.keygen(1024) l = 20 for i in tqdm(range(5)): a = random.randint(0, pow(2, l) - 1) b = random.randint(0, pow(2, l) - 1) encrypted_a = paillier.encrypt(mpz(a), key_pair.public_key) encrypted_b = paillier.encrypt(mpz(b), key_pair.public_key) for j in range(20): _, out = compare(encrypted_a, encrypted_b, l, key_pair.public_key, key_pair.private_key) if a < b and out == 0: print("Test Failed") elif a > b and out == 1: print("Test Failed")
def paillier_enc(x): global key_pair return paillier.encrypt(mpz(x), key_pair.public_key)