networkServer.hpp 15 KB

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