第5讲:隐语PSI介绍及开发实践
SPU 实现的PSI介绍
PSI定义:
安全求交集:Private Set Intersection(PSI)
- 一种特殊的安全多方计算(MPC)协议
- Alice持有集合 X,Bob持有集合Y
- Alice和Bob通过执行PSI协议,得到交集结果 X ∩ Y
- 除交集外不会泄漏交集外的其它信息
PSI分类:
- 2-Party/Multi-Party PSI
- Balanced/Unbalanced PSI
- Semi-honest/Malicious PSI
- PSI with computation:
- PSI-CA(Cardinality)
- PSI-Payload Analytics
- Circuit PSI
SPU 实现的PSI介绍
SPU实现的PSI种类
- 半诚实模型
- 两方
- ecdh、kkrt16、bc22(pcg-psi)
- ec-oprf PSI (Unbalanced PSI)
- dp-psi
- 多方
- ecdh-3-party(可扩展到多方)
- 两方
- 恶意模型
ecdh-PSI介绍
基于ecdh的三方PSI协议
协议流程:
1.Alice和Bob先进行交互,得到shuffle后的两方交集
2.Alice将shuffle后两方交集,发给Charlie
3.Charlie加密后的数据依次给Bob和Alice加密
4.Charlie比较密态数据,得到交集
SPU PSI 调度封装
统一入口
- 入口函数:bucket_psi, mem_psi
支持分桶求交
- 通过分桶支持大规模数据(10亿规模)
输入输出处理
- 检查求交id列是否数据是否完整
- 检查是否有重复项
输出处理
- 支持按求交id列排序
- 输出完整label列
bucket_psi:高级API,通过Hash分桶支持海量数据,覆盖生产级全流程(数据查重、分桶求交、结果广播、结果排序)
mem_psi:低级API,算法内核级的性能 + 统一易用的接口
Operator:算法接入层,向上提供统一接口接入工程化封装;注册工厂模式,提升协议工程化效率
SPU PSI调用架构
隐语PSI开发指南
Secretflow两种部署模式简介
隐语PSI开发指南
1、启动ray集群:
alice首先启动ray集群。注意这里的命令是启动Ray的主节点。
ray start --head --node-ip-address=
"ip" --port="port" --includedashboard=False --disable-usage-stats
bob首先启动ray集群
ray start --head --node-ip-address=
"ip" --port="port" --includedashboard=False --disable-usage-stats
2、初始化secretflow:
sf_cluster_config ={
'parties': {
'alice': {
# replace with alice's real address.
'address': 'ip:port of alice’
,
‘listen_addr': '0.0.0.0:port’
},
'bob': {
# replace with bob's real address.
'address': 'ip:port of bob’,
'listen_addr': '0.0.0.0:port’
},
},
'self_party': 'bob'
}
tls_config = {
"ca_cert": "ca root cert of other parties ",
"cert": "server cert of alice in pem"
,
"key": "server key of alice in pem"
,
}
sf.init(address='alice ray head node address', cluster_config=sf_cluster_config,
tls_config=tls_config)
sf.init(address='bob ray head node address', cluster_config=sf_cluster_config,
tls_config=tls_config )
3、启动SPU设备:
spu_cluster_def = {
nodes': [
# <<< !!! >>> replace <192.168.0.1:12945> to alice node's local ip & free port
{'party': 'alice'
, 'address': '192.168.0.1:12945',
'listen_address': '0.0.0.0:12945’},
# <<< !!! >>> replace <192.168.0.2:12946> to bob node's local ip & free port
{'party': 'bob', 'address': '192.168.0.2:12946',
'listen_address': '0.0.0.0:12946'},
],
'runtime_config': {
'protocol': spu.spu_pb2.SEMI2K,
'field': spu.spu_pb2.FM128,
},
}
spu = sf.SPU(spu_cluster_def)
4、执行PSI
reports = spu.psi_csv(
key=select_keys,
input_path=input_path,
output_path=output_path,
receiver=‘alice’, # receiver get output file.
# psi protocol KKRT_PSI_2PC, BC22_PSI_2PC
protocol=‘ECDH_PSI_2PC',
curve_type=’CURVE_25519’,
# ’CURVE_FOURQ’, ’CURVE_SM2’
precheck_input=False, # check inputfile duplicate entries
sort=False, # sort intersection by key ids
broadcast_result=False, # true receiver send intersection to
other parties
)
reports 结构
# 输入数据量总数
int64 original_count = 1;
# 交接结果
int64 intersection_count = 2
5、运行结果
PSI 交集输出
output_path = {
alice: ‘/data/psi_output.csv’, # 节点alice端的输出
bob: ‘/data/psi_output_bob.csv’, # 节点bob端的输出
}
隐语PSI后续计划
代码实践
这里需要两个设备
使用docker容器
先拉取镜像
docker pull secretflow/secretnote:unstable-amd64
配置 docker-compose脚本
services:
alice:
image: 'secretflow/secretnote:unstable-amd64'
platform: linux/amd64
environment:
- SELF_PARTY=alice
- ALL_PARTIES=alice,bob
ports:
- 8090:8888
entrypoint: /root/scripts/start.sh
volumes:
- /root/scripts
bob:
image: 'secretflow/secretnote:unstable-amd64'
platform: linux/amd64
environment:
- SELF_PARTY=bob
- ALL_PARTIES=alice,bob
ports:
- 8092:8888
entrypoint: /root/scripts/start.sh
volumes:
- /root/scripts
执行脚本
遇到问题
Command ‘docker-compose’ not found, but can be installed
sudo apt install docker-compose
安装docker compose
再次执行
docker-compose up -d
还是报错
> ERROR: The Compose file './docker-compose.yml' is invalid because:
> Unsupported config option for services: 'bob'
后面查看,compose到后面的版本需要在配置脚本时加入版本信息。(对应版本支持对应的配置文件字段)
配置版本过高,自身不支持也会报错
ERROR: Version in "./docker-compose.yml" is unsupported. You might be seeing this error because you're using the wrong Compose file version. Either specify a supported version (e.g "2.2" or "3.3") and place your service definitions under the `services` key, or omit the `version` key and place your service definitions at the root of the file to use version 1.
配置版本过低,也会报错,会有对应的字段不支持
ERROR: The Compose file './docker-compose.yml' is invalid because:
Unsupported config option for services.one: 'platform'
Unsupported config option for services.twor: 'platform'
详细支持的字段和版本号,参考官网:https://docs.docker.com/compose/compose-file/compose-versioning/#versioning
通过网页查询到platform字段对应的版本号为Version 2.4
解决完这个问题后,运行即可成功
(secret) code@code:~/sf$ docker-compose up -d
Creating network "sf_default" with the default driver
Creating sf_one_1 ... done
Creating sf_twor_1 ... done
访问浏览器访问127.0.0.1:8092,配置的端口号进入notebook
在右上角添加用户
注意这里添加用户时使用的ip和端口号
一个为容器本机分配的ip,端口号为映射到主机的端口(8888)
容器本机分配的ip查询
docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{e
nd}}' $(docker ps -aq)
查询容器本地ip地址
参考:https://www.cnblogs.com/wangbiaohistory/p/17403468.html
端口号注意要为映射到主机的端口号,不然会显示不在线
添加完成两个用户在线
通过开源地址下载对应的源码和csv文件
https://github.com/secretflow/secretnote/blob/main/docs/guide/data/iris_bob.csv
上传到secretnote
执行对应的psi.py的代码,进行求交
注意这里遇到的问题一开始说是找不到part one,parttwo也找不到
sf.shutdown()
这条语句报错
TypeError: 'NoneType' object is not iterable
Disconnect the worker, and terminate processes started by secretflow.init(). 593 594 This will automatically run at the end when a Python process that uses Ray exits. (...) 603 feedback. The default value is True.
初看应该就是找不到对应的服务或者说开启的容器?
这让前面的network_conf
配置无法成功,或者说进行的配置无法生效。
暂时不知道原因,可能我容器命名或者compose配置的服务的名称不同导致产生的问题?secretnote的名称需要和创建的服务名称相同?或者说是psi中代码的parties部分的网络,服务命名要和compose中的相同,因为考虑到psi中代码基本上全用的alice和bob,这边就把docker重启了下,重新配置了下服务名字,进行统一,后面执行成功。
总结
通过本次课,动手操作了sf的基本使用,以及docker的使用,过程中遇到了一些问题,也解决了不少问题,有所收获。