server.cpp 45 KB

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