networkServer.cpp 95 KB

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