server.cpp 46 KB

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