-
Notifications
You must be signed in to change notification settings - Fork 3
/
node.cpp
119 lines (93 loc) · 3.35 KB
/
node.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
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
114
115
116
117
118
119
#include <zmq.hpp>
#include <string>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <unistd.h>
using namespace std;
const string clientPrefix="tcp://localhost:";
const string serverPrefix="tcp://*:";
const int dataSize=9;
const string genericData="publicKey";
int procNumber, totalProcs;
int basePort=8000;
int broadcastPort=7000;
zmq::socket_t *reqSocket, *repSocket;
zmq::socket_t **broadSockets;
string s_recv (zmq::socket_t *socket) {
zmq::message_t request;
socket->recv(&request);
return string(static_cast<char*>(request.data()), request.size());
}
void s_send (zmq::socket_t *socket, string send) {
zmq::message_t request(send.length());
memcpy(request.data(), send.c_str(), send.length());
socket->send(request);
}
vector<int> getAddShares(int in)
{
string resp;
if(procNumber) {
cout<<"Received "<<s_recv(broadSockets[0])<<endl;
resp = s_recv(repSocket,genericData);
// multiply encrypted input by waht we just got
s_send(reqSocket, genericData);
resp = s_recv(reqSocket)
//select a random share
s_send(broadSockets[0],genericData); //send share back to p1
s_send(repSocket, genericData); //send something backwards
resp = s_recv(broadSockets[0]);
} else {
//send public key e to all parties
for(int i=0; i<(totalProcs-1); i++) {
s_send(broadSockets[i],genericData);
}
s_send(reqSocket, genericData); //send encrypted x1
for(int i=0; i<(totalProcs-1); i++) {
resp=s_recv(broadSockets[i]); // collect all the shares
}
resp = s_recv(reqSocket);
//now compute its own share. We know all shares now
for(int i=0; i<(totalProcs-1); i++) {
s_send(broadSockets[i],genericData); //send all shares
//resp=s_recv(broadSockets[i]); // receive acknowledgement
}
}
return vector<int>();
}
int main (int argc, char* argv[])
{
procNumber = atoi(argv[1]);
totalProcs =atoi(argv[2]);
if(argc>3) {
basePort=atoi(argv[3]);
broadcastPort=basePort+10;
}
cout<<"Starting "<<procNumber<<" "<<totalProcs<<endl;
zmq::context_t context (1);
reqSocket = new zmq::socket_t (context, ZMQ_REQ);
repSocket = new zmq::socket_t (context, ZMQ_REP);
//cout<<clientPrefix+to_string(basePort+procNumber+1)<<" "<<serverPrefix+to_string(basePort+procNumber)<<endl;
reqSocket->connect(clientPrefix+to_string(basePort+procNumber+1));
repSocket->bind(serverPrefix+to_string(basePort+procNumber));
if(procNumber) {
broadSockets = new zmq::socket_t*[1];
broadSockets[0] = new zmq::socket_t (context, ZMQ_REP);
broadSockets[0]->bind(serverPrefix+to_string(broadcastPort+procNumber));
} else {
broadSockets = new zmq::socket_t*[totalProcs-1];
for(int i=1;i<totalProcs;i++) {
broadSockets[i-1] = new zmq::socket_t (context, ZMQ_REQ);
broadSockets[i-1]->connect(clientPrefix+to_string(broadcastPort+i));
}
}
usleep(2000000);
getAddShares(0);
/*
reqSocket->close();
repSocket->unbind(serverPrefix+to_string(basePort+procNumber));
if(procNumber) broadSockets[0]->unbind(serverPrefix+to_string(broadcastPort+procNumber));
else for(int i=1;i<totalProcs;i++) broadSockets[i-1]->close();
*/
return 0;
}