server.cpp 70 KB

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