Merge pull request 'main' (#36) from sangge/mimajingsai:main into main

Reviewed-on: dqy/mimajingsai#36
This commit is contained in:
dqy 2023-11-17 10:07:52 +08:00
commit 1f582556f0
16 changed files with 147 additions and 108 deletions

View File

@ -1,4 +1,4 @@
name: Deploy App name: Test CI
on: on:
push: push:
@ -6,41 +6,18 @@ on:
- main - main
jobs: jobs:
deploy: test:
name: Deploy to Web Server name: test speed
runs-on: ubuntu-latest runs-on: ubuntu-latest
container:
image: catthehacker/ubuntu:act-latest
steps: steps:
- name: Checkout repository - name: Checkout repository
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: Run script in Docker container
- name: copy file via ssh password run: |
uses: cross-the-world/scp-pipeline@master ls $PWD
with: docker run -v $PWD:/app git.mamahaha.work/sangge/tpre:base python src/speed_test.py
host: "110.41.155.96"
user: ${{ secrets.USERNAME }}
pass: ${{ secrets.PASSWORD }}
local: "src/*"
remote: /root/mimajingsai/src/
- name: copy file via ssh password
uses: cross-the-world/scp-pipeline@master
with:
host: "110.41.130.197"
user: ${{ secrets.USERNAME }}
pass: ${{ secrets.PASSWORD }}
local: "src/*"
remote: /root/mimajingsai/src/
- name: copy file via ssh password
uses: cross-the-world/scp-pipeline@master
with:
host: "110.41.21.35"
user: ${{ secrets.USERNAME }}
pass: ${{ secrets.PASSWORD }}
local: "src/*"
remote: /root/mimajingsai/src/

View File

@ -2,7 +2,11 @@ FROM python:3.11
COPY requirements.txt /app/ COPY requirements.txt /app/
COPY lib/* /lib/ # 设置目标平台参数
ARG TARGETPLATFORM
# 根据目标平台复制相应架构的库文件
COPY lib/${TARGETPLATFORM}/* /lib/
WORKDIR /app WORKDIR /app

View File

@ -381,8 +381,17 @@ async def receive_request(i_m: IP_Message):
def get_own_ip() -> str: def get_own_ip() -> str:
ip = os.environ.get("HOST_IP", "IP not set") ip = os.environ.get("HOST_IP")
return ip if not ip: # 如果环境变量中没有IP
try:
# 从网卡获取IP
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80)) # 通过连接Google DNS获取IP
ip = s.getsockname()[0]
s.close()
except:
raise ValueError("Unable to get IP")
return str(ip)
# get node list from central server # get node list from central server

View File

@ -1,71 +0,0 @@
from tpre import *
import time
# for T in range(2, 20, 2):
N = 10
T = N // 2
# print(f"当前门限值: N = {N}, T = {T}")
start_total_time = time.time()
# 1
start_time = time.time()
pk_a, sk_a = GenerateKeyPair()
# print("pk_a: ", pk_a)
# print("sk_a: ", sk_a)
end_time = time.time()
elapsed_time = end_time - start_time
# print(f"密钥生成运行时间:{elapsed_time}秒")
# 2
start_time = time.time()
m = b"hello world"
capsule_ct = Encrypt(pk_a, m)
capsule = capsule_ct[0]
print("check capsule: ", Checkcapsule(capsule))
capsule = (capsule[0], capsule[1], -1)
print("check capsule: ", Checkcapsule(capsule))
# print("capsule_ct: ", capsule_ct)
end_time = time.time()
elapsed_time = end_time - start_time
# print(f"加密算法运行时间:{elapsed_time}秒")
# 3
pk_b, sk_b = GenerateKeyPair()
# 5
start_time = time.time()
id_tuple = tuple(range(N))
rekeys = GenerateReKey(sk_a, pk_b, N, T, id_tuple)
# print("rekeys: ", rekeys)
end_time = time.time()
elapsed_time = end_time - start_time
# print(f"重加密密钥生成算法运行时间:{elapsed_time}秒")
# 7
start_time = time.time()
cfrag_cts = []
for rekey in rekeys:
cfrag_ct = ReEncrypt(rekey, capsule_ct)
# cfrag_ct = ReEncrypt(rekeys[0], capsule_ct)
cfrag_cts.append(cfrag_ct)
# print("cfrag_cts: ", cfrag_cts)
end_time = time.time()
re_elapsed_time = (end_time - start_time) / len(rekeys)
# print(f"重加密算法运行时间:{re_elapsed_time}秒")
# 9
start_time = time.time()
cfrags = mergecfrag(cfrag_cts)
# print("cfrags: ", cfrags)
# m = DecryptFrags(sk_b, pk_b, pk_a, cfrags)
m = DecryptFrags(sk_a, pk_b, pk_a, cfrags)
# print("m = ", m)
end_time = time.time()
elapsed_time = end_time - start_time
end_total_time = time.time()
total_time = end_total_time - start_total_time - re_elapsed_time * len(rekeys)
# print(f"解密算法运行时间:{elapsed_time}秒")
# print("成功解密:", m)
# print(f"算法总运行时间:{total_time}秒")
# print()

View File

@ -42,7 +42,17 @@ def send_ip():
# 用环境变量获取本机ip # 用环境变量获取本机ip
def get_local_ip(): def get_local_ip():
global ip global ip
ip = os.environ.get("HOST_IP", "IP not set") ip = os.environ.get("HOST_IP")
if not ip: # 如果环境变量中没有IP
try:
# 从网卡获取IP
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80)) # 通过连接Google DNS获取IP
ip = str(s.getsockname()[0])
s.close()
except:
raise ValueError("Unable to get IP")
def init(): def init():

49
src/perf.log Normal file
View File

@ -0,0 +1,49 @@
default
当前门限值: N = 20, T = 10
密钥生成运行时间:0.0026531219482421875秒
加密算法运行时间:0.005305290222167969秒
重加密密钥生成算法运行时间:0.0764169692993164秒
重加密算法运行时间:0.007088994979858399秒
解密算法运行时间:0.08717870712280273秒
成功解密: b'hello world'
算法总运行时间:0.1786430835723877秒
0.01 cores
当前门限值: N = 20, T = 10
密钥生成运行时间:0.9953160285949707秒
加密算法运行时间:0.006381511688232422秒
重加密密钥生成算法运行时间:12.903082609176636秒
重加密算法运行时间:0.989858603477478秒
解密算法运行时间:19.69758915901184秒
成功解密: b'hello world'
算法总运行时间:34.59222791194916秒
0.1 cores
当前门限值: N = 20, T = 10
密钥生成运行时间:0.004191160202026367秒
加密算法运行时间:0.09498381614685059秒
重加密密钥生成算法运行时间:0.7050104141235352秒
重加密算法运行时间:0.09499671459197997秒
解密算法运行时间:1.5005874633789062秒
成功解密: b'hello world'
算法总运行时间:2.3997695684432983秒
1 cores
当前门限值: N = 20, T = 10
密钥生成运行时间:0.0030488967895507812秒
加密算法运行时间:0.005570888519287109秒
重加密密钥生成算法运行时间:0.07791781425476074秒
重加密算法运行时间:0.006881630420684815秒
解密算法运行时间:0.08786344528198242秒
成功解密: b'hello world'
算法总运行时间:0.18128267526626587秒
4 cores
当前门限值: N = 20, T = 10
密钥生成运行时间:0.0026373863220214844秒
加密算法运行时间:0.004965305328369141秒
重加密密钥生成算法运行时间:0.07313323020935059秒
重加密算法运行时间:0.006896591186523438秒
解密算法运行时间:0.08880448341369629秒
成功解密: b'hello world'
算法总运行时间:0.17643699645996094秒

61
src/speed_test.py Normal file
View File

@ -0,0 +1,61 @@
from tpre import *
import time
N = 20
T = N // 2
print(f"当前门限值: N = {N}, T = {T}")
total_time = 0
# 1
start_time = time.time()
pk_a, sk_a = GenerateKeyPair()
m = b"hello world"
end_time = time.time()
elapsed_time = end_time - start_time
total_time += elapsed_time
print(f"密钥生成运行时间:{elapsed_time}")
# 2
start_time = time.time()
capsule_ct = Encrypt(pk_a, m)
end_time = time.time()
elapsed_time = end_time - start_time
total_time += elapsed_time
print(f"加密算法运行时间:{elapsed_time}")
# 3
pk_b, sk_b = GenerateKeyPair()
# 5
start_time = time.time()
id_tuple = tuple(range(N))
rekeys = GenerateReKey(sk_a, pk_b, N, T, id_tuple)
end_time = time.time()
elapsed_time = end_time - start_time
total_time += elapsed_time
print(f"重加密密钥生成算法运行时间:{elapsed_time}")
# 7
start_time = time.time()
cfrag_cts = []
for rekey in rekeys:
cfrag_ct = ReEncrypt(rekey, capsule_ct)
cfrag_cts.append(cfrag_ct)
end_time = time.time()
elapsed_time = (end_time - start_time) / len(rekeys)
total_time += elapsed_time
print(f"重加密算法运行时间:{elapsed_time}")
# 9
start_time = time.time()
cfrags = mergecfrag(cfrag_cts)
m = DecryptFrags(sk_b, pk_b, pk_a, cfrags)
end_time = time.time()
elapsed_time = end_time - start_time
total_time += elapsed_time
print(f"解密算法运行时间:{elapsed_time}")
print("成功解密:", m)
print(f"算法总运行时间:{total_time}")
print()