networkServer.cpp 90 KB

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