安全多方计算之MP-SPDZ实例初探(一)

MP-SPDZ是一个安全多方计算的开源库。里面包含了各种技术实现的安全多方计算,例如混淆电路、秘密分享、OT、同态。功能比较全,而且是持续维护的开源库。主体语言是python,众所周知,python是世界最好的语言。在此基础上定义了很多新的关于MPC的类和库,具体可查看用户手册。

官方文档是:https://github.com/data61/MP-SPDZ  对spdz库进行了详细介绍,以及一些基础的安装编译命令

官方源代码:https://github.com/data61/MP-SPDZ/releases

官方用户手册:Welcome to MP-SPDZ’s documentation! — MP-SPDZ documentation    详细记录了spdz提供的各种类,函数,变量等。

大家可以参照这三个网址进行学习。

本篇博文想要实现一个简单的三方求和程序,来展示一个SPDZ的多方计算使用流程。

首先要安装所依赖的工具库

apt-get install automake build-essential git libboost-dev libboost-thread-dev libntl-dev libsodium-dev libssl-dev libtool m4 python3 texinfo yasm

这个是官方提供的命令,一次性安装几乎所有需要的工具库。所有的工具库在官方文档也有列出:

在下载好源代码后,切换到源代码文件夹。

下载好基础库后,对spdz库进行编译:

make -j 8 tldr

官方提供了一个测试程序,可以跑一跑试一试:这个代码内容就是各种运算的小测试。

./compile.py tutorial
echo 1 2 3 4 > Player-Data/Input-P0-0
echo 1 2 3 4 > Player-Data/Input-P1-0
Scripts/mascot.sh tutorial

实现简单的三方求和:

1.在Programs/Source文件夹内新建一个源代码文件testgp.mpc(后缀名是.mpc):我用的sublime text进行编辑,用vim也行

subl Programs/Source/testgp.mpc

编辑内容:

a = sint.get_input_from(0)
b = sint.get_input_from(1)
c = sint.get_input_from(2)
sum = a + b + c
print_ln('Results =%s',sum.reveal())

保存退出。

2. 编译程序:testgp是你的程序文件名

./compile.py -B 32 testgp

3. 生成证书文件,3代表三方计算。

Scripts/setup-ssl.sh 3

4. 写入三方数据

echo 11 > Player-Data/Input-P0-0
echo 12 > Player-Data/Input-P1-0
echo 13 > Player-Data/Input-P2-0

5.开三个新终端模拟三方计算,进行运算,得到结果:

./shamir-bmr-party.x -N 3 0 testgp
./shamir-bmr-party.x -N 3 1 testgp
./shamir-bmr-party.x -N 3 2 testgp

结束。

这个实例很简单,这个执行程序是基于秘密分享技术进行运算的,实际他还有别技术运算,如混淆电路,HE。对应的不同地执行过程。还在一步一步探索,后续也会更新相关内容。

欢迎大家找我讨论交流各种关于SPDZ开源库的问题。我明白的一定知无不言,我不会的,大家一起讨论讨论说不定就解决啦。

我的SPDZ专栏还有更多关于MP-SPDZ的文章,欢迎浏览。

------------------------------分割线---------------------------------------------------

MP-SDPZ版本0.3.3更新,加了不少内容,一大更新是兼容libOTe库,这也导致在安装的时候会出现很多新问题。如果是初次接触,安装时碰到关于libOTe的错误,可以尝试安装0.3.3之前的版本。有遇到libOTe的错误的,也可以评论区评论,也许有人会给出答案。

简单说下我安装0.3.3碰到的一个错误

/usr/bin/ld: cannot find -llibOTe
/usr/bin/ld: cannot find -lcryptoTools

缺少这两个静态库。这两个静态库在你的spdz目录下的local/lib/x86_64-linux-gnu/libcryptoTools.a和local/lib/x86_64-linux-gnu/liblibOTe.a。将这两个文件移到上一级目录下,也就是local/lib目录下,即可解决。

如果你的deps/libOTe文件夹是空的,可以尝试

make libote

来下载和编译libOTe库。 

还有一些错误复刻不出来了,欢迎加我微信交流:Alice-and-Bob。假期和周末期间可能回复不及时,见谅。现在建了一个微信交流群,感兴趣的同学加我微信拉你进群,备注 “进群” 即可。

SPDZ (Secure Multiparty Computation Protocol in the Semi-Honest Model with Zero-Knowledge) is a cryptographic protocol that allows multiple parties to perform secure computation on their private data without revealing it to each other. It ensures that the computation remains secure even if some of the parties are dishonest. In Python, you can implement SPDZ using libraries like PySyft, which is a Python library for secure multi-party computation and privacy-preserving machine learning. PySyft provides functionality for secure computation, including SPDZ, using techniques such as secret sharing and secure function evaluation. To use SPDZ in Python, you would need to install PySyft and its dependencies. Once installed, you can write Python code that uses PySyft's API to define the computation and execute it securely using the SPDZ protocol. Here's a simple example of using PySyft to perform secure addition using SPDZ: ```python import syft # Create two virtual workers representing the parties involved alice = syft.VirtualWorker(hook=syft.TorchHook(), id="alice") bob = syft.VirtualWorker(hook=syft.TorchHook(), id="bob") # Generate some secret data for each party alice_data = syft.FloatTensor([5]).fix_precision().share(alice, bob) bob_data = syft.FloatTensor([10]).fix_precision().share(alice, bob) # Perform secure addition using SPDZ protocol result = alice_data + bob_data # Retrieve the result result = result.get().float_precision() print(result) # Output: [15] ``` In this example, two virtual workers (representing Alice and Bob) are created using PySyft. Secret data is then generated for each party and shared between them using PySyft's fixed-precision encoding. The secure addition operation is performed using the SPDZ protocol, and the result is retrieved and printed. Note that this is just a basic example to illustrate the usage of PySyft and SPDZ in Python. The actual implementation and usage may vary depending on the specific use case and requirements.
评论 88
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值