localMain.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <random>
  4. #include <chrono>
  5. #include "BGN.hpp"
  6. #include "client.hpp"
  7. #include "server.hpp"
  8. #include "serverEntity.hpp"
  9. using namespace std;
  10. // Initialize the classes we use
  11. void initialize_prsona_classes()
  12. {
  13. Scalar::init();
  14. PrsonaBase::init();
  15. }
  16. // Quick and dirty mean calculation (used for averaging timings)
  17. double mean(
  18. vector<double> xx)
  19. {
  20. return accumulate(xx.begin(), xx.end(), 0.0) / xx.size();
  21. }
  22. void print_user_scores(
  23. const vector<PrsonaClient>& users)
  24. {
  25. std::cout << "<";
  26. for (size_t i = 0; i < users.size(); i++)
  27. std::cout << users[i].get_score() << (i == users.size() - 1 ? ">" : " ");
  28. std::cout << std::endl;
  29. }
  30. bool test_proof_output(
  31. const vector<Proof>& pi)
  32. {
  33. vector<Proof> copy;
  34. stringstream buffer;
  35. for (size_t i = 0; i < pi.size(); i++)
  36. {
  37. Proof currProof;
  38. buffer << pi[i];
  39. buffer >> currProof;
  40. copy.push_back(currProof);
  41. }
  42. bool retval = true;
  43. for (size_t i = 0; i < pi.size(); i++)
  44. {
  45. if (!(copy[i] == pi[i]))
  46. cout << "FAILURE at index " << i+1 << " of " << pi.size() << endl;
  47. retval = retval && copy[i] == pi[i];
  48. }
  49. cout << "TEST PROOF OUTPUT: " << (retval ? "PASSED" : "FAILED") << endl;
  50. return retval;
  51. }
  52. // Time how long it takes to make a proof of valid votes
  53. vector<double> make_votes(
  54. default_random_engine& generator,
  55. vector<vector<TwistBipoint>>& newEncryptedVotes,
  56. vector<vector<Proof>>& validVoteProofs,
  57. const vector<PrsonaClient>& users,
  58. const PrsonaServerEntity& servers,
  59. size_t numVotes)
  60. {
  61. vector<double> retval;
  62. uniform_int_distribution<int> voteDistribution(0, PrsonaBase::get_max_allowed_vote());
  63. size_t numUsers = users.size();
  64. newEncryptedVotes.clear();
  65. for (size_t i = 0; i < numUsers; i++)
  66. {
  67. // Make the correct number of new votes, but shuffle where they go
  68. vector<Scalar> votes;
  69. vector<bool> replaces;
  70. for (size_t j = 0; j < numUsers; j++)
  71. {
  72. votes.push_back(Scalar(voteDistribution(generator)));
  73. replaces.push_back(j < numVotes);
  74. }
  75. shuffle(replaces.begin(), replaces.end(), generator);
  76. Proof baseProof;
  77. vector<Proof> fullProof;
  78. Twistpoint shortTermPublicKey = users[i].get_short_term_public_key();
  79. vector<TwistBipoint> currEncryptedVotes = servers.get_current_votes_by(baseProof, shortTermPublicKey);
  80. fullProof.push_back(baseProof);
  81. servers.get_other_vote_row_commitments(fullProof, shortTermPublicKey);
  82. vector<Proof> currVoteProof;
  83. chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
  84. currEncryptedVotes = users[i].make_votes(currVoteProof, fullProof, currEncryptedVotes, votes, replaces);
  85. chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
  86. newEncryptedVotes.push_back(currEncryptedVotes);
  87. validVoteProofs.push_back(currVoteProof);
  88. chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t1 - t0);
  89. retval.push_back(time_span.count());
  90. }
  91. return retval;
  92. }
  93. // Time how long it takes to validate a proof of valid votes
  94. vector<double> transmit_votes_to_servers(
  95. const vector<vector<TwistBipoint>>& newEncryptedVotes,
  96. const vector<vector<Proof>>& validVoteProofs,
  97. const vector<PrsonaClient>& users,
  98. PrsonaServerEntity& servers)
  99. {
  100. vector<double> retval;
  101. size_t numUsers = users.size();
  102. size_t numServers = servers.get_num_servers();
  103. for (size_t i = 0; i < numUsers; i++)
  104. {
  105. Proof ownerProof;
  106. Twistpoint shortTermPublicKey = users[i].get_short_term_public_key(ownerProof);
  107. for (size_t j = 0; j < numServers; j++)
  108. {
  109. chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
  110. servers.receive_vote(validVoteProofs[i], newEncryptedVotes[i], shortTermPublicKey, j);
  111. chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
  112. chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t1 - t0);
  113. retval.push_back(time_span.count());
  114. }
  115. }
  116. return retval;
  117. }
  118. // Time how long it takes to do the operations associated with an epoch
  119. double epoch(
  120. PrsonaServerEntity& servers)
  121. {
  122. // Do the epoch server calculations
  123. chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
  124. servers.epoch();
  125. chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
  126. // Return the timing of the epoch server calculations
  127. chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t1 - t0);
  128. return time_span.count();
  129. }
  130. // Time how long it takes each user to decrypt their new scores
  131. vector<double> transmit_epoch_updates(
  132. vector<PrsonaClient>& users,
  133. const PrsonaServerEntity& servers)
  134. {
  135. vector<double> retval;
  136. size_t numUsers = users.size();
  137. for (size_t i = 0; i < numUsers; i++)
  138. {
  139. chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
  140. servers.transmit_updates(users[i]);
  141. chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
  142. chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t1 - t0);
  143. retval.push_back(time_span.count());
  144. }
  145. return retval;
  146. }
  147. // Test if the proof of reputation level is working as expected
  148. void test_reputation_proof(
  149. default_random_engine& generator,
  150. const PrsonaServerEntity& servers,
  151. const PrsonaClient& a,
  152. const PrsonaClient& b)
  153. {
  154. bool flag;
  155. mpz_class aScore = a.get_score().toInt();
  156. size_t intScore = 0;
  157. while (intScore < aScore)
  158. intScore++;
  159. intScore = (intScore == 0 ? 1 : intScore);
  160. uniform_int_distribution<size_t> thresholdDistribution(0, intScore-1);
  161. Scalar goodThreshold(thresholdDistribution(generator));
  162. Scalar badThreshold(aScore + 1);
  163. Twistpoint shortTermPublicKey = a.get_short_term_public_key();
  164. vector<Proof> goodRepProof = a.generate_reputation_proof(goodThreshold, servers.get_num_clients());
  165. Proof baseProof;
  166. vector<Proof> fullProof;
  167. EGCiphertext currEncryptedScore = servers.get_current_user_encrypted_tally(baseProof, shortTermPublicKey);
  168. fullProof.push_back(baseProof);
  169. servers.get_other_user_tally_commitments(fullProof, shortTermPublicKey);
  170. flag = b.verify_reputation_proof(goodRepProof, shortTermPublicKey, goodThreshold, fullProof, currEncryptedScore);
  171. cout << "TEST VALID REPUTATION PROOF: " << (flag ? "PASSED (Proof verified)" : "FAILED (Proof not verified)" ) << endl;
  172. vector<Proof> badRepProof = a.generate_reputation_proof(badThreshold, servers.get_num_clients());
  173. baseProof.clear();
  174. fullProof.clear();
  175. currEncryptedScore = servers.get_current_user_encrypted_tally(baseProof, shortTermPublicKey);
  176. fullProof.push_back(baseProof);
  177. servers.get_other_user_tally_commitments(fullProof, shortTermPublicKey);
  178. flag = b.verify_reputation_proof(badRepProof, shortTermPublicKey, badThreshold, fullProof, currEncryptedScore);
  179. cout << "TEST INVALID REPUTATION PROOF: " << (flag ? "FAILED (Proof verified)" : "PASSED (Proof not verified)" ) << endl << endl;
  180. }
  181. // Test if the proof of valid votes is working as expected
  182. void test_vote_proof(
  183. default_random_engine& generator,
  184. const PrsonaClient& user,
  185. PrsonaServerEntity& servers)
  186. {
  187. size_t numUsers = servers.get_num_clients();
  188. vector<Scalar> votes;
  189. vector<bool> replaces;
  190. bool flag;
  191. for (size_t i = 0; i < numUsers; i++)
  192. {
  193. votes.push_back(Scalar(1));
  194. replaces.push_back(true);
  195. }
  196. vector<Proof> validVoteProof;
  197. Proof baseProof;
  198. vector<Proof> fullProof;
  199. Twistpoint shortTermPublicKey = user.get_short_term_public_key();
  200. vector<TwistBipoint> encryptedVotes = servers.get_current_votes_by(baseProof, shortTermPublicKey);
  201. fullProof.push_back(baseProof);
  202. servers.get_other_vote_row_commitments(fullProof, shortTermPublicKey);
  203. encryptedVotes = user.make_votes(validVoteProof, fullProof, encryptedVotes, votes, replaces);
  204. flag = servers.receive_vote(validVoteProof, encryptedVotes, shortTermPublicKey);
  205. cout << "TEST REPLACE VOTE PROOF: " << (flag ? "PASSED (Proof verified)" : "FAILED (Proof not verified)" ) << endl;
  206. for (size_t i = 0; i < numUsers; i++)
  207. replaces[i] = false;
  208. baseProof.clear();
  209. fullProof.clear();
  210. encryptedVotes = servers.get_current_votes_by(baseProof, shortTermPublicKey);
  211. fullProof.push_back(baseProof);
  212. servers.get_other_vote_row_commitments(fullProof, shortTermPublicKey);
  213. encryptedVotes = user.make_votes(validVoteProof, fullProof, encryptedVotes, votes, replaces);
  214. flag = servers.receive_vote(validVoteProof, encryptedVotes, shortTermPublicKey);
  215. cout << "TEST RERANDOMIZE VOTE PROOF: " << (flag ? "PASSED (Proof verified)" : "FAILED (Proof not verified)" ) << endl;
  216. for (size_t i = 0; i < numUsers; i++)
  217. {
  218. votes[i] = Scalar(3);
  219. replaces[i] = true;
  220. }
  221. baseProof.clear();
  222. fullProof.clear();
  223. encryptedVotes = servers.get_current_votes_by(baseProof, shortTermPublicKey);
  224. fullProof.push_back(baseProof);
  225. servers.get_other_vote_row_commitments(fullProof, shortTermPublicKey);
  226. encryptedVotes = user.make_votes(validVoteProof, fullProof, encryptedVotes, votes, replaces);
  227. flag = servers.receive_vote(validVoteProof, encryptedVotes, shortTermPublicKey);
  228. cout << "TEST INVALID REPLACE VOTE PROOF: " << (flag ? "FAILED (Proof verified)" : "PASSED (Proof not verified)" ) << endl << endl;
  229. }
  230. void check_vote_matrix_updates()
  231. {
  232. size_t numServers = 2;
  233. size_t numUsers = 3;
  234. cout << "Testing how the vote matrix updates." << endl;
  235. PrsonaBase::set_client_malicious();
  236. // Entities we operate with
  237. PrsonaServerEntity servers(numServers);
  238. vector<Proof> elGamalBlindGeneratorProof;
  239. BGNPublicKey bgnPublicKey = servers.get_bgn_public_key();
  240. Twistpoint elGamalBlindGenerator = servers.get_blinding_generator(elGamalBlindGeneratorProof);
  241. vector<PrsonaClient> users;
  242. for (size_t i = 0; i < numUsers; i++)
  243. {
  244. PrsonaClient currUser(elGamalBlindGeneratorProof, elGamalBlindGenerator, bgnPublicKey, numServers);
  245. users.push_back(currUser);
  246. servers.add_new_client(users[i]);
  247. }
  248. Proof pseudonymsProof;
  249. vector<Twistpoint> currentPseudonyms = servers.get_current_pseudonyms(pseudonymsProof);
  250. cout << "Making votes." << endl;
  251. for (size_t i = 0; i < numUsers; i++)
  252. {
  253. Twistpoint shortTermPublicKey = users[i].get_short_term_public_key();
  254. size_t myIndex = users[i].binary_search(currentPseudonyms, shortTermPublicKey);
  255. cout << "User " << i+1 << " has initial index " << myIndex << endl;
  256. vector<Scalar> votes;
  257. vector<bool> replaces;
  258. for (size_t j = 0; j < numUsers; j++)
  259. {
  260. if (j == myIndex)
  261. votes.push_back(Scalar(2));
  262. else if (j > myIndex)
  263. votes.push_back(Scalar(1));
  264. else
  265. votes.push_back(Scalar(0));
  266. replaces.push_back(true);
  267. }
  268. Proof baseProof;
  269. vector<Proof> fullProof;
  270. vector<TwistBipoint> currEncryptedVotes = servers.get_current_votes_by(baseProof, shortTermPublicKey);
  271. fullProof.push_back(baseProof);
  272. servers.get_other_vote_row_commitments(fullProof, shortTermPublicKey);
  273. vector<Proof> currVoteProof;
  274. currEncryptedVotes = users[i].make_votes(currVoteProof, fullProof, currEncryptedVotes, votes, replaces);
  275. servers.receive_vote(currVoteProof, currEncryptedVotes, shortTermPublicKey);
  276. cout << "User " << i+1 << " now has the following votes:" << endl;
  277. servers.print_current_votes_by(shortTermPublicKey);
  278. }
  279. servers.print_votes();
  280. epoch(servers);
  281. cout << "First epoch done." << endl;
  282. transmit_epoch_updates(users, servers);
  283. cout << "Updates given to users." << endl;
  284. servers.print_votes();
  285. for (size_t i = 0; i < numUsers; i++)
  286. {
  287. Proof ownerProof;
  288. Twistpoint shortTermPublicKey = users[i].get_short_term_public_key(ownerProof);
  289. cout << "User " << i+1 << " now has the following votes:" << endl;
  290. servers.print_current_votes_by(shortTermPublicKey);
  291. }
  292. }
  293. int main(int argc, char *argv[])
  294. {
  295. initialize_prsona_classes();
  296. // Defaults
  297. size_t numServers = 2;
  298. size_t numUsers = 5;
  299. size_t numRounds = 3;
  300. size_t numVotesPerRound = 3;
  301. size_t lambda = 0;
  302. bool maliciousServers = true;
  303. bool maliciousClients = true;
  304. string seedStr = "seed";
  305. // Potentially accept command line inputs
  306. if (argc > 1)
  307. numServers = atoi(argv[1]);
  308. if (argc > 2)
  309. numUsers = atoi(argv[2]);
  310. if (argc > 3)
  311. numRounds = atoi(argv[3]);
  312. if (argc > 4)
  313. numVotesPerRound = atoi(argv[4]);
  314. if (argc > 5)
  315. lambda = atoi(argv[5]);
  316. if (argc > 6)
  317. maliciousServers = argv[6][0] == 't' || argv[6][0] == 'T';
  318. if (argc > 7)
  319. maliciousClients = argv[7][0] == 't' || argv[7][0] == 'T';
  320. if (argc > 8)
  321. seedStr = argv[8];
  322. cout << "Running the protocol with the following parameters: " << endl;
  323. cout << numServers << " PRSONA servers" << endl;
  324. cout << numUsers << " participants (voters/votees)" << endl;
  325. cout << numRounds << " epochs" << endl;
  326. cout << numVotesPerRound << " new (random) votes by each user per epoch" << endl;
  327. cout << "Proof batching " << (lambda > 0 ? "IS" : "is NOT") << " in use." << (lambda > 0 ? " Batch parameter: " : "");
  328. if (lambda > 0)
  329. cout << lambda;
  330. cout << endl;
  331. cout << "Servers are set to " << (maliciousServers ? "MALICIOUS" : "HBC") << " security" << endl;
  332. cout << "Clients are set to " << (maliciousClients ? "MALICIOUS" : "HBC") << " security" << endl;
  333. cout << "Current randomness seed: \"" << seedStr << "\"" << endl;
  334. cout << endl;
  335. // Set malicious flags where necessary
  336. if (maliciousServers)
  337. PrsonaBase::set_server_malicious();
  338. if (maliciousClients)
  339. PrsonaBase::set_client_malicious();
  340. if (lambda > 0)
  341. PrsonaBase::set_lambda(lambda);
  342. // Entities we operate with
  343. PrsonaServerEntity servers(numServers);
  344. vector<Proof> elGamalBlindGeneratorProof;
  345. BGNPublicKey bgnPublicKey = servers.get_bgn_public_key();
  346. Twistpoint elGamalBlindGenerator = servers.get_blinding_generator(elGamalBlindGeneratorProof);
  347. test_proof_output(elGamalBlindGeneratorProof);
  348. cout << "Initialization: adding users to system" << endl << endl;
  349. vector<PrsonaClient> users;
  350. for (size_t i = 0; i < numUsers; i++)
  351. {
  352. PrsonaClient currUser(elGamalBlindGeneratorProof, elGamalBlindGenerator, bgnPublicKey, numServers);
  353. users.push_back(currUser);
  354. servers.add_new_client(users[i]);
  355. }
  356. // Seeded randomness for random votes used in epoch
  357. seed_seq seed(seedStr.begin(), seedStr.end());
  358. default_random_engine generator(seed);
  359. // Do the epoch operations
  360. for (size_t i = 0; i < numRounds; i++)
  361. {
  362. vector<double> timings;
  363. cout << "Round " << i+1 << " of " << numRounds << ": " << endl;
  364. vector<vector<TwistBipoint>> newEncryptedVotes;
  365. vector<vector<Proof>> validVoteProofs;
  366. timings = make_votes(generator, newEncryptedVotes, validVoteProofs, users, servers, numVotesPerRound);
  367. cout << "Vote generation (with proofs): " << mean(timings) << " seconds per user" << endl;
  368. timings.clear();
  369. timings = transmit_votes_to_servers(newEncryptedVotes, validVoteProofs, users, servers);
  370. cout << "Vote validation: " << mean(timings) << " seconds per vote vector/server" << endl;
  371. timings.clear();
  372. timings.push_back(epoch(servers));
  373. cout << "Epoch computation: " << mean(timings) << " seconds" << endl;
  374. timings.clear();
  375. timings = transmit_epoch_updates(users, servers);
  376. cout << "Transmit epoch updates: " << mean(timings) << " seconds per user" << endl << endl;
  377. }
  378. // Pick random users for our tests
  379. uniform_int_distribution<size_t> userDistribution(0, numUsers - 1);
  380. size_t user_a = userDistribution(generator);
  381. size_t user_b = user_a;
  382. while (user_b == user_a)
  383. user_b = userDistribution(generator);
  384. test_reputation_proof(generator, servers, users[user_a], users[user_b]);
  385. test_vote_proof(generator, users[user_a], servers);
  386. return 0;
  387. }