Example #1
0
def test_e_add():
    for i in range(5):
        priv, pub = paillier.generate_keypair(128)
        for j in range(5):
            a = long(random.randint(0, 1000000))
            b = long(random.randint(0, 1000000))
            ca, cb = paillier.encrypt(pub, a), paillier.encrypt(pub, b)
            cs = paillier.e_add(pub, ca, cb)
            s = paillier.decrypt(priv, pub, cs)
            assert a + b == s
def test_e_add():
    for i in range(5):
        priv, pub = paillier.generate_keypair(128)
        for j in range(5):
            a = long(random.randint(0, 1000000))
            b = long(random.randint(0, 1000000))
            ca, cb = paillier.encrypt(pub, a), paillier.encrypt(pub, b)
            cs = paillier.e_add(pub, ca, cb)
            s = paillier.decrypt(priv, pub, cs)
            assert a + b == s
Example #3
0
    def encode_y(self, pk, C, y):
        N = len(y)
        T = [0] * N
        R_prev = 0
        for i in range(N):
            D = paillier.e_add(pk, paillier.encrypt(pk, -y[i]), C[i])
            F = paillier.e_add(
                pk, paillier.e_add(pk, C[i], paillier.encrypt(pk, y[i])),
                paillier.e_mul_const(pk, C[i], -2 * y[i]))
            if i == 0:
                R = F
                R_prev = R
            else:
                R = paillier.e_add(pk, paillier.e_mul_const(pk, R_prev, 2), F)
                R_prev = R

            T[i] = paillier.e_add(
                pk,
                paillier.e_mul_const(pk, paillier.e_add_const(pk, R, -1),
                                     random.randint(0, pk.n - 1)), D)

        random.shuffle(T)
        return T
Example #4
0
    def encode_y(self, pk, T, y):
        scheme = self.cipher_suite.scheme
        N = len(y)

        C = [0 for i in range(N)]
        c = 0
        if scheme == "Paillier":
            for i in range(N):
                if y[i] == 0:
                    y[i] = 1
                    c_t = T[y[0]][0]
                    for k in range(1, i + 1):
                        c_t = paillier.e_add(pk, c_t, T[y[k]][k])
                    c_t = paillier.scalarize(pk, c_t)
                    C[c] = c_t
                    c += 1
                    y[i] = 0

            for i in range(c, N):
                # r = random.randint(0, pk.n-1)
                C[i] = paillier.random_cipher(pk)

        elif scheme == "ElGamal":
            for i in range(N):
                if y[i] == 0:
                    y[i] = 1
                    c_t = T[y[0]][0]
                    for k in range(1, i + 1):
                        c_t = elgamal.c_mul(pk, c_t, T[y[k]][k])
                    c_t = elgamal.scalarize(pk, c_t)
                    C[c] = c_t
                    c += 1
                    y[i] = 0

            for i in range(c, N):
                # r = random.randint(0, pk.n-1)
                C[i] = elgamal.random_cipher(pk)

        random.shuffle(C)
        return C
Example #5
0
def central_server_oblivious(x):
    """
    :param
    x: A 2-D array
            each column correspond to the result from one subsystem
    :return:
    x_mean : 1-D array
            the mean value across subsystems
    enc_time: float
            the average time for each subsystem to encrypt its result
    collect_time: float
            the time for the central server to compute the average
    dec_time: float
            the average time for each subsystem to decrypt the average send
            back from the central server.
    """

    # Get dimension
    num_subsys = x.shape[0]
    num_feature = x.shape[1]

    # cx intermediate encrypted version of x
    cx = []
    cxn = []

    # x_mean : a list, this is the returned value
    x_mean = []
    x_meann = []

    # private key and public key, the private key is a pair of prime numbers, each about 512 bits
    sk, pk = paillier.generate_keypair(512)

    t0 = time.clock()
    for i in range(0, num_subsys):
        for j in range(0, num_feature):
            if x[i,j] < 0:
                x[i,j] = -x[i,j]
                cxn.append(paillier.encrypt(pk, x[i,j]))
                cx.append(1)
            else:
                cxn.append(1)
                cx.append(paillier.encrypt(pk,x[i,j]))
    t1 = time.clock()
    enc_time = t1 - t0
    # enc_time, encryption time for all sub-system, the final reported time is for each subsystem, i.e. enc_time/num_subsys

    t0 = time.clock()
    for i in range(0, num_subsys):
        for j in range(0, num_feature):
            if j == 0:
                x_mean.append(cx[i*num_feature+j])
                x_meann.append(cxn[i*num_feature+j])
            else:
                x_mean[i] = paillier.e_add(pk, x_mean[i],cx[i*num_feature+j])
                x_meann[i] = paillier.e_add(pk, x_meann[i],cxn[i*num_feature+j])
    t1 = time.clock()
    collect_time = t1 - t0
    # collect_time : time for averaging feature values among users

    t0 = time.clock()
    x_mean = [paillier.decrypt(sk,pk,x)/num_feature for x in x_mean]
    x_meann = [paillier.decrypt(sk,pk,x)/num_feature for x in x_meann]
    t1 = time.clock()
    dec_time = t1 - t0
    # dec_time : decryption time

    x_mean = [x_mean[i] - x_meann[i] for i in range(0,len(x_mean))]

    return (x_mean, enc_time/num_subsys, collect_time, dec_time)
Example #6
0
File: p.py Project: DataPrivacyB/A5
import paillier as p

priv, pub = p.generate_keypair(128)

x = p.encrypt(pub, 250)
y = p.encrypt(pub, 300)
z = p.e_add(pub, x, y)
b = p.e_sub(pub, x, y)
a = p.decrypt(priv, pub, z)
c = p.decrypt(priv, pub, b)
print(x)
print(y)
print(z)
print(a)
print(c)