networkServer.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. std::vector<Proof> hbc_epoch_build_up(
  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. Twistpoint& nextGenerator,
  111. const CivetServer& civetServer,
  112. std::mutex& outputMtx,
  113. const std::string& outputFilename,
  114. std::mutex& usageMtx,
  115. const std::string& usageFilename,
  116. std::vector<size_t>& overallBandwidthData);
  117. void epoch_break_down(
  118. std::default_random_engine& rng,
  119. PrsonaServer *prsonaServer,
  120. const std::vector<std::string>& serverIPs,
  121. const std::vector<int>& serverPorts,
  122. const std::string& selfIP,
  123. int selfPort,
  124. const std::vector<Proof>& generatorProof,
  125. const Twistpoint& nextGenerator,
  126. const CivetServer& civetServer,
  127. std::mutex& outputMtx,
  128. const std::string& outputFilename,
  129. std::mutex& usageMtx,
  130. const std::string& usageFilename,
  131. std::vector<size_t>& bandwidthData);
  132. void hbc_epoch_break_down(
  133. std::default_random_engine& rng,
  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 std::vector<Proof>& generatorProof,
  140. const Twistpoint& nextGenerator,
  141. const CivetServer& civetServer,
  142. std::mutex& outputMtx,
  143. const std::string& outputFilename,
  144. std::mutex& usageMtx,
  145. const std::string& usageFilename,
  146. std::vector<size_t>& bandwidthData);
  147. // HELPERS FOR EPOCH HELPERS
  148. Twistpoint initiate_epoch_updates(
  149. std::default_random_engine& rng,
  150. const std::string& recipient,
  151. int recipientPort,
  152. const std::string& data,
  153. bool isBreakdown,
  154. std::vector<std::vector<Proof>>& generatorProofHolder,
  155. std::vector<size_t>& bandwidthData);
  156. struct mg_connection *distribute_epoch_updates(
  157. const std::string& recipient,
  158. int recipientPort,
  159. const std::string& data,
  160. struct synchronization_tool* sync);
  161. // SCORE TALLYING AND DISTRIBUTION HELPERS
  162. void tally_scores(
  163. PrsonaServer *prsonaServer,
  164. const std::vector<std::string>& serverIPs,
  165. const std::vector<int>& serverPorts,
  166. const std::string& selfIP,
  167. int selfPort,
  168. const Twistpoint& nextGenerator,
  169. std::vector<EGCiphertext>& userTallyScores,
  170. std::vector<CurveBipoint>& serverTallyScores,
  171. std::vector<size_t>& bandwidthData);
  172. void distribute_tallied_scores(
  173. PrsonaServer *prsonaServer,
  174. const std::vector<std::string>& serverIPs,
  175. const std::vector<int>& serverPorts,
  176. const std::string& selfIP,
  177. int selfPort,
  178. const Twistpoint& nextGenerator,
  179. const std::vector<EGCiphertext>& userTallyScores,
  180. const std::vector<CurveBipoint>& serverTallyScores,
  181. std::vector<size_t>& bandwidthData);
  182. // FILE I/O HELPERS
  183. BGN get_bgn_private_key_from_file(
  184. const char *filename);
  185. Twistpoint get_generator_from_file(
  186. const char *filename,
  187. Proof& pi);
  188. Twistpoint get_generator_from_file(
  189. const char *filename,
  190. std::vector<Proof>& pi);
  191. // EPOCH DATA SERIALIZERS/UN-SERIALIZERS
  192. std::string make_epoch_initiator_string(
  193. const std::vector<Proof>& generatorProof,
  194. const Twistpoint& nextGenerator);
  195. ssize_t read_epoch_initiator_string(
  196. const char *filename,
  197. std::vector<Proof>& generatorProof,
  198. Twistpoint& nextGenerator);
  199. std::string make_epoch_update_string(
  200. const std::vector<std::vector<Proof>>& pi,
  201. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  202. const std::vector<std::vector<Twistpoint>>& freshPseudonymCommits,
  203. const std::vector<std::vector<Twistpoint>>& freshPseudonymSeedCommits,
  204. const std::vector<std::vector<CurveBipoint>>& serverTallyCommits,
  205. const std::vector<std::vector<std::vector<TwistBipoint>>>& partwayVoteMatrixCommits,
  206. const std::vector<std::vector<std::vector<TwistBipoint>>>& finalVoteMatrixCommits,
  207. const std::vector<std::vector<Twistpoint>>& userTallyMaskCommits,
  208. const std::vector<std::vector<Twistpoint>>& userTallyMessageCommits,
  209. const std::vector<std::vector<Twistpoint>>& userTallySeedCommits,
  210. const Twistpoint& nextGenerator,
  211. bool doUserTallies);
  212. std::string make_hbc_epoch_update_string(
  213. const std::vector<Proof>& generatorProof,
  214. const std::vector<Twistpoint>& newFreshPseudonyms,
  215. const std::vector<CurveBipoint>& newServerTallies,
  216. const std::vector<std::vector<TwistBipoint>>& newVoteMatrix,
  217. const std::vector<EGCiphertext>& newUserTallies,
  218. const Twistpoint& nextGenerator,
  219. bool doUserTallies);
  220. ssize_t read_epoch_update_string(
  221. const char *filename,
  222. std::vector<std::vector<Proof>>& pi,
  223. std::vector<std::vector<Twistpoint>>& permutationCommits,
  224. std::vector<std::vector<Twistpoint>>& freshPseudonymCommits,
  225. std::vector<std::vector<Twistpoint>>& freshPseudonymSeedCommits,
  226. std::vector<std::vector<CurveBipoint>>& serverTallyCommits,
  227. std::vector<std::vector<std::vector<TwistBipoint>>>& partwayVoteMatrixCommits,
  228. std::vector<std::vector<std::vector<TwistBipoint>>>& finalVoteMatrixCommits,
  229. std::vector<std::vector<Twistpoint>>& userTallyMaskCommits,
  230. std::vector<std::vector<Twistpoint>>& userTallyMessageCommits,
  231. std::vector<std::vector<Twistpoint>>& userTallySeedCommits,
  232. Twistpoint& nextGenerator,
  233. bool& doUserTallies);
  234. ssize_t read_hbc_epoch_update_string(
  235. const char *filename,
  236. std::vector<Proof>& generatorProof,
  237. std::vector<Twistpoint>& newFreshPseudonyms,
  238. std::vector<CurveBipoint>& newServerTallies,
  239. std::vector<std::vector<TwistBipoint>>& newVoteMatrix,
  240. std::vector<Twistpoint>& newUserTallies,
  241. Twistpoint& nextGenerator,
  242. bool& doUserTallies);
  243. /* OTHER SERVER-RELEVANT HANDLERS */
  244. // Used to tell orchestrator when the system is ready to do an epoch change
  245. class EpochReadyHandler : public CivetHandler
  246. {
  247. public:
  248. EpochReadyHandler(
  249. struct synchronization_tool *exitSync,
  250. struct synchronization_tool *readySync,
  251. std::mutex& updateMtx,
  252. size_t numServers);
  253. bool handleGet(
  254. CivetServer *server,
  255. struct mg_connection *conn);
  256. private:
  257. struct synchronization_tool *exitSync, *readySync;
  258. std::mutex& updateMtx;
  259. const size_t numServers;
  260. };
  261. // Used to indicate which epoch the system is currently in
  262. class EpochNumHandler : public CivetHandler
  263. {
  264. public:
  265. EpochNumHandler(
  266. std::atomic<size_t>& epochNum);
  267. bool handleGet(
  268. CivetServer *server,
  269. struct mg_connection *conn);
  270. private:
  271. std::atomic<size_t>& epochNum;
  272. };
  273. // Used to take and release update locks
  274. class UpdateLockWebSocketHandler : public CivetWebSocketHandler
  275. {
  276. public:
  277. UpdateLockWebSocketHandler(
  278. std::mutex& updateMtx,
  279. std::unique_lock<std::mutex> **lockHolder,
  280. bool isLocking);
  281. ~UpdateLockWebSocketHandler();
  282. bool handleConnection(
  283. CivetServer *server,
  284. const struct mg_connection *conn);
  285. void handleReadyState(
  286. CivetServer *server,
  287. struct mg_connection *conn);
  288. bool handleData(
  289. CivetServer *server,
  290. struct mg_connection *conn,
  291. int bits,
  292. char *data,
  293. size_t data_len);
  294. void handleClose(
  295. CivetServer *server,
  296. const struct mg_connection *conn);
  297. private:
  298. std::mutex& updateMtx;
  299. std::unique_lock<std::mutex> **lockHolder;
  300. const bool isLocking;
  301. };
  302. /* SERVER-SPECIFIC HANDLER */
  303. class PrsonaServerWebSocketHandler : public CivetWebSocketHandler {
  304. public:
  305. // CONSTRUCTORS
  306. PrsonaServerWebSocketHandler(
  307. std::default_random_engine& rng,
  308. PrsonaServer *prsonaServer,
  309. const std::vector<std::string>& serverIPs,
  310. const std::vector<int>& serverPorts,
  311. const std::string& selfIP,
  312. int selfPort,
  313. std::mutex& updateMtx,
  314. std::atomic<size_t>& epochNum,
  315. std::mutex& buildUpOutputMtx,
  316. const std::string& buildUpOutputFilename,
  317. std::mutex& breakDownOutputMtx,
  318. const std::string& breakDownOutputFilename,
  319. std::mutex& updateOutputMtx,
  320. const std::string& updateOutputFilename,
  321. std::mutex& voteOutputMtx,
  322. const std::string& voteOutputFilename,
  323. std::mutex& usageMtx,
  324. const std::string& usageFilename);
  325. // REQUIRED BY INHERITED CLASS
  326. bool handleConnection(
  327. CivetServer *server,
  328. const struct mg_connection *conn);
  329. void handleReadyState(
  330. CivetServer *server,
  331. struct mg_connection *conn);
  332. bool handleData(
  333. CivetServer *server,
  334. struct mg_connection *conn,
  335. int bits,
  336. char *data,
  337. size_t data_len);
  338. void handleClose(
  339. CivetServer *server,
  340. const struct mg_connection *conn);
  341. private:
  342. std::default_random_engine& rng;
  343. PrsonaServer *prsonaServer;
  344. const std::vector<std::string> serverIPs;
  345. const std::vector<int> serverPorts;
  346. const std::string selfIP;
  347. const int selfPort;
  348. std::mutex& updateMtx;
  349. std::atomic<size_t>& epochNum;
  350. std::mutex& buildUpOutputMtx;
  351. const std::string buildUpOutputFilename;
  352. std::mutex& breakDownOutputMtx;
  353. const std::string breakDownOutputFilename;
  354. std::mutex& updateOutputMtx;
  355. const std::string updateOutputFilename;
  356. std::mutex& voteOutputMtx;
  357. const std::string voteOutputFilename;
  358. std::mutex& usageMtx;
  359. const std::string usageFilename;
  360. struct synchronization_tool updateSynch, distributeSynch;
  361. // RESPONSE ROUTER FUNCTION
  362. void generate_response(
  363. CivetServer *server,
  364. struct mg_connection *conn,
  365. const char *filename);
  366. // BASIC PUBLIC SYSTEM INFO GETTERS
  367. void get_bgn_public_key(
  368. struct mg_connection *conn
  369. ) const;
  370. void get_num_clients(
  371. struct mg_connection *conn
  372. ) const;
  373. void get_num_servers(
  374. struct mg_connection *conn
  375. ) const;
  376. void get_generator(
  377. struct mg_connection *conn,
  378. bool fresh
  379. );
  380. // ENCRYPTED DATA GETTERS
  381. void get_current_votes_by(
  382. struct mg_connection *conn,
  383. const char *filename
  384. ) const;
  385. void get_all_current_votes(
  386. struct mg_connection *conn
  387. ) const;
  388. void get_current_user_encrypted_tally(
  389. struct mg_connection *conn,
  390. const char *filename
  391. ) const;
  392. void get_current_server_encrypted_tally(
  393. struct mg_connection *conn,
  394. const char *filename
  395. ) const;
  396. void get_current_pseudonyms(
  397. struct mg_connection *conn
  398. ) const;
  399. // PROOF COMMITMENT GETTERS
  400. void get_vote_row_commitment(
  401. struct mg_connection *conn,
  402. const char *filename
  403. ) const;
  404. void get_vote_matrix_commitment(
  405. struct mg_connection *conn
  406. ) const;
  407. void get_user_tally_commitment(
  408. struct mg_connection *conn,
  409. const char *filename
  410. ) const;
  411. void get_server_tally_commitment(
  412. struct mg_connection *conn,
  413. const char *filename
  414. ) const;
  415. void get_pseudonyms_commitment(
  416. struct mg_connection *conn
  417. ) const;
  418. // CLIENT INTERACTIONS
  419. void add_new_client(
  420. struct mg_connection *conn,
  421. const char *filename
  422. );
  423. void receive_vote(
  424. CivetServer *civetServer,
  425. struct mg_connection *conn,
  426. const char *filename
  427. );
  428. // DISTRIBUTION HELPERS FOR CLIENT INTERACTIONS
  429. void distribute_new_user_updates(
  430. const std::vector<Proof>& proofOfValidAddition,
  431. const std::vector<CurveBipoint>& previousVoteTallies,
  432. const std::vector<Twistpoint>& currentPseudonyms,
  433. const std::vector<EGCiphertext>& currentUserEncryptedTallies,
  434. const std::vector<std::vector<TwistBipoint>>& voteMatrix
  435. ) const;
  436. void distribute_new_vote(
  437. std::vector<Proof> pi,
  438. std::vector<TwistBipoint> newVotes,
  439. Twistpoint shortTermPublicKey,
  440. std::vector<size_t>& bandwidthData
  441. ) const;
  442. void import_new_user_update(
  443. struct mg_connection *conn,
  444. const char *filename
  445. );
  446. // CONSTRUCTOR HELPERS
  447. void get_bgn_details(
  448. struct mg_connection *conn
  449. ) const;
  450. void add_seed_to_generator(
  451. struct mg_connection *conn,
  452. const char *filename,
  453. bool fresh
  454. ) const;
  455. void set_generator(
  456. const char *filename,
  457. bool fresh
  458. );
  459. // EPOCH ROUNDS
  460. void build_up_midway_pseudonyms(
  461. CivetServer *civetServer,
  462. struct mg_connection *conn,
  463. const char *filename
  464. );
  465. void hbc_build_up_midway_pseudonyms(
  466. CivetServer *civetServer,
  467. struct mg_connection *conn,
  468. const char *filename
  469. );
  470. void break_down_midway_pseudonyms(
  471. CivetServer *civetServer,
  472. struct mg_connection *conn,
  473. const char *filename
  474. );
  475. void hbc_break_down_midway_pseudonyms(
  476. CivetServer *civetServer,
  477. struct mg_connection *conn,
  478. const char *filename
  479. );
  480. void accept_epoch_updates(
  481. CivetServer *civetServer,
  482. struct mg_connection *conn,
  483. const char *filename
  484. );
  485. void hbc_accept_epoch_updates(
  486. CivetServer *civetServer,
  487. struct mg_connection *conn,
  488. const char *filename
  489. );
  490. // DISTRIBUTED BGN
  491. void get_partial_decryption(
  492. struct mg_connection *conn
  493. ) const;
  494. void receive_tallied_scores(
  495. struct mg_connection *conn,
  496. const char *filename
  497. );
  498. };
  499. #endif