add time_memory_decorator and some test

This commit is contained in:
Your Name 2024-04-18 17:53:42 +08:00
parent 0827b14ff2
commit ef3773c286
10 changed files with 109 additions and 3 deletions

3
.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,3 @@
# 默认忽略的文件
/shelf/
/workspace.xml

14
.idea/algo_present.iml generated Normal file
View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.venv" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="PyDocumentationSettings">
<option name="format" value="PLAIN" />
<option name="myDocStringFormat" value="Plain" />
</component>
</module>

View File

@ -0,0 +1,14 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="PyPackageRequirementsInspection" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ignoredPackages">
<value>
<list size="1">
<item index="0" class="java.lang.String" itemvalue="ddddocr" />
</list>
</value>
</option>
</inspection_tool>
</profile>
</component>

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

7
.idea/misc.xml generated Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Black">
<option name="sdkName" value="Python 3.12 (algo_present)" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12 (algo_present)" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/algo_present.iml" filepath="$PROJECT_DIR$/.idea/algo_present.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@ -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(

11
some_decorators.py Normal file
View File

@ -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

31
some_test_items.py Normal file
View File

@ -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()