SPU(安全处理单元)是一个特定领域的编译器和运行时套件,旨在提供具有可证明安全性的安全计算服务。
SPU实现了多种MPC协议,包括:
SPU的API十分简单,主要有三个主要模块,对应之前的模块描述。
SecretFlow中的SPU
在SecretFlow中,SPU设备包装了上述所有SPU的概念,并提供了更简单的编程接口。
使用示例
import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import Normalizer
def breast_cancer(party_id=None, train: bool = True) -> (np.ndarray, np.ndarray):
scaler = Normalizer(norm='max')
x, y = load_breast_cancer(return_X_y=True)
x = scaler.fit_transform(x)
x_train, x_test, y_train, y_test = train_test_split(
x, y, test_size=0.2, random_state=42
)
if train:
if party_id:
if party_id == 1:
return x_train[:, 15:], _
else:
return x_train[:, :15], y_train
else:
return x_train, y_train
else:
return x_test, y_test
import secretflow as sf
# In case you have a running secretflow runtime already.
sf.shutdown()
sf.init(['alice', 'bob'], num_cpus=8, log_to_driver=True)
alice, bob = sf.PYU('alice'), sf.PYU('bob')
spu = sf.SPU(sf.utils.testing.cluster_def(['alice', 'bob']))
# 加载数据
x1, _ = alice(breast_cancer)(party_id=1)
x2, y = bob(breast_cancer)(party_id=2)
# 导入数据
device = spu
W = jnp.zeros((30,))
b = 0.0
W_, b_, x1_, x2_, y_ = (
sf.to(device, W),
sf.to(device, b),
x1.to(device),
x2.to(device),
y.to(device),
)
#训练模型
losses, W_, b_ = device(
fit,
static_argnames=['epochs'],
num_returns_policy=sf.device.SPUCompilerNumReturnsPolicy.FROM_USER,
user_specified_num_returns=3,
)(W_, b_, x1_, x2_, y_, epochs=10, learning_rate=1e-2)
losses, W_, b_
#reveal result
%matplotlib inline
losses = sf.reveal(losses)
plot_losses(losses)
auc = validate_model(sf.reveal(W_), sf.reveal(b_), X_test, y_test)
print(f'auc={auc}')
A typical SPU config:
import spu
import secteflow as sf
cluster_def={
'nodes': [
{
'party': 'alice',
'id': '0',
# Use the address and port of alice instead.
# Please choose a unused port.
'address': 'address:port',
},
{
'party': 'bob',
'id': '1',
# Use the ip and port of bob instead.
# Please choose a unused port.
'address': 'address:port',
},
],
'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)
standalone config
standalone mode because sf.utils.testing.cluster_def use 127.0.0.1 as the default ip.
spu = sf.SPU(sf.utils.testing.cluster_def(['alice', 'bob']))
https://spu.readthedocs.io/en/beta/reference/runtime_config.html
spu算子调度解析
https://github.com/secretflow/yasl
YASL (Yet Another Simple library) is a C++ library that contains modules and utilities which other SecretFlow code depends on.
隐私求交(Private Set Intersection)是一种使用密码学方法,获取两份数据内容的交集的算法。PSI过程中不泄露任务交集以外的信息。
在隐语中,SPU设备支持三种隐私求交算法:
示例:
使用 uid 作为求交列来处理这两个数据,SPU提供 psi_csv 函数, psi_csv 将csv文件作为输入,并在求交后生成csv文件。默认协议为KKRT。
# alice, bob: PYU设备,负责参与者的本地明文计算。
# spu:SPU设备,由alice和bob组成,负责双方的密文计算。
alice, bob = sf.PYU('alice'), sf.PYU('bob')
spu = sf.SPU(sf.utils.testing.cluster_def(['alice', 'bob']))
input_path = {alice: '.data/alice.csv', bob: '.data/bob.csv'}
output_path = {alice: '.data/alice_psi.csv', bob: '.data/bob_psi.csv'}
spu.psi_csv('uid', input_path, output_path, 'alice')
三方ECDH求交
使用 uid 和 month 作为求交键来执行三方求交。目前三方隐私求交暂时只支持ECDH协议。
carol = sf.PYU('carol')
spu_3pc = sf.SPU(sf.utils.testing.cluster_def(['alice', 'bob', 'carol']))
input_path = {alice: '.data/alice.csv', bob: '.data/bob.csv', carol: '.data/carol.csv'}
output_path = {alice: '.data/alice_psi.csv', bob: '.data/bob_psi.csv', carol: '.data/carol_psi.csv'}
spu_3pc.psi_csv(['uid', 'month'], input_path, output_path, 'alice', protocol='ECDH_PSI_3PC')
build
https://github.com/secretflow/spu/blob/beta/CONTRIBUTING.md#build
通过docker
最新的版本请查看 secretflow tags
docker run -it secretflow/secretflow-anolis8:${version}
cluster 部署
https://secretflow.readthedocs.io/zh_CN/latest/getting_started/deployment.html
https://heu.readthedocs.io/zh_CN/latest/getting_started/quick_start.html
最左边的部分整合了 PHE 算法,具体包括 Paillier、ElGamal 等等,HEU 深度改进并优化了这些算法的性能,性能通常比第三方库更高,因此,即便您不需要隐语,只把 HEU 单独拿出来当做 PHE lib 使用也是有收益的。
实测速度:
heu phe package
加密1000条 0.155 s
加法1000条 4.29ms
官方 phe
加密1000条 11.6 s
加法1000条 13.2 ms
ophe
加密1000条 0.09 s
加法1000条 8.2 ms
PYU是数据所有者的一方,从初始化的device中初始化一个PYU,PYU通过__call___方法会把明文转成 PYUObject,存在多个返回值会生成多个PYUObject