server.hpp 12 KB

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