server.cpp 44 KB

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