12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840 |
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <ctime>
- #include <chrono>
- #include "networkServer.hpp"
- /********************************************************
- ********* *********
- ********* server networking public functions *********
- ********* *********
- ********************************************************/
- /*
- * CREATOR FOR A NEW SERVER
- */
- PrsonaServer *create_server(
- std::default_random_engine& rng,
- std::string dealerIP,
- int dealerPort,
- bool bgnDealer,
- size_t numServers)
- {
- // We simulate the distributed BGN key generation; in our case, the dealer makes the BGN individually and shares it with other servers
- if (bgnDealer)
- return new PrsonaServer(numServers);
- // If we're not the dealer, get the BGN private key from the dealer
- BGN privateKey = get_bgn_private_key(rng, dealerIP, dealerPort);
- // And make a server object accounting for that
- return new PrsonaServer(numServers, privateKey);
- }
- /*
- * CHECK IN FUNCTION USED FOR SYNCHRONIZATION IN SETUP
- */
- void check_in_with_dealer(
- std::string dealerIP,
- int dealerPort)
- {
- std::stringstream buffer;
- std::string data;
- // The actual check in process is very simple; just make the correct GET request
- buffer << "GET " << SERVER_CHECK_IN_URI << " HTTP/1.1\r\n";
- buffer << "Host: " << dealerIP << ":" << dealerPort << "\r\n\r\n";
- data = buffer.str();
- // This also means things are slightly less cumbersome than making a websocket connection would be
- struct mg_connection *conn = mg_connect_client(dealerIP.c_str(), dealerPort, USE_SSL, NULL, 0);
- // Make the actual GET request
- mg_write(conn, data.c_str(), data.length());
- // We don't really care about the response, so we can just ignore it
- mg_close_connection(conn);
- }
- /*
- * INITIATER FOR SHARED GLOBAL VALUES
- */
- void initiate_generators(
- std::default_random_engine& rng,
- PrsonaServer* prsonaServer,
- const std::vector<std::string>& serverIPs,
- const std::vector<int>& serverPorts,
- const std::string& selfIP,
- int selfPort)
- {
- // Form and distribute the first fresh generator
- std::vector<Proof> pi;
- Twistpoint freshGenerator = make_generator(rng, prsonaServer, serverIPs, serverPorts, selfIP, selfPort, true, pi);
- distribute_generator(prsonaServer, serverIPs, serverPorts, selfIP, selfPort, true, pi, freshGenerator);
- // Form and distribute the H used in ElGamal operations
- Twistpoint blindGenerator = make_generator(rng, prsonaServer, serverIPs, serverPorts, selfIP, selfPort, false, pi);
- distribute_generator(prsonaServer, serverIPs, serverPorts, selfIP, selfPort, false, pi, blindGenerator);
- }
- /*
- * FUNCTION TO PERFORM OPERATIONS FOR EXPERIMENT
- */
- void make_epoch(
- std::default_random_engine& rng,
- PrsonaServer *prsonaServer,
- const std::vector<std::string>& serverIPs,
- const std::vector<int>& serverPorts,
- const std::string& selfIP,
- int selfPort,
- std::mutex& updateMtx,
- std::atomic<size_t>& epochNum,
- const CivetServer& civetServer,
- std::mutex& buildUpOutputMtx,
- const std::string& buildUpOutputFilename,
- std::mutex& breakDownOutputMtx,
- const std::string& breakDownOutputFilename,
- std::mutex& fullOutputMtx,
- const std::string& fullOutputFilename,
- std::mutex& usageMtx,
- const std::string& usageFilename)
- {
- // As before, the fresh generator always starts from the same G
- Twistpoint nextGenerator = PrsonaServer::EL_GAMAL_GENERATOR;
- std::vector<size_t> bandwidthData(2);
- std::unique_lock<std::mutex> updateLock(updateMtx, std::defer_lock);
- std::vector<size_t> bandwidthDataBefore = get_server_log_data(civetServer.getContext());
- std::chrono::high_resolution_clock::time_point wallTimeBefore = std::chrono::high_resolution_clock::now();
- clock_t cpuTimeBefore = clock();
- // Take update locks on every machine
- obtain_update_locks(updateLock, serverIPs, serverPorts, selfIP, selfPort, bandwidthData);
- // Do the first half of the epoch calculations (building up the intermediary values)
- std::vector<Proof> generatorProof = epoch_build_up(rng, prsonaServer, serverIPs, serverPorts, selfIP, selfPort, nextGenerator, civetServer, buildUpOutputMtx, buildUpOutputFilename, usageMtx, usageFilename, bandwidthData);
- // Tally up the current scores at the end of the epoch for the users
- std::vector<EGCiphertext> currentUserEncryptedTallies;
- std::vector<CurveBipoint> currentServerEncryptedTallies;
- tally_scores(prsonaServer, serverIPs, serverPorts, selfIP, selfPort, nextGenerator, currentUserEncryptedTallies, currentServerEncryptedTallies, bandwidthData);
- // And distribute these to each server
- distribute_tallied_scores(prsonaServer, serverIPs, serverPorts, selfIP, selfPort, nextGenerator, currentUserEncryptedTallies, currentServerEncryptedTallies, bandwidthData);
- // Do the second half of the epoch calculations (breaking down values to their final values, to be given to users)
- epoch_break_down(rng, prsonaServer, serverIPs, serverPorts, selfIP, selfPort, generatorProof, nextGenerator, civetServer, breakDownOutputMtx, breakDownOutputFilename, usageMtx, usageFilename, bandwidthData);
- // Indicate we are in a new epoch
- epochNum.fetch_add(1);
- // Release the update locks from every machine
- release_update_locks(updateLock, serverIPs, serverPorts, selfIP, selfPort, bandwidthData);
-
- clock_t cpuTimeAfter = clock();
- std::chrono::high_resolution_clock::time_point wallTimeAfter = std::chrono::high_resolution_clock::now();
- std::vector<size_t> bandwidthDataAfter = get_server_log_data(civetServer.getContext());
- std::vector<double> timingData(2);
- timingData[0] = std::chrono::duration_cast<std::chrono::duration<double>>(wallTimeAfter - wallTimeBefore).count();
- timingData[1] = ((double)(cpuTimeAfter - cpuTimeBefore)) / CLOCKS_PER_SEC;
- bandwidthData[0] += bandwidthDataAfter[0] - bandwidthDataBefore[0];
- bandwidthData[1] += bandwidthDataAfter[1] - bandwidthDataBefore[1];
- write_log_data(fullOutputMtx, fullOutputFilename, timingData, bandwidthData);
- write_usage_data(usageMtx, usageFilename);
- }
- /*********************************************************
- ********* *********
- ********* server networking private functions *********
- ********* *********
- *********************************************************/
- /*
- * SHARED GLOBAL UPDATE LOCK GETTERS AND RELEASERS
- */
- void obtain_update_locks(
- std::unique_lock<std::mutex> &updateLock,
- const std::vector<std::string>& serverIPs,
- const std::vector<int>& serverPorts,
- const std::string& selfIP,
- int selfPort,
- std::vector<size_t>& bandwidthData)
- {
- // Get locks on each machine (in a predetermined order, defined universally for all servers)
- size_t i = 0;
- while (i < serverIPs.size())
- {
- // When it's our turn, it's easy to take the lock
- if (serverIPs[i] == selfIP && serverPorts[i] == selfPort)
- {
- updateLock.lock();
- i++;
- continue;
- }
- struct synchronization_tool sync;
- struct mg_connection *conn = NULL;
- // Connect to the server
- std::unique_lock<std::mutex> lck(sync.mtx);
- sync.val = 0;
- sync.val2 = 0;
- while (!conn)
- {
- 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);
- if (!conn)
- std::cerr << "Couldn't connect to server " << i << " to obtain its lock" << std::endl;
- }
- std::vector<size_t> bandwidthDataBefore = get_conn_log_data(mg_get_context(conn), true);
- // Ask for its lock
- mg_websocket_client_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
- // Wait for its response (or the connection to die)
- while (!sync.val2)
- sync.cv.wait(lck);
- std::vector<size_t> bandwidthDataAfter = get_conn_log_data(mg_get_context(conn), true);
- bandwidthData[0] += bandwidthDataAfter[0] - bandwidthDataBefore[0];
- bandwidthData[1] += bandwidthDataAfter[1] - bandwidthDataBefore[1];
- // Close connection
- mg_close_connection(conn);
- // Only move forward once we've confirmed we have the lock (or else we risk deadlock!)
- if (sync.val)
- i++;
- }
- }
- void release_update_locks(
- std::unique_lock<std::mutex> &updateLock,
- const std::vector<std::string>& serverIPs,
- const std::vector<int>& serverPorts,
- const std::string& selfIP,
- int selfPort,
- std::vector<size_t>& bandwidthData)
- {
- // Release locks on each machine (in the opposite of the predetermined order we used to take them)
- ssize_t i = serverIPs.size() - 1;
- while (i >= 0)
- {
- // When it's our turn, it's easy to release the lock
- if (serverIPs[i] == selfIP && serverPorts[i] == selfPort)
- {
- updateLock.unlock();
- i--;
- continue;
- }
- struct synchronization_tool sync;
- struct mg_connection *conn = NULL;
- // Connect to the server
- std::unique_lock<std::mutex> lck(sync.mtx);
- sync.val = 0;
- sync.val2 = 0;
- while (!conn)
- {
- 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);
- if (!conn)
- std::cerr << "Couldn't connect to server " << i << " to release its lock" << std::endl;
- }
- std::vector<size_t> bandwidthDataBefore = get_conn_log_data(mg_get_context(conn), true);
- // Return its lock
- mg_websocket_client_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
- // Wait for its response (or the connection to die)
- while (!sync.val2)
- sync.cv.wait(lck);
- std::vector<size_t> bandwidthDataAfter = get_conn_log_data(mg_get_context(conn), true);
- bandwidthData[0] += bandwidthDataAfter[0] - bandwidthDataBefore[0];
- bandwidthData[1] += bandwidthDataAfter[1] - bandwidthDataBefore[1];
- // Close connection
- mg_close_connection(conn);
- // Only move forward once we've confirmed we released the lock (or else we risk deadlock!)
- if (sync.val)
- i--;
- }
- }
- /*
- * GETTER FOR DEALER VALUE
- */
- BGN get_bgn_private_key(
- std::default_random_engine& rng,
- std::string dealerIP,
- int dealerPort)
- {
- struct synchronization_tool sync;
- char *filename;
- struct mg_connection *conn = NULL;
- // Set up connection to the dealer
- std::unique_lock<std::mutex> lck(sync.mtx);
- sync.val = 0;
- while (!conn)
- {
- 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);
- if (!conn)
- std::cerr << "Couldn't connect to dealer to obtain BGN details." << std::endl;
- }
- // Establish a file to receive BGN data at
- filename = set_temp_filename(rng, conn);
- // Tell dealer to go ahead with data
- mg_websocket_client_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
- // Wait for data
- while (!sync.val)
- sync.cv.wait(lck);
- // Close connection
- mg_close_connection(conn);
- // Un-serialize BGN private key
- BGN retval = get_bgn_private_key_from_file(filename);
- remove(filename);
- delete [] filename;
- return retval;
- }
- /*
- * HELPERS TO INITIATE SHARED GLOBAL VALUES
- */
- Twistpoint make_generator(
- std::default_random_engine& rng,
- PrsonaServer *prsonaServer,
- const std::vector<std::string>& serverIPs,
- const std::vector<int>& serverPorts,
- const std::string& selfIP,
- int selfPort,
- bool fresh,
- std::vector<Proof>& pi)
- {
- // Either way, we always start from the same, default G
- Twistpoint retval = PrsonaServer::EL_GAMAL_GENERATOR;
- pi.clear();
- // Make sure we instruct the servers to contribute correctly (in practice, there is basically no difference between these)
- const char* which = (fresh ? REQUEST_ADD_CURR_SEED_FOR_FRESH_GENERATOR_URI : REQUEST_ADD_RAND_SEED_FOR_EG_BLIND_GENERATOR_URI);
- // Ask each server for its contribution
- for (size_t i = 0; i < serverIPs.size(); i++)
- {
- // Add our own contribution
- if (serverIPs[i] == selfIP && serverPorts[i] == selfPort)
- {
- if (fresh)
- retval = prsonaServer->add_curr_seed_to_generator(pi, retval);
- else
- retval = prsonaServer->add_rand_seed_to_generator(pi, retval);
- continue;
- }
- struct synchronization_tool sync;
- char *filename;
- struct mg_connection *conn = NULL;
- // Serialize current state of generator
- std::stringstream buffer;
- std::string data;
- buffer << retval;
- data = buffer.str();
- // Set up connection to current server
- std::unique_lock<std::mutex> lck(sync.mtx);
- sync.val = 0;
- while (!conn)
- {
- 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);
- if (!conn)
- std::cerr << "Couldn't get server " << i << "'s update on generator" << std::endl;
- }
- // Establish a file to receive the new generator at
- filename = set_temp_filename(rng, conn);
- // Send the current state of the generator
- mg_websocket_client_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
- mg_websocket_client_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
- // Wait for response
- while (!sync.val)
- sync.cv.wait(lck);
- // Close connection
- mg_close_connection(conn);
- // Un-serialize the new state of the generator
- Proof currProof;
- retval = get_generator_from_file(filename, currProof);
- pi.push_back(currProof);
- remove(filename);
- delete [] filename;
- }
- return retval;
- }
- void distribute_generator(
- PrsonaServer *prsonaServer,
- const std::vector<std::string>& serverIPs,
- const std::vector<int>& serverPorts,
- const std::string& selfIP,
- int selfPort,
- bool fresh,
- const std::vector<Proof>& pi,
- const Twistpoint& generator)
- {
- // Serialize the final generator, and its proof of correctness
- std::stringstream buffer;
- std::string data;
- BinarySizeT sizeOfVector(pi.size());
- buffer << sizeOfVector;
- for (size_t i = 0; i < sizeOfVector.val(); i++)
- buffer << pi[i];
- buffer << generator;
- data = buffer.str();
- // Make sure we tell the servers which generator we're giving them correctly
- const char* which = (fresh ? SUBMIT_FRESH_GENERATOR_URI : SUBMIT_EG_BLIND_GENERATOR_URI);
-
- // Distribute the generator to each server
- for (size_t i = 0; i < serverIPs.size(); i++)
- {
- // Load the generator into our own server object
- if (serverIPs[i] == selfIP && serverPorts[i] == selfPort)
- {
- if (fresh)
- prsonaServer->initialize_fresh_generator(pi, generator);
- else
- prsonaServer->set_EG_blind_generator(pi, generator);
- continue;
- }
- struct mg_connection *conn = NULL;
- // Connect to the server
- while (!conn)
- {
- 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);
- if (!conn)
- std::cerr << "Couldn't connect to server " << i << " to give them the " << (fresh ? "fresh" : "blind") << " generator." << std::endl;
- }
- // Send the generator to the server
- mg_websocket_client_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
- mg_websocket_client_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
- // Don't bother waiting for a response; we can just move on to the next
- mg_close_connection(conn);
- }
- }
- /*
- * HELPERS FOR EPOCH CALCULATIONS
- */
- std::vector<Proof> epoch_build_up(
- std::default_random_engine& rng,
- PrsonaServer *prsonaServer,
- const std::vector<std::string>& serverIPs,
- const std::vector<int>& serverPorts,
- const std::string& selfIP,
- int selfPort,
- Twistpoint& nextGenerator,
- const CivetServer& civetServer,
- std::mutex& outputMtx,
- const std::string& outputFilename,
- std::mutex& usageMtx,
- const std::string& usageFilename,
- std::vector<size_t>& overallBandwidthData)
- {
- std::vector<std::vector<std::vector<Proof>>> pi;
- std::vector<std::vector<std::vector<Twistpoint>>> permutationCommits;
- std::vector<std::vector<std::vector<Twistpoint>>> freshPseudonymCommits;
- std::vector<std::vector<std::vector<Twistpoint>>> freshPseudonymSeedCommits;
- std::vector<std::vector<std::vector<CurveBipoint>>> serverTallyCommits;
- std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>> partwayVoteMatrixCommits;
- std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>> finalVoteMatrixCommits;
- std::vector<std::vector<Proof>> generatorProofHolder(1);
- // Go through each server to perform the epoch calculation at hand
- for (size_t i = 0; i < serverIPs.size(); i++)
- {
- // When it's our turn, do things as normal
- if (serverIPs[i] == selfIP && serverPorts[i] == selfPort)
- {
- pi.clear();
- pi.push_back(generatorProofHolder);
- permutationCommits.clear();
- freshPseudonymCommits.clear();
- freshPseudonymSeedCommits.clear();
- serverTallyCommits.clear();
- partwayVoteMatrixCommits.clear();
- finalVoteMatrixCommits.clear();
- std::vector<size_t> bandwidthData(2);
- std::vector<std::vector<size_t>> otherBandwidthDataBefore;
- std::vector<size_t> serverBandwidthDataBefore = get_server_log_data(civetServer.getContext());
- std::chrono::high_resolution_clock::time_point wallTimeBefore = std::chrono::high_resolution_clock::now();
- clock_t cpuTimeBefore = clock();
-
- // Perform the actual calculation
- prsonaServer->build_up_midway_pseudonyms(pi, permutationCommits, freshPseudonymCommits, freshPseudonymSeedCommits, serverTallyCommits, partwayVoteMatrixCommits, finalVoteMatrixCommits, nextGenerator);
- std::vector<std::vector<Twistpoint>> currUserTallyMaskCommits;
- std::vector<std::vector<Twistpoint>> currUserTallyMessageCommits;
- std::vector<std::vector<Twistpoint>> currUserTallySeedCommits;
- // Serialize the relevant data
- 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);
- struct synchronization_tool sync;
- std::vector<struct mg_connection *> conns;
- // Distribute the data to each server (in parallel, roughly)
- std::unique_lock<std::mutex> lck(sync.mtx);
- sync.val = 1;
- for (size_t j = 0; j < serverIPs.size(); j++)
- {
- // But, obviously, don't send it back to ourselves
- if (i == j)
- continue;
- // Send that data
- struct mg_connection *currConn = distribute_epoch_updates(serverIPs[j], serverPorts[j], data, &sync);
- otherBandwidthDataBefore.push_back(get_conn_log_data(mg_get_context(currConn), false));
- // But keep track of that connection, as we can't close it until we know the server's gotten its data
- conns.push_back(currConn);
- }
- // Wait for the other servers to all report back that they have received the update
- while (sync.val < serverIPs.size())
- sync.cv.wait(lck);
- for (size_t j = 0; j < conns.size(); j++)
- {
- std::vector<size_t> currBandwidthDataAfter = get_conn_log_data(mg_get_context(conns[j]), false);
- bandwidthData[0] += currBandwidthDataAfter[0] - otherBandwidthDataBefore[j][0];
- bandwidthData[1] += currBandwidthDataAfter[1] - otherBandwidthDataBefore[j][1];
- overallBandwidthData[0] += currBandwidthDataAfter[0] - otherBandwidthDataBefore[j][0];
- overallBandwidthData[1] += currBandwidthDataAfter[1] - otherBandwidthDataBefore[j][1];
- mg_close_connection(conns[j]);
- }
- clock_t cpuTimeAfter = clock();
- std::chrono::high_resolution_clock::time_point wallTimeAfter = std::chrono::high_resolution_clock::now();
- std::vector<size_t> serverBandwidthDataAfter = get_server_log_data(civetServer.getContext());
- std::vector<double> timingData(2);
- timingData[0] = std::chrono::duration_cast<std::chrono::duration<double>>(wallTimeAfter - wallTimeBefore).count();
- timingData[1] = ((double)(cpuTimeAfter - cpuTimeBefore)) / CLOCKS_PER_SEC;
- bandwidthData[0] += serverBandwidthDataAfter[0] - serverBandwidthDataBefore[0];
- bandwidthData[1] += serverBandwidthDataAfter[1] - serverBandwidthDataBefore[1];
- write_log_data(outputMtx, outputFilename, timingData, bandwidthData);
- write_usage_data(usageMtx, usageFilename);
- // Keep an up-to-date version of the proof of the new fresh generator
- generatorProofHolder = pi[0];
- }
- else // When it's another server's turn, tell them to do their part
- {
- // Serialize the request
- std::string data = make_epoch_initiator_string(generatorProofHolder[0], nextGenerator);
- // And have them do that request
- nextGenerator = initiate_epoch_updates(rng, serverIPs[i], serverPorts[i], data, false, generatorProofHolder, overallBandwidthData);
- }
- }
- // Return the proof of the fresh generator
- return generatorProofHolder[0];
- }
- void epoch_break_down(
- std::default_random_engine& rng,
- PrsonaServer *prsonaServer,
- const std::vector<std::string>& serverIPs,
- const std::vector<int>& serverPorts,
- const std::string& selfIP,
- int selfPort,
- const std::vector<Proof>& generatorProof,
- const Twistpoint& nextGenerator,
- const CivetServer& civetServer,
- std::mutex& outputMtx,
- const std::string& outputFilename,
- std::mutex& usageMtx,
- const std::string& usageFilename,
- std::vector<size_t>& overallBandwidthData)
- {
- std::vector<std::vector<std::vector<Proof>>> pi;
- std::vector<std::vector<std::vector<Twistpoint>>> permutationCommits;
- std::vector<std::vector<std::vector<Twistpoint>>> freshPseudonymCommits;
- std::vector<std::vector<std::vector<Twistpoint>>> freshPseudonymSeedCommits;
- std::vector<std::vector<std::vector<CurveBipoint>>> serverTallyCommits;
- std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>> partwayVoteMatrixCommits;
- std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>> finalVoteMatrixCommits;
- std::vector<std::vector<std::vector<Twistpoint>>> userTallyMaskCommits;
- std::vector<std::vector<std::vector<Twistpoint>>> userTallyMessageCommits;
- std::vector<std::vector<std::vector<Twistpoint>>> userTallySeedCommits;
- // Go through each server to perform the epoch calculation at hand
- for (size_t i = 0; i < serverIPs.size(); i++)
- {
- // When it's our turn, do things as normal
- if (serverIPs[i] == selfIP && serverPorts[i] == selfPort)
- {
- pi.clear();
- permutationCommits.clear();
- freshPseudonymCommits.clear();
- freshPseudonymSeedCommits.clear();
- serverTallyCommits.clear();
- partwayVoteMatrixCommits.clear();
- finalVoteMatrixCommits.clear();
- userTallyMaskCommits.clear();
- userTallyMessageCommits.clear();
- userTallySeedCommits.clear();
- std::vector<size_t> bandwidthData(2);
- std::vector<std::vector<size_t>> otherBandwidthDataBefore;
- std::vector<size_t> serverBandwidthDataBefore = get_server_log_data(civetServer.getContext());
- std::chrono::high_resolution_clock::time_point wallTimeBefore = std::chrono::high_resolution_clock::now();
- clock_t cpuTimeBefore = clock();
- // Perform the actual calculation
- prsonaServer->break_down_midway_pseudonyms(generatorProof, pi, permutationCommits, freshPseudonymCommits, freshPseudonymSeedCommits, serverTallyCommits, partwayVoteMatrixCommits, finalVoteMatrixCommits, userTallyMaskCommits, userTallyMessageCommits, userTallySeedCommits, nextGenerator);
- // Serialize the relevant data
- 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);
- struct synchronization_tool sync;
- std::vector<struct mg_connection *> conns;
- // Distribute the data to each server (in parallel, roughly)
- std::unique_lock<std::mutex> lck(sync.mtx);
- sync.val = 1;
- for (size_t j = 0; j < serverIPs.size(); j++)
- {
- // But, obviously, don't send it back to ourselves
- if (i == j)
- continue;
- // Send that data
- struct mg_connection *currConn = distribute_epoch_updates(serverIPs[j], serverPorts[j], data, &sync);
- otherBandwidthDataBefore.push_back(get_conn_log_data(mg_get_context(currConn), false));
- // But keep track of that connection, as we can't close it until we know the server's gotten its data
- conns.push_back(currConn);
- }
- // Wait for the other servers to all report back that they have received the update
- while (sync.val < serverIPs.size())
- sync.cv.wait(lck);
- for (size_t j = 0; j < conns.size(); j++)
- {
- std::vector<size_t> currBandwidthDataAfter = get_conn_log_data(mg_get_context(conns[j]), false);
- bandwidthData[0] += currBandwidthDataAfter[0] - otherBandwidthDataBefore[j][0];
- bandwidthData[1] += currBandwidthDataAfter[1] - otherBandwidthDataBefore[j][1];
- overallBandwidthData[0] += currBandwidthDataAfter[0] - otherBandwidthDataBefore[j][0];
- overallBandwidthData[1] += currBandwidthDataAfter[1] - otherBandwidthDataBefore[j][1];
- mg_close_connection(conns[j]);
- }
- clock_t cpuTimeAfter = clock();
- std::chrono::high_resolution_clock::time_point wallTimeAfter = std::chrono::high_resolution_clock::now();
- std::vector<size_t> serverBandwidthDataAfter = get_server_log_data(civetServer.getContext());
- std::vector<double> timingData(2);
- timingData[0] = std::chrono::duration_cast<std::chrono::duration<double>>(wallTimeAfter - wallTimeBefore).count();
- timingData[1] = ((double)(cpuTimeAfter - cpuTimeBefore)) / CLOCKS_PER_SEC;
- bandwidthData[0] += serverBandwidthDataAfter[0] - serverBandwidthDataBefore[0];
- bandwidthData[1] += serverBandwidthDataAfter[1] - serverBandwidthDataBefore[1];
- write_log_data(outputMtx, outputFilename, timingData, bandwidthData);
- write_usage_data(usageMtx, usageFilename);
- }
- else // When it's another server's turn, tell them to do their part
- {
- std::vector<std::vector<Proof>> unused;
- // Serialize the request
- std::string data = make_epoch_initiator_string(generatorProof, nextGenerator);
-
- // And have them do that request
- initiate_epoch_updates(rng, serverIPs[i], serverPorts[i], data, true, unused, overallBandwidthData);
- }
- }
- }
- /*
- * HELPERS FOR EPOCH HELPERS
- */
- Twistpoint initiate_epoch_updates(
- std::default_random_engine& rng,
- const std::string& recipient,
- int recipientPort,
- const std::string& data,
- bool isBreakdown,
- std::vector<std::vector<Proof>>& generatorProofHolder,
- std::vector<size_t>& bandwidthData)
- {
- Twistpoint retval;
- struct synchronization_tool sync;
- char * filename = NULL;
- const char* which = (isBreakdown ? REQUEST_EPOCH_BREAK_DOWN_URI : REQUEST_EPOCH_BUILD_UP_URI);
- // Make sure we don't move on until this server has conducted its epoch calculations
- std::unique_lock<std::mutex> lck(sync.mtx);
- sync.val = 0;
- while (!sync.val)
- {
- struct mg_connection *conn = NULL;
-
- // Connect to a server
- sync.val2 = 0;
- while (!conn)
- {
- conn = mg_connect_websocket_client(recipient.c_str(), recipientPort, USE_SSL, NULL, 0, which, "null", epoch_websocket_data_handler, epoch_websocket_close_handler, &sync);
- if (!conn)
- std::cerr << "Couldn't initiate epoch update with server at " << recipient << ":" << recipientPort << std::endl;
- }
-
- // Establish a file to receive update data (when relevant)
- if (!isBreakdown)
- filename = set_temp_filename(rng, conn);
-
- // Send the relevant data
- mg_websocket_client_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
- mg_websocket_client_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
- // Wait for a response (or the connection to die)
- while (!sync.val2)
- sync.cv.wait(lck);
- // Close connection
- mg_close_connection(conn);
- }
-
- // In the second half of the epoch, we don't have a data response to un-serialize, so just move on
- if (isBreakdown)
- return retval;
- std::vector<Proof> generatorProof;
- generatorProofHolder.clear();
- // Otherwise, un-serialize the updated fresh generator (and its proof of correctness)
- retval = get_generator_from_file(filename, generatorProof);
- generatorProofHolder.push_back(generatorProof);
- remove(filename);
- delete [] filename;
- return retval;
- }
- struct mg_connection *distribute_epoch_updates(
- const std::string& recipient,
- int recipientPort,
- const std::string& data,
- struct synchronization_tool* sync)
- {
- struct mg_connection *conn = NULL;
- // Connect to the server
- while (!conn)
- {
- 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);
- if (!conn)
- std::cerr << "Couldn't give epoch updates to server at " << recipient << ":" << recipientPort << std::endl;
- }
- // Send the update
- mg_websocket_client_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
- mg_websocket_client_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
- return conn;
- }
- /*
- * SCORE TALLYING AND DISTRIBUTION HELPERS
- */
- void tally_scores(
- PrsonaServer *prsonaServer,
- const std::vector<std::string>& serverIPs,
- const std::vector<int>& serverPorts,
- const std::string& selfIP,
- int selfPort,
- const Twistpoint& nextGenerator,
- std::vector<EGCiphertext>& userTallyScores,
- std::vector<CurveBipoint>& serverTallyScores,
- std::vector<size_t>& bandwidthData)
- {
- struct synchronization_tool sync;
- std::vector<struct mg_connection *> conns;
- std::vector<std::vector<size_t>> allBandwidthDataBefore;
- // Connect to each server (roughly in parallel)
- std::unique_lock<std::mutex> lck(sync.mtx);
- sync.val = 1;
- for (size_t i = 0; i < serverIPs.size(); i++)
- {
- // Except, skip ourselves (obviously)
- if (serverIPs[i] == selfIP && serverPorts[i] == selfPort)
- continue;
-
- struct mg_connection *currConn = NULL;
- while (!currConn)
- {
- 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);
- if (!currConn)
- std::cerr << "Trouble getting partial decryption from server at " << serverIPs[i] << ":" << serverPorts[i] << std::endl;
- }
- allBandwidthDataBefore.push_back(get_conn_log_data(mg_get_context(currConn), true));
- // Ping server for simulated distributed BGN
- mg_websocket_client_write(currConn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
- conns.push_back(currConn);
- }
- // Since we're only simulating distributed BGN, we're only waiting for ACKs here
- while (sync.val < serverIPs.size())
- sync.cv.wait(lck);
- // Close connections
- for (size_t i = 0; i < conns.size(); i++)
- {
- std::vector<size_t> currBandwidthDataAfter = get_conn_log_data(mg_get_context(conns[i]), true);
- bandwidthData[0] += currBandwidthDataAfter[0] - allBandwidthDataBefore[i][0];
- bandwidthData[1] += currBandwidthDataAfter[1] - allBandwidthDataBefore[i][1];
- mg_close_connection(conns[i]);
- }
- // Now we do the actual calculations
- std::vector<EGCiphertext> retval;
- std::vector<Twistpoint> currentPseudonyms = prsonaServer->get_current_pseudonyms();
- std::vector<Scalar> decryptedTalliedScores = prsonaServer->tally_scores();
- mpz_class maxScorePossibleThisRound = prsonaServer->get_max_possible_score().toInt() * PrsonaBase::get_max_allowed_vote();
- mpz_class topOfScoreRange = decryptedTalliedScores.size() * PrsonaBase::get_max_allowed_vote();
- userTallyScores.clear();
- serverTallyScores.clear();
- for (size_t i = 0; i < decryptedTalliedScores.size(); i++)
- {
- // Scale scores correctly to keep in the desired range
- decryptedTalliedScores[i] = Scalar((decryptedTalliedScores[i].toInt() * topOfScoreRange) / maxScorePossibleThisRound);
- EGCiphertext currCiphertext;
- userTallyScores.push_back(currCiphertext);
- CurveBipoint currServerScore;
- serverTallyScores.push_back(currServerScore);
- Scalar currMask;
- currMask.set_random();
- // Give the server the new weights for the next epoch's calculation, to get passed around to the other servers
- prsonaServer->encrypt(serverTallyScores[i], decryptedTalliedScores[i]);
- // Encrypt the scores for the partway pseudonyms (since we're in the middle of the epoch calculations)
- userTallyScores[i].mask = currentPseudonyms[i] * currMask;
- userTallyScores[i].encryptedMessage = (nextGenerator * currMask) + (prsonaServer->get_blinding_generator() * decryptedTalliedScores[i]);
- }
- }
- void distribute_tallied_scores(
- PrsonaServer *prsonaServer,
- const std::vector<std::string>& serverIPs,
- const std::vector<int>& serverPorts,
- const std::string& selfIP,
- int selfPort,
- const Twistpoint& nextGenerator,
- const std::vector<EGCiphertext>& userTallyScores,
- const std::vector<CurveBipoint>& serverTallyScores,
- std::vector<size_t>& bandwidthData)
- {
- // Serialize scores
- std::stringstream buffer;
- std::string data;
- BinarySizeT sizeOfVector(userTallyScores.size());
- buffer << sizeOfVector;
- for (size_t i = 0; i < sizeOfVector.val(); i++)
- buffer << userTallyScores[i];
- for (size_t i = 0; i < sizeOfVector.val(); i++)
- buffer << serverTallyScores[i];
- data = buffer.str();
- struct synchronization_tool sync;
- std::vector<struct mg_connection *> conns;
- std::vector<std::vector<size_t>> allBandwidthDataBefore;
- // Connect to each server (roughly in parallel)
- std::unique_lock<std::mutex> lck(sync.mtx);
- sync.val = 1;
- for (size_t i = 0; i < serverIPs.size(); i++)
- {
- // When it's our turn, receive our actual new scores for the next epoch
- if (serverIPs[i] == selfIP && serverPorts[i] == selfPort)
- {
- prsonaServer->receive_tallied_scores(userTallyScores, serverTallyScores);
- continue;
- }
-
- struct mg_connection *currConn = NULL;
- while (!currConn)
- {
- 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);
- if (!currConn)
- std::cerr << "Trouble giving full re-encryption to server at " << serverIPs[i] << ":" << serverPorts[i] << std::endl;
- }
- allBandwidthDataBefore.push_back(get_conn_log_data(mg_get_context(currConn), false));
- // Send the relevant data
- mg_websocket_client_write(currConn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
- mg_websocket_client_write(currConn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
- conns.push_back(currConn);
- }
- // Wait for each server to ACK the scores
- while (sync.val < serverIPs.size())
- sync.cv.wait(lck);
- // Close connections
- for (size_t i = 0; i < conns.size(); i++)
- {
- std::vector<size_t> currBandwidthDataAfter = get_conn_log_data(mg_get_context(conns[i]), false);
- bandwidthData[0] += currBandwidthDataAfter[0] - allBandwidthDataBefore[i][0];
- bandwidthData[1] += currBandwidthDataAfter[1] - allBandwidthDataBefore[i][1];
- mg_close_connection(conns[i]);
- }
- }
- /*
- * FILE I/O HELPERS
- */
- BGN get_bgn_private_key_from_file(
- const char *filename)
- {
- std::ifstream bgnFile(filename);
- BGN privateKey;
- bgnFile >> privateKey;
- return privateKey;
- }
- Twistpoint get_generator_from_file(
- const char *filename,
- Proof& pi)
- {
- std::ifstream genFile(filename);
- Twistpoint retval;
- genFile >> pi;
- genFile >> retval;
- return retval;
- }
- Twistpoint get_generator_from_file(
- const char *filename,
- std::vector<Proof>& pi)
- {
- std::ifstream epochFile(filename);
-
- Twistpoint retval;
- BinarySizeT sizeOfVector;
- pi.clear();
- epochFile >> sizeOfVector;
- for (size_t i = 0; i < sizeOfVector.val(); i++)
- {
- Proof currProof;
- epochFile >> currProof;
- pi.push_back(currProof);
- }
- epochFile >> retval;
- return retval;
- }
- /*
- * EPOCH DATA SERIALIZERS/UN-SERIALIZERS
- */
- std::string make_epoch_initiator_string(
- const std::vector<Proof>& generatorProof,
- const Twistpoint& nextGenerator)
- {
- std::stringstream buffer;
-
- BinarySizeT sizeOfVector(generatorProof.size());
- buffer << sizeOfVector;
- for (size_t i = 0; i < sizeOfVector.val(); i++)
- buffer << generatorProof[i];
-
- buffer << nextGenerator;
- return buffer.str();
- }
- ssize_t read_epoch_initiator_string(
- const char *filename,
- std::vector<Proof>& generatorProof,
- Twistpoint& nextGenerator)
- {
- std::ifstream file(filename);
- file.ignore(std::numeric_limits<std::streamsize>::max());
- std::streamsize retval = file.gcount();
- file.clear();
- file.seekg(0, std::ios_base::beg);
- BinarySizeT sizeOfVector;
- generatorProof.clear();
- file >> sizeOfVector;
- for (size_t i = 0; i < sizeOfVector.val(); i++)
- {
- Proof currProof;
- file >> currProof;
- generatorProof.push_back(currProof);
- }
-
- file >> nextGenerator;
- return retval;
- }
- std::string make_epoch_update_string(
- const std::vector<std::vector<Proof>>& pi,
- const std::vector<std::vector<Twistpoint>>& permutationCommits,
- const std::vector<std::vector<Twistpoint>>& freshPseudonymCommits,
- const std::vector<std::vector<Twistpoint>>& freshPseudonymSeedCommits,
- const std::vector<std::vector<CurveBipoint>>& serverTallyCommits,
- const std::vector<std::vector<std::vector<TwistBipoint>>>& partwayVoteMatrixCommits,
- const std::vector<std::vector<std::vector<TwistBipoint>>>& finalVoteMatrixCommits,
- const std::vector<std::vector<Twistpoint>>& userTallyMaskCommits,
- const std::vector<std::vector<Twistpoint>>& userTallyMessageCommits,
- const std::vector<std::vector<Twistpoint>>& userTallySeedCommits,
- const Twistpoint& nextGenerator,
- bool doUserTallies)
- {
- std::stringstream buffer;
-
- BinarySizeT sizeOfVectorI, sizeOfVectorJ;
- sizeOfVectorI.set(pi.size());
- buffer << sizeOfVectorI;
- for (size_t i = 0; i < sizeOfVectorI.val(); i++)
- {
- sizeOfVectorJ.set(pi[i].size());
- buffer << sizeOfVectorJ;
- for (size_t j = 0; j < sizeOfVectorJ.val(); j++)
- buffer << pi[i][j];
- }
- sizeOfVectorI.set(permutationCommits.size());
- buffer << sizeOfVectorI;
- for (size_t i = 0; i < sizeOfVectorI.val(); i++)
- for (size_t j = 0; j < sizeOfVectorI.val(); j++)
- buffer << permutationCommits[i][j];
- sizeOfVectorI.set(freshPseudonymCommits.size());
- buffer << sizeOfVectorI;
- for (size_t i = 0; i < sizeOfVectorI.val(); i++)
- for (size_t j = 0; j < sizeOfVectorI.val(); j++)
- buffer << freshPseudonymCommits[i][j];
- sizeOfVectorI.set(freshPseudonymSeedCommits.size());
- buffer << sizeOfVectorI;
- for (size_t i = 0; i < sizeOfVectorI.val(); i++)
- for (size_t j = 0; j < sizeOfVectorI.val(); j++)
- buffer << freshPseudonymSeedCommits[i][j];
- sizeOfVectorI.set(serverTallyCommits.size());
- buffer << sizeOfVectorI;
- for (size_t i = 0; i < sizeOfVectorI.val(); i++)
- for (size_t j = 0; j < sizeOfVectorI.val(); j++)
- buffer << serverTallyCommits[i][j];
- sizeOfVectorI.set(partwayVoteMatrixCommits.size());
- buffer << sizeOfVectorI;
- for (size_t i = 0; i < sizeOfVectorI.val(); i++)
- for (size_t j = 0; j < sizeOfVectorI.val(); j++)
- for (size_t k = 0; k < sizeOfVectorI.val(); k++)
- buffer << partwayVoteMatrixCommits[i][j][k];
- sizeOfVectorI.set(finalVoteMatrixCommits.size());
- buffer << sizeOfVectorI;
- for (size_t i = 0; i < sizeOfVectorI.val(); i++)
- for (size_t j = 0; j < sizeOfVectorI.val(); j++)
- for (size_t k = 0; k < sizeOfVectorI.val(); k++)
- buffer << finalVoteMatrixCommits[i][j][k];
- sizeOfVectorI.set(userTallyMaskCommits.size());
- buffer << sizeOfVectorI;
- for (size_t i = 0; i < sizeOfVectorI.val(); i++)
- for (size_t j = 0; j < sizeOfVectorI.val(); j++)
- buffer << userTallyMaskCommits[i][j];
- sizeOfVectorI.set(userTallyMessageCommits.size());
- buffer << sizeOfVectorI;
- for (size_t i = 0; i < sizeOfVectorI.val(); i++)
- for (size_t j = 0; j < sizeOfVectorI.val(); j++)
- buffer << userTallyMessageCommits[i][j];
- sizeOfVectorI.set(userTallySeedCommits.size());
- buffer << sizeOfVectorI;
- for (size_t i = 0; i < sizeOfVectorI.val(); i++)
- for (size_t j = 0; j < sizeOfVectorI.val(); j++)
- buffer << userTallySeedCommits[i][j];
- buffer << nextGenerator;
- BinaryBool flag(doUserTallies);
- buffer << flag;
- return buffer.str();
- }
- ssize_t read_epoch_update_string(
- const char *filename,
- std::vector<std::vector<Proof>>& pi,
- std::vector<std::vector<Twistpoint>>& permutationCommits,
- std::vector<std::vector<Twistpoint>>& freshPseudonymCommits,
- std::vector<std::vector<Twistpoint>>& freshPseudonymSeedCommits,
- std::vector<std::vector<CurveBipoint>>& serverTallyCommits,
- std::vector<std::vector<std::vector<TwistBipoint>>>& partwayVoteMatrixCommits,
- std::vector<std::vector<std::vector<TwistBipoint>>>& finalVoteMatrixCommits,
- std::vector<std::vector<Twistpoint>>& userTallyMaskCommits,
- std::vector<std::vector<Twistpoint>>& userTallyMessageCommits,
- std::vector<std::vector<Twistpoint>>& userTallySeedCommits,
- Twistpoint& nextGenerator,
- bool& doUserTallies)
- {
- std::ifstream file(filename);
- file.ignore(std::numeric_limits<std::streamsize>::max());
- std::streamsize retval = file.gcount();
- file.clear();
- file.seekg(0, std::ios_base::beg);
- BinarySizeT sizeOfVectorI, sizeOfVectorJ;
-
- pi.clear();
- permutationCommits.clear();
- freshPseudonymCommits.clear();
- freshPseudonymSeedCommits.clear();
- serverTallyCommits.clear();
- partwayVoteMatrixCommits.clear();
- finalVoteMatrixCommits.clear();
- userTallyMaskCommits.clear();
- userTallyMessageCommits.clear();
- userTallySeedCommits.clear();
- file >> sizeOfVectorI;
- for (size_t i = 0; i < sizeOfVectorI.val(); i++)
- {
- std::vector<Proof> currRow;
- file >> sizeOfVectorJ;
- for (size_t j = 0; j < sizeOfVectorJ.val(); j++)
- {
- Proof currProof;
- file >> currProof;
- currRow.push_back(currProof);
- }
- pi.push_back(currRow);
- }
- file >> sizeOfVectorI;
- for (size_t i = 0; i < sizeOfVectorI.val(); i++)
- {
- std::vector<Twistpoint> currRow;
- for (size_t j = 0; j < sizeOfVectorI.val(); j++)
- {
- Twistpoint currCommit;
- file >> currCommit;
- currRow.push_back(currCommit);
- }
- permutationCommits.push_back(currRow);
- }
- file >> sizeOfVectorI;
- for (size_t i = 0; i < sizeOfVectorI.val(); i++)
- {
- std::vector<Twistpoint> currRow;
- for (size_t j = 0; j < sizeOfVectorI.val(); j++)
- {
- Twistpoint currCommit;
- file >> currCommit;
- currRow.push_back(currCommit);
- }
- freshPseudonymCommits.push_back(currRow);
- }
- file >> sizeOfVectorI;
- for (size_t i = 0; i < sizeOfVectorI.val(); i++)
- {
- std::vector<Twistpoint> currRow;
- for (size_t j = 0; j < sizeOfVectorI.val(); j++)
- {
- Twistpoint currCommit;
- file >> currCommit;
- currRow.push_back(currCommit);
- }
- freshPseudonymSeedCommits.push_back(currRow);
- }
- file >> sizeOfVectorI;
- for (size_t i = 0; i < sizeOfVectorI.val(); i++)
- {
- std::vector<CurveBipoint> currRow;
- for (size_t j = 0; j < sizeOfVectorI.val(); j++)
- {
- CurveBipoint currCommit;
- file >> currCommit;
- currRow.push_back(currCommit);
- }
- serverTallyCommits.push_back(currRow);
- }
- file >> sizeOfVectorI;
- for (size_t i = 0; i < sizeOfVectorI.val(); i++)
- {
- std::vector<std::vector<TwistBipoint>> currMatrix;
- for (size_t j = 0; j < sizeOfVectorI.val(); j++)
- {
- std::vector<TwistBipoint> currRow;
- for (size_t k = 0; k < sizeOfVectorI.val(); k++)
- {
- TwistBipoint currCommit;
- file >> currCommit;
- currRow.push_back(currCommit);
- }
- currMatrix.push_back(currRow);
- }
- partwayVoteMatrixCommits.push_back(currMatrix);
- }
- file >> sizeOfVectorI;
- for (size_t i = 0; i < sizeOfVectorI.val(); i++)
- {
- std::vector<std::vector<TwistBipoint>> currMatrix;
- for (size_t j = 0; j < sizeOfVectorI.val(); j++)
- {
- std::vector<TwistBipoint> currRow;
- for (size_t k = 0; k < sizeOfVectorI.val(); k++)
- {
- TwistBipoint currCommit;
- file >> currCommit;
- currRow.push_back(currCommit);
- }
- currMatrix.push_back(currRow);
- }
- finalVoteMatrixCommits.push_back(currMatrix);
- }
- file >> sizeOfVectorI;
- for (size_t i = 0; i < sizeOfVectorI.val(); i++)
- {
- std::vector<Twistpoint> currRow;
- for (size_t j = 0; j < sizeOfVectorI.val(); j++)
- {
- Twistpoint currCommit;
- file >> currCommit;
- currRow.push_back(currCommit);
- }
- userTallyMaskCommits.push_back(currRow);
- }
- file >> sizeOfVectorI;
- for (size_t i = 0; i < sizeOfVectorI.val(); i++)
- {
- std::vector<Twistpoint> currRow;
- for (size_t j = 0; j < sizeOfVectorI.val(); j++)
- {
- Twistpoint currCommit;
- file >> currCommit;
- currRow.push_back(currCommit);
- }
- userTallyMessageCommits.push_back(currRow);
- }
- file >> sizeOfVectorI;
- for (size_t i = 0; i < sizeOfVectorI.val(); i++)
- {
- std::vector<Twistpoint> currRow;
- for (size_t j = 0; j < sizeOfVectorI.val(); j++)
- {
- Twistpoint currCommit;
- file >> currCommit;
- currRow.push_back(currCommit);
- }
- userTallySeedCommits.push_back(currRow);
- }
- file >> nextGenerator;
-
- BinaryBool binaryDoUserTallies;
- file >> binaryDoUserTallies;
- doUserTallies = binaryDoUserTallies.val();
- return retval;
- }
- /**********************************************************
- **** ****
- **** other server-relevant handler member functions ****
- **** ****
- **********************************************************/
- /*
- * EPOCH READY HANDLER
- */
- EpochReadyHandler::EpochReadyHandler(
- struct synchronization_tool *exitSync,
- struct synchronization_tool *readySync,
- std::mutex& updateMtx,
- size_t numServers)
- : exitSync(exitSync), readySync(readySync), updateMtx(updateMtx), numServers(numServers)
- { /* */ }
- bool EpochReadyHandler::handleGet(
- CivetServer *server,
- struct mg_connection *conn)
- {
- std::unique_lock<std::mutex> exitLock(exitSync->mtx, std::defer_lock);
- std::unique_lock<std::mutex> updateLock(updateMtx, std::defer_lock);
- std::unique_lock<std::mutex> readyLock(readySync->mtx);
- if (readySync->val < numServers)
- {
- mg_printf(conn, "HTTP/1.1 503 Service Unavailable\r\n"
- "Content-Type: text/plain\r\n"
- "Connection: close\r\n\r\n");
- mg_printf(conn, "Server is waiting for other servers to begin.\n");
- }
- else if (!exitLock.try_lock())
- {
- mg_printf(conn, "HTTP/1.1 503 Service Unavailable\r\n"
- "Content-Type: text/plain\r\n"
- "Connection: close\r\n\r\n");
- mg_printf(conn, "Server is still in a previous epoch.\n");
- }
- else if (!updateLock.try_lock())
- {
- mg_printf(conn, "HTTP/1.1 503 Service Unavailable\r\n"
- "Content-Type: text/plain\r\n"
- "Connection: close\r\n\r\n");
- mg_printf(conn, "Server is handling other updates.\n");
- }
- else
- {
- mg_printf(conn, "HTTP/1.1 200 OK\r\n"
- "Content-Type: text/plain\r\n"
- "Connection: close\r\n\r\n");
- mg_printf(conn, "Server is ready for epoch.\n");
- }
- return true;
- }
- /*
- * EPOCH NUM HANDLER
- */
- EpochNumHandler::EpochNumHandler(
- std::atomic<size_t>& epochNum)
- : epochNum(epochNum)
- { /* */ }
- bool EpochNumHandler::handleGet(
- CivetServer *server,
- struct mg_connection *conn)
- {
- mg_printf(conn, "HTTP/1.1 200 OK\r\n"
- "Content-Type: text/plain\r\n"
- "Connection: close\r\n\r\n");
- mg_printf(conn, "Epoch num: %lu\n", epochNum.load());
- return true;
- }
- /*
- * UPDATE LOCK HANDLER
- */
- UpdateLockWebSocketHandler::UpdateLockWebSocketHandler(
- std::mutex& updateMtx,
- std::unique_lock<std::mutex> **lockHolder,
- bool isLocking)
- : updateMtx(updateMtx), lockHolder(lockHolder), isLocking(isLocking)
- { /* */ }
- UpdateLockWebSocketHandler::~UpdateLockWebSocketHandler()
- {
- delete *lockHolder;
- *lockHolder = NULL;
- }
- bool UpdateLockWebSocketHandler::handleConnection(
- CivetServer *server,
- const struct mg_connection *conn)
- {
- return true;
- }
- void UpdateLockWebSocketHandler::handleReadyState(
- CivetServer *server,
- struct mg_connection *conn)
- { /* */ }
- bool UpdateLockWebSocketHandler::handleData(
- CivetServer *server,
- struct mg_connection *conn,
- int bits,
- char *data,
- size_t data_len)
- {
- switch (bits & 0xf)
- {
- case MG_WEBSOCKET_OPCODE_DATACOMPLETE:
- if (isLocking)
- {
- std::unique_lock<std::mutex> *tempHolder = new std::unique_lock<std::mutex>(updateMtx);
-
- // Once you get to this line, we now hold the lock, and lockHolder is guaranteed to be NULL
- *lockHolder = tempHolder;
- // Respond to notify that the requesting process holds the lock
- mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
- }
- else
- {
- // 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)
- std::unique_lock<std::mutex> *tempHolder = *lockHolder;
- *lockHolder = NULL;
- delete tempHolder;
-
- // Respond to notify that the requesting process has released the lock
- mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
- }
- break;
- case MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE:
- break;
- default:
- std::cerr << "Unknown opcode: failing." << std::endl;
- break;
- }
- return false;
- }
- void UpdateLockWebSocketHandler::handleClose(
- CivetServer *server,
- const struct mg_connection *conn)
- { /* */ }
- /*********************************************************
- **** ****
- **** PrsonaServerWebSocketHandler member functions ****
- **** ****
- *********************************************************/
- /*
- * CONSTRUCTOR
- */
- PrsonaServerWebSocketHandler::PrsonaServerWebSocketHandler(
- std::default_random_engine& rng,
- PrsonaServer *prsonaServer,
- const std::vector<std::string>& serverIPs,
- const std::vector<int>& serverPorts,
- const std::string& selfIP,
- int selfPort,
- std::mutex& updateMtx,
- std::atomic<size_t>& epochNum,
- std::mutex& buildUpOutputMtx,
- const std::string& buildUpOutputFilename,
- std::mutex& breakDownOutputMtx,
- const std::string& breakDownOutputFilename,
- std::mutex& updateOutputMtx,
- const std::string& updateOutputFilename,
- std::mutex& voteOutputMtx,
- const std::string& voteOutputFilename,
- std::mutex& usageMtx,
- const std::string& usageFilename)
- : rng(rng), prsonaServer(prsonaServer), serverIPs(serverIPs), serverPorts(serverPorts), selfIP(selfIP), selfPort(selfPort), updateMtx(updateMtx), epochNum(epochNum), buildUpOutputMtx(buildUpOutputMtx), buildUpOutputFilename(buildUpOutputFilename), breakDownOutputMtx(breakDownOutputMtx), breakDownOutputFilename(breakDownOutputFilename), updateOutputMtx(updateOutputMtx), updateOutputFilename(updateOutputFilename), voteOutputMtx(voteOutputMtx), voteOutputFilename(voteOutputFilename), usageMtx(usageMtx), usageFilename(usageFilename)
- { /* */ }
- /*
- * REQUIRED BY INHERITED CLASS
- */
- bool PrsonaServerWebSocketHandler::handleConnection(
- CivetServer *server,
- const struct mg_connection *conn)
- {
- const struct mg_request_info *info = mg_get_request_info(conn);
-
- // Check if the request being made is something this server can respond to
- bool flag = info->query_string && info->query_string[0] >= PRSONA_GIVE_BGN_PUBKEY && info->query_string[0] <= PRSONA_RECEIVE_EPOCH_UPDATE;
- flag = flag || (info->query_string && info->query_string[0] == PRSONA_GIVE_PARTIAL_DECRYPTION);
- flag = flag || (info->query_string && info->query_string[0] == PRSONA_RECEIVE_PARTIAL_DECRYPTION);
- return flag;
- }
- void PrsonaServerWebSocketHandler::handleReadyState(
- CivetServer *server,
- struct mg_connection *conn)
- {
- const struct mg_request_info *info = mg_get_request_info(conn);
- // Set filenames for query types that will need to un-serialize data to respond correctly
- switch (info->query_string[0])
- {
- case PRSONA_GIVE_VOTE_ROW:
- case PRSONA_GIVE_VOTE_MATRIX:
- case PRSONA_GIVE_CLIENT_TALLY:
- case PRSONA_GIVE_SERVER_TALLY:
- case PRSONA_GIVE_PSEUDONYMS:
- case PRSONA_GIVE_VOTE_ROW_COMMITMENT:
- case PRSONA_GIVE_VOTE_MATRIX_COMMITMENT:
- case PRSONA_GIVE_CLIENT_TALLY_COMMITMENT:
- case PRSONA_GIVE_SERVER_TALLY_COMMITMENT:
- case PRSONA_GIVE_PSEUDONYMS_COMMITMENT:
- case PRSONA_RECEIVE_NEW_CLIENT:
- case PRSONA_RECEIVE_VOTE:
- case PRSONA_RECEIVE_UPDATE_WITH_NEW_USER:
- case PRSONA_ADD_CURR_SEED_TO_GENERATOR:
- case PRSONA_RECEIVE_FRESH_GENERATOR:
- case PRSONA_ADD_RAND_SEED_TO_GENERATOR:
- case PRSONA_RECEIVE_EG_BLIND_GENERATOR:
- case PRSONA_PERFORM_EPOCH_BUILD_UP:
- case PRSONA_PERFORM_EPOCH_BREAK_DOWN:
- case PRSONA_RECEIVE_EPOCH_UPDATE:
- case PRSONA_RECEIVE_PARTIAL_DECRYPTION:
- set_temp_filename(rng, conn);
- break;
- default:
- mg_set_user_connection_data(conn, NULL);
- break;
- }
- }
- bool PrsonaServerWebSocketHandler::handleData(
- CivetServer *server,
- struct mg_connection *conn,
- int bits,
- char *data,
- size_t data_len)
- {
- char *filename = (char *) mg_get_user_connection_data(conn);
- FILE *currFile = NULL;
- switch (bits & 0xf)
- {
- // Requester has indicated they have sent all relevant data
- case MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE:
- case MG_WEBSOCKET_OPCODE_DATACOMPLETE:
- generate_response(server, conn, filename);
- break;
- // Requester has sent more data (which may theoretically be broken up into multiple packets)
- case MG_WEBSOCKET_OPCODE_BINARY:
- case MG_WEBSOCKET_OPCODE_CONTINUATION:
- currFile = fopen(filename, "ab");
- fwrite(data, sizeof(char), data_len, currFile);
- fclose(currFile);
- return true;
- // Something strange has happened
- default:
- std::cerr << "Unknown packet type received. Failing." << std::endl;
- break;
- }
- return false;
- }
- void PrsonaServerWebSocketHandler::handleClose(
- CivetServer *server,
- const struct mg_connection *conn)
- {
- char *filename = (char *) mg_get_user_connection_data(conn);
- // If we didn't have a temp file for this request, don't do anything
- if (!filename)
- return;
- // If we did, delete it
- remove(filename);
- delete [] filename;
- }
- /*
- * RESPONSE ROUTER FUNCTION
- */
- void PrsonaServerWebSocketHandler::generate_response(
- CivetServer *server,
- struct mg_connection *conn,
- const char *filename)
- {
- const struct mg_request_info *info = mg_get_request_info(conn);
- // Select the correct response for this type of request
- switch (info->query_string[0])
- {
- // BASIC PUBLIC SYSTEM INFO GETTERS
- case PRSONA_GIVE_BGN_PUBKEY:
- get_bgn_public_key(conn);
- break;
- case PRSONA_GIVE_NUM_CLIENTS:
- get_num_clients(conn);
- break;
- case PRSONA_GIVE_NUM_SERVERS:
- get_num_servers(conn);
- break;
- case PRSONA_GIVE_FRESH_GENERATOR:
- get_generator(conn, true);
- break;
- case PRSONA_GIVE_EG_BLIND_GENERATOR:
- get_generator(conn, false);
- break;
- // ENCRYPTED DATA GETTERS
- case PRSONA_GIVE_VOTE_ROW:
- get_current_votes_by(conn, filename);
- break;
- case PRSONA_GIVE_VOTE_MATRIX:
- get_all_current_votes(conn);
- break;
- case PRSONA_GIVE_CLIENT_TALLY:
- get_current_user_encrypted_tally(conn, filename);
- break;
- case PRSONA_GIVE_SERVER_TALLY:
- get_current_server_encrypted_tally(conn, filename);
- break;
- case PRSONA_GIVE_PSEUDONYMS:
- get_current_pseudonyms(conn);
- break;
- // PROOF COMMITMENT GETTERS
- case PRSONA_GIVE_VOTE_ROW_COMMITMENT:
- get_vote_row_commitment(conn, filename);
- break;
- case PRSONA_GIVE_VOTE_MATRIX_COMMITMENT:
- get_vote_matrix_commitment(conn);
- break;
- case PRSONA_GIVE_CLIENT_TALLY_COMMITMENT:
- get_user_tally_commitment(conn, filename);
- break;
- case PRSONA_GIVE_SERVER_TALLY_COMMITMENT:
- get_server_tally_commitment(conn, filename);
- break;
- case PRSONA_GIVE_PSEUDONYMS_COMMITMENT:
- get_pseudonyms_commitment(conn);
- break;
- // CLIENT INTERACTIONS
- case PRSONA_RECEIVE_NEW_CLIENT:
- add_new_client(conn, filename);
- break;
- case PRSONA_RECEIVE_VOTE:
- receive_vote(server, conn, filename);
- break;
- // CLIENT INTERACTION HELPER
- case PRSONA_RECEIVE_UPDATE_WITH_NEW_USER:
- import_new_user_update(conn, filename);
- break;
- // CONSTRUCTOR HELPERS
- case PRSONA_GIVE_BGN_PRIVKEY:
- get_bgn_details(conn);
- break;
- case PRSONA_ADD_CURR_SEED_TO_GENERATOR:
- add_seed_to_generator(conn, filename, true);
- break;
- case PRSONA_RECEIVE_FRESH_GENERATOR:
- set_generator(filename, true);
- break;
- case PRSONA_ADD_RAND_SEED_TO_GENERATOR:
- add_seed_to_generator(conn, filename, false);
- break;
- case PRSONA_RECEIVE_EG_BLIND_GENERATOR:
- set_generator(filename, false);
- break;
- // EPOCH ROUNDS
- case PRSONA_PERFORM_EPOCH_BUILD_UP:
- build_up_midway_pseudonyms(server, conn, filename);
- break;
- case PRSONA_PERFORM_EPOCH_BREAK_DOWN:
- break_down_midway_pseudonyms(server, conn, filename);
- break;
- case PRSONA_RECEIVE_EPOCH_UPDATE:
- accept_epoch_updates(server, conn, filename);
- break;
- // DISTRIBUTED BGN
- case PRSONA_GIVE_PARTIAL_DECRYPTION:
- get_partial_decryption(conn);
- break;
- case PRSONA_RECEIVE_PARTIAL_DECRYPTION:
- receive_tallied_scores(conn, filename);
- break;
- default:
- break;
- }
- }
- /*
- * BASIC PUBLIC SYSTEM INFO GETTERS
- */
- void PrsonaServerWebSocketHandler::get_bgn_public_key(
- struct mg_connection *conn) const
- {
- // Retrieve value
- BGNPublicKey pubKey = prsonaServer->get_bgn_public_key();
- // Serialize response
- std::stringstream buffer;
- std::string data;
- buffer << pubKey;
- data = buffer.str();
- // Send response
- mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
- }
- void PrsonaServerWebSocketHandler::get_num_clients(
- struct mg_connection *conn) const
- {
- // Retrieve value
- BinarySizeT numClients(prsonaServer->get_num_clients());
- // Serialize response
- std::stringstream buffer;
- std::string data;
- buffer << numClients;
- data = buffer.str();
- // Send response
- mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
- }
- void PrsonaServerWebSocketHandler::get_num_servers(
- struct mg_connection *conn) const
- {
- // Retrieve value
- BinarySizeT numServers(prsonaServer->get_num_servers());
- // Serialize response
- std::stringstream buffer;
- std::string data;
- buffer << numServers;
- data = buffer.str();
- // Send response
- mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
- }
- void PrsonaServerWebSocketHandler::get_generator(
- struct mg_connection *conn,
- bool fresh)
- {
- // Retrieve value
- Twistpoint generator;
- std::vector<Proof> pi;
- if (fresh)
- generator = prsonaServer->get_fresh_generator(pi);
- else
- generator = prsonaServer->get_blinding_generator(pi);
- // Serialize response
- std::stringstream buffer;
- std::string data;
- BinarySizeT sizeOfVector(pi.size());
- buffer << sizeOfVector;
- for (size_t i = 0; i < sizeOfVector.val(); i++)
- buffer << pi[i];
- buffer << generator;
- data = buffer.str();
- // Send response
- mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
- mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
- }
- /*
- * ENCRYPTED DATA GETTERS
- */
- void PrsonaServerWebSocketHandler::get_current_votes_by(
- struct mg_connection *conn,
- const char *filename) const
- {
- std::ifstream file(filename);
- // Un-serialize request
- Twistpoint shortTermPublicKey;
- file >> shortTermPublicKey;
- // Retrieve value
- Proof pi;
- std::vector<TwistBipoint> votes = prsonaServer->get_current_votes_by(pi, shortTermPublicKey);
- // Serialize response
- std::stringstream buffer;
- std::string data;
- buffer << pi;
- BinarySizeT sizeOfVector(votes.size());
- buffer << sizeOfVector;
- for (size_t i = 0; i < sizeOfVector.val(); i++)
- buffer << votes[i];
- data = buffer.str();
- // Send response
- mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
- }
- void PrsonaServerWebSocketHandler::get_all_current_votes(
- struct mg_connection *conn) const
- {
- // Retrieve value
- Proof pi;
- std::vector<std::vector<TwistBipoint>> votes = prsonaServer->get_all_current_votes(pi);
- // Serialize response
- std::stringstream buffer;
- std::string data;
- buffer << pi;
- BinarySizeT sizeOfVector(votes.size());
- buffer << sizeOfVector;
- for (size_t i = 0; i < sizeOfVector.val(); i++)
- for (size_t j = 0; j < sizeOfVector.val(); j++)
- buffer << votes[i][j];
- data = buffer.str();
- // Send response
- mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
- }
- void PrsonaServerWebSocketHandler::get_current_user_encrypted_tally(
- struct mg_connection *conn,
- const char *filename) const
- {
- std::ifstream file(filename);
- // Un-serialize request
- Twistpoint shortTermPublicKey;
- file >> shortTermPublicKey;
- // Retrieve value
- Proof pi;
- EGCiphertext tally = prsonaServer->get_current_user_encrypted_tally(pi, shortTermPublicKey);
- // Serialize response
- std::stringstream buffer;
- std::string data;
- buffer << pi;
- buffer << tally;
- data = buffer.str();
- // Send response
- mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
- }
- void PrsonaServerWebSocketHandler::get_current_server_encrypted_tally(
- struct mg_connection *conn,
- const char *filename) const
- {
- std::ifstream file(filename);
- // Un-serialize request
- Twistpoint shortTermPublicKey;
- file >> shortTermPublicKey;
- // Retrieve value
- Proof pi;
- CurveBipoint tally = prsonaServer->get_current_server_encrypted_tally(pi, shortTermPublicKey);
- // Serialize response
- std::stringstream buffer;
- std::string data;
- buffer << pi;
- buffer << tally;
- data = buffer.str();
- // Send response
- mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
- }
- void PrsonaServerWebSocketHandler::get_current_pseudonyms(
- struct mg_connection *conn) const
- {
- // Retrieve value
- Proof pi;
- std::vector<Twistpoint> pseudonyms = prsonaServer->get_current_pseudonyms(pi);
- // Serialize response
- std::stringstream buffer;
- std::string data;
- buffer << pi;
- BinarySizeT sizeOfVector(pseudonyms.size());
- buffer << sizeOfVector;
- for (size_t i = 0; i < sizeOfVector.val(); i++)
- buffer << pseudonyms[i];
- data = buffer.str();
- // Send response
- mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
- }
- /*
- * PROOF COMMITMENT GETTERS
- */
- void PrsonaServerWebSocketHandler::get_vote_row_commitment(
- struct mg_connection *conn,
- const char *filename) const
- {
- std::ifstream file(filename);
- // Un-serialize request
- Twistpoint shortTermPublicKey;
- file >> shortTermPublicKey;
- // Retrieve value
- Proof pi = prsonaServer->get_vote_row_commitment(shortTermPublicKey);
- // Serialize response
- std::stringstream buffer;
- std::string data;
- buffer << pi;
- data = buffer.str();
- // Send response
- mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
- }
- void PrsonaServerWebSocketHandler::get_vote_matrix_commitment(
- struct mg_connection *conn) const
- {
- // Retrieve value
- Proof pi = prsonaServer->get_vote_matrix_commitment();
- // Serialize response
- std::stringstream buffer;
- std::string data;
- buffer << pi;
- data = buffer.str();
- // Send response
- mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
- }
- void PrsonaServerWebSocketHandler::get_user_tally_commitment(
- struct mg_connection *conn,
- const char *filename) const
- {
- std::ifstream file(filename);
- // Un-serialize request
- Twistpoint shortTermPublicKey;
- file >> shortTermPublicKey;
- // Retrieve value
- Proof pi = prsonaServer->get_user_tally_commitment(shortTermPublicKey);
- // Serialize response
- std::stringstream buffer;
- std::string data;
- buffer << pi;
- data = buffer.str();
- // Send response
- mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
- }
- void PrsonaServerWebSocketHandler::get_server_tally_commitment(
- struct mg_connection *conn,
- const char *filename) const
- {
- std::ifstream file(filename);
- // Un-serialize request
- Twistpoint shortTermPublicKey;
- file >> shortTermPublicKey;
- // Retrieve value
- Proof pi = prsonaServer->get_server_tally_commitment(shortTermPublicKey);
- // Serialize response
- std::stringstream buffer;
- std::string data;
- buffer << pi;
- data = buffer.str();
- // Send response
- mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
- }
- void PrsonaServerWebSocketHandler::get_pseudonyms_commitment(
- struct mg_connection *conn) const
- {
- // Retrieve value
- Proof pi = prsonaServer->get_pseudonyms_commitment();
- // Serialize response
- std::stringstream buffer;
- std::string data;
- buffer << pi;
- data = buffer.str();
- // Send response
- mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
- }
- /*
- * CLIENT INTERACTIONS
- */
- void PrsonaServerWebSocketHandler::add_new_client(
- struct mg_connection *conn,
- const char *filename)
- {
- std::ifstream file(filename);
- // Un-serialize request
- Proof proofOfValidKey;
- file >> proofOfValidKey;
- Twistpoint shortTermPublicKey, empty;
- file >> shortTermPublicKey;
- std::cout << "STPK: " << std::hex << shortTermPublicKey << std::endl;
- std::vector<size_t> bandwidthData(2);
- // Obtain global update lock
- std::unique_lock<std::mutex> updateLock(updateMtx, std::defer_lock);
- obtain_update_locks(updateLock, serverIPs, serverPorts, selfIP, selfPort, bandwidthData);
- // Add new client to server object
- std::vector<Proof> proofOfValidAddition;
- prsonaServer->add_new_client(proofOfValidAddition, proofOfValidKey, shortTermPublicKey);
- // Share this update with other servers
- std::vector<CurveBipoint> previousVoteTallies;
- std::vector<Twistpoint> currentPseudonyms;
- std::vector<EGCiphertext> currentUserEncryptedTallies;
- std::vector<std::vector<TwistBipoint>> voteMatrix;
- prsonaServer->export_new_user_update(previousVoteTallies, currentPseudonyms, currentUserEncryptedTallies, voteMatrix);
- distribute_new_user_updates(proofOfValidAddition, previousVoteTallies, currentPseudonyms, currentUserEncryptedTallies, voteMatrix);
- // Release global update lock
- release_update_locks(updateLock, serverIPs, serverPorts, selfIP, selfPort, bandwidthData);
- // Serialize response
- std::stringstream buffer;
- std::string data;
- BinarySizeT sizeOfVector(proofOfValidAddition.size());
- buffer << sizeOfVector;
- for (size_t i = 0; i < sizeOfVector.val(); i++)
- buffer << proofOfValidAddition[i];
- data = buffer.str();
- // Send response
- mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
- }
- void PrsonaServerWebSocketHandler::receive_vote(
- CivetServer *civetServer,
- struct mg_connection *conn,
- const char *filename)
- {
- std::ifstream file(filename);
- file.ignore(std::numeric_limits<std::streamsize>::max());
- std::streamsize bandwidthRcv = file.gcount();
- file.clear();
- file.seekg(0, std::ios_base::beg);
- // Un-serialize request
- BinarySizeT sizeOfVector;
- file >> sizeOfVector;
- std::vector<Proof> pi;
- for (size_t i = 0; i < sizeOfVector.val(); i++)
- {
- Proof currProof;
- file >> currProof;
- pi.push_back(currProof);
- }
- file >> sizeOfVector;
- std::vector<TwistBipoint> newVotes;
- for (size_t i = 0; i < sizeOfVector.val(); i++)
- {
- TwistBipoint currVote;
- file >> currVote;
- newVotes.push_back(currVote);
- }
- Twistpoint shortTermPublicKey;
- file >> shortTermPublicKey;
- BinaryBool shouldDeal;
- file >> shouldDeal;
- // If we're dealing this update to the other servers, obtain global update lock
- std::unique_lock<std::mutex> updateLock(updateMtx, std::defer_lock);
- std::vector<size_t> bandwidthData(2);
- std::vector<size_t> bandwidthDataBefore = get_server_log_data(civetServer->getContext());
- std::chrono::high_resolution_clock::time_point wallTimeBefore = std::chrono::high_resolution_clock::now();
- clock_t cpuTimeBefore = clock();
- if (shouldDeal.val())
- obtain_update_locks(updateLock, serverIPs, serverPorts, selfIP, selfPort, bandwidthData);
- // Load votes into server object
- prsonaServer->receive_vote(pi, newVotes, shortTermPublicKey);
- // If we're dealing this update to the other servers, actually do that
- if (shouldDeal.val())
- {
- distribute_new_vote(pi, newVotes, shortTermPublicKey, bandwidthData);
- // Then release the global update lock
- release_update_locks(updateLock, serverIPs, serverPorts, selfIP, selfPort, bandwidthData);
- }
- clock_t cpuTimeAfter = clock();
- std::chrono::high_resolution_clock::time_point wallTimeAfter = std::chrono::high_resolution_clock::now();
- std::vector<size_t> bandwidthDataAfter = get_server_log_data(civetServer->getContext());
- std::vector<double> timingData(2);
- timingData[0] = std::chrono::duration_cast<std::chrono::duration<double>>(wallTimeAfter - wallTimeBefore).count();
- timingData[1] = ((double)(cpuTimeAfter - cpuTimeBefore)) / CLOCKS_PER_SEC;
- bandwidthData[0] += bandwidthDataAfter[0] - bandwidthDataBefore[0] + bandwidthRcv;
- bandwidthData[1] += bandwidthDataAfter[1] - bandwidthDataBefore[1];
- write_log_data(voteOutputMtx, voteOutputFilename, timingData, bandwidthData);
- write_usage_data(usageMtx, usageFilename);
- // Notify client their request has been completed
- mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
- }
- /*
- * DISTRIBUTION HELPERS FOR CLIENT INTERACTIONS
- */
- void PrsonaServerWebSocketHandler::distribute_new_user_updates(
- const std::vector<Proof>& proofOfValidAddition,
- const std::vector<CurveBipoint>& previousVoteTallies,
- const std::vector<Twistpoint>& currentPseudonyms,
- const std::vector<EGCiphertext>& currentUserEncryptedTallies,
- const std::vector<std::vector<TwistBipoint>>& voteMatrix) const
- {
- struct synchronization_tool sync;
- // Serialize data
- std::stringstream buffer;
- std::string data;
- BinarySizeT sizeOfVector;
- sizeOfVector.set(proofOfValidAddition.size());
- buffer << sizeOfVector;
- for (size_t i = 0; i < sizeOfVector.val(); i++)
- buffer << proofOfValidAddition[i];
- sizeOfVector.set(previousVoteTallies.size());
- buffer << sizeOfVector;
- for (size_t i = 0; i < sizeOfVector.val(); i++)
- buffer << previousVoteTallies[i];
- sizeOfVector.set(currentPseudonyms.size());
- buffer << sizeOfVector;
- for (size_t i = 0; i < sizeOfVector.val(); i++)
- buffer << currentPseudonyms[i];
- sizeOfVector.set(currentUserEncryptedTallies.size());
- buffer << sizeOfVector;
- for (size_t i = 0; i < sizeOfVector.val(); i++)
- buffer << currentUserEncryptedTallies[i];
- sizeOfVector.set(voteMatrix.size());
- buffer << sizeOfVector;
- for (size_t i = 0; i < sizeOfVector.val(); i++)
- for (size_t j = 0; j < sizeOfVector.val(); j++)
- buffer << voteMatrix[i][j];
- data = buffer.str();
- // Connect to each server and give them data on the new user
- size_t i = 0;
- while (i < serverIPs.size())
- {
- if (serverIPs[i] == selfIP && serverPorts[i] == selfPort)
- {
- i++;
- continue;
- }
- struct mg_connection *conn = NULL;
- std::unique_lock<std::mutex> lck(sync.mtx);
- sync.val = 0;
- sync.val2 = 0;
- while (!conn)
- {
- 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);
-
- if (!conn)
- std::cerr << "Couldn't give server " << i << " new user" << std::endl;
- }
- // Send the server the new user data
- mg_websocket_client_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
- mg_websocket_client_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
- // Wait for its response (or the connection to die)
- while (!sync.val2)
- sync.cv.wait(lck);
- // Close connection
- mg_close_connection(conn);
- // Only move forward once we've confirmed we correctly gave the update (or else we risk synchronization issues!)
- if (sync.val)
- i++;
- }
- }
- void PrsonaServerWebSocketHandler::distribute_new_vote(
- std::vector<Proof> pi,
- std::vector<TwistBipoint> newVotes,
- Twistpoint shortTermPublicKey,
- std::vector<size_t>& bandwidthData) const
- {
- struct synchronization_tool sync;
- // Serialize data
- std::stringstream buffer;
- std::string data;
- BinarySizeT sizeOfVector;
- sizeOfVector.set(pi.size());
- buffer << sizeOfVector;
- for (size_t i = 0; i < sizeOfVector.val(); i++)
- buffer << pi[i];
- sizeOfVector.set(newVotes.size());
- buffer << sizeOfVector;
- for (size_t i = 0; i < sizeOfVector.val(); i++)
- buffer << newVotes[i];
- buffer << shortTermPublicKey;
- BinaryBool flag(false);
- buffer << flag;
- data = buffer.str();
- // Connect to each server and give them the new votes
- size_t i = 0;
- while (i < serverIPs.size())
- {
- if (serverIPs[i] == selfIP && serverPorts[i] == selfPort)
- {
- i++;
- continue;
- }
- struct mg_connection *conn = NULL;
- std::unique_lock<std::mutex> syncLock(sync.mtx);
- sync.val = 0;
- sync.val2 = 0;
- while (!conn)
- {
- 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);
- if (!conn)
- std::cerr << "Couldn't give server " << i << " new vote data" << std::endl;
- }
- std::vector<size_t> bandwidthDataBefore = get_conn_log_data(mg_get_context(conn), false);
-
- // Send the server the new vote data
- mg_websocket_client_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
- mg_websocket_client_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
- // Wait for its response (or the connection to die)
- while (!sync.val2)
- sync.cv.wait(syncLock);
- std::vector<size_t> bandwidthDataAfter = get_conn_log_data(mg_get_context(conn), false);
- bandwidthData[0] += bandwidthDataAfter[0] - bandwidthDataBefore[0];
- bandwidthData[1] += bandwidthDataAfter[1] - bandwidthDataBefore[1];
- // Close connection
- mg_close_connection(conn);
- // Only move forward once we've confirmed we correctly gave the update (or else we risk synchronization issues!)
- if (sync.val)
- i++;
- }
- }
- void PrsonaServerWebSocketHandler::import_new_user_update(
- struct mg_connection *conn,
- const char *filename)
- {
- std::vector<Proof> proofOfValidAddition;
- std::vector<CurveBipoint> previousVoteTallies;
- std::vector<Twistpoint> currentPseudonyms;
- std::vector<EGCiphertext> currentUserEncryptedTallies;
- std::vector<std::vector<TwistBipoint>> voteMatrix;
- std::ifstream file(filename);
- // Un-serialize request
- BinarySizeT sizeOfVector;
-
- file >> sizeOfVector;
- for (size_t i = 0; i < sizeOfVector.val(); i++)
- {
- Proof currProof;
- file >> currProof;
- proofOfValidAddition.push_back(currProof);
- }
- file >> sizeOfVector;
- for (size_t i = 0; i < sizeOfVector.val(); i++)
- {
- CurveBipoint currTally;
- file >> currTally;
- previousVoteTallies.push_back(currTally);
- }
- file >> sizeOfVector;
- for (size_t i = 0; i < sizeOfVector.val(); i++)
- {
- Twistpoint currNym;
- file >> currNym;
- currentPseudonyms.push_back(currNym);
- }
- file >> sizeOfVector;
- for (size_t i = 0; i < sizeOfVector.val(); i++)
- {
- EGCiphertext currTally;
- file >> currTally;
- currentUserEncryptedTallies.push_back(currTally);
- }
- file >> sizeOfVector;
- for (size_t i = 0; i < sizeOfVector.val(); i++)
- {
- std::vector<TwistBipoint> currRow;
- for (size_t j = 0; j < sizeOfVector.val(); j++)
- {
- TwistBipoint currVote;
- file >> currVote;
- currRow.push_back(currVote);
- }
- voteMatrix.push_back(currRow);
- }
- // Load data into server object
- prsonaServer->import_new_user_update(proofOfValidAddition, previousVoteTallies, currentPseudonyms, currentUserEncryptedTallies, voteMatrix);
- // Acknowledge successful request
- mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
- }
- /*
- * CONSTRUCTOR HELPERS
- */
- void PrsonaServerWebSocketHandler::get_bgn_details(
- struct mg_connection *conn) const
- {
- // Retrieve value
- const BGN& sharedBGN = prsonaServer->get_bgn_details();
- // Serialize response
- std::stringstream buffer;
- std::string data;
- buffer << sharedBGN;
- data = buffer.str();
- // Send response
- mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
- }
- void PrsonaServerWebSocketHandler::add_seed_to_generator(
- struct mg_connection *conn,
- const char *filename,
- bool fresh) const
- {
- std::ifstream file(filename);
- // Un-serialize request
- Twistpoint currGenerator;
- file >> currGenerator;
- // Retrieve value
- std::vector<Proof> pi;
- if (fresh)
- currGenerator = prsonaServer->add_curr_seed_to_generator(pi, currGenerator);
- else
- currGenerator = prsonaServer->add_rand_seed_to_generator(pi, currGenerator);
- // Serialize response
- std::stringstream buffer;
- std::string data;
- buffer << pi[0];
- buffer << currGenerator;
- data = buffer.str();
- // Send response
- mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
- }
- void PrsonaServerWebSocketHandler::set_generator(
- const char *filename,
- bool fresh)
- {
- std::ifstream file(filename);
- // Un-serialize request
- BinarySizeT sizeOfVector;
- file >> sizeOfVector;
- std::vector<Proof> pi;
- for (size_t i = 0; i < sizeOfVector.val(); i++)
- {
- Proof currProof;
- file >> currProof;
- pi.push_back(currProof);
- }
- Twistpoint newGenerator;
- file >> newGenerator;
- // Load value into server object
- if (fresh)
- prsonaServer->initialize_fresh_generator(pi, newGenerator);
- else
- prsonaServer->set_EG_blind_generator(pi, newGenerator);
- }
- /*
- * EPOCH ROUNDS
- */
- void PrsonaServerWebSocketHandler::build_up_midway_pseudonyms(
- CivetServer *civetServer,
- struct mg_connection *conn,
- const char *filename)
- {
- std::vector<std::vector<Proof>> generatorProofHolder;
- std::vector<Proof> generatorProof;
- Twistpoint nextGenerator;
- // Un-serialize request
- ssize_t bandwidthRcv = read_epoch_initiator_string(filename, generatorProof, nextGenerator);
- generatorProofHolder.push_back(generatorProof);
- std::vector<std::vector<std::vector<Proof>>> pi;
- pi.push_back(generatorProofHolder);
- std::vector<std::vector<std::vector<Twistpoint>>> permutationCommits;
- std::vector<std::vector<std::vector<Twistpoint>>> freshPseudonymCommits;
- std::vector<std::vector<std::vector<Twistpoint>>> freshPseudonymSeedCommits;
- std::vector<std::vector<std::vector<CurveBipoint>>> serverTallyCommits;
- std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>> partwayVoteMatrixCommits;
- std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>> finalVoteMatrixCommits;
- std::vector<size_t> bandwidthData(2);
- std::vector<std::vector<size_t>> otherBandwidthDataBefore;
- std::vector<size_t> serverBandwidthDataBefore = get_server_log_data(civetServer->getContext());
- std::chrono::high_resolution_clock::time_point wallTimeBefore = std::chrono::high_resolution_clock::now();
- clock_t cpuTimeBefore = clock();
- // Do actual epoch calculation
- prsonaServer->build_up_midway_pseudonyms(pi, permutationCommits, freshPseudonymCommits, freshPseudonymSeedCommits, serverTallyCommits, partwayVoteMatrixCommits, finalVoteMatrixCommits, nextGenerator);
- std::vector<std::vector<Twistpoint>> userTallyMaskCommits, userTallyMessageCommits, userTallySeedCommits;
- // Serialize update data
- 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);
- struct synchronization_tool sync;
- std::vector<struct mg_connection *> conns;
- // Connect to all other servers (roughly in parallel)
- std::unique_lock<std::mutex> lck(sync.mtx);
- sync.val = 1;
- for (size_t i = 0; i < serverIPs.size(); i++)
- {
- if (serverIPs[i] == selfIP && serverPorts[i] == selfPort)
- continue;
- // Send them update data
- struct mg_connection *currConn = distribute_epoch_updates(serverIPs[i], serverPorts[i], data, &sync);
- conns.push_back(currConn);
- otherBandwidthDataBefore.push_back(get_conn_log_data(mg_get_context(currConn), false));
- }
- // Wait for all to acknowledge receipt of the update data
- while (sync.val < serverIPs.size())
- sync.cv.wait(lck);
- // Close connections
- for (size_t i = 0; i < conns.size(); i++)
- {
- std::vector<size_t> currBandwidthDataAfter = get_conn_log_data(mg_get_context(conns[i]), false);
- bandwidthData[0] += currBandwidthDataAfter[0] - otherBandwidthDataBefore[i][0];
- bandwidthData[1] += currBandwidthDataAfter[1] - otherBandwidthDataBefore[i][1];
- mg_close_connection(conns[i]);
- }
- clock_t cpuTimeAfter = clock();
- std::chrono::high_resolution_clock::time_point wallTimeAfter = std::chrono::high_resolution_clock::now();
- std::vector<size_t> serverBandwidthDataAfter = get_server_log_data(civetServer->getContext());
- std::vector<double> timingData(2);
- timingData[0] = std::chrono::duration_cast<std::chrono::duration<double>>(wallTimeAfter - wallTimeBefore).count();
- timingData[1] = ((double)(cpuTimeAfter - cpuTimeBefore)) / CLOCKS_PER_SEC;
- bandwidthData[0] += serverBandwidthDataAfter[0] - serverBandwidthDataBefore[0] + bandwidthRcv;
- bandwidthData[1] += serverBandwidthDataAfter[1] - serverBandwidthDataBefore[1];
- write_log_data(buildUpOutputMtx, buildUpOutputFilename, timingData, bandwidthData);
- write_usage_data(usageMtx, usageFilename);
-
- // Serialize response
- data = make_epoch_initiator_string(pi[0][0], nextGenerator);
- // Send response
- mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), data.length());
- mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
- }
- void PrsonaServerWebSocketHandler::break_down_midway_pseudonyms(
- CivetServer *civetServer,
- struct mg_connection *conn,
- const char *filename)
- {
- std::vector<Proof> generatorProof;
- Twistpoint nextGenerator;
- // Un-serialize request
- ssize_t bandwidthRcv = read_epoch_initiator_string(filename, generatorProof, nextGenerator);
-
- std::vector<std::vector<std::vector<Proof>>> pi;
- std::vector<std::vector<std::vector<Twistpoint>>> permutationCommits;
- std::vector<std::vector<std::vector<Twistpoint>>> freshPseudonymCommits;
- std::vector<std::vector<std::vector<Twistpoint>>> freshPseudonymSeedCommits;
- std::vector<std::vector<std::vector<CurveBipoint>>> serverTallyCommits;
- std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>> partwayVoteMatrixCommits;
- std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>> finalVoteMatrixCommits;
- std::vector<std::vector<std::vector<Twistpoint>>> userTallyMaskCommits;
- std::vector<std::vector<std::vector<Twistpoint>>> userTallyMessageCommits;
- std::vector<std::vector<std::vector<Twistpoint>>> userTallySeedCommits;
- std::vector<size_t> bandwidthData(2);
- std::vector<std::vector<size_t>> otherBandwidthDataBefore;
- std::vector<size_t> serverBandwidthDataBefore = get_server_log_data(civetServer->getContext());
- std::chrono::high_resolution_clock::time_point wallTimeBefore = std::chrono::high_resolution_clock::now();
- clock_t cpuTimeBefore = clock();
- // Do actual epoch calculation
- prsonaServer->break_down_midway_pseudonyms(generatorProof, pi, permutationCommits, freshPseudonymCommits, freshPseudonymSeedCommits, serverTallyCommits, partwayVoteMatrixCommits, finalVoteMatrixCommits, userTallyMaskCommits, userTallyMessageCommits, userTallySeedCommits, nextGenerator);
- // Serialize update data
- 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);
- struct synchronization_tool sync;
- std::vector<struct mg_connection *> conns;
- // Connect to all other servers (roughly in parallel)
- std::unique_lock<std::mutex> lck(sync.mtx);
- sync.val = 1;
- for (size_t i = 0; i < serverIPs.size(); i++)
- {
- if (serverIPs[i] == selfIP && serverPorts[i] == selfPort)
- continue;
- // Send them update data
- struct mg_connection *currConn = distribute_epoch_updates(serverIPs[i], serverPorts[i], data, &sync);
- conns.push_back(currConn);
- otherBandwidthDataBefore.push_back(get_conn_log_data(mg_get_context(currConn), false));
- }
- // Wait for all to acknowledge receipt of the update data
- while (sync.val < serverIPs.size())
- sync.cv.wait(lck);
- // Close connections
- for (size_t i = 0; i < conns.size(); i++)
- {
- std::vector<size_t> currBandwidthDataAfter = get_conn_log_data(mg_get_context(conns[i]), false);
- bandwidthData[0] += currBandwidthDataAfter[0] - otherBandwidthDataBefore[i][0];
- bandwidthData[1] += currBandwidthDataAfter[1] - otherBandwidthDataBefore[i][1];
- mg_close_connection(conns[i]);
- }
- clock_t cpuTimeAfter = clock();
- std::chrono::high_resolution_clock::time_point wallTimeAfter = std::chrono::high_resolution_clock::now();
- std::vector<size_t> serverBandwidthDataAfter = get_server_log_data(civetServer->getContext());
- std::vector<double> timingData(2);
- timingData[0] = std::chrono::duration_cast<std::chrono::duration<double>>(wallTimeAfter - wallTimeBefore).count();
- timingData[1] = ((double)(cpuTimeAfter - cpuTimeBefore)) / CLOCKS_PER_SEC;
- bandwidthData[0] += serverBandwidthDataAfter[0] - serverBandwidthDataBefore[0] + bandwidthRcv;
- bandwidthData[1] += serverBandwidthDataAfter[1] - serverBandwidthDataBefore[1];
- write_log_data(breakDownOutputMtx, breakDownOutputFilename, timingData, bandwidthData);
- write_usage_data(usageMtx, usageFilename);
- // Keep our epoch value up-to-date
- epochNum.fetch_add(1);
- // Tell initiator we have finished
- mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
- }
- void PrsonaServerWebSocketHandler::accept_epoch_updates(
- CivetServer *civetServer,
- struct mg_connection *conn,
- const char *filename)
- {
- std::vector<std::vector<Proof>> pi;
- std::vector<std::vector<Twistpoint>> permutationCommits;
- std::vector<std::vector<Twistpoint>> freshPseudonymCommits;
- std::vector<std::vector<Twistpoint>> freshPseudonymSeedCommits;
- std::vector<std::vector<CurveBipoint>> serverTallyCommits;
- std::vector<std::vector<std::vector<TwistBipoint>>> partwayVoteMatrixCommits;
- std::vector<std::vector<std::vector<TwistBipoint>>> finalVoteMatrixCommits;
- std::vector<std::vector<Twistpoint>> userTallyMaskCommits;
- std::vector<std::vector<Twistpoint>> userTallyMessageCommits;
- std::vector<std::vector<Twistpoint>> userTallySeedCommits;
- Twistpoint nextGenerator;
- bool doUserTallies;
- // Un-serialize request
- ssize_t bandwidthRcv = read_epoch_update_string(filename, pi, permutationCommits, freshPseudonymCommits, freshPseudonymSeedCommits, serverTallyCommits, partwayVoteMatrixCommits, finalVoteMatrixCommits, userTallyMaskCommits, userTallyMessageCommits, userTallySeedCommits, nextGenerator, doUserTallies);
- std::vector<size_t> bandwidthDataBefore = get_server_log_data(civetServer->getContext());
- std::chrono::high_resolution_clock::time_point wallTimeBefore = std::chrono::high_resolution_clock::now();
- clock_t cpuTimeBefore = clock();
- // Load data into server object
- prsonaServer->accept_epoch_updates(pi, permutationCommits, freshPseudonymCommits, freshPseudonymSeedCommits, serverTallyCommits, partwayVoteMatrixCommits, finalVoteMatrixCommits, userTallyMaskCommits, userTallyMessageCommits, userTallySeedCommits, nextGenerator, doUserTallies);
- clock_t cpuTimeAfter = clock();
- std::chrono::high_resolution_clock::time_point wallTimeAfter = std::chrono::high_resolution_clock::now();
- std::vector<size_t> bandwidthDataAfter = get_server_log_data(civetServer->getContext());
- std::vector<double> timingData(2);
- timingData[0] = std::chrono::duration_cast<std::chrono::duration<double>>(wallTimeAfter - wallTimeBefore).count();
- timingData[1] = ((double)(cpuTimeAfter - cpuTimeBefore)) / CLOCKS_PER_SEC;
- std::vector<size_t> bandwidthData(2);
- bandwidthData[0] = bandwidthDataAfter[0] - bandwidthDataBefore[0] + bandwidthRcv;
- bandwidthData[1] = bandwidthDataAfter[1] - bandwidthDataBefore[1];
- write_log_data(updateOutputMtx, updateOutputFilename, timingData, bandwidthData);
- write_usage_data(usageMtx, usageFilename);
- // Acknowledge receipt of request
- mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
- }
- /*
- * DISTRIBUTED BGN
- */
- void PrsonaServerWebSocketHandler::get_partial_decryption(
- struct mg_connection *conn) const
- {
- // Send back ACK (since all we do here is simulate distributed BGN)
- mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
- }
- void PrsonaServerWebSocketHandler::receive_tallied_scores(
- struct mg_connection *conn,
- const char *filename)
- {
- std::ifstream file(filename);
- // Un-serialize request
- std::vector<EGCiphertext> userScores;
- std::vector<CurveBipoint> serverScores;
- BinarySizeT sizeOfVector;
- file >> sizeOfVector;
- for (size_t i = 0; i < sizeOfVector.val(); i++)
- {
- EGCiphertext currScore;
- file >> currScore;
- userScores.push_back(currScore);
- }
- for (size_t i = 0; i < sizeOfVector.val(); i++)
- {
- CurveBipoint currScore;
- file >> currScore;
- serverScores.push_back(currScore);
- }
- // Load into server object
- prsonaServer->receive_tallied_scores(userScores, serverScores);
- // Acknowledge receipt of data
- mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
- }
|