def test_serialize(self): pubkey, seckey = KeyPair().generate() pk_serialized = pubkey.serialize() sk_serialized = seckey.serialize() pubkey2, seckey2 = KeyPair().deserialize(pk_serialized, sk_serialized) self.assertTrue(pubkey.pk == pubkey2.pk and seckey.sk == seckey2.sk)
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 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 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 testInplacePlaintext(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(s.decrypt(x) == np.array([4., 6., 8., 10., 12.]))
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 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 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 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 modelTrainingDemoNotebook(self): """If this test fails, you probably broke the demo notebook located at PySonar/notebooks/Sonar - Decentralized Model Training Simulation (local blockchain).ipynb """ pubkey, prikey = KeyPair().generate(n_length=1024) d = LinearClassifier(desc="DiabetesClassifier", n_inputs=10, n_labels=1) d.encrypt(pubkey) self.assertTrue(True)
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 paillierLinearClassifierNotebook(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(n_length=1024) model = LinearClassifier(n_inputs=4, n_labels=2).encrypt(pubkey) input = np.array([[0, 0, 1, 1], [0, 0, 1, 0], [1, 0, 1, 1], [0, 0, 1, 0]]) target = np.array([[0, 1], [0, 0], [1, 1], [0, 0]]) for iter in range(3): for i in range(len(input)): model.learn(input=input[i], target=target[i], alpha=0.5) model = model.decrypt(prikey) for i in range(len(input)): model.forward(input[i])
def testDimOne(self): p, s = KeyPair().generate() x = PaillierTensor(p, np.array([1, 2, 3, 4, 5.])) self.assertTrue(x.dim() == 1)