def test_flatten(self): """Check that flattening occurs as expected.""" node = Rotate(flatten=True) x = np.ones((28, 28)) flattened = node.forward(x) np.testing.assert_array_almost_equal(flattened, x.flatten(), decimal=4, verbose=True)
def test_forward_plain_sum(self): """Check rotation sums desired axis.""" x = self.data node = Rotate(sum_axis=1) plaintext = node.forward(x) self.assertIsInstance(plaintext, np.ndarray) np.testing.assert_array_almost_equal(plaintext, x, decimal=4, verbose=True)
def test_forward_plain(self): """Check lack of encryption without params.""" x = self.data node = Rotate() plaintext = node.forward(x) self.assertIsInstance(plaintext, np.ndarray) np.testing.assert_array_almost_equal(plaintext, x, decimal=4, verbose=True)
def test_forward_decrypt(self): """Decrypt forward pass without re-encryption.""" x = self.data cypher = ReArray(x, self.reseal_args) node = Rotate() plaintext = node.forward(cypher) self.assertIsInstance(plaintext, np.ndarray) np.testing.assert_array_almost_equal(plaintext, x, decimal=4, verbose=True)
def test_forward_with_encryptor(self): """Check cyphertext is generated from encryptor properly.""" plaintext = self.data encryptor = ReArray(np.array([1]), **self.reseal_args) node = Rotate(encryptor=encryptor) cyphertext = node.forward(plaintext) self.assertIsInstance(cyphertext, ReArray) x = np.array(cyphertext) np.testing.assert_array_almost_equal(x, plaintext, decimal=4, verbose=True)
def test_forward(self): """Check encryption is successfull with given params.""" x = self.data node = Rotate(provider=ReArray, **self.reseal_args) cyphertext = node.forward(x) # check parameters are exactly as provided # check cyphertext is expected type self.assertIsInstance(cyphertext, ReArray) # decyrpt again manually plaintext = np.array(cyphertext) np.testing.assert_array_almost_equal(plaintext, x, decimal=4, verbose=True)
def test_forward_rotation(self): """Check that encryption parameters have been changed, on axis.""" x = self.data encryptor = ReArray( np.array([1]), { "scheme": 2, # seal.scheme_type.CKK, "poly_modulus_degree": 8192 * 2, # 438 # "coefficient_modulus": [60, 40, 40, 60], "coefficient_modulus": [45, 30, 30, 30, 30, 45 ], # coefficient mod length of 6 "scale": pow(2.0, 30), "cache": True, }) node = Rotate(encryptor=encryptor) cyphertext_in = ReArray(x, **self.reseal_args) cyphertext_out = node.forward(cyphertext_in) self.assertIsInstance(cyphertext_out, ReArray) out_cm = cyphertext_out.cyphertext[0].coefficient_modulus in_cm = cyphertext_in.cyphertext[0].coefficient_modulus self.assertNotEqual(in_cm, out_cm)
def test_forward_encrypt_axis(self): x = self.data axis = 1 encryptor = ReArray( np.array([1]), { "scheme": 2, # seal.scheme_type.CKK, "poly_modulus_degree": 8192 * 2, # 438 # "coefficient_modulus": [60, 40, 40, 60], "coefficient_modulus": [45, 30, 30, 30, 30, 45 ], # coefficient mod length of 6 "scale": pow(2.0, 30), "cache": True, }) node = Rotate(encryptor=encryptor, axis=1) cyphertext_in = ReArray(x, **self.reseal_args) cyphertext_lst_out = node.forward(cyphertext_in) self.assertIsInstance(cyphertext_lst_out, list) self.assertIsInstance(cyphertext_lst_out[0], ReArray) np.testing.assert_array_almost_equal(cyphertext_lst_out, cyphertext_in, decimal=4, verbose=True)