server.cpp 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932
  1. #include <iostream>
  2. #include <thread>
  3. #include "server.hpp"
  4. /********************
  5. * PUBLIC FUNCTIONS *
  6. ********************/
  7. /*
  8. * CONSTRUCTORS
  9. */
  10. // Used to generate the first server; instantiates BGN for the first time
  11. PrsonaServer::PrsonaServer(
  12. size_t numServers)
  13. : numServers(numServers)
  14. {
  15. currentSeed.set_random();
  16. decryptMtx = new std::mutex();
  17. }
  18. // Used for all other servers, so they have the same BGN parameters
  19. PrsonaServer::PrsonaServer(
  20. size_t numServers,
  21. const BGN& otherBgn)
  22. : numServers(numServers), bgnSystem(otherBgn)
  23. {
  24. currentSeed.set_random();
  25. decryptMtx = new std::mutex();
  26. }
  27. PrsonaServer::PrsonaServer(
  28. const PrsonaServer& other)
  29. {
  30. copy(other);
  31. }
  32. PrsonaServer::PrsonaServer(
  33. PrsonaServer&& other)
  34. {
  35. numServers = std::move(other.numServers);
  36. decryptMtx = other.decryptMtx;
  37. other.decryptMtx = NULL;
  38. bgnSystem = std::move(other.bgnSystem);
  39. currentSeed = std::move(other.currentSeed);
  40. nextSeed = std::move(other.nextSeed);
  41. currentGeneratorProof = std::move(other.currentGeneratorProof);
  42. currentFreshGenerator = std::move(other.currentFreshGenerator);
  43. previousVoteTallies = std::move(other.previousVoteTallies);
  44. currentPseudonyms = std::move(other.currentPseudonyms);
  45. currentUserEncryptedTallies = std::move(other.currentUserEncryptedTallies);
  46. voteMatrix = std::move(other.voteMatrix);
  47. }
  48. PrsonaServer &PrsonaServer::operator=(
  49. const PrsonaServer& other)
  50. {
  51. if (&other != this)
  52. {
  53. remove();
  54. copy(other);
  55. }
  56. return *this;
  57. }
  58. PrsonaServer &PrsonaServer::operator=(
  59. PrsonaServer&& other)
  60. {
  61. if (&other != this)
  62. {
  63. remove();
  64. numServers = std::move(other.numServers);
  65. decryptMtx = other.decryptMtx;
  66. other.decryptMtx = new std::mutex();
  67. bgnSystem = std::move(other.bgnSystem);
  68. currentSeed = std::move(other.currentSeed);
  69. nextSeed = std::move(other.nextSeed);
  70. currentGeneratorProof = std::move(other.currentGeneratorProof);
  71. currentFreshGenerator = std::move(other.currentFreshGenerator);
  72. previousVoteTallies = std::move(other.previousVoteTallies);
  73. currentPseudonyms = std::move(other.currentPseudonyms);
  74. currentUserEncryptedTallies = std::move(other.currentUserEncryptedTallies);
  75. voteMatrix = std::move(other.voteMatrix);
  76. }
  77. return *this;
  78. }
  79. PrsonaServer::~PrsonaServer()
  80. {
  81. remove();
  82. }
  83. void PrsonaServer::copy(
  84. const PrsonaServer& other)
  85. {
  86. numServers = other.numServers;
  87. decryptMtx = new std::mutex();
  88. bgnSystem = other.bgnSystem;
  89. currentSeed = other.currentSeed;
  90. nextSeed = other.nextSeed;
  91. currentGeneratorProof = other.currentGeneratorProof;
  92. currentFreshGenerator = other.currentFreshGenerator;
  93. previousVoteTallies = other.previousVoteTallies;
  94. currentPseudonyms = other.currentPseudonyms;
  95. currentUserEncryptedTallies = other.currentUserEncryptedTallies;
  96. voteMatrix = other.voteMatrix;
  97. }
  98. void PrsonaServer::remove()
  99. {
  100. delete decryptMtx;
  101. decryptMtx = NULL;
  102. }
  103. /*
  104. * BASIC PUBLIC SYSTEM INFO GETTERS
  105. */
  106. BGNPublicKey PrsonaServer::get_bgn_public_key() const
  107. {
  108. return bgnSystem.get_public_key();
  109. }
  110. size_t PrsonaServer::get_num_clients() const
  111. {
  112. return currentPseudonyms.size();
  113. }
  114. size_t PrsonaServer::get_num_servers() const
  115. {
  116. return numServers;
  117. }
  118. Twistpoint PrsonaServer::get_fresh_generator(
  119. std::vector<Proof>& pi) const
  120. {
  121. pi = currentGeneratorProof;
  122. return currentFreshGenerator;
  123. }
  124. /*
  125. * FRESH GENERATOR CALCULATION
  126. */
  127. // To calculate the current epoch's generator, start from the base generator,
  128. // then have every server call this function on it iteratively (in any order).
  129. Twistpoint PrsonaServer::add_curr_seed_to_generator(
  130. std::vector<Proof>& pi,
  131. const Twistpoint& currGenerator) const
  132. {
  133. pi.push_back(add_to_generator_proof(currGenerator, currentSeed));
  134. return currGenerator * currentSeed;
  135. }
  136. // To calculate the next epoch's generator, start from the base generator,
  137. // then have every server call this function on it iteratively (in any order).
  138. Twistpoint PrsonaServer::add_next_seed_to_generator(
  139. std::vector<Proof>& pi,
  140. const Twistpoint& currGenerator) const
  141. {
  142. pi.push_back(add_to_generator_proof(currGenerator, nextSeed));
  143. return currGenerator * nextSeed;
  144. }
  145. /*
  146. * ENCRYPTED DATA GETTERS
  147. */
  148. /* Call this in order to get the current encrypted votes cast by a given user
  149. * (who is identified by their short term public key).
  150. * In practice, this is intended for clients,
  151. * who need to know their current votes in order to rerandomize them. */
  152. std::vector<TwistBipoint> PrsonaServer::get_current_votes_by(
  153. Proof& pi,
  154. const Twistpoint& shortTermPublicKey) const
  155. {
  156. std::vector<TwistBipoint> retval;
  157. size_t voteSubmitter = binary_search(shortTermPublicKey);
  158. retval = voteMatrix[voteSubmitter];
  159. pi = generate_valid_vote_row_proof(retval);
  160. return retval;
  161. }
  162. std::vector<std::vector<TwistBipoint>> PrsonaServer::get_all_current_votes(
  163. Proof& pi) const
  164. {
  165. pi = generate_valid_vote_matrix_proof(voteMatrix);
  166. return voteMatrix;
  167. }
  168. /* Call this in order to get the current encrypted tally of a given user
  169. * (who is identified by their short term public key).
  170. * In practice, this is intended for clients, so that the servers vouch
  171. * for their ciphertexts being valid as part of their reputation proofs. */
  172. EGCiphertext PrsonaServer::get_current_user_encrypted_tally(
  173. Proof& pi,
  174. const Twistpoint& shortTermPublicKey) const
  175. {
  176. EGCiphertext retval;
  177. size_t tallyOwner = binary_search(shortTermPublicKey);
  178. retval = currentUserEncryptedTallies[tallyOwner];
  179. pi = generate_valid_user_tally_proof(retval);
  180. return retval;
  181. }
  182. CurveBipoint PrsonaServer::get_current_server_encrypted_tally(
  183. Proof& pi,
  184. const Twistpoint& shortTermPublicKey) const
  185. {
  186. CurveBipoint retval;
  187. size_t tallyOwner = binary_search(shortTermPublicKey);
  188. retval = previousVoteTallies[tallyOwner];
  189. pi = generate_valid_server_tally_proof(retval);
  190. return retval;
  191. }
  192. std::vector<Twistpoint> PrsonaServer::get_current_pseudonyms(
  193. Proof& pi) const
  194. {
  195. pi = generate_valid_pseudonyms_proof(currentPseudonyms);
  196. return currentPseudonyms;
  197. }
  198. std::vector<Twistpoint> PrsonaServer::get_current_pseudonyms() const
  199. {
  200. return currentPseudonyms;
  201. }
  202. /*
  203. * PROOF COMMITMENT GETTERS
  204. */
  205. Proof PrsonaServer::get_vote_row_commitment(
  206. const Twistpoint& request) const
  207. {
  208. size_t requestID = binary_search(request);
  209. return generate_valid_vote_row_proof(voteMatrix[requestID]);
  210. }
  211. Proof PrsonaServer::get_vote_matrix_commitment() const
  212. {
  213. return generate_valid_vote_matrix_proof(voteMatrix);
  214. }
  215. Proof PrsonaServer::get_user_tally_commitment(
  216. const Twistpoint& request) const
  217. {
  218. size_t requestID = binary_search(request);
  219. return generate_valid_user_tally_proof(currentUserEncryptedTallies[requestID]);
  220. }
  221. Proof PrsonaServer::get_server_tally_commitment(
  222. const Twistpoint& request) const
  223. {
  224. size_t requestID = binary_search(request);
  225. return generate_valid_server_tally_proof(previousVoteTallies[requestID]);
  226. }
  227. Proof PrsonaServer::get_pseudonyms_commitment() const
  228. {
  229. return generate_valid_pseudonyms_proof(currentPseudonyms);
  230. }
  231. void PrsonaServer::print_current_commitments() const
  232. {
  233. std::stringstream helper;
  234. std::cout << "Commitments:\n";
  235. std::cout << "User tallies:\n";
  236. for (size_t i = 0; i < currentUserEncryptedTallies.size(); i++)
  237. {
  238. helper.str("");
  239. helper << currentUserEncryptedTallies[i];
  240. std::cout << "Tally " << i + 1 << " of " << currentUserEncryptedTallies.size() << ": " << std::hex << oracle(helper.str()) << std::dec << "\n";
  241. }
  242. std::cout << "Server tallies:\n";
  243. for (size_t i = 0; i < previousVoteTallies.size(); i++)
  244. {
  245. helper.str("");
  246. helper << previousVoteTallies[i];
  247. std::cout << "Tally " << i + 1 << " of " << previousVoteTallies.size() << ": " << std::hex << oracle(helper.str()) << std::dec << "\n";
  248. }
  249. }
  250. /*
  251. * CLIENT INTERACTIONS
  252. */
  253. /* Add a new client (who is identified only by their short term public key)
  254. * One server will do this, then ask all other servers to import their
  255. * (proven) exported data. */
  256. void PrsonaServer::add_new_client(
  257. std::vector<Proof>& proofOfValidAddition,
  258. const Proof& proofOfValidKey,
  259. const Twistpoint& shortTermPublicKey)
  260. {
  261. if (!verify_ownership_proof(proofOfValidKey, currentFreshGenerator, shortTermPublicKey))
  262. {
  263. std::cerr << "Could not verify proof of valid key." << std::endl;
  264. return;
  265. }
  266. currentPseudonyms.push_back(shortTermPublicKey);
  267. // The first epoch's score for a new user will be low,
  268. // but will typically converge on an average score quickly
  269. Scalar tallySeed;
  270. CurveBipoint encryptedDefaultTally = bgnSystem.get_public_key().curveEncrypt(tallySeed, DEFAULT_TALLY);
  271. previousVoteTallies.push_back(encryptedDefaultTally);
  272. Scalar seedForUserTally;
  273. seedForUserTally.set_random();
  274. EGCiphertext newUserEncryptedTally;
  275. newUserEncryptedTally.mask = shortTermPublicKey * seedForUserTally;
  276. newUserEncryptedTally.encryptedMessage = currentFreshGenerator * seedForUserTally + elGamalBlindGenerator * DEFAULT_TALLY;
  277. currentUserEncryptedTallies.push_back(newUserEncryptedTally);
  278. // Users are defaulted to casting a neutral vote for others.
  279. TwistBipoint encryptedDefaultVote, encryptedSelfVote;
  280. Scalar currDefaultSeed, currSelfSeed;
  281. encryptedDefaultVote = bgnSystem.get_public_key().twistEncrypt(currDefaultSeed, DEFAULT_VOTE);
  282. encryptedSelfVote = bgnSystem.get_public_key().twistEncrypt(currSelfSeed, Scalar(MAX_ALLOWED_VOTE));
  283. std::vector<TwistBipoint> newRow;
  284. std::vector<Scalar> userVoteSeeds;
  285. std::vector<Scalar> otherVoteSeeds;
  286. for (size_t i = 0; i < voteMatrix.size(); i++)
  287. {
  288. Scalar addedSeed;
  289. encryptedDefaultVote = bgnSystem.get_public_key().rerandomize(addedSeed, encryptedDefaultVote);
  290. currDefaultSeed = currDefaultSeed + addedSeed;
  291. otherVoteSeeds.push_back(Scalar());
  292. otherVoteSeeds[i] = currDefaultSeed;
  293. voteMatrix[i].push_back(encryptedDefaultVote);
  294. encryptedDefaultVote = bgnSystem.get_public_key().rerandomize(addedSeed, encryptedDefaultVote);
  295. currDefaultSeed = currDefaultSeed + addedSeed;
  296. userVoteSeeds.push_back(Scalar());
  297. userVoteSeeds[i] = currDefaultSeed;
  298. newRow.push_back(encryptedDefaultVote);
  299. }
  300. // Because we are adding the new user to the end (and then sorting it),
  301. // this last element (bottom right corner) is always the self vote.
  302. userVoteSeeds.push_back(Scalar());
  303. userVoteSeeds[newRow.size()] = currSelfSeed;
  304. otherVoteSeeds.push_back(Scalar());
  305. otherVoteSeeds[newRow.size()] = currSelfSeed;
  306. newRow.push_back(encryptedSelfVote);
  307. voteMatrix.push_back(newRow);
  308. std::vector<size_t> sortOrder = order_data();
  309. std::vector<Scalar> newUserVoteSeeds;
  310. std::vector<Scalar> newOtherVoteSeeds;
  311. for (size_t i = 0; i < sortOrder.size(); i++)
  312. {
  313. newUserVoteSeeds.push_back(userVoteSeeds[sortOrder[i]]);
  314. newOtherVoteSeeds.push_back(otherVoteSeeds[sortOrder[i]]);
  315. }
  316. proofOfValidAddition = generate_proof_of_added_user(tallySeed, seedForUserTally, newUserVoteSeeds, newOtherVoteSeeds);
  317. }
  318. // Receive a new vote row from a user (identified by short term public key).
  319. bool PrsonaServer::receive_vote(
  320. const std::vector<Proof>& pi,
  321. const std::vector<TwistBipoint>& newVotes,
  322. const Twistpoint& shortTermPublicKey)
  323. {
  324. size_t voteSubmitter = binary_search(shortTermPublicKey);
  325. std::vector<TwistBipoint> oldVotes = voteMatrix[voteSubmitter];
  326. if (!verify_vote_proof(pi, oldVotes, newVotes, shortTermPublicKey))
  327. return false;
  328. voteMatrix[voteSubmitter] = newVotes;
  329. return true;
  330. }
  331. /*********************
  332. * PRIVATE FUNCTIONS *
  333. *********************/
  334. /*
  335. * CONSTRUCTOR HELPERS
  336. */
  337. const BGN& PrsonaServer::get_bgn_details() const
  338. {
  339. return bgnSystem;
  340. }
  341. bool PrsonaServer::initialize_fresh_generator(
  342. const std::vector<Proof>& pi,
  343. const Twistpoint& firstGenerator)
  344. {
  345. if (!verify_generator_proof(pi, firstGenerator, numServers))
  346. {
  347. std::cerr << "Could not verify generator proof, aborting." << std::endl;
  348. return false;
  349. }
  350. currentGeneratorProof = pi;
  351. currentFreshGenerator = firstGenerator;
  352. return true;
  353. }
  354. // To calculate the blind generator for ElGamal, start from the base generator,
  355. // then have every server call this function on it iteratively (in any order).
  356. Twistpoint PrsonaServer::add_rand_seed_to_generator(
  357. std::vector<Proof>& pi,
  358. const Twistpoint& currGenerator) const
  359. {
  360. Scalar lambda;
  361. lambda.set_random();
  362. pi.push_back(add_to_generator_proof(currGenerator, lambda));
  363. return currGenerator * lambda;
  364. }
  365. bool PrsonaServer::set_EG_blind_generator(
  366. const std::vector<Proof>& pi,
  367. const Twistpoint& currGenerator)
  368. {
  369. return PrsonaBase::set_EG_blind_generator(pi, currGenerator, numServers);
  370. }
  371. /*
  372. * SCORE TALLYING
  373. */
  374. void homomorphic_multiplication_r(
  375. void *a,
  376. void *b,
  377. const void *c,
  378. const void *d)
  379. {
  380. BGN *bgnSystem = (BGN *) a;
  381. Quadripoint *dst = (Quadripoint *) b;
  382. const CurveBipoint *x = (const CurveBipoint *) c;
  383. const TwistBipoint *y = (const TwistBipoint *) d;
  384. *dst = bgnSystem->homomorphic_multiplication_no_rerandomize(*x, *y);
  385. }
  386. void homomorphic_addition_r(
  387. void *a,
  388. void *b,
  389. size_t size)
  390. {
  391. BGN *bgnSystem = (BGN *) a;
  392. Quadripoint *arr = (Quadripoint *) b;
  393. switch (size)
  394. {
  395. case 0:
  396. case 1:
  397. return;
  398. case 2:
  399. arr[0] = bgnSystem->homomorphic_addition_no_rerandomize(arr[0], arr[1]);
  400. return;
  401. default:
  402. break;
  403. }
  404. size_t halfSize = size / 2;
  405. std::thread threadA(homomorphic_addition_r, bgnSystem, arr, halfSize);
  406. std::thread threadB(homomorphic_addition_r, bgnSystem, arr + halfSize, size - halfSize);
  407. threadA.join();
  408. threadB.join();
  409. arr[0] = bgnSystem->homomorphic_addition_no_rerandomize(arr[0], arr[halfSize]);
  410. }
  411. void tally_r(
  412. void *a,
  413. void *b,
  414. void *c,
  415. const void *d,
  416. const void *e,
  417. size_t i)
  418. {
  419. std::mutex *decryptMtx = (std::mutex *) a;
  420. BGN *bgnSystem = (BGN *) b;
  421. Scalar *dst = (Scalar *) c;
  422. const std::vector<CurveBipoint> *previousVoteTallies = (const std::vector<CurveBipoint> *) d;
  423. const std::vector<std::vector<TwistBipoint>> *voteMatrix = (const std::vector<std::vector<TwistBipoint>> *) e;
  424. Quadripoint *weightedVotes = new Quadripoint[previousVoteTallies->size()];
  425. std::vector<std::thread> parallelizedMults;
  426. // ZIP
  427. for (size_t j = 0; j < previousVoteTallies->size(); j++)
  428. parallelizedMults.push_back(std::thread(homomorphic_multiplication_r, bgnSystem, weightedVotes + j, &((*previousVoteTallies)[j]), &((*voteMatrix)[j][i])));
  429. for (size_t j = 0; j < parallelizedMults.size(); j++)
  430. parallelizedMults[j].join();
  431. // FOLDL
  432. homomorphic_addition_r(bgnSystem, weightedVotes, previousVoteTallies->size());
  433. // DECRYPT
  434. std::unique_lock<std::mutex> lck(*decryptMtx);
  435. *dst = bgnSystem->decrypt(weightedVotes[0]);
  436. delete [] weightedVotes;
  437. }
  438. /* Calculate scores homomorphically (as an individual server would normally)
  439. * and then decrypt them (which the servers would normally work together to do).
  440. *
  441. * Note that since these calculations are just for us, we don't need to do any
  442. * expensive rerandomizations of intermediate values. */
  443. std::vector<Scalar> PrsonaServer::tally_scores()
  444. {
  445. std::vector<Quadripoint> BGNEncryptedTallies;
  446. Scalar *decryptedTallies = new Scalar[voteMatrix.size()];
  447. std::vector<std::thread> parallelizedTallies;
  448. for (size_t i = 0; i < voteMatrix.size(); i++)
  449. parallelizedTallies.push_back(std::thread(tally_r, decryptMtx, &bgnSystem, decryptedTallies + i, &previousVoteTallies, &voteMatrix, i));
  450. for (size_t i = 0; i < parallelizedTallies.size(); i++)
  451. parallelizedTallies[i].join();
  452. std::vector<Scalar> retval(decryptedTallies, decryptedTallies + voteMatrix.size());
  453. delete [] decryptedTallies;
  454. return retval;
  455. }
  456. /* Calculate what the maximum possible score this round was (that is,
  457. * given the current user weights, what was the highest possible score?).
  458. *
  459. * As with individual scores, this also does the decryption that servers
  460. * would ordinarily work together to form. */
  461. Scalar PrsonaServer::get_max_possible_score()
  462. {
  463. // FOLDL
  464. CurveBipoint currEncryptedVal = previousVoteTallies[0];
  465. for (size_t i = 1; i < previousVoteTallies.size(); i++)
  466. currEncryptedVal = bgnSystem.homomorphic_addition_no_rerandomize(currEncryptedVal, previousVoteTallies[i]);
  467. // DECRYPT
  468. std::unique_lock<std::mutex> lck(*decryptMtx);
  469. Scalar retval = bgnSystem.decrypt(currEncryptedVal);
  470. return retval;
  471. }
  472. void PrsonaServer::receive_tallied_scores(
  473. const std::vector<EGCiphertext>& userTallyScores,
  474. const std::vector<CurveBipoint>& serverTallyScores)
  475. {
  476. currentUserEncryptedTallies = userTallyScores;
  477. previousVoteTallies = serverTallyScores;
  478. }
  479. void PrsonaServer::encrypt(
  480. CurveBipoint& element,
  481. const Scalar& value)
  482. {
  483. bgnSystem.encrypt(element, value);
  484. }
  485. /*
  486. * EPOCH ROUNDS
  487. */
  488. // The first round, going from A_0 to A_0.5
  489. void PrsonaServer::build_up_midway_pseudonyms(
  490. std::vector<std::vector<std::vector<Proof>>>& pi,
  491. std::vector<std::vector<std::vector<Twistpoint>>>& permutationCommits,
  492. std::vector<std::vector<std::vector<Twistpoint>>>& freshPseudonymCommits,
  493. std::vector<std::vector<std::vector<Twistpoint>>>& freshPseudonymSeedCommits,
  494. std::vector<std::vector<std::vector<CurveBipoint>>>& serverTallyCommits,
  495. std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>>& partwayVoteMatrixCommits,
  496. std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>>& finalVoteMatrixCommits,
  497. Twistpoint& nextGenerator)
  498. {
  499. nextSeed.set_random();
  500. std::vector<std::vector<Twistpoint>> currPermutationCommits;
  501. std::vector<std::vector<Twistpoint>> currFreshPseudonymCommits;
  502. std::vector<std::vector<Twistpoint>> currFreshPseudonymSeedCommits;
  503. std::vector<std::vector<CurveBipoint>> currServerTallyCommits;
  504. std::vector<std::vector<std::vector<TwistBipoint>>> currPartwayVoteMatrixCommits;
  505. std::vector<std::vector<std::vector<TwistBipoint>>> currFinalVoteMatrixCommits;
  506. std::vector<std::vector<Twistpoint>> currUserTallyMaskCommits;
  507. std::vector<std::vector<Twistpoint>> currUserTallyMessageCommits;
  508. std::vector<std::vector<Twistpoint>> currUserTallySeedCommits;
  509. pi.push_back(epoch_calculations(currPermutationCommits, currFreshPseudonymCommits, currFreshPseudonymSeedCommits, currServerTallyCommits, currPartwayVoteMatrixCommits, currFinalVoteMatrixCommits, currUserTallyMaskCommits, currUserTallyMessageCommits, currUserTallySeedCommits, nextSeed, nextGenerator, false));
  510. permutationCommits.push_back(currPermutationCommits);
  511. freshPseudonymCommits.push_back(currFreshPseudonymCommits);
  512. freshPseudonymSeedCommits.push_back(currFreshPseudonymSeedCommits);
  513. serverTallyCommits.push_back(currServerTallyCommits);
  514. partwayVoteMatrixCommits.push_back(currPartwayVoteMatrixCommits);
  515. finalVoteMatrixCommits.push_back(currFinalVoteMatrixCommits);
  516. pi[0][0].push_back(add_to_generator_proof(nextGenerator, nextSeed));
  517. nextGenerator = nextGenerator * nextSeed;
  518. }
  519. // In between these rounds, scores are tallied, decrypted,
  520. // and encrypted to fresh user pseudonyms (possible through weird math)
  521. // The second round, going from A_0.5 to A_1
  522. void PrsonaServer::break_down_midway_pseudonyms(
  523. const std::vector<Proof>& generatorProof,
  524. std::vector<std::vector<std::vector<Proof>>>& pi,
  525. std::vector<std::vector<std::vector<Twistpoint>>>& permutationCommits,
  526. std::vector<std::vector<std::vector<Twistpoint>>>& freshPseudonymCommits,
  527. std::vector<std::vector<std::vector<Twistpoint>>>& freshPseudonymSeedCommits,
  528. std::vector<std::vector<std::vector<CurveBipoint>>>& serverTallyCommits,
  529. std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>>& partwayVoteMatrixCommits,
  530. std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>>& finalVoteMatrixCommits,
  531. std::vector<std::vector<std::vector<Twistpoint>>>& userTallyMaskCommits,
  532. std::vector<std::vector<std::vector<Twistpoint>>>& userTallyMessageCommits,
  533. std::vector<std::vector<std::vector<Twistpoint>>>& userTallySeedCommits,
  534. const Twistpoint& nextGenerator)
  535. {
  536. if (!initialize_fresh_generator(generatorProof, nextGenerator))
  537. {
  538. std::cerr << "New fresh generator could not be verified." << std::endl;
  539. return;
  540. }
  541. Scalar inverseSeed = currentSeed.curveMultInverse();
  542. std::vector<std::vector<Twistpoint>> currPermutationCommits;
  543. std::vector<std::vector<Twistpoint>> currFreshPseudonymCommits;
  544. std::vector<std::vector<Twistpoint>> currFreshPseudonymSeedCommits;
  545. std::vector<std::vector<CurveBipoint>> currServerTallyCommits;
  546. std::vector<std::vector<std::vector<TwistBipoint>>> currPartwayVoteMatrixCommits;
  547. std::vector<std::vector<std::vector<TwistBipoint>>> currFinalVoteMatrixCommits;
  548. std::vector<std::vector<Twistpoint>> currUserTallyMaskCommits;
  549. std::vector<std::vector<Twistpoint>> currUserTallyMessageCommits;
  550. std::vector<std::vector<Twistpoint>> currUserTallySeedCommits;
  551. pi.push_back(epoch_calculations(currPermutationCommits, currFreshPseudonymCommits, currFreshPseudonymSeedCommits, currServerTallyCommits, currPartwayVoteMatrixCommits, currFinalVoteMatrixCommits, currUserTallyMaskCommits, currUserTallyMessageCommits, currUserTallySeedCommits, inverseSeed, nextGenerator, true));
  552. permutationCommits.push_back(currPermutationCommits);
  553. freshPseudonymCommits.push_back(currFreshPseudonymCommits);
  554. freshPseudonymSeedCommits.push_back(currFreshPseudonymSeedCommits);
  555. serverTallyCommits.push_back(currServerTallyCommits);
  556. partwayVoteMatrixCommits.push_back(currPartwayVoteMatrixCommits);
  557. finalVoteMatrixCommits.push_back(currFinalVoteMatrixCommits);
  558. userTallyMaskCommits.push_back(currUserTallyMaskCommits);
  559. userTallyMessageCommits.push_back(currUserTallyMessageCommits);
  560. userTallySeedCommits.push_back(currUserTallySeedCommits);
  561. currentSeed = nextSeed;
  562. }
  563. /*
  564. * EPOCH HELPERS
  565. */
  566. void generate_permutation_commitment_r(
  567. const void *a,
  568. void *b,
  569. const void *c,
  570. void *d)
  571. {
  572. const PrsonaServer *server = (const PrsonaServer *) a;
  573. std::vector<std::vector<Twistpoint>> *permutationCommits = (std::vector<std::vector<Twistpoint>> *) b;
  574. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  575. std::vector<std::vector<Scalar>> *permutationSeeds = (std::vector<std::vector<Scalar>> *) d;
  576. *permutationCommits = server->generate_commitment_matrix(*permutations, *permutationSeeds);
  577. }
  578. void generate_pseudonym_commitment_r(
  579. const void *a,
  580. void *b,
  581. const void *c,
  582. const void *d,
  583. void *e,
  584. void *f)
  585. {
  586. const PrsonaServer *server = (const PrsonaServer *) a;
  587. std::vector<std::vector<Twistpoint>> *freshPseudonymCommits = (std::vector<std::vector<Twistpoint>> *) b;
  588. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  589. const Scalar *power = (const Scalar *) d;
  590. std::vector<std::vector<Scalar>> *freshPseudonymSeeds = (std::vector<std::vector<Scalar>> *) e;
  591. std::vector<std::vector<Twistpoint>> *freshPseudonymSeedCommits = (std::vector<std::vector<Twistpoint>> *) f;
  592. freshPseudonymSeedCommits->clear();
  593. *freshPseudonymCommits = server->generate_pseudonym_matrix(*permutations, *power, *freshPseudonymSeeds, *freshPseudonymSeedCommits);
  594. }
  595. void generate_server_tally_commitment_r(
  596. const void *a,
  597. void *b,
  598. const void *c,
  599. void *d)
  600. {
  601. const PrsonaServer *server = (const PrsonaServer *) a;
  602. std::vector<std::vector<CurveBipoint>> *serverTallyCommits = (std::vector<std::vector<CurveBipoint>> *) b;
  603. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  604. std::vector<std::vector<Scalar>> *serverTallySeeds = (std::vector<std::vector<Scalar>> *) d;
  605. *serverTallyCommits = server->generate_server_tally_matrix(*permutations, *serverTallySeeds);
  606. }
  607. void generate_matrix_commitment_r(
  608. const void *a,
  609. void *b,
  610. void *c,
  611. void *d,
  612. const void *e,
  613. const void *f,
  614. void *g,
  615. void *h)
  616. {
  617. const PrsonaServer *server = (const PrsonaServer *) a;
  618. std::vector<std::vector<std::vector<TwistBipoint>>> *partwayVoteMatrixCommits = (std::vector<std::vector<std::vector<TwistBipoint>>> *) b;
  619. std::vector<std::vector<TwistBipoint>> *partialVoteMatrix = (std::vector<std::vector<TwistBipoint>> *) c;
  620. std::vector<std::vector<std::vector<TwistBipoint>>> *finalVoteMatrixCommits = (std::vector<std::vector<std::vector<TwistBipoint>>> *) d;
  621. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) e;
  622. const std::vector<std::vector<TwistBipoint>> *voteMatrix = (const std::vector<std::vector<TwistBipoint>> *) f;
  623. std::vector<std::vector<std::vector<Scalar>>> *partwayVoteMatrixSeeds = (std::vector<std::vector<std::vector<Scalar>>> *) g;
  624. std::vector<std::vector<std::vector<Scalar>>> *finalVoteMatrixSeeds = (std::vector<std::vector<std::vector<Scalar>>> *) h;
  625. *partwayVoteMatrixCommits = server->generate_vote_tensor(*permutations, *voteMatrix, *partwayVoteMatrixSeeds, false);
  626. *partialVoteMatrix = server->calculate_next_vote_matrix(*partwayVoteMatrixCommits);
  627. *finalVoteMatrixCommits = server->generate_vote_tensor(*permutations, *partialVoteMatrix, *finalVoteMatrixSeeds, true);
  628. }
  629. void generate_user_tally_commitment_r(
  630. const void *a,
  631. const void *b,
  632. const void *c,
  633. const void *d,
  634. const void *e,
  635. void *f,
  636. void *g,
  637. void *h,
  638. void *i,
  639. void *j,
  640. void *k)
  641. {
  642. const PrsonaServer *server = (const PrsonaServer *) a;
  643. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) b;
  644. const Scalar *power = (const Scalar *) c;
  645. const Twistpoint *nextGenerator = (const Twistpoint *) d;
  646. const std::vector<Twistpoint> *currentPseudonyms = (const std::vector<Twistpoint> *) e;
  647. std::vector<Twistpoint> *userTallyMasks = (std::vector<Twistpoint> *) f;
  648. std::vector<std::vector<Twistpoint>> *userTallyMaskCommits = (std::vector<std::vector<Twistpoint>> *) g;
  649. std::vector<Twistpoint> *userTallyMessages = (std::vector<Twistpoint> *) h;
  650. std::vector<std::vector<Twistpoint>> *userTallyMessageCommits = (std::vector<std::vector<Twistpoint>> *) i;
  651. std::vector<std::vector<Scalar>> *userTallySeeds = (std::vector<std::vector<Scalar>> *) j;
  652. std::vector<std::vector<Twistpoint>> *userTallySeedCommits = (std::vector<std::vector<Twistpoint>> *) k;
  653. userTallyMaskCommits->clear();
  654. userTallyMessageCommits->clear();
  655. userTallySeedCommits->clear();
  656. server->generate_user_tally_matrix(*permutations, *power, *nextGenerator, *currentPseudonyms, *userTallyMasks, *userTallyMaskCommits, *userTallyMessages, *userTallyMessageCommits, *userTallySeeds, *userTallySeedCommits);
  657. }
  658. void generate_permutation_proof_r(
  659. const void *a,
  660. void *b,
  661. const void *c,
  662. const void *d,
  663. const void *e)
  664. {
  665. const PrsonaServer *server = (const PrsonaServer *) a;
  666. std::vector<Proof> *dst = (std::vector<Proof> *) b;
  667. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  668. const std::vector<std::vector<Scalar>> *permutationSeeds = (const std::vector<std::vector<Scalar>> *) d;
  669. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) e;
  670. *dst = server->generate_valid_permutation_proof(*permutations, *permutationSeeds, *permutationCommits);
  671. }
  672. void generate_pseudonym_proof_r(
  673. const void *a,
  674. void *b,
  675. const void *c,
  676. const void *d,
  677. const void *e,
  678. const void *f,
  679. const void *g,
  680. const void *h,
  681. const void *i,
  682. const void *j)
  683. {
  684. const PrsonaServer *server = (const PrsonaServer *) a;
  685. std::vector<Proof> *dst = (std::vector<Proof> *) b;
  686. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  687. const Scalar *power = (const Scalar *) d;
  688. const std::vector<std::vector<Scalar>> *permutationSeeds = (const std::vector<std::vector<Scalar>> *) e;
  689. const std::vector<std::vector<Scalar>> *freshPseudonymSeeds = (const std::vector<std::vector<Scalar>> *) f;
  690. const std::vector<Twistpoint> *currentPseudonyms = (const std::vector<Twistpoint> *) g;
  691. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) h;
  692. const std::vector<std::vector<Twistpoint>> *freshPseudonymCommits = (const std::vector<std::vector<Twistpoint>> *) i;
  693. const std::vector<std::vector<Twistpoint>> *freshPseudonymSeedCommits = (const std::vector<std::vector<Twistpoint>> *) j;
  694. *dst = server->generate_proof_of_reordering_plus_power(*permutations, *power, *permutationSeeds, *freshPseudonymSeeds, *currentPseudonyms, *permutationCommits, *freshPseudonymCommits, *freshPseudonymSeedCommits);
  695. }
  696. void generate_server_tally_proof_r(
  697. const void *a,
  698. void *b,
  699. const void *c,
  700. const void *d,
  701. const void *e,
  702. const void *f,
  703. const void *g,
  704. const void *h,
  705. const void *i,
  706. const void *j)
  707. {
  708. const PrsonaServer *server = (const PrsonaServer *) a;
  709. std::vector<Proof> *dst = (std::vector<Proof> *) b;
  710. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  711. const std::vector<std::vector<Scalar>> *permutationSeeds = (const std::vector<std::vector<Scalar>> *) d;
  712. const std::vector<std::vector<Scalar>> *serverTallySeeds = (const std::vector<std::vector<Scalar>> *) e;
  713. const std::vector<CurveBipoint> *previousVoteTallies = (const std::vector<CurveBipoint> *) f;
  714. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) g;
  715. const std::vector<std::vector<CurveBipoint>> *serverTallyCommits = (const std::vector<std::vector<CurveBipoint>> *) h;
  716. const CurveBipoint *curveG = (const CurveBipoint *) i;
  717. const CurveBipoint *curveH = (const CurveBipoint *) j;
  718. *dst = server->generate_proof_of_reordering<CurveBipoint>(*permutations, *permutationSeeds, *serverTallySeeds, *previousVoteTallies, *permutationCommits, *serverTallyCommits, *curveG, *curveH);
  719. }
  720. void generate_first_half_matrix_proof_r(
  721. const void *a,
  722. void *b,
  723. const void *c,
  724. const void *d,
  725. const void *e,
  726. const void *f,
  727. const void *g,
  728. const void *h)
  729. {
  730. const PrsonaServer *server = (const PrsonaServer *) a;
  731. std::vector<std::vector<Proof>> *dst = (std::vector<std::vector<Proof>> *) b;
  732. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  733. const std::vector<std::vector<Scalar>> *permutationSeeds = (const std::vector<std::vector<Scalar>> *) d;
  734. const std::vector<std::vector<std::vector<Scalar>>> *partwayVoteMatrixSeeds = (const std::vector<std::vector<std::vector<Scalar>>> *) e;
  735. const std::vector<std::vector<TwistBipoint>> *voteMatrix = (const std::vector<std::vector<TwistBipoint>> *) f;
  736. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) g;
  737. const std::vector<std::vector<std::vector<TwistBipoint>>> *partwayVoteMatrixCommits = (const std::vector<std::vector<std::vector<TwistBipoint>>> *) h;
  738. server->generate_vote_tensor_proofs(*dst, *permutations, *permutationSeeds, *partwayVoteMatrixSeeds, *voteMatrix, *permutationCommits, *partwayVoteMatrixCommits, false);
  739. }
  740. void generate_second_half_matrix_proof_r(
  741. const void *a,
  742. void *b,
  743. const void *c,
  744. const void *d,
  745. const void *e,
  746. const void *f,
  747. const void *g,
  748. const void *h)
  749. {
  750. const PrsonaServer *server = (const PrsonaServer *) a;
  751. std::vector<std::vector<Proof>> *dst = (std::vector<std::vector<Proof>> *) b;
  752. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  753. const std::vector<std::vector<Scalar>> *permutationSeeds = (const std::vector<std::vector<Scalar>> *) d;
  754. const std::vector<std::vector<std::vector<Scalar>>> *finalVoteMatrixSeeds = (const std::vector<std::vector<std::vector<Scalar>>> *) e;
  755. const std::vector<std::vector<TwistBipoint>> *partialVoteMatrix = (const std::vector<std::vector<TwistBipoint>> *) f;
  756. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) g;
  757. const std::vector<std::vector<std::vector<TwistBipoint>>> *finalVoteMatrixCommits = (const std::vector<std::vector<std::vector<TwistBipoint>>> *) h;
  758. server->generate_vote_tensor_proofs(*dst, *permutations, *permutationSeeds, *finalVoteMatrixSeeds, *partialVoteMatrix, *permutationCommits, *finalVoteMatrixCommits, true);
  759. }
  760. void generate_user_tally_proof_r(
  761. const void *a,
  762. void *b,
  763. const void *c,
  764. const void *d,
  765. const void *e,
  766. const void *f,
  767. const void *g,
  768. const void *h,
  769. const void *i,
  770. const void *j,
  771. const void *k,
  772. const void *l,
  773. const void *m,
  774. const void *n)
  775. {
  776. const PrsonaServer *server = (const PrsonaServer *) a;
  777. std::vector<Proof> *dst = (std::vector<Proof> *) b;
  778. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  779. const Scalar *power = (const Scalar *) d;
  780. const Twistpoint *nextGenerator = (const Twistpoint *) e;
  781. const std::vector<std::vector<Scalar>> *permutationSeeds = (const std::vector<std::vector<Scalar>> *) f;
  782. const std::vector<std::vector<Scalar>> *userTallySeeds = (const std::vector<std::vector<Scalar>> *) g;
  783. const std::vector<Twistpoint> *currentPseudonyms = (const std::vector<Twistpoint> *) h;
  784. const std::vector<Twistpoint> *userTallyMasks = (const std::vector<Twistpoint> *) i;
  785. const std::vector<Twistpoint> *userTallyMessages = (const std::vector<Twistpoint> *) j;
  786. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) k;
  787. const std::vector<std::vector<Twistpoint>> *userTallyMaskCommits = (const std::vector<std::vector<Twistpoint>> *) l;
  788. const std::vector<std::vector<Twistpoint>> *userTallyMessageCommits = (const std::vector<std::vector<Twistpoint>> *) m;
  789. const std::vector<std::vector<Twistpoint>> *userTallySeedCommits = (const std::vector<std::vector<Twistpoint>> *) n;
  790. *dst = server->generate_user_tally_proofs(*permutations, *power, *nextGenerator, *permutationSeeds, *userTallySeeds, *currentPseudonyms, *userTallyMasks, *userTallyMessages, *permutationCommits, *userTallyMaskCommits, *userTallyMessageCommits, *userTallySeedCommits);
  791. }
  792. std::vector<std::vector<Proof>> PrsonaServer::epoch_calculations(
  793. std::vector<std::vector<Twistpoint>>& permutationCommits,
  794. std::vector<std::vector<Twistpoint>>& freshPseudonymCommits,
  795. std::vector<std::vector<Twistpoint>>& freshPseudonymSeedCommits,
  796. std::vector<std::vector<CurveBipoint>>& serverTallyCommits,
  797. std::vector<std::vector<std::vector<TwistBipoint>>>& partwayVoteMatrixCommits,
  798. std::vector<std::vector<std::vector<TwistBipoint>>>& finalVoteMatrixCommits,
  799. std::vector<std::vector<Twistpoint>>& userTallyMaskCommits,
  800. std::vector<std::vector<Twistpoint>>& userTallyMessageCommits,
  801. std::vector<std::vector<Twistpoint>>& userTallySeedCommits,
  802. const Scalar& power,
  803. const Twistpoint& nextGenerator,
  804. bool doUserTallies)
  805. {
  806. std::vector<std::vector<Scalar>> permutations = generate_permutation_matrix(power);
  807. std::vector<std::vector<Proof>> retval, firstHalfTensorProof, secondHalfTensorProof;
  808. CurveBipoint curveG = bgnSystem.get_public_key().get_bipoint_curvegen();
  809. CurveBipoint curveH = bgnSystem.get_public_key().get_bipoint_curve_subgroup_gen();
  810. std::vector<Proof> *individualProofs = new std::vector<Proof>[doUserTallies ? 4 : 3];
  811. std::vector<std::vector<Scalar>> permutationSeeds;
  812. std::thread permutationCommitThread(generate_permutation_commitment_r, this, &permutationCommits, &permutations, &permutationSeeds);
  813. std::vector<std::vector<Scalar>> freshPseudonymSeeds;
  814. std::thread pseudonymCommitThread(generate_pseudonym_commitment_r, this, &freshPseudonymCommits, &permutations, &power, &freshPseudonymSeeds, &freshPseudonymSeedCommits);
  815. std::vector<std::vector<Scalar>> serverTallySeeds;
  816. std::thread serverTallyCommitThread(generate_server_tally_commitment_r, this, &serverTallyCommits, &permutations, &serverTallySeeds);
  817. std::vector<std::vector<std::vector<Scalar>>> partwayVoteMatrixSeeds;
  818. std::vector<std::vector<std::vector<Scalar>>> finalVoteMatrixSeeds;
  819. std::vector<std::vector<TwistBipoint>> partialVoteMatrix;
  820. std::thread voteMatrixCommitThread(generate_matrix_commitment_r, this, &partwayVoteMatrixCommits, &partialVoteMatrix, &finalVoteMatrixCommits, &permutations, &voteMatrix, &partwayVoteMatrixSeeds, &finalVoteMatrixSeeds);
  821. std::vector<Twistpoint> userTallyMasks;
  822. std::vector<Twistpoint> userTallyMessages;
  823. std::vector<std::vector<Scalar>> userTallySeeds;
  824. std::thread *userTallyCommitThread = NULL;
  825. if (doUserTallies)
  826. userTallyCommitThread = new std::thread(generate_user_tally_commitment_r, this, &permutations, &power, &nextGenerator, &currentPseudonyms, &userTallyMasks, &userTallyMaskCommits, &userTallyMessages, &userTallyMessageCommits, &userTallySeeds, &userTallySeedCommits);
  827. permutationCommitThread.join();
  828. std::thread permutationProofThread(generate_permutation_proof_r, this, individualProofs, &permutations, &permutationSeeds, &permutationCommits);
  829. pseudonymCommitThread.join();
  830. std::thread pseudonymProofThread(generate_pseudonym_proof_r, this, individualProofs + 1, &permutations, &power, &permutationSeeds, &freshPseudonymSeeds, &currentPseudonyms, &permutationCommits, &freshPseudonymCommits, &freshPseudonymSeedCommits);
  831. serverTallyCommitThread.join();
  832. std::thread serverTallyProofThread(generate_server_tally_proof_r, this, individualProofs + 2, &permutations, &permutationSeeds, &serverTallySeeds, &previousVoteTallies, &permutationCommits, &serverTallyCommits, &curveG, &curveH);
  833. voteMatrixCommitThread.join();
  834. std::thread firstHalfProofThread(generate_first_half_matrix_proof_r, this, &firstHalfTensorProof, &permutations, &permutationSeeds, &partwayVoteMatrixSeeds, &voteMatrix, &permutationCommits, &partwayVoteMatrixCommits);
  835. std::thread secondHalfProofThread(generate_second_half_matrix_proof_r, this, &secondHalfTensorProof, &permutations, &permutationSeeds, &finalVoteMatrixSeeds, &partialVoteMatrix, &permutationCommits, &finalVoteMatrixCommits);
  836. if (userTallyCommitThread)
  837. {
  838. userTallyCommitThread->join();
  839. delete userTallyCommitThread;
  840. userTallyCommitThread = NULL;
  841. }
  842. std::thread *userTallyProofThread = NULL;
  843. if (doUserTallies)
  844. userTallyProofThread = new std::thread(generate_user_tally_proof_r, this, individualProofs + 3, &permutations, &power, &nextGenerator, &permutationSeeds, &userTallySeeds, &currentPseudonyms, &userTallyMasks, &userTallyMessages, &permutationCommits, &userTallyMaskCommits, &userTallyMessageCommits, &userTallySeedCommits);
  845. permutationProofThread.join();
  846. pseudonymProofThread.join();
  847. serverTallyProofThread.join();
  848. firstHalfProofThread.join();
  849. secondHalfProofThread.join();
  850. if (userTallyProofThread)
  851. {
  852. userTallyProofThread->join();
  853. delete userTallyProofThread;
  854. userTallyProofThread = NULL;
  855. }
  856. retval.push_back(individualProofs[0]);
  857. retval.push_back(individualProofs[1]);
  858. retval.push_back(individualProofs[2]);
  859. for (size_t i = 0; i < firstHalfTensorProof.size(); i++)
  860. retval.push_back(firstHalfTensorProof[i]);
  861. for (size_t i = 0; i < secondHalfTensorProof.size(); i++)
  862. retval.push_back(secondHalfTensorProof[i]);
  863. if (doUserTallies)
  864. retval.push_back(individualProofs[3]);
  865. delete [] individualProofs;
  866. // Replace internal values
  867. update_data(freshPseudonymCommits, serverTallyCommits, finalVoteMatrixCommits, userTallyMaskCommits, userTallyMessageCommits);
  868. return retval;
  869. }
  870. void verify_permutation_r(
  871. const void *a,
  872. void *b,
  873. const void *c,
  874. const void *d)
  875. {
  876. const PrsonaServer *server = (const PrsonaServer *) a;
  877. bool *dst = (bool *) b;
  878. const std::vector<Proof> *pi = (const std::vector<Proof> *) c;
  879. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) d;
  880. *dst = server->verify_valid_permutation_proof(*pi, *permutationCommits);
  881. }
  882. void verify_pseudonym_r(
  883. const void *a,
  884. void *b,
  885. const void *c,
  886. const void *d,
  887. const void *e,
  888. const void *f,
  889. const void *g)
  890. {
  891. const PrsonaServer *server = (const PrsonaServer *) a;
  892. bool *dst = (bool *) b;
  893. const std::vector<Proof> *pi = (const std::vector<Proof> *) c;
  894. const std::vector<Twistpoint> *currentPseudonyms = (const std::vector<Twistpoint> *) d;
  895. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) e;
  896. const std::vector<std::vector<Twistpoint>> *freshPseudonymCommits = (const std::vector<std::vector<Twistpoint>> *) f;
  897. const std::vector<std::vector<Twistpoint>> *freshPseudonymSeedCommits = (const std::vector<std::vector<Twistpoint>> *) g;
  898. *dst = server->verify_proof_of_reordering_plus_power(*pi, *currentPseudonyms, *permutationCommits, *freshPseudonymCommits, *freshPseudonymSeedCommits);
  899. }
  900. void verify_server_tally_r(
  901. const void *a,
  902. void *b,
  903. const void *c,
  904. const void *d,
  905. const void *e,
  906. const void *f,
  907. const void *g,
  908. const void *h)
  909. {
  910. const PrsonaServer *server = (const PrsonaServer *) a;
  911. bool *dst = (bool *) b;
  912. const std::vector<Proof> *pi = (const std::vector<Proof> *) c;
  913. const std::vector<CurveBipoint> *previousVoteTallies = (const std::vector<CurveBipoint> *) d;
  914. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) e;
  915. const std::vector<std::vector<CurveBipoint>> *serverTallyCommits = (const std::vector<std::vector<CurveBipoint>> *) f;
  916. const CurveBipoint *curveG = (const CurveBipoint *) g;
  917. const CurveBipoint *curveH = (const CurveBipoint *) h;
  918. *dst = server->verify_proof_of_reordering<CurveBipoint>(*pi, *previousVoteTallies, *permutationCommits, *serverTallyCommits, *curveG, *curveH);
  919. }
  920. void verify_first_half_matrix_r(
  921. const void *a,
  922. void *b,
  923. const void *c,
  924. const void *d,
  925. const void *e,
  926. const void *f,
  927. const void *g)
  928. {
  929. const PrsonaServer *server = (const PrsonaServer *) a;
  930. bool *dst = (bool *) b;
  931. const std::vector<std::vector<Proof>> *pi = (const std::vector<std::vector<Proof>> *) c;
  932. const size_t *currOffset = (const size_t *) d;
  933. const std::vector<std::vector<TwistBipoint>> *voteMatrix = (const std::vector<std::vector<TwistBipoint>> *) e;
  934. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) f;
  935. const std::vector<std::vector<std::vector<TwistBipoint>>> *partwayVoteMatrixCommits = (const std::vector<std::vector<std::vector<TwistBipoint>>> *) g;
  936. *dst = server->verify_vote_tensor_proofs(*pi, *currOffset, *voteMatrix, *permutationCommits, *partwayVoteMatrixCommits, false);
  937. }
  938. void verify_second_half_matrix_r(
  939. const void *a,
  940. void *b,
  941. const void *c,
  942. const void *d,
  943. const void *e,
  944. const void *f,
  945. const void *g)
  946. {
  947. const PrsonaServer *server = (const PrsonaServer *) a;
  948. bool *dst = (bool *) b;
  949. const std::vector<std::vector<Proof>> *pi = (const std::vector<std::vector<Proof>> *) c;
  950. const size_t *currOffset = (const size_t *) d;
  951. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) e;
  952. const std::vector<std::vector<std::vector<TwistBipoint>>> *partwayVoteMatrixCommits = (const std::vector<std::vector<std::vector<TwistBipoint>>> *) f;
  953. const std::vector<std::vector<std::vector<TwistBipoint>>> *finalVoteMatrixCommits = (const std::vector<std::vector<std::vector<TwistBipoint>>> *) g;
  954. std::vector<std::vector<TwistBipoint>> partialVoteMatrix = server->calculate_next_vote_matrix(*partwayVoteMatrixCommits);
  955. *dst = server->verify_vote_tensor_proofs(*pi, *currOffset, partialVoteMatrix, *permutationCommits, *finalVoteMatrixCommits, true);
  956. }
  957. void verify_user_tally_r(
  958. const void *a,
  959. void *b,
  960. const void *c,
  961. const void *d,
  962. const void *e,
  963. const void *f,
  964. const void *g,
  965. const void *h,
  966. const void *i,
  967. const void *j)
  968. {
  969. const PrsonaServer *server = (const PrsonaServer *) a;
  970. bool *dst = (bool *) b;
  971. const std::vector<Proof> *pi = (const std::vector<Proof> *) c;
  972. const Twistpoint *nextGenerator = (const Twistpoint *) d;
  973. const std::vector<Twistpoint> *currentPseudonyms = (const std::vector<Twistpoint> *) e;
  974. const std::vector<EGCiphertext> *currentUserEncryptedTallies = (const std::vector<EGCiphertext> *) f;
  975. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) g;
  976. const std::vector<std::vector<Twistpoint>> *userTallyMaskCommits = (const std::vector<std::vector<Twistpoint>> *) h;
  977. const std::vector<std::vector<Twistpoint>> *userTallyMessageCommits = (const std::vector<std::vector<Twistpoint>> *) i;
  978. const std::vector<std::vector<Twistpoint>> *userTallySeedCommits = (const std::vector<std::vector<Twistpoint>> *) j;
  979. std::vector<Twistpoint> userTallyMasks;
  980. std::vector<Twistpoint> userTallyMessages;
  981. for (size_t i = 0; i < currentUserEncryptedTallies->size(); i++)
  982. {
  983. userTallyMasks.push_back((*currentUserEncryptedTallies)[i].mask);
  984. userTallyMessages.push_back((*currentUserEncryptedTallies)[i].encryptedMessage);
  985. }
  986. *dst = server->verify_user_tally_proofs(*pi, *nextGenerator, *currentPseudonyms, userTallyMasks, userTallyMessages, *permutationCommits, *userTallyMaskCommits, *userTallyMessageCommits, *userTallySeedCommits);
  987. }
  988. bool PrsonaServer::accept_epoch_updates(
  989. const std::vector<std::vector<Proof>>& pi,
  990. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  991. const std::vector<std::vector<Twistpoint>>& freshPseudonymCommits,
  992. const std::vector<std::vector<Twistpoint>>& freshPseudonymSeedCommits,
  993. const std::vector<std::vector<CurveBipoint>>& serverTallyCommits,
  994. const std::vector<std::vector<std::vector<TwistBipoint>>>& partwayVoteMatrixCommits,
  995. const std::vector<std::vector<std::vector<TwistBipoint>>>& finalVoteMatrixCommits,
  996. const std::vector<std::vector<Twistpoint>>& userTallyMaskCommits,
  997. const std::vector<std::vector<Twistpoint>>& userTallyMessageCommits,
  998. const std::vector<std::vector<Twistpoint>>& userTallySeedCommits,
  999. const Twistpoint& nextGenerator,
  1000. bool doUserTallies)
  1001. {
  1002. bool permutationVerification = false;
  1003. bool pseudonymVerification = false;
  1004. bool serverTallyVerification = false;
  1005. bool firstHalfVerification = false;
  1006. bool secondHalfVerification = false;
  1007. bool userTallyVerification = true;
  1008. if ((userTallyMaskCommits.empty() && doUserTallies) || (!userTallyMaskCommits.empty() && !doUserTallies))
  1009. {
  1010. std::cerr << "user tallies are not in expected state." << std::endl;
  1011. return false;
  1012. }
  1013. if (pi.empty())
  1014. return false;
  1015. size_t currOffset = 0;
  1016. std::thread permutationThread(verify_permutation_r, this, &permutationVerification, &(pi[currOffset]), &permutationCommits);
  1017. currOffset++;
  1018. std::thread pseudonymThread(verify_pseudonym_r, this, &pseudonymVerification, &(pi[currOffset]), &currentPseudonyms, &permutationCommits, &freshPseudonymCommits, &freshPseudonymSeedCommits);
  1019. currOffset++;
  1020. CurveBipoint curveG = bgnSystem.get_public_key().get_bipoint_curvegen();
  1021. CurveBipoint curveH = bgnSystem.get_public_key().get_bipoint_curve_subgroup_gen();
  1022. std::thread serverTallyThread(verify_server_tally_r, this, &serverTallyVerification, &(pi[currOffset]), &previousVoteTallies, &permutationCommits, &serverTallyCommits, &curveG, &curveH);
  1023. currOffset++;
  1024. size_t firstHalfOffset = currOffset;
  1025. std::thread firstHalfThread(verify_first_half_matrix_r, this, &firstHalfVerification, &pi, &firstHalfOffset, &voteMatrix, &permutationCommits, &partwayVoteMatrixCommits);
  1026. currOffset += voteMatrix.size();
  1027. size_t secondHalfOffset = currOffset;
  1028. std::thread secondHalfThread(verify_second_half_matrix_r, this, &secondHalfVerification, &pi, &secondHalfOffset, &permutationCommits, &partwayVoteMatrixCommits, &finalVoteMatrixCommits);
  1029. currOffset += voteMatrix.size();
  1030. std::thread *userTallyThread = NULL;
  1031. if (doUserTallies)
  1032. userTallyThread = new std::thread(verify_user_tally_r, this, &userTallyVerification, &(pi[currOffset]), &nextGenerator, &currentPseudonyms, &currentUserEncryptedTallies, &permutationCommits, &userTallyMaskCommits, &userTallyMessageCommits, &userTallySeedCommits);
  1033. currOffset++;
  1034. permutationThread.join();
  1035. pseudonymThread.join();
  1036. serverTallyThread.join();
  1037. firstHalfThread.join();
  1038. secondHalfThread.join();
  1039. if (userTallyThread)
  1040. {
  1041. userTallyThread->join();
  1042. delete userTallyThread;
  1043. userTallyThread = NULL;
  1044. }
  1045. if (!permutationVerification)
  1046. {
  1047. std::cerr << "Could not verify valid permutation matrix." << std::endl;
  1048. return false;
  1049. }
  1050. if (!pseudonymVerification)
  1051. {
  1052. std::cerr << "Could not verify valid pseudonym vector." << std::endl;
  1053. return false;
  1054. }
  1055. if (!serverTallyVerification)
  1056. {
  1057. std::cerr << "Could not verify valid server tally vector." << std::endl;
  1058. return false;
  1059. }
  1060. if (!userTallyVerification)
  1061. {
  1062. std::cerr << "Could not verify user tallies." << std::endl;
  1063. return false;
  1064. }
  1065. if (!firstHalfVerification)
  1066. {
  1067. std::cerr << "Could not verify first half vote matrix." << std::endl;
  1068. return false;
  1069. }
  1070. if (!secondHalfVerification)
  1071. {
  1072. std::cerr << "Could not verify second half vote matrix." << std::endl;
  1073. return false;
  1074. }
  1075. bool finalVerification = update_data(freshPseudonymCommits, serverTallyCommits, finalVoteMatrixCommits, userTallyMaskCommits, userTallyMessageCommits);
  1076. return finalVerification;
  1077. }
  1078. std::vector<std::vector<Scalar>> PrsonaServer::generate_permutation_matrix(
  1079. const Scalar& reorderSeed) const
  1080. {
  1081. std::vector<std::vector<Scalar>> retval;
  1082. for (size_t i = 0; i < currentPseudonyms.size(); i++)
  1083. {
  1084. std::vector<Scalar> currRow;
  1085. for (size_t j = 0; j < currentPseudonyms.size(); j++)
  1086. currRow.push_back(Scalar(0));
  1087. retval.push_back(currRow);
  1088. }
  1089. std::vector<Twistpoint> nextPseudonyms;
  1090. for (size_t i = 0; i < currentPseudonyms.size(); i++)
  1091. nextPseudonyms.push_back(currentPseudonyms[i] * reorderSeed);
  1092. std::vector<size_t> order = sort_data(nextPseudonyms);
  1093. for (size_t i = 0; i < order.size(); i++)
  1094. retval[order[i]][i] = Scalar(1);
  1095. return retval;
  1096. }
  1097. std::vector<std::vector<Twistpoint>> PrsonaServer::generate_commitment_matrix(
  1098. const std::vector<std::vector<Scalar>>& permutations,
  1099. std::vector<std::vector<Scalar>>& seeds) const
  1100. {
  1101. std::vector<std::vector<Twistpoint>> retval;
  1102. Twistpoint g = EL_GAMAL_GENERATOR;
  1103. Twistpoint h = elGamalBlindGenerator;
  1104. seeds.clear();
  1105. for (size_t i = 0; i < permutations.size(); i++)
  1106. {
  1107. std::vector<Scalar> currSeeds;
  1108. for (size_t j = 0; j < permutations[i].size(); j++)
  1109. currSeeds.push_back(Scalar(0));
  1110. seeds.push_back(currSeeds);
  1111. }
  1112. for (size_t i = 0; i < permutations.size(); i++)
  1113. {
  1114. std::vector<Twistpoint> currRow;
  1115. size_t last = permutations[i].size() - 1;
  1116. for (size_t j = 0; j < permutations[i].size(); j++)
  1117. {
  1118. Twistpoint element;
  1119. if (j != last)
  1120. {
  1121. seeds[i][j].set_random();
  1122. seeds[i][last] = seeds[i][last] - seeds[i][j];
  1123. }
  1124. element = g * permutations[i][j] + h * seeds[i][j];
  1125. currRow.push_back(element);
  1126. }
  1127. retval.push_back(currRow);
  1128. }
  1129. return retval;
  1130. }
  1131. std::vector<std::vector<Twistpoint>> PrsonaServer::generate_pseudonym_matrix(
  1132. const std::vector<std::vector<Scalar>>& permutations,
  1133. const Scalar& power,
  1134. std::vector<std::vector<Scalar>>& seeds,
  1135. std::vector<std::vector<Twistpoint>>& seedCommits) const
  1136. {
  1137. return generate_reordered_plus_power_matrix<Twistpoint>(permutations, power, currentPseudonyms, seeds, seedCommits, elGamalBlindGenerator);
  1138. }
  1139. std::vector<std::vector<CurveBipoint>> PrsonaServer::generate_server_tally_matrix(
  1140. const std::vector<std::vector<Scalar>>& permutations,
  1141. std::vector<std::vector<Scalar>>& seeds) const
  1142. {
  1143. return generate_reordered_matrix<CurveBipoint>(permutations, previousVoteTallies, seeds, bgnSystem.get_public_key().get_bipoint_curve_subgroup_gen(), false);
  1144. }
  1145. std::vector<std::vector<std::vector<TwistBipoint>>> PrsonaServer::generate_vote_tensor(
  1146. const std::vector<std::vector<Scalar>>& permutations,
  1147. const std::vector<std::vector<TwistBipoint>>& currVoteMatrix,
  1148. std::vector<std::vector<std::vector<Scalar>>>& seeds,
  1149. bool inverted) const
  1150. {
  1151. std::vector<std::vector<std::vector<TwistBipoint>>> retval;
  1152. for (size_t i = 0; i < currVoteMatrix.size(); i++)
  1153. {
  1154. std::vector<std::vector<Scalar>> currSeeds;
  1155. std::vector<TwistBipoint> inputRow;
  1156. if (inverted)
  1157. {
  1158. for (size_t j = 0; j < currVoteMatrix.size(); j++)
  1159. inputRow.push_back(currVoteMatrix[j][i]);
  1160. }
  1161. else
  1162. {
  1163. inputRow = currVoteMatrix[i];
  1164. }
  1165. retval.push_back(generate_reordered_matrix<TwistBipoint>(permutations, inputRow, currSeeds, bgnSystem.get_public_key().get_bipoint_twist_subgroup_gen(), false));
  1166. seeds.push_back(currSeeds);
  1167. }
  1168. return retval;
  1169. }
  1170. std::vector<std::vector<TwistBipoint>> PrsonaServer::calculate_next_vote_matrix(
  1171. const std::vector<std::vector<std::vector<TwistBipoint>>>& voteTensor) const
  1172. {
  1173. std::vector<std::vector<TwistBipoint>> retval;
  1174. for (size_t i = 0; i < voteTensor.size(); i++)
  1175. {
  1176. std::vector<TwistBipoint> currRow;
  1177. for (size_t j = 0; j < voteTensor[i].size(); j++)
  1178. {
  1179. TwistBipoint sum = voteTensor[i][j][0];
  1180. for (size_t k = 1; k < voteTensor[i][j].size(); k++)
  1181. sum = sum + voteTensor[i][j][k];
  1182. currRow.push_back(sum);
  1183. }
  1184. retval.push_back(currRow);
  1185. }
  1186. return retval;
  1187. }
  1188. void generate_tensor_r(
  1189. const void *a,
  1190. void *b,
  1191. const void *c,
  1192. const void *d,
  1193. const void *e,
  1194. const void *f,
  1195. const void *g,
  1196. const void *h,
  1197. const void *i,
  1198. const void *j)
  1199. {
  1200. const PrsonaServer *server = (const PrsonaServer *) a;
  1201. std::vector<Proof> *dst = (std::vector<Proof> *) b;
  1202. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  1203. const std::vector<std::vector<Scalar>> *permutationSeeds = (const std::vector<std::vector<Scalar>> *) d;
  1204. const std::vector<std::vector<Scalar>> *matrixSeeds = (const std::vector<std::vector<Scalar>> *) e;
  1205. const std::vector<TwistBipoint> *inputRow = (const std::vector<TwistBipoint> *) f;
  1206. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) g;
  1207. const std::vector<std::vector<TwistBipoint>> *matrixCommits = (const std::vector<std::vector<TwistBipoint>> *) h;
  1208. const TwistBipoint *twistG = (const TwistBipoint *) i;
  1209. const TwistBipoint *twistH = (const TwistBipoint *) j;
  1210. *dst = server->generate_proof_of_reordering<TwistBipoint>(*permutations, *permutationSeeds, *matrixSeeds, *inputRow, *permutationCommits, *matrixCommits, *twistG, *twistH);
  1211. }
  1212. void PrsonaServer::generate_vote_tensor_proofs(
  1213. std::vector<std::vector<Proof>>& pi,
  1214. const std::vector<std::vector<Scalar>>& permutations,
  1215. const std::vector<std::vector<Scalar>>& permutationSeeds,
  1216. const std::vector<std::vector<std::vector<Scalar>>>& matrixSeeds,
  1217. const std::vector<std::vector<TwistBipoint>>& currMatrix,
  1218. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1219. const std::vector<std::vector<std::vector<TwistBipoint>>>& matrixCommits,
  1220. bool inverted) const
  1221. {
  1222. TwistBipoint twistG = bgnSystem.get_public_key().get_bipoint_twistgen();
  1223. TwistBipoint twistH = bgnSystem.get_public_key().get_bipoint_twist_subgroup_gen();
  1224. std::vector<Proof> *intermediaryProofs = new std::vector<Proof>[currMatrix.size()];
  1225. std::vector<TwistBipoint> *inputRows = new std::vector<TwistBipoint>[currMatrix.size()];
  1226. std::vector<std::thread> individualTensorProofThreads;
  1227. for (size_t i = 0; i < currMatrix.size(); i++)
  1228. {
  1229. std::vector<TwistBipoint> currRow;
  1230. if (inverted)
  1231. {
  1232. for (size_t j = 0; j < currMatrix.size(); j++)
  1233. currRow.push_back(currMatrix[j][i]);
  1234. }
  1235. else
  1236. {
  1237. currRow = currMatrix[i];
  1238. }
  1239. inputRows[i] = currRow;
  1240. individualTensorProofThreads.push_back(std::thread(generate_tensor_r, this, intermediaryProofs + i, &permutations, &permutationSeeds, &(matrixSeeds[i]), inputRows + i, &permutationCommits, &(matrixCommits[i]), &twistG, &twistH));
  1241. }
  1242. for (size_t i = 0; i < individualTensorProofThreads.size(); i++)
  1243. {
  1244. individualTensorProofThreads[i].join();
  1245. pi.push_back(intermediaryProofs[i]);
  1246. }
  1247. delete [] inputRows;
  1248. delete [] intermediaryProofs;
  1249. }
  1250. void verify_tensor_r(
  1251. const void *a,
  1252. void *b,
  1253. const void *c,
  1254. const void *d,
  1255. const void *e,
  1256. const void *f,
  1257. const void *g,
  1258. const void *h)
  1259. {
  1260. const PrsonaServer *server = (const PrsonaServer *) a;
  1261. bool *dst = (bool *) b;
  1262. const std::vector<Proof> *pi = (const std::vector<Proof> *) c;
  1263. const std::vector<TwistBipoint> *inputRow = (const std::vector<TwistBipoint> *) d;
  1264. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) e;
  1265. const std::vector<std::vector<TwistBipoint>> *matrixCommits = (const std::vector<std::vector<TwistBipoint>> *) f;
  1266. const TwistBipoint *twistG = (const TwistBipoint *) g;
  1267. const TwistBipoint *twistH = (const TwistBipoint *) h;
  1268. *dst = server->verify_proof_of_reordering<TwistBipoint>(*pi, *inputRow, *permutationCommits, *matrixCommits, *twistG, *twistH);
  1269. }
  1270. bool PrsonaServer::verify_vote_tensor_proofs(
  1271. const std::vector<std::vector<Proof>>& pi,
  1272. size_t start_offset,
  1273. const std::vector<std::vector<TwistBipoint>>& currMatrix,
  1274. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1275. const std::vector<std::vector<std::vector<TwistBipoint>>>& matrixCommits,
  1276. bool inverted) const
  1277. {
  1278. TwistBipoint twistG = bgnSystem.get_public_key().get_bipoint_twistgen();
  1279. TwistBipoint twistH = bgnSystem.get_public_key().get_bipoint_twist_subgroup_gen();
  1280. bool *intermediaryValues = new bool[currMatrix.size()];
  1281. std::vector<TwistBipoint> *inputRows = new std::vector<TwistBipoint>[currMatrix.size()];
  1282. std::vector<std::thread> individualTensorProofThreads;
  1283. for (size_t i = 0; i < currMatrix.size(); i++)
  1284. {
  1285. std::vector<TwistBipoint> currRow;
  1286. if (inverted)
  1287. {
  1288. for (size_t j = 0; j < currMatrix.size(); j++)
  1289. currRow.push_back(currMatrix[j][i]);
  1290. }
  1291. else
  1292. {
  1293. currRow = currMatrix[i];
  1294. }
  1295. inputRows[i] = currRow;
  1296. size_t whichProof = i + start_offset;
  1297. individualTensorProofThreads.push_back(std::thread(verify_tensor_r, this, intermediaryValues + i, &(pi[whichProof]), inputRows + i, &permutationCommits, &(matrixCommits[i]), &twistG, &twistH));
  1298. }
  1299. bool retval = true;
  1300. for (size_t i = 0; i < individualTensorProofThreads.size(); i++)
  1301. {
  1302. individualTensorProofThreads[i].join();
  1303. retval = retval && intermediaryValues[i];
  1304. }
  1305. delete [] inputRows;
  1306. delete [] intermediaryValues;
  1307. return retval;
  1308. }
  1309. void PrsonaServer::generate_user_tally_matrix(
  1310. const std::vector<std::vector<Scalar>>& permutations,
  1311. const Scalar& power,
  1312. const Twistpoint& nextGenerator,
  1313. const std::vector<Twistpoint>& currPseudonyms,
  1314. std::vector<Twistpoint>& masks,
  1315. std::vector<std::vector<Twistpoint>>& maskCommits,
  1316. std::vector<Twistpoint>& messages,
  1317. std::vector<std::vector<Twistpoint>>& messageCommits,
  1318. std::vector<std::vector<Scalar>>& userTallySeeds,
  1319. std::vector<std::vector<Twistpoint>>& userTallySeedCommits) const
  1320. {
  1321. masks.clear();
  1322. messages.clear();
  1323. for (size_t i = 0; i < currentUserEncryptedTallies.size(); i++)
  1324. {
  1325. masks.push_back(currentUserEncryptedTallies[i].mask);
  1326. messages.push_back(currentUserEncryptedTallies[i].encryptedMessage);
  1327. }
  1328. maskCommits.clear();
  1329. messageCommits.clear();
  1330. userTallySeeds.clear();
  1331. userTallySeedCommits.clear();
  1332. for (size_t i = 0; i < permutations.size(); i++)
  1333. {
  1334. std::vector<Scalar> currSeeds;
  1335. std::vector<Twistpoint> currRow;
  1336. for (size_t j = 0; j < permutations[i].size(); j++)
  1337. {
  1338. currSeeds.push_back(Scalar(0));
  1339. currRow.push_back(Twistpoint());
  1340. }
  1341. userTallySeeds.push_back(currSeeds);
  1342. maskCommits.push_back(currRow);
  1343. messageCommits.push_back(currRow);
  1344. userTallySeedCommits.push_back(currRow);
  1345. }
  1346. for (size_t i = 0; i < permutations.size(); i++)
  1347. {
  1348. size_t last = permutations[i].size() - 1;
  1349. for (size_t j = 0; j < permutations[i].size(); j++)
  1350. {
  1351. if (j != last)
  1352. {
  1353. userTallySeeds[i][j].set_random();
  1354. userTallySeeds[i][last] = userTallySeeds[i][last] - userTallySeeds[i][j];
  1355. }
  1356. maskCommits[i][j] = masks[j] * permutations[j][i] * power +
  1357. currPseudonyms[j] * power * permutations[j][i] * userTallySeeds[i][j] +
  1358. elGamalBlindGenerator * userTallySeeds[i][j];
  1359. messageCommits[i][j] = messages[j] * permutations[j][i] +
  1360. nextGenerator * permutations[j][i] * userTallySeeds[i][j] +
  1361. elGamalBlindGenerator * userTallySeeds[i][j];
  1362. userTallySeedCommits[i][j] = EL_GAMAL_GENERATOR * userTallySeeds[i][j];
  1363. }
  1364. }
  1365. }
  1366. template <typename T>
  1367. std::vector<std::vector<T>> PrsonaServer::generate_reordered_plus_power_matrix(
  1368. const std::vector<std::vector<Scalar>>& permutations,
  1369. const Scalar& power,
  1370. const std::vector<T>& oldValues,
  1371. std::vector<std::vector<Scalar>>& seeds,
  1372. std::vector<std::vector<Twistpoint>>& seedCommits,
  1373. const T& h) const
  1374. {
  1375. std::vector<std::vector<Scalar>> permutation_plus_power;
  1376. seedCommits.clear();
  1377. for (size_t i = 0; i < permutations.size(); i++)
  1378. {
  1379. std::vector<Scalar> currPermutations;
  1380. std::vector<Twistpoint> currSeedCommits;
  1381. for (size_t j = 0; j < permutations[i].size(); j++)
  1382. {
  1383. currPermutations.push_back(permutations[i][j] * power);
  1384. currSeedCommits.push_back(Twistpoint());
  1385. }
  1386. permutation_plus_power.push_back(currPermutations);
  1387. seedCommits.push_back(currSeedCommits);
  1388. }
  1389. std::vector<std::vector<T>> retval = generate_reordered_matrix<T>(permutation_plus_power, oldValues, seeds, h, true);
  1390. for (size_t i = 0; i < permutations.size(); i++)
  1391. for (size_t j = 0; j < permutations[i].size(); j++)
  1392. seedCommits[i][j] = EL_GAMAL_GENERATOR * seeds[i][j];
  1393. return retval;
  1394. }
  1395. template <typename T>
  1396. std::vector<std::vector<T>> PrsonaServer::generate_reordered_matrix(
  1397. const std::vector<std::vector<Scalar>>& permutations,
  1398. const std::vector<T>& oldValues,
  1399. std::vector<std::vector<Scalar>>& seeds,
  1400. const T& h,
  1401. bool cancelOut) const
  1402. {
  1403. std::vector<std::vector<T>> retval;
  1404. seeds.clear();
  1405. for (size_t i = 0; i < permutations.size(); i++)
  1406. {
  1407. std::vector<Scalar> currSeeds;
  1408. std::vector<T> currRow;
  1409. for (size_t j = 0; j < permutations[i].size(); j++)\
  1410. {
  1411. currSeeds.push_back(Scalar(0));
  1412. currRow.push_back(T());
  1413. }
  1414. seeds.push_back(currSeeds);
  1415. retval.push_back(currRow);
  1416. }
  1417. for (size_t i = 0; i < permutations.size(); i++)
  1418. {
  1419. size_t last = permutations[i].size() - 1;
  1420. for (size_t j = 0; j < permutations[i].size(); j++)
  1421. {
  1422. if (!cancelOut)
  1423. {
  1424. seeds[i][j].set_random();
  1425. }
  1426. else if (j != last)
  1427. {
  1428. seeds[i][j].set_random();
  1429. seeds[i][last] = seeds[i][last] - seeds[i][j];
  1430. }
  1431. retval[i][j] = oldValues[j] * permutations[j][i] + h * seeds[i][j];
  1432. }
  1433. }
  1434. return retval;
  1435. }
  1436. std::vector<size_t> PrsonaServer::sort_data(
  1437. const std::vector<Twistpoint>& inputs) const
  1438. {
  1439. std::vector<size_t> retval;
  1440. // SortingType's index member allows us to replicate the "sort" across
  1441. std::vector<SortingType> sortTracker;
  1442. for (size_t i = 0; i < inputs.size(); i++)
  1443. {
  1444. SortingType curr;
  1445. curr.pseudonym = inputs[i];
  1446. curr.index = i;
  1447. sortTracker.push_back(curr);
  1448. }
  1449. std::sort(sortTracker.begin(), sortTracker.end());
  1450. for (size_t i = 0; i < inputs.size(); i++)
  1451. retval.push_back(sortTracker[i].index);
  1452. return retval;
  1453. }
  1454. bool PrsonaServer::update_data(
  1455. const std::vector<std::vector<Twistpoint>>& freshPseudonymCommits,
  1456. const std::vector<std::vector<CurveBipoint>>& serverTallyCommits,
  1457. const std::vector<std::vector<std::vector<TwistBipoint>>>& voteMatrixCommits,
  1458. const std::vector<std::vector<Twistpoint>>& userTallyMaskCommits,
  1459. const std::vector<std::vector<Twistpoint>>& userTallyMessageCommits)
  1460. {
  1461. std::vector<Twistpoint> newPseudonyms;
  1462. std::vector<CurveBipoint> newVoteTallies;
  1463. std::vector<EGCiphertext> newUserTallies;
  1464. for (size_t i = 0; i < freshPseudonymCommits.size(); i++)
  1465. {
  1466. Twistpoint pseudonymSum = freshPseudonymCommits[i][0];
  1467. CurveBipoint voteTallySum = serverTallyCommits[i][0];
  1468. Twistpoint userTallyMask, userTallyMessage;
  1469. if (!userTallyMaskCommits.empty())
  1470. {
  1471. userTallyMask = userTallyMaskCommits[i][0];
  1472. userTallyMessage = userTallyMessageCommits[i][0];
  1473. }
  1474. for (size_t j = 1; j < freshPseudonymCommits[i].size(); j++)
  1475. {
  1476. pseudonymSum = pseudonymSum + freshPseudonymCommits[i][j];
  1477. voteTallySum = voteTallySum + serverTallyCommits[i][j];
  1478. if (!userTallyMaskCommits.empty())
  1479. {
  1480. userTallyMask = userTallyMask + userTallyMaskCommits[i][j];
  1481. userTallyMessage = userTallyMessage + userTallyMessageCommits[i][j];
  1482. }
  1483. }
  1484. newPseudonyms.push_back(pseudonymSum);
  1485. newVoteTallies.push_back(voteTallySum);
  1486. if (!userTallyMaskCommits.empty())
  1487. newUserTallies.push_back(EGCiphertext(userTallyMask, userTallyMessage));
  1488. }
  1489. if (!pseudonyms_sorted(newPseudonyms))
  1490. {
  1491. std::cerr << "Pseudonyms not sorted correctly." << std::endl;
  1492. return false;
  1493. }
  1494. currentPseudonyms = newPseudonyms;
  1495. previousVoteTallies = newVoteTallies;
  1496. voteMatrix = calculate_next_vote_matrix(voteMatrixCommits);
  1497. currentUserEncryptedTallies = newUserTallies;
  1498. return true;
  1499. }
  1500. bool PrsonaServer::pseudonyms_sorted(
  1501. const std::vector<Twistpoint> newPseudonyms) const
  1502. {
  1503. bool retval = true;
  1504. for (size_t i = 0; i < newPseudonyms.size() - 1; i++)
  1505. retval = retval && (newPseudonyms[i] < newPseudonyms[i + 1]);
  1506. return retval;
  1507. }
  1508. /*
  1509. * DATA MAINTENANCE
  1510. */
  1511. void PrsonaServer::export_new_user_update(
  1512. std::vector<CurveBipoint>& otherPreviousVoteTallies,
  1513. std::vector<Twistpoint>& otherCurrentPseudonyms,
  1514. std::vector<EGCiphertext>& otherCurrentUserEncryptedTallies,
  1515. std::vector<std::vector<TwistBipoint>>& otherVoteMatrix) const
  1516. {
  1517. otherPreviousVoteTallies = previousVoteTallies;
  1518. otherCurrentPseudonyms = currentPseudonyms;
  1519. otherCurrentUserEncryptedTallies = currentUserEncryptedTallies;
  1520. otherVoteMatrix = voteMatrix;
  1521. }
  1522. bool PrsonaServer::import_new_user_update(
  1523. const std::vector<Proof>& pi,
  1524. const std::vector<CurveBipoint>& otherPreviousVoteTallies,
  1525. const std::vector<Twistpoint>& otherCurrentPseudonyms,
  1526. const std::vector<EGCiphertext>& otherCurrentUserEncryptedTallies,
  1527. const std::vector<std::vector<TwistBipoint>>& otherVoteMatrix)
  1528. {
  1529. size_t newIndex = 0;
  1530. if (!currentPseudonyms.empty())
  1531. while (otherCurrentPseudonyms[newIndex] == currentPseudonyms[newIndex])
  1532. newIndex++;
  1533. Twistpoint shortTermPublicKey = otherCurrentPseudonyms[newIndex];
  1534. bool flag = verify_proof_of_added_user(pi, currentFreshGenerator, shortTermPublicKey, bgnSystem.get_public_key().get_bipoint_twistgen(), bgnSystem.get_public_key().get_bipoint_twist_subgroup_gen(), bgnSystem.get_public_key().get_bipoint_curvegen(), bgnSystem.get_public_key().get_bipoint_curve_subgroup_gen(), newIndex, otherCurrentUserEncryptedTallies[newIndex], otherPreviousVoteTallies[newIndex], otherVoteMatrix);
  1535. if (!flag)
  1536. {
  1537. std::cerr << "Other server added new user invalidly, aborting." << std::endl;
  1538. return false;
  1539. }
  1540. for (size_t i = 0; i < otherCurrentPseudonyms.size(); i++)
  1541. {
  1542. if (i == newIndex)
  1543. continue;
  1544. size_t otherI = (i > newIndex ? i - 1 : i);
  1545. flag = flag && otherCurrentPseudonyms[i] == currentPseudonyms[otherI];
  1546. flag = flag && otherCurrentUserEncryptedTallies[i] == currentUserEncryptedTallies[otherI];
  1547. flag = flag && otherPreviousVoteTallies[i] == previousVoteTallies[otherI];
  1548. for (size_t j = 0; j < otherCurrentPseudonyms.size(); j++)
  1549. {
  1550. if (j == newIndex)
  1551. continue;
  1552. size_t otherJ = (j > newIndex ? j - 1 : j);
  1553. flag = flag && otherVoteMatrix[i][j] == voteMatrix[otherI][otherJ];
  1554. }
  1555. }
  1556. if (!flag)
  1557. {
  1558. std::cerr << "Other server illicitly changed other value during new user add." << std::endl;
  1559. return false;
  1560. }
  1561. previousVoteTallies = otherPreviousVoteTallies;
  1562. currentPseudonyms = otherCurrentPseudonyms;
  1563. currentUserEncryptedTallies = otherCurrentUserEncryptedTallies;
  1564. voteMatrix = otherVoteMatrix;
  1565. return true;
  1566. }
  1567. /*
  1568. * DATA SAFEKEEPING
  1569. */
  1570. /* This is what powers the "shuffle"; really, as pseudonyms get updated,
  1571. * the pseudonyms are no longer in the order prescribed by operator<().
  1572. * So, we put them (and everything else) back into that order,
  1573. * effectively shuffling them (and making lookups easier later on). */
  1574. std::vector<size_t> PrsonaServer::order_data()
  1575. {
  1576. std::vector<size_t> retval = sort_data(currentPseudonyms);
  1577. // Order all other data in the same way, for consistency
  1578. std::vector<Twistpoint> newPseudonyms;
  1579. std::vector<CurveBipoint> newVoteTallies;
  1580. std::vector<EGCiphertext> newUserEncryptedTallies;
  1581. std::vector<std::vector<TwistBipoint>> newVoteMatrix;
  1582. for (size_t i = 0; i < retval.size(); i++)
  1583. {
  1584. newPseudonyms.push_back(currentPseudonyms[retval[i]]);
  1585. newVoteTallies.push_back(previousVoteTallies[retval[i]]);
  1586. if (!currentUserEncryptedTallies.empty())
  1587. newUserEncryptedTallies.push_back(currentUserEncryptedTallies[retval[i]]);
  1588. std::vector<TwistBipoint> currNewRow;
  1589. for (size_t j = 0; j < currentPseudonyms.size(); j++)
  1590. currNewRow.push_back(voteMatrix[retval[i]][retval[j]]);
  1591. newVoteMatrix.push_back(currNewRow);
  1592. }
  1593. previousVoteTallies = newVoteTallies;
  1594. currentPseudonyms = newPseudonyms;
  1595. currentUserEncryptedTallies = newUserEncryptedTallies;
  1596. voteMatrix = newVoteMatrix;
  1597. return retval;
  1598. }
  1599. /*
  1600. * BINARY SEARCH
  1601. */
  1602. // Completely normal binary search
  1603. size_t PrsonaServer::binary_search(
  1604. const Twistpoint& index) const
  1605. {
  1606. return PrsonaBase::binary_search(currentPseudonyms, index);
  1607. }
  1608. /*
  1609. * VALID VOTE PROOFS
  1610. */
  1611. bool PrsonaServer::verify_vote_proof(
  1612. const std::vector<Proof>& pi,
  1613. const std::vector<TwistBipoint>& oldVotes,
  1614. const std::vector<TwistBipoint>& newVotes,
  1615. const Twistpoint& shortTermPublicKey) const
  1616. {
  1617. const BGNPublicKey& pubKey = bgnSystem.get_public_key();
  1618. return PrsonaBase::verify_vote_proof(pubKey.get_bipoint_twistgen(), pubKey.get_bipoint_twist_subgroup_gen(), pi, oldVotes, newVotes, currentFreshGenerator, shortTermPublicKey);
  1619. }
  1620. void PrsonaServer::print_scores(
  1621. const std::vector<CurveBipoint>& scores)
  1622. {
  1623. std::unique_lock<std::mutex> lck(*decryptMtx);
  1624. std::cout << "[";
  1625. for (size_t i = 0; i < scores.size(); i++)
  1626. std::cout << bgnSystem.decrypt(scores[i]) << (i == scores.size() - 1 ? "]" : " ");
  1627. std::cout << std::endl;
  1628. }