networkServer.cpp 118 KB

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