def testInline(self): p, s = KeyPair().generate() x = PaillierTensor(p, np.array([1, 2, 3, 4, 5.])) x2 = TensorBase(np.array([3, 4, 5, 6, 7.])) x *= x2 self.assertTrue(x.decrypt(s) == np.array([3., 8., 15., 24., 35.]))
def paillier_HE_example_notebook(self): """If this test fails, you probably broke the demo notebook located at PySyft/notebooks/Syft - Paillier Homomorphic Encryption Example.ipynb """ pubkey, prikey = KeyPair().generate() x = PaillierTensor(pubkey, np.array([1, 2, 3, 4, 5.])) out1 = x.decrypt(prikey) self.assertEqual(out1, np.array([1., 2., 3., 4., 5.])) out2 = (x + x[0]).decrypt(prikey) self.assertEqual(out2, np.array([2., 3., 4., 5., 6.])) out3 = (x * 5).decrypt(prikey) self.assertEqual(out3, np.array([5., 10., 15., 20., 25.])) out4 = (x + x / 5).decrypt(prikey) self.assertEqual(out4, np.array([1.2, 2.4, 3.6, 4.8, 6.])) pubkey_str = pubkey.serialize() prikey_str = prikey.serialize() pubkey2, prikey2 = KeyPair().deserialize(pubkey_str, prikey_str) out5 = prikey2.decrypt(x) self.assertEqual(out5, np.array([1., 2., 3., 4., 5.])) y = PaillierTensor(pubkey, (np.ones(5)) / 2) out6 = prikey.decrypt(y) self.assertEqual(out6, np.array([.5, .5, .5, .5, .5])) y_str = pickle.dumps(y) y2 = pickle.loads(y_str) out7 = prikey.decrypt(y2) self.assertEqual(out7, np.array([.5, .5, .5, .5, .5]))