import unittest import CoreAlgorithm as paillier class TestPaillierEncryption(unittest.TestCase): """测试Paillier加密算法的不同运算功能.""" @classmethod def setUpClass(cls): cls.public_key, cls.private_key = paillier.generate_paillier_keypair() cls.x = 90000.23 cls.y = 90 cls.z = 0.5 cls.x_encrypted = cls.public_key.encrypt(cls.x) cls.y_encrypted = cls.public_key.encrypt(cls.y) cls.z_encrypted = cls.public_key.encrypt(cls.z) def test_encryption_and_basic_arithmetic(self): """Test encrypted values with basic arithmetic operations.""" t_encrypted = self.x_encrypted + self.y_encrypted * 0.5 # Perform encrypted arithmetic operations t_decrypted = self.private_key.decrypt(t_encrypted) self.assertAlmostEqual(t_decrypted, self.x + self.y * self.z, places=5, msg="Encrypted arithmetic result is incorrect") def test_comparisons(self): """Test encrypted value comparisons.""" self.assertNotEqual(self.x_encrypted, self.y_encrypted, msg="Encrypted values should not be equal") self.assertFalse(self.x_encrypted == self.y_encrypted, msg="Equality comparison failed") self.assertTrue(self.x_encrypted != self.y_encrypted, msg="Inequality comparison failed") # 这里可以继续添加更多测试用例,例如对自增、自减、比较操作等的测试 if __name__ == "__main__": unittest.main()