main.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. void 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. users[i].make_votes(pi, encryptedVotes, votes, replace);
  102. servers.receive_vote(pi, encryptedVotes, users[i].get_short_term_public_key(unused));
  103. }
  104. servers.epoch();
  105. for (size_t i = 0; i < numUsers; i++)
  106. servers.transmit_score(users[i]);
  107. }
  108. int main(int argc, char *argv[])
  109. {
  110. size_t numServers, numUsers, numRounds, numVotesPerRound;
  111. bool maliciousServers, maliciousUsers;
  112. string seedStr;
  113. // err - 1 because the help text returns 1 (and shouldn't flag as an error occurring)
  114. // int err;
  115. // if ((err = argparse(argc, argv, numServers, numUsers, numRounds, numVotesPerRound, maliciousServers, maliciousUsers, seedStr)))
  116. // return (err - 1);
  117. numServers = 2;
  118. numUsers = 5;
  119. numRounds = 3;
  120. numVotesPerRound = 3;
  121. maliciousServers = false;
  122. maliciousUsers = false;
  123. if (maliciousServers)
  124. {
  125. PrsonaServer::set_malicious_server();
  126. PrsonaClient::set_malicious_server();
  127. }
  128. if (maliciousUsers)
  129. {
  130. PrsonaServer::set_malicious_client();
  131. PrsonaClient::set_malicious_client();
  132. }
  133. PrsonaServerEntity servers(numServers);
  134. BGNPublicKey bgnPublicKey = servers.get_bgn_public_key();
  135. Curvepoint EGBlindGenerator = servers.get_blinding_generator();
  136. vector<PrsonaClient> users;
  137. for (size_t i = 0; i < numUsers; i++)
  138. {
  139. PrsonaClient currUser(bgnPublicKey, EGBlindGenerator);
  140. servers.add_new_client(currUser);
  141. users.push_back(currUser);
  142. }
  143. seed_seq seed(seedStr.begin(), seedStr.end());
  144. default_random_engine generator(seed);
  145. for (size_t i = 0; i < numRounds; i++)
  146. {
  147. cout << "Round " << i << " commencing." << endl;
  148. epoch(generator, servers, users, numVotesPerRound);
  149. }
  150. return 0;
  151. }