NKDBsec/example/sstest.py

87 lines
1.9 KiB
Python
Raw Normal View History

2024-01-20 14:14:46 +08:00
import time
curt = time.time()
import json
import numpy as np
import dbsecmpc
from multiprocessing import Process
conf = {
"NODE_INFO": [
{
"HOST": "127.0.0.1",
"PORT": 25500,
"NODE_ID": "P0"
},
{
"HOST": "127.0.0.1",
"PORT": 25700,
"NODE_ID": "P1"
},
{
"HOST": "127.0.0.1",
"PORT": 25900,
"NODE_ID": "P2"
}
],
"DATA_NODES": [
"P0",
"P1",
"P2"
],
"COMPUTATION_NODES": {
"P0": 0,
"P1": 1,
"P2": 2
},
"RESULT_NODES": [
"P0",
"P1",
"P2"
]
}
def test(id):
'''
This module aimed to test the secret share I/O
'''
confbuf = json.dumps(conf)
dbsecmpc.init(id, confbuf)
np.random.seed(199)
colnum, rownum = 2, 10
xplaintable = np.random.rand(colnum * rownum)*(100)
xplaintable.shape = (colnum, rownum)
print("P0 input: ", xplaintable)
rtx = dbsecmpc.privateinput(["P0"], xplaintable)
np.random.seed(307)
yplaintable = np.random.rand(colnum * rownum)*(100)
yplaintable.shape = (colnum, rownum)
print("P1 input: ", yplaintable)
rty = dbsecmpc.privateinput(["P1"], yplaintable)
# res = dbsecmpc.opvector(rtx, rty, dbsecmpc.OP_GREATER)
# print("P{} secret share: ".format(id), res)
res = dbsecmpc.opvector(dbsecmpc.opvector(rtx, rty, dbsecmpc.OP_ADD), rty, dbsecmpc.OP_MUL)
print("P{} secret share: ".format(id), res)
res = dbsecmpc.reveal(["P2"], res)
print("P{} output: ".format(id), res)
pc1 = Process(target = test, args=(0,))
pc2 = Process(target = test, args=(1,))
pc3 = Process(target = test, args=(2,))
st = time.time()
pc1.start()
pc2.start()
pc3.start()
pc1.join()
pc2.join()
pc3.join()
print(time.time() - st)