-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2pc_mult.cpp
More file actions
113 lines (88 loc) · 3.63 KB
/
2pc_mult.cpp
File metadata and controls
113 lines (88 loc) · 3.63 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <mpi.h>
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int Q = 10007; // abitrary large prime for modular arithmetic
// helper function to keep integers within modulo ring
int mod(int x) {
return ((x % Q) + Q) % Q;
}
int main(int argc, char** argv) {
MPI_Init(&argc, &argv);
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
srand(time(NULL) + rank);
if (rank == 0) {
// dealer
int x1 = rand() % Q;
int x2 = rand() % Q;
// secret share x1
int x1_0 = rand() % Q;
int x1_1 = mod(x1 - x1_0);
// secret share x2
int x2_0 = rand() % Q;
int x2_1 = mod(x2 - x2_0);
// beaver triple a, b, c = a * b
int a = rand() % Q;
int b = rand() % Q;
int c = mod(a * b);
int a0 = rand() % Q;
int a1 = mod(a - a0);
int b0 = rand() % Q;
int b1 = mod(b - b0);
int c0 = rand() % Q;
int c1 = mod(c - c0);
// send shares to Rank 1 (Alice)
MPI_Send(&x1_0, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
MPI_Send(&x2_0, 1, MPI_INT, 1, 1, MPI_COMM_WORLD);
MPI_Send(&a0, 1, MPI_INT, 1, 2, MPI_COMM_WORLD);
MPI_Send(&b0, 1, MPI_INT, 1, 3, MPI_COMM_WORLD);
MPI_Send(&c0, 1, MPI_INT, 1, 4, MPI_COMM_WORLD);
// send shares to Rank 2 (Bob)
MPI_Send(&x1_1, 1, MPI_INT, 2, 0, MPI_COMM_WORLD);
MPI_Send(&x2_1, 1, MPI_INT, 2, 1, MPI_COMM_WORLD);
MPI_Send(&a1, 1, MPI_INT, 2, 2, MPI_COMM_WORLD);
MPI_Send(&b1, 1, MPI_INT, 2, 3, MPI_COMM_WORLD);
MPI_Send(&c1, 1, MPI_INT, 2, 4, MPI_COMM_WORLD);
// receive result shares
int z0, z1;
MPI_Recv(&z0, 1, MPI_INT, 1, 20, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Recv(&z1, 1, MPI_INT, 2, 20, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
int result = mod(z0 + z1);
cout << "[Dealer] x1 = " << x1 << ", x2 = " << x2 << endl;
cout << "[Dealer] Ground truth x1 * x2 = " << mod(x1 * x2) << endl;
cout << "[Dealer] Securely computed result = " << result << endl;
}
if (rank == 1 || rank == 2) {
// party
int x1_share, x2_share, a_share, b_share, c_share;
MPI_Recv(&x1_share, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Recv(&x2_share, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Recv(&a_share, 1, MPI_INT, 0, 2, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Recv(&b_share, 1, MPI_INT, 0, 3, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Recv(&c_share, 1, MPI_INT, 0, 4, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
// local e and d
int epsilon = mod(x1_share - a_share);
int delta = mod(x2_share - b_share);
int epsilon_other, delta_other;
// exchange e and d with the other party
MPI_Sendrecv(&epsilon, 1, MPI_INT, 3 - rank, 10,
&epsilon_other, 1, MPI_INT, 3 - rank, 10,
MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Sendrecv(&delta, 1, MPI_INT, 3 - rank, 11,
&delta_other, 1, MPI_INT, 3 - rank, 11,
MPI_COMM_WORLD, MPI_STATUS_IGNORE);
int epsilon_sum = mod(epsilon + epsilon_other);
int delta_sum = mod(delta + delta_other);
// compute local result share
int result_share = mod(
c_share + epsilon_sum * b_share + delta_sum * a_share +
(rank == 1 ? epsilon_sum * delta_sum : 0) // only one party adds this term
);
// send result share back to dealer
MPI_Send(&result_share, 1, MPI_INT, 0, 20, MPI_COMM_WORLD);
}
MPI_Finalize();
return 0;
}