server.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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();
  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();
  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();
  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();
  99. return retval;
  100. }
  101. std::vector<Curvepoint> PrsonaServer::get_current_pseudonyms(Proof& pi) const
  102. {
  103. pi = generate_valid_pseudonyms_proof();
  104. return currentPseudonyms;
  105. }
  106. /*
  107. * CLIENT INTERACTIONS
  108. */
  109. /* Add a new client (who is identified only by their short term public key)
  110. * One server will do this, then ask all other servers to import their
  111. * (proven) exported data. */
  112. void PrsonaServer::add_new_client(
  113. std::vector<Proof>& proofOfValidAddition,
  114. const Proof& proofOfValidKey,
  115. const Curvepoint& shortTermPublicKey)
  116. {
  117. if (!verify_ownership_proof(
  118. proofOfValidKey, currentFreshGenerator, shortTermPublicKey))
  119. {
  120. std::cerr << "Could not verify proof of valid key." << std::endl;
  121. return;
  122. }
  123. currentPseudonyms.push_back(shortTermPublicKey);
  124. // The first epoch's score for a new user will be low,
  125. // but will typically converge on an average score quickly
  126. Scalar tallySeed;
  127. TwistBipoint encryptedDefaultTally =
  128. bgnSystem.get_public_key().twistEncrypt(tallySeed, DEFAULT_TALLY);
  129. previousVoteTallies.push_back(encryptedDefaultTally);
  130. Scalar seedForUserTally;
  131. seedForUserTally.set_random();
  132. EGCiphertext newUserEncryptedTally;
  133. newUserEncryptedTally.mask = shortTermPublicKey * seedForUserTally;
  134. newUserEncryptedTally.encryptedMessage =
  135. currentFreshGenerator * seedForUserTally +
  136. elGamalBlindGenerator * DEFAULT_TALLY;
  137. currentUserEncryptedTallies.push_back(newUserEncryptedTally);
  138. // Users are defaulted to casting a neutral vote for others.
  139. CurveBipoint encryptedDefaultVote, encryptedSelfVote;
  140. Scalar currDefaultSeed, currSelfSeed;
  141. encryptedDefaultVote =
  142. bgnSystem.get_public_key().curveEncrypt(currDefaultSeed, DEFAULT_VOTE);
  143. encryptedSelfVote =
  144. bgnSystem.get_public_key().curveEncrypt(currSelfSeed, Scalar(MAX_ALLOWED_VOTE));
  145. std::vector<CurveBipoint> newRow;
  146. std::vector<Scalar> userVoteSeeds;
  147. std::vector<Scalar> otherVoteSeeds;
  148. for (size_t i = 0; i < voteMatrix.size(); i++)
  149. {
  150. Scalar addedSeed;
  151. encryptedDefaultVote = bgnSystem.get_public_key().rerandomize(addedSeed, encryptedDefaultVote);
  152. currDefaultSeed = currDefaultSeed.curveAdd(addedSeed);
  153. otherVoteSeeds.push_back(Scalar());
  154. otherVoteSeeds[i] = currDefaultSeed;
  155. voteMatrix[i].push_back(encryptedDefaultVote);
  156. encryptedDefaultVote = bgnSystem.get_public_key().rerandomize(addedSeed, encryptedDefaultVote);
  157. currDefaultSeed = currDefaultSeed.curveAdd(addedSeed);
  158. userVoteSeeds.push_back(Scalar());
  159. userVoteSeeds[i] = currDefaultSeed;
  160. newRow.push_back(encryptedDefaultVote);
  161. }
  162. // Because we are adding the new user to the end (and then sorting it),
  163. // this last element (bottom right corner) is always the self vote.
  164. userVoteSeeds.push_back(Scalar());
  165. userVoteSeeds[newRow.size()] = currSelfSeed;
  166. otherVoteSeeds.push_back(Scalar());
  167. otherVoteSeeds[newRow.size()] = currSelfSeed;
  168. newRow.push_back(encryptedSelfVote);
  169. voteMatrix.push_back(newRow);
  170. Proof unused;
  171. std::vector<size_t> sortOrder = order_data(unused);
  172. std::vector<Scalar> newUserVoteSeeds;
  173. std::vector<Scalar> newOtherVoteSeeds;
  174. for (size_t i = 0; i < sortOrder.size(); i++)
  175. {
  176. newUserVoteSeeds.push_back(userVoteSeeds[sortOrder[i]]);
  177. newOtherVoteSeeds.push_back(otherVoteSeeds[sortOrder[i]]);
  178. }
  179. proofOfValidAddition = generate_proof_of_added_user(
  180. tallySeed,
  181. seedForUserTally,
  182. newUserVoteSeeds,
  183. newOtherVoteSeeds);
  184. }
  185. // Receive a new vote row from a user (identified by short term public key).
  186. bool PrsonaServer::receive_vote(
  187. const std::vector<Proof>& pi,
  188. const std::vector<CurveBipoint>& newVotes,
  189. const Curvepoint& shortTermPublicKey)
  190. {
  191. size_t voteSubmitter = binary_search(shortTermPublicKey);
  192. std::vector<CurveBipoint> oldVotes = voteMatrix[voteSubmitter];
  193. if (!verify_vote_proof(pi, oldVotes, newVotes, shortTermPublicKey))
  194. return false;
  195. voteMatrix[voteSubmitter] = newVotes;
  196. return true;
  197. }
  198. /*********************
  199. * PRIVATE FUNCTIONS *
  200. *********************/
  201. /*
  202. * CONSTRUCTOR HELPERS
  203. */
  204. const BGN& PrsonaServer::get_bgn_details() const
  205. {
  206. return bgnSystem;
  207. }
  208. bool PrsonaServer::initialize_fresh_generator(
  209. const std::vector<Proof>& pi,
  210. const Curvepoint& firstGenerator)
  211. {
  212. if (!verify_generator_proof(pi, firstGenerator, numServers))
  213. {
  214. std::cerr << "Could not verify generator proof, aborting." << std::endl;
  215. return false;
  216. }
  217. currentFreshGenerator = firstGenerator;
  218. return true;
  219. }
  220. // To calculate the blind generator for ElGamal, start from the base generator,
  221. // then have every server call this function on it iteratively (in any order).
  222. Curvepoint PrsonaServer::add_rand_seed_to_generator(
  223. std::vector<Proof>& pi,
  224. const Curvepoint& currGenerator) const
  225. {
  226. Scalar lambda;
  227. lambda.set_random();
  228. pi.push_back(add_to_generator_proof(currGenerator, lambda));
  229. return currGenerator * lambda;
  230. }
  231. bool PrsonaServer::set_EG_blind_generator(
  232. const std::vector<Proof>& pi,
  233. const Curvepoint& currGenerator)
  234. {
  235. return PrsonaBase::set_EG_blind_generator(pi, currGenerator, numServers);
  236. }
  237. /*
  238. * SCORE TALLYING
  239. */
  240. /* Calculate scores homomorphically (as an individual server would normally)
  241. * and then decrypt them (which the servers would normally work together to do).
  242. *
  243. * Note that since these calculations are just for us, we don't need to do any
  244. * expensive rerandomizations of intermediate values. */
  245. std::vector<Scalar> PrsonaServer::tally_scores()
  246. {
  247. std::vector<Quadripoint> BGNEncryptedTallies;
  248. std::vector<Scalar> decryptedTallies;
  249. for (size_t i = 0; i < voteMatrix.size(); i++)
  250. {
  251. std::vector<Quadripoint> weightedVotes;
  252. // ZIP
  253. for (size_t j = 0; j < previousVoteTallies.size(); j++)
  254. {
  255. Quadripoint curr =
  256. bgnSystem.homomorphic_multiplication_no_rerandomize(
  257. voteMatrix[j][i], previousVoteTallies[j]);
  258. weightedVotes.push_back(curr);
  259. }
  260. // FOLDL
  261. Quadripoint currEncryptedTally = weightedVotes[0];
  262. for (size_t j = 1; j < weightedVotes.size(); j++)
  263. {
  264. currEncryptedTally =
  265. bgnSystem.homomorphic_addition_no_rerandomize(
  266. currEncryptedTally, weightedVotes[j]);
  267. }
  268. // DECRYPT
  269. decryptedTallies.push_back(bgnSystem.decrypt(currEncryptedTally));
  270. }
  271. return decryptedTallies;
  272. }
  273. /* Calculate what the maximum possible score this round was (that is,
  274. * given the current user weights, what was the highest possible score?).
  275. *
  276. * As with individual scores, this also does the decryption that servers
  277. * would ordinarily work together to form. */
  278. Scalar PrsonaServer::get_max_possible_score(Proof& pi)
  279. {
  280. // FOLDL
  281. TwistBipoint currEncryptedVal = previousVoteTallies[0];
  282. for (size_t i = 1; i < previousVoteTallies.size(); i++)
  283. {
  284. currEncryptedVal =
  285. bgnSystem.homomorphic_addition_no_rerandomize(
  286. currEncryptedVal, previousVoteTallies[i]);
  287. }
  288. // DECRYPT
  289. Scalar retval = bgnSystem.decrypt(currEncryptedVal);
  290. pi = generate_proof_of_correct_sum();
  291. return retval;
  292. }
  293. /*
  294. * EPOCH ROUNDS
  295. */
  296. // The first round, going from A_0 to A_0.5
  297. void PrsonaServer::build_up_midway_pseudonyms(
  298. Proof& pi, Curvepoint& nextGenerator)
  299. {
  300. nextSeed.set_random();
  301. nextGenerator = nextGenerator * nextSeed;
  302. currentUserEncryptedTallies.clear();
  303. for (size_t i = 0; i < currentPseudonyms.size(); i++)
  304. currentPseudonyms[i] = currentPseudonyms[i] * nextSeed;
  305. rerandomize_data();
  306. order_data(pi);
  307. }
  308. // In between these rounds, scores are tallied, decrypted,
  309. // and encrypted to fresh user pseudonyms (possible through weird math)
  310. // The second round, going from A_0.5 to A_1
  311. void PrsonaServer::break_down_midway_pseudonyms(
  312. Proof& pi, const Curvepoint& nextGenerator)
  313. {
  314. Scalar inverseSeed = currentSeed.curveInverse();
  315. for (size_t i = 0; i < currentPseudonyms.size(); i++)
  316. {
  317. currentPseudonyms[i] = currentPseudonyms[i] * inverseSeed;
  318. currentUserEncryptedTallies[i].mask =
  319. currentUserEncryptedTallies[i].mask * inverseSeed;
  320. }
  321. currentSeed = nextSeed;
  322. currentFreshGenerator = nextGenerator;
  323. rerandomize_data();
  324. order_data(pi);
  325. }
  326. /*
  327. * DATA MAINTENANCE
  328. */
  329. void PrsonaServer::import_updates(
  330. const Proof& pi,
  331. const std::vector<TwistBipoint>& otherPreviousVoteTallies,
  332. const std::vector<Curvepoint>& otherCurrentPseudonyms,
  333. const std::vector<EGCiphertext>& otherCurrentUserEncryptedTallies,
  334. const std::vector<std::vector<CurveBipoint>>& otherVoteMatrix)
  335. {
  336. if (!verify_update_proof(pi))
  337. {
  338. std::cerr << "Could not verify valid update." << std::endl;
  339. return;
  340. }
  341. previousVoteTallies = otherPreviousVoteTallies;
  342. currentPseudonyms = otherCurrentPseudonyms;
  343. currentUserEncryptedTallies = otherCurrentUserEncryptedTallies;
  344. voteMatrix = otherVoteMatrix;
  345. }
  346. void PrsonaServer::export_updates(
  347. std::vector<TwistBipoint>& otherPreviousVoteTallies,
  348. std::vector<Curvepoint>& otherCurrentPseudonyms,
  349. std::vector<EGCiphertext>& otherCurrentUserEncryptedTallies,
  350. std::vector<std::vector<CurveBipoint>>& otherVoteMatrix) const
  351. {
  352. otherPreviousVoteTallies = previousVoteTallies;
  353. otherCurrentPseudonyms = currentPseudonyms;
  354. otherCurrentUserEncryptedTallies = currentUserEncryptedTallies;
  355. otherVoteMatrix = voteMatrix;
  356. }
  357. /*
  358. * DATA SAFEKEEPING
  359. */
  360. // Everything needs to be rerandomized during epoch rounds
  361. // NOTE: this may need to add something about proofs later?
  362. void PrsonaServer::rerandomize_data()
  363. {
  364. for (size_t i = 0; i < voteMatrix.size(); i++)
  365. {
  366. for (size_t j = 0; j < voteMatrix[0].size(); j++)
  367. voteMatrix[i][j] = bgnSystem.rerandomize(voteMatrix[i][j]);
  368. bgnSystem.rerandomize(previousVoteTallies[i]);
  369. if (!currentUserEncryptedTallies.empty())
  370. {
  371. Scalar rerandomizer;
  372. rerandomizer.set_random();
  373. currentUserEncryptedTallies[i].mask =
  374. currentUserEncryptedTallies[i].mask +
  375. currentPseudonyms[i] * rerandomizer;
  376. currentUserEncryptedTallies[i].encryptedMessage =
  377. currentUserEncryptedTallies[i].encryptedMessage +
  378. currentFreshGenerator * rerandomizer;
  379. }
  380. }
  381. }
  382. /* This is what powers the "shuffle"; really, as pseudonyms get updated,
  383. * the pseudonyms are no longer in the order prescribed by operator<().
  384. * So, we put them (and everything else) back into that order,
  385. * effectively shuffling them (and making lookups easier later on). */
  386. std::vector<size_t> PrsonaServer::order_data(Proof& pi)
  387. {
  388. std::vector<size_t> retval;
  389. // SortingType's index member allows us to replicate the "sort" across
  390. std::vector<SortingType> sortTracker;
  391. for (size_t i = 0; i < currentPseudonyms.size(); i++)
  392. {
  393. SortingType curr;
  394. curr.pseudonym = currentPseudonyms[i];
  395. curr.index = i;
  396. sortTracker.push_back(curr);
  397. }
  398. std::sort(sortTracker.begin(), sortTracker.end());
  399. // Order all other data in the same way, for consistency
  400. std::vector<Curvepoint> newPseudonyms;
  401. std::vector<TwistBipoint> newVoteTallies;
  402. std::vector<EGCiphertext> newUserEncryptedTallies;
  403. std::vector<std::vector<CurveBipoint>> newVoteMatrix;
  404. for (size_t i = 0; i < sortTracker.size(); i++)
  405. {
  406. newPseudonyms.push_back(sortTracker[i].pseudonym);
  407. newVoteTallies.push_back(previousVoteTallies[sortTracker[i].index]);
  408. if (!currentUserEncryptedTallies.empty())
  409. {
  410. newUserEncryptedTallies.push_back(
  411. currentUserEncryptedTallies[sortTracker[i].index]);
  412. }
  413. std::vector<CurveBipoint> currNewRow;
  414. for (size_t j = 0; j < currentPseudonyms.size(); j++)
  415. {
  416. currNewRow.push_back(
  417. voteMatrix[sortTracker[i].index][sortTracker[j].index]);
  418. }
  419. newVoteMatrix.push_back(currNewRow);
  420. retval.push_back(sortTracker[i].index);
  421. }
  422. previousVoteTallies = newVoteTallies;
  423. currentPseudonyms = newPseudonyms;
  424. currentUserEncryptedTallies = newUserEncryptedTallies;
  425. voteMatrix = newVoteMatrix;
  426. pi = generate_proof_of_shuffle();
  427. return retval;
  428. }
  429. /*
  430. * BINARY SEARCH
  431. */
  432. // Completely normal binary search
  433. size_t PrsonaServer::binary_search(const Curvepoint& index) const
  434. {
  435. return PrsonaBase::binary_search(currentPseudonyms, index);
  436. }
  437. /*
  438. * VALID VOTE PROOFS
  439. */
  440. bool PrsonaServer::verify_vote_proof(
  441. const std::vector<Proof>& pi,
  442. const std::vector<CurveBipoint>& oldVotes,
  443. const std::vector<CurveBipoint>& newVotes,
  444. const Curvepoint& shortTermPublicKey) const
  445. {
  446. const BGNPublicKey& pubKey = bgnSystem.get_public_key();
  447. return PrsonaBase::verify_vote_proof(
  448. pubKey.get_bipoint_curvegen(),
  449. pubKey.get_bipoint_curve_subgroup_gen(),
  450. pi,
  451. oldVotes,
  452. newVotes,
  453. currentFreshGenerator,
  454. shortTermPublicKey);
  455. }