main.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // #include <boost/program_options.hpp>
  2. // namespace po = boost::program_options;
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <random>
  6. #include <chrono>
  7. #include "BGN.hpp"
  8. #include "client.hpp"
  9. #include "server.hpp"
  10. #include "serverEntity.hpp"
  11. using namespace std;
  12. // int argparse(int argc, char *argv[], size_t &numServers, size_t &numClients, size_t &numRounds, size_t &numVotes, bool &maliciousServers, bool &maliciousUsers, string &seedStr)
  13. // {
  14. // string config_file;
  15. // // Declare a group of options that will be
  16. // // allowed only on command line
  17. // po::options_description generic("General options");
  18. // generic.add_options()
  19. // ("help", "produce this help message")
  20. // ("config,c", po::value<string>(&config_file)->default_value(""),
  21. // "name of a configuration file")
  22. // ;
  23. // // Declare a group of options that will be
  24. // // allowed both on command line and in
  25. // // config file
  26. // po::options_description config("Configuration");
  27. // config.add_options()
  28. // ("malicious-servers,M", "presence of this flag indicates servers will operate in malicious model")
  29. // ("malicious-users,U", "presence of this flag indicates users will operate in malicious model")
  30. // ("seed", po::value<string>(&seedStr)->default_value("default"),
  31. // "the random seed to use for this test")
  32. // ;
  33. // // Hidden options, will be allowed both on command line and
  34. // // in config file, but will not be shown to the user.
  35. // po::options_description hidden("Hidden options");
  36. // hidden.add_options()
  37. // ("number-servers,S", po::value<size_t>(&numServers)->default_value(2),
  38. // "number of servers in test")
  39. // ("number-users,N", po::value<size_t>(&numClients)->default_value(5),
  40. // "number of users in test")
  41. // ("number-rounds,R", po::value<size_t>(&numRounds)->default_value(3),
  42. // "number of rounds to perform in test")
  43. // ("number-votes,V", po::value<size_t>(&numVotes)->default_value(3),
  44. // "number of votes each user makes per round during test")
  45. // ;
  46. // po::options_description cmdline_options;
  47. // cmdline_options.add(generic).add(config).add(hidden);
  48. // po::options_description config_file_options;
  49. // config_file_options.add(config).add(hidden);
  50. // po::options_description visible("Allowed options");
  51. // visible.add(generic).add(config);
  52. // po::positional_options_description p;
  53. // p.add("number-servers", 1);
  54. // p.add("number-users", 1);
  55. // p.add("number-rounds", 1);
  56. // p.add("number-votes", 1);
  57. // po::variables_map vm;
  58. // store(po::command_line_parser(argc, argv).
  59. // options(cmdline_options).positional(p).run(), vm);
  60. // notify(vm);
  61. // if (!config_file.empty())
  62. // {
  63. // ifstream config(config_file.c_str());
  64. // if (!config)
  65. // {
  66. // cerr << "Cannot open config file: " << config_file << "\n";
  67. // return 2;
  68. // }
  69. // else
  70. // {
  71. // store(parse_config_file(config, config_file_options), vm);
  72. // notify(vm);
  73. // }
  74. // }
  75. // if (vm.count("help"))
  76. // {
  77. // cout << visible << endl;
  78. // return 1;
  79. // }
  80. // maliciousServers = vm.count("malicious-servers");
  81. // maliciousUsers = vm.count("malicious-users");
  82. // return 0;
  83. // }
  84. double epoch(default_random_engine& generator, PrsonaServerEntity& servers, vector<PrsonaClient>& users, size_t numVotes)
  85. {
  86. Proof unused;
  87. uniform_int_distribution<int> voteDistribution(0, 3);
  88. size_t numUsers = users.size();
  89. for (size_t i = 0; i < numUsers; i++)
  90. {
  91. vector<Scalar> votes;
  92. vector<bool> replace;
  93. for (size_t j = 0; j < numUsers; j++)
  94. {
  95. votes.push_back(Scalar(voteDistribution(generator)));
  96. replace.push_back(j < numVotes);
  97. }
  98. shuffle(replace.begin(), replace.end(), generator);
  99. vector<CurveBipoint> encryptedVotes;
  100. Proof pi;
  101. Curvepoint shortTermPublicKey = users[i].get_short_term_public_key(pi);
  102. encryptedVotes = servers.get_current_votes_by(pi, shortTermPublicKey);
  103. users[i].receive_encrypted_votes(pi, encryptedVotes, false);
  104. users[i].make_votes(pi, encryptedVotes, votes, replace);
  105. servers.receive_vote(pi, encryptedVotes, users[i].get_short_term_public_key(unused));
  106. }
  107. chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
  108. servers.epoch();
  109. chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
  110. for (size_t i = 0; i < numUsers; i++)
  111. servers.transmit_score(users[i]);
  112. chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t1 - t0);
  113. return time_span.count();
  114. }
  115. int main(int argc, char *argv[])
  116. {
  117. Scalar::init();
  118. size_t numServers = 2;
  119. size_t numUsers = 5;
  120. size_t numRounds = 3;
  121. size_t numVotesPerRound = 3;
  122. bool maliciousServers = false;
  123. bool maliciousUsers = false;
  124. string seedStr = "seed";
  125. if (argc > 1)
  126. numServers = atoi(argv[1]);
  127. if (argc > 2)
  128. numUsers = atoi(argv[2]);
  129. if (argc > 3)
  130. numRounds = atoi(argv[3]);
  131. if (argc > 4)
  132. numVotesPerRound = atoi(argv[4]);
  133. cout << "Running the protocol with the following parameters: " << endl;
  134. cout << numServers << " PRSONA servers" << endl;
  135. cout << numUsers << " participants (voters/votees)" << endl;
  136. cout << numRounds << " epochs" << endl;
  137. cout << numVotesPerRound << " new (random) votes by each user in each epoch" << endl;
  138. if (maliciousServers)
  139. {
  140. PrsonaServer::set_malicious_server();
  141. PrsonaClient::set_malicious_server();
  142. }
  143. if (maliciousUsers)
  144. {
  145. PrsonaServer::set_malicious_client();
  146. PrsonaClient::set_malicious_client();
  147. }
  148. PrsonaServerEntity servers(numServers);
  149. BGNPublicKey bgnPublicKey = servers.get_bgn_public_key();
  150. Curvepoint EGBlindGenerator = servers.get_blinding_generator();
  151. cout << "Initialization: adding users to system" << endl;
  152. vector<PrsonaClient> users;
  153. for (size_t i = 0; i < numUsers; i++)
  154. {
  155. PrsonaClient currUser(bgnPublicKey, EGBlindGenerator);
  156. servers.add_new_client(currUser);
  157. users.push_back(currUser);
  158. }
  159. seed_seq seed(seedStr.begin(), seedStr.end());
  160. default_random_engine generator(seed);
  161. for (size_t i = 0; i < numRounds; i++)
  162. {
  163. cout << "Round " << i+1 << " of " << numRounds << ": ";
  164. cout << epoch(generator, servers, users, numVotesPerRound) << " seconds" << endl;
  165. }
  166. return 0;
  167. }