def test_decrypt(): for i in range(5): priv, pub = paillier.generate_keypair(64) for j in range(5): pt = long(random.randint(0, 1000000)) ct = paillier.encrypt(pub, pt) assert pt == paillier.decrypt(priv, pub, ct)
def test_e_mul_const(): for i in range(5): priv, pub = paillier.generate_keypair(128) for j in range(5): a = long(random.randint(0, 1000000)) c = paillier.encrypt(pub, a) for n in range(0, 11): cs = paillier.e_mul_const(pub, c, n) s = paillier.decrypt(priv, pub, cs) assert a * n == 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
def generate_keypair(self): cs = self.cipher_suite scheme = cs.scheme if scheme == "Paillier": sk, pk = paillier.generate_keypair(cs.modulus_length) # print(pk.n) elif scheme == "ElGamal": sk, pk = elgamal.generate_keys(cs.modulus_length) # print(pk.p) return sk, pk
def createPass(user, pa): username = sha.encrypt(user) password = sha.encrypt(pa) priv, pub = pail.generate_keypair(256) userD = {} if os.path.isfile('UserDB.pkl'): with open('UserDB.pkl', 'rb') as fb: userD = pickle.load(fb) if 'USER_DICT' in userD: print("ADDING NEW USER") userD['USER_DICT'].append({username: (password, priv, pub)}) else: userD = {'USER_DICT': [{username: (password, priv, pub)}]} with open('UserDB.pkl', 'wb') as fw: pickle.dump(userD, fw)
import matplotlib.pyplot as plt from matplotlib import style style.use('ggplot') import numpy as np import paillier as p priv, pub = p.generate_keypair(128) X = np.array([[1, 2], [1.5, 1.8], [5, 8]]) X = np.int_(X) X = X.tolist() Y = [] # array = np.arange().reshape(X.shape[0],X.shape[1]) # Y = np.zeros((X.shape[0],X.shape[1])) # Y.astype(int) # X = X.astype(int) # X = np.int_(X) # print(X.shape) i = 0 print(X) for x in X: x = p.encryptArray(pub, x) Y.append(x) i += 1 # print(Y) i = 0 z = [] # np.zeros((X.shape[0],X.shape[1])) for y in Y: y = p.decryptArray(priv, pub, y) z.append(y)
def test_keys_long(): priv, pub = paillier.generate_keypair(256) for i in range(5): assert paillier.invmod(priv.m, pub.n) == priv.l
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)
def key_gen(self, n_length): self.pk, self.sk = generate_keypair(n_length=n_length)
def generate_keypair(self): cs = self.cipher_suite return paillier.generate_keypair(cs.modulus_length)