forked from sangge/tpre-python
main #23
@ -1,4 +1,4 @@
|
||||
name: Deploy App
|
||||
name: Test CI
|
||||
|
||||
on:
|
||||
push:
|
||||
@ -6,41 +6,18 @@ on:
|
||||
- main
|
||||
|
||||
jobs:
|
||||
deploy:
|
||||
name: Deploy to Web Server
|
||||
test:
|
||||
name: test speed
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
container:
|
||||
image: catthehacker/ubuntu:act-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v3
|
||||
|
||||
|
||||
- name: copy file via ssh password
|
||||
uses: cross-the-world/scp-pipeline@master
|
||||
with:
|
||||
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/
|
||||
|
||||
|
||||
|
||||
- name: Run script in Docker container
|
||||
run: |
|
||||
ls $PWD/src
|
||||
docker run -v .:/app git.mamahaha.work/sangge/tpre:base ls
|
||||
|
@ -15,16 +15,15 @@ gmssl-python
|
||||
|
||||
## 安装步骤
|
||||
|
||||
```
|
||||
```bash
|
||||
pip install
|
||||
```
|
||||
|
||||
|
||||
## 使用说明
|
||||
|
||||
|
||||
## 参考文献
|
||||
https://www.cnblogs.com/pam-sh/p/17364656.html#tprelib%E7%AE%97%E6%B3%95
|
||||
|
||||
<https://www.cnblogs.com/pam-sh/p/17364656.html#tprelib%E7%AE%97%E6%B3%95>
|
||||
|
||||
## 许可证
|
||||
|
||||
|
26
README_en.md
26
README_en.md
@ -7,6 +7,7 @@ This project is designed for the National Cryptography Competition and is implem
|
||||
The project uses the Chinese national standard cryptography algorithm to implement distributed proxy re-encryption (TPRE).
|
||||
|
||||
## Project Structure
|
||||
|
||||
.
|
||||
├── basedockerfile (being used to build base iamge)
|
||||
├── dockerfile (being used to build application)
|
||||
@ -22,55 +23,70 @@ The project uses the Chinese national standard cryptography algorithm to impleme
|
||||
## Environment Dependencies
|
||||
|
||||
### Bare mental version(UNTESTED)
|
||||
|
||||
System requirements:
|
||||
|
||||
- Linux
|
||||
- Windows(may need to complie and install gmssl yourself)
|
||||
|
||||
The project relies on the following software:
|
||||
|
||||
- Python 3.11
|
||||
- gmssl
|
||||
- gmssl-python
|
||||
|
||||
### Docker installer
|
||||
|
||||
```bash
|
||||
apt update && apt install docker.io mosh -y
|
||||
```
|
||||
|
||||
### Docker version
|
||||
|
||||
docker version:
|
||||
|
||||
- Version: 24.0.5
|
||||
- API version: 1.43
|
||||
- Go version: go1.20.6
|
||||
|
||||
## Installation Steps
|
||||
|
||||
### Pre-installation
|
||||
|
||||
This project depends on gmssl, so you need to compile it from source first.
|
||||
Visit [GmSSL](https://github.com/guanzhi/GmSSL) to learn how to install.
|
||||
|
||||
Then install essential python libs
|
||||
|
||||
```bash
|
||||
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
|
||||
```
|
||||
|
||||
|
||||
## Docker Installation
|
||||
|
||||
### Use base image and build yourself
|
||||
|
||||
```bash
|
||||
docker build . -f basedockerfile -t git.mamahaha.work/sangge/tpre:base
|
||||
docker pull git.mamahaha.work/sangge/tpre:base
|
||||
(or docker pull git.mamahaha.work/sangge/tpre:base)
|
||||
docker build . -t your_image_name
|
||||
docker run your_image_name
|
||||
```
|
||||
|
||||
### Use pre-build image
|
||||
|
||||
```bash
|
||||
docker pull git.mamahaha.work/sangge/tpre:latest
|
||||
docker run git.mamahaha.work/sangge/tpre:latest
|
||||
```
|
||||
|
||||
## Usage Instructions
|
||||
|
||||
details in [docs](doc/README_app_en.md)
|
||||
|
||||
## References
|
||||
|
||||
[TPRE Algorithm Blog Post](https://www.cnblogs.com/pam-sh/p/17364656.html#tprelib%E7%AE%97%E6%B3%95)
|
||||
[Gmssl-python library](https://github.com/GmSSL/GmSSL-Python)
|
||||
|
||||
|
||||
## License
|
||||
|
||||
GNU GENERAL PUBLIC LICENSE v3
|
||||
|
@ -2,7 +2,11 @@ FROM python:3.11
|
||||
|
||||
COPY requirements.txt /app/
|
||||
|
||||
COPY lib/* /lib/
|
||||
# 设置目标平台参数
|
||||
ARG TARGETPLATFORM
|
||||
|
||||
# 根据目标平台复制相应架构的库文件
|
||||
COPY lib/${TARGETPLATFORM}/* /lib/
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
|
@ -1,27 +1,61 @@
|
||||
# APP Doc
|
||||
|
||||
## Run docker
|
||||
|
||||
```bash
|
||||
docker run -it -p 8000:8000 -p 8001:8001 -p 8002:8002 -v ~/mimajingsai:/app -e HOST_IP=110.41.130.197 git.mamahaha.work/sangge/tpre:base bash
|
||||
```
|
||||
|
||||
```bash
|
||||
tpre3: docker run -it -p 8000:8000 -p 8001:8001 -p 8002:8002 -v ~/mimajingsai:/app -e HOST_IP=60.204.233.103 git.mamahaha.work/sangge/tpre:base bash
|
||||
```
|
||||
|
||||
## Cloud server ip
|
||||
|
||||
**tpre1**: 110.41.155.96
|
||||
**tpre2**: 110.41.130.197
|
||||
**tpre3**: 110.41.21.35
|
||||
|
||||
## Agent re-encryption process
|
||||
|
||||
### Client request message
|
||||
|
||||
```bash
|
||||
python client_cli.py 110.41.21.35 aaa
|
||||
```
|
||||
|
||||
## Client router
|
||||
|
||||
/request_node
|
||||
get method
|
||||
pr
|
||||
**/receive_messages**
|
||||
post method
|
||||
|
||||
**/request_message**
|
||||
post method
|
||||
|
||||
docker run -it -p 8000:8000 -p 8001:8001 -p 8002:8002 -v ~/mimajingsai:/app -e HOST_IP=110.41.130.197 git.mamahaha.work/sangge/tpre:base bash
|
||||
**/receive_request**
|
||||
post method
|
||||
|
||||
**/recieve_pk**
|
||||
post method
|
||||
|
||||
tpre3: docker run -it -p 8000:8000 -p 8001:8001 -p 8002:8002 -v ~/mimajingsai:/app -e HOST_IP=60.204.233.103 git.mamahaha.work/sangge/tpre:base bash
|
||||
## Central server router
|
||||
|
||||
**/server/show_nodes**
|
||||
get method
|
||||
|
||||
110.41.155.96 tpre1
|
||||
110.41.130.197 tpre2
|
||||
110.41.21.35 tpre3
|
||||
**/server/get_node**
|
||||
get method
|
||||
|
||||
python client_cli.py 110.41.21.35 aaa
|
||||
**/server/delete_node**
|
||||
get method
|
||||
|
||||
**/server/heartbeat**
|
||||
get method
|
||||
|
||||
apt update && apt install docker.io mosh -y
|
||||
**/server/send_nodes_list**
|
||||
get method
|
||||
|
||||
60.204.236.38 tpre1
|
||||
1.94.42.18 tpre2
|
||||
60.204.233.103 tpre3
|
||||
## Node router
|
||||
|
||||
**/user_src**
|
||||
post method
|
||||
|
BIN
lib/linux/arm64/libgmssl.so
Executable file
BIN
lib/linux/arm64/libgmssl.so
Executable file
Binary file not shown.
BIN
lib/linux/arm64/libgmssl.so.3
Executable file
BIN
lib/linux/arm64/libgmssl.so.3
Executable file
Binary file not shown.
BIN
lib/linux/arm64/libgmssl.so.3.1
Executable file
BIN
lib/linux/arm64/libgmssl.so.3.1
Executable file
Binary file not shown.
BIN
lib/linux/arm64/libsdf_dummy.so
Executable file
BIN
lib/linux/arm64/libsdf_dummy.so
Executable file
Binary file not shown.
BIN
lib/linux/arm64/libsdf_dummy.so.3
Executable file
BIN
lib/linux/arm64/libsdf_dummy.so.3
Executable file
Binary file not shown.
BIN
lib/linux/arm64/libsdf_dummy.so.3.1
Executable file
BIN
lib/linux/arm64/libsdf_dummy.so.3.1
Executable file
Binary file not shown.
BIN
lib/linux/arm64/libskf_dummy.so
Executable file
BIN
lib/linux/arm64/libskf_dummy.so
Executable file
Binary file not shown.
BIN
lib/linux/arm64/libskf_dummy.so.3
Executable file
BIN
lib/linux/arm64/libskf_dummy.so.3
Executable file
Binary file not shown.
BIN
lib/linux/arm64/libskf_dummy.so.3.1
Executable file
BIN
lib/linux/arm64/libskf_dummy.so.3.1
Executable file
Binary file not shown.
@ -95,6 +95,7 @@ def clean_env():
|
||||
with sqlite3.connect("client.db") as db:
|
||||
db.execute("DELETE FROM node")
|
||||
db.execute("DELETE FROM message")
|
||||
db.execute("DELETE FROM senderinfo")
|
||||
db.commit()
|
||||
print("Exit app")
|
||||
|
||||
@ -121,8 +122,10 @@ async def receive_messages(message: C):
|
||||
return:
|
||||
status_code
|
||||
"""
|
||||
print(f"Received message: {message}")
|
||||
|
||||
if not message.Tuple or not message.ip:
|
||||
print("Invalid input data received.")
|
||||
raise HTTPException(status_code=400, detail="Invalid input data")
|
||||
|
||||
C_capsule, C_ct = message.Tuple
|
||||
@ -144,6 +147,7 @@ async def receive_messages(message: C):
|
||||
(bin_C_capsule, str(C_ct), ip),
|
||||
)
|
||||
db.commit()
|
||||
print("Data inserted successfully into database.")
|
||||
check_merge(C_ct, ip)
|
||||
return HTTPException(status_code=200, detail="Message received")
|
||||
except Exception as e:
|
||||
@ -211,7 +215,7 @@ def check_merge(ct: int, ip: str):
|
||||
|
||||
print("merge success", message)
|
||||
node_response = True
|
||||
|
||||
|
||||
print("merge:", node_response)
|
||||
|
||||
|
||||
@ -230,11 +234,13 @@ async def send_messages(
|
||||
for i in range(4):
|
||||
id += int(ip_parts[i]) << (24 - (8 * i))
|
||||
id_list.append(id)
|
||||
print(f"Calculated IDs: {id_list}")
|
||||
# generate rk
|
||||
rk_list = GenerateReKey(sk, pk_B, len(node_ips), shreshold, tuple(id_list)) # type: ignore
|
||||
|
||||
print(f"Generated ReKey list: {rk_list}")
|
||||
capsule, ct = Encrypt(pk, message) # type: ignore
|
||||
# capsule_ct = (capsule, int.from_bytes(ct))
|
||||
print(f"Encrypted message to capsule={capsule}, ct={ct}")
|
||||
|
||||
for i in range(len(node_ips)):
|
||||
url = "http://" + node_ips[i][0] + ":8001" + "/user_src"
|
||||
@ -245,11 +251,15 @@ async def send_messages(
|
||||
"ct": int.from_bytes(ct),
|
||||
"rk": rk_list[i],
|
||||
}
|
||||
print(json.dumps(payload))
|
||||
print(f"Sending payload to {url}: {json.dumps(payload)}")
|
||||
response = requests.post(url, json=payload)
|
||||
|
||||
if response.status_code == 200:
|
||||
print(f"send to {node_ips[i]} successful")
|
||||
else:
|
||||
print(
|
||||
f"Failed to send to {node_ips[i]}. Response code: {response.status_code}, Response text: {response.text}"
|
||||
)
|
||||
return 0
|
||||
|
||||
|
||||
@ -269,6 +279,9 @@ class Request_Message(BaseModel):
|
||||
@app.post("/request_message")
|
||||
async def request_message(i_m: Request_Message):
|
||||
global message, node_response, pk
|
||||
print(
|
||||
f"Function 'request_message' called with: dest_ip={i_m.dest_ip}, message_name={i_m.message_name}"
|
||||
)
|
||||
dest_ip = i_m.dest_ip
|
||||
# dest_ip = dest_ip.split(":")[0]
|
||||
message_name = i_m.message_name
|
||||
@ -281,21 +294,25 @@ async def request_message(i_m: Request_Message):
|
||||
"source_ip": source_ip,
|
||||
"pk": pk,
|
||||
}
|
||||
print(f"Sending request to {url} with payload: {payload}")
|
||||
try:
|
||||
response = requests.post(url, json=payload, timeout=1)
|
||||
print(f"Response received from {url}: {response.text}")
|
||||
# print("menxian and pk", response.text)
|
||||
|
||||
except requests.Timeout:
|
||||
print("can't post")
|
||||
print("Timeout error: can't post to the destination.")
|
||||
# print("can't post")
|
||||
# content = {"message": "post timeout", "error": str(e)}
|
||||
# return JSONResponse(content, status_code=400)
|
||||
|
||||
# wait 3s to receive message from nodes
|
||||
for _ in range(10):
|
||||
print("wait:", node_response)
|
||||
print(f"Waiting for node_response... Current value: {node_response}")
|
||||
# print("wait:", node_response)
|
||||
if node_response:
|
||||
data = message
|
||||
|
||||
print(f"Node response received with message: {data}")
|
||||
# reset message and node_response
|
||||
message = b""
|
||||
node_response = False
|
||||
@ -303,6 +320,7 @@ async def request_message(i_m: Request_Message):
|
||||
# return message to frontend
|
||||
return {"message": str(data)}
|
||||
await asyncio.sleep(0.2)
|
||||
print("Timeout while waiting for node_response.")
|
||||
content = {"message": "receive timeout"}
|
||||
return JSONResponse(content, status_code=400)
|
||||
|
||||
@ -311,14 +329,20 @@ async def request_message(i_m: Request_Message):
|
||||
@app.post("/receive_request")
|
||||
async def receive_request(i_m: IP_Message):
|
||||
global pk
|
||||
print(
|
||||
f"Function 'receive_request' called with: dest_ip={i_m.dest_ip}, source_ip={i_m.source_ip}, pk={i_m.pk}"
|
||||
)
|
||||
source_ip = get_own_ip()
|
||||
print(f"Own IP: {source_ip}")
|
||||
if source_ip != i_m.dest_ip:
|
||||
print("Mismatch in destination IP.")
|
||||
return HTTPException(status_code=400, detail="Wrong ip")
|
||||
dest_ip = i_m.source_ip
|
||||
# threshold = random.randrange(1, 2)
|
||||
threshold = 2
|
||||
own_public_key = pk
|
||||
pk_B = i_m.pk
|
||||
print(f"Using own public key: {own_public_key} and received public key: {pk_B}")
|
||||
|
||||
with sqlite3.connect("client.db") as db:
|
||||
cursor = db.execute(
|
||||
@ -330,20 +354,33 @@ async def receive_request(i_m: IP_Message):
|
||||
(threshold,),
|
||||
)
|
||||
node_ips = cursor.fetchall()
|
||||
print(f"Selected node IPs from database: {node_ips}")
|
||||
|
||||
# message name
|
||||
# message_name = i_m.message_name
|
||||
# message = xxxxx
|
||||
message = b"hello world" + random.randbytes(8)
|
||||
print(f"Generated message: {message}")
|
||||
|
||||
# send message to nodes
|
||||
await send_messages(tuple(node_ips), message, dest_ip, pk_B, threshold)
|
||||
response = {"threshold": threshold, "public_key": own_public_key}
|
||||
print("###############RESPONSE = ", response)
|
||||
print(f"Sending response: {response}")
|
||||
return response
|
||||
|
||||
|
||||
def get_own_ip() -> str:
|
||||
ip = os.environ.get("HOST_IP", "IP not set")
|
||||
return ip
|
||||
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 = s.getsockname()[0]
|
||||
s.close()
|
||||
except:
|
||||
raise ValueError("Unable to get IP")
|
||||
return str(ip)
|
||||
|
||||
|
||||
# get node list from central server
|
||||
|
93
src/demo2.py
Normal file
93
src/demo2.py
Normal file
@ -0,0 +1,93 @@
|
||||
from tpre import *
|
||||
import time
|
||||
import openpyxl
|
||||
|
||||
# 初始化Excel工作簿和工作表
|
||||
wb = openpyxl.Workbook()
|
||||
ws = wb.active
|
||||
ws.title = "算法性能结果"
|
||||
headers = [
|
||||
"门限值 N",
|
||||
"门限值 T",
|
||||
"密钥生成运行时间",
|
||||
"加密算法运行时间",
|
||||
"重加密密钥生成算法运行时间",
|
||||
"重加密算法运行时间",
|
||||
"解密算法运行时间",
|
||||
"算法总运行时间",
|
||||
]
|
||||
ws.append(headers)
|
||||
|
||||
|
||||
for N in range(4, 21, 2):
|
||||
T = N // 2
|
||||
print(f"当前门限值: N = {N}, T = {T}")
|
||||
|
||||
start_total_time = time.time()
|
||||
# 1
|
||||
start_time = time.time()
|
||||
pk_a, sk_a = GenerateKeyPair()
|
||||
m = b"hello world"
|
||||
end_time = time.time()
|
||||
elapsed_time_key_gen = end_time - start_time
|
||||
print(f"密钥生成运行时间:{elapsed_time_key_gen}秒")
|
||||
|
||||
# ... [中间代码不变]
|
||||
# 2
|
||||
start_time = time.time()
|
||||
capsule_ct = Encrypt(pk_a, m)
|
||||
end_time = time.time()
|
||||
elapsed_time_enc = end_time - start_time
|
||||
print(f"加密算法运行时间:{elapsed_time_enc}秒")
|
||||
|
||||
# 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_rekey_gen = end_time - start_time
|
||||
print(f"重加密密钥生成算法运行时间:{elapsed_time_rekey_gen}秒")
|
||||
|
||||
# 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()
|
||||
re_elapsed_time = (end_time - start_time) / len(rekeys)
|
||||
print(f"重加密算法运行时间:{re_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_dec = 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_dec}秒")
|
||||
print("成功解密:", m)
|
||||
print(f"算法总运行时间:{total_time}秒")
|
||||
print()
|
||||
|
||||
# 将结果保存到Excel
|
||||
ws.append(
|
||||
[
|
||||
N,
|
||||
T,
|
||||
elapsed_time_key_gen,
|
||||
elapsed_time_enc,
|
||||
elapsed_time_rekey_gen,
|
||||
re_elapsed_time,
|
||||
elapsed_time_dec,
|
||||
total_time,
|
||||
]
|
||||
)
|
||||
|
||||
# 保存Excel文件
|
||||
wb.save("结果.xlsx")
|
@ -1,19 +1,21 @@
|
||||
from tpre import *
|
||||
import time
|
||||
|
||||
for N in range(4,21,4):
|
||||
# N = 10
|
||||
# T = 5
|
||||
T = N // 2
|
||||
print(f"当前门限值: N = {N}, T = {T}")
|
||||
|
||||
start_total_time = time.time()
|
||||
N = 20
|
||||
T = N // 2
|
||||
print(f"当前门限值: N = {N}, T = {T}")
|
||||
|
||||
for i in range(1, 10):
|
||||
total_time = 0
|
||||
|
||||
# 1
|
||||
start_time = time.time()
|
||||
pk_a, sk_a = GenerateKeyPair()
|
||||
m = b"hello world"
|
||||
m = b"hello world" * pow(10, i)
|
||||
print(f"明文长度:{len(m)}")
|
||||
end_time = time.time()
|
||||
elapsed_time = end_time - start_time
|
||||
total_time += elapsed_time
|
||||
print(f"密钥生成运行时间:{elapsed_time}秒")
|
||||
|
||||
# 2
|
||||
@ -21,18 +23,19 @@ for N in range(4,21,4):
|
||||
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
|
||||
@ -44,6 +47,7 @@ for N in range(4,21,4):
|
||||
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
|
||||
@ -52,9 +56,8 @@ for N in range(4,21,4):
|
||||
m = DecryptFrags(sk_b, pk_b, pk_a, cfrags)
|
||||
end_time = time.time()
|
||||
elapsed_time = end_time - start_time
|
||||
end_total_time = time.time()
|
||||
total_time = end_total_time - start_total_time
|
||||
total_time += elapsed_time
|
||||
print(f"解密算法运行时间:{elapsed_time}秒")
|
||||
print("成功解密:", m)
|
||||
print("成功解密:")
|
||||
print(f"算法总运行时间:{total_time}秒")
|
||||
print()
|
66
src/maxnode_test.py
Normal file
66
src/maxnode_test.py
Normal file
@ -0,0 +1,66 @@
|
||||
from tpre import *
|
||||
import time
|
||||
|
||||
N = 80
|
||||
total_time = 0
|
||||
|
||||
while total_time < 1:
|
||||
|
||||
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()
|
||||
N += 1
|
26
src/node.py
26
src/node.py
@ -36,12 +36,23 @@ def send_ip():
|
||||
# ip = get_local_ip() # type: ignore
|
||||
global id
|
||||
id = requests.get(url, timeout=3)
|
||||
print("中心服务器返回节点ID为: ", id)
|
||||
|
||||
|
||||
# 用环境变量获取本机ip
|
||||
def get_local_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():
|
||||
@ -84,7 +95,10 @@ class Req(BaseModel):
|
||||
@app.post("/user_src") # 接收用户1发送的信息
|
||||
async def user_src(message: Req):
|
||||
global client_ip_src, client_ip_des
|
||||
# kfrag , capsule_ct ,client_ip_src , client_ip_des = json_data[] # 看梁俊勇
|
||||
print(
|
||||
f"Function 'user_src' called with: source_ip={message.source_ip}, dest_ip={message.dest_ip}, capsule={message.capsule}, ct={message.ct}, rk={message.rk}"
|
||||
)
|
||||
# kfrag , capsule_ct ,client_ip_src , client_ip_des = json_data[]
|
||||
"""
|
||||
payload = {
|
||||
"source_ip": local_ip,
|
||||
@ -100,10 +114,12 @@ async def user_src(message: Req):
|
||||
ct = message.ct
|
||||
capsule_ct = (capsule, ct.to_bytes(32))
|
||||
rk = message.rk
|
||||
|
||||
print(f"Computed capsule_ct: {capsule_ct}")
|
||||
a, b = ReEncrypt(rk, capsule_ct)
|
||||
processed_message = (a, int.from_bytes(b))
|
||||
print(f"Re-encrypted message: {processed_message}")
|
||||
await send_user_des_message(source_ip, dest_ip, processed_message)
|
||||
print("Message sent to destination user.")
|
||||
return HTTPException(status_code=200, detail="message recieved")
|
||||
|
||||
|
||||
@ -114,10 +130,10 @@ async def send_user_des_message(source_ip: str, dest_ip: str, re_message): #
|
||||
response = requests.post(
|
||||
"http://" + dest_ip + ":8002" + "/receive_messages", json=data
|
||||
)
|
||||
print("send stauts:" ,response.text)
|
||||
print("send stauts:", response.text)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn # pylint: disable=e0401
|
||||
|
||||
uvicorn.run("node:app", host="0.0.0.0", port=8001, reload=True,log_level="debug")
|
||||
uvicorn.run("node:app", host="0.0.0.0", port=8001, reload=True, log_level="debug")
|
||||
|
148
src/perf.log
Normal file
148
src/perf.log
Normal file
@ -0,0 +1,148 @@
|
||||
12th Gen Intel(R) Core(TM) i5-12490F
|
||||
|
||||
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秒
|
||||
|
||||
rk3399
|
||||
0.01 cores
|
||||
当前门限值: N = 20, T = 10
|
||||
密钥生成运行时间:3.9984750747680664秒
|
||||
加密算法运行时间:9.599598169326782秒
|
||||
重加密密钥生成算法运行时间:132.99906015396118秒
|
||||
重加密算法运行时间:12.120013177394867秒
|
||||
解密算法运行时间:153.29800581932068秒
|
||||
成功解密: b'hello world'
|
||||
算法总运行时间:312.0151523947716秒
|
||||
|
||||
0.1 cores
|
||||
当前门限值: N = 20, T = 10
|
||||
密钥生成运行时间:0.09907650947570801秒
|
||||
加密算法运行时间:0.205247163772583秒
|
||||
重加密密钥生成算法运行时间:7.498294830322266秒
|
||||
重加密算法运行时间:0.7300507187843323秒
|
||||
解密算法运行时间:8.998314619064331秒
|
||||
成功解密: b'hello world'
|
||||
算法总运行时间:17.53098384141922秒
|
||||
|
||||
1 cores
|
||||
当前门限值: N = 20, T = 10
|
||||
密钥生成运行时间:0.008650541305541992秒
|
||||
加密算法运行时间:0.02130866050720215秒
|
||||
重加密密钥生成算法运行时间:0.30187034606933594秒
|
||||
重加密算法运行时间:0.0274674654006958秒
|
||||
解密算法运行时间:0.3521096706390381秒
|
||||
成功解密: b'hello world'
|
||||
算法总运行时间:0.7114066839218139秒
|
||||
|
||||
4 cores
|
||||
当前门限值: N = 20, T = 10
|
||||
密钥生成运行时间:0.00883340835571289秒
|
||||
加密算法运行时间:0.021309614181518555秒
|
||||
重加密密钥生成算法运行时间:0.3036940097808838秒
|
||||
重加密算法运行时间:0.0277299165725708秒
|
||||
解密算法运行时间:0.3464491367340088秒
|
||||
成功解密: b'hello world'
|
||||
算法总运行时间:0.7080160856246949秒
|
||||
|
||||
12th Gen Intel(R) Core(TM) i5-12490F
|
||||
|
||||
当前门限值: N = 20, T = 10
|
||||
明文长度:110
|
||||
密钥生成运行时间:0.0033216476440429688秒
|
||||
加密算法运行时间:0.00811624526977539秒
|
||||
重加密密钥生成算法运行时间:0.11786699295043945秒
|
||||
重加密算法运行时间:0.009650790691375732秒
|
||||
解密算法运行时间:0.12125396728515625秒
|
||||
成功解密:
|
||||
算法总运行时间:0.2602096438407898秒
|
||||
|
||||
明文长度:1100
|
||||
密钥生成运行时间:0.0034990310668945312秒
|
||||
加密算法运行时间:0.008537054061889648秒
|
||||
重加密密钥生成算法运行时间:0.10165071487426758秒
|
||||
重加密算法运行时间:0.009756004810333252秒
|
||||
解密算法运行时间:0.13438773155212402秒
|
||||
成功解密:
|
||||
算法总运行时间:0.257830536365509秒
|
||||
|
||||
明文长度:11000
|
||||
密钥生成运行时间:0.0035142898559570312秒
|
||||
加密算法运行时间:0.005819797515869141秒
|
||||
重加密密钥生成算法运行时间:0.1130058765411377秒
|
||||
重加密算法运行时间:0.010429942607879638秒
|
||||
解密算法运行时间:0.1242990493774414秒
|
||||
成功解密:
|
||||
算法总运行时间:0.25706895589828493秒
|
||||
|
||||
明文长度:110000
|
||||
密钥生成运行时间:0.002706289291381836秒
|
||||
加密算法运行时间:0.00833749771118164秒
|
||||
重加密密钥生成算法运行时间:0.11022734642028809秒
|
||||
重加密算法运行时间:0.010864639282226562秒
|
||||
解密算法运行时间:0.13867974281311035秒
|
||||
成功解密:
|
||||
算法总运行时间:0.2708155155181885秒
|
||||
|
||||
明文长度:1100000
|
||||
密钥生成运行时间:0.003710031509399414秒
|
||||
加密算法运行时间:0.04558920860290527秒
|
||||
重加密密钥生成算法运行时间:0.10261368751525879秒
|
||||
重加密算法运行时间:0.009720635414123536秒
|
||||
解密算法运行时间:0.15311646461486816秒
|
||||
成功解密:
|
||||
算法总运行时间:0.3147500276565552秒
|
||||
|
||||
明文长度:11000000
|
||||
密钥生成运行时间:0.008045673370361328秒
|
||||
加密算法运行时间:0.3575568199157715秒
|
||||
重加密密钥生成算法运行时间:0.09267783164978027秒
|
||||
重加密算法运行时间:0.009347784519195556秒
|
||||
解密算法运行时间:0.4754812717437744秒
|
||||
成功解密:
|
||||
算法总运行时间:0.9431093811988831秒
|
||||
|
||||
当前门限值: N = 94, T = 47
|
||||
算法总运行时间:0.967951292687274秒
|
||||
|
||||
当前门限值: N = 95, T = 47
|
||||
算法总运行时间:0.9765587304767809秒
|
||||
|
||||
当前门限值: N = 96, T = 48
|
||||
算法总运行时间:1.019304744899273秒
|
@ -65,9 +65,11 @@ async def get_node(ip: str) -> int:
|
||||
ip_int = 0
|
||||
for i in range(4):
|
||||
ip_int += int(ip_parts[i]) << (24 - (8 * i))
|
||||
print("IP", ip, "对应的ID为", ip_int)
|
||||
|
||||
# 获取当前时间
|
||||
current_time = int(time.time())
|
||||
print("当前时间: ", current_time)
|
||||
|
||||
# 插入数据
|
||||
cursor.execute(
|
||||
@ -102,6 +104,7 @@ async def delete_node(ip: str) -> None:
|
||||
# 接收节点心跳包
|
||||
@app.get("/server/heartbeat")
|
||||
async def receive_heartbeat(ip: str):
|
||||
print("收到来自", ip, "的心跳包")
|
||||
cursor.execute(
|
||||
"UPDATE nodes SET last_heartbeat = ? WHERE ip = ?", (time.time(), ip)
|
||||
)
|
||||
@ -112,7 +115,9 @@ async def receive_heartbeat_internal():
|
||||
while 1:
|
||||
timeout = 70
|
||||
# 删除超时的节点
|
||||
cursor.execute("DELETE FROM nodes WHERE last_heartbeat < ?", (time.time() - timeout,))
|
||||
cursor.execute(
|
||||
"DELETE FROM nodes WHERE last_heartbeat < ?", (time.time() - timeout,)
|
||||
)
|
||||
conn.commit()
|
||||
await asyncio.sleep(timeout)
|
||||
|
||||
@ -135,6 +140,8 @@ async def send_nodes_list(count: int) -> list:
|
||||
id, ip, last_heartbeat = row
|
||||
nodes_list.append(ip)
|
||||
|
||||
print("收到来自客户端的节点列表请求...")
|
||||
print(nodes_list)
|
||||
return nodes_list
|
||||
|
||||
|
||||
|
61
src/speed_test.py
Normal file
61
src/speed_test.py
Normal 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()
|
21
todolist.md
Normal file
21
todolist.md
Normal file
@ -0,0 +1,21 @@
|
||||
# todolist
|
||||
|
||||
- [x] 测试单核和多核性能
|
||||
- 这个算法在获得足够的CPU资源(即接近或等于1个完整核心)时表现最佳。
|
||||
- 过低的CPU资源分配会严重影响性能,而适度的分配(如0.1核心)则能提供更合理的性能。
|
||||
- 单核和多核性能差异不大
|
||||
|
||||
- [x] 测试不同cpu架构性能的差异
|
||||
- 测试了12th Gen Intel(R) Core(TM) i5-12490F 和 rk3399两颗cpu的性能
|
||||
|
||||
- [x] 测试不同消息长度的时间
|
||||
- 测试了10M文本的加密速度,在1s内可以完成全部算法内容
|
||||
|
||||
- [ ] 测试极限(1s)时的节点数
|
||||
- 12th Gen i5 CPU大概是90多个节点时达到1s的时间上限
|
||||
|
||||
- [x] 非docker部署需要获取本机ip
|
||||
- 添加了通过网卡获取ip的方法
|
||||
|
||||
- [ ] 复习预备知识
|
||||
- [ ] 准备圆场话术
|
Loading…
x
Reference in New Issue
Block a user