networkServer.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. #ifndef __PRSONA_NETWORK_SERVER_HPP
  2. #define __PRSONA_NETWORK_SERVER_HPP
  3. #include <random>
  4. #include <string>
  5. #include <vector>
  6. #include <atomic>
  7. #include "server.hpp"
  8. #include "networking.hpp"
  9. /* "PUBLIC" FUNCTIONS */
  10. // CREATOR FOR A NEW SERVER
  11. PrsonaServer *create_server(
  12. std::default_random_engine& rng,
  13. std::string dealerIP,
  14. int dealerPort,
  15. bool bgnDealer,
  16. size_t numServers);
  17. // CHECK IN FUNCTION USED FOR SYNCHRONIZATION IN SETUP
  18. void check_in_with_dealer(
  19. std::string dealerIP,
  20. int dealerPort);
  21. // INITIATER FOR SHARED GLOBAL VALUES
  22. void initiate_generators(
  23. std::default_random_engine& rng,
  24. PrsonaServer* prsonaServer,
  25. const std::vector<std::string>& serverIPs,
  26. const std::vector<int>& serverPorts,
  27. const std::string& selfIP,
  28. int selfPort);
  29. // FUNCTION TO PERFORM OPERATIONS FOR EXPERIMENT
  30. void make_epoch(
  31. std::default_random_engine& rng,
  32. PrsonaServer *prsonaServer,
  33. const std::vector<std::string>& serverIPs,
  34. const std::vector<int>& serverPorts,
  35. const std::string& selfIP,
  36. int selfPort,
  37. std::mutex& updateMtx,
  38. std::atomic<size_t>& epochNum,
  39. const CivetServer& civetServer,
  40. std::mutex& buildUpOutputMtx,
  41. const std::string& buildUpOutputFilename,
  42. std::mutex& breakDownOutputMtx,
  43. const std::string& breakDownOutputFilename,
  44. std::mutex& fullOutputMtx,
  45. const std::string& fullOutputFilename);
  46. /* "PRIVATE" FUNCTIONS */
  47. // SHARED GLOBAL UPDATE LOCK GETTERS AND RELEASERS
  48. void obtain_update_locks(
  49. std::unique_lock<std::mutex> &updateLock,
  50. const std::vector<std::string>& serverIPs,
  51. const std::vector<int>& serverPorts,
  52. const std::string& selfIP,
  53. int selfPort,
  54. std::vector<size_t>& bandwidthData);
  55. void release_update_locks(
  56. std::unique_lock<std::mutex> &updateLock,
  57. const std::vector<std::string>& serverIPs,
  58. const std::vector<int>& serverPorts,
  59. const std::string& selfIP,
  60. int selfPort,
  61. std::vector<size_t>& bandwidthData);
  62. // GETTER FOR DEALER VALUE
  63. BGN get_bgn_private_key(
  64. std::default_random_engine& rng,
  65. std::string dealerIP,
  66. int dealerPort);
  67. // HELPERS TO INITIATE SHARED GLOBAL VALUES
  68. Twistpoint make_generator(
  69. std::default_random_engine& rng,
  70. PrsonaServer *prsonaServer,
  71. const std::vector<std::string>& serverIPs,
  72. const std::vector<int>& serverPorts,
  73. const std::string& selfIP,
  74. int selfPort,
  75. bool fresh,
  76. std::vector<Proof>& pi);
  77. void distribute_generator(
  78. PrsonaServer *prsonaServer,
  79. const std::vector<std::string>& serverIPs,
  80. const std::vector<int>& serverPorts,
  81. const std::string& selfIP,
  82. int selfPort,
  83. bool fresh,
  84. const std::vector<Proof>& pi,
  85. const Twistpoint& generator);
  86. // HELPERS FOR EPOCH CALCULATIONS
  87. std::vector<Proof> epoch_build_up(
  88. std::default_random_engine& rng,
  89. PrsonaServer *prsonaServer,
  90. const std::vector<std::string>& serverIPs,
  91. const std::vector<int>& serverPorts,
  92. const std::string& selfIP,
  93. int selfPort,
  94. Twistpoint& nextGenerator,
  95. const CivetServer& civetServer,
  96. std::mutex& outputMtx,
  97. const std::string& outputFilename,
  98. std::vector<size_t>& bandwidthData);
  99. void epoch_break_down(
  100. std::default_random_engine& rng,
  101. PrsonaServer *prsonaServer,
  102. const std::vector<std::string>& serverIPs,
  103. const std::vector<int>& serverPorts,
  104. const std::string& selfIP,
  105. int selfPort,
  106. const std::vector<Proof>& generatorProof,
  107. const Twistpoint& nextGenerator,
  108. const CivetServer& civetServer,
  109. std::mutex& outputMtx,
  110. const std::string& outputFilename,
  111. std::vector<size_t>& bandwidthData);
  112. // HELPERS FOR EPOCH HELPERS
  113. Twistpoint initiate_epoch_updates(
  114. std::default_random_engine& rng,
  115. const std::string& recipient,
  116. int recipientPort,
  117. const std::string& data,
  118. bool isBreakdown,
  119. std::vector<std::vector<Proof>>& generatorProofHolder,
  120. std::vector<size_t>& bandwidthData);
  121. struct mg_connection *distribute_epoch_updates(
  122. const std::string& recipient,
  123. int recipientPort,
  124. const std::string& data,
  125. struct synchronization_tool* sync);
  126. // SCORE TALLYING AND DISTRIBUTION HELPERS
  127. void tally_scores(
  128. PrsonaServer *prsonaServer,
  129. const std::vector<std::string>& serverIPs,
  130. const std::vector<int>& serverPorts,
  131. const std::string& selfIP,
  132. int selfPort,
  133. const Twistpoint& nextGenerator,
  134. std::vector<EGCiphertext>& userTallyScores,
  135. std::vector<CurveBipoint>& serverTallyScores,
  136. std::vector<size_t>& bandwidthData);
  137. void distribute_tallied_scores(
  138. PrsonaServer *prsonaServer,
  139. const std::vector<std::string>& serverIPs,
  140. const std::vector<int>& serverPorts,
  141. const std::string& selfIP,
  142. int selfPort,
  143. const Twistpoint& nextGenerator,
  144. const std::vector<EGCiphertext>& userTallyScores,
  145. const std::vector<CurveBipoint>& serverTallyScores,
  146. std::vector<size_t>& bandwidthData);
  147. // FILE I/O HELPERS
  148. BGN get_bgn_private_key_from_file(
  149. const char *filename);
  150. Twistpoint get_generator_from_file(
  151. const char *filename,
  152. Proof& pi);
  153. Twistpoint get_generator_from_file(
  154. const char *filename,
  155. std::vector<Proof>& pi);
  156. // EPOCH DATA SERIALIZERS/UN-SERIALIZERS
  157. std::string make_epoch_initiator_string(
  158. const std::vector<Proof>& generatorProof,
  159. const Twistpoint& nextGenerator);
  160. ssize_t read_epoch_initiator_string(
  161. const char *filename,
  162. std::vector<Proof>& generatorProof,
  163. Twistpoint& nextGenerator);
  164. std::string make_epoch_update_string(
  165. const std::vector<std::vector<Proof>>& pi,
  166. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  167. const std::vector<std::vector<Twistpoint>>& freshPseudonymCommits,
  168. const std::vector<std::vector<Twistpoint>>& freshPseudonymSeedCommits,
  169. const std::vector<std::vector<CurveBipoint>>& serverTallyCommits,
  170. const std::vector<std::vector<std::vector<TwistBipoint>>>& partwayVoteMatrixCommits,
  171. const std::vector<std::vector<std::vector<TwistBipoint>>>& finalVoteMatrixCommits,
  172. const std::vector<std::vector<Twistpoint>>& userTallyMaskCommits,
  173. const std::vector<std::vector<Twistpoint>>& userTallyMessageCommits,
  174. const std::vector<std::vector<Twistpoint>>& userTallySeedCommits,
  175. const Twistpoint& nextGenerator,
  176. bool doUserTallies);
  177. ssize_t read_epoch_update_string(
  178. const char *filename,
  179. std::vector<std::vector<Proof>>& pi,
  180. std::vector<std::vector<Twistpoint>>& permutationCommits,
  181. std::vector<std::vector<Twistpoint>>& freshPseudonymCommits,
  182. std::vector<std::vector<Twistpoint>>& freshPseudonymSeedCommits,
  183. std::vector<std::vector<CurveBipoint>>& serverTallyCommits,
  184. std::vector<std::vector<std::vector<TwistBipoint>>>& partwayVoteMatrixCommits,
  185. std::vector<std::vector<std::vector<TwistBipoint>>>& finalVoteMatrixCommits,
  186. std::vector<std::vector<Twistpoint>>& userTallyMaskCommits,
  187. std::vector<std::vector<Twistpoint>>& userTallyMessageCommits,
  188. std::vector<std::vector<Twistpoint>>& userTallySeedCommits,
  189. Twistpoint& nextGenerator,
  190. bool& doUserTallies);
  191. /* OTHER SERVER-RELEVANT HANDLERS */
  192. // Used to tell orchestrator when the system is ready to do an epoch change
  193. class EpochReadyHandler : public CivetHandler
  194. {
  195. public:
  196. EpochReadyHandler(
  197. struct synchronization_tool *exitSync,
  198. struct synchronization_tool *readySync,
  199. std::mutex& updateMtx,
  200. size_t numServers);
  201. bool handleGet(
  202. CivetServer *server,
  203. struct mg_connection *conn);
  204. private:
  205. struct synchronization_tool *exitSync, *readySync;
  206. std::mutex& updateMtx;
  207. const size_t numServers;
  208. };
  209. // Used to indicate which epoch the system is currently in
  210. class EpochNumHandler : public CivetHandler
  211. {
  212. public:
  213. EpochNumHandler(
  214. std::atomic<size_t>& epochNum);
  215. bool handleGet(
  216. CivetServer *server,
  217. struct mg_connection *conn);
  218. private:
  219. std::atomic<size_t>& epochNum;
  220. };
  221. // Used to take and release update locks
  222. class UpdateLockWebSocketHandler : public CivetWebSocketHandler
  223. {
  224. public:
  225. UpdateLockWebSocketHandler(
  226. std::mutex& updateMtx,
  227. std::unique_lock<std::mutex> **lockHolder,
  228. bool isLocking);
  229. ~UpdateLockWebSocketHandler();
  230. bool handleConnection(
  231. CivetServer *server,
  232. const struct mg_connection *conn);
  233. void handleReadyState(
  234. CivetServer *server,
  235. struct mg_connection *conn);
  236. bool handleData(
  237. CivetServer *server,
  238. struct mg_connection *conn,
  239. int bits,
  240. char *data,
  241. size_t data_len);
  242. void handleClose(
  243. CivetServer *server,
  244. const struct mg_connection *conn);
  245. private:
  246. std::mutex& updateMtx;
  247. std::unique_lock<std::mutex> **lockHolder;
  248. const bool isLocking;
  249. };
  250. /* SERVER-SPECIFIC HANDLER */
  251. class PrsonaServerWebSocketHandler : public CivetWebSocketHandler {
  252. public:
  253. // CONSTRUCTORS
  254. PrsonaServerWebSocketHandler(
  255. std::default_random_engine& rng,
  256. PrsonaServer *prsonaServer,
  257. const std::vector<std::string>& serverIPs,
  258. const std::vector<int>& serverPorts,
  259. const std::string& selfIP,
  260. int selfPort,
  261. std::mutex& updateMtx,
  262. std::atomic<size_t>& epochNum,
  263. std::mutex& buildUpOutputMtx,
  264. const std::string& buildUpOutputFilename,
  265. std::mutex& breakDownOutputMtx,
  266. const std::string& breakDownOutputFilename,
  267. std::mutex& updateOutputMtx,
  268. const std::string& updateOutputFilename,
  269. std::mutex& voteOutputMtx,
  270. const std::string& voteOutputFilename);
  271. // REQUIRED BY INHERITED CLASS
  272. bool handleConnection(
  273. CivetServer *server,
  274. const struct mg_connection *conn);
  275. void handleReadyState(
  276. CivetServer *server,
  277. struct mg_connection *conn);
  278. bool handleData(
  279. CivetServer *server,
  280. struct mg_connection *conn,
  281. int bits,
  282. char *data,
  283. size_t data_len);
  284. void handleClose(
  285. CivetServer *server,
  286. const struct mg_connection *conn);
  287. private:
  288. std::default_random_engine& rng;
  289. PrsonaServer *prsonaServer;
  290. const std::vector<std::string> serverIPs;
  291. const std::vector<int> serverPorts;
  292. const std::string selfIP;
  293. const int selfPort;
  294. std::mutex& updateMtx;
  295. std::atomic<size_t>& epochNum;
  296. std::mutex& buildUpOutputMtx;
  297. const std::string buildUpOutputFilename;
  298. std::mutex& breakDownOutputMtx;
  299. const std::string breakDownOutputFilename;
  300. std::mutex& updateOutputMtx;
  301. const std::string updateOutputFilename;
  302. std::mutex& voteOutputMtx;
  303. const std::string voteOutputFilename;
  304. struct synchronization_tool updateSynch, distributeSynch;
  305. // RESPONSE ROUTER FUNCTION
  306. void generate_response(
  307. CivetServer *server,
  308. struct mg_connection *conn,
  309. const char *filename);
  310. // BASIC PUBLIC SYSTEM INFO GETTERS
  311. void get_bgn_public_key(
  312. struct mg_connection *conn
  313. ) const;
  314. void get_num_clients(
  315. struct mg_connection *conn
  316. ) const;
  317. void get_num_servers(
  318. struct mg_connection *conn
  319. ) const;
  320. void get_generator(
  321. struct mg_connection *conn,
  322. bool fresh
  323. );
  324. // ENCRYPTED DATA GETTERS
  325. void get_current_votes_by(
  326. struct mg_connection *conn,
  327. const char *filename
  328. ) const;
  329. void get_all_current_votes(
  330. struct mg_connection *conn
  331. ) const;
  332. void get_current_user_encrypted_tally(
  333. struct mg_connection *conn,
  334. const char *filename
  335. ) const;
  336. void get_current_server_encrypted_tally(
  337. struct mg_connection *conn,
  338. const char *filename
  339. ) const;
  340. void get_current_pseudonyms(
  341. struct mg_connection *conn
  342. ) const;
  343. // PROOF COMMITMENT GETTERS
  344. void get_vote_row_commitment(
  345. struct mg_connection *conn,
  346. const char *filename
  347. ) const;
  348. void get_vote_matrix_commitment(
  349. struct mg_connection *conn
  350. ) const;
  351. void get_user_tally_commitment(
  352. struct mg_connection *conn,
  353. const char *filename
  354. ) const;
  355. void get_server_tally_commitment(
  356. struct mg_connection *conn,
  357. const char *filename
  358. ) const;
  359. void get_pseudonyms_commitment(
  360. struct mg_connection *conn
  361. ) const;
  362. // CLIENT INTERACTIONS
  363. void add_new_client(
  364. struct mg_connection *conn,
  365. const char *filename
  366. );
  367. void receive_vote(
  368. CivetServer *civetServer,
  369. struct mg_connection *conn,
  370. const char *filename
  371. );
  372. // DISTRIBUTION HELPERS FOR CLIENT INTERACTIONS
  373. void distribute_new_user_updates(
  374. const std::vector<Proof>& proofOfValidAddition,
  375. const std::vector<CurveBipoint>& previousVoteTallies,
  376. const std::vector<Twistpoint>& currentPseudonyms,
  377. const std::vector<EGCiphertext>& currentUserEncryptedTallies,
  378. const std::vector<std::vector<TwistBipoint>>& voteMatrix
  379. ) const;
  380. void distribute_new_vote(
  381. std::vector<Proof> pi,
  382. std::vector<TwistBipoint> newVotes,
  383. Twistpoint shortTermPublicKey,
  384. std::vector<size_t>& bandwidthData
  385. ) const;
  386. void import_new_user_update(
  387. struct mg_connection *conn,
  388. const char *filename
  389. );
  390. // CONSTRUCTOR HELPERS
  391. void get_bgn_details(
  392. struct mg_connection *conn
  393. ) const;
  394. void add_seed_to_generator(
  395. struct mg_connection *conn,
  396. const char *filename,
  397. bool fresh
  398. ) const;
  399. void set_generator(
  400. const char *filename,
  401. bool fresh
  402. );
  403. // EPOCH ROUNDS
  404. void build_up_midway_pseudonyms(
  405. CivetServer *civetServer,
  406. struct mg_connection *conn,
  407. const char *filename
  408. );
  409. void break_down_midway_pseudonyms(
  410. CivetServer *civetServer,
  411. struct mg_connection *conn,
  412. const char *filename
  413. );
  414. void accept_epoch_updates(
  415. CivetServer *civetServer,
  416. struct mg_connection *conn,
  417. const char *filename
  418. );
  419. // DISTRIBUTED BGN
  420. void get_partial_decryption(
  421. struct mg_connection *conn
  422. ) const;
  423. void receive_tallied_scores(
  424. struct mg_connection *conn,
  425. const char *filename
  426. );
  427. };
  428. #endif