server.cpp 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942
  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. std::cout << "Fresh Generator: " << std::hex << firstGenerator << std::dec << std::endl;
  351. currentGeneratorProof = pi;
  352. currentFreshGenerator = firstGenerator;
  353. return true;
  354. }
  355. // To calculate the blind generator for ElGamal, start from the base generator,
  356. // then have every server call this function on it iteratively (in any order).
  357. Twistpoint PrsonaServer::add_rand_seed_to_generator(
  358. std::vector<Proof>& pi,
  359. const Twistpoint& currGenerator) const
  360. {
  361. Scalar lambda;
  362. lambda.set_random();
  363. pi.push_back(add_to_generator_proof(currGenerator, lambda));
  364. return currGenerator * lambda;
  365. }
  366. bool PrsonaServer::set_EG_blind_generator(
  367. const std::vector<Proof>& pi,
  368. const Twistpoint& currGenerator)
  369. {
  370. return PrsonaBase::set_EG_blind_generator(pi, currGenerator, numServers);
  371. }
  372. /*
  373. * SCORE TALLYING
  374. */
  375. void homomorphic_multiplication_r(
  376. void *a,
  377. void *b,
  378. const void *c,
  379. const void *d)
  380. {
  381. BGN *bgnSystem = (BGN *) a;
  382. Quadripoint *dst = (Quadripoint *) b;
  383. const CurveBipoint *x = (const CurveBipoint *) c;
  384. const TwistBipoint *y = (const TwistBipoint *) d;
  385. *dst = bgnSystem->homomorphic_multiplication_no_rerandomize(*x, *y);
  386. }
  387. void homomorphic_addition_r(
  388. void *a,
  389. void *b,
  390. size_t size)
  391. {
  392. BGN *bgnSystem = (BGN *) a;
  393. Quadripoint *arr = (Quadripoint *) b;
  394. switch (size)
  395. {
  396. case 0:
  397. case 1:
  398. return;
  399. case 2:
  400. arr[0] = bgnSystem->homomorphic_addition_no_rerandomize(arr[0], arr[1]);
  401. return;
  402. default:
  403. break;
  404. }
  405. size_t halfSize = size / 2;
  406. std::thread threadA(homomorphic_addition_r, bgnSystem, arr, halfSize);
  407. std::thread threadB(homomorphic_addition_r, bgnSystem, arr + halfSize, size - halfSize);
  408. threadA.join();
  409. threadB.join();
  410. arr[0] = bgnSystem->homomorphic_addition_no_rerandomize(arr[0], arr[halfSize]);
  411. }
  412. void tally_r(
  413. void *a,
  414. void *b,
  415. void *c,
  416. const void *d,
  417. const void *e,
  418. size_t i)
  419. {
  420. std::mutex *decryptMtx = (std::mutex *) a;
  421. BGN *bgnSystem = (BGN *) b;
  422. Scalar *dst = (Scalar *) c;
  423. const std::vector<CurveBipoint> *previousVoteTallies = (const std::vector<CurveBipoint> *) d;
  424. const std::vector<std::vector<TwistBipoint>> *voteMatrix = (const std::vector<std::vector<TwistBipoint>> *) e;
  425. Quadripoint *weightedVotes = new Quadripoint[previousVoteTallies->size()];
  426. std::vector<std::thread> parallelizedMults;
  427. // ZIP
  428. for (size_t j = 0; j < previousVoteTallies->size(); j++)
  429. parallelizedMults.push_back(std::thread(homomorphic_multiplication_r, bgnSystem, weightedVotes + j, &((*previousVoteTallies)[j]), &((*voteMatrix)[j][i])));
  430. for (size_t j = 0; j < parallelizedMults.size(); j++)
  431. parallelizedMults[j].join();
  432. // FOLDL
  433. homomorphic_addition_r(bgnSystem, weightedVotes, previousVoteTallies->size());
  434. // DECRYPT
  435. std::unique_lock<std::mutex> lck(*decryptMtx);
  436. *dst = bgnSystem->decrypt(weightedVotes[0]);
  437. delete [] weightedVotes;
  438. }
  439. /* Calculate scores homomorphically (as an individual server would normally)
  440. * and then decrypt them (which the servers would normally work together to do).
  441. *
  442. * Note that since these calculations are just for us, we don't need to do any
  443. * expensive rerandomizations of intermediate values. */
  444. std::vector<Scalar> PrsonaServer::tally_scores()
  445. {
  446. std::vector<Quadripoint> BGNEncryptedTallies;
  447. Scalar *decryptedTallies = new Scalar[voteMatrix.size()];
  448. std::vector<std::thread> parallelizedTallies;
  449. for (size_t i = 0; i < voteMatrix.size(); i++)
  450. parallelizedTallies.push_back(std::thread(tally_r, decryptMtx, &bgnSystem, decryptedTallies + i, &previousVoteTallies, &voteMatrix, i));
  451. for (size_t i = 0; i < parallelizedTallies.size(); i++)
  452. parallelizedTallies[i].join();
  453. std::vector<Scalar> retval(decryptedTallies, decryptedTallies + voteMatrix.size());
  454. delete [] decryptedTallies;
  455. return retval;
  456. }
  457. /* Calculate what the maximum possible score this round was (that is,
  458. * given the current user weights, what was the highest possible score?).
  459. *
  460. * As with individual scores, this also does the decryption that servers
  461. * would ordinarily work together to form. */
  462. Scalar PrsonaServer::get_max_possible_score()
  463. {
  464. // FOLDL
  465. CurveBipoint currEncryptedVal = previousVoteTallies[0];
  466. for (size_t i = 1; i < previousVoteTallies.size(); i++)
  467. currEncryptedVal = bgnSystem.homomorphic_addition_no_rerandomize(currEncryptedVal, previousVoteTallies[i]);
  468. // DECRYPT
  469. std::unique_lock<std::mutex> lck(*decryptMtx);
  470. Scalar retval = bgnSystem.decrypt(currEncryptedVal);
  471. return retval;
  472. }
  473. void PrsonaServer::receive_tallied_scores(
  474. const std::vector<EGCiphertext>& userTallyScores,
  475. const std::vector<CurveBipoint>& serverTallyScores)
  476. {
  477. currentUserEncryptedTallies = userTallyScores;
  478. previousVoteTallies = serverTallyScores;
  479. }
  480. void PrsonaServer::encrypt(
  481. CurveBipoint& element,
  482. const Scalar& value)
  483. {
  484. bgnSystem.encrypt(element, value);
  485. }
  486. /*
  487. * EPOCH ROUNDS
  488. */
  489. // The first round, going from A_0 to A_0.5
  490. void PrsonaServer::build_up_midway_pseudonyms(
  491. std::vector<std::vector<std::vector<Proof>>>& pi,
  492. std::vector<std::vector<std::vector<Twistpoint>>>& permutationCommits,
  493. std::vector<std::vector<std::vector<Twistpoint>>>& freshPseudonymCommits,
  494. std::vector<std::vector<std::vector<Twistpoint>>>& freshPseudonymSeedCommits,
  495. std::vector<std::vector<std::vector<CurveBipoint>>>& serverTallyCommits,
  496. std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>>& partwayVoteMatrixCommits,
  497. std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>>& finalVoteMatrixCommits,
  498. Twistpoint& nextGenerator)
  499. {
  500. nextSeed.set_random();
  501. std::vector<std::vector<Twistpoint>> currPermutationCommits;
  502. std::vector<std::vector<Twistpoint>> currFreshPseudonymCommits;
  503. std::vector<std::vector<Twistpoint>> currFreshPseudonymSeedCommits;
  504. std::vector<std::vector<CurveBipoint>> currServerTallyCommits;
  505. std::vector<std::vector<std::vector<TwistBipoint>>> currPartwayVoteMatrixCommits;
  506. std::vector<std::vector<std::vector<TwistBipoint>>> currFinalVoteMatrixCommits;
  507. std::vector<std::vector<Twistpoint>> currUserTallyMaskCommits;
  508. std::vector<std::vector<Twistpoint>> currUserTallyMessageCommits;
  509. std::vector<std::vector<Twistpoint>> currUserTallySeedCommits;
  510. pi.push_back(epoch_calculations(currPermutationCommits, currFreshPseudonymCommits, currFreshPseudonymSeedCommits, currServerTallyCommits, currPartwayVoteMatrixCommits, currFinalVoteMatrixCommits, currUserTallyMaskCommits, currUserTallyMessageCommits, currUserTallySeedCommits, nextSeed, nextGenerator, false));
  511. permutationCommits.push_back(currPermutationCommits);
  512. freshPseudonymCommits.push_back(currFreshPseudonymCommits);
  513. freshPseudonymSeedCommits.push_back(currFreshPseudonymSeedCommits);
  514. serverTallyCommits.push_back(currServerTallyCommits);
  515. partwayVoteMatrixCommits.push_back(currPartwayVoteMatrixCommits);
  516. finalVoteMatrixCommits.push_back(currFinalVoteMatrixCommits);
  517. pi[0][0].push_back(add_to_generator_proof(nextGenerator, nextSeed));
  518. nextGenerator = nextGenerator * nextSeed;
  519. }
  520. // In between these rounds, scores are tallied, decrypted,
  521. // and encrypted to fresh user pseudonyms (possible through weird math)
  522. // The second round, going from A_0.5 to A_1
  523. void PrsonaServer::break_down_midway_pseudonyms(
  524. const std::vector<Proof>& generatorProof,
  525. std::vector<std::vector<std::vector<Proof>>>& pi,
  526. std::vector<std::vector<std::vector<Twistpoint>>>& permutationCommits,
  527. std::vector<std::vector<std::vector<Twistpoint>>>& freshPseudonymCommits,
  528. std::vector<std::vector<std::vector<Twistpoint>>>& freshPseudonymSeedCommits,
  529. std::vector<std::vector<std::vector<CurveBipoint>>>& serverTallyCommits,
  530. std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>>& partwayVoteMatrixCommits,
  531. std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>>& finalVoteMatrixCommits,
  532. std::vector<std::vector<std::vector<Twistpoint>>>& userTallyMaskCommits,
  533. std::vector<std::vector<std::vector<Twistpoint>>>& userTallyMessageCommits,
  534. std::vector<std::vector<std::vector<Twistpoint>>>& userTallySeedCommits,
  535. const Twistpoint& nextGenerator)
  536. {
  537. if (!initialize_fresh_generator(generatorProof, nextGenerator))
  538. {
  539. std::cerr << "New fresh generator could not be verified." << std::endl;
  540. return;
  541. }
  542. Scalar inverseSeed = currentSeed.curveMultInverse();
  543. std::vector<std::vector<Twistpoint>> currPermutationCommits;
  544. std::vector<std::vector<Twistpoint>> currFreshPseudonymCommits;
  545. std::vector<std::vector<Twistpoint>> currFreshPseudonymSeedCommits;
  546. std::vector<std::vector<CurveBipoint>> currServerTallyCommits;
  547. std::vector<std::vector<std::vector<TwistBipoint>>> currPartwayVoteMatrixCommits;
  548. std::vector<std::vector<std::vector<TwistBipoint>>> currFinalVoteMatrixCommits;
  549. std::vector<std::vector<Twistpoint>> currUserTallyMaskCommits;
  550. std::vector<std::vector<Twistpoint>> currUserTallyMessageCommits;
  551. std::vector<std::vector<Twistpoint>> currUserTallySeedCommits;
  552. pi.push_back(epoch_calculations(currPermutationCommits, currFreshPseudonymCommits, currFreshPseudonymSeedCommits, currServerTallyCommits, currPartwayVoteMatrixCommits, currFinalVoteMatrixCommits, currUserTallyMaskCommits, currUserTallyMessageCommits, currUserTallySeedCommits, inverseSeed, nextGenerator, true));
  553. permutationCommits.push_back(currPermutationCommits);
  554. freshPseudonymCommits.push_back(currFreshPseudonymCommits);
  555. freshPseudonymSeedCommits.push_back(currFreshPseudonymSeedCommits);
  556. serverTallyCommits.push_back(currServerTallyCommits);
  557. partwayVoteMatrixCommits.push_back(currPartwayVoteMatrixCommits);
  558. finalVoteMatrixCommits.push_back(currFinalVoteMatrixCommits);
  559. userTallyMaskCommits.push_back(currUserTallyMaskCommits);
  560. userTallyMessageCommits.push_back(currUserTallyMessageCommits);
  561. userTallySeedCommits.push_back(currUserTallySeedCommits);
  562. currentSeed = nextSeed;
  563. }
  564. /*
  565. * EPOCH HELPERS
  566. */
  567. void generate_permutation_commitment_r(
  568. const void *a,
  569. void *b,
  570. const void *c,
  571. void *d)
  572. {
  573. const PrsonaServer *server = (const PrsonaServer *) a;
  574. std::vector<std::vector<Twistpoint>> *permutationCommits = (std::vector<std::vector<Twistpoint>> *) b;
  575. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  576. std::vector<std::vector<Scalar>> *permutationSeeds = (std::vector<std::vector<Scalar>> *) d;
  577. *permutationCommits = server->generate_commitment_matrix(*permutations, *permutationSeeds);
  578. }
  579. void generate_pseudonym_commitment_r(
  580. const void *a,
  581. void *b,
  582. const void *c,
  583. const void *d,
  584. void *e,
  585. void *f)
  586. {
  587. const PrsonaServer *server = (const PrsonaServer *) a;
  588. std::vector<std::vector<Twistpoint>> *freshPseudonymCommits = (std::vector<std::vector<Twistpoint>> *) b;
  589. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  590. const Scalar *power = (const Scalar *) d;
  591. std::vector<std::vector<Scalar>> *freshPseudonymSeeds = (std::vector<std::vector<Scalar>> *) e;
  592. std::vector<std::vector<Twistpoint>> *freshPseudonymSeedCommits = (std::vector<std::vector<Twistpoint>> *) f;
  593. freshPseudonymSeedCommits->clear();
  594. *freshPseudonymCommits = server->generate_pseudonym_matrix(*permutations, *power, *freshPseudonymSeeds, *freshPseudonymSeedCommits);
  595. }
  596. void generate_server_tally_commitment_r(
  597. const void *a,
  598. void *b,
  599. const void *c,
  600. void *d)
  601. {
  602. const PrsonaServer *server = (const PrsonaServer *) a;
  603. std::vector<std::vector<CurveBipoint>> *serverTallyCommits = (std::vector<std::vector<CurveBipoint>> *) b;
  604. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  605. std::vector<std::vector<Scalar>> *serverTallySeeds = (std::vector<std::vector<Scalar>> *) d;
  606. *serverTallyCommits = server->generate_server_tally_matrix(*permutations, *serverTallySeeds);
  607. }
  608. void generate_matrix_commitment_r(
  609. const void *a,
  610. void *b,
  611. void *c,
  612. void *d,
  613. const void *e,
  614. const void *f,
  615. void *g,
  616. void *h)
  617. {
  618. const PrsonaServer *server = (const PrsonaServer *) a;
  619. std::vector<std::vector<std::vector<TwistBipoint>>> *partwayVoteMatrixCommits = (std::vector<std::vector<std::vector<TwistBipoint>>> *) b;
  620. std::vector<std::vector<TwistBipoint>> *partialVoteMatrix = (std::vector<std::vector<TwistBipoint>> *) c;
  621. std::vector<std::vector<std::vector<TwistBipoint>>> *finalVoteMatrixCommits = (std::vector<std::vector<std::vector<TwistBipoint>>> *) d;
  622. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) e;
  623. const std::vector<std::vector<TwistBipoint>> *voteMatrix = (const std::vector<std::vector<TwistBipoint>> *) f;
  624. std::vector<std::vector<std::vector<Scalar>>> *partwayVoteMatrixSeeds = (std::vector<std::vector<std::vector<Scalar>>> *) g;
  625. std::vector<std::vector<std::vector<Scalar>>> *finalVoteMatrixSeeds = (std::vector<std::vector<std::vector<Scalar>>> *) h;
  626. *partwayVoteMatrixCommits = server->generate_vote_tensor(*permutations, *voteMatrix, *partwayVoteMatrixSeeds, false);
  627. *partialVoteMatrix = server->calculate_next_vote_matrix(*partwayVoteMatrixCommits);
  628. *finalVoteMatrixCommits = server->generate_vote_tensor(*permutations, *partialVoteMatrix, *finalVoteMatrixSeeds, true);
  629. }
  630. void generate_user_tally_commitment_r(
  631. const void *a,
  632. const void *b,
  633. const void *c,
  634. const void *d,
  635. const void *e,
  636. void *f,
  637. void *g,
  638. void *h,
  639. void *i,
  640. void *j,
  641. void *k)
  642. {
  643. const PrsonaServer *server = (const PrsonaServer *) a;
  644. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) b;
  645. const Scalar *power = (const Scalar *) c;
  646. const Twistpoint *nextGenerator = (const Twistpoint *) d;
  647. const std::vector<Twistpoint> *currentPseudonyms = (const std::vector<Twistpoint> *) e;
  648. std::vector<Twistpoint> *userTallyMasks = (std::vector<Twistpoint> *) f;
  649. std::vector<std::vector<Twistpoint>> *userTallyMaskCommits = (std::vector<std::vector<Twistpoint>> *) g;
  650. std::vector<Twistpoint> *userTallyMessages = (std::vector<Twistpoint> *) h;
  651. std::vector<std::vector<Twistpoint>> *userTallyMessageCommits = (std::vector<std::vector<Twistpoint>> *) i;
  652. std::vector<std::vector<Scalar>> *userTallySeeds = (std::vector<std::vector<Scalar>> *) j;
  653. std::vector<std::vector<Twistpoint>> *userTallySeedCommits = (std::vector<std::vector<Twistpoint>> *) k;
  654. userTallyMaskCommits->clear();
  655. userTallyMessageCommits->clear();
  656. userTallySeedCommits->clear();
  657. server->generate_user_tally_matrix(*permutations, *power, *nextGenerator, *currentPseudonyms, *userTallyMasks, *userTallyMaskCommits, *userTallyMessages, *userTallyMessageCommits, *userTallySeeds, *userTallySeedCommits);
  658. }
  659. void generate_permutation_proof_r(
  660. const void *a,
  661. void *b,
  662. const void *c,
  663. const void *d,
  664. const void *e)
  665. {
  666. const PrsonaServer *server = (const PrsonaServer *) a;
  667. std::vector<Proof> *dst = (std::vector<Proof> *) b;
  668. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  669. const std::vector<std::vector<Scalar>> *permutationSeeds = (const std::vector<std::vector<Scalar>> *) d;
  670. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) e;
  671. *dst = server->generate_valid_permutation_proof(*permutations, *permutationSeeds, *permutationCommits);
  672. }
  673. void generate_pseudonym_proof_r(
  674. const void *a,
  675. void *b,
  676. const void *c,
  677. const void *d,
  678. const void *e,
  679. const void *f,
  680. const void *g,
  681. const void *h,
  682. const void *i,
  683. const void *j)
  684. {
  685. const PrsonaServer *server = (const PrsonaServer *) a;
  686. std::vector<Proof> *dst = (std::vector<Proof> *) b;
  687. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  688. const Scalar *power = (const Scalar *) d;
  689. const std::vector<std::vector<Scalar>> *permutationSeeds = (const std::vector<std::vector<Scalar>> *) e;
  690. const std::vector<std::vector<Scalar>> *freshPseudonymSeeds = (const std::vector<std::vector<Scalar>> *) f;
  691. const std::vector<Twistpoint> *currentPseudonyms = (const std::vector<Twistpoint> *) g;
  692. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) h;
  693. const std::vector<std::vector<Twistpoint>> *freshPseudonymCommits = (const std::vector<std::vector<Twistpoint>> *) i;
  694. const std::vector<std::vector<Twistpoint>> *freshPseudonymSeedCommits = (const std::vector<std::vector<Twistpoint>> *) j;
  695. *dst = server->generate_proof_of_reordering_plus_power(*permutations, *power, *permutationSeeds, *freshPseudonymSeeds, *currentPseudonyms, *permutationCommits, *freshPseudonymCommits, *freshPseudonymSeedCommits);
  696. }
  697. void generate_server_tally_proof_r(
  698. const void *a,
  699. void *b,
  700. const void *c,
  701. const void *d,
  702. const void *e,
  703. const void *f,
  704. const void *g,
  705. const void *h,
  706. const void *i,
  707. const void *j)
  708. {
  709. const PrsonaServer *server = (const PrsonaServer *) a;
  710. std::vector<Proof> *dst = (std::vector<Proof> *) b;
  711. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  712. const std::vector<std::vector<Scalar>> *permutationSeeds = (const std::vector<std::vector<Scalar>> *) d;
  713. const std::vector<std::vector<Scalar>> *serverTallySeeds = (const std::vector<std::vector<Scalar>> *) e;
  714. const std::vector<CurveBipoint> *previousVoteTallies = (const std::vector<CurveBipoint> *) f;
  715. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) g;
  716. const std::vector<std::vector<CurveBipoint>> *serverTallyCommits = (const std::vector<std::vector<CurveBipoint>> *) h;
  717. const CurveBipoint *curveG = (const CurveBipoint *) i;
  718. const CurveBipoint *curveH = (const CurveBipoint *) j;
  719. *dst = server->generate_proof_of_reordering<CurveBipoint>(*permutations, *permutationSeeds, *serverTallySeeds, *previousVoteTallies, *permutationCommits, *serverTallyCommits, *curveG, *curveH);
  720. }
  721. void generate_first_half_matrix_proof_r(
  722. const void *a,
  723. void *b,
  724. const void *c,
  725. const void *d,
  726. const void *e,
  727. const void *f,
  728. const void *g,
  729. const void *h)
  730. {
  731. const PrsonaServer *server = (const PrsonaServer *) a;
  732. std::vector<std::vector<Proof>> *dst = (std::vector<std::vector<Proof>> *) b;
  733. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  734. const std::vector<std::vector<Scalar>> *permutationSeeds = (const std::vector<std::vector<Scalar>> *) d;
  735. const std::vector<std::vector<std::vector<Scalar>>> *partwayVoteMatrixSeeds = (const std::vector<std::vector<std::vector<Scalar>>> *) e;
  736. const std::vector<std::vector<TwistBipoint>> *voteMatrix = (const std::vector<std::vector<TwistBipoint>> *) f;
  737. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) g;
  738. const std::vector<std::vector<std::vector<TwistBipoint>>> *partwayVoteMatrixCommits = (const std::vector<std::vector<std::vector<TwistBipoint>>> *) h;
  739. server->generate_vote_tensor_proofs(*dst, *permutations, *permutationSeeds, *partwayVoteMatrixSeeds, *voteMatrix, *permutationCommits, *partwayVoteMatrixCommits, false);
  740. }
  741. void generate_second_half_matrix_proof_r(
  742. const void *a,
  743. void *b,
  744. const void *c,
  745. const void *d,
  746. const void *e,
  747. const void *f,
  748. const void *g,
  749. const void *h)
  750. {
  751. const PrsonaServer *server = (const PrsonaServer *) a;
  752. std::vector<std::vector<Proof>> *dst = (std::vector<std::vector<Proof>> *) b;
  753. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  754. const std::vector<std::vector<Scalar>> *permutationSeeds = (const std::vector<std::vector<Scalar>> *) d;
  755. const std::vector<std::vector<std::vector<Scalar>>> *finalVoteMatrixSeeds = (const std::vector<std::vector<std::vector<Scalar>>> *) e;
  756. const std::vector<std::vector<TwistBipoint>> *partialVoteMatrix = (const std::vector<std::vector<TwistBipoint>> *) f;
  757. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) g;
  758. const std::vector<std::vector<std::vector<TwistBipoint>>> *finalVoteMatrixCommits = (const std::vector<std::vector<std::vector<TwistBipoint>>> *) h;
  759. server->generate_vote_tensor_proofs(*dst, *permutations, *permutationSeeds, *finalVoteMatrixSeeds, *partialVoteMatrix, *permutationCommits, *finalVoteMatrixCommits, true);
  760. }
  761. void generate_user_tally_proof_r(
  762. const void *a,
  763. void *b,
  764. const void *c,
  765. const void *d,
  766. const void *e,
  767. const void *f,
  768. const void *g,
  769. const void *h,
  770. const void *i,
  771. const void *j,
  772. const void *k,
  773. const void *l,
  774. const void *m,
  775. const void *n)
  776. {
  777. const PrsonaServer *server = (const PrsonaServer *) a;
  778. std::vector<Proof> *dst = (std::vector<Proof> *) b;
  779. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  780. const Scalar *power = (const Scalar *) d;
  781. const Twistpoint *nextGenerator = (const Twistpoint *) e;
  782. const std::vector<std::vector<Scalar>> *permutationSeeds = (const std::vector<std::vector<Scalar>> *) f;
  783. const std::vector<std::vector<Scalar>> *userTallySeeds = (const std::vector<std::vector<Scalar>> *) g;
  784. const std::vector<Twistpoint> *currentPseudonyms = (const std::vector<Twistpoint> *) h;
  785. const std::vector<Twistpoint> *userTallyMasks = (const std::vector<Twistpoint> *) i;
  786. const std::vector<Twistpoint> *userTallyMessages = (const std::vector<Twistpoint> *) j;
  787. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) k;
  788. const std::vector<std::vector<Twistpoint>> *userTallyMaskCommits = (const std::vector<std::vector<Twistpoint>> *) l;
  789. const std::vector<std::vector<Twistpoint>> *userTallyMessageCommits = (const std::vector<std::vector<Twistpoint>> *) m;
  790. const std::vector<std::vector<Twistpoint>> *userTallySeedCommits = (const std::vector<std::vector<Twistpoint>> *) n;
  791. *dst = server->generate_user_tally_proofs(*permutations, *power, *nextGenerator, *permutationSeeds, *userTallySeeds, *currentPseudonyms, *userTallyMasks, *userTallyMessages, *permutationCommits, *userTallyMaskCommits, *userTallyMessageCommits, *userTallySeedCommits);
  792. }
  793. std::vector<std::vector<Proof>> PrsonaServer::epoch_calculations(
  794. std::vector<std::vector<Twistpoint>>& permutationCommits,
  795. std::vector<std::vector<Twistpoint>>& freshPseudonymCommits,
  796. std::vector<std::vector<Twistpoint>>& freshPseudonymSeedCommits,
  797. std::vector<std::vector<CurveBipoint>>& serverTallyCommits,
  798. std::vector<std::vector<std::vector<TwistBipoint>>>& partwayVoteMatrixCommits,
  799. std::vector<std::vector<std::vector<TwistBipoint>>>& finalVoteMatrixCommits,
  800. std::vector<std::vector<Twistpoint>>& userTallyMaskCommits,
  801. std::vector<std::vector<Twistpoint>>& userTallyMessageCommits,
  802. std::vector<std::vector<Twistpoint>>& userTallySeedCommits,
  803. const Scalar& power,
  804. const Twistpoint& nextGenerator,
  805. bool doUserTallies)
  806. {
  807. std::vector<std::vector<Scalar>> permutations = generate_permutation_matrix(power);
  808. // for (size_t i = 0; i < permutations.size(); i++)
  809. // {
  810. // std::cout << (i == 0 ? "[[" : " [");
  811. // for (size_t j = 0; j < permutations[i].size(); j++)
  812. // std::cout << std::hex << permutations[i][j] << std::dec << (j == permutations[i].size() - 1 ? "]" : " ");
  813. // std::cout << (i == permutations.size() - 1 ? "]" : "") << std::endl;
  814. // }
  815. std::vector<std::vector<Proof>> retval, firstHalfTensorProof, secondHalfTensorProof;
  816. CurveBipoint curveG = bgnSystem.get_public_key().get_bipoint_curvegen();
  817. CurveBipoint curveH = bgnSystem.get_public_key().get_bipoint_curve_subgroup_gen();
  818. std::vector<Proof> *individualProofs = new std::vector<Proof>[doUserTallies ? 4 : 3];
  819. std::vector<std::vector<Scalar>> permutationSeeds;
  820. std::thread permutationCommitThread(generate_permutation_commitment_r, this, &permutationCommits, &permutations, &permutationSeeds);
  821. std::vector<std::vector<Scalar>> freshPseudonymSeeds;
  822. std::thread pseudonymCommitThread(generate_pseudonym_commitment_r, this, &freshPseudonymCommits, &permutations, &power, &freshPseudonymSeeds, &freshPseudonymSeedCommits);
  823. std::vector<std::vector<Scalar>> serverTallySeeds;
  824. std::thread serverTallyCommitThread(generate_server_tally_commitment_r, this, &serverTallyCommits, &permutations, &serverTallySeeds);
  825. std::vector<std::vector<std::vector<Scalar>>> partwayVoteMatrixSeeds;
  826. std::vector<std::vector<std::vector<Scalar>>> finalVoteMatrixSeeds;
  827. std::vector<std::vector<TwistBipoint>> partialVoteMatrix;
  828. std::thread voteMatrixCommitThread(generate_matrix_commitment_r, this, &partwayVoteMatrixCommits, &partialVoteMatrix, &finalVoteMatrixCommits, &permutations, &voteMatrix, &partwayVoteMatrixSeeds, &finalVoteMatrixSeeds);
  829. std::vector<Twistpoint> userTallyMasks;
  830. std::vector<Twistpoint> userTallyMessages;
  831. std::vector<std::vector<Scalar>> userTallySeeds;
  832. std::thread *userTallyCommitThread = NULL;
  833. if (doUserTallies)
  834. userTallyCommitThread = new std::thread(generate_user_tally_commitment_r, this, &permutations, &power, &nextGenerator, &currentPseudonyms, &userTallyMasks, &userTallyMaskCommits, &userTallyMessages, &userTallyMessageCommits, &userTallySeeds, &userTallySeedCommits);
  835. permutationCommitThread.join();
  836. std::thread permutationProofThread(generate_permutation_proof_r, this, individualProofs, &permutations, &permutationSeeds, &permutationCommits);
  837. pseudonymCommitThread.join();
  838. std::thread pseudonymProofThread(generate_pseudonym_proof_r, this, individualProofs + 1, &permutations, &power, &permutationSeeds, &freshPseudonymSeeds, &currentPseudonyms, &permutationCommits, &freshPseudonymCommits, &freshPseudonymSeedCommits);
  839. serverTallyCommitThread.join();
  840. std::thread serverTallyProofThread(generate_server_tally_proof_r, this, individualProofs + 2, &permutations, &permutationSeeds, &serverTallySeeds, &previousVoteTallies, &permutationCommits, &serverTallyCommits, &curveG, &curveH);
  841. voteMatrixCommitThread.join();
  842. std::thread firstHalfProofThread(generate_first_half_matrix_proof_r, this, &firstHalfTensorProof, &permutations, &permutationSeeds, &partwayVoteMatrixSeeds, &voteMatrix, &permutationCommits, &partwayVoteMatrixCommits);
  843. std::thread secondHalfProofThread(generate_second_half_matrix_proof_r, this, &secondHalfTensorProof, &permutations, &permutationSeeds, &finalVoteMatrixSeeds, &partialVoteMatrix, &permutationCommits, &finalVoteMatrixCommits);
  844. if (userTallyCommitThread)
  845. {
  846. userTallyCommitThread->join();
  847. delete userTallyCommitThread;
  848. userTallyCommitThread = NULL;
  849. }
  850. std::thread *userTallyProofThread = NULL;
  851. if (doUserTallies)
  852. userTallyProofThread = new std::thread(generate_user_tally_proof_r, this, individualProofs + 3, &permutations, &power, &nextGenerator, &permutationSeeds, &userTallySeeds, &currentPseudonyms, &userTallyMasks, &userTallyMessages, &permutationCommits, &userTallyMaskCommits, &userTallyMessageCommits, &userTallySeedCommits);
  853. permutationProofThread.join();
  854. pseudonymProofThread.join();
  855. serverTallyProofThread.join();
  856. firstHalfProofThread.join();
  857. secondHalfProofThread.join();
  858. if (userTallyProofThread)
  859. {
  860. userTallyProofThread->join();
  861. delete userTallyProofThread;
  862. userTallyProofThread = NULL;
  863. }
  864. retval.push_back(individualProofs[0]);
  865. retval.push_back(individualProofs[1]);
  866. retval.push_back(individualProofs[2]);
  867. for (size_t i = 0; i < firstHalfTensorProof.size(); i++)
  868. retval.push_back(firstHalfTensorProof[i]);
  869. for (size_t i = 0; i < secondHalfTensorProof.size(); i++)
  870. retval.push_back(secondHalfTensorProof[i]);
  871. if (doUserTallies)
  872. retval.push_back(individualProofs[3]);
  873. delete [] individualProofs;
  874. // Replace internal values
  875. update_data(freshPseudonymCommits, serverTallyCommits, finalVoteMatrixCommits, userTallyMaskCommits, userTallyMessageCommits);
  876. return retval;
  877. }
  878. void verify_permutation_r(
  879. const void *a,
  880. void *b,
  881. const void *c,
  882. const void *d)
  883. {
  884. const PrsonaServer *server = (const PrsonaServer *) a;
  885. bool *dst = (bool *) b;
  886. const std::vector<Proof> *pi = (const std::vector<Proof> *) c;
  887. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) d;
  888. *dst = server->verify_valid_permutation_proof(*pi, *permutationCommits);
  889. }
  890. void verify_pseudonym_r(
  891. const void *a,
  892. void *b,
  893. const void *c,
  894. const void *d,
  895. const void *e,
  896. const void *f,
  897. const void *g)
  898. {
  899. const PrsonaServer *server = (const PrsonaServer *) a;
  900. bool *dst = (bool *) b;
  901. const std::vector<Proof> *pi = (const std::vector<Proof> *) c;
  902. const std::vector<Twistpoint> *currentPseudonyms = (const std::vector<Twistpoint> *) d;
  903. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) e;
  904. const std::vector<std::vector<Twistpoint>> *freshPseudonymCommits = (const std::vector<std::vector<Twistpoint>> *) f;
  905. const std::vector<std::vector<Twistpoint>> *freshPseudonymSeedCommits = (const std::vector<std::vector<Twistpoint>> *) g;
  906. *dst = server->verify_proof_of_reordering_plus_power(*pi, *currentPseudonyms, *permutationCommits, *freshPseudonymCommits, *freshPseudonymSeedCommits);
  907. }
  908. void verify_server_tally_r(
  909. const void *a,
  910. void *b,
  911. const void *c,
  912. const void *d,
  913. const void *e,
  914. const void *f,
  915. const void *g,
  916. const void *h)
  917. {
  918. const PrsonaServer *server = (const PrsonaServer *) a;
  919. bool *dst = (bool *) b;
  920. const std::vector<Proof> *pi = (const std::vector<Proof> *) c;
  921. const std::vector<CurveBipoint> *previousVoteTallies = (const std::vector<CurveBipoint> *) d;
  922. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) e;
  923. const std::vector<std::vector<CurveBipoint>> *serverTallyCommits = (const std::vector<std::vector<CurveBipoint>> *) f;
  924. const CurveBipoint *curveG = (const CurveBipoint *) g;
  925. const CurveBipoint *curveH = (const CurveBipoint *) h;
  926. *dst = server->verify_proof_of_reordering<CurveBipoint>(*pi, *previousVoteTallies, *permutationCommits, *serverTallyCommits, *curveG, *curveH);
  927. }
  928. void verify_first_half_matrix_r(
  929. const void *a,
  930. void *b,
  931. const void *c,
  932. const void *d,
  933. const void *e,
  934. const void *f,
  935. const void *g)
  936. {
  937. const PrsonaServer *server = (const PrsonaServer *) a;
  938. bool *dst = (bool *) b;
  939. const std::vector<std::vector<Proof>> *pi = (const std::vector<std::vector<Proof>> *) c;
  940. const size_t *currOffset = (const size_t *) d;
  941. const std::vector<std::vector<TwistBipoint>> *voteMatrix = (const std::vector<std::vector<TwistBipoint>> *) e;
  942. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) f;
  943. const std::vector<std::vector<std::vector<TwistBipoint>>> *partwayVoteMatrixCommits = (const std::vector<std::vector<std::vector<TwistBipoint>>> *) g;
  944. *dst = server->verify_vote_tensor_proofs(*pi, *currOffset, *voteMatrix, *permutationCommits, *partwayVoteMatrixCommits, false);
  945. }
  946. void verify_second_half_matrix_r(
  947. const void *a,
  948. void *b,
  949. const void *c,
  950. const void *d,
  951. const void *e,
  952. const void *f,
  953. const void *g)
  954. {
  955. const PrsonaServer *server = (const PrsonaServer *) a;
  956. bool *dst = (bool *) b;
  957. const std::vector<std::vector<Proof>> *pi = (const std::vector<std::vector<Proof>> *) c;
  958. const size_t *currOffset = (const size_t *) d;
  959. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) e;
  960. const std::vector<std::vector<std::vector<TwistBipoint>>> *partwayVoteMatrixCommits = (const std::vector<std::vector<std::vector<TwistBipoint>>> *) f;
  961. const std::vector<std::vector<std::vector<TwistBipoint>>> *finalVoteMatrixCommits = (const std::vector<std::vector<std::vector<TwistBipoint>>> *) g;
  962. std::vector<std::vector<TwistBipoint>> partialVoteMatrix = server->calculate_next_vote_matrix(*partwayVoteMatrixCommits);
  963. *dst = server->verify_vote_tensor_proofs(*pi, *currOffset, partialVoteMatrix, *permutationCommits, *finalVoteMatrixCommits, true);
  964. }
  965. void verify_user_tally_r(
  966. const void *a,
  967. void *b,
  968. const void *c,
  969. const void *d,
  970. const void *e,
  971. const void *f,
  972. const void *g,
  973. const void *h,
  974. const void *i,
  975. const void *j)
  976. {
  977. const PrsonaServer *server = (const PrsonaServer *) a;
  978. bool *dst = (bool *) b;
  979. const std::vector<Proof> *pi = (const std::vector<Proof> *) c;
  980. const Twistpoint *nextGenerator = (const Twistpoint *) d;
  981. const std::vector<Twistpoint> *currentPseudonyms = (const std::vector<Twistpoint> *) e;
  982. const std::vector<EGCiphertext> *currentUserEncryptedTallies = (const std::vector<EGCiphertext> *) f;
  983. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) g;
  984. const std::vector<std::vector<Twistpoint>> *userTallyMaskCommits = (const std::vector<std::vector<Twistpoint>> *) h;
  985. const std::vector<std::vector<Twistpoint>> *userTallyMessageCommits = (const std::vector<std::vector<Twistpoint>> *) i;
  986. const std::vector<std::vector<Twistpoint>> *userTallySeedCommits = (const std::vector<std::vector<Twistpoint>> *) j;
  987. std::vector<Twistpoint> userTallyMasks;
  988. std::vector<Twistpoint> userTallyMessages;
  989. for (size_t i = 0; i < currentUserEncryptedTallies->size(); i++)
  990. {
  991. userTallyMasks.push_back((*currentUserEncryptedTallies)[i].mask);
  992. userTallyMessages.push_back((*currentUserEncryptedTallies)[i].encryptedMessage);
  993. }
  994. *dst = server->verify_user_tally_proofs(*pi, *nextGenerator, *currentPseudonyms, userTallyMasks, userTallyMessages, *permutationCommits, *userTallyMaskCommits, *userTallyMessageCommits, *userTallySeedCommits);
  995. }
  996. bool PrsonaServer::accept_epoch_updates(
  997. const std::vector<std::vector<Proof>>& pi,
  998. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  999. const std::vector<std::vector<Twistpoint>>& freshPseudonymCommits,
  1000. const std::vector<std::vector<Twistpoint>>& freshPseudonymSeedCommits,
  1001. const std::vector<std::vector<CurveBipoint>>& serverTallyCommits,
  1002. const std::vector<std::vector<std::vector<TwistBipoint>>>& partwayVoteMatrixCommits,
  1003. const std::vector<std::vector<std::vector<TwistBipoint>>>& finalVoteMatrixCommits,
  1004. const std::vector<std::vector<Twistpoint>>& userTallyMaskCommits,
  1005. const std::vector<std::vector<Twistpoint>>& userTallyMessageCommits,
  1006. const std::vector<std::vector<Twistpoint>>& userTallySeedCommits,
  1007. const Twistpoint& nextGenerator,
  1008. bool doUserTallies)
  1009. {
  1010. bool permutationVerification = false;
  1011. bool pseudonymVerification = false;
  1012. bool serverTallyVerification = false;
  1013. bool firstHalfVerification = false;
  1014. bool secondHalfVerification = false;
  1015. bool userTallyVerification = true;
  1016. if ((userTallyMaskCommits.empty() && doUserTallies) || (!userTallyMaskCommits.empty() && !doUserTallies))
  1017. {
  1018. std::cerr << "user tallies are not in expected state." << std::endl;
  1019. return false;
  1020. }
  1021. if (pi.empty())
  1022. return false;
  1023. size_t currOffset = 0;
  1024. std::thread permutationThread(verify_permutation_r, this, &permutationVerification, &(pi[currOffset]), &permutationCommits);
  1025. currOffset++;
  1026. std::thread pseudonymThread(verify_pseudonym_r, this, &pseudonymVerification, &(pi[currOffset]), &currentPseudonyms, &permutationCommits, &freshPseudonymCommits, &freshPseudonymSeedCommits);
  1027. currOffset++;
  1028. CurveBipoint curveG = bgnSystem.get_public_key().get_bipoint_curvegen();
  1029. CurveBipoint curveH = bgnSystem.get_public_key().get_bipoint_curve_subgroup_gen();
  1030. std::thread serverTallyThread(verify_server_tally_r, this, &serverTallyVerification, &(pi[currOffset]), &previousVoteTallies, &permutationCommits, &serverTallyCommits, &curveG, &curveH);
  1031. currOffset++;
  1032. size_t firstHalfOffset = currOffset;
  1033. std::thread firstHalfThread(verify_first_half_matrix_r, this, &firstHalfVerification, &pi, &firstHalfOffset, &voteMatrix, &permutationCommits, &partwayVoteMatrixCommits);
  1034. currOffset += voteMatrix.size();
  1035. size_t secondHalfOffset = currOffset;
  1036. std::thread secondHalfThread(verify_second_half_matrix_r, this, &secondHalfVerification, &pi, &secondHalfOffset, &permutationCommits, &partwayVoteMatrixCommits, &finalVoteMatrixCommits);
  1037. currOffset += voteMatrix.size();
  1038. std::thread *userTallyThread = NULL;
  1039. if (doUserTallies)
  1040. userTallyThread = new std::thread(verify_user_tally_r, this, &userTallyVerification, &(pi[currOffset]), &nextGenerator, &currentPseudonyms, &currentUserEncryptedTallies, &permutationCommits, &userTallyMaskCommits, &userTallyMessageCommits, &userTallySeedCommits);
  1041. currOffset++;
  1042. permutationThread.join();
  1043. pseudonymThread.join();
  1044. serverTallyThread.join();
  1045. firstHalfThread.join();
  1046. secondHalfThread.join();
  1047. if (userTallyThread)
  1048. {
  1049. userTallyThread->join();
  1050. delete userTallyThread;
  1051. userTallyThread = NULL;
  1052. }
  1053. if (!permutationVerification)
  1054. {
  1055. std::cerr << "Could not verify valid permutation matrix." << std::endl;
  1056. return false;
  1057. }
  1058. if (!pseudonymVerification)
  1059. {
  1060. std::cerr << "Could not verify valid pseudonym vector." << std::endl;
  1061. return false;
  1062. }
  1063. if (!serverTallyVerification)
  1064. {
  1065. std::cerr << "Could not verify valid server tally vector." << std::endl;
  1066. return false;
  1067. }
  1068. if (!userTallyVerification)
  1069. {
  1070. std::cerr << "Could not verify user tallies." << std::endl;
  1071. return false;
  1072. }
  1073. if (!firstHalfVerification)
  1074. {
  1075. std::cerr << "Could not verify first half vote matrix." << std::endl;
  1076. return false;
  1077. }
  1078. if (!secondHalfVerification)
  1079. {
  1080. std::cerr << "Could not verify second half vote matrix." << std::endl;
  1081. return false;
  1082. }
  1083. bool finalVerification = update_data(freshPseudonymCommits, serverTallyCommits, finalVoteMatrixCommits, userTallyMaskCommits, userTallyMessageCommits);
  1084. return finalVerification;
  1085. }
  1086. std::vector<std::vector<Scalar>> PrsonaServer::generate_permutation_matrix(
  1087. const Scalar& reorderSeed) const
  1088. {
  1089. std::vector<std::vector<Scalar>> retval;
  1090. for (size_t i = 0; i < currentPseudonyms.size(); i++)
  1091. {
  1092. std::vector<Scalar> currRow;
  1093. for (size_t j = 0; j < currentPseudonyms.size(); j++)
  1094. currRow.push_back(Scalar(0));
  1095. retval.push_back(currRow);
  1096. }
  1097. std::vector<Twistpoint> nextPseudonyms;
  1098. for (size_t i = 0; i < currentPseudonyms.size(); i++)
  1099. nextPseudonyms.push_back(currentPseudonyms[i] * reorderSeed);
  1100. std::vector<size_t> order = sort_data(nextPseudonyms);
  1101. for (size_t i = 0; i < order.size(); i++)
  1102. retval[order[i]][i] = Scalar(1);
  1103. return retval;
  1104. }
  1105. std::vector<std::vector<Twistpoint>> PrsonaServer::generate_commitment_matrix(
  1106. const std::vector<std::vector<Scalar>>& permutations,
  1107. std::vector<std::vector<Scalar>>& seeds) const
  1108. {
  1109. std::vector<std::vector<Twistpoint>> retval;
  1110. Twistpoint g = EL_GAMAL_GENERATOR;
  1111. Twistpoint h = elGamalBlindGenerator;
  1112. seeds.clear();
  1113. for (size_t i = 0; i < permutations.size(); i++)
  1114. {
  1115. std::vector<Scalar> currSeeds;
  1116. for (size_t j = 0; j < permutations[i].size(); j++)
  1117. currSeeds.push_back(Scalar(0));
  1118. seeds.push_back(currSeeds);
  1119. }
  1120. for (size_t i = 0; i < permutations.size(); i++)
  1121. {
  1122. std::vector<Twistpoint> currRow;
  1123. size_t last = permutations[i].size() - 1;
  1124. for (size_t j = 0; j < permutations[i].size(); j++)
  1125. {
  1126. Twistpoint element;
  1127. if (j != last)
  1128. {
  1129. seeds[i][j].set_random();
  1130. seeds[i][last] = seeds[i][last] - seeds[i][j];
  1131. }
  1132. element = g * permutations[i][j] + h * seeds[i][j];
  1133. currRow.push_back(element);
  1134. }
  1135. retval.push_back(currRow);
  1136. }
  1137. return retval;
  1138. }
  1139. std::vector<std::vector<Twistpoint>> PrsonaServer::generate_pseudonym_matrix(
  1140. const std::vector<std::vector<Scalar>>& permutations,
  1141. const Scalar& power,
  1142. std::vector<std::vector<Scalar>>& seeds,
  1143. std::vector<std::vector<Twistpoint>>& seedCommits) const
  1144. {
  1145. return generate_reordered_plus_power_matrix<Twistpoint>(permutations, power, currentPseudonyms, seeds, seedCommits, elGamalBlindGenerator);
  1146. }
  1147. std::vector<std::vector<CurveBipoint>> PrsonaServer::generate_server_tally_matrix(
  1148. const std::vector<std::vector<Scalar>>& permutations,
  1149. std::vector<std::vector<Scalar>>& seeds) const
  1150. {
  1151. return generate_reordered_matrix<CurveBipoint>(permutations, previousVoteTallies, seeds, bgnSystem.get_public_key().get_bipoint_curve_subgroup_gen(), false);
  1152. }
  1153. std::vector<std::vector<std::vector<TwistBipoint>>> PrsonaServer::generate_vote_tensor(
  1154. const std::vector<std::vector<Scalar>>& permutations,
  1155. const std::vector<std::vector<TwistBipoint>>& currVoteMatrix,
  1156. std::vector<std::vector<std::vector<Scalar>>>& seeds,
  1157. bool inverted) const
  1158. {
  1159. std::vector<std::vector<std::vector<TwistBipoint>>> retval;
  1160. for (size_t i = 0; i < currVoteMatrix.size(); i++)
  1161. {
  1162. std::vector<std::vector<Scalar>> currSeeds;
  1163. std::vector<TwistBipoint> inputRow;
  1164. if (inverted)
  1165. {
  1166. for (size_t j = 0; j < currVoteMatrix.size(); j++)
  1167. inputRow.push_back(currVoteMatrix[j][i]);
  1168. }
  1169. else
  1170. {
  1171. inputRow = currVoteMatrix[i];
  1172. }
  1173. retval.push_back(generate_reordered_matrix<TwistBipoint>(permutations, inputRow, currSeeds, bgnSystem.get_public_key().get_bipoint_twist_subgroup_gen(), false));
  1174. seeds.push_back(currSeeds);
  1175. }
  1176. return retval;
  1177. }
  1178. std::vector<std::vector<TwistBipoint>> PrsonaServer::calculate_next_vote_matrix(
  1179. const std::vector<std::vector<std::vector<TwistBipoint>>>& voteTensor) const
  1180. {
  1181. std::vector<std::vector<TwistBipoint>> retval;
  1182. for (size_t i = 0; i < voteTensor.size(); i++)
  1183. {
  1184. std::vector<TwistBipoint> currRow;
  1185. for (size_t j = 0; j < voteTensor[i].size(); j++)
  1186. {
  1187. TwistBipoint sum = voteTensor[i][j][0];
  1188. for (size_t k = 1; k < voteTensor[i][j].size(); k++)
  1189. sum = sum + voteTensor[i][j][k];
  1190. currRow.push_back(sum);
  1191. }
  1192. retval.push_back(currRow);
  1193. }
  1194. return retval;
  1195. }
  1196. void generate_tensor_r(
  1197. const void *a,
  1198. void *b,
  1199. const void *c,
  1200. const void *d,
  1201. const void *e,
  1202. const void *f,
  1203. const void *g,
  1204. const void *h,
  1205. const void *i,
  1206. const void *j)
  1207. {
  1208. const PrsonaServer *server = (const PrsonaServer *) a;
  1209. std::vector<Proof> *dst = (std::vector<Proof> *) b;
  1210. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  1211. const std::vector<std::vector<Scalar>> *permutationSeeds = (const std::vector<std::vector<Scalar>> *) d;
  1212. const std::vector<std::vector<Scalar>> *matrixSeeds = (const std::vector<std::vector<Scalar>> *) e;
  1213. const std::vector<TwistBipoint> *inputRow = (const std::vector<TwistBipoint> *) f;
  1214. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) g;
  1215. const std::vector<std::vector<TwistBipoint>> *matrixCommits = (const std::vector<std::vector<TwistBipoint>> *) h;
  1216. const TwistBipoint *twistG = (const TwistBipoint *) i;
  1217. const TwistBipoint *twistH = (const TwistBipoint *) j;
  1218. *dst = server->generate_proof_of_reordering<TwistBipoint>(*permutations, *permutationSeeds, *matrixSeeds, *inputRow, *permutationCommits, *matrixCommits, *twistG, *twistH);
  1219. }
  1220. void PrsonaServer::generate_vote_tensor_proofs(
  1221. std::vector<std::vector<Proof>>& pi,
  1222. const std::vector<std::vector<Scalar>>& permutations,
  1223. const std::vector<std::vector<Scalar>>& permutationSeeds,
  1224. const std::vector<std::vector<std::vector<Scalar>>>& matrixSeeds,
  1225. const std::vector<std::vector<TwistBipoint>>& currMatrix,
  1226. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1227. const std::vector<std::vector<std::vector<TwistBipoint>>>& matrixCommits,
  1228. bool inverted) const
  1229. {
  1230. TwistBipoint twistG = bgnSystem.get_public_key().get_bipoint_twistgen();
  1231. TwistBipoint twistH = bgnSystem.get_public_key().get_bipoint_twist_subgroup_gen();
  1232. std::vector<Proof> *intermediaryProofs = new std::vector<Proof>[currMatrix.size()];
  1233. std::vector<TwistBipoint> *inputRows = new std::vector<TwistBipoint>[currMatrix.size()];
  1234. std::vector<std::thread> individualTensorProofThreads;
  1235. for (size_t i = 0; i < currMatrix.size(); i++)
  1236. {
  1237. std::vector<TwistBipoint> currRow;
  1238. if (inverted)
  1239. {
  1240. for (size_t j = 0; j < currMatrix.size(); j++)
  1241. currRow.push_back(currMatrix[j][i]);
  1242. }
  1243. else
  1244. {
  1245. currRow = currMatrix[i];
  1246. }
  1247. inputRows[i] = currRow;
  1248. individualTensorProofThreads.push_back(std::thread(generate_tensor_r, this, intermediaryProofs + i, &permutations, &permutationSeeds, &(matrixSeeds[i]), inputRows + i, &permutationCommits, &(matrixCommits[i]), &twistG, &twistH));
  1249. }
  1250. for (size_t i = 0; i < individualTensorProofThreads.size(); i++)
  1251. {
  1252. individualTensorProofThreads[i].join();
  1253. pi.push_back(intermediaryProofs[i]);
  1254. }
  1255. delete [] inputRows;
  1256. delete [] intermediaryProofs;
  1257. }
  1258. void verify_tensor_r(
  1259. const void *a,
  1260. void *b,
  1261. const void *c,
  1262. const void *d,
  1263. const void *e,
  1264. const void *f,
  1265. const void *g,
  1266. const void *h)
  1267. {
  1268. const PrsonaServer *server = (const PrsonaServer *) a;
  1269. bool *dst = (bool *) b;
  1270. const std::vector<Proof> *pi = (const std::vector<Proof> *) c;
  1271. const std::vector<TwistBipoint> *inputRow = (const std::vector<TwistBipoint> *) d;
  1272. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) e;
  1273. const std::vector<std::vector<TwistBipoint>> *matrixCommits = (const std::vector<std::vector<TwistBipoint>> *) f;
  1274. const TwistBipoint *twistG = (const TwistBipoint *) g;
  1275. const TwistBipoint *twistH = (const TwistBipoint *) h;
  1276. *dst = server->verify_proof_of_reordering<TwistBipoint>(*pi, *inputRow, *permutationCommits, *matrixCommits, *twistG, *twistH);
  1277. }
  1278. bool PrsonaServer::verify_vote_tensor_proofs(
  1279. const std::vector<std::vector<Proof>>& pi,
  1280. size_t start_offset,
  1281. const std::vector<std::vector<TwistBipoint>>& currMatrix,
  1282. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1283. const std::vector<std::vector<std::vector<TwistBipoint>>>& matrixCommits,
  1284. bool inverted) const
  1285. {
  1286. TwistBipoint twistG = bgnSystem.get_public_key().get_bipoint_twistgen();
  1287. TwistBipoint twistH = bgnSystem.get_public_key().get_bipoint_twist_subgroup_gen();
  1288. bool *intermediaryValues = new bool[currMatrix.size()];
  1289. std::vector<TwistBipoint> *inputRows = new std::vector<TwistBipoint>[currMatrix.size()];
  1290. std::vector<std::thread> individualTensorProofThreads;
  1291. for (size_t i = 0; i < currMatrix.size(); i++)
  1292. {
  1293. std::vector<TwistBipoint> currRow;
  1294. if (inverted)
  1295. {
  1296. for (size_t j = 0; j < currMatrix.size(); j++)
  1297. currRow.push_back(currMatrix[j][i]);
  1298. }
  1299. else
  1300. {
  1301. currRow = currMatrix[i];
  1302. }
  1303. inputRows[i] = currRow;
  1304. size_t whichProof = i + start_offset;
  1305. individualTensorProofThreads.push_back(std::thread(verify_tensor_r, this, intermediaryValues + i, &(pi[whichProof]), inputRows + i, &permutationCommits, &(matrixCommits[i]), &twistG, &twistH));
  1306. }
  1307. bool retval = true;
  1308. for (size_t i = 0; i < individualTensorProofThreads.size(); i++)
  1309. {
  1310. individualTensorProofThreads[i].join();
  1311. retval = retval && intermediaryValues[i];
  1312. }
  1313. delete [] inputRows;
  1314. delete [] intermediaryValues;
  1315. return retval;
  1316. }
  1317. void PrsonaServer::generate_user_tally_matrix(
  1318. const std::vector<std::vector<Scalar>>& permutations,
  1319. const Scalar& power,
  1320. const Twistpoint& nextGenerator,
  1321. const std::vector<Twistpoint>& currPseudonyms,
  1322. std::vector<Twistpoint>& masks,
  1323. std::vector<std::vector<Twistpoint>>& maskCommits,
  1324. std::vector<Twistpoint>& messages,
  1325. std::vector<std::vector<Twistpoint>>& messageCommits,
  1326. std::vector<std::vector<Scalar>>& userTallySeeds,
  1327. std::vector<std::vector<Twistpoint>>& userTallySeedCommits) const
  1328. {
  1329. masks.clear();
  1330. messages.clear();
  1331. for (size_t i = 0; i < currentUserEncryptedTallies.size(); i++)
  1332. {
  1333. masks.push_back(currentUserEncryptedTallies[i].mask);
  1334. messages.push_back(currentUserEncryptedTallies[i].encryptedMessage);
  1335. }
  1336. maskCommits.clear();
  1337. messageCommits.clear();
  1338. userTallySeeds.clear();
  1339. userTallySeedCommits.clear();
  1340. for (size_t i = 0; i < permutations.size(); i++)
  1341. {
  1342. std::vector<Scalar> currSeeds;
  1343. std::vector<Twistpoint> currRow;
  1344. for (size_t j = 0; j < permutations[i].size(); j++)
  1345. {
  1346. currSeeds.push_back(Scalar(0));
  1347. currRow.push_back(Twistpoint());
  1348. }
  1349. userTallySeeds.push_back(currSeeds);
  1350. maskCommits.push_back(currRow);
  1351. messageCommits.push_back(currRow);
  1352. userTallySeedCommits.push_back(currRow);
  1353. }
  1354. for (size_t i = 0; i < permutations.size(); i++)
  1355. {
  1356. size_t last = permutations[i].size() - 1;
  1357. for (size_t j = 0; j < permutations[i].size(); j++)
  1358. {
  1359. if (j != last)
  1360. {
  1361. userTallySeeds[i][j].set_random();
  1362. userTallySeeds[i][last] = userTallySeeds[i][last] - userTallySeeds[i][j];
  1363. }
  1364. maskCommits[i][j] = masks[j] * permutations[j][i] * power +
  1365. currPseudonyms[j] * power * permutations[j][i] * userTallySeeds[i][j] +
  1366. elGamalBlindGenerator * userTallySeeds[i][j];
  1367. messageCommits[i][j] = messages[j] * permutations[j][i] +
  1368. nextGenerator * permutations[j][i] * userTallySeeds[i][j] +
  1369. elGamalBlindGenerator * userTallySeeds[i][j];
  1370. userTallySeedCommits[i][j] = EL_GAMAL_GENERATOR * userTallySeeds[i][j];
  1371. }
  1372. }
  1373. }
  1374. template <typename T>
  1375. std::vector<std::vector<T>> PrsonaServer::generate_reordered_plus_power_matrix(
  1376. const std::vector<std::vector<Scalar>>& permutations,
  1377. const Scalar& power,
  1378. const std::vector<T>& oldValues,
  1379. std::vector<std::vector<Scalar>>& seeds,
  1380. std::vector<std::vector<Twistpoint>>& seedCommits,
  1381. const T& h) const
  1382. {
  1383. std::vector<std::vector<Scalar>> permutation_plus_power;
  1384. seedCommits.clear();
  1385. for (size_t i = 0; i < permutations.size(); i++)
  1386. {
  1387. std::vector<Scalar> currPermutations;
  1388. std::vector<Twistpoint> currSeedCommits;
  1389. for (size_t j = 0; j < permutations[i].size(); j++)
  1390. {
  1391. currPermutations.push_back(permutations[i][j] * power);
  1392. currSeedCommits.push_back(Twistpoint());
  1393. }
  1394. permutation_plus_power.push_back(currPermutations);
  1395. seedCommits.push_back(currSeedCommits);
  1396. }
  1397. std::vector<std::vector<T>> retval = generate_reordered_matrix<T>(permutation_plus_power, oldValues, seeds, h, true);
  1398. for (size_t i = 0; i < permutations.size(); i++)
  1399. for (size_t j = 0; j < permutations[i].size(); j++)
  1400. seedCommits[i][j] = EL_GAMAL_GENERATOR * seeds[i][j];
  1401. return retval;
  1402. }
  1403. template <typename T>
  1404. std::vector<std::vector<T>> PrsonaServer::generate_reordered_matrix(
  1405. const std::vector<std::vector<Scalar>>& permutations,
  1406. const std::vector<T>& oldValues,
  1407. std::vector<std::vector<Scalar>>& seeds,
  1408. const T& h,
  1409. bool cancelOut) const
  1410. {
  1411. std::vector<std::vector<T>> retval;
  1412. seeds.clear();
  1413. for (size_t i = 0; i < permutations.size(); i++)
  1414. {
  1415. std::vector<Scalar> currSeeds;
  1416. std::vector<T> currRow;
  1417. for (size_t j = 0; j < permutations[i].size(); j++)\
  1418. {
  1419. currSeeds.push_back(Scalar(0));
  1420. currRow.push_back(T());
  1421. }
  1422. seeds.push_back(currSeeds);
  1423. retval.push_back(currRow);
  1424. }
  1425. for (size_t i = 0; i < permutations.size(); i++)
  1426. {
  1427. size_t last = permutations[i].size() - 1;
  1428. for (size_t j = 0; j < permutations[i].size(); j++)
  1429. {
  1430. if (!cancelOut)
  1431. {
  1432. seeds[i][j].set_random();
  1433. }
  1434. else if (j != last)
  1435. {
  1436. seeds[i][j].set_random();
  1437. seeds[i][last] = seeds[i][last] - seeds[i][j];
  1438. }
  1439. retval[i][j] = oldValues[j] * permutations[j][i] + h * seeds[i][j];
  1440. }
  1441. }
  1442. return retval;
  1443. }
  1444. std::vector<size_t> PrsonaServer::sort_data(
  1445. const std::vector<Twistpoint>& inputs) const
  1446. {
  1447. std::vector<size_t> retval;
  1448. // SortingType's index member allows us to replicate the "sort" across
  1449. std::vector<SortingType> sortTracker;
  1450. for (size_t i = 0; i < inputs.size(); i++)
  1451. {
  1452. SortingType curr;
  1453. curr.pseudonym = inputs[i];
  1454. curr.index = i;
  1455. sortTracker.push_back(curr);
  1456. }
  1457. std::sort(sortTracker.begin(), sortTracker.end());
  1458. for (size_t i = 0; i < inputs.size(); i++)
  1459. retval.push_back(sortTracker[i].index);
  1460. return retval;
  1461. }
  1462. bool PrsonaServer::update_data(
  1463. const std::vector<std::vector<Twistpoint>>& freshPseudonymCommits,
  1464. const std::vector<std::vector<CurveBipoint>>& serverTallyCommits,
  1465. const std::vector<std::vector<std::vector<TwistBipoint>>>& voteMatrixCommits,
  1466. const std::vector<std::vector<Twistpoint>>& userTallyMaskCommits,
  1467. const std::vector<std::vector<Twistpoint>>& userTallyMessageCommits)
  1468. {
  1469. std::vector<Twistpoint> newPseudonyms;
  1470. std::vector<CurveBipoint> newVoteTallies;
  1471. std::vector<EGCiphertext> newUserTallies;
  1472. for (size_t i = 0; i < freshPseudonymCommits.size(); i++)
  1473. {
  1474. Twistpoint pseudonymSum = freshPseudonymCommits[i][0];
  1475. CurveBipoint voteTallySum = serverTallyCommits[i][0];
  1476. Twistpoint userTallyMask, userTallyMessage;
  1477. if (!userTallyMaskCommits.empty())
  1478. {
  1479. userTallyMask = userTallyMaskCommits[i][0];
  1480. userTallyMessage = userTallyMessageCommits[i][0];
  1481. }
  1482. for (size_t j = 1; j < freshPseudonymCommits[i].size(); j++)
  1483. {
  1484. pseudonymSum = pseudonymSum + freshPseudonymCommits[i][j];
  1485. voteTallySum = voteTallySum + serverTallyCommits[i][j];
  1486. if (!userTallyMaskCommits.empty())
  1487. {
  1488. userTallyMask = userTallyMask + userTallyMaskCommits[i][j];
  1489. userTallyMessage = userTallyMessage + userTallyMessageCommits[i][j];
  1490. }
  1491. }
  1492. newPseudonyms.push_back(pseudonymSum);
  1493. newVoteTallies.push_back(voteTallySum);
  1494. if (!userTallyMaskCommits.empty())
  1495. newUserTallies.push_back(EGCiphertext(userTallyMask, userTallyMessage));
  1496. }
  1497. if (!pseudonyms_sorted(newPseudonyms))
  1498. {
  1499. std::cerr << "Pseudonyms not sorted correctly." << std::endl;
  1500. return false;
  1501. }
  1502. currentPseudonyms = newPseudonyms;
  1503. previousVoteTallies = newVoteTallies;
  1504. voteMatrix = calculate_next_vote_matrix(voteMatrixCommits);
  1505. currentUserEncryptedTallies = newUserTallies;
  1506. return true;
  1507. }
  1508. bool PrsonaServer::pseudonyms_sorted(
  1509. const std::vector<Twistpoint> newPseudonyms) const
  1510. {
  1511. bool retval = true;
  1512. for (size_t i = 0; i < newPseudonyms.size() - 1; i++)
  1513. retval = retval && (newPseudonyms[i] < newPseudonyms[i + 1]);
  1514. return retval;
  1515. }
  1516. /*
  1517. * DATA MAINTENANCE
  1518. */
  1519. void PrsonaServer::export_new_user_update(
  1520. std::vector<CurveBipoint>& otherPreviousVoteTallies,
  1521. std::vector<Twistpoint>& otherCurrentPseudonyms,
  1522. std::vector<EGCiphertext>& otherCurrentUserEncryptedTallies,
  1523. std::vector<std::vector<TwistBipoint>>& otherVoteMatrix) const
  1524. {
  1525. otherPreviousVoteTallies = previousVoteTallies;
  1526. otherCurrentPseudonyms = currentPseudonyms;
  1527. otherCurrentUserEncryptedTallies = currentUserEncryptedTallies;
  1528. otherVoteMatrix = voteMatrix;
  1529. }
  1530. bool PrsonaServer::import_new_user_update(
  1531. const std::vector<Proof>& pi,
  1532. const std::vector<CurveBipoint>& otherPreviousVoteTallies,
  1533. const std::vector<Twistpoint>& otherCurrentPseudonyms,
  1534. const std::vector<EGCiphertext>& otherCurrentUserEncryptedTallies,
  1535. const std::vector<std::vector<TwistBipoint>>& otherVoteMatrix)
  1536. {
  1537. size_t newIndex = 0;
  1538. if (!currentPseudonyms.empty())
  1539. while (otherCurrentPseudonyms[newIndex] == currentPseudonyms[newIndex])
  1540. newIndex++;
  1541. Twistpoint shortTermPublicKey = otherCurrentPseudonyms[newIndex];
  1542. 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);
  1543. if (!flag)
  1544. {
  1545. std::cerr << "Other server added new user invalidly, aborting." << std::endl;
  1546. return false;
  1547. }
  1548. for (size_t i = 0; i < otherCurrentPseudonyms.size(); i++)
  1549. {
  1550. if (i == newIndex)
  1551. continue;
  1552. size_t otherI = (i > newIndex ? i - 1 : i);
  1553. flag = flag && otherCurrentPseudonyms[i] == currentPseudonyms[otherI];
  1554. flag = flag && otherCurrentUserEncryptedTallies[i] == currentUserEncryptedTallies[otherI];
  1555. flag = flag && otherPreviousVoteTallies[i] == previousVoteTallies[otherI];
  1556. for (size_t j = 0; j < otherCurrentPseudonyms.size(); j++)
  1557. {
  1558. if (j == newIndex)
  1559. continue;
  1560. size_t otherJ = (j > newIndex ? j - 1 : j);
  1561. flag = flag && otherVoteMatrix[i][j] == voteMatrix[otherI][otherJ];
  1562. }
  1563. }
  1564. if (!flag)
  1565. {
  1566. std::cerr << "Other server illicitly changed other value during new user add." << std::endl;
  1567. return false;
  1568. }
  1569. previousVoteTallies = otherPreviousVoteTallies;
  1570. currentPseudonyms = otherCurrentPseudonyms;
  1571. currentUserEncryptedTallies = otherCurrentUserEncryptedTallies;
  1572. voteMatrix = otherVoteMatrix;
  1573. return true;
  1574. }
  1575. /*
  1576. * DATA SAFEKEEPING
  1577. */
  1578. /* This is what powers the "shuffle"; really, as pseudonyms get updated,
  1579. * the pseudonyms are no longer in the order prescribed by operator<().
  1580. * So, we put them (and everything else) back into that order,
  1581. * effectively shuffling them (and making lookups easier later on). */
  1582. std::vector<size_t> PrsonaServer::order_data()
  1583. {
  1584. std::vector<size_t> retval = sort_data(currentPseudonyms);
  1585. // Order all other data in the same way, for consistency
  1586. std::vector<Twistpoint> newPseudonyms;
  1587. std::vector<CurveBipoint> newVoteTallies;
  1588. std::vector<EGCiphertext> newUserEncryptedTallies;
  1589. std::vector<std::vector<TwistBipoint>> newVoteMatrix;
  1590. for (size_t i = 0; i < retval.size(); i++)
  1591. {
  1592. newPseudonyms.push_back(currentPseudonyms[retval[i]]);
  1593. newVoteTallies.push_back(previousVoteTallies[retval[i]]);
  1594. if (!currentUserEncryptedTallies.empty())
  1595. newUserEncryptedTallies.push_back(currentUserEncryptedTallies[retval[i]]);
  1596. std::vector<TwistBipoint> currNewRow;
  1597. for (size_t j = 0; j < currentPseudonyms.size(); j++)
  1598. currNewRow.push_back(voteMatrix[retval[i]][retval[j]]);
  1599. newVoteMatrix.push_back(currNewRow);
  1600. }
  1601. previousVoteTallies = newVoteTallies;
  1602. currentPseudonyms = newPseudonyms;
  1603. currentUserEncryptedTallies = newUserEncryptedTallies;
  1604. voteMatrix = newVoteMatrix;
  1605. return retval;
  1606. }
  1607. /*
  1608. * BINARY SEARCH
  1609. */
  1610. // Completely normal binary search
  1611. size_t PrsonaServer::binary_search(
  1612. const Twistpoint& index) const
  1613. {
  1614. return PrsonaBase::binary_search(currentPseudonyms, index);
  1615. }
  1616. /*
  1617. * VALID VOTE PROOFS
  1618. */
  1619. bool PrsonaServer::verify_vote_proof(
  1620. const std::vector<Proof>& pi,
  1621. const std::vector<TwistBipoint>& oldVotes,
  1622. const std::vector<TwistBipoint>& newVotes,
  1623. const Twistpoint& shortTermPublicKey) const
  1624. {
  1625. const BGNPublicKey& pubKey = bgnSystem.get_public_key();
  1626. return PrsonaBase::verify_vote_proof(pubKey.get_bipoint_twistgen(), pubKey.get_bipoint_twist_subgroup_gen(), pi, oldVotes, newVotes, currentFreshGenerator, shortTermPublicKey);
  1627. }
  1628. void PrsonaServer::print_scores(
  1629. const std::vector<CurveBipoint>& scores)
  1630. {
  1631. std::unique_lock<std::mutex> lck(*decryptMtx);
  1632. std::cout << "[";
  1633. for (size_t i = 0; i < scores.size(); i++)
  1634. std::cout << bgnSystem.decrypt(scores[i]) << (i == scores.size() - 1 ? "]" : " ");
  1635. std::cout << std::endl;
  1636. }