server.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. #ifndef __PRSONA_SERVER_HPP
  2. #define __PRSONA_SERVER_HPP
  3. #include <vector>
  4. #include "BGN.hpp"
  5. #include "Curvepoint.hpp"
  6. #include "Bipoint.hpp"
  7. #include "base.hpp"
  8. #include "EGCiphertext.hpp"
  9. #include "proof.hpp"
  10. class PrsonaServer : public PrsonaBase {
  11. public:
  12. // CONSTRUCTORS
  13. PrsonaServer(size_t numServers);
  14. PrsonaServer(size_t numServers, const BGN& other_bgn);
  15. // BASIC PUBLIC SYSTEM INFO GETTERS
  16. BGNPublicKey get_bgn_public_key() const;
  17. size_t get_num_clients() const;
  18. size_t get_num_servers() const;
  19. Twistpoint get_fresh_generator(std::vector<Proof>& pi) const;
  20. // FRESH GENERATOR CALCULATION
  21. Twistpoint add_curr_seed_to_generator(
  22. std::vector<Proof>& pi,
  23. const Twistpoint& currGenerator) const;
  24. Twistpoint add_next_seed_to_generator(
  25. std::vector<Proof>& pi,
  26. const Twistpoint& currGenerator) const;
  27. Twistpoint add_rand_seed_to_generator(
  28. std::vector<Proof>& pi,
  29. const Twistpoint& currGenerator) const;
  30. // ENCRYPTED DATA GETTERS
  31. std::vector<TwistBipoint> get_current_votes_by(
  32. Proof& pi, const Twistpoint& shortTermPublicKey) const;
  33. std::vector<std::vector<TwistBipoint>> get_all_current_votes(
  34. Proof& pi) const;
  35. EGCiphertext get_current_user_encrypted_tally(
  36. Proof& pi, const Twistpoint& shortTermPublicKey) const;
  37. CurveBipoint get_current_server_encrypted_tally(
  38. Proof& pi, const Twistpoint& shortTermPublicKey) const;
  39. std::vector<Twistpoint> get_current_pseudonyms(Proof& pi) const;
  40. std::vector<Twistpoint> get_current_pseudonyms() const;
  41. // PROOF COMMITMENT GETTERS
  42. Proof get_vote_row_commitment(const Twistpoint& request) const;
  43. Proof get_vote_matrix_commitment() const;
  44. Proof get_user_tally_commitment(const Twistpoint& request) const;
  45. Proof get_server_tally_commitment(const Twistpoint& request) const;
  46. Proof get_pseudonyms_commitment() const;
  47. void print_current_commitments() const;
  48. // CLIENT INTERACTIONS
  49. void add_new_client(
  50. std::vector<Proof>& proofOfValidAddition,
  51. const Proof& proofOfValidKey,
  52. const Twistpoint& shortTermPublicKey);
  53. bool receive_vote(
  54. const std::vector<Proof>& pi,
  55. const std::vector<TwistBipoint>& newVotes,
  56. const Twistpoint& shortTermPublicKey);
  57. void print_scores(const std::vector<CurveBipoint>& scores);
  58. // CONSTRUCTOR HELPERS
  59. const BGN& get_bgn_details() const;
  60. bool initialize_fresh_generator(
  61. const std::vector<Proof>& pi,
  62. const Twistpoint& firstGenerator);
  63. bool set_EG_blind_generator(
  64. const std::vector<Proof>& pi,
  65. const Twistpoint& currGenerator);
  66. // EPOCH ROUNDS
  67. void build_up_midway_pseudonyms(
  68. std::vector<std::vector<std::vector<Proof>>>& pi,
  69. std::vector<std::vector<std::vector<Twistpoint>>>& permutationCommits,
  70. std::vector<std::vector<std::vector<Twistpoint>>>& freshPseudonymCommits,
  71. std::vector<std::vector<std::vector<Twistpoint>>>& freshPseudonymSeedCommits,
  72. std::vector<std::vector<std::vector<CurveBipoint>>>& serverTallyCommits,
  73. std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>>& partwayVoteMatrixCommits,
  74. std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>>& finalVoteMatrixCommits,
  75. Twistpoint& nextGenerator);
  76. void break_down_midway_pseudonyms(
  77. const std::vector<Proof>& generatorProof,
  78. std::vector<std::vector<std::vector<Proof>>>& pi,
  79. std::vector<std::vector<std::vector<Twistpoint>>>& permutationCommits,
  80. std::vector<std::vector<std::vector<Twistpoint>>>& freshPseudonymCommits,
  81. std::vector<std::vector<std::vector<Twistpoint>>>& freshPseudonymSeedCommits,
  82. std::vector<std::vector<std::vector<CurveBipoint>>>& serverTallyCommits,
  83. std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>>& partwayVoteMatrixCommits,
  84. std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>>& finalVoteMatrixCommits,
  85. std::vector<std::vector<std::vector<Twistpoint>>>& userTallyMaskCommits,
  86. std::vector<std::vector<std::vector<Twistpoint>>>& userTallyMessageCommits,
  87. std::vector<std::vector<std::vector<Twistpoint>>>& userTallySeedCommits,
  88. const Twistpoint& nextGenerator);
  89. bool accept_epoch_updates(
  90. const std::vector<std::vector<Proof>>& pi,
  91. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  92. const std::vector<std::vector<Twistpoint>>& freshPseudonymCommits,
  93. const std::vector<std::vector<Twistpoint>>& freshPseudonymSeedCommits,
  94. const std::vector<std::vector<CurveBipoint>>& serverTallyCommits,
  95. const std::vector<std::vector<std::vector<TwistBipoint>>>& partwayVoteMatrixCommits,
  96. const std::vector<std::vector<std::vector<TwistBipoint>>>& finalVoteMatrixCommits,
  97. const std::vector<std::vector<Twistpoint>>& userTallyMaskCommits,
  98. const std::vector<std::vector<Twistpoint>>& userTallyMessageCommits,
  99. const std::vector<std::vector<Twistpoint>>& userTallySeedCommits,
  100. const Twistpoint& nextGenerator,
  101. bool doUserTallies);
  102. // DATA MAINTENANCE
  103. void export_new_user_update(
  104. std::vector<CurveBipoint>& otherPreviousVoteTallies,
  105. std::vector<Twistpoint>& otherCurrentPseudonyms,
  106. std::vector<EGCiphertext>& otherCurrentUserEncryptedTallies,
  107. std::vector<std::vector<TwistBipoint>>& otherVoteMatrix
  108. ) const;
  109. bool import_new_user_update(
  110. const std::vector<Proof>& pi,
  111. const std::vector<CurveBipoint>& otherPreviousVoteTallies,
  112. const std::vector<Twistpoint>& otherCurrentPseudonyms,
  113. const std::vector<EGCiphertext>& otherCurrentUserEncryptedTallies,
  114. const std::vector<std::vector<TwistBipoint>>& otherVoteMatrix);
  115. // SCORE TALLYING
  116. std::vector<Scalar> tally_scores();
  117. Scalar get_max_possible_score();
  118. void receive_tallied_scores(
  119. const std::vector<EGCiphertext>& userTallyScores,
  120. const std::vector<CurveBipoint>& serverTallyScores);
  121. void encrypt(
  122. CurveBipoint& element,
  123. const Scalar& value);
  124. private:
  125. // constants for servers
  126. const size_t numServers;
  127. // Identical between all servers (but collaboratively constructed)
  128. BGN bgnSystem;
  129. // Private; different for each server
  130. Scalar currentSeed;
  131. Scalar nextSeed;
  132. // The actual data, which is collaboratively updated by all servers
  133. std::vector<Proof> currentGeneratorProof;
  134. Twistpoint currentFreshGenerator;
  135. std::vector<CurveBipoint> previousVoteTallies;
  136. std::vector<Twistpoint> currentPseudonyms;
  137. std::vector<EGCiphertext> currentUserEncryptedTallies;
  138. std::vector<std::vector<TwistBipoint>> voteMatrix;
  139. /**
  140. * NOTE: voteMatrix structure:
  141. * Each element represents a vote by <rowID> applied to <colID>.
  142. * The outer vector is a vector of rows and the inner vector is
  143. * a vector of encrypted votes.
  144. */
  145. // An imaginary class; it's just used right now to coordinate servers
  146. // in memory instead of via network action.
  147. friend class PrsonaServerEntity;
  148. // EPOCH HELPERS
  149. std::vector<std::vector<Proof>> epoch_calculations(
  150. std::vector<std::vector<Twistpoint>>& permutationCommits,
  151. std::vector<std::vector<Twistpoint>>& freshPseudonymCommits,
  152. std::vector<std::vector<Twistpoint>>& freshPseudonymSeedCommits,
  153. std::vector<std::vector<CurveBipoint>>& serverTallyCommits,
  154. std::vector<std::vector<std::vector<TwistBipoint>>>& partwayVoteMatrixCommits,
  155. std::vector<std::vector<std::vector<TwistBipoint>>>& finalVoteMatrixCommits,
  156. std::vector<std::vector<Twistpoint>>& userTallyMaskCommits,
  157. std::vector<std::vector<Twistpoint>>& userTallyMessageCommits,
  158. std::vector<std::vector<Twistpoint>>& userTallySeedCommits,
  159. const Scalar& power,
  160. const Twistpoint& nextGenerator,
  161. bool doUserTallies);
  162. std::vector<std::vector<Scalar>> generate_permutation_matrix(
  163. const Scalar& reorderSeed
  164. ) const;
  165. std::vector<std::vector<Twistpoint>> generate_commitment_matrix(
  166. const std::vector<std::vector<Scalar>>& permutations,
  167. std::vector<std::vector<Scalar>>& seeds
  168. ) const;
  169. std::vector<std::vector<Twistpoint>> generate_pseudonym_matrix(
  170. const std::vector<std::vector<Scalar>>& permutations,
  171. const Scalar& power,
  172. std::vector<std::vector<Scalar>>& seeds,
  173. std::vector<std::vector<Twistpoint>>& seedCommits
  174. ) const;
  175. std::vector<std::vector<CurveBipoint>> generate_server_tally_matrix(
  176. const std::vector<std::vector<Scalar>>& permutations,
  177. std::vector<std::vector<Scalar>>& seeds
  178. ) const;
  179. std::vector<std::vector<std::vector<TwistBipoint>>> generate_vote_tensor(
  180. const std::vector<std::vector<Scalar>>& permutations,
  181. const std::vector<std::vector<TwistBipoint>>& currVoteMatrix,
  182. std::vector<std::vector<std::vector<Scalar>>>& seeds,
  183. bool inverted
  184. ) const;
  185. std::vector<std::vector<TwistBipoint>> calculate_next_vote_matrix(
  186. const std::vector<std::vector<std::vector<TwistBipoint>>>& voteTensor
  187. ) const;
  188. void generate_vote_tensor_proofs(
  189. std::vector<std::vector<Proof>>& pi,
  190. const std::vector<std::vector<Scalar>>& permutations,
  191. const std::vector<std::vector<Scalar>>& permutationSeeds,
  192. const std::vector<std::vector<std::vector<Scalar>>>& matrixSeeds,
  193. const std::vector<std::vector<TwistBipoint>>& currMatrix,
  194. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  195. const std::vector<std::vector<std::vector<TwistBipoint>>>& matrixCommits,
  196. bool inverted
  197. ) const;
  198. bool verify_vote_tensor_proofs(
  199. const std::vector<std::vector<Proof>>& pi,
  200. size_t start_offset,
  201. const std::vector<std::vector<TwistBipoint>>& currMatrix,
  202. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  203. const std::vector<std::vector<std::vector<TwistBipoint>>>& matrixCommits,
  204. bool inverted
  205. ) const;
  206. void generate_user_tally_matrix(
  207. const std::vector<std::vector<Scalar>>& permutations,
  208. const Scalar& power,
  209. const Twistpoint& nextGenerator,
  210. const std::vector<Twistpoint>& currPseudonyms,
  211. std::vector<Twistpoint>& masks,
  212. std::vector<std::vector<Twistpoint>>& maskCommits,
  213. std::vector<Twistpoint>& messages,
  214. std::vector<std::vector<Twistpoint>>& messageCommits,
  215. std::vector<std::vector<Scalar>>& userTallySeeds,
  216. std::vector<std::vector<Twistpoint>>& userTallySeedCommits
  217. ) const;
  218. template <typename T>
  219. std::vector<std::vector<T>> generate_reordered_plus_power_matrix(
  220. const std::vector<std::vector<Scalar>>& permutations,
  221. const Scalar& power,
  222. const std::vector<T>& oldValues,
  223. std::vector<std::vector<Scalar>>& seeds,
  224. std::vector<std::vector<Twistpoint>>& seedCommits,
  225. const T& h
  226. ) const;
  227. template <typename T>
  228. std::vector<std::vector<T>> generate_reordered_matrix(
  229. const std::vector<std::vector<Scalar>>& permutations,
  230. const std::vector<T>& oldValues,
  231. std::vector<std::vector<Scalar>>& seeds,
  232. const T& h,
  233. bool cancelOut
  234. ) const;
  235. template <typename T>
  236. std::vector<std::vector<T>> transpose_matrix(
  237. const std::vector<std::vector<T>>& input
  238. ) const;
  239. std::vector<size_t> sort_data(
  240. const std::vector<Twistpoint>& inputs
  241. ) const;
  242. // A helper class for "ordering" data and for binary search
  243. struct SortingType {
  244. Twistpoint pseudonym;
  245. size_t index;
  246. bool operator<( const SortingType& rhs ) const
  247. { return pseudonym < rhs.pseudonym; }
  248. };
  249. template <typename T>
  250. T encrypt(
  251. const T& g,
  252. const T& h,
  253. const Scalar& plaintext,
  254. const Scalar& lambda
  255. ) const;
  256. bool update_data(
  257. const std::vector<std::vector<Twistpoint>>& freshPseudonymCommits,
  258. const std::vector<std::vector<CurveBipoint>>& serverTallyCommits,
  259. const std::vector<std::vector<std::vector<TwistBipoint>>>& voteMatrixCommits,
  260. const std::vector<std::vector<Twistpoint>>& userTallyMaskCommits,
  261. const std::vector<std::vector<Twistpoint>>& userTallyMessageCommits);
  262. bool pseudonyms_sorted(
  263. const std::vector<Twistpoint> newPseudonyms
  264. ) const;
  265. // DATA SAFEKEEPING
  266. std::vector<size_t> order_data();
  267. // BINARY SEARCH
  268. size_t binary_search(const Twistpoint& index) const;
  269. // VALID VOTE PROOFS
  270. bool verify_vote_proof(
  271. const std::vector<Proof>& pi,
  272. const std::vector<TwistBipoint>& oldVotes,
  273. const std::vector<TwistBipoint>& newVotes,
  274. const Twistpoint& shortTermPublicKey
  275. ) const;
  276. };
  277. #endif