networkServer.cpp 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <ctime>
  5. #include <chrono>
  6. #include "networkServer.hpp"
  7. /********************************************************
  8. ********* *********
  9. ********* server networking public functions *********
  10. ********* *********
  11. ********************************************************/
  12. /*
  13. * CREATOR FOR A NEW SERVER
  14. */
  15. PrsonaServer *create_server(
  16. std::default_random_engine& rng,
  17. std::string dealerIP,
  18. int dealerPort,
  19. bool bgnDealer,
  20. size_t numServers)
  21. {
  22. // We simulate the distributed BGN key generation; in our case, the dealer makes the BGN individually and shares it with other servers
  23. if (bgnDealer)
  24. return new PrsonaServer(numServers);
  25. // If we're not the dealer, get the BGN private key from the dealer
  26. BGN privateKey = get_bgn_private_key(rng, dealerIP, dealerPort);
  27. // And make a server object accounting for that
  28. return new PrsonaServer(numServers, privateKey);
  29. }
  30. /*
  31. * CHECK IN FUNCTION USED FOR SYNCHRONIZATION IN SETUP
  32. */
  33. void check_in_with_dealer(
  34. std::string dealerIP,
  35. int dealerPort)
  36. {
  37. std::stringstream buffer;
  38. std::string data;
  39. // The actual check in process is very simple; just make the correct GET request
  40. buffer << "GET " << SERVER_CHECK_IN_URI << " HTTP/1.1\r\n";
  41. buffer << "Host: " << dealerIP << ":" << dealerPort << "\r\n\r\n";
  42. data = buffer.str();
  43. // This also means things are slightly less cumbersome than making a websocket connection would be
  44. struct mg_connection *conn = mg_connect_client(dealerIP.c_str(), dealerPort, USE_SSL, NULL, 0);
  45. // Make the actual GET request
  46. mg_write(conn, data.c_str(), data.length());
  47. // We don't really care about the response, so we can just ignore it
  48. mg_close_connection(conn);
  49. }
  50. /*
  51. * INITIATER FOR SHARED GLOBAL VALUES
  52. */
  53. void initiate_generators(
  54. std::default_random_engine& rng,
  55. PrsonaServer* prsonaServer,
  56. const std::vector<std::string>& serverIPs,
  57. const std::vector<int>& serverPorts,
  58. const std::string& selfIP,
  59. int selfPort)
  60. {
  61. // Form and distribute the first fresh generator
  62. std::vector<Proof> pi;
  63. Twistpoint freshGenerator = make_generator(rng, prsonaServer, serverIPs, serverPorts, selfIP, selfPort, true, pi);
  64. distribute_generator(prsonaServer, serverIPs, serverPorts, selfIP, selfPort, true, pi, freshGenerator);
  65. // Form and distribute the H used in ElGamal operations
  66. Twistpoint blindGenerator = make_generator(rng, prsonaServer, serverIPs, serverPorts, selfIP, selfPort, false, pi);
  67. distribute_generator(prsonaServer, serverIPs, serverPorts, selfIP, selfPort, false, pi, blindGenerator);
  68. }
  69. /*
  70. * FUNCTION TO PERFORM OPERATIONS FOR EXPERIMENT
  71. */
  72. void make_epoch(
  73. std::default_random_engine& rng,
  74. PrsonaServer *prsonaServer,
  75. const std::vector<std::string>& serverIPs,
  76. const std::vector<int>& serverPorts,
  77. const std::string& selfIP,
  78. int selfPort,
  79. std::mutex& updateMtx,
  80. std::atomic<size_t>& epochNum,
  81. const CivetServer& civetServer,
  82. std::mutex& buildUpOutputMtx,
  83. const std::string& buildUpOutputFilename,
  84. std::mutex& breakDownOutputMtx,
  85. const std::string& breakDownOutputFilename,
  86. std::mutex& fullOutputMtx,
  87. const std::string& fullOutputFilename,
  88. std::mutex& usageMtx,
  89. const std::string& usageFilename)
  90. {
  91. // As before, the fresh generator always starts from the same G
  92. Twistpoint nextGenerator = PrsonaServer::EL_GAMAL_GENERATOR;
  93. std::vector<size_t> bandwidthData(2);
  94. std::unique_lock<std::mutex> updateLock(updateMtx, std::defer_lock);
  95. std::vector<size_t> bandwidthDataBefore = get_server_log_data(civetServer.getContext());
  96. std::chrono::high_resolution_clock::time_point wallTimeBefore = std::chrono::high_resolution_clock::now();
  97. clock_t cpuTimeBefore = clock();
  98. // Take update locks on every machine
  99. obtain_update_locks(updateLock, serverIPs, serverPorts, selfIP, selfPort, bandwidthData);
  100. // Do the first half of the epoch calculations (building up the intermediary values)
  101. std::vector<Proof> generatorProof = epoch_build_up(rng, prsonaServer, serverIPs, serverPorts, selfIP, selfPort, nextGenerator, civetServer, buildUpOutputMtx, buildUpOutputFilename, usageMtx, usageFilename, bandwidthData);
  102. // Tally up the current scores at the end of the epoch for the users
  103. std::vector<EGCiphertext> currentUserEncryptedTallies;
  104. std::vector<CurveBipoint> currentServerEncryptedTallies;
  105. tally_scores(prsonaServer, serverIPs, serverPorts, selfIP, selfPort, nextGenerator, currentUserEncryptedTallies, currentServerEncryptedTallies, bandwidthData);
  106. // And distribute these to each server
  107. distribute_tallied_scores(prsonaServer, serverIPs, serverPorts, selfIP, selfPort, nextGenerator, currentUserEncryptedTallies, currentServerEncryptedTallies, bandwidthData);
  108. // Do the second half of the epoch calculations (breaking down values to their final values, to be given to users)
  109. epoch_break_down(rng, prsonaServer, serverIPs, serverPorts, selfIP, selfPort, generatorProof, nextGenerator, civetServer, breakDownOutputMtx, breakDownOutputFilename, usageMtx, usageFilename, bandwidthData);
  110. // Indicate we are in a new epoch
  111. epochNum.fetch_add(1);
  112. // Release the update locks from every machine
  113. release_update_locks(updateLock, serverIPs, serverPorts, selfIP, selfPort, bandwidthData);
  114. clock_t cpuTimeAfter = clock();
  115. std::chrono::high_resolution_clock::time_point wallTimeAfter = std::chrono::high_resolution_clock::now();
  116. std::vector<size_t> bandwidthDataAfter = get_server_log_data(civetServer.getContext());
  117. std::vector<double> timingData(2);
  118. timingData[0] = std::chrono::duration_cast<std::chrono::duration<double>>(wallTimeAfter - wallTimeBefore).count();
  119. timingData[1] = ((double)(cpuTimeAfter - cpuTimeBefore)) / CLOCKS_PER_SEC;
  120. bandwidthData[0] += bandwidthDataAfter[0] - bandwidthDataBefore[0];
  121. bandwidthData[1] += bandwidthDataAfter[1] - bandwidthDataBefore[1];
  122. write_log_data(fullOutputMtx, fullOutputFilename, timingData, bandwidthData);
  123. write_usage_data(usageMtx, usageFilename);
  124. }
  125. /*********************************************************
  126. ********* *********
  127. ********* server networking private functions *********
  128. ********* *********
  129. *********************************************************/
  130. /*
  131. * SHARED GLOBAL UPDATE LOCK GETTERS AND RELEASERS
  132. */
  133. void obtain_update_locks(
  134. std::unique_lock<std::mutex> &updateLock,
  135. const std::vector<std::string>& serverIPs,
  136. const std::vector<int>& serverPorts,
  137. const std::string& selfIP,
  138. int selfPort,
  139. std::vector<size_t>& bandwidthData)
  140. {
  141. // Get locks on each machine (in a predetermined order, defined universally for all servers)
  142. size_t i = 0;
  143. while (i < serverIPs.size())
  144. {
  145. // When it's our turn, it's easy to take the lock
  146. if (serverIPs[i] == selfIP && serverPorts[i] == selfPort)
  147. {
  148. updateLock.lock();
  149. i++;
  150. continue;
  151. }
  152. struct synchronization_tool sync;
  153. struct mg_connection *conn = NULL;
  154. // Connect to the server
  155. std::unique_lock<std::mutex> lck(sync.mtx);
  156. sync.val = 0;
  157. sync.val2 = 0;
  158. while (!conn)
  159. {
  160. conn = mg_connect_websocket_client(serverIPs[i].c_str(), serverPorts[i], USE_SSL, NULL, 0, UPDATE_LOCK_URI, "null", synchro_websocket_data_handler, synchro_websocket_close_handler, &sync);
  161. if (!conn)
  162. std::cerr << "Couldn't connect to server " << i << " to obtain its lock" << std::endl;
  163. }
  164. std::vector<size_t> bandwidthDataBefore = get_conn_log_data(mg_get_context(conn), true);
  165. // Ask for its lock
  166. mg_websocket_client_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
  167. // Wait for its response (or the connection to die)
  168. while (!sync.val2)
  169. sync.cv.wait(lck);
  170. std::vector<size_t> bandwidthDataAfter = get_conn_log_data(mg_get_context(conn), true);
  171. bandwidthData[0] += bandwidthDataAfter[0] - bandwidthDataBefore[0];
  172. bandwidthData[1] += bandwidthDataAfter[1] - bandwidthDataBefore[1];
  173. // Close connection
  174. mg_close_connection(conn);
  175. // Only move forward once we've confirmed we have the lock (or else we risk deadlock!)
  176. if (sync.val)
  177. i++;
  178. }
  179. }
  180. void release_update_locks(
  181. std::unique_lock<std::mutex> &updateLock,
  182. const std::vector<std::string>& serverIPs,
  183. const std::vector<int>& serverPorts,
  184. const std::string& selfIP,
  185. int selfPort,
  186. std::vector<size_t>& bandwidthData)
  187. {
  188. // Release locks on each machine (in the opposite of the predetermined order we used to take them)
  189. ssize_t i = serverIPs.size() - 1;
  190. while (i >= 0)
  191. {
  192. // When it's our turn, it's easy to release the lock
  193. if (serverIPs[i] == selfIP && serverPorts[i] == selfPort)
  194. {
  195. updateLock.unlock();
  196. i--;
  197. continue;
  198. }
  199. struct synchronization_tool sync;
  200. struct mg_connection *conn = NULL;
  201. // Connect to the server
  202. std::unique_lock<std::mutex> lck(sync.mtx);
  203. sync.val = 0;
  204. sync.val2 = 0;
  205. while (!conn)
  206. {
  207. conn = mg_connect_websocket_client(serverIPs[i].c_str(), serverPorts[i], USE_SSL, NULL, 0, UPDATE_UNLOCK_URI, "null", synchro_websocket_data_handler, synchro_websocket_close_handler, &sync);
  208. if (!conn)
  209. std::cerr << "Couldn't connect to server " << i << " to release its lock" << std::endl;
  210. }
  211. std::vector<size_t> bandwidthDataBefore = get_conn_log_data(mg_get_context(conn), true);
  212. // Return its lock
  213. mg_websocket_client_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
  214. // Wait for its response (or the connection to die)
  215. while (!sync.val2)
  216. sync.cv.wait(lck);
  217. std::vector<size_t> bandwidthDataAfter = get_conn_log_data(mg_get_context(conn), true);
  218. bandwidthData[0] += bandwidthDataAfter[0] - bandwidthDataBefore[0];
  219. bandwidthData[1] += bandwidthDataAfter[1] - bandwidthDataBefore[1];
  220. // Close connection
  221. mg_close_connection(conn);
  222. // Only move forward once we've confirmed we released the lock (or else we risk deadlock!)
  223. if (sync.val)
  224. i--;
  225. }
  226. }
  227. /*
  228. * GETTER FOR DEALER VALUE
  229. */
  230. BGN get_bgn_private_key(
  231. std::default_random_engine& rng,
  232. std::string dealerIP,
  233. int dealerPort)
  234. {
  235. struct synchronization_tool sync;
  236. char *filename;
  237. struct mg_connection *conn = NULL;
  238. // Set up connection to the dealer
  239. std::unique_lock<std::mutex> lck(sync.mtx);
  240. sync.val = 0;
  241. while (!conn)
  242. {
  243. conn = mg_connect_websocket_client(dealerIP.c_str(), dealerPort, USE_SSL, NULL, 0, REQUEST_BGN_PRIVKEY_URI, "null", file_websocket_data_handler, file_websocket_close_handler, &sync);
  244. if (!conn)
  245. std::cerr << "Couldn't connect to dealer to obtain BGN details." << std::endl;
  246. }
  247. // Establish a file to receive BGN data at
  248. filename = set_temp_filename(rng, conn);
  249. // Tell dealer to go ahead with data
  250. mg_websocket_client_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
  251. // Wait for data
  252. while (!sync.val)
  253. sync.cv.wait(lck);
  254. // Close connection
  255. mg_close_connection(conn);
  256. // Un-serialize BGN private key
  257. BGN retval = get_bgn_private_key_from_file(filename);
  258. remove(filename);
  259. delete [] filename;
  260. return retval;
  261. }
  262. /*
  263. * HELPERS TO INITIATE SHARED GLOBAL VALUES
  264. */
  265. Twistpoint make_generator(
  266. std::default_random_engine& rng,
  267. PrsonaServer *prsonaServer,
  268. const std::vector<std::string>& serverIPs,
  269. const std::vector<int>& serverPorts,
  270. const std::string& selfIP,
  271. int selfPort,
  272. bool fresh,
  273. std::vector<Proof>& pi)
  274. {
  275. // Either way, we always start from the same, default G
  276. Twistpoint retval = PrsonaServer::EL_GAMAL_GENERATOR;
  277. pi.clear();
  278. // Make sure we instruct the servers to contribute correctly (in practice, there is basically no difference between these)
  279. const char* which = (fresh ? REQUEST_ADD_CURR_SEED_FOR_FRESH_GENERATOR_URI : REQUEST_ADD_RAND_SEED_FOR_EG_BLIND_GENERATOR_URI);
  280. // Ask each server for its contribution
  281. for (size_t i = 0; i < serverIPs.size(); i++)
  282. {
  283. // Add our own contribution
  284. if (serverIPs[i] == selfIP && serverPorts[i] == selfPort)
  285. {
  286. if (fresh)
  287. retval = prsonaServer->add_curr_seed_to_generator(pi, retval);
  288. else
  289. retval = prsonaServer->add_rand_seed_to_generator(pi, retval);
  290. continue;
  291. }
  292. struct synchronization_tool sync;
  293. char *filename;
  294. struct mg_connection *conn = NULL;
  295. // Serialize current state of generator
  296. std::stringstream buffer;
  297. std::string data;
  298. buffer << retval;
  299. data = buffer.str();
  300. // Set up connection to current server
  301. std::unique_lock<std::mutex> lck(sync.mtx);
  302. sync.val = 0;
  303. while (!conn)
  304. {
  305. conn = mg_connect_websocket_client(serverIPs[i].c_str(), serverPorts[i], USE_SSL, NULL, 0, which, "null", file_websocket_data_handler, file_websocket_close_handler, &sync);
  306. if (!conn)
  307. std::cerr << "Couldn't get server " << i << "'s update on generator" << std::endl;
  308. }
  309. // Establish a file to receive the new generator at
  310. filename = set_temp_filename(rng, conn);
  311. // Send the current state of the generator
  312. mg_websocket_client_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
  313. mg_websocket_client_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
  314. // Wait for response
  315. while (!sync.val)
  316. sync.cv.wait(lck);
  317. // Close connection
  318. mg_close_connection(conn);
  319. // Un-serialize the new state of the generator
  320. Proof currProof;
  321. retval = get_generator_from_file(filename, currProof);
  322. pi.push_back(currProof);
  323. remove(filename);
  324. delete [] filename;
  325. }
  326. return retval;
  327. }
  328. void distribute_generator(
  329. PrsonaServer *prsonaServer,
  330. const std::vector<std::string>& serverIPs,
  331. const std::vector<int>& serverPorts,
  332. const std::string& selfIP,
  333. int selfPort,
  334. bool fresh,
  335. const std::vector<Proof>& pi,
  336. const Twistpoint& generator)
  337. {
  338. // Serialize the final generator, and its proof of correctness
  339. std::stringstream buffer;
  340. std::string data;
  341. BinarySizeT sizeOfVector(pi.size());
  342. buffer << sizeOfVector;
  343. for (size_t i = 0; i < sizeOfVector.val(); i++)
  344. buffer << pi[i];
  345. buffer << generator;
  346. data = buffer.str();
  347. // Make sure we tell the servers which generator we're giving them correctly
  348. const char* which = (fresh ? SUBMIT_FRESH_GENERATOR_URI : SUBMIT_EG_BLIND_GENERATOR_URI);
  349. // Distribute the generator to each server
  350. for (size_t i = 0; i < serverIPs.size(); i++)
  351. {
  352. // Load the generator into our own server object
  353. if (serverIPs[i] == selfIP && serverPorts[i] == selfPort)
  354. {
  355. if (fresh)
  356. prsonaServer->initialize_fresh_generator(pi, generator);
  357. else
  358. prsonaServer->set_EG_blind_generator(pi, generator);
  359. continue;
  360. }
  361. struct mg_connection *conn = NULL;
  362. // Connect to the server
  363. while (!conn)
  364. {
  365. conn = mg_connect_websocket_client(serverIPs[i].c_str(), serverPorts[i], USE_SSL, NULL, 0, which, "null", empty_websocket_data_handler, empty_websocket_close_handler, NULL);
  366. if (!conn)
  367. std::cerr << "Couldn't connect to server " << i << " to give them the " << (fresh ? "fresh" : "blind") << " generator." << std::endl;
  368. }
  369. // Send the generator to the server
  370. mg_websocket_client_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
  371. mg_websocket_client_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
  372. // Don't bother waiting for a response; we can just move on to the next
  373. mg_close_connection(conn);
  374. }
  375. }
  376. /*
  377. * HELPERS FOR EPOCH CALCULATIONS
  378. */
  379. std::vector<Proof> epoch_build_up(
  380. std::default_random_engine& rng,
  381. PrsonaServer *prsonaServer,
  382. const std::vector<std::string>& serverIPs,
  383. const std::vector<int>& serverPorts,
  384. const std::string& selfIP,
  385. int selfPort,
  386. Twistpoint& nextGenerator,
  387. const CivetServer& civetServer,
  388. std::mutex& outputMtx,
  389. const std::string& outputFilename,
  390. std::mutex& usageMtx,
  391. const std::string& usageFilename,
  392. std::vector<size_t>& overallBandwidthData)
  393. {
  394. std::vector<std::vector<std::vector<Proof>>> pi;
  395. std::vector<std::vector<std::vector<Twistpoint>>> permutationCommits;
  396. std::vector<std::vector<std::vector<Twistpoint>>> freshPseudonymCommits;
  397. std::vector<std::vector<std::vector<Twistpoint>>> freshPseudonymSeedCommits;
  398. std::vector<std::vector<std::vector<CurveBipoint>>> serverTallyCommits;
  399. std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>> partwayVoteMatrixCommits;
  400. std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>> finalVoteMatrixCommits;
  401. std::vector<std::vector<Proof>> generatorProofHolder(1);
  402. // Go through each server to perform the epoch calculation at hand
  403. for (size_t i = 0; i < serverIPs.size(); i++)
  404. {
  405. // When it's our turn, do things as normal
  406. if (serverIPs[i] == selfIP && serverPorts[i] == selfPort)
  407. {
  408. pi.clear();
  409. pi.push_back(generatorProofHolder);
  410. permutationCommits.clear();
  411. freshPseudonymCommits.clear();
  412. freshPseudonymSeedCommits.clear();
  413. serverTallyCommits.clear();
  414. partwayVoteMatrixCommits.clear();
  415. finalVoteMatrixCommits.clear();
  416. std::vector<size_t> bandwidthData(2);
  417. std::vector<std::vector<size_t>> otherBandwidthDataBefore;
  418. std::vector<size_t> serverBandwidthDataBefore = get_server_log_data(civetServer.getContext());
  419. std::chrono::high_resolution_clock::time_point wallTimeBefore = std::chrono::high_resolution_clock::now();
  420. clock_t cpuTimeBefore = clock();
  421. // Perform the actual calculation
  422. prsonaServer->build_up_midway_pseudonyms(pi, permutationCommits, freshPseudonymCommits, freshPseudonymSeedCommits, serverTallyCommits, partwayVoteMatrixCommits, finalVoteMatrixCommits, nextGenerator);
  423. std::vector<std::vector<Twistpoint>> currUserTallyMaskCommits;
  424. std::vector<std::vector<Twistpoint>> currUserTallyMessageCommits;
  425. std::vector<std::vector<Twistpoint>> currUserTallySeedCommits;
  426. // Serialize the relevant data
  427. std::string data = make_epoch_update_string(pi[1], permutationCommits[0], freshPseudonymCommits[0], freshPseudonymSeedCommits[0], serverTallyCommits[0], partwayVoteMatrixCommits[0], finalVoteMatrixCommits[0], currUserTallyMaskCommits, currUserTallyMessageCommits, currUserTallySeedCommits, nextGenerator, false);
  428. struct synchronization_tool sync;
  429. std::vector<struct mg_connection *> conns;
  430. // Distribute the data to each server (in parallel, roughly)
  431. std::unique_lock<std::mutex> lck(sync.mtx);
  432. sync.val = 1;
  433. for (size_t j = 0; j < serverIPs.size(); j++)
  434. {
  435. // But, obviously, don't send it back to ourselves
  436. if (i == j)
  437. continue;
  438. // Send that data
  439. struct mg_connection *currConn = distribute_epoch_updates(serverIPs[j], serverPorts[j], data, &sync);
  440. otherBandwidthDataBefore.push_back(get_conn_log_data(mg_get_context(currConn), false));
  441. // But keep track of that connection, as we can't close it until we know the server's gotten its data
  442. conns.push_back(currConn);
  443. }
  444. // Wait for the other servers to all report back that they have received the update
  445. while (sync.val < serverIPs.size())
  446. sync.cv.wait(lck);
  447. for (size_t j = 0; j < conns.size(); j++)
  448. {
  449. std::vector<size_t> currBandwidthDataAfter = get_conn_log_data(mg_get_context(conns[j]), false);
  450. bandwidthData[0] += currBandwidthDataAfter[0] - otherBandwidthDataBefore[j][0];
  451. bandwidthData[1] += currBandwidthDataAfter[1] - otherBandwidthDataBefore[j][1];
  452. overallBandwidthData[0] += currBandwidthDataAfter[0] - otherBandwidthDataBefore[j][0];
  453. overallBandwidthData[1] += currBandwidthDataAfter[1] - otherBandwidthDataBefore[j][1];
  454. mg_close_connection(conns[j]);
  455. }
  456. clock_t cpuTimeAfter = clock();
  457. std::chrono::high_resolution_clock::time_point wallTimeAfter = std::chrono::high_resolution_clock::now();
  458. std::vector<size_t> serverBandwidthDataAfter = get_server_log_data(civetServer.getContext());
  459. std::vector<double> timingData(2);
  460. timingData[0] = std::chrono::duration_cast<std::chrono::duration<double>>(wallTimeAfter - wallTimeBefore).count();
  461. timingData[1] = ((double)(cpuTimeAfter - cpuTimeBefore)) / CLOCKS_PER_SEC;
  462. bandwidthData[0] += serverBandwidthDataAfter[0] - serverBandwidthDataBefore[0];
  463. bandwidthData[1] += serverBandwidthDataAfter[1] - serverBandwidthDataBefore[1];
  464. write_log_data(outputMtx, outputFilename, timingData, bandwidthData);
  465. write_usage_data(usageMtx, usageFilename);
  466. // Keep an up-to-date version of the proof of the new fresh generator
  467. generatorProofHolder = pi[0];
  468. }
  469. else // When it's another server's turn, tell them to do their part
  470. {
  471. // Serialize the request
  472. std::string data = make_epoch_initiator_string(generatorProofHolder[0], nextGenerator);
  473. // And have them do that request
  474. nextGenerator = initiate_epoch_updates(rng, serverIPs[i], serverPorts[i], data, false, generatorProofHolder, overallBandwidthData);
  475. }
  476. }
  477. // Return the proof of the fresh generator
  478. return generatorProofHolder[0];
  479. }
  480. void epoch_break_down(
  481. std::default_random_engine& rng,
  482. PrsonaServer *prsonaServer,
  483. const std::vector<std::string>& serverIPs,
  484. const std::vector<int>& serverPorts,
  485. const std::string& selfIP,
  486. int selfPort,
  487. const std::vector<Proof>& generatorProof,
  488. const Twistpoint& nextGenerator,
  489. const CivetServer& civetServer,
  490. std::mutex& outputMtx,
  491. const std::string& outputFilename,
  492. std::mutex& usageMtx,
  493. const std::string& usageFilename,
  494. std::vector<size_t>& overallBandwidthData)
  495. {
  496. std::vector<std::vector<std::vector<Proof>>> pi;
  497. std::vector<std::vector<std::vector<Twistpoint>>> permutationCommits;
  498. std::vector<std::vector<std::vector<Twistpoint>>> freshPseudonymCommits;
  499. std::vector<std::vector<std::vector<Twistpoint>>> freshPseudonymSeedCommits;
  500. std::vector<std::vector<std::vector<CurveBipoint>>> serverTallyCommits;
  501. std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>> partwayVoteMatrixCommits;
  502. std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>> finalVoteMatrixCommits;
  503. std::vector<std::vector<std::vector<Twistpoint>>> userTallyMaskCommits;
  504. std::vector<std::vector<std::vector<Twistpoint>>> userTallyMessageCommits;
  505. std::vector<std::vector<std::vector<Twistpoint>>> userTallySeedCommits;
  506. // Go through each server to perform the epoch calculation at hand
  507. for (size_t i = 0; i < serverIPs.size(); i++)
  508. {
  509. // When it's our turn, do things as normal
  510. if (serverIPs[i] == selfIP && serverPorts[i] == selfPort)
  511. {
  512. pi.clear();
  513. permutationCommits.clear();
  514. freshPseudonymCommits.clear();
  515. freshPseudonymSeedCommits.clear();
  516. serverTallyCommits.clear();
  517. partwayVoteMatrixCommits.clear();
  518. finalVoteMatrixCommits.clear();
  519. userTallyMaskCommits.clear();
  520. userTallyMessageCommits.clear();
  521. userTallySeedCommits.clear();
  522. std::vector<size_t> bandwidthData(2);
  523. std::vector<std::vector<size_t>> otherBandwidthDataBefore;
  524. std::vector<size_t> serverBandwidthDataBefore = get_server_log_data(civetServer.getContext());
  525. std::chrono::high_resolution_clock::time_point wallTimeBefore = std::chrono::high_resolution_clock::now();
  526. clock_t cpuTimeBefore = clock();
  527. // Perform the actual calculation
  528. prsonaServer->break_down_midway_pseudonyms(generatorProof, pi, permutationCommits, freshPseudonymCommits, freshPseudonymSeedCommits, serverTallyCommits, partwayVoteMatrixCommits, finalVoteMatrixCommits, userTallyMaskCommits, userTallyMessageCommits, userTallySeedCommits, nextGenerator);
  529. // Serialize the relevant data
  530. std::string data = make_epoch_update_string(pi[0], permutationCommits[0], freshPseudonymCommits[0], freshPseudonymSeedCommits[0], serverTallyCommits[0], partwayVoteMatrixCommits[0], finalVoteMatrixCommits[0], userTallyMaskCommits[0], userTallyMessageCommits[0], userTallySeedCommits[0], nextGenerator, true);
  531. struct synchronization_tool sync;
  532. std::vector<struct mg_connection *> conns;
  533. // Distribute the data to each server (in parallel, roughly)
  534. std::unique_lock<std::mutex> lck(sync.mtx);
  535. sync.val = 1;
  536. for (size_t j = 0; j < serverIPs.size(); j++)
  537. {
  538. // But, obviously, don't send it back to ourselves
  539. if (i == j)
  540. continue;
  541. // Send that data
  542. struct mg_connection *currConn = distribute_epoch_updates(serverIPs[j], serverPorts[j], data, &sync);
  543. otherBandwidthDataBefore.push_back(get_conn_log_data(mg_get_context(currConn), false));
  544. // But keep track of that connection, as we can't close it until we know the server's gotten its data
  545. conns.push_back(currConn);
  546. }
  547. // Wait for the other servers to all report back that they have received the update
  548. while (sync.val < serverIPs.size())
  549. sync.cv.wait(lck);
  550. for (size_t j = 0; j < conns.size(); j++)
  551. {
  552. std::vector<size_t> currBandwidthDataAfter = get_conn_log_data(mg_get_context(conns[j]), false);
  553. bandwidthData[0] += currBandwidthDataAfter[0] - otherBandwidthDataBefore[j][0];
  554. bandwidthData[1] += currBandwidthDataAfter[1] - otherBandwidthDataBefore[j][1];
  555. overallBandwidthData[0] += currBandwidthDataAfter[0] - otherBandwidthDataBefore[j][0];
  556. overallBandwidthData[1] += currBandwidthDataAfter[1] - otherBandwidthDataBefore[j][1];
  557. mg_close_connection(conns[j]);
  558. }
  559. clock_t cpuTimeAfter = clock();
  560. std::chrono::high_resolution_clock::time_point wallTimeAfter = std::chrono::high_resolution_clock::now();
  561. std::vector<size_t> serverBandwidthDataAfter = get_server_log_data(civetServer.getContext());
  562. std::vector<double> timingData(2);
  563. timingData[0] = std::chrono::duration_cast<std::chrono::duration<double>>(wallTimeAfter - wallTimeBefore).count();
  564. timingData[1] = ((double)(cpuTimeAfter - cpuTimeBefore)) / CLOCKS_PER_SEC;
  565. bandwidthData[0] += serverBandwidthDataAfter[0] - serverBandwidthDataBefore[0];
  566. bandwidthData[1] += serverBandwidthDataAfter[1] - serverBandwidthDataBefore[1];
  567. write_log_data(outputMtx, outputFilename, timingData, bandwidthData);
  568. write_usage_data(usageMtx, usageFilename);
  569. }
  570. else // When it's another server's turn, tell them to do their part
  571. {
  572. std::vector<std::vector<Proof>> unused;
  573. // Serialize the request
  574. std::string data = make_epoch_initiator_string(generatorProof, nextGenerator);
  575. // And have them do that request
  576. initiate_epoch_updates(rng, serverIPs[i], serverPorts[i], data, true, unused, overallBandwidthData);
  577. }
  578. }
  579. }
  580. /*
  581. * HELPERS FOR EPOCH HELPERS
  582. */
  583. Twistpoint initiate_epoch_updates(
  584. std::default_random_engine& rng,
  585. const std::string& recipient,
  586. int recipientPort,
  587. const std::string& data,
  588. bool isBreakdown,
  589. std::vector<std::vector<Proof>>& generatorProofHolder,
  590. std::vector<size_t>& bandwidthData)
  591. {
  592. Twistpoint retval;
  593. struct synchronization_tool sync;
  594. char * filename = NULL;
  595. const char* which = (isBreakdown ? REQUEST_EPOCH_BREAK_DOWN_URI : REQUEST_EPOCH_BUILD_UP_URI);
  596. // Make sure we don't move on until this server has conducted its epoch calculations
  597. std::unique_lock<std::mutex> lck(sync.mtx);
  598. sync.val = 0;
  599. while (!sync.val)
  600. {
  601. struct mg_connection *conn = NULL;
  602. // Connect to a server
  603. sync.val2 = 0;
  604. while (!conn)
  605. {
  606. conn = mg_connect_websocket_client(recipient.c_str(), recipientPort, USE_SSL, NULL, 0, which, "null", epoch_websocket_data_handler, epoch_websocket_close_handler, &sync);
  607. if (!conn)
  608. std::cerr << "Couldn't initiate epoch update with server at " << recipient << ":" << recipientPort << std::endl;
  609. }
  610. // Establish a file to receive update data (when relevant)
  611. if (!isBreakdown)
  612. filename = set_temp_filename(rng, conn);
  613. // Send the relevant data
  614. mg_websocket_client_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
  615. mg_websocket_client_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
  616. // Wait for a response (or the connection to die)
  617. while (!sync.val2)
  618. sync.cv.wait(lck);
  619. // Close connection
  620. mg_close_connection(conn);
  621. }
  622. // In the second half of the epoch, we don't have a data response to un-serialize, so just move on
  623. if (isBreakdown)
  624. return retval;
  625. std::vector<Proof> generatorProof;
  626. generatorProofHolder.clear();
  627. // Otherwise, un-serialize the updated fresh generator (and its proof of correctness)
  628. retval = get_generator_from_file(filename, generatorProof);
  629. generatorProofHolder.push_back(generatorProof);
  630. remove(filename);
  631. delete [] filename;
  632. return retval;
  633. }
  634. struct mg_connection *distribute_epoch_updates(
  635. const std::string& recipient,
  636. int recipientPort,
  637. const std::string& data,
  638. struct synchronization_tool* sync)
  639. {
  640. struct mg_connection *conn = NULL;
  641. // Connect to the server
  642. while (!conn)
  643. {
  644. conn = mg_connect_websocket_client(recipient.c_str(), recipientPort, USE_SSL, NULL, 0, SUBMIT_EPOCH_UPDATES_URI, "null", synchro_websocket_data_handler, synchro_websocket_close_handler, sync);
  645. if (!conn)
  646. std::cerr << "Couldn't give epoch updates to server at " << recipient << ":" << recipientPort << std::endl;
  647. }
  648. // Send the update
  649. mg_websocket_client_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
  650. mg_websocket_client_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
  651. return conn;
  652. }
  653. /*
  654. * SCORE TALLYING AND DISTRIBUTION HELPERS
  655. */
  656. void tally_scores(
  657. PrsonaServer *prsonaServer,
  658. const std::vector<std::string>& serverIPs,
  659. const std::vector<int>& serverPorts,
  660. const std::string& selfIP,
  661. int selfPort,
  662. const Twistpoint& nextGenerator,
  663. std::vector<EGCiphertext>& userTallyScores,
  664. std::vector<CurveBipoint>& serverTallyScores,
  665. std::vector<size_t>& bandwidthData)
  666. {
  667. struct synchronization_tool sync;
  668. std::vector<struct mg_connection *> conns;
  669. std::vector<std::vector<size_t>> allBandwidthDataBefore;
  670. // Connect to each server (roughly in parallel)
  671. std::unique_lock<std::mutex> lck(sync.mtx);
  672. sync.val = 1;
  673. for (size_t i = 0; i < serverIPs.size(); i++)
  674. {
  675. // Except, skip ourselves (obviously)
  676. if (serverIPs[i] == selfIP && serverPorts[i] == selfPort)
  677. continue;
  678. struct mg_connection *currConn = NULL;
  679. while (!currConn)
  680. {
  681. currConn = mg_connect_websocket_client(serverIPs[i].c_str(), serverPorts[i], USE_SSL, NULL, 0, REQUEST_PARTIAL_DECRYPTION_URI, "null", synchro_websocket_data_handler, synchro_websocket_close_handler, &sync);
  682. if (!currConn)
  683. std::cerr << "Trouble getting partial decryption from server at " << serverIPs[i] << ":" << serverPorts[i] << std::endl;
  684. }
  685. allBandwidthDataBefore.push_back(get_conn_log_data(mg_get_context(currConn), true));
  686. // Ping server for simulated distributed BGN
  687. mg_websocket_client_write(currConn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
  688. conns.push_back(currConn);
  689. }
  690. // Since we're only simulating distributed BGN, we're only waiting for ACKs here
  691. while (sync.val < serverIPs.size())
  692. sync.cv.wait(lck);
  693. // Close connections
  694. for (size_t i = 0; i < conns.size(); i++)
  695. {
  696. std::vector<size_t> currBandwidthDataAfter = get_conn_log_data(mg_get_context(conns[i]), true);
  697. bandwidthData[0] += currBandwidthDataAfter[0] - allBandwidthDataBefore[i][0];
  698. bandwidthData[1] += currBandwidthDataAfter[1] - allBandwidthDataBefore[i][1];
  699. mg_close_connection(conns[i]);
  700. }
  701. // Now we do the actual calculations
  702. std::vector<EGCiphertext> retval;
  703. std::vector<Twistpoint> currentPseudonyms = prsonaServer->get_current_pseudonyms();
  704. std::vector<Scalar> decryptedTalliedScores = prsonaServer->tally_scores();
  705. mpz_class maxScorePossibleThisRound = prsonaServer->get_max_possible_score().toInt() * PrsonaBase::get_max_allowed_vote();
  706. mpz_class topOfScoreRange = decryptedTalliedScores.size() * PrsonaBase::get_max_allowed_vote();
  707. userTallyScores.clear();
  708. serverTallyScores.clear();
  709. for (size_t i = 0; i < decryptedTalliedScores.size(); i++)
  710. {
  711. // Scale scores correctly to keep in the desired range
  712. decryptedTalliedScores[i] = Scalar((decryptedTalliedScores[i].toInt() * topOfScoreRange) / maxScorePossibleThisRound);
  713. EGCiphertext currCiphertext;
  714. userTallyScores.push_back(currCiphertext);
  715. CurveBipoint currServerScore;
  716. serverTallyScores.push_back(currServerScore);
  717. Scalar currMask;
  718. currMask.set_random();
  719. // Give the server the new weights for the next epoch's calculation, to get passed around to the other servers
  720. prsonaServer->encrypt(serverTallyScores[i], decryptedTalliedScores[i]);
  721. // Encrypt the scores for the partway pseudonyms (since we're in the middle of the epoch calculations)
  722. userTallyScores[i].mask = currentPseudonyms[i] * currMask;
  723. userTallyScores[i].encryptedMessage = (nextGenerator * currMask) + (prsonaServer->get_blinding_generator() * decryptedTalliedScores[i]);
  724. }
  725. }
  726. void distribute_tallied_scores(
  727. PrsonaServer *prsonaServer,
  728. const std::vector<std::string>& serverIPs,
  729. const std::vector<int>& serverPorts,
  730. const std::string& selfIP,
  731. int selfPort,
  732. const Twistpoint& nextGenerator,
  733. const std::vector<EGCiphertext>& userTallyScores,
  734. const std::vector<CurveBipoint>& serverTallyScores,
  735. std::vector<size_t>& bandwidthData)
  736. {
  737. // Serialize scores
  738. std::stringstream buffer;
  739. std::string data;
  740. BinarySizeT sizeOfVector(userTallyScores.size());
  741. buffer << sizeOfVector;
  742. for (size_t i = 0; i < sizeOfVector.val(); i++)
  743. buffer << userTallyScores[i];
  744. for (size_t i = 0; i < sizeOfVector.val(); i++)
  745. buffer << serverTallyScores[i];
  746. data = buffer.str();
  747. struct synchronization_tool sync;
  748. std::vector<struct mg_connection *> conns;
  749. std::vector<std::vector<size_t>> allBandwidthDataBefore;
  750. // Connect to each server (roughly in parallel)
  751. std::unique_lock<std::mutex> lck(sync.mtx);
  752. sync.val = 1;
  753. for (size_t i = 0; i < serverIPs.size(); i++)
  754. {
  755. // When it's our turn, receive our actual new scores for the next epoch
  756. if (serverIPs[i] == selfIP && serverPorts[i] == selfPort)
  757. {
  758. prsonaServer->receive_tallied_scores(userTallyScores, serverTallyScores);
  759. continue;
  760. }
  761. struct mg_connection *currConn = NULL;
  762. while (!currConn)
  763. {
  764. currConn = mg_connect_websocket_client(serverIPs[i].c_str(), serverPorts[i], USE_SSL, NULL, 0, SUBMIT_PARTIAL_DECRYPTION_URI, "null", synchro_websocket_data_handler, synchro_websocket_close_handler, &sync);
  765. if (!currConn)
  766. std::cerr << "Trouble giving full re-encryption to server at " << serverIPs[i] << ":" << serverPorts[i] << std::endl;
  767. }
  768. allBandwidthDataBefore.push_back(get_conn_log_data(mg_get_context(currConn), false));
  769. // Send the relevant data
  770. mg_websocket_client_write(currConn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
  771. mg_websocket_client_write(currConn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
  772. conns.push_back(currConn);
  773. }
  774. // Wait for each server to ACK the scores
  775. while (sync.val < serverIPs.size())
  776. sync.cv.wait(lck);
  777. // Close connections
  778. for (size_t i = 0; i < conns.size(); i++)
  779. {
  780. std::vector<size_t> currBandwidthDataAfter = get_conn_log_data(mg_get_context(conns[i]), false);
  781. bandwidthData[0] += currBandwidthDataAfter[0] - allBandwidthDataBefore[i][0];
  782. bandwidthData[1] += currBandwidthDataAfter[1] - allBandwidthDataBefore[i][1];
  783. mg_close_connection(conns[i]);
  784. }
  785. }
  786. /*
  787. * FILE I/O HELPERS
  788. */
  789. BGN get_bgn_private_key_from_file(
  790. const char *filename)
  791. {
  792. std::ifstream bgnFile(filename);
  793. BGN privateKey;
  794. bgnFile >> privateKey;
  795. return privateKey;
  796. }
  797. Twistpoint get_generator_from_file(
  798. const char *filename,
  799. Proof& pi)
  800. {
  801. std::ifstream genFile(filename);
  802. Twistpoint retval;
  803. genFile >> pi;
  804. genFile >> retval;
  805. return retval;
  806. }
  807. Twistpoint get_generator_from_file(
  808. const char *filename,
  809. std::vector<Proof>& pi)
  810. {
  811. std::ifstream epochFile(filename);
  812. Twistpoint retval;
  813. BinarySizeT sizeOfVector;
  814. pi.clear();
  815. epochFile >> sizeOfVector;
  816. for (size_t i = 0; i < sizeOfVector.val(); i++)
  817. {
  818. Proof currProof;
  819. epochFile >> currProof;
  820. pi.push_back(currProof);
  821. }
  822. epochFile >> retval;
  823. return retval;
  824. }
  825. /*
  826. * EPOCH DATA SERIALIZERS/UN-SERIALIZERS
  827. */
  828. std::string make_epoch_initiator_string(
  829. const std::vector<Proof>& generatorProof,
  830. const Twistpoint& nextGenerator)
  831. {
  832. std::stringstream buffer;
  833. BinarySizeT sizeOfVector(generatorProof.size());
  834. buffer << sizeOfVector;
  835. for (size_t i = 0; i < sizeOfVector.val(); i++)
  836. buffer << generatorProof[i];
  837. buffer << nextGenerator;
  838. return buffer.str();
  839. }
  840. ssize_t read_epoch_initiator_string(
  841. const char *filename,
  842. std::vector<Proof>& generatorProof,
  843. Twistpoint& nextGenerator)
  844. {
  845. std::ifstream file(filename);
  846. file.ignore(std::numeric_limits<std::streamsize>::max());
  847. std::streamsize retval = file.gcount();
  848. file.clear();
  849. file.seekg(0, std::ios_base::beg);
  850. BinarySizeT sizeOfVector;
  851. generatorProof.clear();
  852. file >> sizeOfVector;
  853. for (size_t i = 0; i < sizeOfVector.val(); i++)
  854. {
  855. Proof currProof;
  856. file >> currProof;
  857. generatorProof.push_back(currProof);
  858. }
  859. file >> nextGenerator;
  860. return retval;
  861. }
  862. std::string make_epoch_update_string(
  863. const std::vector<std::vector<Proof>>& pi,
  864. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  865. const std::vector<std::vector<Twistpoint>>& freshPseudonymCommits,
  866. const std::vector<std::vector<Twistpoint>>& freshPseudonymSeedCommits,
  867. const std::vector<std::vector<CurveBipoint>>& serverTallyCommits,
  868. const std::vector<std::vector<std::vector<TwistBipoint>>>& partwayVoteMatrixCommits,
  869. const std::vector<std::vector<std::vector<TwistBipoint>>>& finalVoteMatrixCommits,
  870. const std::vector<std::vector<Twistpoint>>& userTallyMaskCommits,
  871. const std::vector<std::vector<Twistpoint>>& userTallyMessageCommits,
  872. const std::vector<std::vector<Twistpoint>>& userTallySeedCommits,
  873. const Twistpoint& nextGenerator,
  874. bool doUserTallies)
  875. {
  876. std::stringstream buffer;
  877. BinarySizeT sizeOfVectorI, sizeOfVectorJ;
  878. sizeOfVectorI.set(pi.size());
  879. buffer << sizeOfVectorI;
  880. for (size_t i = 0; i < sizeOfVectorI.val(); i++)
  881. {
  882. sizeOfVectorJ.set(pi[i].size());
  883. buffer << sizeOfVectorJ;
  884. for (size_t j = 0; j < sizeOfVectorJ.val(); j++)
  885. buffer << pi[i][j];
  886. }
  887. sizeOfVectorI.set(permutationCommits.size());
  888. buffer << sizeOfVectorI;
  889. for (size_t i = 0; i < sizeOfVectorI.val(); i++)
  890. for (size_t j = 0; j < sizeOfVectorI.val(); j++)
  891. buffer << permutationCommits[i][j];
  892. sizeOfVectorI.set(freshPseudonymCommits.size());
  893. buffer << sizeOfVectorI;
  894. for (size_t i = 0; i < sizeOfVectorI.val(); i++)
  895. for (size_t j = 0; j < sizeOfVectorI.val(); j++)
  896. buffer << freshPseudonymCommits[i][j];
  897. sizeOfVectorI.set(freshPseudonymSeedCommits.size());
  898. buffer << sizeOfVectorI;
  899. for (size_t i = 0; i < sizeOfVectorI.val(); i++)
  900. for (size_t j = 0; j < sizeOfVectorI.val(); j++)
  901. buffer << freshPseudonymSeedCommits[i][j];
  902. sizeOfVectorI.set(serverTallyCommits.size());
  903. buffer << sizeOfVectorI;
  904. for (size_t i = 0; i < sizeOfVectorI.val(); i++)
  905. for (size_t j = 0; j < sizeOfVectorI.val(); j++)
  906. buffer << serverTallyCommits[i][j];
  907. sizeOfVectorI.set(partwayVoteMatrixCommits.size());
  908. buffer << sizeOfVectorI;
  909. for (size_t i = 0; i < sizeOfVectorI.val(); i++)
  910. for (size_t j = 0; j < sizeOfVectorI.val(); j++)
  911. for (size_t k = 0; k < sizeOfVectorI.val(); k++)
  912. buffer << partwayVoteMatrixCommits[i][j][k];
  913. sizeOfVectorI.set(finalVoteMatrixCommits.size());
  914. buffer << sizeOfVectorI;
  915. for (size_t i = 0; i < sizeOfVectorI.val(); i++)
  916. for (size_t j = 0; j < sizeOfVectorI.val(); j++)
  917. for (size_t k = 0; k < sizeOfVectorI.val(); k++)
  918. buffer << finalVoteMatrixCommits[i][j][k];
  919. sizeOfVectorI.set(userTallyMaskCommits.size());
  920. buffer << sizeOfVectorI;
  921. for (size_t i = 0; i < sizeOfVectorI.val(); i++)
  922. for (size_t j = 0; j < sizeOfVectorI.val(); j++)
  923. buffer << userTallyMaskCommits[i][j];
  924. sizeOfVectorI.set(userTallyMessageCommits.size());
  925. buffer << sizeOfVectorI;
  926. for (size_t i = 0; i < sizeOfVectorI.val(); i++)
  927. for (size_t j = 0; j < sizeOfVectorI.val(); j++)
  928. buffer << userTallyMessageCommits[i][j];
  929. sizeOfVectorI.set(userTallySeedCommits.size());
  930. buffer << sizeOfVectorI;
  931. for (size_t i = 0; i < sizeOfVectorI.val(); i++)
  932. for (size_t j = 0; j < sizeOfVectorI.val(); j++)
  933. buffer << userTallySeedCommits[i][j];
  934. buffer << nextGenerator;
  935. BinaryBool flag(doUserTallies);
  936. buffer << flag;
  937. return buffer.str();
  938. }
  939. ssize_t read_epoch_update_string(
  940. const char *filename,
  941. std::vector<std::vector<Proof>>& pi,
  942. std::vector<std::vector<Twistpoint>>& permutationCommits,
  943. std::vector<std::vector<Twistpoint>>& freshPseudonymCommits,
  944. std::vector<std::vector<Twistpoint>>& freshPseudonymSeedCommits,
  945. std::vector<std::vector<CurveBipoint>>& serverTallyCommits,
  946. std::vector<std::vector<std::vector<TwistBipoint>>>& partwayVoteMatrixCommits,
  947. std::vector<std::vector<std::vector<TwistBipoint>>>& finalVoteMatrixCommits,
  948. std::vector<std::vector<Twistpoint>>& userTallyMaskCommits,
  949. std::vector<std::vector<Twistpoint>>& userTallyMessageCommits,
  950. std::vector<std::vector<Twistpoint>>& userTallySeedCommits,
  951. Twistpoint& nextGenerator,
  952. bool& doUserTallies)
  953. {
  954. std::ifstream file(filename);
  955. file.ignore(std::numeric_limits<std::streamsize>::max());
  956. std::streamsize retval = file.gcount();
  957. file.clear();
  958. file.seekg(0, std::ios_base::beg);
  959. BinarySizeT sizeOfVectorI, sizeOfVectorJ;
  960. pi.clear();
  961. permutationCommits.clear();
  962. freshPseudonymCommits.clear();
  963. freshPseudonymSeedCommits.clear();
  964. serverTallyCommits.clear();
  965. partwayVoteMatrixCommits.clear();
  966. finalVoteMatrixCommits.clear();
  967. userTallyMaskCommits.clear();
  968. userTallyMessageCommits.clear();
  969. userTallySeedCommits.clear();
  970. file >> sizeOfVectorI;
  971. for (size_t i = 0; i < sizeOfVectorI.val(); i++)
  972. {
  973. std::vector<Proof> currRow;
  974. file >> sizeOfVectorJ;
  975. for (size_t j = 0; j < sizeOfVectorJ.val(); j++)
  976. {
  977. Proof currProof;
  978. file >> currProof;
  979. currRow.push_back(currProof);
  980. }
  981. pi.push_back(currRow);
  982. }
  983. file >> sizeOfVectorI;
  984. for (size_t i = 0; i < sizeOfVectorI.val(); i++)
  985. {
  986. std::vector<Twistpoint> currRow;
  987. for (size_t j = 0; j < sizeOfVectorI.val(); j++)
  988. {
  989. Twistpoint currCommit;
  990. file >> currCommit;
  991. currRow.push_back(currCommit);
  992. }
  993. permutationCommits.push_back(currRow);
  994. }
  995. file >> sizeOfVectorI;
  996. for (size_t i = 0; i < sizeOfVectorI.val(); i++)
  997. {
  998. std::vector<Twistpoint> currRow;
  999. for (size_t j = 0; j < sizeOfVectorI.val(); j++)
  1000. {
  1001. Twistpoint currCommit;
  1002. file >> currCommit;
  1003. currRow.push_back(currCommit);
  1004. }
  1005. freshPseudonymCommits.push_back(currRow);
  1006. }
  1007. file >> sizeOfVectorI;
  1008. for (size_t i = 0; i < sizeOfVectorI.val(); i++)
  1009. {
  1010. std::vector<Twistpoint> currRow;
  1011. for (size_t j = 0; j < sizeOfVectorI.val(); j++)
  1012. {
  1013. Twistpoint currCommit;
  1014. file >> currCommit;
  1015. currRow.push_back(currCommit);
  1016. }
  1017. freshPseudonymSeedCommits.push_back(currRow);
  1018. }
  1019. file >> sizeOfVectorI;
  1020. for (size_t i = 0; i < sizeOfVectorI.val(); i++)
  1021. {
  1022. std::vector<CurveBipoint> currRow;
  1023. for (size_t j = 0; j < sizeOfVectorI.val(); j++)
  1024. {
  1025. CurveBipoint currCommit;
  1026. file >> currCommit;
  1027. currRow.push_back(currCommit);
  1028. }
  1029. serverTallyCommits.push_back(currRow);
  1030. }
  1031. file >> sizeOfVectorI;
  1032. for (size_t i = 0; i < sizeOfVectorI.val(); i++)
  1033. {
  1034. std::vector<std::vector<TwistBipoint>> currMatrix;
  1035. for (size_t j = 0; j < sizeOfVectorI.val(); j++)
  1036. {
  1037. std::vector<TwistBipoint> currRow;
  1038. for (size_t k = 0; k < sizeOfVectorI.val(); k++)
  1039. {
  1040. TwistBipoint currCommit;
  1041. file >> currCommit;
  1042. currRow.push_back(currCommit);
  1043. }
  1044. currMatrix.push_back(currRow);
  1045. }
  1046. partwayVoteMatrixCommits.push_back(currMatrix);
  1047. }
  1048. file >> sizeOfVectorI;
  1049. for (size_t i = 0; i < sizeOfVectorI.val(); i++)
  1050. {
  1051. std::vector<std::vector<TwistBipoint>> currMatrix;
  1052. for (size_t j = 0; j < sizeOfVectorI.val(); j++)
  1053. {
  1054. std::vector<TwistBipoint> currRow;
  1055. for (size_t k = 0; k < sizeOfVectorI.val(); k++)
  1056. {
  1057. TwistBipoint currCommit;
  1058. file >> currCommit;
  1059. currRow.push_back(currCommit);
  1060. }
  1061. currMatrix.push_back(currRow);
  1062. }
  1063. finalVoteMatrixCommits.push_back(currMatrix);
  1064. }
  1065. file >> sizeOfVectorI;
  1066. for (size_t i = 0; i < sizeOfVectorI.val(); i++)
  1067. {
  1068. std::vector<Twistpoint> currRow;
  1069. for (size_t j = 0; j < sizeOfVectorI.val(); j++)
  1070. {
  1071. Twistpoint currCommit;
  1072. file >> currCommit;
  1073. currRow.push_back(currCommit);
  1074. }
  1075. userTallyMaskCommits.push_back(currRow);
  1076. }
  1077. file >> sizeOfVectorI;
  1078. for (size_t i = 0; i < sizeOfVectorI.val(); i++)
  1079. {
  1080. std::vector<Twistpoint> currRow;
  1081. for (size_t j = 0; j < sizeOfVectorI.val(); j++)
  1082. {
  1083. Twistpoint currCommit;
  1084. file >> currCommit;
  1085. currRow.push_back(currCommit);
  1086. }
  1087. userTallyMessageCommits.push_back(currRow);
  1088. }
  1089. file >> sizeOfVectorI;
  1090. for (size_t i = 0; i < sizeOfVectorI.val(); i++)
  1091. {
  1092. std::vector<Twistpoint> currRow;
  1093. for (size_t j = 0; j < sizeOfVectorI.val(); j++)
  1094. {
  1095. Twistpoint currCommit;
  1096. file >> currCommit;
  1097. currRow.push_back(currCommit);
  1098. }
  1099. userTallySeedCommits.push_back(currRow);
  1100. }
  1101. file >> nextGenerator;
  1102. BinaryBool binaryDoUserTallies;
  1103. file >> binaryDoUserTallies;
  1104. doUserTallies = binaryDoUserTallies.val();
  1105. return retval;
  1106. }
  1107. /**********************************************************
  1108. **** ****
  1109. **** other server-relevant handler member functions ****
  1110. **** ****
  1111. **********************************************************/
  1112. /*
  1113. * EPOCH READY HANDLER
  1114. */
  1115. EpochReadyHandler::EpochReadyHandler(
  1116. struct synchronization_tool *exitSync,
  1117. struct synchronization_tool *readySync,
  1118. std::mutex& updateMtx,
  1119. size_t numServers)
  1120. : exitSync(exitSync), readySync(readySync), updateMtx(updateMtx), numServers(numServers)
  1121. { /* */ }
  1122. bool EpochReadyHandler::handleGet(
  1123. CivetServer *server,
  1124. struct mg_connection *conn)
  1125. {
  1126. std::unique_lock<std::mutex> exitLock(exitSync->mtx, std::defer_lock);
  1127. std::unique_lock<std::mutex> updateLock(updateMtx, std::defer_lock);
  1128. std::unique_lock<std::mutex> readyLock(readySync->mtx);
  1129. if (readySync->val < numServers)
  1130. {
  1131. mg_printf(conn, "HTTP/1.1 503 Service Unavailable\r\n"
  1132. "Content-Type: text/plain\r\n"
  1133. "Connection: close\r\n\r\n");
  1134. mg_printf(conn, "Server is waiting for other servers to begin.\n");
  1135. }
  1136. else if (!exitLock.try_lock())
  1137. {
  1138. mg_printf(conn, "HTTP/1.1 503 Service Unavailable\r\n"
  1139. "Content-Type: text/plain\r\n"
  1140. "Connection: close\r\n\r\n");
  1141. mg_printf(conn, "Server is still in a previous epoch.\n");
  1142. }
  1143. else if (!updateLock.try_lock())
  1144. {
  1145. mg_printf(conn, "HTTP/1.1 503 Service Unavailable\r\n"
  1146. "Content-Type: text/plain\r\n"
  1147. "Connection: close\r\n\r\n");
  1148. mg_printf(conn, "Server is handling other updates.\n");
  1149. }
  1150. else
  1151. {
  1152. mg_printf(conn, "HTTP/1.1 200 OK\r\n"
  1153. "Content-Type: text/plain\r\n"
  1154. "Connection: close\r\n\r\n");
  1155. mg_printf(conn, "Server is ready for epoch.\n");
  1156. }
  1157. return true;
  1158. }
  1159. /*
  1160. * EPOCH NUM HANDLER
  1161. */
  1162. EpochNumHandler::EpochNumHandler(
  1163. std::atomic<size_t>& epochNum)
  1164. : epochNum(epochNum)
  1165. { /* */ }
  1166. bool EpochNumHandler::handleGet(
  1167. CivetServer *server,
  1168. struct mg_connection *conn)
  1169. {
  1170. mg_printf(conn, "HTTP/1.1 200 OK\r\n"
  1171. "Content-Type: text/plain\r\n"
  1172. "Connection: close\r\n\r\n");
  1173. mg_printf(conn, "Epoch num: %lu\n", epochNum.load());
  1174. return true;
  1175. }
  1176. /*
  1177. * UPDATE LOCK HANDLER
  1178. */
  1179. UpdateLockWebSocketHandler::UpdateLockWebSocketHandler(
  1180. std::mutex& updateMtx,
  1181. std::unique_lock<std::mutex> **lockHolder,
  1182. bool isLocking)
  1183. : updateMtx(updateMtx), lockHolder(lockHolder), isLocking(isLocking)
  1184. { /* */ }
  1185. UpdateLockWebSocketHandler::~UpdateLockWebSocketHandler()
  1186. {
  1187. delete *lockHolder;
  1188. *lockHolder = NULL;
  1189. }
  1190. bool UpdateLockWebSocketHandler::handleConnection(
  1191. CivetServer *server,
  1192. const struct mg_connection *conn)
  1193. {
  1194. return true;
  1195. }
  1196. void UpdateLockWebSocketHandler::handleReadyState(
  1197. CivetServer *server,
  1198. struct mg_connection *conn)
  1199. { /* */ }
  1200. bool UpdateLockWebSocketHandler::handleData(
  1201. CivetServer *server,
  1202. struct mg_connection *conn,
  1203. int bits,
  1204. char *data,
  1205. size_t data_len)
  1206. {
  1207. switch (bits & 0xf)
  1208. {
  1209. case MG_WEBSOCKET_OPCODE_DATACOMPLETE:
  1210. if (isLocking)
  1211. {
  1212. std::unique_lock<std::mutex> *tempHolder = new std::unique_lock<std::mutex>(updateMtx);
  1213. // Once you get to this line, we now hold the lock, and lockHolder is guaranteed to be NULL
  1214. *lockHolder = tempHolder;
  1215. // Respond to notify that the requesting process holds the lock
  1216. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
  1217. }
  1218. else
  1219. {
  1220. // You must do things in this order so that *lockHolder will be guaranteed to be NULL at the time the lock unlocks (deletion of the lock object)
  1221. std::unique_lock<std::mutex> *tempHolder = *lockHolder;
  1222. *lockHolder = NULL;
  1223. delete tempHolder;
  1224. // Respond to notify that the requesting process has released the lock
  1225. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
  1226. }
  1227. break;
  1228. case MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE:
  1229. break;
  1230. default:
  1231. std::cerr << "Unknown opcode: failing." << std::endl;
  1232. break;
  1233. }
  1234. return false;
  1235. }
  1236. void UpdateLockWebSocketHandler::handleClose(
  1237. CivetServer *server,
  1238. const struct mg_connection *conn)
  1239. { /* */ }
  1240. /*********************************************************
  1241. **** ****
  1242. **** PrsonaServerWebSocketHandler member functions ****
  1243. **** ****
  1244. *********************************************************/
  1245. /*
  1246. * CONSTRUCTOR
  1247. */
  1248. PrsonaServerWebSocketHandler::PrsonaServerWebSocketHandler(
  1249. std::default_random_engine& rng,
  1250. PrsonaServer *prsonaServer,
  1251. const std::vector<std::string>& serverIPs,
  1252. const std::vector<int>& serverPorts,
  1253. const std::string& selfIP,
  1254. int selfPort,
  1255. std::mutex& updateMtx,
  1256. std::atomic<size_t>& epochNum,
  1257. std::mutex& buildUpOutputMtx,
  1258. const std::string& buildUpOutputFilename,
  1259. std::mutex& breakDownOutputMtx,
  1260. const std::string& breakDownOutputFilename,
  1261. std::mutex& updateOutputMtx,
  1262. const std::string& updateOutputFilename,
  1263. std::mutex& voteOutputMtx,
  1264. const std::string& voteOutputFilename,
  1265. std::mutex& usageMtx,
  1266. const std::string& usageFilename)
  1267. : rng(rng), prsonaServer(prsonaServer), serverIPs(serverIPs), serverPorts(serverPorts), selfIP(selfIP), selfPort(selfPort), updateMtx(updateMtx), epochNum(epochNum), buildUpOutputMtx(buildUpOutputMtx), buildUpOutputFilename(buildUpOutputFilename), breakDownOutputMtx(breakDownOutputMtx), breakDownOutputFilename(breakDownOutputFilename), updateOutputMtx(updateOutputMtx), updateOutputFilename(updateOutputFilename), voteOutputMtx(voteOutputMtx), voteOutputFilename(voteOutputFilename), usageMtx(usageMtx), usageFilename(usageFilename)
  1268. { /* */ }
  1269. /*
  1270. * REQUIRED BY INHERITED CLASS
  1271. */
  1272. bool PrsonaServerWebSocketHandler::handleConnection(
  1273. CivetServer *server,
  1274. const struct mg_connection *conn)
  1275. {
  1276. const struct mg_request_info *info = mg_get_request_info(conn);
  1277. // Check if the request being made is something this server can respond to
  1278. bool flag = info->query_string && info->query_string[0] >= PRSONA_GIVE_BGN_PUBKEY && info->query_string[0] <= PRSONA_RECEIVE_EPOCH_UPDATE;
  1279. flag = flag || (info->query_string && info->query_string[0] == PRSONA_GIVE_PARTIAL_DECRYPTION);
  1280. flag = flag || (info->query_string && info->query_string[0] == PRSONA_RECEIVE_PARTIAL_DECRYPTION);
  1281. return flag;
  1282. }
  1283. void PrsonaServerWebSocketHandler::handleReadyState(
  1284. CivetServer *server,
  1285. struct mg_connection *conn)
  1286. {
  1287. const struct mg_request_info *info = mg_get_request_info(conn);
  1288. // Set filenames for query types that will need to un-serialize data to respond correctly
  1289. switch (info->query_string[0])
  1290. {
  1291. case PRSONA_GIVE_VOTE_ROW:
  1292. case PRSONA_GIVE_VOTE_MATRIX:
  1293. case PRSONA_GIVE_CLIENT_TALLY:
  1294. case PRSONA_GIVE_SERVER_TALLY:
  1295. case PRSONA_GIVE_PSEUDONYMS:
  1296. case PRSONA_GIVE_VOTE_ROW_COMMITMENT:
  1297. case PRSONA_GIVE_VOTE_MATRIX_COMMITMENT:
  1298. case PRSONA_GIVE_CLIENT_TALLY_COMMITMENT:
  1299. case PRSONA_GIVE_SERVER_TALLY_COMMITMENT:
  1300. case PRSONA_GIVE_PSEUDONYMS_COMMITMENT:
  1301. case PRSONA_RECEIVE_NEW_CLIENT:
  1302. case PRSONA_RECEIVE_VOTE:
  1303. case PRSONA_RECEIVE_UPDATE_WITH_NEW_USER:
  1304. case PRSONA_ADD_CURR_SEED_TO_GENERATOR:
  1305. case PRSONA_RECEIVE_FRESH_GENERATOR:
  1306. case PRSONA_ADD_RAND_SEED_TO_GENERATOR:
  1307. case PRSONA_RECEIVE_EG_BLIND_GENERATOR:
  1308. case PRSONA_PERFORM_EPOCH_BUILD_UP:
  1309. case PRSONA_PERFORM_EPOCH_BREAK_DOWN:
  1310. case PRSONA_RECEIVE_EPOCH_UPDATE:
  1311. case PRSONA_RECEIVE_PARTIAL_DECRYPTION:
  1312. set_temp_filename(rng, conn);
  1313. break;
  1314. default:
  1315. mg_set_user_connection_data(conn, NULL);
  1316. break;
  1317. }
  1318. }
  1319. bool PrsonaServerWebSocketHandler::handleData(
  1320. CivetServer *server,
  1321. struct mg_connection *conn,
  1322. int bits,
  1323. char *data,
  1324. size_t data_len)
  1325. {
  1326. char *filename = (char *) mg_get_user_connection_data(conn);
  1327. FILE *currFile = NULL;
  1328. switch (bits & 0xf)
  1329. {
  1330. // Requester has indicated they have sent all relevant data
  1331. case MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE:
  1332. case MG_WEBSOCKET_OPCODE_DATACOMPLETE:
  1333. generate_response(server, conn, filename);
  1334. break;
  1335. // Requester has sent more data (which may theoretically be broken up into multiple packets)
  1336. case MG_WEBSOCKET_OPCODE_BINARY:
  1337. case MG_WEBSOCKET_OPCODE_CONTINUATION:
  1338. currFile = fopen(filename, "ab");
  1339. fwrite(data, sizeof(char), data_len, currFile);
  1340. fclose(currFile);
  1341. return true;
  1342. // Something strange has happened
  1343. default:
  1344. std::cerr << "Unknown packet type received. Failing." << std::endl;
  1345. break;
  1346. }
  1347. return false;
  1348. }
  1349. void PrsonaServerWebSocketHandler::handleClose(
  1350. CivetServer *server,
  1351. const struct mg_connection *conn)
  1352. {
  1353. char *filename = (char *) mg_get_user_connection_data(conn);
  1354. // If we didn't have a temp file for this request, don't do anything
  1355. if (!filename)
  1356. return;
  1357. // If we did, delete it
  1358. remove(filename);
  1359. delete [] filename;
  1360. }
  1361. /*
  1362. * RESPONSE ROUTER FUNCTION
  1363. */
  1364. void PrsonaServerWebSocketHandler::generate_response(
  1365. CivetServer *server,
  1366. struct mg_connection *conn,
  1367. const char *filename)
  1368. {
  1369. const struct mg_request_info *info = mg_get_request_info(conn);
  1370. // Select the correct response for this type of request
  1371. switch (info->query_string[0])
  1372. {
  1373. // BASIC PUBLIC SYSTEM INFO GETTERS
  1374. case PRSONA_GIVE_BGN_PUBKEY:
  1375. get_bgn_public_key(conn);
  1376. break;
  1377. case PRSONA_GIVE_NUM_CLIENTS:
  1378. get_num_clients(conn);
  1379. break;
  1380. case PRSONA_GIVE_NUM_SERVERS:
  1381. get_num_servers(conn);
  1382. break;
  1383. case PRSONA_GIVE_FRESH_GENERATOR:
  1384. get_generator(conn, true);
  1385. break;
  1386. case PRSONA_GIVE_EG_BLIND_GENERATOR:
  1387. get_generator(conn, false);
  1388. break;
  1389. // ENCRYPTED DATA GETTERS
  1390. case PRSONA_GIVE_VOTE_ROW:
  1391. get_current_votes_by(conn, filename);
  1392. break;
  1393. case PRSONA_GIVE_VOTE_MATRIX:
  1394. get_all_current_votes(conn);
  1395. break;
  1396. case PRSONA_GIVE_CLIENT_TALLY:
  1397. get_current_user_encrypted_tally(conn, filename);
  1398. break;
  1399. case PRSONA_GIVE_SERVER_TALLY:
  1400. get_current_server_encrypted_tally(conn, filename);
  1401. break;
  1402. case PRSONA_GIVE_PSEUDONYMS:
  1403. get_current_pseudonyms(conn);
  1404. break;
  1405. // PROOF COMMITMENT GETTERS
  1406. case PRSONA_GIVE_VOTE_ROW_COMMITMENT:
  1407. get_vote_row_commitment(conn, filename);
  1408. break;
  1409. case PRSONA_GIVE_VOTE_MATRIX_COMMITMENT:
  1410. get_vote_matrix_commitment(conn);
  1411. break;
  1412. case PRSONA_GIVE_CLIENT_TALLY_COMMITMENT:
  1413. get_user_tally_commitment(conn, filename);
  1414. break;
  1415. case PRSONA_GIVE_SERVER_TALLY_COMMITMENT:
  1416. get_server_tally_commitment(conn, filename);
  1417. break;
  1418. case PRSONA_GIVE_PSEUDONYMS_COMMITMENT:
  1419. get_pseudonyms_commitment(conn);
  1420. break;
  1421. // CLIENT INTERACTIONS
  1422. case PRSONA_RECEIVE_NEW_CLIENT:
  1423. add_new_client(conn, filename);
  1424. break;
  1425. case PRSONA_RECEIVE_VOTE:
  1426. receive_vote(server, conn, filename);
  1427. break;
  1428. // CLIENT INTERACTION HELPER
  1429. case PRSONA_RECEIVE_UPDATE_WITH_NEW_USER:
  1430. import_new_user_update(conn, filename);
  1431. break;
  1432. // CONSTRUCTOR HELPERS
  1433. case PRSONA_GIVE_BGN_PRIVKEY:
  1434. get_bgn_details(conn);
  1435. break;
  1436. case PRSONA_ADD_CURR_SEED_TO_GENERATOR:
  1437. add_seed_to_generator(conn, filename, true);
  1438. break;
  1439. case PRSONA_RECEIVE_FRESH_GENERATOR:
  1440. set_generator(filename, true);
  1441. break;
  1442. case PRSONA_ADD_RAND_SEED_TO_GENERATOR:
  1443. add_seed_to_generator(conn, filename, false);
  1444. break;
  1445. case PRSONA_RECEIVE_EG_BLIND_GENERATOR:
  1446. set_generator(filename, false);
  1447. break;
  1448. // EPOCH ROUNDS
  1449. case PRSONA_PERFORM_EPOCH_BUILD_UP:
  1450. build_up_midway_pseudonyms(server, conn, filename);
  1451. break;
  1452. case PRSONA_PERFORM_EPOCH_BREAK_DOWN:
  1453. break_down_midway_pseudonyms(server, conn, filename);
  1454. break;
  1455. case PRSONA_RECEIVE_EPOCH_UPDATE:
  1456. accept_epoch_updates(server, conn, filename);
  1457. break;
  1458. // DISTRIBUTED BGN
  1459. case PRSONA_GIVE_PARTIAL_DECRYPTION:
  1460. get_partial_decryption(conn);
  1461. break;
  1462. case PRSONA_RECEIVE_PARTIAL_DECRYPTION:
  1463. receive_tallied_scores(conn, filename);
  1464. break;
  1465. default:
  1466. break;
  1467. }
  1468. }
  1469. /*
  1470. * BASIC PUBLIC SYSTEM INFO GETTERS
  1471. */
  1472. void PrsonaServerWebSocketHandler::get_bgn_public_key(
  1473. struct mg_connection *conn) const
  1474. {
  1475. // Retrieve value
  1476. BGNPublicKey pubKey = prsonaServer->get_bgn_public_key();
  1477. // Serialize response
  1478. std::stringstream buffer;
  1479. std::string data;
  1480. buffer << pubKey;
  1481. data = buffer.str();
  1482. // Send response
  1483. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
  1484. }
  1485. void PrsonaServerWebSocketHandler::get_num_clients(
  1486. struct mg_connection *conn) const
  1487. {
  1488. // Retrieve value
  1489. BinarySizeT numClients(prsonaServer->get_num_clients());
  1490. // Serialize response
  1491. std::stringstream buffer;
  1492. std::string data;
  1493. buffer << numClients;
  1494. data = buffer.str();
  1495. // Send response
  1496. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
  1497. }
  1498. void PrsonaServerWebSocketHandler::get_num_servers(
  1499. struct mg_connection *conn) const
  1500. {
  1501. // Retrieve value
  1502. BinarySizeT numServers(prsonaServer->get_num_servers());
  1503. // Serialize response
  1504. std::stringstream buffer;
  1505. std::string data;
  1506. buffer << numServers;
  1507. data = buffer.str();
  1508. // Send response
  1509. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
  1510. }
  1511. void PrsonaServerWebSocketHandler::get_generator(
  1512. struct mg_connection *conn,
  1513. bool fresh)
  1514. {
  1515. // Retrieve value
  1516. Twistpoint generator;
  1517. std::vector<Proof> pi;
  1518. if (fresh)
  1519. generator = prsonaServer->get_fresh_generator(pi);
  1520. else
  1521. generator = prsonaServer->get_blinding_generator(pi);
  1522. // Serialize response
  1523. std::stringstream buffer;
  1524. std::string data;
  1525. BinarySizeT sizeOfVector(pi.size());
  1526. buffer << sizeOfVector;
  1527. for (size_t i = 0; i < sizeOfVector.val(); i++)
  1528. buffer << pi[i];
  1529. buffer << generator;
  1530. data = buffer.str();
  1531. // Send response
  1532. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
  1533. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
  1534. }
  1535. /*
  1536. * ENCRYPTED DATA GETTERS
  1537. */
  1538. void PrsonaServerWebSocketHandler::get_current_votes_by(
  1539. struct mg_connection *conn,
  1540. const char *filename) const
  1541. {
  1542. std::ifstream file(filename);
  1543. // Un-serialize request
  1544. Twistpoint shortTermPublicKey;
  1545. file >> shortTermPublicKey;
  1546. // Retrieve value
  1547. Proof pi;
  1548. std::vector<TwistBipoint> votes = prsonaServer->get_current_votes_by(pi, shortTermPublicKey);
  1549. // Serialize response
  1550. std::stringstream buffer;
  1551. std::string data;
  1552. buffer << pi;
  1553. BinarySizeT sizeOfVector(votes.size());
  1554. buffer << sizeOfVector;
  1555. for (size_t i = 0; i < sizeOfVector.val(); i++)
  1556. buffer << votes[i];
  1557. data = buffer.str();
  1558. // Send response
  1559. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
  1560. }
  1561. void PrsonaServerWebSocketHandler::get_all_current_votes(
  1562. struct mg_connection *conn) const
  1563. {
  1564. // Retrieve value
  1565. Proof pi;
  1566. std::vector<std::vector<TwistBipoint>> votes = prsonaServer->get_all_current_votes(pi);
  1567. // Serialize response
  1568. std::stringstream buffer;
  1569. std::string data;
  1570. buffer << pi;
  1571. BinarySizeT sizeOfVector(votes.size());
  1572. buffer << sizeOfVector;
  1573. for (size_t i = 0; i < sizeOfVector.val(); i++)
  1574. for (size_t j = 0; j < sizeOfVector.val(); j++)
  1575. buffer << votes[i][j];
  1576. data = buffer.str();
  1577. // Send response
  1578. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
  1579. }
  1580. void PrsonaServerWebSocketHandler::get_current_user_encrypted_tally(
  1581. struct mg_connection *conn,
  1582. const char *filename) const
  1583. {
  1584. std::ifstream file(filename);
  1585. // Un-serialize request
  1586. Twistpoint shortTermPublicKey;
  1587. file >> shortTermPublicKey;
  1588. // Retrieve value
  1589. Proof pi;
  1590. EGCiphertext tally = prsonaServer->get_current_user_encrypted_tally(pi, shortTermPublicKey);
  1591. // Serialize response
  1592. std::stringstream buffer;
  1593. std::string data;
  1594. buffer << pi;
  1595. buffer << tally;
  1596. data = buffer.str();
  1597. // Send response
  1598. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
  1599. }
  1600. void PrsonaServerWebSocketHandler::get_current_server_encrypted_tally(
  1601. struct mg_connection *conn,
  1602. const char *filename) const
  1603. {
  1604. std::ifstream file(filename);
  1605. // Un-serialize request
  1606. Twistpoint shortTermPublicKey;
  1607. file >> shortTermPublicKey;
  1608. // Retrieve value
  1609. Proof pi;
  1610. CurveBipoint tally = prsonaServer->get_current_server_encrypted_tally(pi, shortTermPublicKey);
  1611. // Serialize response
  1612. std::stringstream buffer;
  1613. std::string data;
  1614. buffer << pi;
  1615. buffer << tally;
  1616. data = buffer.str();
  1617. // Send response
  1618. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
  1619. }
  1620. void PrsonaServerWebSocketHandler::get_current_pseudonyms(
  1621. struct mg_connection *conn) const
  1622. {
  1623. // Retrieve value
  1624. Proof pi;
  1625. std::vector<Twistpoint> pseudonyms = prsonaServer->get_current_pseudonyms(pi);
  1626. // Serialize response
  1627. std::stringstream buffer;
  1628. std::string data;
  1629. buffer << pi;
  1630. BinarySizeT sizeOfVector(pseudonyms.size());
  1631. buffer << sizeOfVector;
  1632. for (size_t i = 0; i < sizeOfVector.val(); i++)
  1633. buffer << pseudonyms[i];
  1634. data = buffer.str();
  1635. // Send response
  1636. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
  1637. }
  1638. /*
  1639. * PROOF COMMITMENT GETTERS
  1640. */
  1641. void PrsonaServerWebSocketHandler::get_vote_row_commitment(
  1642. struct mg_connection *conn,
  1643. const char *filename) const
  1644. {
  1645. std::ifstream file(filename);
  1646. // Un-serialize request
  1647. Twistpoint shortTermPublicKey;
  1648. file >> shortTermPublicKey;
  1649. // Retrieve value
  1650. Proof pi = prsonaServer->get_vote_row_commitment(shortTermPublicKey);
  1651. // Serialize response
  1652. std::stringstream buffer;
  1653. std::string data;
  1654. buffer << pi;
  1655. data = buffer.str();
  1656. // Send response
  1657. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
  1658. }
  1659. void PrsonaServerWebSocketHandler::get_vote_matrix_commitment(
  1660. struct mg_connection *conn) const
  1661. {
  1662. // Retrieve value
  1663. Proof pi = prsonaServer->get_vote_matrix_commitment();
  1664. // Serialize response
  1665. std::stringstream buffer;
  1666. std::string data;
  1667. buffer << pi;
  1668. data = buffer.str();
  1669. // Send response
  1670. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
  1671. }
  1672. void PrsonaServerWebSocketHandler::get_user_tally_commitment(
  1673. struct mg_connection *conn,
  1674. const char *filename) const
  1675. {
  1676. std::ifstream file(filename);
  1677. // Un-serialize request
  1678. Twistpoint shortTermPublicKey;
  1679. file >> shortTermPublicKey;
  1680. // Retrieve value
  1681. Proof pi = prsonaServer->get_user_tally_commitment(shortTermPublicKey);
  1682. // Serialize response
  1683. std::stringstream buffer;
  1684. std::string data;
  1685. buffer << pi;
  1686. data = buffer.str();
  1687. // Send response
  1688. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
  1689. }
  1690. void PrsonaServerWebSocketHandler::get_server_tally_commitment(
  1691. struct mg_connection *conn,
  1692. const char *filename) const
  1693. {
  1694. std::ifstream file(filename);
  1695. // Un-serialize request
  1696. Twistpoint shortTermPublicKey;
  1697. file >> shortTermPublicKey;
  1698. // Retrieve value
  1699. Proof pi = prsonaServer->get_server_tally_commitment(shortTermPublicKey);
  1700. // Serialize response
  1701. std::stringstream buffer;
  1702. std::string data;
  1703. buffer << pi;
  1704. data = buffer.str();
  1705. // Send response
  1706. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
  1707. }
  1708. void PrsonaServerWebSocketHandler::get_pseudonyms_commitment(
  1709. struct mg_connection *conn) const
  1710. {
  1711. // Retrieve value
  1712. Proof pi = prsonaServer->get_pseudonyms_commitment();
  1713. // Serialize response
  1714. std::stringstream buffer;
  1715. std::string data;
  1716. buffer << pi;
  1717. data = buffer.str();
  1718. // Send response
  1719. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
  1720. }
  1721. /*
  1722. * CLIENT INTERACTIONS
  1723. */
  1724. void PrsonaServerWebSocketHandler::add_new_client(
  1725. struct mg_connection *conn,
  1726. const char *filename)
  1727. {
  1728. std::ifstream file(filename);
  1729. // Un-serialize request
  1730. Proof proofOfValidKey;
  1731. file >> proofOfValidKey;
  1732. Twistpoint shortTermPublicKey, empty;
  1733. file >> shortTermPublicKey;
  1734. std::vector<size_t> bandwidthData(2);
  1735. // Obtain global update lock
  1736. std::unique_lock<std::mutex> updateLock(updateMtx, std::defer_lock);
  1737. obtain_update_locks(updateLock, serverIPs, serverPorts, selfIP, selfPort, bandwidthData);
  1738. // Add new client to server object
  1739. std::vector<Proof> proofOfValidAddition;
  1740. prsonaServer->add_new_client(proofOfValidAddition, proofOfValidKey, shortTermPublicKey);
  1741. // Share this update with other servers
  1742. std::vector<CurveBipoint> previousVoteTallies;
  1743. std::vector<Twistpoint> currentPseudonyms;
  1744. std::vector<EGCiphertext> currentUserEncryptedTallies;
  1745. std::vector<std::vector<TwistBipoint>> voteMatrix;
  1746. prsonaServer->export_new_user_update(previousVoteTallies, currentPseudonyms, currentUserEncryptedTallies, voteMatrix);
  1747. distribute_new_user_updates(proofOfValidAddition, previousVoteTallies, currentPseudonyms, currentUserEncryptedTallies, voteMatrix);
  1748. // Release global update lock
  1749. release_update_locks(updateLock, serverIPs, serverPorts, selfIP, selfPort, bandwidthData);
  1750. // Serialize response
  1751. std::stringstream buffer;
  1752. std::string data;
  1753. BinarySizeT sizeOfVector(proofOfValidAddition.size());
  1754. buffer << sizeOfVector;
  1755. for (size_t i = 0; i < sizeOfVector.val(); i++)
  1756. buffer << proofOfValidAddition[i];
  1757. data = buffer.str();
  1758. // Send response
  1759. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
  1760. }
  1761. void PrsonaServerWebSocketHandler::receive_vote(
  1762. CivetServer *civetServer,
  1763. struct mg_connection *conn,
  1764. const char *filename)
  1765. {
  1766. std::ifstream file(filename);
  1767. file.ignore(std::numeric_limits<std::streamsize>::max());
  1768. std::streamsize bandwidthRcv = file.gcount();
  1769. file.clear();
  1770. file.seekg(0, std::ios_base::beg);
  1771. // Un-serialize request
  1772. BinarySizeT sizeOfVector;
  1773. file >> sizeOfVector;
  1774. std::vector<Proof> pi;
  1775. for (size_t i = 0; i < sizeOfVector.val(); i++)
  1776. {
  1777. Proof currProof;
  1778. file >> currProof;
  1779. pi.push_back(currProof);
  1780. }
  1781. file >> sizeOfVector;
  1782. std::vector<TwistBipoint> newVotes;
  1783. for (size_t i = 0; i < sizeOfVector.val(); i++)
  1784. {
  1785. TwistBipoint currVote;
  1786. file >> currVote;
  1787. newVotes.push_back(currVote);
  1788. }
  1789. Twistpoint shortTermPublicKey;
  1790. file >> shortTermPublicKey;
  1791. BinaryBool shouldDeal;
  1792. file >> shouldDeal;
  1793. // If we're dealing this update to the other servers, obtain global update lock
  1794. std::unique_lock<std::mutex> updateLock(updateMtx, std::defer_lock);
  1795. std::vector<size_t> bandwidthData(2);
  1796. std::vector<size_t> bandwidthDataBefore = get_server_log_data(civetServer->getContext());
  1797. std::chrono::high_resolution_clock::time_point wallTimeBefore = std::chrono::high_resolution_clock::now();
  1798. clock_t cpuTimeBefore = clock();
  1799. if (shouldDeal.val())
  1800. obtain_update_locks(updateLock, serverIPs, serverPorts, selfIP, selfPort, bandwidthData);
  1801. // Load votes into server object
  1802. prsonaServer->receive_vote(pi, newVotes, shortTermPublicKey);
  1803. // If we're dealing this update to the other servers, actually do that
  1804. if (shouldDeal.val())
  1805. {
  1806. distribute_new_vote(pi, newVotes, shortTermPublicKey, bandwidthData);
  1807. // Then release the global update lock
  1808. release_update_locks(updateLock, serverIPs, serverPorts, selfIP, selfPort, bandwidthData);
  1809. }
  1810. clock_t cpuTimeAfter = clock();
  1811. std::chrono::high_resolution_clock::time_point wallTimeAfter = std::chrono::high_resolution_clock::now();
  1812. std::vector<size_t> bandwidthDataAfter = get_server_log_data(civetServer->getContext());
  1813. std::vector<double> timingData(2);
  1814. timingData[0] = std::chrono::duration_cast<std::chrono::duration<double>>(wallTimeAfter - wallTimeBefore).count();
  1815. timingData[1] = ((double)(cpuTimeAfter - cpuTimeBefore)) / CLOCKS_PER_SEC;
  1816. bandwidthData[0] += bandwidthDataAfter[0] - bandwidthDataBefore[0] + bandwidthRcv;
  1817. bandwidthData[1] += bandwidthDataAfter[1] - bandwidthDataBefore[1];
  1818. write_log_data(voteOutputMtx, voteOutputFilename, timingData, bandwidthData);
  1819. write_usage_data(usageMtx, usageFilename);
  1820. // Notify client their request has been completed
  1821. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
  1822. }
  1823. /*
  1824. * DISTRIBUTION HELPERS FOR CLIENT INTERACTIONS
  1825. */
  1826. void PrsonaServerWebSocketHandler::distribute_new_user_updates(
  1827. const std::vector<Proof>& proofOfValidAddition,
  1828. const std::vector<CurveBipoint>& previousVoteTallies,
  1829. const std::vector<Twistpoint>& currentPseudonyms,
  1830. const std::vector<EGCiphertext>& currentUserEncryptedTallies,
  1831. const std::vector<std::vector<TwistBipoint>>& voteMatrix) const
  1832. {
  1833. struct synchronization_tool sync;
  1834. // Serialize data
  1835. std::stringstream buffer;
  1836. std::string data;
  1837. BinarySizeT sizeOfVector;
  1838. sizeOfVector.set(proofOfValidAddition.size());
  1839. buffer << sizeOfVector;
  1840. for (size_t i = 0; i < sizeOfVector.val(); i++)
  1841. buffer << proofOfValidAddition[i];
  1842. sizeOfVector.set(previousVoteTallies.size());
  1843. buffer << sizeOfVector;
  1844. for (size_t i = 0; i < sizeOfVector.val(); i++)
  1845. buffer << previousVoteTallies[i];
  1846. sizeOfVector.set(currentPseudonyms.size());
  1847. buffer << sizeOfVector;
  1848. for (size_t i = 0; i < sizeOfVector.val(); i++)
  1849. buffer << currentPseudonyms[i];
  1850. sizeOfVector.set(currentUserEncryptedTallies.size());
  1851. buffer << sizeOfVector;
  1852. for (size_t i = 0; i < sizeOfVector.val(); i++)
  1853. buffer << currentUserEncryptedTallies[i];
  1854. sizeOfVector.set(voteMatrix.size());
  1855. buffer << sizeOfVector;
  1856. for (size_t i = 0; i < sizeOfVector.val(); i++)
  1857. for (size_t j = 0; j < sizeOfVector.val(); j++)
  1858. buffer << voteMatrix[i][j];
  1859. data = buffer.str();
  1860. // Connect to each server and give them data on the new user
  1861. size_t i = 0;
  1862. while (i < serverIPs.size())
  1863. {
  1864. if (serverIPs[i] == selfIP && serverPorts[i] == selfPort)
  1865. {
  1866. i++;
  1867. continue;
  1868. }
  1869. struct mg_connection *conn = NULL;
  1870. std::unique_lock<std::mutex> lck(sync.mtx);
  1871. sync.val = 0;
  1872. sync.val2 = 0;
  1873. while (!conn)
  1874. {
  1875. conn = mg_connect_websocket_client(serverIPs[i].c_str(), serverPorts[i], USE_SSL, NULL, 0, SUBMIT_UPDATE_WITH_NEW_USER_URI, "null", synchro_websocket_data_handler, synchro_websocket_close_handler, &sync);
  1876. if (!conn)
  1877. std::cerr << "Couldn't give server " << i << " new user" << std::endl;
  1878. }
  1879. // Send the server the new user data
  1880. mg_websocket_client_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
  1881. mg_websocket_client_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
  1882. // Wait for its response (or the connection to die)
  1883. while (!sync.val2)
  1884. sync.cv.wait(lck);
  1885. // Close connection
  1886. mg_close_connection(conn);
  1887. // Only move forward once we've confirmed we correctly gave the update (or else we risk synchronization issues!)
  1888. if (sync.val)
  1889. i++;
  1890. }
  1891. }
  1892. void PrsonaServerWebSocketHandler::distribute_new_vote(
  1893. std::vector<Proof> pi,
  1894. std::vector<TwistBipoint> newVotes,
  1895. Twistpoint shortTermPublicKey,
  1896. std::vector<size_t>& bandwidthData) const
  1897. {
  1898. struct synchronization_tool sync;
  1899. // Serialize data
  1900. std::stringstream buffer;
  1901. std::string data;
  1902. BinarySizeT sizeOfVector;
  1903. sizeOfVector.set(pi.size());
  1904. buffer << sizeOfVector;
  1905. for (size_t i = 0; i < sizeOfVector.val(); i++)
  1906. buffer << pi[i];
  1907. sizeOfVector.set(newVotes.size());
  1908. buffer << sizeOfVector;
  1909. for (size_t i = 0; i < sizeOfVector.val(); i++)
  1910. buffer << newVotes[i];
  1911. buffer << shortTermPublicKey;
  1912. BinaryBool flag(false);
  1913. buffer << flag;
  1914. data = buffer.str();
  1915. // Connect to each server and give them the new votes
  1916. size_t i = 0;
  1917. while (i < serverIPs.size())
  1918. {
  1919. if (serverIPs[i] == selfIP && serverPorts[i] == selfPort)
  1920. {
  1921. i++;
  1922. continue;
  1923. }
  1924. struct mg_connection *conn = NULL;
  1925. std::unique_lock<std::mutex> syncLock(sync.mtx);
  1926. sync.val = 0;
  1927. sync.val2 = 0;
  1928. while (!conn)
  1929. {
  1930. conn = mg_connect_websocket_client(serverIPs[i].c_str(), serverPorts[i], USE_SSL, NULL, 0, SUBMIT_VOTE_URI, "null", synchro_websocket_data_handler, synchro_websocket_close_handler, &sync);
  1931. if (!conn)
  1932. std::cerr << "Couldn't give server " << i << " new vote data" << std::endl;
  1933. }
  1934. std::vector<size_t> bandwidthDataBefore = get_conn_log_data(mg_get_context(conn), false);
  1935. // Send the server the new vote data
  1936. mg_websocket_client_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
  1937. mg_websocket_client_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
  1938. // Wait for its response (or the connection to die)
  1939. while (!sync.val2)
  1940. sync.cv.wait(syncLock);
  1941. std::vector<size_t> bandwidthDataAfter = get_conn_log_data(mg_get_context(conn), false);
  1942. bandwidthData[0] += bandwidthDataAfter[0] - bandwidthDataBefore[0];
  1943. bandwidthData[1] += bandwidthDataAfter[1] - bandwidthDataBefore[1];
  1944. // Close connection
  1945. mg_close_connection(conn);
  1946. // Only move forward once we've confirmed we correctly gave the update (or else we risk synchronization issues!)
  1947. if (sync.val)
  1948. i++;
  1949. }
  1950. }
  1951. void PrsonaServerWebSocketHandler::import_new_user_update(
  1952. struct mg_connection *conn,
  1953. const char *filename)
  1954. {
  1955. std::vector<Proof> proofOfValidAddition;
  1956. std::vector<CurveBipoint> previousVoteTallies;
  1957. std::vector<Twistpoint> currentPseudonyms;
  1958. std::vector<EGCiphertext> currentUserEncryptedTallies;
  1959. std::vector<std::vector<TwistBipoint>> voteMatrix;
  1960. std::ifstream file(filename);
  1961. // Un-serialize request
  1962. BinarySizeT sizeOfVector;
  1963. file >> sizeOfVector;
  1964. for (size_t i = 0; i < sizeOfVector.val(); i++)
  1965. {
  1966. Proof currProof;
  1967. file >> currProof;
  1968. proofOfValidAddition.push_back(currProof);
  1969. }
  1970. file >> sizeOfVector;
  1971. for (size_t i = 0; i < sizeOfVector.val(); i++)
  1972. {
  1973. CurveBipoint currTally;
  1974. file >> currTally;
  1975. previousVoteTallies.push_back(currTally);
  1976. }
  1977. file >> sizeOfVector;
  1978. for (size_t i = 0; i < sizeOfVector.val(); i++)
  1979. {
  1980. Twistpoint currNym;
  1981. file >> currNym;
  1982. currentPseudonyms.push_back(currNym);
  1983. }
  1984. file >> sizeOfVector;
  1985. for (size_t i = 0; i < sizeOfVector.val(); i++)
  1986. {
  1987. EGCiphertext currTally;
  1988. file >> currTally;
  1989. currentUserEncryptedTallies.push_back(currTally);
  1990. }
  1991. file >> sizeOfVector;
  1992. for (size_t i = 0; i < sizeOfVector.val(); i++)
  1993. {
  1994. std::vector<TwistBipoint> currRow;
  1995. for (size_t j = 0; j < sizeOfVector.val(); j++)
  1996. {
  1997. TwistBipoint currVote;
  1998. file >> currVote;
  1999. currRow.push_back(currVote);
  2000. }
  2001. voteMatrix.push_back(currRow);
  2002. }
  2003. // Load data into server object
  2004. prsonaServer->import_new_user_update(proofOfValidAddition, previousVoteTallies, currentPseudonyms, currentUserEncryptedTallies, voteMatrix);
  2005. // Acknowledge successful request
  2006. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
  2007. }
  2008. /*
  2009. * CONSTRUCTOR HELPERS
  2010. */
  2011. void PrsonaServerWebSocketHandler::get_bgn_details(
  2012. struct mg_connection *conn) const
  2013. {
  2014. // Retrieve value
  2015. const BGN& sharedBGN = prsonaServer->get_bgn_details();
  2016. // Serialize response
  2017. std::stringstream buffer;
  2018. std::string data;
  2019. buffer << sharedBGN;
  2020. data = buffer.str();
  2021. // Send response
  2022. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
  2023. }
  2024. void PrsonaServerWebSocketHandler::add_seed_to_generator(
  2025. struct mg_connection *conn,
  2026. const char *filename,
  2027. bool fresh) const
  2028. {
  2029. std::ifstream file(filename);
  2030. // Un-serialize request
  2031. Twistpoint currGenerator;
  2032. file >> currGenerator;
  2033. // Retrieve value
  2034. std::vector<Proof> pi;
  2035. if (fresh)
  2036. currGenerator = prsonaServer->add_curr_seed_to_generator(pi, currGenerator);
  2037. else
  2038. currGenerator = prsonaServer->add_rand_seed_to_generator(pi, currGenerator);
  2039. // Serialize response
  2040. std::stringstream buffer;
  2041. std::string data;
  2042. buffer << pi[0];
  2043. buffer << currGenerator;
  2044. data = buffer.str();
  2045. // Send response
  2046. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
  2047. }
  2048. void PrsonaServerWebSocketHandler::set_generator(
  2049. const char *filename,
  2050. bool fresh)
  2051. {
  2052. std::ifstream file(filename);
  2053. // Un-serialize request
  2054. BinarySizeT sizeOfVector;
  2055. file >> sizeOfVector;
  2056. std::vector<Proof> pi;
  2057. for (size_t i = 0; i < sizeOfVector.val(); i++)
  2058. {
  2059. Proof currProof;
  2060. file >> currProof;
  2061. pi.push_back(currProof);
  2062. }
  2063. Twistpoint newGenerator;
  2064. file >> newGenerator;
  2065. // Load value into server object
  2066. if (fresh)
  2067. prsonaServer->initialize_fresh_generator(pi, newGenerator);
  2068. else
  2069. prsonaServer->set_EG_blind_generator(pi, newGenerator);
  2070. }
  2071. /*
  2072. * EPOCH ROUNDS
  2073. */
  2074. void PrsonaServerWebSocketHandler::build_up_midway_pseudonyms(
  2075. CivetServer *civetServer,
  2076. struct mg_connection *conn,
  2077. const char *filename)
  2078. {
  2079. std::vector<std::vector<Proof>> generatorProofHolder;
  2080. std::vector<Proof> generatorProof;
  2081. Twistpoint nextGenerator;
  2082. // Un-serialize request
  2083. ssize_t bandwidthRcv = read_epoch_initiator_string(filename, generatorProof, nextGenerator);
  2084. generatorProofHolder.push_back(generatorProof);
  2085. std::vector<std::vector<std::vector<Proof>>> pi;
  2086. pi.push_back(generatorProofHolder);
  2087. std::vector<std::vector<std::vector<Twistpoint>>> permutationCommits;
  2088. std::vector<std::vector<std::vector<Twistpoint>>> freshPseudonymCommits;
  2089. std::vector<std::vector<std::vector<Twistpoint>>> freshPseudonymSeedCommits;
  2090. std::vector<std::vector<std::vector<CurveBipoint>>> serverTallyCommits;
  2091. std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>> partwayVoteMatrixCommits;
  2092. std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>> finalVoteMatrixCommits;
  2093. std::vector<size_t> bandwidthData(2);
  2094. std::vector<std::vector<size_t>> otherBandwidthDataBefore;
  2095. std::vector<size_t> serverBandwidthDataBefore = get_server_log_data(civetServer->getContext());
  2096. std::chrono::high_resolution_clock::time_point wallTimeBefore = std::chrono::high_resolution_clock::now();
  2097. clock_t cpuTimeBefore = clock();
  2098. // Do actual epoch calculation
  2099. prsonaServer->build_up_midway_pseudonyms(pi, permutationCommits, freshPseudonymCommits, freshPseudonymSeedCommits, serverTallyCommits, partwayVoteMatrixCommits, finalVoteMatrixCommits, nextGenerator);
  2100. std::vector<std::vector<Twistpoint>> userTallyMaskCommits, userTallyMessageCommits, userTallySeedCommits;
  2101. // Serialize update data
  2102. std::string data = make_epoch_update_string(pi[1], permutationCommits[0], freshPseudonymCommits[0], freshPseudonymSeedCommits[0], serverTallyCommits[0], partwayVoteMatrixCommits[0], finalVoteMatrixCommits[0], userTallyMaskCommits, userTallyMessageCommits, userTallySeedCommits, nextGenerator, false);
  2103. struct synchronization_tool sync;
  2104. std::vector<struct mg_connection *> conns;
  2105. // Connect to all other servers (roughly in parallel)
  2106. std::unique_lock<std::mutex> lck(sync.mtx);
  2107. sync.val = 1;
  2108. for (size_t i = 0; i < serverIPs.size(); i++)
  2109. {
  2110. if (serverIPs[i] == selfIP && serverPorts[i] == selfPort)
  2111. continue;
  2112. // Send them update data
  2113. struct mg_connection *currConn = distribute_epoch_updates(serverIPs[i], serverPorts[i], data, &sync);
  2114. conns.push_back(currConn);
  2115. otherBandwidthDataBefore.push_back(get_conn_log_data(mg_get_context(currConn), false));
  2116. }
  2117. // Wait for all to acknowledge receipt of the update data
  2118. while (sync.val < serverIPs.size())
  2119. sync.cv.wait(lck);
  2120. // Close connections
  2121. for (size_t i = 0; i < conns.size(); i++)
  2122. {
  2123. std::vector<size_t> currBandwidthDataAfter = get_conn_log_data(mg_get_context(conns[i]), false);
  2124. bandwidthData[0] += currBandwidthDataAfter[0] - otherBandwidthDataBefore[i][0];
  2125. bandwidthData[1] += currBandwidthDataAfter[1] - otherBandwidthDataBefore[i][1];
  2126. mg_close_connection(conns[i]);
  2127. }
  2128. clock_t cpuTimeAfter = clock();
  2129. std::chrono::high_resolution_clock::time_point wallTimeAfter = std::chrono::high_resolution_clock::now();
  2130. std::vector<size_t> serverBandwidthDataAfter = get_server_log_data(civetServer->getContext());
  2131. std::vector<double> timingData(2);
  2132. timingData[0] = std::chrono::duration_cast<std::chrono::duration<double>>(wallTimeAfter - wallTimeBefore).count();
  2133. timingData[1] = ((double)(cpuTimeAfter - cpuTimeBefore)) / CLOCKS_PER_SEC;
  2134. bandwidthData[0] += serverBandwidthDataAfter[0] - serverBandwidthDataBefore[0] + bandwidthRcv;
  2135. bandwidthData[1] += serverBandwidthDataAfter[1] - serverBandwidthDataBefore[1];
  2136. write_log_data(buildUpOutputMtx, buildUpOutputFilename, timingData, bandwidthData);
  2137. write_usage_data(usageMtx, usageFilename);
  2138. // Serialize response
  2139. data = make_epoch_initiator_string(pi[0][0], nextGenerator);
  2140. // Send response
  2141. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
  2142. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
  2143. }
  2144. void PrsonaServerWebSocketHandler::break_down_midway_pseudonyms(
  2145. CivetServer *civetServer,
  2146. struct mg_connection *conn,
  2147. const char *filename)
  2148. {
  2149. std::vector<Proof> generatorProof;
  2150. Twistpoint nextGenerator;
  2151. // Un-serialize request
  2152. ssize_t bandwidthRcv = read_epoch_initiator_string(filename, generatorProof, nextGenerator);
  2153. std::vector<std::vector<std::vector<Proof>>> pi;
  2154. std::vector<std::vector<std::vector<Twistpoint>>> permutationCommits;
  2155. std::vector<std::vector<std::vector<Twistpoint>>> freshPseudonymCommits;
  2156. std::vector<std::vector<std::vector<Twistpoint>>> freshPseudonymSeedCommits;
  2157. std::vector<std::vector<std::vector<CurveBipoint>>> serverTallyCommits;
  2158. std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>> partwayVoteMatrixCommits;
  2159. std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>> finalVoteMatrixCommits;
  2160. std::vector<std::vector<std::vector<Twistpoint>>> userTallyMaskCommits;
  2161. std::vector<std::vector<std::vector<Twistpoint>>> userTallyMessageCommits;
  2162. std::vector<std::vector<std::vector<Twistpoint>>> userTallySeedCommits;
  2163. std::vector<size_t> bandwidthData(2);
  2164. std::vector<std::vector<size_t>> otherBandwidthDataBefore;
  2165. std::vector<size_t> serverBandwidthDataBefore = get_server_log_data(civetServer->getContext());
  2166. std::chrono::high_resolution_clock::time_point wallTimeBefore = std::chrono::high_resolution_clock::now();
  2167. clock_t cpuTimeBefore = clock();
  2168. // Do actual epoch calculation
  2169. prsonaServer->break_down_midway_pseudonyms(generatorProof, pi, permutationCommits, freshPseudonymCommits, freshPseudonymSeedCommits, serverTallyCommits, partwayVoteMatrixCommits, finalVoteMatrixCommits, userTallyMaskCommits, userTallyMessageCommits, userTallySeedCommits, nextGenerator);
  2170. // Serialize update data
  2171. std::string data = make_epoch_update_string(pi[0], permutationCommits[0], freshPseudonymCommits[0], freshPseudonymSeedCommits[0], serverTallyCommits[0], partwayVoteMatrixCommits[0], finalVoteMatrixCommits[0], userTallyMaskCommits[0], userTallyMessageCommits[0], userTallySeedCommits[0], nextGenerator, true);
  2172. struct synchronization_tool sync;
  2173. std::vector<struct mg_connection *> conns;
  2174. // Connect to all other servers (roughly in parallel)
  2175. std::unique_lock<std::mutex> lck(sync.mtx);
  2176. sync.val = 1;
  2177. for (size_t i = 0; i < serverIPs.size(); i++)
  2178. {
  2179. if (serverIPs[i] == selfIP && serverPorts[i] == selfPort)
  2180. continue;
  2181. // Send them update data
  2182. struct mg_connection *currConn = distribute_epoch_updates(serverIPs[i], serverPorts[i], data, &sync);
  2183. conns.push_back(currConn);
  2184. otherBandwidthDataBefore.push_back(get_conn_log_data(mg_get_context(currConn), false));
  2185. }
  2186. // Wait for all to acknowledge receipt of the update data
  2187. while (sync.val < serverIPs.size())
  2188. sync.cv.wait(lck);
  2189. // Close connections
  2190. for (size_t i = 0; i < conns.size(); i++)
  2191. {
  2192. std::vector<size_t> currBandwidthDataAfter = get_conn_log_data(mg_get_context(conns[i]), false);
  2193. bandwidthData[0] += currBandwidthDataAfter[0] - otherBandwidthDataBefore[i][0];
  2194. bandwidthData[1] += currBandwidthDataAfter[1] - otherBandwidthDataBefore[i][1];
  2195. mg_close_connection(conns[i]);
  2196. }
  2197. clock_t cpuTimeAfter = clock();
  2198. std::chrono::high_resolution_clock::time_point wallTimeAfter = std::chrono::high_resolution_clock::now();
  2199. std::vector<size_t> serverBandwidthDataAfter = get_server_log_data(civetServer->getContext());
  2200. std::vector<double> timingData(2);
  2201. timingData[0] = std::chrono::duration_cast<std::chrono::duration<double>>(wallTimeAfter - wallTimeBefore).count();
  2202. timingData[1] = ((double)(cpuTimeAfter - cpuTimeBefore)) / CLOCKS_PER_SEC;
  2203. bandwidthData[0] += serverBandwidthDataAfter[0] - serverBandwidthDataBefore[0] + bandwidthRcv;
  2204. bandwidthData[1] += serverBandwidthDataAfter[1] - serverBandwidthDataBefore[1];
  2205. write_log_data(breakDownOutputMtx, breakDownOutputFilename, timingData, bandwidthData);
  2206. write_usage_data(usageMtx, usageFilename);
  2207. // Keep our epoch value up-to-date
  2208. epochNum.fetch_add(1);
  2209. // Tell initiator we have finished
  2210. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
  2211. }
  2212. void PrsonaServerWebSocketHandler::accept_epoch_updates(
  2213. CivetServer *civetServer,
  2214. struct mg_connection *conn,
  2215. const char *filename)
  2216. {
  2217. std::vector<std::vector<Proof>> pi;
  2218. std::vector<std::vector<Twistpoint>> permutationCommits;
  2219. std::vector<std::vector<Twistpoint>> freshPseudonymCommits;
  2220. std::vector<std::vector<Twistpoint>> freshPseudonymSeedCommits;
  2221. std::vector<std::vector<CurveBipoint>> serverTallyCommits;
  2222. std::vector<std::vector<std::vector<TwistBipoint>>> partwayVoteMatrixCommits;
  2223. std::vector<std::vector<std::vector<TwistBipoint>>> finalVoteMatrixCommits;
  2224. std::vector<std::vector<Twistpoint>> userTallyMaskCommits;
  2225. std::vector<std::vector<Twistpoint>> userTallyMessageCommits;
  2226. std::vector<std::vector<Twistpoint>> userTallySeedCommits;
  2227. Twistpoint nextGenerator;
  2228. bool doUserTallies;
  2229. // Un-serialize request
  2230. ssize_t bandwidthRcv = read_epoch_update_string(filename, pi, permutationCommits, freshPseudonymCommits, freshPseudonymSeedCommits, serverTallyCommits, partwayVoteMatrixCommits, finalVoteMatrixCommits, userTallyMaskCommits, userTallyMessageCommits, userTallySeedCommits, nextGenerator, doUserTallies);
  2231. std::vector<size_t> bandwidthDataBefore = get_server_log_data(civetServer->getContext());
  2232. std::chrono::high_resolution_clock::time_point wallTimeBefore = std::chrono::high_resolution_clock::now();
  2233. clock_t cpuTimeBefore = clock();
  2234. // Load data into server object
  2235. prsonaServer->accept_epoch_updates(pi, permutationCommits, freshPseudonymCommits, freshPseudonymSeedCommits, serverTallyCommits, partwayVoteMatrixCommits, finalVoteMatrixCommits, userTallyMaskCommits, userTallyMessageCommits, userTallySeedCommits, nextGenerator, doUserTallies);
  2236. clock_t cpuTimeAfter = clock();
  2237. std::chrono::high_resolution_clock::time_point wallTimeAfter = std::chrono::high_resolution_clock::now();
  2238. std::vector<size_t> bandwidthDataAfter = get_server_log_data(civetServer->getContext());
  2239. std::vector<double> timingData(2);
  2240. timingData[0] = std::chrono::duration_cast<std::chrono::duration<double>>(wallTimeAfter - wallTimeBefore).count();
  2241. timingData[1] = ((double)(cpuTimeAfter - cpuTimeBefore)) / CLOCKS_PER_SEC;
  2242. std::vector<size_t> bandwidthData(2);
  2243. bandwidthData[0] = bandwidthDataAfter[0] - bandwidthDataBefore[0] + bandwidthRcv;
  2244. bandwidthData[1] = bandwidthDataAfter[1] - bandwidthDataBefore[1];
  2245. write_log_data(updateOutputMtx, updateOutputFilename, timingData, bandwidthData);
  2246. write_usage_data(usageMtx, usageFilename);
  2247. // Acknowledge receipt of request
  2248. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
  2249. }
  2250. /*
  2251. * DISTRIBUTED BGN
  2252. */
  2253. void PrsonaServerWebSocketHandler::get_partial_decryption(
  2254. struct mg_connection *conn) const
  2255. {
  2256. // Send back ACK (since all we do here is simulate distributed BGN)
  2257. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
  2258. }
  2259. void PrsonaServerWebSocketHandler::receive_tallied_scores(
  2260. struct mg_connection *conn,
  2261. const char *filename)
  2262. {
  2263. std::ifstream file(filename);
  2264. // Un-serialize request
  2265. std::vector<EGCiphertext> userScores;
  2266. std::vector<CurveBipoint> serverScores;
  2267. BinarySizeT sizeOfVector;
  2268. file >> sizeOfVector;
  2269. for (size_t i = 0; i < sizeOfVector.val(); i++)
  2270. {
  2271. EGCiphertext currScore;
  2272. file >> currScore;
  2273. userScores.push_back(currScore);
  2274. }
  2275. for (size_t i = 0; i < sizeOfVector.val(); i++)
  2276. {
  2277. CurveBipoint currScore;
  2278. file >> currScore;
  2279. serverScores.push_back(currScore);
  2280. }
  2281. // Load into server object
  2282. prsonaServer->receive_tallied_scores(userScores, serverScores);
  2283. // Acknowledge receipt of data
  2284. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
  2285. }