def testKeyring(self): keyring1 = paillier.PaillierPrivateKeyring() public_key1, private_key1 = paillier.generate_paillier_keypair( keyring1) public_key2, private_key2 = paillier.generate_paillier_keypair( keyring1) self.assertEqual(private_key1, keyring1[public_key1]) self.assertEqual(private_key2, keyring1[public_key2]) ciphertext1 = public_key1.encrypt(5318008) ciphertext2 = public_key2.encrypt(1337) self.assertEqual(5318008, keyring1.decrypt(ciphertext1)) self.assertEqual(1337, keyring1.decrypt(ciphertext2)) keyring2 = paillier.PaillierPrivateKeyring( [private_key1, private_key2]) self.assertEqual(keyring1, keyring2) self.assertRaises(TypeError, keyring1.add, public_key1) keyring1.add(private_key1) self.assertEqual(2, len(keyring1)) del keyring1[public_key1] self.assertEqual(1, len(keyring1)) self.assertRaises(KeyError, keyring1.decrypt, ciphertext1)
def test_phe(): from phe import paillier print('Testing phe...') keyring = paillier.PaillierPrivateKeyring() public_key, private_key = paillier.generate_paillier_keypair(keyring) nums = np.array([2., 3.1, 18]) encrypted_nums = [public_key.encrypt(x) + 1 for x in nums] decrypted_nums = [keyring.decrypt(x) for x in encrypted_nums] print(nums) print(encrypted_nums) print(np.array(encrypted_nums).dtype) print(decrypted_nums) mat = np.array([[1., 2.], [3., 4.]]) mat_e = np.zeros(mat.shape, dtype=object) print(mat) print(mat_e) for index, x in np.ndenumerate(mat): mat_e[index] = public_key.encrypt(float(x)) print(mat_e)
def __init__(self, model, optim, encryption=False): self.model = model self.optim = optim # Encryption-based Setup self.pool = None self.keyring = None if encryption: self.pool = mp.Pool(1) self.keyring = paillier.PaillierPrivateKeyring() self.public_key, self.private_key = ( paillier.generate_paillier_keypair(self.keyring, n_length=128))
def test_mp(): from multiprocessing import Array, Manager, RawArray import multiprocessing as mp from phe import paillier keyring = paillier.PaillierPrivateKeyring() public_key, private_key = paillier.generate_paillier_keypair(keyring, n_length=128) g = Gradient() grads = g.grads print(grads.shape) begin_time = time.time() grads_e = np.zeros(grads.shape, dtype=object) for index, x in np.ndenumerate(grads): grads_e[index] = public_key.encrypt(float(x)) end_time = time.time() print('Base time: {}'.format(end_time - begin_time)) # Multiprocessing grads_e = np.zeros(grads.shape, dtype=object) pool = mp.Pool(mp.cpu_count()) grads_e = np.zeros(grads.shape, dtype=object) # sa.delete("shm://test") # a = sa.create("shm://test", grads.shape, dtype=object) # print(a) begin_time = time.time() nargs = [(public_key, index, x, grads_e) for index, x in np.ndenumerate(grads)] grads_e = np.reshape(pool.map(encrypt, nargs), grads.shape) end_time = time.time() print('MP time: {}'.format(end_time - begin_time)) pool.close()
def num(s): try: return int(s) except ValueError: return float(s) if (len(sys.argv) > 1): vote1 = num(sys.argv[1]) if (len(sys.argv) > 2): vote2 = num(sys.argv[2]) public_key, private_key = paillier.generate_paillier_keypair() keyring = paillier.PaillierPrivateKeyring() keyring.add(private_key) public_key1, private_key1 = paillier.generate_paillier_keypair(keyring) print 'Votes 1=', vote1 print 'Votes 2=', vote2 encrypted1 = public_key.encrypt(vote1) print 'Encrypted1=', encrypted1 encrypted2 = public_key.encrypt(vote2) print 'Encrypted2=', encrypted2
def generate_keys(): ppk = paillier.PaillierPrivateKeyring() public_key, private_key = paillier.generate_paillier_keypair(ppk, 128) return (public_key, private_key)