networkServer.cpp 95 KB

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