server.cpp 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940
  1. #include <iostream>
  2. #include <thread>
  3. #include "server.hpp"
  4. /********************
  5. * PUBLIC FUNCTIONS *
  6. ********************/
  7. /*
  8. * CONSTRUCTORS
  9. */
  10. // Used to generate the first server; instantiates BGN for the first time
  11. PrsonaServer::PrsonaServer(
  12. size_t numServers)
  13. : numServers(numServers)
  14. {
  15. currentSeed.set_random();
  16. decryptMtx = new std::mutex();
  17. }
  18. // Used for all other servers, so they have the same BGN parameters
  19. PrsonaServer::PrsonaServer(
  20. size_t numServers,
  21. const BGN& otherBgn)
  22. : numServers(numServers), bgnSystem(otherBgn)
  23. {
  24. currentSeed.set_random();
  25. decryptMtx = new std::mutex();
  26. }
  27. PrsonaServer::PrsonaServer(
  28. const PrsonaServer& other)
  29. {
  30. copy(other);
  31. }
  32. PrsonaServer::PrsonaServer(
  33. PrsonaServer&& other)
  34. {
  35. numServers = std::move(other.numServers);
  36. decryptMtx = other.decryptMtx;
  37. other.decryptMtx = NULL;
  38. bgnSystem = std::move(other.bgnSystem);
  39. currentSeed = std::move(other.currentSeed);
  40. nextSeed = std::move(other.nextSeed);
  41. currentGeneratorProof = std::move(other.currentGeneratorProof);
  42. currentFreshGenerator = std::move(other.currentFreshGenerator);
  43. previousVoteTallies = std::move(other.previousVoteTallies);
  44. currentPseudonyms = std::move(other.currentPseudonyms);
  45. currentUserEncryptedTallies = std::move(other.currentUserEncryptedTallies);
  46. voteMatrix = std::move(other.voteMatrix);
  47. }
  48. PrsonaServer &PrsonaServer::operator=(
  49. const PrsonaServer& other)
  50. {
  51. if (&other != this)
  52. {
  53. remove();
  54. copy(other);
  55. }
  56. return *this;
  57. }
  58. PrsonaServer &PrsonaServer::operator=(
  59. PrsonaServer&& other)
  60. {
  61. if (&other != this)
  62. {
  63. remove();
  64. numServers = std::move(other.numServers);
  65. decryptMtx = other.decryptMtx;
  66. other.decryptMtx = new std::mutex();
  67. bgnSystem = std::move(other.bgnSystem);
  68. currentSeed = std::move(other.currentSeed);
  69. nextSeed = std::move(other.nextSeed);
  70. currentGeneratorProof = std::move(other.currentGeneratorProof);
  71. currentFreshGenerator = std::move(other.currentFreshGenerator);
  72. previousVoteTallies = std::move(other.previousVoteTallies);
  73. currentPseudonyms = std::move(other.currentPseudonyms);
  74. currentUserEncryptedTallies = std::move(other.currentUserEncryptedTallies);
  75. voteMatrix = std::move(other.voteMatrix);
  76. }
  77. return *this;
  78. }
  79. PrsonaServer::~PrsonaServer()
  80. {
  81. remove();
  82. }
  83. void PrsonaServer::copy(
  84. const PrsonaServer& other)
  85. {
  86. numServers = other.numServers;
  87. decryptMtx = new std::mutex();
  88. bgnSystem = other.bgnSystem;
  89. currentSeed = other.currentSeed;
  90. nextSeed = other.nextSeed;
  91. currentGeneratorProof = other.currentGeneratorProof;
  92. currentFreshGenerator = other.currentFreshGenerator;
  93. previousVoteTallies = other.previousVoteTallies;
  94. currentPseudonyms = other.currentPseudonyms;
  95. currentUserEncryptedTallies = other.currentUserEncryptedTallies;
  96. voteMatrix = other.voteMatrix;
  97. }
  98. void PrsonaServer::remove()
  99. {
  100. delete decryptMtx;
  101. decryptMtx = NULL;
  102. }
  103. /*
  104. * BASIC PUBLIC SYSTEM INFO GETTERS
  105. */
  106. BGNPublicKey PrsonaServer::get_bgn_public_key() const
  107. {
  108. return bgnSystem.get_public_key();
  109. }
  110. size_t PrsonaServer::get_num_clients() const
  111. {
  112. return currentPseudonyms.size();
  113. }
  114. size_t PrsonaServer::get_num_servers() const
  115. {
  116. return numServers;
  117. }
  118. Twistpoint PrsonaServer::get_fresh_generator(
  119. std::vector<Proof>& pi) const
  120. {
  121. pi = currentGeneratorProof;
  122. return currentFreshGenerator;
  123. }
  124. /*
  125. * FRESH GENERATOR CALCULATION
  126. */
  127. // To calculate the current epoch's generator, start from the base generator,
  128. // then have every server call this function on it iteratively (in any order).
  129. Twistpoint PrsonaServer::add_curr_seed_to_generator(
  130. std::vector<Proof>& pi,
  131. const Twistpoint& currGenerator) const
  132. {
  133. pi.push_back(add_to_generator_proof(currGenerator, currentSeed));
  134. return currGenerator * currentSeed;
  135. }
  136. // To calculate the next epoch's generator, start from the base generator,
  137. // then have every server call this function on it iteratively (in any order).
  138. Twistpoint PrsonaServer::add_next_seed_to_generator(
  139. std::vector<Proof>& pi,
  140. const Twistpoint& currGenerator) const
  141. {
  142. pi.push_back(add_to_generator_proof(currGenerator, nextSeed));
  143. return currGenerator * nextSeed;
  144. }
  145. /*
  146. * ENCRYPTED DATA GETTERS
  147. */
  148. /* Call this in order to get the current encrypted votes cast by a given user
  149. * (who is identified by their short term public key).
  150. * In practice, this is intended for clients,
  151. * who need to know their current votes in order to rerandomize them. */
  152. std::vector<TwistBipoint> PrsonaServer::get_current_votes_by(
  153. Proof& pi,
  154. const Twistpoint& shortTermPublicKey) const
  155. {
  156. std::vector<TwistBipoint> retval;
  157. size_t voteSubmitter = binary_search(shortTermPublicKey);
  158. retval = voteMatrix[voteSubmitter];
  159. pi = generate_valid_vote_row_proof(retval);
  160. return retval;
  161. }
  162. std::vector<std::vector<TwistBipoint>> PrsonaServer::get_all_current_votes(
  163. Proof& pi) const
  164. {
  165. pi = generate_valid_vote_matrix_proof(voteMatrix);
  166. return voteMatrix;
  167. }
  168. /* Call this in order to get the current encrypted tally of a given user
  169. * (who is identified by their short term public key).
  170. * In practice, this is intended for clients, so that the servers vouch
  171. * for their ciphertexts being valid as part of their reputation proofs. */
  172. EGCiphertext PrsonaServer::get_current_user_encrypted_tally(
  173. Proof& pi,
  174. const Twistpoint& shortTermPublicKey) const
  175. {
  176. EGCiphertext retval;
  177. size_t tallyOwner = binary_search(shortTermPublicKey);
  178. retval = currentUserEncryptedTallies[tallyOwner];
  179. pi = generate_valid_user_tally_proof(retval);
  180. return retval;
  181. }
  182. CurveBipoint PrsonaServer::get_current_server_encrypted_tally(
  183. Proof& pi,
  184. const Twistpoint& shortTermPublicKey) const
  185. {
  186. CurveBipoint retval;
  187. size_t tallyOwner = binary_search(shortTermPublicKey);
  188. retval = previousVoteTallies[tallyOwner];
  189. pi = generate_valid_server_tally_proof(retval);
  190. return retval;
  191. }
  192. std::vector<Twistpoint> PrsonaServer::get_current_pseudonyms(
  193. Proof& pi) const
  194. {
  195. pi = generate_valid_pseudonyms_proof(currentPseudonyms);
  196. return currentPseudonyms;
  197. }
  198. std::vector<Twistpoint> PrsonaServer::get_current_pseudonyms() const
  199. {
  200. return currentPseudonyms;
  201. }
  202. /*
  203. * PROOF COMMITMENT GETTERS
  204. */
  205. Proof PrsonaServer::get_vote_row_commitment(
  206. const Twistpoint& request) const
  207. {
  208. size_t requestID = binary_search(request);
  209. return generate_valid_vote_row_proof(voteMatrix[requestID]);
  210. }
  211. Proof PrsonaServer::get_vote_matrix_commitment() const
  212. {
  213. return generate_valid_vote_matrix_proof(voteMatrix);
  214. }
  215. Proof PrsonaServer::get_user_tally_commitment(
  216. const Twistpoint& request) const
  217. {
  218. size_t requestID = binary_search(request);
  219. return generate_valid_user_tally_proof(currentUserEncryptedTallies[requestID]);
  220. }
  221. Proof PrsonaServer::get_server_tally_commitment(
  222. const Twistpoint& request) const
  223. {
  224. size_t requestID = binary_search(request);
  225. return generate_valid_server_tally_proof(previousVoteTallies[requestID]);
  226. }
  227. Proof PrsonaServer::get_pseudonyms_commitment() const
  228. {
  229. return generate_valid_pseudonyms_proof(currentPseudonyms);
  230. }
  231. void PrsonaServer::print_current_commitments() const
  232. {
  233. std::stringstream helper;
  234. std::cout << "Commitments:\n";
  235. std::cout << "User tallies:\n";
  236. for (size_t i = 0; i < currentUserEncryptedTallies.size(); i++)
  237. {
  238. helper.str("");
  239. helper << currentUserEncryptedTallies[i];
  240. std::cout << "Tally " << i + 1 << " of " << currentUserEncryptedTallies.size() << ": " << std::hex << oracle(helper.str()) << std::dec << "\n";
  241. }
  242. std::cout << "Server tallies:\n";
  243. for (size_t i = 0; i < previousVoteTallies.size(); i++)
  244. {
  245. helper.str("");
  246. helper << previousVoteTallies[i];
  247. std::cout << "Tally " << i + 1 << " of " << previousVoteTallies.size() << ": " << std::hex << oracle(helper.str()) << std::dec << "\n";
  248. }
  249. }
  250. /*
  251. * CLIENT INTERACTIONS
  252. */
  253. /* Add a new client (who is identified only by their short term public key)
  254. * One server will do this, then ask all other servers to import their
  255. * (proven) exported data. */
  256. void PrsonaServer::add_new_client(
  257. std::vector<Proof>& proofOfValidAddition,
  258. const Proof& proofOfValidKey,
  259. const Twistpoint& shortTermPublicKey)
  260. {
  261. if (!verify_ownership_proof(proofOfValidKey, currentFreshGenerator, shortTermPublicKey))
  262. {
  263. std::cerr << "Could not verify proof of valid key." << std::endl;
  264. return;
  265. }
  266. currentPseudonyms.push_back(shortTermPublicKey);
  267. // The first epoch's score for a new user will be low,
  268. // but will typically converge on an average score quickly
  269. Scalar tallySeed;
  270. CurveBipoint encryptedDefaultTally = bgnSystem.get_public_key().curveEncrypt(tallySeed, DEFAULT_TALLY);
  271. previousVoteTallies.push_back(encryptedDefaultTally);
  272. Scalar seedForUserTally;
  273. seedForUserTally.set_random();
  274. EGCiphertext newUserEncryptedTally;
  275. newUserEncryptedTally.mask = shortTermPublicKey * seedForUserTally;
  276. newUserEncryptedTally.encryptedMessage = currentFreshGenerator * seedForUserTally + elGamalBlindGenerator * DEFAULT_TALLY;
  277. currentUserEncryptedTallies.push_back(newUserEncryptedTally);
  278. // Users are defaulted to casting a neutral vote for others.
  279. TwistBipoint encryptedDefaultVote, encryptedSelfVote;
  280. Scalar currDefaultSeed, currSelfSeed;
  281. encryptedDefaultVote = bgnSystem.get_public_key().twistEncrypt(currDefaultSeed, DEFAULT_VOTE);
  282. encryptedSelfVote = bgnSystem.get_public_key().twistEncrypt(currSelfSeed, Scalar(MAX_ALLOWED_VOTE));
  283. std::vector<TwistBipoint> newRow;
  284. std::vector<Scalar> userVoteSeeds;
  285. std::vector<Scalar> otherVoteSeeds;
  286. for (size_t i = 0; i < voteMatrix.size(); i++)
  287. {
  288. Scalar addedSeed;
  289. encryptedDefaultVote = bgnSystem.get_public_key().rerandomize(addedSeed, encryptedDefaultVote);
  290. currDefaultSeed = currDefaultSeed + addedSeed;
  291. otherVoteSeeds.push_back(Scalar());
  292. otherVoteSeeds[i] = currDefaultSeed;
  293. voteMatrix[i].push_back(encryptedDefaultVote);
  294. encryptedDefaultVote = bgnSystem.get_public_key().rerandomize(addedSeed, encryptedDefaultVote);
  295. currDefaultSeed = currDefaultSeed + addedSeed;
  296. userVoteSeeds.push_back(Scalar());
  297. userVoteSeeds[i] = currDefaultSeed;
  298. newRow.push_back(encryptedDefaultVote);
  299. }
  300. // Because we are adding the new user to the end (and then sorting it),
  301. // this last element (bottom right corner) is always the self vote.
  302. userVoteSeeds.push_back(Scalar());
  303. userVoteSeeds[newRow.size()] = currSelfSeed;
  304. otherVoteSeeds.push_back(Scalar());
  305. otherVoteSeeds[newRow.size()] = currSelfSeed;
  306. newRow.push_back(encryptedSelfVote);
  307. voteMatrix.push_back(newRow);
  308. std::vector<size_t> sortOrder = order_data();
  309. std::vector<Scalar> newUserVoteSeeds;
  310. std::vector<Scalar> newOtherVoteSeeds;
  311. for (size_t i = 0; i < sortOrder.size(); i++)
  312. {
  313. newUserVoteSeeds.push_back(userVoteSeeds[sortOrder[i]]);
  314. newOtherVoteSeeds.push_back(otherVoteSeeds[sortOrder[i]]);
  315. }
  316. proofOfValidAddition = generate_proof_of_added_user(tallySeed, seedForUserTally, newUserVoteSeeds, newOtherVoteSeeds);
  317. }
  318. // Receive a new vote row from a user (identified by short term public key).
  319. bool PrsonaServer::receive_vote(
  320. const std::vector<Proof>& pi,
  321. const std::vector<TwistBipoint>& newVotes,
  322. const Twistpoint& shortTermPublicKey)
  323. {
  324. size_t voteSubmitter = binary_search(shortTermPublicKey);
  325. std::vector<TwistBipoint> oldVotes = voteMatrix[voteSubmitter];
  326. if (!verify_vote_proof(pi, oldVotes, newVotes, shortTermPublicKey))
  327. return false;
  328. voteMatrix[voteSubmitter] = newVotes;
  329. return true;
  330. }
  331. /*********************
  332. * PRIVATE FUNCTIONS *
  333. *********************/
  334. /*
  335. * CONSTRUCTOR HELPERS
  336. */
  337. const BGN& PrsonaServer::get_bgn_details() const
  338. {
  339. return bgnSystem;
  340. }
  341. bool PrsonaServer::initialize_fresh_generator(
  342. const std::vector<Proof>& pi,
  343. const Twistpoint& firstGenerator)
  344. {
  345. if (!verify_generator_proof(pi, firstGenerator, numServers))
  346. {
  347. std::cerr << "Could not verify generator proof, aborting." << std::endl;
  348. return false;
  349. }
  350. currentGeneratorProof = pi;
  351. currentFreshGenerator = firstGenerator;
  352. return true;
  353. }
  354. // To calculate the blind generator for ElGamal, start from the base generator,
  355. // then have every server call this function on it iteratively (in any order).
  356. Twistpoint PrsonaServer::add_rand_seed_to_generator(
  357. std::vector<Proof>& pi,
  358. const Twistpoint& currGenerator) const
  359. {
  360. Scalar lambda;
  361. lambda.set_random();
  362. pi.push_back(add_to_generator_proof(currGenerator, lambda));
  363. return currGenerator * lambda;
  364. }
  365. bool PrsonaServer::set_EG_blind_generator(
  366. const std::vector<Proof>& pi,
  367. const Twistpoint& currGenerator)
  368. {
  369. return PrsonaBase::set_EG_blind_generator(pi, currGenerator, numServers);
  370. }
  371. /*
  372. * SCORE TALLYING
  373. */
  374. void homomorphic_multiplication_r(
  375. void *a,
  376. void *b,
  377. const void *c,
  378. const void *d)
  379. {
  380. BGN *bgnSystem = (BGN *) a;
  381. Quadripoint *dst = (Quadripoint *) b;
  382. const CurveBipoint *x = (const CurveBipoint *) c;
  383. const TwistBipoint *y = (const TwistBipoint *) d;
  384. *dst = bgnSystem->homomorphic_multiplication_no_rerandomize(*x, *y);
  385. }
  386. void homomorphic_addition_r(
  387. void *a,
  388. void *b,
  389. size_t size)
  390. {
  391. BGN *bgnSystem = (BGN *) a;
  392. Quadripoint *arr = (Quadripoint *) b;
  393. switch (size)
  394. {
  395. case 0:
  396. case 1:
  397. return;
  398. case 2:
  399. arr[0] = bgnSystem->homomorphic_addition_no_rerandomize(arr[0], arr[1]);
  400. return;
  401. default:
  402. break;
  403. }
  404. size_t halfSize = size / 2;
  405. std::thread threadA(homomorphic_addition_r, bgnSystem, arr, halfSize);
  406. std::thread threadB(homomorphic_addition_r, bgnSystem, arr + halfSize, size - halfSize);
  407. threadA.join();
  408. threadB.join();
  409. arr[0] = bgnSystem->homomorphic_addition_no_rerandomize(arr[0], arr[halfSize]);
  410. }
  411. void tally_r(
  412. void *a,
  413. void *b,
  414. void *c,
  415. const void *d,
  416. const void *e,
  417. size_t i)
  418. {
  419. std::mutex *decryptMtx = (std::mutex *) a;
  420. BGN *bgnSystem = (BGN *) b;
  421. Scalar *dst = (Scalar *) c;
  422. const std::vector<CurveBipoint> *previousVoteTallies = (const std::vector<CurveBipoint> *) d;
  423. const std::vector<std::vector<TwistBipoint>> *voteMatrix = (const std::vector<std::vector<TwistBipoint>> *) e;
  424. Quadripoint *weightedVotes = new Quadripoint[previousVoteTallies->size()];
  425. std::vector<std::thread> parallelizedMults;
  426. // ZIP
  427. for (size_t j = 0; j < previousVoteTallies->size(); j++)
  428. parallelizedMults.push_back(std::thread(homomorphic_multiplication_r, bgnSystem, weightedVotes + j, &((*previousVoteTallies)[j]), &((*voteMatrix)[j][i])));
  429. for (size_t j = 0; j < parallelizedMults.size(); j++)
  430. parallelizedMults[j].join();
  431. // FOLDL
  432. homomorphic_addition_r(bgnSystem, weightedVotes, previousVoteTallies->size());
  433. // DECRYPT
  434. std::unique_lock<std::mutex> lck(*decryptMtx);
  435. *dst = bgnSystem->decrypt(weightedVotes[0]);
  436. delete [] weightedVotes;
  437. }
  438. /* Calculate scores homomorphically (as an individual server would normally)
  439. * and then decrypt them (which the servers would normally work together to do).
  440. *
  441. * Note that since these calculations are just for us, we don't need to do any
  442. * expensive rerandomizations of intermediate values. */
  443. std::vector<Scalar> PrsonaServer::tally_scores()
  444. {
  445. std::vector<Quadripoint> BGNEncryptedTallies;
  446. Scalar *decryptedTallies = new Scalar[voteMatrix.size()];
  447. std::vector<std::thread> parallelizedTallies;
  448. for (size_t i = 0; i < voteMatrix.size(); i++)
  449. parallelizedTallies.push_back(std::thread(tally_r, decryptMtx, &bgnSystem, decryptedTallies + i, &previousVoteTallies, &voteMatrix, i));
  450. for (size_t i = 0; i < parallelizedTallies.size(); i++)
  451. parallelizedTallies[i].join();
  452. std::vector<Scalar> retval(decryptedTallies, decryptedTallies + voteMatrix.size());
  453. delete [] decryptedTallies;
  454. return retval;
  455. }
  456. /* Calculate what the maximum possible score this round was (that is,
  457. * given the current user weights, what was the highest possible score?).
  458. *
  459. * As with individual scores, this also does the decryption that servers
  460. * would ordinarily work together to form. */
  461. Scalar PrsonaServer::get_max_possible_score()
  462. {
  463. // FOLDL
  464. CurveBipoint currEncryptedVal = previousVoteTallies[0];
  465. for (size_t i = 1; i < previousVoteTallies.size(); i++)
  466. currEncryptedVal = bgnSystem.homomorphic_addition_no_rerandomize(currEncryptedVal, previousVoteTallies[i]);
  467. // DECRYPT
  468. std::unique_lock<std::mutex> lck(*decryptMtx);
  469. Scalar retval = bgnSystem.decrypt(currEncryptedVal);
  470. return retval;
  471. }
  472. void PrsonaServer::receive_tallied_scores(
  473. const std::vector<EGCiphertext>& userTallyScores,
  474. const std::vector<CurveBipoint>& serverTallyScores)
  475. {
  476. currentUserEncryptedTallies = userTallyScores;
  477. previousVoteTallies = serverTallyScores;
  478. }
  479. void PrsonaServer::encrypt(
  480. CurveBipoint& element,
  481. const Scalar& value)
  482. {
  483. bgnSystem.encrypt(element, value);
  484. }
  485. /*
  486. * EPOCH ROUNDS
  487. */
  488. // The first round, going from A_0 to A_0.5
  489. void PrsonaServer::build_up_midway_pseudonyms(
  490. std::vector<std::vector<std::vector<Proof>>>& pi,
  491. std::vector<std::vector<std::vector<Twistpoint>>>& permutationCommits,
  492. std::vector<std::vector<std::vector<Twistpoint>>>& freshPseudonymCommits,
  493. std::vector<std::vector<std::vector<Twistpoint>>>& freshPseudonymSeedCommits,
  494. std::vector<std::vector<std::vector<CurveBipoint>>>& serverTallyCommits,
  495. std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>>& partwayVoteMatrixCommits,
  496. std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>>& finalVoteMatrixCommits,
  497. Twistpoint& nextGenerator)
  498. {
  499. nextSeed.set_random();
  500. std::vector<std::vector<Twistpoint>> currPermutationCommits;
  501. std::vector<std::vector<Twistpoint>> currFreshPseudonymCommits;
  502. std::vector<std::vector<Twistpoint>> currFreshPseudonymSeedCommits;
  503. std::vector<std::vector<CurveBipoint>> currServerTallyCommits;
  504. std::vector<std::vector<std::vector<TwistBipoint>>> currPartwayVoteMatrixCommits;
  505. std::vector<std::vector<std::vector<TwistBipoint>>> currFinalVoteMatrixCommits;
  506. std::vector<std::vector<Twistpoint>> currUserTallyMaskCommits;
  507. std::vector<std::vector<Twistpoint>> currUserTallyMessageCommits;
  508. std::vector<std::vector<Twistpoint>> currUserTallySeedCommits;
  509. pi.push_back(epoch_calculations(currPermutationCommits, currFreshPseudonymCommits, currFreshPseudonymSeedCommits, currServerTallyCommits, currPartwayVoteMatrixCommits, currFinalVoteMatrixCommits, currUserTallyMaskCommits, currUserTallyMessageCommits, currUserTallySeedCommits, nextSeed, nextGenerator, false));
  510. permutationCommits.push_back(currPermutationCommits);
  511. freshPseudonymCommits.push_back(currFreshPseudonymCommits);
  512. freshPseudonymSeedCommits.push_back(currFreshPseudonymSeedCommits);
  513. serverTallyCommits.push_back(currServerTallyCommits);
  514. partwayVoteMatrixCommits.push_back(currPartwayVoteMatrixCommits);
  515. finalVoteMatrixCommits.push_back(currFinalVoteMatrixCommits);
  516. pi[0][0].push_back(add_to_generator_proof(nextGenerator, nextSeed));
  517. nextGenerator = nextGenerator * nextSeed;
  518. }
  519. // In between these rounds, scores are tallied, decrypted,
  520. // and encrypted to fresh user pseudonyms (possible through weird math)
  521. // The second round, going from A_0.5 to A_1
  522. void PrsonaServer::break_down_midway_pseudonyms(
  523. const std::vector<Proof>& generatorProof,
  524. std::vector<std::vector<std::vector<Proof>>>& pi,
  525. std::vector<std::vector<std::vector<Twistpoint>>>& permutationCommits,
  526. std::vector<std::vector<std::vector<Twistpoint>>>& freshPseudonymCommits,
  527. std::vector<std::vector<std::vector<Twistpoint>>>& freshPseudonymSeedCommits,
  528. std::vector<std::vector<std::vector<CurveBipoint>>>& serverTallyCommits,
  529. std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>>& partwayVoteMatrixCommits,
  530. std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>>& finalVoteMatrixCommits,
  531. std::vector<std::vector<std::vector<Twistpoint>>>& userTallyMaskCommits,
  532. std::vector<std::vector<std::vector<Twistpoint>>>& userTallyMessageCommits,
  533. std::vector<std::vector<std::vector<Twistpoint>>>& userTallySeedCommits,
  534. const Twistpoint& nextGenerator)
  535. {
  536. if (!initialize_fresh_generator(generatorProof, nextGenerator))
  537. {
  538. std::cerr << "New fresh generator could not be verified." << std::endl;
  539. return;
  540. }
  541. Scalar inverseSeed = currentSeed.curveMultInverse();
  542. std::vector<std::vector<Twistpoint>> currPermutationCommits;
  543. std::vector<std::vector<Twistpoint>> currFreshPseudonymCommits;
  544. std::vector<std::vector<Twistpoint>> currFreshPseudonymSeedCommits;
  545. std::vector<std::vector<CurveBipoint>> currServerTallyCommits;
  546. std::vector<std::vector<std::vector<TwistBipoint>>> currPartwayVoteMatrixCommits;
  547. std::vector<std::vector<std::vector<TwistBipoint>>> currFinalVoteMatrixCommits;
  548. std::vector<std::vector<Twistpoint>> currUserTallyMaskCommits;
  549. std::vector<std::vector<Twistpoint>> currUserTallyMessageCommits;
  550. std::vector<std::vector<Twistpoint>> currUserTallySeedCommits;
  551. pi.push_back(epoch_calculations(currPermutationCommits, currFreshPseudonymCommits, currFreshPseudonymSeedCommits, currServerTallyCommits, currPartwayVoteMatrixCommits, currFinalVoteMatrixCommits, currUserTallyMaskCommits, currUserTallyMessageCommits, currUserTallySeedCommits, inverseSeed, nextGenerator, true));
  552. permutationCommits.push_back(currPermutationCommits);
  553. freshPseudonymCommits.push_back(currFreshPseudonymCommits);
  554. freshPseudonymSeedCommits.push_back(currFreshPseudonymSeedCommits);
  555. serverTallyCommits.push_back(currServerTallyCommits);
  556. partwayVoteMatrixCommits.push_back(currPartwayVoteMatrixCommits);
  557. finalVoteMatrixCommits.push_back(currFinalVoteMatrixCommits);
  558. userTallyMaskCommits.push_back(currUserTallyMaskCommits);
  559. userTallyMessageCommits.push_back(currUserTallyMessageCommits);
  560. userTallySeedCommits.push_back(currUserTallySeedCommits);
  561. currentSeed = nextSeed;
  562. }
  563. /*
  564. * EPOCH HELPERS
  565. */
  566. void generate_permutation_commitment_r(
  567. const void *a,
  568. void *b,
  569. const void *c,
  570. void *d)
  571. {
  572. const PrsonaServer *server = (const PrsonaServer *) a;
  573. std::vector<std::vector<Twistpoint>> *permutationCommits = (std::vector<std::vector<Twistpoint>> *) b;
  574. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  575. std::vector<std::vector<Scalar>> *permutationSeeds = (std::vector<std::vector<Scalar>> *) d;
  576. *permutationCommits = server->generate_commitment_matrix(*permutations, *permutationSeeds);
  577. }
  578. void generate_pseudonym_commitment_r(
  579. const void *a,
  580. void *b,
  581. const void *c,
  582. const void *d,
  583. void *e,
  584. void *f)
  585. {
  586. const PrsonaServer *server = (const PrsonaServer *) a;
  587. std::vector<std::vector<Twistpoint>> *freshPseudonymCommits = (std::vector<std::vector<Twistpoint>> *) b;
  588. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  589. const Scalar *power = (const Scalar *) d;
  590. std::vector<std::vector<Scalar>> *freshPseudonymSeeds = (std::vector<std::vector<Scalar>> *) e;
  591. std::vector<std::vector<Twistpoint>> *freshPseudonymSeedCommits = (std::vector<std::vector<Twistpoint>> *) f;
  592. freshPseudonymSeedCommits->clear();
  593. *freshPseudonymCommits = server->generate_pseudonym_matrix(*permutations, *power, *freshPseudonymSeeds, *freshPseudonymSeedCommits);
  594. }
  595. void generate_server_tally_commitment_r(
  596. const void *a,
  597. void *b,
  598. const void *c,
  599. void *d)
  600. {
  601. const PrsonaServer *server = (const PrsonaServer *) a;
  602. std::vector<std::vector<CurveBipoint>> *serverTallyCommits = (std::vector<std::vector<CurveBipoint>> *) b;
  603. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  604. std::vector<std::vector<Scalar>> *serverTallySeeds = (std::vector<std::vector<Scalar>> *) d;
  605. *serverTallyCommits = server->generate_server_tally_matrix(*permutations, *serverTallySeeds);
  606. }
  607. void generate_matrix_commitment_r(
  608. const void *a,
  609. void *b,
  610. void *c,
  611. void *d,
  612. const void *e,
  613. const void *f,
  614. void *g,
  615. void *h)
  616. {
  617. const PrsonaServer *server = (const PrsonaServer *) a;
  618. std::vector<std::vector<std::vector<TwistBipoint>>> *partwayVoteMatrixCommits = (std::vector<std::vector<std::vector<TwistBipoint>>> *) b;
  619. std::vector<std::vector<TwistBipoint>> *partialVoteMatrix = (std::vector<std::vector<TwistBipoint>> *) c;
  620. std::vector<std::vector<std::vector<TwistBipoint>>> *finalVoteMatrixCommits = (std::vector<std::vector<std::vector<TwistBipoint>>> *) d;
  621. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) e;
  622. const std::vector<std::vector<TwistBipoint>> *voteMatrix = (const std::vector<std::vector<TwistBipoint>> *) f;
  623. std::vector<std::vector<std::vector<Scalar>>> *partwayVoteMatrixSeeds = (std::vector<std::vector<std::vector<Scalar>>> *) g;
  624. std::vector<std::vector<std::vector<Scalar>>> *finalVoteMatrixSeeds = (std::vector<std::vector<std::vector<Scalar>>> *) h;
  625. *partwayVoteMatrixCommits = server->generate_vote_tensor(*permutations, *voteMatrix, *partwayVoteMatrixSeeds, false);
  626. *partialVoteMatrix = server->calculate_next_vote_matrix(*partwayVoteMatrixCommits);
  627. *finalVoteMatrixCommits = server->generate_vote_tensor(*permutations, *partialVoteMatrix, *finalVoteMatrixSeeds, true);
  628. }
  629. void generate_user_tally_commitment_r(
  630. const void *a,
  631. const void *b,
  632. const void *c,
  633. const void *d,
  634. const void *e,
  635. void *f,
  636. void *g,
  637. void *h,
  638. void *i,
  639. void *j,
  640. void *k)
  641. {
  642. const PrsonaServer *server = (const PrsonaServer *) a;
  643. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) b;
  644. const Scalar *power = (const Scalar *) c;
  645. const Twistpoint *nextGenerator = (const Twistpoint *) d;
  646. const std::vector<Twistpoint> *currentPseudonyms = (const std::vector<Twistpoint> *) e;
  647. std::vector<Twistpoint> *userTallyMasks = (std::vector<Twistpoint> *) f;
  648. std::vector<std::vector<Twistpoint>> *userTallyMaskCommits = (std::vector<std::vector<Twistpoint>> *) g;
  649. std::vector<Twistpoint> *userTallyMessages = (std::vector<Twistpoint> *) h;
  650. std::vector<std::vector<Twistpoint>> *userTallyMessageCommits = (std::vector<std::vector<Twistpoint>> *) i;
  651. std::vector<std::vector<Scalar>> *userTallySeeds = (std::vector<std::vector<Scalar>> *) j;
  652. std::vector<std::vector<Twistpoint>> *userTallySeedCommits = (std::vector<std::vector<Twistpoint>> *) k;
  653. userTallyMaskCommits->clear();
  654. userTallyMessageCommits->clear();
  655. userTallySeedCommits->clear();
  656. server->generate_user_tally_matrix(*permutations, *power, *nextGenerator, *currentPseudonyms, *userTallyMasks, *userTallyMaskCommits, *userTallyMessages, *userTallyMessageCommits, *userTallySeeds, *userTallySeedCommits);
  657. }
  658. void generate_permutation_proof_r(
  659. const void *a,
  660. void *b,
  661. const void *c,
  662. const void *d,
  663. const void *e)
  664. {
  665. const PrsonaServer *server = (const PrsonaServer *) a;
  666. std::vector<Proof> *dst = (std::vector<Proof> *) b;
  667. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  668. const std::vector<std::vector<Scalar>> *permutationSeeds = (const std::vector<std::vector<Scalar>> *) d;
  669. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) e;
  670. *dst = server->generate_valid_permutation_proof(*permutations, *permutationSeeds, *permutationCommits);
  671. }
  672. void generate_pseudonym_proof_r(
  673. const void *a,
  674. void *b,
  675. const void *c,
  676. const void *d,
  677. const void *e,
  678. const void *f,
  679. const void *g,
  680. const void *h,
  681. const void *i,
  682. const void *j)
  683. {
  684. const PrsonaServer *server = (const PrsonaServer *) a;
  685. std::vector<Proof> *dst = (std::vector<Proof> *) b;
  686. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  687. const Scalar *power = (const Scalar *) d;
  688. const std::vector<std::vector<Scalar>> *permutationSeeds = (const std::vector<std::vector<Scalar>> *) e;
  689. const std::vector<std::vector<Scalar>> *freshPseudonymSeeds = (const std::vector<std::vector<Scalar>> *) f;
  690. const std::vector<Twistpoint> *currentPseudonyms = (const std::vector<Twistpoint> *) g;
  691. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) h;
  692. const std::vector<std::vector<Twistpoint>> *freshPseudonymCommits = (const std::vector<std::vector<Twistpoint>> *) i;
  693. const std::vector<std::vector<Twistpoint>> *freshPseudonymSeedCommits = (const std::vector<std::vector<Twistpoint>> *) j;
  694. *dst = server->generate_proof_of_reordering_plus_power(*permutations, *power, *permutationSeeds, *freshPseudonymSeeds, *currentPseudonyms, *permutationCommits, *freshPseudonymCommits, *freshPseudonymSeedCommits);
  695. }
  696. void generate_server_tally_proof_r(
  697. const void *a,
  698. void *b,
  699. const void *c,
  700. const void *d,
  701. const void *e,
  702. const void *f,
  703. const void *g,
  704. const void *h,
  705. const void *i,
  706. const void *j)
  707. {
  708. const PrsonaServer *server = (const PrsonaServer *) a;
  709. std::vector<Proof> *dst = (std::vector<Proof> *) b;
  710. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  711. const std::vector<std::vector<Scalar>> *permutationSeeds = (const std::vector<std::vector<Scalar>> *) d;
  712. const std::vector<std::vector<Scalar>> *serverTallySeeds = (const std::vector<std::vector<Scalar>> *) e;
  713. const std::vector<CurveBipoint> *previousVoteTallies = (const std::vector<CurveBipoint> *) f;
  714. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) g;
  715. const std::vector<std::vector<CurveBipoint>> *serverTallyCommits = (const std::vector<std::vector<CurveBipoint>> *) h;
  716. const CurveBipoint *curveG = (const CurveBipoint *) i;
  717. const CurveBipoint *curveH = (const CurveBipoint *) j;
  718. *dst = server->generate_proof_of_reordering<CurveBipoint>(*permutations, *permutationSeeds, *serverTallySeeds, *previousVoteTallies, *permutationCommits, *serverTallyCommits, *curveG, *curveH);
  719. }
  720. void generate_first_half_matrix_proof_r(
  721. const void *a,
  722. void *b,
  723. const void *c,
  724. const void *d,
  725. const void *e,
  726. const void *f,
  727. const void *g,
  728. const void *h)
  729. {
  730. const PrsonaServer *server = (const PrsonaServer *) a;
  731. std::vector<std::vector<Proof>> *dst = (std::vector<std::vector<Proof>> *) b;
  732. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  733. const std::vector<std::vector<Scalar>> *permutationSeeds = (const std::vector<std::vector<Scalar>> *) d;
  734. const std::vector<std::vector<std::vector<Scalar>>> *partwayVoteMatrixSeeds = (const std::vector<std::vector<std::vector<Scalar>>> *) e;
  735. const std::vector<std::vector<TwistBipoint>> *voteMatrix = (const std::vector<std::vector<TwistBipoint>> *) f;
  736. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) g;
  737. const std::vector<std::vector<std::vector<TwistBipoint>>> *partwayVoteMatrixCommits = (const std::vector<std::vector<std::vector<TwistBipoint>>> *) h;
  738. server->generate_vote_tensor_proofs(*dst, *permutations, *permutationSeeds, *partwayVoteMatrixSeeds, *voteMatrix, *permutationCommits, *partwayVoteMatrixCommits, false);
  739. }
  740. void generate_second_half_matrix_proof_r(
  741. const void *a,
  742. void *b,
  743. const void *c,
  744. const void *d,
  745. const void *e,
  746. const void *f,
  747. const void *g,
  748. const void *h)
  749. {
  750. const PrsonaServer *server = (const PrsonaServer *) a;
  751. std::vector<std::vector<Proof>> *dst = (std::vector<std::vector<Proof>> *) b;
  752. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  753. const std::vector<std::vector<Scalar>> *permutationSeeds = (const std::vector<std::vector<Scalar>> *) d;
  754. const std::vector<std::vector<std::vector<Scalar>>> *finalVoteMatrixSeeds = (const std::vector<std::vector<std::vector<Scalar>>> *) e;
  755. const std::vector<std::vector<TwistBipoint>> *partialVoteMatrix = (const std::vector<std::vector<TwistBipoint>> *) f;
  756. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) g;
  757. const std::vector<std::vector<std::vector<TwistBipoint>>> *finalVoteMatrixCommits = (const std::vector<std::vector<std::vector<TwistBipoint>>> *) h;
  758. server->generate_vote_tensor_proofs(*dst, *permutations, *permutationSeeds, *finalVoteMatrixSeeds, *partialVoteMatrix, *permutationCommits, *finalVoteMatrixCommits, true);
  759. }
  760. void generate_user_tally_proof_r(
  761. const void *a,
  762. void *b,
  763. const void *c,
  764. const void *d,
  765. const void *e,
  766. const void *f,
  767. const void *g,
  768. const void *h,
  769. const void *i,
  770. const void *j,
  771. const void *k,
  772. const void *l,
  773. const void *m,
  774. const void *n)
  775. {
  776. const PrsonaServer *server = (const PrsonaServer *) a;
  777. std::vector<Proof> *dst = (std::vector<Proof> *) b;
  778. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  779. const Scalar *power = (const Scalar *) d;
  780. const Twistpoint *nextGenerator = (const Twistpoint *) e;
  781. const std::vector<std::vector<Scalar>> *permutationSeeds = (const std::vector<std::vector<Scalar>> *) f;
  782. const std::vector<std::vector<Scalar>> *userTallySeeds = (const std::vector<std::vector<Scalar>> *) g;
  783. const std::vector<Twistpoint> *currentPseudonyms = (const std::vector<Twistpoint> *) h;
  784. const std::vector<Twistpoint> *userTallyMasks = (const std::vector<Twistpoint> *) i;
  785. const std::vector<Twistpoint> *userTallyMessages = (const std::vector<Twistpoint> *) j;
  786. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) k;
  787. const std::vector<std::vector<Twistpoint>> *userTallyMaskCommits = (const std::vector<std::vector<Twistpoint>> *) l;
  788. const std::vector<std::vector<Twistpoint>> *userTallyMessageCommits = (const std::vector<std::vector<Twistpoint>> *) m;
  789. const std::vector<std::vector<Twistpoint>> *userTallySeedCommits = (const std::vector<std::vector<Twistpoint>> *) n;
  790. *dst = server->generate_user_tally_proofs(*permutations, *power, *nextGenerator, *permutationSeeds, *userTallySeeds, *currentPseudonyms, *userTallyMasks, *userTallyMessages, *permutationCommits, *userTallyMaskCommits, *userTallyMessageCommits, *userTallySeedCommits);
  791. }
  792. std::vector<std::vector<Proof>> PrsonaServer::epoch_calculations(
  793. std::vector<std::vector<Twistpoint>>& permutationCommits,
  794. std::vector<std::vector<Twistpoint>>& freshPseudonymCommits,
  795. std::vector<std::vector<Twistpoint>>& freshPseudonymSeedCommits,
  796. std::vector<std::vector<CurveBipoint>>& serverTallyCommits,
  797. std::vector<std::vector<std::vector<TwistBipoint>>>& partwayVoteMatrixCommits,
  798. std::vector<std::vector<std::vector<TwistBipoint>>>& finalVoteMatrixCommits,
  799. std::vector<std::vector<Twistpoint>>& userTallyMaskCommits,
  800. std::vector<std::vector<Twistpoint>>& userTallyMessageCommits,
  801. std::vector<std::vector<Twistpoint>>& userTallySeedCommits,
  802. const Scalar& power,
  803. const Twistpoint& nextGenerator,
  804. bool doUserTallies)
  805. {
  806. std::vector<std::vector<Scalar>> permutations = generate_permutation_matrix(power);
  807. // for (size_t i = 0; i < permutations.size(); i++)
  808. // {
  809. // std::cout << (i == 0 ? "[[" : " [");
  810. // for (size_t j = 0; j < permutations[i].size(); j++)
  811. // std::cout << std::hex << permutations[i][j] << std::dec << (j == permutations[i].size() - 1 ? "]" : " ");
  812. // std::cout << (i == permutations.size() - 1 ? "]" : "") << std::endl;
  813. // }
  814. std::vector<std::vector<Proof>> retval, firstHalfTensorProof, secondHalfTensorProof;
  815. CurveBipoint curveG = bgnSystem.get_public_key().get_bipoint_curvegen();
  816. CurveBipoint curveH = bgnSystem.get_public_key().get_bipoint_curve_subgroup_gen();
  817. std::vector<Proof> *individualProofs = new std::vector<Proof>[doUserTallies ? 4 : 3];
  818. std::vector<std::vector<Scalar>> permutationSeeds;
  819. std::thread permutationCommitThread(generate_permutation_commitment_r, this, &permutationCommits, &permutations, &permutationSeeds);
  820. std::vector<std::vector<Scalar>> freshPseudonymSeeds;
  821. std::thread pseudonymCommitThread(generate_pseudonym_commitment_r, this, &freshPseudonymCommits, &permutations, &power, &freshPseudonymSeeds, &freshPseudonymSeedCommits);
  822. std::vector<std::vector<Scalar>> serverTallySeeds;
  823. std::thread serverTallyCommitThread(generate_server_tally_commitment_r, this, &serverTallyCommits, &permutations, &serverTallySeeds);
  824. std::vector<std::vector<std::vector<Scalar>>> partwayVoteMatrixSeeds;
  825. std::vector<std::vector<std::vector<Scalar>>> finalVoteMatrixSeeds;
  826. std::vector<std::vector<TwistBipoint>> partialVoteMatrix;
  827. std::thread voteMatrixCommitThread(generate_matrix_commitment_r, this, &partwayVoteMatrixCommits, &partialVoteMatrix, &finalVoteMatrixCommits, &permutations, &voteMatrix, &partwayVoteMatrixSeeds, &finalVoteMatrixSeeds);
  828. std::vector<Twistpoint> userTallyMasks;
  829. std::vector<Twistpoint> userTallyMessages;
  830. std::vector<std::vector<Scalar>> userTallySeeds;
  831. std::thread *userTallyCommitThread = NULL;
  832. if (doUserTallies)
  833. userTallyCommitThread = new std::thread(generate_user_tally_commitment_r, this, &permutations, &power, &nextGenerator, &currentPseudonyms, &userTallyMasks, &userTallyMaskCommits, &userTallyMessages, &userTallyMessageCommits, &userTallySeeds, &userTallySeedCommits);
  834. permutationCommitThread.join();
  835. std::thread permutationProofThread(generate_permutation_proof_r, this, individualProofs, &permutations, &permutationSeeds, &permutationCommits);
  836. pseudonymCommitThread.join();
  837. std::thread pseudonymProofThread(generate_pseudonym_proof_r, this, individualProofs + 1, &permutations, &power, &permutationSeeds, &freshPseudonymSeeds, &currentPseudonyms, &permutationCommits, &freshPseudonymCommits, &freshPseudonymSeedCommits);
  838. serverTallyCommitThread.join();
  839. std::thread serverTallyProofThread(generate_server_tally_proof_r, this, individualProofs + 2, &permutations, &permutationSeeds, &serverTallySeeds, &previousVoteTallies, &permutationCommits, &serverTallyCommits, &curveG, &curveH);
  840. voteMatrixCommitThread.join();
  841. std::thread firstHalfProofThread(generate_first_half_matrix_proof_r, this, &firstHalfTensorProof, &permutations, &permutationSeeds, &partwayVoteMatrixSeeds, &voteMatrix, &permutationCommits, &partwayVoteMatrixCommits);
  842. std::thread secondHalfProofThread(generate_second_half_matrix_proof_r, this, &secondHalfTensorProof, &permutations, &permutationSeeds, &finalVoteMatrixSeeds, &partialVoteMatrix, &permutationCommits, &finalVoteMatrixCommits);
  843. if (userTallyCommitThread)
  844. {
  845. userTallyCommitThread->join();
  846. delete userTallyCommitThread;
  847. userTallyCommitThread = NULL;
  848. }
  849. std::thread *userTallyProofThread = NULL;
  850. if (doUserTallies)
  851. userTallyProofThread = new std::thread(generate_user_tally_proof_r, this, individualProofs + 3, &permutations, &power, &nextGenerator, &permutationSeeds, &userTallySeeds, &currentPseudonyms, &userTallyMasks, &userTallyMessages, &permutationCommits, &userTallyMaskCommits, &userTallyMessageCommits, &userTallySeedCommits);
  852. permutationProofThread.join();
  853. pseudonymProofThread.join();
  854. serverTallyProofThread.join();
  855. firstHalfProofThread.join();
  856. secondHalfProofThread.join();
  857. if (userTallyProofThread)
  858. {
  859. userTallyProofThread->join();
  860. delete userTallyProofThread;
  861. userTallyProofThread = NULL;
  862. }
  863. retval.push_back(individualProofs[0]);
  864. retval.push_back(individualProofs[1]);
  865. retval.push_back(individualProofs[2]);
  866. for (size_t i = 0; i < firstHalfTensorProof.size(); i++)
  867. retval.push_back(firstHalfTensorProof[i]);
  868. for (size_t i = 0; i < secondHalfTensorProof.size(); i++)
  869. retval.push_back(secondHalfTensorProof[i]);
  870. if (doUserTallies)
  871. retval.push_back(individualProofs[3]);
  872. delete [] individualProofs;
  873. // Replace internal values
  874. update_data(freshPseudonymCommits, serverTallyCommits, finalVoteMatrixCommits, userTallyMaskCommits, userTallyMessageCommits);
  875. return retval;
  876. }
  877. void verify_permutation_r(
  878. const void *a,
  879. void *b,
  880. const void *c,
  881. const void *d)
  882. {
  883. const PrsonaServer *server = (const PrsonaServer *) a;
  884. bool *dst = (bool *) b;
  885. const std::vector<Proof> *pi = (const std::vector<Proof> *) c;
  886. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) d;
  887. *dst = server->verify_valid_permutation_proof(*pi, *permutationCommits);
  888. }
  889. void verify_pseudonym_r(
  890. const void *a,
  891. void *b,
  892. const void *c,
  893. const void *d,
  894. const void *e,
  895. const void *f,
  896. const void *g)
  897. {
  898. const PrsonaServer *server = (const PrsonaServer *) a;
  899. bool *dst = (bool *) b;
  900. const std::vector<Proof> *pi = (const std::vector<Proof> *) c;
  901. const std::vector<Twistpoint> *currentPseudonyms = (const std::vector<Twistpoint> *) d;
  902. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) e;
  903. const std::vector<std::vector<Twistpoint>> *freshPseudonymCommits = (const std::vector<std::vector<Twistpoint>> *) f;
  904. const std::vector<std::vector<Twistpoint>> *freshPseudonymSeedCommits = (const std::vector<std::vector<Twistpoint>> *) g;
  905. *dst = server->verify_proof_of_reordering_plus_power(*pi, *currentPseudonyms, *permutationCommits, *freshPseudonymCommits, *freshPseudonymSeedCommits);
  906. }
  907. void verify_server_tally_r(
  908. const void *a,
  909. void *b,
  910. const void *c,
  911. const void *d,
  912. const void *e,
  913. const void *f,
  914. const void *g,
  915. const void *h)
  916. {
  917. const PrsonaServer *server = (const PrsonaServer *) a;
  918. bool *dst = (bool *) b;
  919. const std::vector<Proof> *pi = (const std::vector<Proof> *) c;
  920. const std::vector<CurveBipoint> *previousVoteTallies = (const std::vector<CurveBipoint> *) d;
  921. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) e;
  922. const std::vector<std::vector<CurveBipoint>> *serverTallyCommits = (const std::vector<std::vector<CurveBipoint>> *) f;
  923. const CurveBipoint *curveG = (const CurveBipoint *) g;
  924. const CurveBipoint *curveH = (const CurveBipoint *) h;
  925. *dst = server->verify_proof_of_reordering<CurveBipoint>(*pi, *previousVoteTallies, *permutationCommits, *serverTallyCommits, *curveG, *curveH);
  926. }
  927. void verify_first_half_matrix_r(
  928. const void *a,
  929. void *b,
  930. const void *c,
  931. const void *d,
  932. const void *e,
  933. const void *f,
  934. const void *g)
  935. {
  936. const PrsonaServer *server = (const PrsonaServer *) a;
  937. bool *dst = (bool *) b;
  938. const std::vector<std::vector<Proof>> *pi = (const std::vector<std::vector<Proof>> *) c;
  939. const size_t *currOffset = (const size_t *) d;
  940. const std::vector<std::vector<TwistBipoint>> *voteMatrix = (const std::vector<std::vector<TwistBipoint>> *) e;
  941. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) f;
  942. const std::vector<std::vector<std::vector<TwistBipoint>>> *partwayVoteMatrixCommits = (const std::vector<std::vector<std::vector<TwistBipoint>>> *) g;
  943. *dst = server->verify_vote_tensor_proofs(*pi, *currOffset, *voteMatrix, *permutationCommits, *partwayVoteMatrixCommits, false);
  944. }
  945. void verify_second_half_matrix_r(
  946. const void *a,
  947. void *b,
  948. const void *c,
  949. const void *d,
  950. const void *e,
  951. const void *f,
  952. const void *g)
  953. {
  954. const PrsonaServer *server = (const PrsonaServer *) a;
  955. bool *dst = (bool *) b;
  956. const std::vector<std::vector<Proof>> *pi = (const std::vector<std::vector<Proof>> *) c;
  957. const size_t *currOffset = (const size_t *) d;
  958. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) e;
  959. const std::vector<std::vector<std::vector<TwistBipoint>>> *partwayVoteMatrixCommits = (const std::vector<std::vector<std::vector<TwistBipoint>>> *) f;
  960. const std::vector<std::vector<std::vector<TwistBipoint>>> *finalVoteMatrixCommits = (const std::vector<std::vector<std::vector<TwistBipoint>>> *) g;
  961. std::vector<std::vector<TwistBipoint>> partialVoteMatrix = server->calculate_next_vote_matrix(*partwayVoteMatrixCommits);
  962. *dst = server->verify_vote_tensor_proofs(*pi, *currOffset, partialVoteMatrix, *permutationCommits, *finalVoteMatrixCommits, true);
  963. }
  964. void verify_user_tally_r(
  965. const void *a,
  966. void *b,
  967. const void *c,
  968. const void *d,
  969. const void *e,
  970. const void *f,
  971. const void *g,
  972. const void *h,
  973. const void *i,
  974. const void *j)
  975. {
  976. const PrsonaServer *server = (const PrsonaServer *) a;
  977. bool *dst = (bool *) b;
  978. const std::vector<Proof> *pi = (const std::vector<Proof> *) c;
  979. const Twistpoint *nextGenerator = (const Twistpoint *) d;
  980. const std::vector<Twistpoint> *currentPseudonyms = (const std::vector<Twistpoint> *) e;
  981. const std::vector<EGCiphertext> *currentUserEncryptedTallies = (const std::vector<EGCiphertext> *) f;
  982. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) g;
  983. const std::vector<std::vector<Twistpoint>> *userTallyMaskCommits = (const std::vector<std::vector<Twistpoint>> *) h;
  984. const std::vector<std::vector<Twistpoint>> *userTallyMessageCommits = (const std::vector<std::vector<Twistpoint>> *) i;
  985. const std::vector<std::vector<Twistpoint>> *userTallySeedCommits = (const std::vector<std::vector<Twistpoint>> *) j;
  986. std::vector<Twistpoint> userTallyMasks;
  987. std::vector<Twistpoint> userTallyMessages;
  988. for (size_t i = 0; i < currentUserEncryptedTallies->size(); i++)
  989. {
  990. userTallyMasks.push_back((*currentUserEncryptedTallies)[i].mask);
  991. userTallyMessages.push_back((*currentUserEncryptedTallies)[i].encryptedMessage);
  992. }
  993. *dst = server->verify_user_tally_proofs(*pi, *nextGenerator, *currentPseudonyms, userTallyMasks, userTallyMessages, *permutationCommits, *userTallyMaskCommits, *userTallyMessageCommits, *userTallySeedCommits);
  994. }
  995. bool PrsonaServer::accept_epoch_updates(
  996. const std::vector<std::vector<Proof>>& pi,
  997. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  998. const std::vector<std::vector<Twistpoint>>& freshPseudonymCommits,
  999. const std::vector<std::vector<Twistpoint>>& freshPseudonymSeedCommits,
  1000. const std::vector<std::vector<CurveBipoint>>& serverTallyCommits,
  1001. const std::vector<std::vector<std::vector<TwistBipoint>>>& partwayVoteMatrixCommits,
  1002. const std::vector<std::vector<std::vector<TwistBipoint>>>& finalVoteMatrixCommits,
  1003. const std::vector<std::vector<Twistpoint>>& userTallyMaskCommits,
  1004. const std::vector<std::vector<Twistpoint>>& userTallyMessageCommits,
  1005. const std::vector<std::vector<Twistpoint>>& userTallySeedCommits,
  1006. const Twistpoint& nextGenerator,
  1007. bool doUserTallies)
  1008. {
  1009. bool permutationVerification = false;
  1010. bool pseudonymVerification = false;
  1011. bool serverTallyVerification = false;
  1012. bool firstHalfVerification = false;
  1013. bool secondHalfVerification = false;
  1014. bool userTallyVerification = true;
  1015. if ((userTallyMaskCommits.empty() && doUserTallies) || (!userTallyMaskCommits.empty() && !doUserTallies))
  1016. {
  1017. std::cerr << "user tallies are not in expected state." << std::endl;
  1018. return false;
  1019. }
  1020. if (pi.empty())
  1021. return false;
  1022. size_t currOffset = 0;
  1023. std::thread permutationThread(verify_permutation_r, this, &permutationVerification, &(pi[currOffset]), &permutationCommits);
  1024. currOffset++;
  1025. std::thread pseudonymThread(verify_pseudonym_r, this, &pseudonymVerification, &(pi[currOffset]), &currentPseudonyms, &permutationCommits, &freshPseudonymCommits, &freshPseudonymSeedCommits);
  1026. currOffset++;
  1027. CurveBipoint curveG = bgnSystem.get_public_key().get_bipoint_curvegen();
  1028. CurveBipoint curveH = bgnSystem.get_public_key().get_bipoint_curve_subgroup_gen();
  1029. std::thread serverTallyThread(verify_server_tally_r, this, &serverTallyVerification, &(pi[currOffset]), &previousVoteTallies, &permutationCommits, &serverTallyCommits, &curveG, &curveH);
  1030. currOffset++;
  1031. size_t firstHalfOffset = currOffset;
  1032. std::thread firstHalfThread(verify_first_half_matrix_r, this, &firstHalfVerification, &pi, &firstHalfOffset, &voteMatrix, &permutationCommits, &partwayVoteMatrixCommits);
  1033. currOffset += voteMatrix.size();
  1034. size_t secondHalfOffset = currOffset;
  1035. std::thread secondHalfThread(verify_second_half_matrix_r, this, &secondHalfVerification, &pi, &secondHalfOffset, &permutationCommits, &partwayVoteMatrixCommits, &finalVoteMatrixCommits);
  1036. currOffset += voteMatrix.size();
  1037. std::thread *userTallyThread = NULL;
  1038. if (doUserTallies)
  1039. userTallyThread = new std::thread(verify_user_tally_r, this, &userTallyVerification, &(pi[currOffset]), &nextGenerator, &currentPseudonyms, &currentUserEncryptedTallies, &permutationCommits, &userTallyMaskCommits, &userTallyMessageCommits, &userTallySeedCommits);
  1040. currOffset++;
  1041. permutationThread.join();
  1042. pseudonymThread.join();
  1043. serverTallyThread.join();
  1044. firstHalfThread.join();
  1045. secondHalfThread.join();
  1046. if (userTallyThread)
  1047. {
  1048. userTallyThread->join();
  1049. delete userTallyThread;
  1050. userTallyThread = NULL;
  1051. }
  1052. if (!permutationVerification)
  1053. {
  1054. std::cerr << "Could not verify valid permutation matrix." << std::endl;
  1055. return false;
  1056. }
  1057. if (!pseudonymVerification)
  1058. {
  1059. std::cerr << "Could not verify valid pseudonym vector." << std::endl;
  1060. return false;
  1061. }
  1062. if (!serverTallyVerification)
  1063. {
  1064. std::cerr << "Could not verify valid server tally vector." << std::endl;
  1065. return false;
  1066. }
  1067. if (!userTallyVerification)
  1068. {
  1069. std::cerr << "Could not verify user tallies." << std::endl;
  1070. return false;
  1071. }
  1072. if (!firstHalfVerification)
  1073. {
  1074. std::cerr << "Could not verify first half vote matrix." << std::endl;
  1075. return false;
  1076. }
  1077. if (!secondHalfVerification)
  1078. {
  1079. std::cerr << "Could not verify second half vote matrix." << std::endl;
  1080. return false;
  1081. }
  1082. bool finalVerification = update_data(freshPseudonymCommits, serverTallyCommits, finalVoteMatrixCommits, userTallyMaskCommits, userTallyMessageCommits);
  1083. return finalVerification;
  1084. }
  1085. std::vector<std::vector<Scalar>> PrsonaServer::generate_permutation_matrix(
  1086. const Scalar& reorderSeed) const
  1087. {
  1088. std::vector<std::vector<Scalar>> retval;
  1089. for (size_t i = 0; i < currentPseudonyms.size(); i++)
  1090. {
  1091. std::vector<Scalar> currRow;
  1092. for (size_t j = 0; j < currentPseudonyms.size(); j++)
  1093. currRow.push_back(Scalar(0));
  1094. retval.push_back(currRow);
  1095. }
  1096. std::vector<Twistpoint> nextPseudonyms;
  1097. for (size_t i = 0; i < currentPseudonyms.size(); i++)
  1098. nextPseudonyms.push_back(currentPseudonyms[i] * reorderSeed);
  1099. std::vector<size_t> order = sort_data(nextPseudonyms);
  1100. for (size_t i = 0; i < order.size(); i++)
  1101. retval[order[i]][i] = Scalar(1);
  1102. return retval;
  1103. }
  1104. std::vector<std::vector<Twistpoint>> PrsonaServer::generate_commitment_matrix(
  1105. const std::vector<std::vector<Scalar>>& permutations,
  1106. std::vector<std::vector<Scalar>>& seeds) const
  1107. {
  1108. std::vector<std::vector<Twistpoint>> retval;
  1109. Twistpoint g = EL_GAMAL_GENERATOR;
  1110. Twistpoint h = elGamalBlindGenerator;
  1111. seeds.clear();
  1112. for (size_t i = 0; i < permutations.size(); i++)
  1113. {
  1114. std::vector<Scalar> currSeeds;
  1115. for (size_t j = 0; j < permutations[i].size(); j++)
  1116. currSeeds.push_back(Scalar(0));
  1117. seeds.push_back(currSeeds);
  1118. }
  1119. for (size_t i = 0; i < permutations.size(); i++)
  1120. {
  1121. std::vector<Twistpoint> currRow;
  1122. size_t last = permutations[i].size() - 1;
  1123. for (size_t j = 0; j < permutations[i].size(); j++)
  1124. {
  1125. Twistpoint element;
  1126. if (j != last)
  1127. {
  1128. seeds[i][j].set_random();
  1129. seeds[i][last] = seeds[i][last] - seeds[i][j];
  1130. }
  1131. element = g * permutations[i][j] + h * seeds[i][j];
  1132. currRow.push_back(element);
  1133. }
  1134. retval.push_back(currRow);
  1135. }
  1136. return retval;
  1137. }
  1138. std::vector<std::vector<Twistpoint>> PrsonaServer::generate_pseudonym_matrix(
  1139. const std::vector<std::vector<Scalar>>& permutations,
  1140. const Scalar& power,
  1141. std::vector<std::vector<Scalar>>& seeds,
  1142. std::vector<std::vector<Twistpoint>>& seedCommits) const
  1143. {
  1144. return generate_reordered_plus_power_matrix<Twistpoint>(permutations, power, currentPseudonyms, seeds, seedCommits, elGamalBlindGenerator);
  1145. }
  1146. std::vector<std::vector<CurveBipoint>> PrsonaServer::generate_server_tally_matrix(
  1147. const std::vector<std::vector<Scalar>>& permutations,
  1148. std::vector<std::vector<Scalar>>& seeds) const
  1149. {
  1150. return generate_reordered_matrix<CurveBipoint>(permutations, previousVoteTallies, seeds, bgnSystem.get_public_key().get_bipoint_curve_subgroup_gen(), false);
  1151. }
  1152. std::vector<std::vector<std::vector<TwistBipoint>>> PrsonaServer::generate_vote_tensor(
  1153. const std::vector<std::vector<Scalar>>& permutations,
  1154. const std::vector<std::vector<TwistBipoint>>& currVoteMatrix,
  1155. std::vector<std::vector<std::vector<Scalar>>>& seeds,
  1156. bool inverted) const
  1157. {
  1158. std::vector<std::vector<std::vector<TwistBipoint>>> retval;
  1159. for (size_t i = 0; i < currVoteMatrix.size(); i++)
  1160. {
  1161. std::vector<std::vector<Scalar>> currSeeds;
  1162. std::vector<TwistBipoint> inputRow;
  1163. if (inverted)
  1164. {
  1165. for (size_t j = 0; j < currVoteMatrix.size(); j++)
  1166. inputRow.push_back(currVoteMatrix[j][i]);
  1167. }
  1168. else
  1169. {
  1170. inputRow = currVoteMatrix[i];
  1171. }
  1172. retval.push_back(generate_reordered_matrix<TwistBipoint>(permutations, inputRow, currSeeds, bgnSystem.get_public_key().get_bipoint_twist_subgroup_gen(), false));
  1173. seeds.push_back(currSeeds);
  1174. }
  1175. return retval;
  1176. }
  1177. std::vector<std::vector<TwistBipoint>> PrsonaServer::calculate_next_vote_matrix(
  1178. const std::vector<std::vector<std::vector<TwistBipoint>>>& voteTensor) const
  1179. {
  1180. std::vector<std::vector<TwistBipoint>> retval;
  1181. for (size_t i = 0; i < voteTensor.size(); i++)
  1182. {
  1183. std::vector<TwistBipoint> currRow;
  1184. for (size_t j = 0; j < voteTensor[i].size(); j++)
  1185. {
  1186. TwistBipoint sum = voteTensor[i][j][0];
  1187. for (size_t k = 1; k < voteTensor[i][j].size(); k++)
  1188. sum = sum + voteTensor[i][j][k];
  1189. currRow.push_back(sum);
  1190. }
  1191. retval.push_back(currRow);
  1192. }
  1193. return retval;
  1194. }
  1195. void generate_tensor_r(
  1196. const void *a,
  1197. void *b,
  1198. const void *c,
  1199. const void *d,
  1200. const void *e,
  1201. const void *f,
  1202. const void *g,
  1203. const void *h,
  1204. const void *i,
  1205. const void *j)
  1206. {
  1207. const PrsonaServer *server = (const PrsonaServer *) a;
  1208. std::vector<Proof> *dst = (std::vector<Proof> *) b;
  1209. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  1210. const std::vector<std::vector<Scalar>> *permutationSeeds = (const std::vector<std::vector<Scalar>> *) d;
  1211. const std::vector<std::vector<Scalar>> *matrixSeeds = (const std::vector<std::vector<Scalar>> *) e;
  1212. const std::vector<TwistBipoint> *inputRow = (const std::vector<TwistBipoint> *) f;
  1213. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) g;
  1214. const std::vector<std::vector<TwistBipoint>> *matrixCommits = (const std::vector<std::vector<TwistBipoint>> *) h;
  1215. const TwistBipoint *twistG = (const TwistBipoint *) i;
  1216. const TwistBipoint *twistH = (const TwistBipoint *) j;
  1217. *dst = server->generate_proof_of_reordering<TwistBipoint>(*permutations, *permutationSeeds, *matrixSeeds, *inputRow, *permutationCommits, *matrixCommits, *twistG, *twistH);
  1218. }
  1219. void PrsonaServer::generate_vote_tensor_proofs(
  1220. std::vector<std::vector<Proof>>& pi,
  1221. const std::vector<std::vector<Scalar>>& permutations,
  1222. const std::vector<std::vector<Scalar>>& permutationSeeds,
  1223. const std::vector<std::vector<std::vector<Scalar>>>& matrixSeeds,
  1224. const std::vector<std::vector<TwistBipoint>>& currMatrix,
  1225. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1226. const std::vector<std::vector<std::vector<TwistBipoint>>>& matrixCommits,
  1227. bool inverted) const
  1228. {
  1229. TwistBipoint twistG = bgnSystem.get_public_key().get_bipoint_twistgen();
  1230. TwistBipoint twistH = bgnSystem.get_public_key().get_bipoint_twist_subgroup_gen();
  1231. std::vector<Proof> *intermediaryProofs = new std::vector<Proof>[currMatrix.size()];
  1232. std::vector<TwistBipoint> *inputRows = new std::vector<TwistBipoint>[currMatrix.size()];
  1233. std::vector<std::thread> individualTensorProofThreads;
  1234. for (size_t i = 0; i < currMatrix.size(); i++)
  1235. {
  1236. std::vector<TwistBipoint> currRow;
  1237. if (inverted)
  1238. {
  1239. for (size_t j = 0; j < currMatrix.size(); j++)
  1240. currRow.push_back(currMatrix[j][i]);
  1241. }
  1242. else
  1243. {
  1244. currRow = currMatrix[i];
  1245. }
  1246. inputRows[i] = currRow;
  1247. individualTensorProofThreads.push_back(std::thread(generate_tensor_r, this, intermediaryProofs + i, &permutations, &permutationSeeds, &(matrixSeeds[i]), inputRows + i, &permutationCommits, &(matrixCommits[i]), &twistG, &twistH));
  1248. }
  1249. for (size_t i = 0; i < individualTensorProofThreads.size(); i++)
  1250. {
  1251. individualTensorProofThreads[i].join();
  1252. pi.push_back(intermediaryProofs[i]);
  1253. }
  1254. delete [] inputRows;
  1255. delete [] intermediaryProofs;
  1256. }
  1257. void verify_tensor_r(
  1258. const void *a,
  1259. void *b,
  1260. const void *c,
  1261. const void *d,
  1262. const void *e,
  1263. const void *f,
  1264. const void *g,
  1265. const void *h)
  1266. {
  1267. const PrsonaServer *server = (const PrsonaServer *) a;
  1268. bool *dst = (bool *) b;
  1269. const std::vector<Proof> *pi = (const std::vector<Proof> *) c;
  1270. const std::vector<TwistBipoint> *inputRow = (const std::vector<TwistBipoint> *) d;
  1271. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) e;
  1272. const std::vector<std::vector<TwistBipoint>> *matrixCommits = (const std::vector<std::vector<TwistBipoint>> *) f;
  1273. const TwistBipoint *twistG = (const TwistBipoint *) g;
  1274. const TwistBipoint *twistH = (const TwistBipoint *) h;
  1275. *dst = server->verify_proof_of_reordering<TwistBipoint>(*pi, *inputRow, *permutationCommits, *matrixCommits, *twistG, *twistH);
  1276. }
  1277. bool PrsonaServer::verify_vote_tensor_proofs(
  1278. const std::vector<std::vector<Proof>>& pi,
  1279. size_t start_offset,
  1280. const std::vector<std::vector<TwistBipoint>>& currMatrix,
  1281. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1282. const std::vector<std::vector<std::vector<TwistBipoint>>>& matrixCommits,
  1283. bool inverted) const
  1284. {
  1285. TwistBipoint twistG = bgnSystem.get_public_key().get_bipoint_twistgen();
  1286. TwistBipoint twistH = bgnSystem.get_public_key().get_bipoint_twist_subgroup_gen();
  1287. bool *intermediaryValues = new bool[currMatrix.size()];
  1288. std::vector<TwistBipoint> *inputRows = new std::vector<TwistBipoint>[currMatrix.size()];
  1289. std::vector<std::thread> individualTensorProofThreads;
  1290. for (size_t i = 0; i < currMatrix.size(); i++)
  1291. {
  1292. std::vector<TwistBipoint> currRow;
  1293. if (inverted)
  1294. {
  1295. for (size_t j = 0; j < currMatrix.size(); j++)
  1296. currRow.push_back(currMatrix[j][i]);
  1297. }
  1298. else
  1299. {
  1300. currRow = currMatrix[i];
  1301. }
  1302. inputRows[i] = currRow;
  1303. size_t whichProof = i + start_offset;
  1304. individualTensorProofThreads.push_back(std::thread(verify_tensor_r, this, intermediaryValues + i, &(pi[whichProof]), inputRows + i, &permutationCommits, &(matrixCommits[i]), &twistG, &twistH));
  1305. }
  1306. bool retval = true;
  1307. for (size_t i = 0; i < individualTensorProofThreads.size(); i++)
  1308. {
  1309. individualTensorProofThreads[i].join();
  1310. retval = retval && intermediaryValues[i];
  1311. }
  1312. delete [] inputRows;
  1313. delete [] intermediaryValues;
  1314. return retval;
  1315. }
  1316. void PrsonaServer::generate_user_tally_matrix(
  1317. const std::vector<std::vector<Scalar>>& permutations,
  1318. const Scalar& power,
  1319. const Twistpoint& nextGenerator,
  1320. const std::vector<Twistpoint>& currPseudonyms,
  1321. std::vector<Twistpoint>& masks,
  1322. std::vector<std::vector<Twistpoint>>& maskCommits,
  1323. std::vector<Twistpoint>& messages,
  1324. std::vector<std::vector<Twistpoint>>& messageCommits,
  1325. std::vector<std::vector<Scalar>>& userTallySeeds,
  1326. std::vector<std::vector<Twistpoint>>& userTallySeedCommits) const
  1327. {
  1328. masks.clear();
  1329. messages.clear();
  1330. for (size_t i = 0; i < currentUserEncryptedTallies.size(); i++)
  1331. {
  1332. masks.push_back(currentUserEncryptedTallies[i].mask);
  1333. messages.push_back(currentUserEncryptedTallies[i].encryptedMessage);
  1334. }
  1335. maskCommits.clear();
  1336. messageCommits.clear();
  1337. userTallySeeds.clear();
  1338. userTallySeedCommits.clear();
  1339. for (size_t i = 0; i < permutations.size(); i++)
  1340. {
  1341. std::vector<Scalar> currSeeds;
  1342. std::vector<Twistpoint> currRow;
  1343. for (size_t j = 0; j < permutations[i].size(); j++)
  1344. {
  1345. currSeeds.push_back(Scalar(0));
  1346. currRow.push_back(Twistpoint());
  1347. }
  1348. userTallySeeds.push_back(currSeeds);
  1349. maskCommits.push_back(currRow);
  1350. messageCommits.push_back(currRow);
  1351. userTallySeedCommits.push_back(currRow);
  1352. }
  1353. for (size_t i = 0; i < permutations.size(); i++)
  1354. {
  1355. size_t last = permutations[i].size() - 1;
  1356. for (size_t j = 0; j < permutations[i].size(); j++)
  1357. {
  1358. if (j != last)
  1359. {
  1360. userTallySeeds[i][j].set_random();
  1361. userTallySeeds[i][last] = userTallySeeds[i][last] - userTallySeeds[i][j];
  1362. }
  1363. maskCommits[i][j] = masks[j] * permutations[j][i] * power +
  1364. currPseudonyms[j] * power * permutations[j][i] * userTallySeeds[i][j] +
  1365. elGamalBlindGenerator * userTallySeeds[i][j];
  1366. messageCommits[i][j] = messages[j] * permutations[j][i] +
  1367. nextGenerator * permutations[j][i] * userTallySeeds[i][j] +
  1368. elGamalBlindGenerator * userTallySeeds[i][j];
  1369. userTallySeedCommits[i][j] = EL_GAMAL_GENERATOR * userTallySeeds[i][j];
  1370. }
  1371. }
  1372. }
  1373. template <typename T>
  1374. std::vector<std::vector<T>> PrsonaServer::generate_reordered_plus_power_matrix(
  1375. const std::vector<std::vector<Scalar>>& permutations,
  1376. const Scalar& power,
  1377. const std::vector<T>& oldValues,
  1378. std::vector<std::vector<Scalar>>& seeds,
  1379. std::vector<std::vector<Twistpoint>>& seedCommits,
  1380. const T& h) const
  1381. {
  1382. std::vector<std::vector<Scalar>> permutation_plus_power;
  1383. seedCommits.clear();
  1384. for (size_t i = 0; i < permutations.size(); i++)
  1385. {
  1386. std::vector<Scalar> currPermutations;
  1387. std::vector<Twistpoint> currSeedCommits;
  1388. for (size_t j = 0; j < permutations[i].size(); j++)
  1389. {
  1390. currPermutations.push_back(permutations[i][j] * power);
  1391. currSeedCommits.push_back(Twistpoint());
  1392. }
  1393. permutation_plus_power.push_back(currPermutations);
  1394. seedCommits.push_back(currSeedCommits);
  1395. }
  1396. std::vector<std::vector<T>> retval = generate_reordered_matrix<T>(permutation_plus_power, oldValues, seeds, h, true);
  1397. for (size_t i = 0; i < permutations.size(); i++)
  1398. for (size_t j = 0; j < permutations[i].size(); j++)
  1399. seedCommits[i][j] = EL_GAMAL_GENERATOR * seeds[i][j];
  1400. return retval;
  1401. }
  1402. template <typename T>
  1403. std::vector<std::vector<T>> PrsonaServer::generate_reordered_matrix(
  1404. const std::vector<std::vector<Scalar>>& permutations,
  1405. const std::vector<T>& oldValues,
  1406. std::vector<std::vector<Scalar>>& seeds,
  1407. const T& h,
  1408. bool cancelOut) const
  1409. {
  1410. std::vector<std::vector<T>> retval;
  1411. seeds.clear();
  1412. for (size_t i = 0; i < permutations.size(); i++)
  1413. {
  1414. std::vector<Scalar> currSeeds;
  1415. std::vector<T> currRow;
  1416. for (size_t j = 0; j < permutations[i].size(); j++)\
  1417. {
  1418. currSeeds.push_back(Scalar(0));
  1419. currRow.push_back(T());
  1420. }
  1421. seeds.push_back(currSeeds);
  1422. retval.push_back(currRow);
  1423. }
  1424. for (size_t i = 0; i < permutations.size(); i++)
  1425. {
  1426. size_t last = permutations[i].size() - 1;
  1427. for (size_t j = 0; j < permutations[i].size(); j++)
  1428. {
  1429. if (!cancelOut)
  1430. {
  1431. seeds[i][j].set_random();
  1432. }
  1433. else if (j != last)
  1434. {
  1435. seeds[i][j].set_random();
  1436. seeds[i][last] = seeds[i][last] - seeds[i][j];
  1437. }
  1438. retval[i][j] = oldValues[j] * permutations[j][i] + h * seeds[i][j];
  1439. }
  1440. }
  1441. return retval;
  1442. }
  1443. std::vector<size_t> PrsonaServer::sort_data(
  1444. const std::vector<Twistpoint>& inputs) const
  1445. {
  1446. std::vector<size_t> retval;
  1447. // SortingType's index member allows us to replicate the "sort" across
  1448. std::vector<SortingType> sortTracker;
  1449. for (size_t i = 0; i < inputs.size(); i++)
  1450. {
  1451. SortingType curr;
  1452. curr.pseudonym = inputs[i];
  1453. curr.index = i;
  1454. sortTracker.push_back(curr);
  1455. }
  1456. std::sort(sortTracker.begin(), sortTracker.end());
  1457. for (size_t i = 0; i < inputs.size(); i++)
  1458. retval.push_back(sortTracker[i].index);
  1459. return retval;
  1460. }
  1461. bool PrsonaServer::update_data(
  1462. const std::vector<std::vector<Twistpoint>>& freshPseudonymCommits,
  1463. const std::vector<std::vector<CurveBipoint>>& serverTallyCommits,
  1464. const std::vector<std::vector<std::vector<TwistBipoint>>>& voteMatrixCommits,
  1465. const std::vector<std::vector<Twistpoint>>& userTallyMaskCommits,
  1466. const std::vector<std::vector<Twistpoint>>& userTallyMessageCommits)
  1467. {
  1468. std::vector<Twistpoint> newPseudonyms;
  1469. std::vector<CurveBipoint> newVoteTallies;
  1470. std::vector<EGCiphertext> newUserTallies;
  1471. for (size_t i = 0; i < freshPseudonymCommits.size(); i++)
  1472. {
  1473. Twistpoint pseudonymSum = freshPseudonymCommits[i][0];
  1474. CurveBipoint voteTallySum = serverTallyCommits[i][0];
  1475. Twistpoint userTallyMask, userTallyMessage;
  1476. if (!userTallyMaskCommits.empty())
  1477. {
  1478. userTallyMask = userTallyMaskCommits[i][0];
  1479. userTallyMessage = userTallyMessageCommits[i][0];
  1480. }
  1481. for (size_t j = 1; j < freshPseudonymCommits[i].size(); j++)
  1482. {
  1483. pseudonymSum = pseudonymSum + freshPseudonymCommits[i][j];
  1484. voteTallySum = voteTallySum + serverTallyCommits[i][j];
  1485. if (!userTallyMaskCommits.empty())
  1486. {
  1487. userTallyMask = userTallyMask + userTallyMaskCommits[i][j];
  1488. userTallyMessage = userTallyMessage + userTallyMessageCommits[i][j];
  1489. }
  1490. }
  1491. newPseudonyms.push_back(pseudonymSum);
  1492. newVoteTallies.push_back(voteTallySum);
  1493. if (!userTallyMaskCommits.empty())
  1494. newUserTallies.push_back(EGCiphertext(userTallyMask, userTallyMessage));
  1495. }
  1496. if (!pseudonyms_sorted(newPseudonyms))
  1497. {
  1498. std::cerr << "Pseudonyms not sorted correctly." << std::endl;
  1499. return false;
  1500. }
  1501. currentPseudonyms = newPseudonyms;
  1502. previousVoteTallies = newVoteTallies;
  1503. voteMatrix = calculate_next_vote_matrix(voteMatrixCommits);
  1504. currentUserEncryptedTallies = newUserTallies;
  1505. return true;
  1506. }
  1507. bool PrsonaServer::pseudonyms_sorted(
  1508. const std::vector<Twistpoint> newPseudonyms) const
  1509. {
  1510. bool retval = true;
  1511. for (size_t i = 0; i < newPseudonyms.size() - 1; i++)
  1512. retval = retval && (newPseudonyms[i] < newPseudonyms[i + 1]);
  1513. return retval;
  1514. }
  1515. /*
  1516. * DATA MAINTENANCE
  1517. */
  1518. void PrsonaServer::export_new_user_update(
  1519. std::vector<CurveBipoint>& otherPreviousVoteTallies,
  1520. std::vector<Twistpoint>& otherCurrentPseudonyms,
  1521. std::vector<EGCiphertext>& otherCurrentUserEncryptedTallies,
  1522. std::vector<std::vector<TwistBipoint>>& otherVoteMatrix) const
  1523. {
  1524. otherPreviousVoteTallies = previousVoteTallies;
  1525. otherCurrentPseudonyms = currentPseudonyms;
  1526. otherCurrentUserEncryptedTallies = currentUserEncryptedTallies;
  1527. otherVoteMatrix = voteMatrix;
  1528. }
  1529. bool PrsonaServer::import_new_user_update(
  1530. const std::vector<Proof>& pi,
  1531. const std::vector<CurveBipoint>& otherPreviousVoteTallies,
  1532. const std::vector<Twistpoint>& otherCurrentPseudonyms,
  1533. const std::vector<EGCiphertext>& otherCurrentUserEncryptedTallies,
  1534. const std::vector<std::vector<TwistBipoint>>& otherVoteMatrix)
  1535. {
  1536. size_t newIndex = 0;
  1537. if (!currentPseudonyms.empty())
  1538. while (otherCurrentPseudonyms[newIndex] == currentPseudonyms[newIndex])
  1539. newIndex++;
  1540. Twistpoint shortTermPublicKey = otherCurrentPseudonyms[newIndex];
  1541. bool flag = verify_proof_of_added_user(pi, currentFreshGenerator, shortTermPublicKey, bgnSystem.get_public_key().get_bipoint_twistgen(), bgnSystem.get_public_key().get_bipoint_twist_subgroup_gen(), bgnSystem.get_public_key().get_bipoint_curvegen(), bgnSystem.get_public_key().get_bipoint_curve_subgroup_gen(), newIndex, otherCurrentUserEncryptedTallies[newIndex], otherPreviousVoteTallies[newIndex], otherVoteMatrix);
  1542. if (!flag)
  1543. {
  1544. std::cerr << "Other server added new user invalidly, aborting." << std::endl;
  1545. return false;
  1546. }
  1547. for (size_t i = 0; i < otherCurrentPseudonyms.size(); i++)
  1548. {
  1549. if (i == newIndex)
  1550. continue;
  1551. size_t otherI = (i > newIndex ? i - 1 : i);
  1552. flag = flag && otherCurrentPseudonyms[i] == currentPseudonyms[otherI];
  1553. flag = flag && otherCurrentUserEncryptedTallies[i] == currentUserEncryptedTallies[otherI];
  1554. flag = flag && otherPreviousVoteTallies[i] == previousVoteTallies[otherI];
  1555. for (size_t j = 0; j < otherCurrentPseudonyms.size(); j++)
  1556. {
  1557. if (j == newIndex)
  1558. continue;
  1559. size_t otherJ = (j > newIndex ? j - 1 : j);
  1560. flag = flag && otherVoteMatrix[i][j] == voteMatrix[otherI][otherJ];
  1561. }
  1562. }
  1563. if (!flag)
  1564. {
  1565. std::cerr << "Other server illicitly changed other value during new user add." << std::endl;
  1566. return false;
  1567. }
  1568. previousVoteTallies = otherPreviousVoteTallies;
  1569. currentPseudonyms = otherCurrentPseudonyms;
  1570. currentUserEncryptedTallies = otherCurrentUserEncryptedTallies;
  1571. voteMatrix = otherVoteMatrix;
  1572. return true;
  1573. }
  1574. /*
  1575. * DATA SAFEKEEPING
  1576. */
  1577. /* This is what powers the "shuffle"; really, as pseudonyms get updated,
  1578. * the pseudonyms are no longer in the order prescribed by operator<().
  1579. * So, we put them (and everything else) back into that order,
  1580. * effectively shuffling them (and making lookups easier later on). */
  1581. std::vector<size_t> PrsonaServer::order_data()
  1582. {
  1583. std::vector<size_t> retval = sort_data(currentPseudonyms);
  1584. // Order all other data in the same way, for consistency
  1585. std::vector<Twistpoint> newPseudonyms;
  1586. std::vector<CurveBipoint> newVoteTallies;
  1587. std::vector<EGCiphertext> newUserEncryptedTallies;
  1588. std::vector<std::vector<TwistBipoint>> newVoteMatrix;
  1589. for (size_t i = 0; i < retval.size(); i++)
  1590. {
  1591. newPseudonyms.push_back(currentPseudonyms[retval[i]]);
  1592. newVoteTallies.push_back(previousVoteTallies[retval[i]]);
  1593. if (!currentUserEncryptedTallies.empty())
  1594. newUserEncryptedTallies.push_back(currentUserEncryptedTallies[retval[i]]);
  1595. std::vector<TwistBipoint> currNewRow;
  1596. for (size_t j = 0; j < currentPseudonyms.size(); j++)
  1597. currNewRow.push_back(voteMatrix[retval[i]][retval[j]]);
  1598. newVoteMatrix.push_back(currNewRow);
  1599. }
  1600. previousVoteTallies = newVoteTallies;
  1601. currentPseudonyms = newPseudonyms;
  1602. currentUserEncryptedTallies = newUserEncryptedTallies;
  1603. voteMatrix = newVoteMatrix;
  1604. return retval;
  1605. }
  1606. /*
  1607. * BINARY SEARCH
  1608. */
  1609. // Completely normal binary search
  1610. size_t PrsonaServer::binary_search(
  1611. const Twistpoint& index) const
  1612. {
  1613. return PrsonaBase::binary_search(currentPseudonyms, index);
  1614. }
  1615. /*
  1616. * VALID VOTE PROOFS
  1617. */
  1618. bool PrsonaServer::verify_vote_proof(
  1619. const std::vector<Proof>& pi,
  1620. const std::vector<TwistBipoint>& oldVotes,
  1621. const std::vector<TwistBipoint>& newVotes,
  1622. const Twistpoint& shortTermPublicKey) const
  1623. {
  1624. const BGNPublicKey& pubKey = bgnSystem.get_public_key();
  1625. return PrsonaBase::verify_vote_proof(pubKey.get_bipoint_twistgen(), pubKey.get_bipoint_twist_subgroup_gen(), pi, oldVotes, newVotes, currentFreshGenerator, shortTermPublicKey);
  1626. }
  1627. void PrsonaServer::print_scores(
  1628. const std::vector<CurveBipoint>& scores)
  1629. {
  1630. std::unique_lock<std::mutex> lck(*decryptMtx);
  1631. std::cout << "[";
  1632. for (size_t i = 0; i < scores.size(); i++)
  1633. std::cout << bgnSystem.decrypt(scores[i]) << (i == scores.size() - 1 ? "]" : " ");
  1634. std::cout << std::endl;
  1635. }