forked from data61/MP-SPDZ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol-tutorial.cpp
74 lines (61 loc) · 1.67 KB
/
protocol-tutorial.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* protocol-tutorial.cpp
*
* This file demonstrates the use of the low-level capabilities
* to build a protocol, namely Rep3 multiplication and opening.
*
*/
#include "Networking/CryptoPlayer.h"
#include "Math/Z2k.hpp"
int main(int argc, char** argv)
{
if (argc != 2)
{
cerr << "Usage: " << argv[0] << " <playerno>" << endl;
exit(1);
}
// set up networking on localhost
int my_number = atoi(argv[1]);
int port_base = 9999;
Names N(my_number, 3, "localhost", port_base);
CryptoPlayer P(N);
// correlated randomness for resharing
SeededPRNG G[2];
// synchronize with other parties
octetStream os;
os.append(G[0].get_seed(), SEED_SIZE);
P.pass_around(os, os, 1);
G[1].SetSeed(os.consume(SEED_SIZE));
// simplify code
typedef Z2<64> Z;
// start with same shares on all parties for simplicity
// replicated secret sharing of 3
Z a[2] = {1, 1};
// and 6
Z b[2] = {2, 2};
// compute an additive sharing of the product
Z cc = a[0] * (b[0] + b[1]) + a[1] * b[0];
// result shares
Z c[2];
// re-randomize
c[0] = cc + G[0].get<Z>() - G[1].get<Z>();
// send and receive share
os.reset_write_head();
c[0].pack(os);
P.pass_around(os, os, 1);
c[1].unpack(os);
// open value to party 0
if (P.my_num() == 1)
{
os.reset_write_head();
c[0].pack(os);
P.send_to(0, os);
}
// output result on party 0, which should be 18
if (P.my_num() == 0)
{
P.receive_player(1, os);
cout << "My shares: " << c[0] << ", " << c[1] << endl;
cout << "Result: " << (os.get<Z>() + c[0] + c[1]) << endl;
}
}