serverEntity.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. #include <iostream>
  2. #include "serverEntity.hpp"
  3. /********************
  4. * PUBLIC FUNCTIONS *
  5. ********************/
  6. /*
  7. * CONSTRUCTORS
  8. */
  9. PrsonaServerEntity::PrsonaServerEntity(size_t numServers)
  10. {
  11. if (numServers < 1)
  12. {
  13. std::cerr << "You have to have at least 1 server. "
  14. << "I'm making it anyways." << std::endl;
  15. }
  16. // Make the first server, which makes the BGN parameters
  17. PrsonaServer firstServer(numServers);
  18. servers.push_back(firstServer);
  19. // Make the rest of the servers, which take the BGN parameters
  20. const BGN& sharedBGN = firstServer.get_bgn_details();
  21. for (size_t i = 1; i < numServers; i++)
  22. servers.push_back(PrsonaServer(numServers, sharedBGN));
  23. // After all servers have made their seeds,
  24. // make sure they have the initial fresh generator
  25. std::vector<Proof> pi;
  26. Curvepoint firstGenerator = get_fresh_generator(pi);
  27. for (size_t i = 0; i < numServers; i++)
  28. servers[i].initialize_fresh_generator(pi, firstGenerator);
  29. pi.clear();
  30. // It's important that no server knows the DLOG between g and h for ElGamal,
  31. // so have each server collaborate to make h.
  32. Curvepoint blindGenerator = PrsonaServer::EL_GAMAL_GENERATOR;
  33. for (size_t i = 0; i < numServers; i++)
  34. {
  35. blindGenerator =
  36. servers[i].add_rand_seed_to_generator(pi, blindGenerator);
  37. }
  38. for (size_t i = 0; i < numServers; i++)
  39. servers[i].set_EG_blind_generator(pi, blindGenerator);
  40. }
  41. /*
  42. * BASIC PUBLIC SYSTEM INFO GETTERS
  43. */
  44. BGNPublicKey PrsonaServerEntity::get_bgn_public_key() const
  45. {
  46. return get_bgn_public_key(0);
  47. }
  48. BGNPublicKey PrsonaServerEntity::get_bgn_public_key(size_t which) const
  49. {
  50. return servers[which].get_bgn_public_key();
  51. }
  52. Curvepoint PrsonaServerEntity::get_blinding_generator() const
  53. {
  54. return get_blinding_generator(0);
  55. }
  56. Curvepoint PrsonaServerEntity::get_blinding_generator(size_t which) const
  57. {
  58. std::vector<Proof> pi;
  59. Curvepoint retval = get_blinding_generator(pi, which);
  60. if (!servers[which].verify_generator_proof(
  61. pi, retval, servers[which].get_num_servers()))
  62. {
  63. std::cerr << "Error making the generator, aborting." << std::endl;
  64. return Curvepoint();
  65. }
  66. return retval;
  67. }
  68. Curvepoint PrsonaServerEntity::get_blinding_generator(
  69. std::vector<Proof>& pi) const
  70. {
  71. return get_blinding_generator(pi, 0);
  72. }
  73. Curvepoint PrsonaServerEntity::get_blinding_generator(
  74. std::vector<Proof>& pi, size_t which) const
  75. {
  76. return servers[which].get_blinding_generator(pi);
  77. }
  78. Curvepoint PrsonaServerEntity::get_fresh_generator() const
  79. {
  80. return get_fresh_generator(0);
  81. }
  82. Curvepoint PrsonaServerEntity::get_fresh_generator(size_t which) const
  83. {
  84. std::vector<Proof> pi;
  85. Curvepoint retval = get_fresh_generator(pi, which);
  86. if (!servers[which].verify_generator_proof(
  87. pi, retval, servers[which].get_num_servers()))
  88. {
  89. std::cerr << "Error making the generator, aborting." << std::endl;
  90. return Curvepoint();
  91. }
  92. return retval;
  93. }
  94. Curvepoint PrsonaServerEntity::get_fresh_generator(
  95. std::vector<Proof>& pi) const
  96. {
  97. return get_fresh_generator(pi, 0);
  98. }
  99. Curvepoint PrsonaServerEntity::get_fresh_generator(
  100. std::vector<Proof>& pi, size_t which) const
  101. {
  102. Curvepoint retval = PrsonaServer::EL_GAMAL_GENERATOR;
  103. pi.clear();
  104. for (size_t j = 0; j < servers[which].get_num_servers(); j++)
  105. {
  106. size_t index = (which + j) % servers[which].get_num_servers();
  107. retval = servers[index].add_curr_seed_to_generator(pi, retval);
  108. }
  109. return retval;
  110. }
  111. size_t PrsonaServerEntity::get_num_clients() const
  112. {
  113. return get_num_clients(0);
  114. }
  115. size_t PrsonaServerEntity::get_num_clients(size_t which) const
  116. {
  117. return servers[which].get_num_clients();
  118. }
  119. size_t PrsonaServerEntity::get_num_servers() const
  120. {
  121. return get_num_servers(0);
  122. }
  123. size_t PrsonaServerEntity::get_num_servers(size_t which) const
  124. {
  125. return servers[which].get_num_servers();
  126. }
  127. /*
  128. * ENCRYPTED DATA GETTERS
  129. */
  130. std::vector<CurveBipoint> PrsonaServerEntity::get_current_votes_by(
  131. Proof& pi, const Curvepoint& shortTermPublicKey) const
  132. {
  133. return get_current_votes_by(pi, shortTermPublicKey, 0);
  134. }
  135. /* Call this in order to get the current encrypted votes cast by a given user
  136. * (who is identified by their short term public key).
  137. * In practice, this is intended for clients,
  138. * who need to know their current votes in order to rerandomize them. */
  139. std::vector<CurveBipoint> PrsonaServerEntity::get_current_votes_by(
  140. Proof& pi, const Curvepoint& shortTermPublicKey, size_t which) const
  141. {
  142. return servers[which].get_current_votes_by(pi, shortTermPublicKey);
  143. }
  144. std::vector<std::vector<CurveBipoint>> PrsonaServerEntity::get_all_current_votes(
  145. Proof& pi) const
  146. {
  147. return get_all_current_votes(pi, 0);
  148. }
  149. std::vector<std::vector<CurveBipoint>> PrsonaServerEntity::get_all_current_votes(
  150. Proof& pi, size_t which) const
  151. {
  152. return servers[which].get_all_current_votes(pi);
  153. }
  154. EGCiphertext PrsonaServerEntity::get_current_user_encrypted_tally(
  155. Proof& pi, const Curvepoint& shortTermPublicKey) const
  156. {
  157. return get_current_user_encrypted_tally(pi, shortTermPublicKey, 0);
  158. }
  159. EGCiphertext PrsonaServerEntity::get_current_user_encrypted_tally(
  160. Proof& pi, const Curvepoint& shortTermPublicKey, size_t which) const
  161. {
  162. return servers[which].get_current_user_encrypted_tally(
  163. pi, shortTermPublicKey);
  164. }
  165. TwistBipoint PrsonaServerEntity::get_current_server_encrypted_tally(
  166. Proof& pi, const Curvepoint& shortTermPublicKey) const
  167. {
  168. return get_current_server_encrypted_tally(pi, shortTermPublicKey, 0);
  169. }
  170. TwistBipoint PrsonaServerEntity::get_current_server_encrypted_tally(
  171. Proof& pi, const Curvepoint& shortTermPublicKey, size_t which) const
  172. {
  173. return servers[which].get_current_server_encrypted_tally(
  174. pi, shortTermPublicKey);
  175. }
  176. std::vector<Curvepoint> PrsonaServerEntity::get_current_pseudonyms(
  177. Proof& pi) const
  178. {
  179. return get_current_pseudonyms(pi, 0);
  180. }
  181. std::vector<Curvepoint> PrsonaServerEntity::get_current_pseudonyms(
  182. Proof& pi, size_t which) const
  183. {
  184. return servers[which].get_current_pseudonyms(pi);
  185. }
  186. /*
  187. * PROOF COMMITMENT GETTERS
  188. */
  189. void PrsonaServerEntity::get_other_vote_row_commitments(
  190. std::vector<Proof>& pi, const Curvepoint& request) const
  191. {
  192. get_other_vote_row_commitments(pi, request, 0);
  193. }
  194. void PrsonaServerEntity::get_other_vote_row_commitments(
  195. std::vector<Proof>& pi, const Curvepoint& request, size_t whichNot) const
  196. {
  197. for (size_t i = 0; i < servers.size(); i++)
  198. {
  199. if (i == whichNot)
  200. continue;
  201. pi.push_back(servers[i].get_vote_row_commitment(request));
  202. }
  203. }
  204. void PrsonaServerEntity::get_other_vote_matrix_commitments(
  205. std::vector<Proof>& pi) const
  206. {
  207. get_other_vote_matrix_commitments(pi, 0);
  208. }
  209. void PrsonaServerEntity::get_other_vote_matrix_commitments(
  210. std::vector<Proof>& pi, size_t whichNot) const
  211. {
  212. for (size_t i = 0; i < servers.size(); i++)
  213. {
  214. if (i == whichNot)
  215. continue;
  216. pi.push_back(servers[i].get_vote_matrix_commitment());
  217. }
  218. }
  219. void PrsonaServerEntity::get_other_user_tally_commitments(
  220. std::vector<Proof>& pi, const Curvepoint& request) const
  221. {
  222. get_other_user_tally_commitments(pi, request, 0);
  223. }
  224. void PrsonaServerEntity::get_other_user_tally_commitments(
  225. std::vector<Proof>& pi, const Curvepoint& request, size_t whichNot) const
  226. {
  227. for (size_t i = 0; i < servers.size(); i++)
  228. {
  229. if (i == whichNot)
  230. continue;
  231. pi.push_back(servers[i].get_user_tally_commitment(request));
  232. }
  233. }
  234. void PrsonaServerEntity::get_other_server_tally_commitments(
  235. std::vector<Proof>& pi, const Curvepoint& request) const
  236. {
  237. get_other_server_tally_commitments(pi, request, 0);
  238. }
  239. void PrsonaServerEntity::get_other_server_tally_commitments(
  240. std::vector<Proof>& pi, const Curvepoint& request, size_t whichNot) const
  241. {
  242. for (size_t i = 0; i < servers.size(); i++)
  243. {
  244. if (i == whichNot)
  245. continue;
  246. pi.push_back(servers[i].get_server_tally_commitment(request));
  247. }
  248. }
  249. void PrsonaServerEntity::get_other_pseudonyms_commitments(
  250. std::vector<Proof>& pi) const
  251. {
  252. get_other_pseudonyms_commitments(pi, 0);
  253. }
  254. void PrsonaServerEntity::get_other_pseudonyms_commitments(
  255. std::vector<Proof>& pi, size_t whichNot) const
  256. {
  257. for (size_t i = 0; i < servers.size(); i++)
  258. {
  259. if (i == whichNot)
  260. continue;
  261. pi.push_back(servers[i].get_pseudonyms_commitment());
  262. }
  263. }
  264. /*
  265. * CLIENT INTERACTIONS
  266. */
  267. void PrsonaServerEntity::add_new_client(PrsonaClient& newUser)
  268. {
  269. add_new_client(newUser, 0);
  270. }
  271. /* Add a new client (who is identified only by their short term public key)
  272. * One server does the main work, then other servers import their (proven)
  273. * exported data. */
  274. void PrsonaServerEntity::add_new_client(PrsonaClient& newUser, size_t which)
  275. {
  276. Proof proofOfValidSTPK, proofOfValidVotes;
  277. std::vector<Proof> proofOfValidGenerator, proofOfCorrectAddition;
  278. Curvepoint freshGenerator =
  279. get_fresh_generator(proofOfValidGenerator, which);
  280. // Users can't actually announce a short term public key
  281. // if they don't know the fresh generator.
  282. newUser.receive_fresh_generator(proofOfValidGenerator, freshGenerator);
  283. Curvepoint shortTermPublicKey = newUser.get_short_term_public_key(
  284. proofOfValidSTPK);
  285. // Do the actual work of adding the client to the first server
  286. servers[which].add_new_client(
  287. proofOfCorrectAddition, proofOfValidSTPK, shortTermPublicKey);
  288. // Then, export the data to the rest of the servers
  289. std::vector<TwistBipoint> previousVoteTallies;
  290. std::vector<Curvepoint> currentPseudonyms;
  291. std::vector<EGCiphertext> currentUserEncryptedTallies;
  292. std::vector<std::vector<CurveBipoint>> voteMatrix;
  293. previousVoteTallies = servers[which].previousVoteTallies;
  294. currentPseudonyms = servers[which].currentPseudonyms;
  295. currentUserEncryptedTallies = servers[which].currentUserEncryptedTallies;
  296. voteMatrix = servers[which].voteMatrix;
  297. for (size_t j = 1; j < servers[which].get_num_servers(); j++)
  298. {
  299. size_t index = (which + j) % servers[which].get_num_servers();
  300. servers[index].import_new_user_update(
  301. proofOfCorrectAddition,
  302. previousVoteTallies,
  303. currentPseudonyms,
  304. currentUserEncryptedTallies,
  305. voteMatrix);
  306. }
  307. // Finally, give the user the information it needs
  308. // about its current tally and votes
  309. transmit_new_user_data(proofOfCorrectAddition, newUser);
  310. }
  311. // Receive a new vote row from a user (identified by short term public key).
  312. bool PrsonaServerEntity::receive_vote(
  313. const std::vector<Proof>& pi,
  314. const std::vector<CurveBipoint>& newVotes,
  315. const Curvepoint& shortTermPublicKey)
  316. {
  317. return receive_vote(pi, newVotes, shortTermPublicKey, 0);
  318. }
  319. bool PrsonaServerEntity::receive_vote(
  320. const std::vector<Proof>& pi,
  321. const std::vector<CurveBipoint>& newVotes,
  322. const Curvepoint& shortTermPublicKey,
  323. size_t which)
  324. {
  325. bool retval = true;
  326. for (size_t i = 0; i < servers[which].get_num_servers(); i++)
  327. {
  328. size_t index = (i + which) % servers[which].get_num_servers();
  329. retval = retval &&
  330. servers[index].receive_vote(pi, newVotes, shortTermPublicKey);
  331. }
  332. return retval;
  333. }
  334. void PrsonaServerEntity::transmit_new_user_data(
  335. const std::vector<Proof>& pi, PrsonaClient& newUser) const
  336. {
  337. newUser.receive_new_user_data(pi);
  338. }
  339. void PrsonaServerEntity::transmit_updates(PrsonaClient& currUser) const
  340. {
  341. transmit_updates(currUser, 0);
  342. }
  343. // After tallying scores and new vote matrix,
  344. // give those to a user for the new epoch
  345. void PrsonaServerEntity::transmit_updates(
  346. PrsonaClient& currUser, size_t which) const
  347. {
  348. Proof proofOfValidSTPK, proofOfScore, proofOfCorrectVotes;
  349. std::vector<Proof> proofOfValidGenerator;
  350. Curvepoint freshGenerator =
  351. get_fresh_generator(proofOfValidGenerator, which);
  352. // Get users the next fresh generator so they can correctly
  353. // ask for their new scores and vote row
  354. currUser.receive_fresh_generator(proofOfValidGenerator, freshGenerator);
  355. currUser.receive_vote_tally();
  356. }
  357. /*
  358. * EPOCH
  359. */
  360. void PrsonaServerEntity::epoch()
  361. {
  362. epoch(0);
  363. }
  364. // Do the epoch process
  365. void PrsonaServerEntity::epoch(size_t which)
  366. {
  367. Curvepoint nextGenerator = PrsonaServer::EL_GAMAL_GENERATOR;
  368. std::vector<std::vector<std::vector<Proof>>> pi;
  369. std::vector<std::vector<std::vector<Curvepoint>>> permutationCommits;
  370. std::vector<std::vector<std::vector<Curvepoint>>> freshPseudonymCommits;
  371. std::vector<std::vector<std::vector<Curvepoint>>> freshPseudonymSeedCommits;
  372. std::vector<std::vector<std::vector<TwistBipoint>>> serverTallyCommits;
  373. std::vector<std::vector<std::vector<std::vector<CurveBipoint>>>> partwayVoteMatrixCommits;
  374. std::vector<std::vector<std::vector<std::vector<CurveBipoint>>>> finalVoteMatrixCommits;
  375. std::vector<std::vector<std::vector<Curvepoint>>> userTallyMaskCommits;
  376. std::vector<std::vector<std::vector<Curvepoint>>> userTallyMessageCommits;
  377. std::vector<std::vector<std::vector<Curvepoint>>> userTallySeedCommits;
  378. std::vector<std::vector<Proof>> generator_proof_holder(1);
  379. pi.push_back(generator_proof_holder);
  380. // go from A_0 to A_0.5
  381. for (size_t i = 0; i < servers[which].get_num_servers(); i++)
  382. {
  383. size_t realI = (which + i) % servers[which].get_num_servers();
  384. servers[realI].build_up_midway_pseudonyms(
  385. pi,
  386. permutationCommits,
  387. freshPseudonymCommits,
  388. freshPseudonymSeedCommits,
  389. serverTallyCommits,
  390. partwayVoteMatrixCommits,
  391. finalVoteMatrixCommits,
  392. nextGenerator);
  393. for (size_t j = 1; j < servers[which].get_num_servers(); j++)
  394. {
  395. std::vector<std::vector<Curvepoint>> currUserTallyMaskCommits;
  396. std::vector<std::vector<Curvepoint>> currUserTallyMessageCommits;
  397. std::vector<std::vector<Curvepoint>> currUserTallySeedCommits;
  398. size_t realJ = (realI + j) % servers[which].get_num_servers();
  399. servers[realJ].accept_epoch_updates(
  400. pi[i + 1],
  401. permutationCommits[i],
  402. freshPseudonymCommits[i],
  403. freshPseudonymSeedCommits[i],
  404. serverTallyCommits[i],
  405. partwayVoteMatrixCommits[i],
  406. finalVoteMatrixCommits[i],
  407. currUserTallyMaskCommits,
  408. currUserTallyMessageCommits,
  409. currUserTallySeedCommits,
  410. nextGenerator,
  411. false);
  412. }
  413. }
  414. std::vector<Proof> generator_proof = pi[0][0];
  415. pi.clear();
  416. permutationCommits.clear();
  417. freshPseudonymCommits.clear();
  418. freshPseudonymSeedCommits.clear();
  419. serverTallyCommits.clear();
  420. partwayVoteMatrixCommits.clear();
  421. finalVoteMatrixCommits.clear();
  422. /* Imagine that server 0 is encrypting these, then would do a ZKP that it
  423. * knows a secret mask and encrypted the correct value everyone else already
  424. * knows. Everyone else then adds a mask and proves they added a secret mask
  425. * to the committed values. */
  426. std::vector<EGCiphertext> currentUserEncryptedTallies;
  427. std::vector<TwistBipoint> currentServerEncryptedTallies;
  428. tally_scores(
  429. nextGenerator,
  430. currentUserEncryptedTallies,
  431. currentServerEncryptedTallies,
  432. which);
  433. distribute_tallied_scores(
  434. currentUserEncryptedTallies,
  435. currentServerEncryptedTallies);
  436. // go from A_0.5 to A_1
  437. for (size_t i = 0; i < servers[which].get_num_servers(); i++)
  438. {
  439. size_t realI = (which + i) % servers[which].get_num_servers();
  440. servers[realI].break_down_midway_pseudonyms(
  441. generator_proof,
  442. pi,
  443. permutationCommits,
  444. freshPseudonymCommits,
  445. freshPseudonymSeedCommits,
  446. serverTallyCommits,
  447. partwayVoteMatrixCommits,
  448. finalVoteMatrixCommits,
  449. userTallyMaskCommits,
  450. userTallyMessageCommits,
  451. userTallySeedCommits,
  452. nextGenerator);
  453. for (size_t j = 1; j < servers[which].get_num_servers(); j++)
  454. {
  455. size_t realJ = (realI + j) % servers[which].get_num_servers();
  456. servers[realJ].accept_epoch_updates(
  457. pi[i],
  458. permutationCommits[i],
  459. freshPseudonymCommits[i],
  460. freshPseudonymSeedCommits[i],
  461. serverTallyCommits[i],
  462. partwayVoteMatrixCommits[i],
  463. finalVoteMatrixCommits[i],
  464. userTallyMaskCommits[i],
  465. userTallyMessageCommits[i],
  466. userTallySeedCommits[i],
  467. nextGenerator,
  468. true);
  469. }
  470. }
  471. }
  472. void PrsonaServerEntity::print_scores() const
  473. {
  474. BGN bgnSystem = servers[0].bgnSystem;
  475. std::vector<TwistBipoint> scores = servers[0].previousVoteTallies;
  476. std::cout << "[";
  477. for (size_t i = 0; i < scores.size(); i++)
  478. {
  479. std::cout << bgnSystem.decrypt(scores[i])
  480. << (i == scores.size() - 1 ? "]" : " ");
  481. }
  482. std::cout << std::endl;
  483. }
  484. void PrsonaServerEntity::print_votes() const
  485. {
  486. BGN bgnSystem = servers[0].bgnSystem;
  487. std::vector<std::vector<CurveBipoint>> voteMatrix = servers[0].voteMatrix;
  488. for (size_t i = 0; i < voteMatrix.size(); i++)
  489. {
  490. std::cout << (i == 0 ? "[[" : " [");
  491. for (size_t j = 0; j < voteMatrix[i].size(); j++)
  492. {
  493. std::cout << bgnSystem.decrypt(voteMatrix[i][j])
  494. << (j == voteMatrix[i].size() - 1 ? "]" : " ");
  495. }
  496. std::cout << (i == voteMatrix.size() - 1 ? "]" : " ") << std::endl;
  497. }
  498. }
  499. void PrsonaServerEntity::print_current_votes_by(
  500. const Curvepoint& index) const
  501. {
  502. BGN bgnSystem = servers[0].bgnSystem;
  503. size_t realIndex =
  504. servers[0].binary_search(index);
  505. std::vector<CurveBipoint> scores = servers[0].voteMatrix[realIndex];
  506. std::cout << "[";
  507. for (size_t i = 0; i < scores.size(); i++)
  508. {
  509. std::cout << bgnSystem.decrypt(scores[i])
  510. << (i == scores.size() - 1 ? "]" : " ");
  511. }
  512. std::cout << std::endl;
  513. }
  514. /*********************
  515. * PRIVATE FUNCTIONS *
  516. *********************/
  517. /*
  518. * SCORE TALLYING
  519. */
  520. /* Calculate scores, then scale the values appropriately,
  521. * so they are in the correct range to be used as vote weights.
  522. *
  523. * We're treating it as if we are one server, so that server gets the updated
  524. * weights to be sent to all other servers for the next epoch. */
  525. void PrsonaServerEntity::tally_scores(
  526. const Curvepoint& nextGenerator,
  527. std::vector<EGCiphertext>& userTallyScores,
  528. std::vector<TwistBipoint>& serverTallyScores,
  529. size_t which)
  530. {
  531. std::vector<EGCiphertext> retval;
  532. std::vector<Scalar> decryptedTalliedScores = servers[which].tally_scores();
  533. mpz_class maxScorePossibleThisRound =
  534. servers[which].get_max_possible_score().toInt() *
  535. PrsonaBase::get_max_allowed_vote();
  536. mpz_class topOfScoreRange =
  537. decryptedTalliedScores.size() * PrsonaBase::get_max_allowed_vote();
  538. userTallyScores.clear();
  539. serverTallyScores.clear();
  540. for (size_t i = 0; i < decryptedTalliedScores.size(); i++)
  541. {
  542. decryptedTalliedScores[i] =
  543. Scalar(
  544. (decryptedTalliedScores[i].toInt() * topOfScoreRange) /
  545. maxScorePossibleThisRound
  546. );
  547. EGCiphertext currCiphertext;
  548. userTallyScores.push_back(currCiphertext);
  549. TwistBipoint currServerScore;
  550. serverTallyScores.push_back(currServerScore);
  551. Scalar currMask;
  552. currMask.set_random();
  553. // Give the server the new weights,
  554. // to get passed around to the other servers
  555. servers[which].bgnSystem.encrypt(
  556. serverTallyScores[i], decryptedTalliedScores[i]);
  557. userTallyScores[i].mask = servers[which].currentPseudonyms[i] * currMask;
  558. userTallyScores[i].encryptedMessage =
  559. (nextGenerator * currMask) +
  560. (servers[which].get_blinding_generator() * decryptedTalliedScores[i]);
  561. }
  562. }
  563. void PrsonaServerEntity::distribute_tallied_scores(
  564. const std::vector<EGCiphertext>& userScores,
  565. const std::vector<TwistBipoint>& serverScores)
  566. {
  567. for (size_t i = 0; i < servers[0].get_num_servers(); i++)
  568. {
  569. servers[i].currentUserEncryptedTallies = userScores;
  570. servers[i].previousVoteTallies = serverScores;
  571. }
  572. }
  573. /*
  574. * BINARY SEARCH
  575. */
  576. // Completely normal binary search
  577. size_t PrsonaServerEntity::binary_search(
  578. const Curvepoint& shortTermPublicKey, size_t which) const
  579. {
  580. return servers[which].binary_search(shortTermPublicKey);
  581. }