def testSimpleReversed(self): p, s = KeyPair().generate() x = PaillierTensor(p, np.array([1, 2, 3, 4, 5.])) x2 = PaillierTensor(p, np.array([3, 4, 5, 6, 7.])) y = (x2 + x).decrypt(s) self.assertTrue(y == np.array([4., 6., 8., 10., 12.]))
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 testInplaceReversed(self): p, s = KeyPair().generate() x = PaillierTensor(p, np.array([1, 2, 3, 4, 5.])) x2 = PaillierTensor(p, np.array([3, 4, 5, 6, 7.])) x2 += x self.assertTrue(s.decrypt(x2) == np.array([4., 6., 8., 10., 12.]))
def test_in_place(self): p, s = KeyPair().generate() x = PaillierTensor(p, np.array([1, 2, 3, 4, 5.])) x2 = PaillierTensor(p, np.array([3, 4, 5, 6, 7.])) x += x2 self.assertTrue(s.decrypt(x) == np.array([4., 6., 8., 10., 12.]))
def testScalarInplace(self): p, s = KeyPair().generate() x = PaillierTensor(p, np.array([1, 2, 3, 4, 5.])) x += 1 self.assertTrue(s.decrypt(x) == np.array([2., 3., 4., 5., 6.]))
def testScalar(self): p, s = KeyPair().generate() x = PaillierTensor(p, np.array([2., 4., 6., 8., 10.])) x /= 2 self.assertTrue(s.decrypt(x) == np.array([1, 2, 3, 4, 5.]))
def testBasicReversed(self): p, s = KeyPair().generate() x = PaillierTensor(p, np.array([1, 2, 3, 4, 5.])) x2 = TensorBase(np.array([3, 4, 5, 6, 7.])) y = x2 * x self.assertTrue(y.decrypt(s) == np.array([3., 8., 15., 24., 35.]))
def testScalar(self): p, s = KeyPair().generate() x = PaillierTensor(p, np.array([1, 2, 3, 4, 5.])) y = x + 40 self.assertTrue(s.decrypt(y) == np.array([41., 42., 43., 44., 45.]))
def test_add_depth(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.assertEqual(x._add_depth, 1)
def testBasic(self): p, s = KeyPair().generate() x = PaillierTensor(p, np.array([3., 8., 15., 24., 35.])) x2 = TensorBase(np.array([3, 4, 5, 6, 7.])) y = x / x2 print(y.decrypt(s)) self.assertTrue(y.decrypt(s) == np.array([1., 2., 3., 4., 5.]))
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]))
def testDimOne(self): p, s = KeyPair().generate() x = PaillierTensor(p, np.array([1, 2, 3, 4, 5.])) self.assertTrue(x.dim() == 1)