From ef3773c2862172dc93ecf0dd2e7588ee4fa5f756 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 18 Apr 2024 17:53:42 +0800 Subject: [PATCH] add time_memory_decorator and some test --- .idea/.gitignore | 3 ++ .idea/algo_present.iml | 14 +++++++++ .idea/inspectionProfiles/Project_Default.xml | 14 +++++++++ .../inspectionProfiles/profiles_settings.xml | 6 ++++ .idea/misc.xml | 7 +++++ .idea/modules.xml | 8 +++++ .idea/vcs.xml | 6 ++++ CoreAlgorithm.py | 12 +++++-- some_decorators.py | 11 +++++++ some_test_items.py | 31 +++++++++++++++++++ 10 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/algo_present.iml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 some_decorators.py create mode 100644 some_test_items.py diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..359bb53 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml diff --git a/.idea/algo_present.iml b/.idea/algo_present.iml new file mode 100644 index 0000000..7a6134d --- /dev/null +++ b/.idea/algo_present.iml @@ -0,0 +1,14 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..4202bcc --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,14 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..62afc1b --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..6ca325c --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/CoreAlgorithm.py b/CoreAlgorithm.py index a83b732..4518dde 100644 --- a/CoreAlgorithm.py +++ b/CoreAlgorithm.py @@ -2,12 +2,11 @@ import random from collections.abc import Mapping from encoding import EncodedNumber from util import invert, powmod, getprimeover -from visualize import show_name_args DEFAULT_KEYSIZE = 1024 # 定义默认的二进制数长度 -@show_name_args + def generate_paillier_keypair( private_keyring=None, n_length=DEFAULT_KEYSIZE ): # 生成公钥和私钥的函数 @@ -57,6 +56,7 @@ class PaillierPublicKey(object): # 定义公钥类 def get_n_and_g(self): # 获取该公钥的 n 和 g 的值 return self.n, self.g + def raw_encrypt( self, plaintext, r_value=None ): # 用于返回加密后的密文,其中r_value可给随机数赋值 @@ -83,6 +83,7 @@ class PaillierPublicKey(object): # 定义公钥类 def get_random_lt_n(self): # 返回一个1——n间的随机整数 return random.SystemRandom().randrange(1, self.n) + def encrypt( self, value, precision=None, r_value=None ): # value表示要加密的值,precision是加密精度,r_value是随机数 @@ -93,6 +94,7 @@ class PaillierPublicKey(object): # 定义公钥类 encoding = EncodedNumber.encode(self, value, precision) return self.encrypt_encoded(encoding, r_value) + def encrypt_encoded( self, encoding, r_value ): # 将已编码的数值对象转换为加密后的数值对象,并可以选择进行混淆处理 @@ -144,6 +146,7 @@ class PaillierPrivateKey(object): # 私钥 encoded = self.decrypt_encoded(encrypted_number) return encoded.decode() + def decrypt_encoded( self, encrypted_number, Encoding=None ): # 用于解密密文并返回解密后的EncodedNumber类型 @@ -161,7 +164,7 @@ class PaillierPrivateKey(object): # 私钥 if Encoding is None: # 将Encoding设置为未赋值的EncodedNumber变量 Encoding = EncodedNumber - """提取 encrypted_number 中的 ciphertext + """提取 encrypted_number 中的 ciphertext 这里是禁用安全模式, 所以是直接提取ciphertext, 随后调用raw_decrypt函数对ciphertext进行处理:""" @@ -233,6 +236,7 @@ class PaillierPrivateKeyring(Mapping): # 私钥环类,并继承了Mapping类 private_key # 将该公钥和对用的私钥一块儿加入到私钥环中 ) + def decrypt(self, encrypted_number): # 对密文进行解密 relevant_private_key = self.__keyring[ encrypted_number.public_key @@ -328,6 +332,7 @@ class EncryptedNumber(object): # 浮点数或整数的Pailier加密 self.obfuscate() return self.__ciphertext + def decrease_exponent_to( self, new_exp ): # 返回一个指数较低但大小相同的数(即返回一个同值的,但指数较低的EncryptedNumber) @@ -342,6 +347,7 @@ class EncryptedNumber(object): # 浮点数或整数的Pailier加密 multiplied.exponent = new_exp # 降指数后的新指数 return multiplied + def obfuscate(self): # 混淆密文 r = self.public_key.get_random_lt_n() # 生成一个(1——n)间的随机数r,不 >= r r_pow_n = powmod( diff --git a/some_decorators.py b/some_decorators.py new file mode 100644 index 0000000..65a993c --- /dev/null +++ b/some_decorators.py @@ -0,0 +1,11 @@ +import time + +# 配置装饰器跟踪函数执行,在函数执行前后添加时间计算,并打印每个函数的执行时间 +def track_performance(func): + def wrapper(*args, **kwargs): + start_time = time.time() + result = func(*args, **kwargs) + end_time = time.time() + print(f"{func.__name__} 执行了 {end_time - start_time:.10f} 秒") + return result + return wrapper \ No newline at end of file diff --git a/some_test_items.py b/some_test_items.py new file mode 100644 index 0000000..2d24567 --- /dev/null +++ b/some_test_items.py @@ -0,0 +1,31 @@ +from CoreAlgorithm import * +from memory_profiler import profile +import threading +from some_decorators import track_performance + +@track_performance +def time_memory_explain_test(): + Public_Key, Private_Key = generate_paillier_keypair() # 随机生成1024长的公钥和私钥 + x = 90000424.23 + y = 90849442 + x_encrypted = Public_Key.encrypt(x) # 加密后的x + y_encrypted = Public_Key.encrypt(y) # 加密后的y + t_encrypted = (x_encrypted + y_encrypted * 0.5) + Private_Key.decrypt(t_encrypted) + + +@profile() +def multi_thread_test(): + threads = [] + # 创建并启动5个线程 + for i in range(20): + t = threading.Thread(target=time_memory_explain_test) + threads.append(t) + t.start() + + # 等待所有线程完成 + for t in threads: + t.join() + + +multi_thread_test() \ No newline at end of file