562
社区成员




SecretFlow 是一款隐私保护计算框架,用于实现数据的密态计算和共享,广泛应用于金融、医疗等领域,确保数据在流通和计算过程中的隐私安全。
docker run -it secretflow/secretflow-anolis8:latest
docker run -it secretflow-registry.cnhangzhou.cr.aliyuncs.com/secretflow-anolis8:latest
import secretflow as sf
print(sf.__version__)
方式二:pypi
pip installU – secretflow
pip install –U secretflow-lite
方式三:源码
需要 pip ≥19.3,建议使用 conda 管理 Python 环境。首先,下载源码并建立 Python 虚拟环境:
git clone https://github.com/secretflow/secretflow.git
cd secretflow
conda create -n secretflow python==3.8
conda activate secretflow
然后安装 SecretFlow。涉及到 C++ 编译,建议使用镜像 secretflow/release-ci:latest
:
python setup.py bdist_wheel
pip install dist/*.whl
特点 :适合仿真实验,验证代码效果,既支持单机仿真,也支持多机仿真,只需要执行一次代码,安全增强。
单机仿真示例
导入 SecretFlow 并初始化,指定参与方和地址为本地:
import secretflow as sf
sf.init(parties=['alice', 'bob'], address='local')
创建参与方对象,执行函数:
alice = sf.PYU('alice')
bob = sf.PYU('bob')
result_alice = alice(lambda x : x + 1)(2)
result_bob = bob(lambda x : x - 1)(2)
print(result_alice.reveal()) # 输出:3
print(result_bob.reveal()) # 输出:1
集群仿真示例
--node-ip-address="192.168.1.101" --port="6379"
--resources='{"alice": 16} '
--include-dashboard=False
--disable-usage-stats
* 在第二台机器上部署 Ray 从节点,模拟参与方 bob:```bash
ray start \
--address="192.168.1.101:6379" \
--resources=' {"bob": 16} ' \
--include-dashboard=False \
--disable-usage-stats
* 执行 Python 代码初始化 SecretFlow,指定参与方和 Ray 主节点地址:
import secretflow as sf
sf.init(parties=['alice', 'bob'], address='192.168.1.101:6379')
alice = sf.PYU('alice')
bob = sf.PYU('bob')
result_alice = alice(lambda x : x)(2)
result_bob = bob(lambda x : x)(2)
print(result_alice.reveal()) # 输出:2
print(result_bob.reveal()) # 输出:2
* 可选创建密态设备 SPU,用于多方安全计算:
import spu
cluster_def = {
'nodes': [{
'party': 'alice', 'address': '192.168.1.101:6380',
}, {
'party': 'bob', 'address': '192.168.1.102:6380',
},
], 'runtime_config': {
'protocol': spu.spu_pb2.SEMI2K, 'field': spu.spu_pb2.FM128, 'sigmoid_mode': spu.spu_pb2.RuntimeConfig.SIGMOID_REAL,
}
}
spu = sf.SPU(cluster_def=cluster_def)
特点 :相比仿真模式,每个参与方都是独立的 Ray 集群,所有参与方都需要执行代码,安全增强。
部署步骤
--node-ip-address="192.168.1.101" --port="6379"
--resources='{"alice": 16} '
--include-dashboard=False
--disable-usage-stats
在 bob 的机器上:
```bash
ray start --head \
--node-ip-address="192.168.1.102" --port="6379" \
--resources='{"bob": 16} ' \
--include-dashboard=False \
--disable-usage-stats
* 在每台机器上分别执行 Python 代码,初始化 SecretFlow,指定自己的参与方信息和其他参与方的地址。在 alice 的机器上:
import secretflow as sf
cluster_config ={
'parties': {
'alice’: {'address': '192.168.1.101:6379'}, 'bob': {'address': '192.168.1.102:6379'},
}, 'self_party': 'alice',
}
sf.init(address='192.168.1.101:6379', cluster_config=cluster_config)
在 bob 的机器上:
import secretflow as sf
cluster_config ={
'parties': {
'alice’: {'address': '192.168.1.101:6379'}, 'bob': {'address': '192.168.1.102:6379'},
}, 'self_party': 'bob',
}
sf.init(address='192.168.1.102:6379', cluster_config=cluster_config)
* 可选创建 SPU,用于多方安全计算:
import spu
cluster_def = {
'nodes': [{
'party': 'alice', 'address': '192.168.1.101:6380',
}, {
'party': 'bob', 'address': '192.168.1.102:6380',
},
], 'runtime_config': {
'protocol': spu.spu_pb2.SEMI2K, 'field': spu.spu_pb2.FM128, 'sigmoid_mode': spu.spu_pb2.RuntimeConfig.SIGMOID_REAL,
}
}
spu = sf.SPU(cluster_def=cluster_def)
sf.init(parties=['alice', 'bob'], address='local')
alice = sf.PYU('alice')
bob = sf.PYU('bob')
def compute_sum(x, y):
return x + y
result = alice(compute_sum)(2, bob(lambda: 3)())
print(result.reveal()) # 输出:5
建议使用 docker 方式运行 SecretNote:
```bash
docker run -p 8888:8888 secretflow/secretnote
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple
netstat -an | grep <端口>
命令查看端口是否被占用,如被占用则更换端口。sf.init(address='{Ray head node address}', cluster_config=cluster_config, timeout=30)
docker network create secretflow-network
然后在运行容器时指定该网络:
docker run --network=secretflow-network ...
SecretFlow 的安装方式多样,包括 docker 镜像、pypi 和源码安装,可根据实际需求选择。部署模式分为仿真模式和生产模式,仿真模式适合实验验证,生产模式适用于实际生产环境,需注意端口冲突和安全增强。同时,借助 KUSCIA 和 SecretNote 可进一步优化部署和使用体验,提高工作效率。在实际应用中,可参考以下示例代码进行操作:
安装验证 :
import secretflow as sf
print(sf.__version__)
单机仿真 :
import secretflow as sf
sf.init(parties=['alice', 'bob'], address='local')
alice = sf.PYU('alice')
bob = sf.PYU('bob')
result_alice = alice(lambda x : x + 1)(2)
result_bob = bob(lambda x : x - 1)(2)
print(result_alice.reveal())
print(result_bob.reveal())
集群仿真 :
import secretflow as sf
sf.init(parties=['alice', 'bob'], address='192.168.1.101:6379')
alice = sf.PYU('alice')
bob = sf.PYU('bob')
result_alice = alice(lambda x : x)(2)
result_bob = bob(lambda x : x)(2)
print(result_alice.reveal())
print(result_bob.reveal())
生产模式部署 :
import secretflow as sf
cluster_config ={
'parties': {
'alice’: {'address': '192.168.1.101:6379'}, 'bob': {'address': '192.168.1.102:6379'},
}, 'self_party': 'alice',
}
sf.init(address='192.168.1.101:6379', cluster_config=cluster_config)