From b2354738fe425acde157631ab88ce8fb73833ace Mon Sep 17 00:00:00 2001 From: sangge-win <2251250136@qq.com> Date: Sun, 14 Apr 2024 00:01:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=AE=80=E5=8D=95=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test_CoreAlgo.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 test_CoreAlgo.py diff --git a/test_CoreAlgo.py b/test_CoreAlgo.py new file mode 100644 index 0000000..4b38031 --- /dev/null +++ b/test_CoreAlgo.py @@ -0,0 +1,44 @@ +import unittest +import CoreAlgorithm as paillier +from typing import Tuple + + +def generate_paillier_keypair() -> ( + Tuple[paillier.PaillierPublicKey, paillier.PaillierPrivateKey] +): + """生成Paillier公钥和私钥对.""" + public_key, private_key = paillier.generate_paillier_keypair() + return public_key, private_key + + +class TestPaillierEncryption(unittest.TestCase): + """测试Paillier加密算法的不同运算功能.""" + + @classmethod + def setUpClass(cls): + cls.public_key, cls.private_key = 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()