예제 #1
0
def dgk_compare_has_priv():
    global config, data

    if not config['dgk_priv']:
        print("Private Key not available for DGK.")
        return Response("", status=404)

    if not data['compare_operand']:
        print("Comparison operand not set.")
        return Response("", status=404)

    # get bits
    y_b = get_bits(data['compare_operand'], config['dgk_l'])
    # get encrypted bits
    y_enc = []
    for b in y_b:
        y_enc.append(dgk.encrypt(b, config['dgk_pub']))

    response = requests.post("http://127.0.0.1:5000/dgk_compare_no_priv",
                             json={'y_enc': y_enc})

    c, dela = response.json()

    delb = 0
    for ci in c:
        ci_iszero = dgk.decrypt_iszero(ci, config['dgk_priv'])
        if (ci_iszero):
            delb = 1
            break

    return jsonify(dela ^ delb)
예제 #2
0
def dgk_decrypt():
    global config
    if not config['dgk_priv']:
        print("No private key available.")
        return Response("", status=404, mimetype='application/json')

    m = dgk.decrypt_iszero(request.json['c'], config['dgk_priv'])
    if (m == 1):
        return jsonify(True)
    else:
        return jsonify(False)
def step2_B(c, pk):
    delb = 0
    for ci in c:
        ci_iszero = dgk.decrypt_iszero(ci, priv)
        if (ci_iszero):
            delb = 1
            break
    N, _ = pk
    c2 = gm.encrypt(delb, pk)
    c2_bit = list(c2)[0]
    return c2_bit
예제 #4
0
import numpy as np
import random
# change this if want to generate keys
generate_keys = False

if (generate_keys):
    #keygen without dlut and with save in file (default)
    dgk.keygen(2048, 160, 18)

pub = np.load("dgk/pub.npy", allow_pickle=True).item()
priv = np.load("dgk/priv.npy", allow_pickle=True).item()

#100 iszero tests
for i in range(100):
    m = np.random.randint(0, 2)
    c = dgk.encrypt(m, pub)
    m_iszero = dgk.decrypt_iszero(c, priv)

    if (m == 0 and not m_iszero) or (m != 0 and m_iszero):
        print("Failed!!!!")
        quit()

print("Success!!!!")

# Checking dot product
a = random.sample(range(1, 100), 5)
b = random.sample(range(1, 100), 5)

print(np.dot(a, b))
x = dgk.dot(a, b)
print(x)
예제 #5
0
def compare(x, y, l):
    # get bits
    y_b = get_bits(y, l)
    # get encrypted bits
    y_enc = []
    for b in y_b:
        y_enc.append(dgk.encrypt(b, pub))
    y_enc = np.array(y_enc)
    # get bits
    x_b = get_bits(x, l)
    # get encrypted bits
    x_enc = []
    for b in x_b:
        x_enc.append(dgk.encrypt(b, pub))
    x_enc = np.array(x_enc)
    # get encryption of 1
    one_enc = dgk.encrypt(1, pub)
    # get [x xor y]
    xxory = []
    for i in range(l):
        if x_b[i] == 0:
            xxory.append(y_enc[i])
        else:
            xxory.append(
                ((one_enc % pub['n']) *
                 (mod_inverse(y_enc[i], pub['n']) % pub['n'])) % pub['n'])
    xxory = np.array(xxory)

    dela = np.random.randint(0, 2)
    s = 1 - 2 * dela
    s_enc = dgk.encrypt(s, pub)

    # precompute xor cubes
    xor3precomp = [1] * l
    i = l - 2
    while (i >= 0):
        xor3precomp[i] = ((pow(xxory[i + 1], 3, pub['n']) % pub['n']) *
                          (xor3precomp[i + 1] % pub['n'])) % pub['n']
        i -= 1
    # compute [c]
    c = []
    for i in range(l):
        temp = ((s_enc % pub['n']) * (x_enc[i] % pub['n']) *
                (mod_inverse(y_enc[i], pub['n'])) % pub['n'] *
                (xor3precomp[i] % pub['n'])) % pub['n']
        r = np.random.randint(1, pub['u'])
        r_dash = random.getrandbits(int(2.5 * pub['t']))
        temp = (pow(temp, r, pub['n']) *
                pow(pub['h'], r_dash, pub['n'])) % pub['n']
        c.append(temp)
    c = np.array(c)
    # shuffle c
    np.random.shuffle(c)

    #-----------------B-------------------
    delb = 0
    for ci in c:
        ci_iszero = dgk.decrypt_iszero(ci, priv)
        if (ci_iszero):
            delb = 1
            break

    #-----------------AB-------------------
    # how to use the values dela delb in an actual algo??
    print("IS x <= y ? ", bool(dela ^ delb))