server.cpp 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080
  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. void PrsonaServer::hbc_build_up_midway_pseudonyms(
  520. std::vector<Proof>& generatorProof,
  521. std::vector<Twistpoint>& newFreshPseudonyms,
  522. std::vector<CurveBipoint>& newServerTallies,
  523. std::vector<std::vector<TwistBipoint>>& newVoteMatrix,
  524. Twistpoint& nextGenerator)
  525. {
  526. nextSeed.set_random();
  527. std::vector<EGCiphertext> newUserTallies;
  528. hbc_epoch_calculations(newFreshPseudonyms, newServerTallies, newVoteMatrix, newUserTallies, nextSeed, nextGenerator, false);
  529. generatorProof.push_back(add_to_generator_proof(nextGenerator, nextSeed));
  530. nextGenerator = nextGenerator * nextSeed;
  531. }
  532. // In between these rounds, scores are tallied, decrypted,
  533. // and encrypted to fresh user pseudonyms (possible through weird math)
  534. // The second round, going from A_0.5 to A_1
  535. void PrsonaServer::break_down_midway_pseudonyms(
  536. const std::vector<Proof>& generatorProof,
  537. std::vector<std::vector<std::vector<Proof>>>& pi,
  538. std::vector<std::vector<std::vector<Twistpoint>>>& permutationCommits,
  539. std::vector<std::vector<std::vector<Twistpoint>>>& freshPseudonymCommits,
  540. std::vector<std::vector<std::vector<Twistpoint>>>& freshPseudonymSeedCommits,
  541. std::vector<std::vector<std::vector<CurveBipoint>>>& serverTallyCommits,
  542. std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>>& partwayVoteMatrixCommits,
  543. std::vector<std::vector<std::vector<std::vector<TwistBipoint>>>>& finalVoteMatrixCommits,
  544. std::vector<std::vector<std::vector<Twistpoint>>>& userTallyMaskCommits,
  545. std::vector<std::vector<std::vector<Twistpoint>>>& userTallyMessageCommits,
  546. std::vector<std::vector<std::vector<Twistpoint>>>& userTallySeedCommits,
  547. const Twistpoint& nextGenerator)
  548. {
  549. if (!initialize_fresh_generator(generatorProof, nextGenerator))
  550. {
  551. std::cerr << "New fresh generator could not be verified." << std::endl;
  552. return;
  553. }
  554. Scalar inverseSeed = currentSeed.curveMultInverse();
  555. std::vector<std::vector<Twistpoint>> currPermutationCommits;
  556. std::vector<std::vector<Twistpoint>> currFreshPseudonymCommits;
  557. std::vector<std::vector<Twistpoint>> currFreshPseudonymSeedCommits;
  558. std::vector<std::vector<CurveBipoint>> currServerTallyCommits;
  559. std::vector<std::vector<std::vector<TwistBipoint>>> currPartwayVoteMatrixCommits;
  560. std::vector<std::vector<std::vector<TwistBipoint>>> currFinalVoteMatrixCommits;
  561. std::vector<std::vector<Twistpoint>> currUserTallyMaskCommits;
  562. std::vector<std::vector<Twistpoint>> currUserTallyMessageCommits;
  563. std::vector<std::vector<Twistpoint>> currUserTallySeedCommits;
  564. pi.push_back(epoch_calculations(currPermutationCommits, currFreshPseudonymCommits, currFreshPseudonymSeedCommits, currServerTallyCommits, currPartwayVoteMatrixCommits, currFinalVoteMatrixCommits, currUserTallyMaskCommits, currUserTallyMessageCommits, currUserTallySeedCommits, inverseSeed, nextGenerator, true));
  565. permutationCommits.push_back(currPermutationCommits);
  566. freshPseudonymCommits.push_back(currFreshPseudonymCommits);
  567. freshPseudonymSeedCommits.push_back(currFreshPseudonymSeedCommits);
  568. serverTallyCommits.push_back(currServerTallyCommits);
  569. partwayVoteMatrixCommits.push_back(currPartwayVoteMatrixCommits);
  570. finalVoteMatrixCommits.push_back(currFinalVoteMatrixCommits);
  571. userTallyMaskCommits.push_back(currUserTallyMaskCommits);
  572. userTallyMessageCommits.push_back(currUserTallyMessageCommits);
  573. userTallySeedCommits.push_back(currUserTallySeedCommits);
  574. currentSeed = nextSeed;
  575. }
  576. void PrsonaServer::hbc_break_down_midway_pseudonyms(
  577. const std::vector<Proof>& generatorProof,
  578. std::vector<Twistpoint>& newFreshPseudonyms,
  579. std::vector<CurveBipoint>& newServerTallies,
  580. std::vector<std::vector<TwistBipoint>>& newVoteMatrix,
  581. std::vector<EGCiphertext>& newUserTallies,
  582. const Twistpoint& nextGenerator)
  583. {
  584. if (!initialize_fresh_generator(generatorProof, nextGenerator))
  585. {
  586. std::cerr << "New fresh generator could not be verified." << std::endl;
  587. return;
  588. }
  589. Scalar inverseSeed = currentSeed.curveMultInverse();
  590. hbc_epoch_calculations(newFreshPseudonyms, newServerTallies, newVoteMatrix, newUserTallies, inverseSeed, nextGenerator, true);
  591. currentSeed = nextSeed;
  592. }
  593. /*
  594. * EPOCH HELPERS
  595. */
  596. void generate_permutation_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<Twistpoint>> *permutationCommits = (std::vector<std::vector<Twistpoint>> *) b;
  604. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  605. std::vector<std::vector<Scalar>> *permutationSeeds = (std::vector<std::vector<Scalar>> *) d;
  606. *permutationCommits = server->generate_commitment_matrix(*permutations, *permutationSeeds);
  607. }
  608. void generate_pseudonym_commitment_r(
  609. const void *a,
  610. void *b,
  611. const void *c,
  612. const void *d,
  613. void *e,
  614. void *f)
  615. {
  616. const PrsonaServer *server = (const PrsonaServer *) a;
  617. std::vector<std::vector<Twistpoint>> *freshPseudonymCommits = (std::vector<std::vector<Twistpoint>> *) b;
  618. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  619. const Scalar *power = (const Scalar *) d;
  620. std::vector<std::vector<Scalar>> *freshPseudonymSeeds = (std::vector<std::vector<Scalar>> *) e;
  621. std::vector<std::vector<Twistpoint>> *freshPseudonymSeedCommits = (std::vector<std::vector<Twistpoint>> *) f;
  622. freshPseudonymSeedCommits->clear();
  623. *freshPseudonymCommits = server->generate_pseudonym_matrix(*permutations, *power, *freshPseudonymSeeds, *freshPseudonymSeedCommits);
  624. }
  625. void generate_server_tally_commitment_r(
  626. const void *a,
  627. void *b,
  628. const void *c,
  629. void *d)
  630. {
  631. const PrsonaServer *server = (const PrsonaServer *) a;
  632. std::vector<std::vector<CurveBipoint>> *serverTallyCommits = (std::vector<std::vector<CurveBipoint>> *) b;
  633. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  634. std::vector<std::vector<Scalar>> *serverTallySeeds = (std::vector<std::vector<Scalar>> *) d;
  635. *serverTallyCommits = server->generate_server_tally_matrix(*permutations, *serverTallySeeds);
  636. }
  637. void generate_matrix_commitment_r(
  638. const void *a,
  639. void *b,
  640. void *c,
  641. void *d,
  642. const void *e,
  643. const void *f,
  644. void *g,
  645. void *h)
  646. {
  647. const PrsonaServer *server = (const PrsonaServer *) a;
  648. std::vector<std::vector<std::vector<TwistBipoint>>> *partwayVoteMatrixCommits = (std::vector<std::vector<std::vector<TwistBipoint>>> *) b;
  649. std::vector<std::vector<TwistBipoint>> *partialVoteMatrix = (std::vector<std::vector<TwistBipoint>> *) c;
  650. std::vector<std::vector<std::vector<TwistBipoint>>> *finalVoteMatrixCommits = (std::vector<std::vector<std::vector<TwistBipoint>>> *) d;
  651. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) e;
  652. const std::vector<std::vector<TwistBipoint>> *voteMatrix = (const std::vector<std::vector<TwistBipoint>> *) f;
  653. std::vector<std::vector<std::vector<Scalar>>> *partwayVoteMatrixSeeds = (std::vector<std::vector<std::vector<Scalar>>> *) g;
  654. std::vector<std::vector<std::vector<Scalar>>> *finalVoteMatrixSeeds = (std::vector<std::vector<std::vector<Scalar>>> *) h;
  655. *partwayVoteMatrixCommits = server->generate_vote_tensor(*permutations, *voteMatrix, *partwayVoteMatrixSeeds, false);
  656. *partialVoteMatrix = server->calculate_next_vote_matrix(*partwayVoteMatrixCommits);
  657. *finalVoteMatrixCommits = server->generate_vote_tensor(*permutations, *partialVoteMatrix, *finalVoteMatrixSeeds, true);
  658. }
  659. void generate_user_tally_commitment_r(
  660. const void *a,
  661. const void *b,
  662. const void *c,
  663. const void *d,
  664. const void *e,
  665. void *f,
  666. void *g,
  667. void *h,
  668. void *i,
  669. void *j,
  670. void *k)
  671. {
  672. const PrsonaServer *server = (const PrsonaServer *) a;
  673. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) b;
  674. const Scalar *power = (const Scalar *) c;
  675. const Twistpoint *nextGenerator = (const Twistpoint *) d;
  676. const std::vector<Twistpoint> *currentPseudonyms = (const std::vector<Twistpoint> *) e;
  677. std::vector<Twistpoint> *userTallyMasks = (std::vector<Twistpoint> *) f;
  678. std::vector<std::vector<Twistpoint>> *userTallyMaskCommits = (std::vector<std::vector<Twistpoint>> *) g;
  679. std::vector<Twistpoint> *userTallyMessages = (std::vector<Twistpoint> *) h;
  680. std::vector<std::vector<Twistpoint>> *userTallyMessageCommits = (std::vector<std::vector<Twistpoint>> *) i;
  681. std::vector<std::vector<Scalar>> *userTallySeeds = (std::vector<std::vector<Scalar>> *) j;
  682. std::vector<std::vector<Twistpoint>> *userTallySeedCommits = (std::vector<std::vector<Twistpoint>> *) k;
  683. userTallyMaskCommits->clear();
  684. userTallyMessageCommits->clear();
  685. userTallySeedCommits->clear();
  686. server->generate_user_tally_matrix(*permutations, *power, *nextGenerator, *currentPseudonyms, *userTallyMasks, *userTallyMaskCommits, *userTallyMessages, *userTallyMessageCommits, *userTallySeeds, *userTallySeedCommits);
  687. }
  688. void generate_permutation_proof_r(
  689. const void *a,
  690. void *b,
  691. const void *c,
  692. const void *d,
  693. const void *e)
  694. {
  695. const PrsonaServer *server = (const PrsonaServer *) a;
  696. std::vector<Proof> *dst = (std::vector<Proof> *) b;
  697. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  698. const std::vector<std::vector<Scalar>> *permutationSeeds = (const std::vector<std::vector<Scalar>> *) d;
  699. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) e;
  700. *dst = server->generate_valid_permutation_proof(*permutations, *permutationSeeds, *permutationCommits);
  701. }
  702. void generate_pseudonym_proof_r(
  703. const void *a,
  704. void *b,
  705. const void *c,
  706. const void *d,
  707. const void *e,
  708. const void *f,
  709. const void *g,
  710. const void *h,
  711. const void *i,
  712. const void *j)
  713. {
  714. const PrsonaServer *server = (const PrsonaServer *) a;
  715. std::vector<Proof> *dst = (std::vector<Proof> *) b;
  716. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  717. const Scalar *power = (const Scalar *) d;
  718. const std::vector<std::vector<Scalar>> *permutationSeeds = (const std::vector<std::vector<Scalar>> *) e;
  719. const std::vector<std::vector<Scalar>> *freshPseudonymSeeds = (const std::vector<std::vector<Scalar>> *) f;
  720. const std::vector<Twistpoint> *currentPseudonyms = (const std::vector<Twistpoint> *) g;
  721. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) h;
  722. const std::vector<std::vector<Twistpoint>> *freshPseudonymCommits = (const std::vector<std::vector<Twistpoint>> *) i;
  723. const std::vector<std::vector<Twistpoint>> *freshPseudonymSeedCommits = (const std::vector<std::vector<Twistpoint>> *) j;
  724. *dst = server->generate_proof_of_reordering_plus_power(*permutations, *power, *permutationSeeds, *freshPseudonymSeeds, *currentPseudonyms, *permutationCommits, *freshPseudonymCommits, *freshPseudonymSeedCommits);
  725. }
  726. void generate_server_tally_proof_r(
  727. const void *a,
  728. void *b,
  729. const void *c,
  730. const void *d,
  731. const void *e,
  732. const void *f,
  733. const void *g,
  734. const void *h,
  735. const void *i,
  736. const void *j)
  737. {
  738. const PrsonaServer *server = (const PrsonaServer *) a;
  739. std::vector<Proof> *dst = (std::vector<Proof> *) b;
  740. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  741. const std::vector<std::vector<Scalar>> *permutationSeeds = (const std::vector<std::vector<Scalar>> *) d;
  742. const std::vector<std::vector<Scalar>> *serverTallySeeds = (const std::vector<std::vector<Scalar>> *) e;
  743. const std::vector<CurveBipoint> *previousVoteTallies = (const std::vector<CurveBipoint> *) f;
  744. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) g;
  745. const std::vector<std::vector<CurveBipoint>> *serverTallyCommits = (const std::vector<std::vector<CurveBipoint>> *) h;
  746. const CurveBipoint *curveG = (const CurveBipoint *) i;
  747. const CurveBipoint *curveH = (const CurveBipoint *) j;
  748. *dst = server->generate_proof_of_reordering<CurveBipoint>(*permutations, *permutationSeeds, *serverTallySeeds, *previousVoteTallies, *permutationCommits, *serverTallyCommits, *curveG, *curveH);
  749. }
  750. void generate_first_half_matrix_proof_r(
  751. const void *a,
  752. void *b,
  753. const void *c,
  754. const void *d,
  755. const void *e,
  756. const void *f,
  757. const void *g,
  758. const void *h)
  759. {
  760. const PrsonaServer *server = (const PrsonaServer *) a;
  761. std::vector<std::vector<Proof>> *dst = (std::vector<std::vector<Proof>> *) b;
  762. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  763. const std::vector<std::vector<Scalar>> *permutationSeeds = (const std::vector<std::vector<Scalar>> *) d;
  764. const std::vector<std::vector<std::vector<Scalar>>> *partwayVoteMatrixSeeds = (const std::vector<std::vector<std::vector<Scalar>>> *) e;
  765. const std::vector<std::vector<TwistBipoint>> *voteMatrix = (const std::vector<std::vector<TwistBipoint>> *) f;
  766. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) g;
  767. const std::vector<std::vector<std::vector<TwistBipoint>>> *partwayVoteMatrixCommits = (const std::vector<std::vector<std::vector<TwistBipoint>>> *) h;
  768. server->generate_vote_tensor_proofs(*dst, *permutations, *permutationSeeds, *partwayVoteMatrixSeeds, *voteMatrix, *permutationCommits, *partwayVoteMatrixCommits, false);
  769. }
  770. void generate_second_half_matrix_proof_r(
  771. const void *a,
  772. void *b,
  773. const void *c,
  774. const void *d,
  775. const void *e,
  776. const void *f,
  777. const void *g,
  778. const void *h)
  779. {
  780. const PrsonaServer *server = (const PrsonaServer *) a;
  781. std::vector<std::vector<Proof>> *dst = (std::vector<std::vector<Proof>> *) b;
  782. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  783. const std::vector<std::vector<Scalar>> *permutationSeeds = (const std::vector<std::vector<Scalar>> *) d;
  784. const std::vector<std::vector<std::vector<Scalar>>> *finalVoteMatrixSeeds = (const std::vector<std::vector<std::vector<Scalar>>> *) e;
  785. const std::vector<std::vector<TwistBipoint>> *partialVoteMatrix = (const std::vector<std::vector<TwistBipoint>> *) f;
  786. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) g;
  787. const std::vector<std::vector<std::vector<TwistBipoint>>> *finalVoteMatrixCommits = (const std::vector<std::vector<std::vector<TwistBipoint>>> *) h;
  788. server->generate_vote_tensor_proofs(*dst, *permutations, *permutationSeeds, *finalVoteMatrixSeeds, *partialVoteMatrix, *permutationCommits, *finalVoteMatrixCommits, true);
  789. }
  790. void generate_user_tally_proof_r(
  791. const void *a,
  792. void *b,
  793. const void *c,
  794. const void *d,
  795. const void *e,
  796. const void *f,
  797. const void *g,
  798. const void *h,
  799. const void *i,
  800. const void *j,
  801. const void *k,
  802. const void *l,
  803. const void *m,
  804. const void *n)
  805. {
  806. const PrsonaServer *server = (const PrsonaServer *) a;
  807. std::vector<Proof> *dst = (std::vector<Proof> *) b;
  808. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  809. const Scalar *power = (const Scalar *) d;
  810. const Twistpoint *nextGenerator = (const Twistpoint *) e;
  811. const std::vector<std::vector<Scalar>> *permutationSeeds = (const std::vector<std::vector<Scalar>> *) f;
  812. const std::vector<std::vector<Scalar>> *userTallySeeds = (const std::vector<std::vector<Scalar>> *) g;
  813. const std::vector<Twistpoint> *currentPseudonyms = (const std::vector<Twistpoint> *) h;
  814. const std::vector<Twistpoint> *userTallyMasks = (const std::vector<Twistpoint> *) i;
  815. const std::vector<Twistpoint> *userTallyMessages = (const std::vector<Twistpoint> *) j;
  816. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) k;
  817. const std::vector<std::vector<Twistpoint>> *userTallyMaskCommits = (const std::vector<std::vector<Twistpoint>> *) l;
  818. const std::vector<std::vector<Twistpoint>> *userTallyMessageCommits = (const std::vector<std::vector<Twistpoint>> *) m;
  819. const std::vector<std::vector<Twistpoint>> *userTallySeedCommits = (const std::vector<std::vector<Twistpoint>> *) n;
  820. *dst = server->generate_user_tally_proofs(*permutations, *power, *nextGenerator, *permutationSeeds, *userTallySeeds, *currentPseudonyms, *userTallyMasks, *userTallyMessages, *permutationCommits, *userTallyMaskCommits, *userTallyMessageCommits, *userTallySeedCommits);
  821. }
  822. std::vector<std::vector<Proof>> PrsonaServer::epoch_calculations(
  823. std::vector<std::vector<Twistpoint>>& permutationCommits,
  824. std::vector<std::vector<Twistpoint>>& freshPseudonymCommits,
  825. std::vector<std::vector<Twistpoint>>& freshPseudonymSeedCommits,
  826. std::vector<std::vector<CurveBipoint>>& serverTallyCommits,
  827. std::vector<std::vector<std::vector<TwistBipoint>>>& partwayVoteMatrixCommits,
  828. std::vector<std::vector<std::vector<TwistBipoint>>>& finalVoteMatrixCommits,
  829. std::vector<std::vector<Twistpoint>>& userTallyMaskCommits,
  830. std::vector<std::vector<Twistpoint>>& userTallyMessageCommits,
  831. std::vector<std::vector<Twistpoint>>& userTallySeedCommits,
  832. const Scalar& power,
  833. const Twistpoint& nextGenerator,
  834. bool doUserTallies)
  835. {
  836. std::vector<std::vector<Scalar>> permutations = generate_permutation_matrix(power);
  837. // for (size_t i = 0; i < permutations.size(); i++)
  838. // {
  839. // std::cout << (i == 0 ? "[[" : " [");
  840. // for (size_t j = 0; j < permutations[i].size(); j++)
  841. // std::cout << std::hex << permutations[i][j] << std::dec << (j == permutations[i].size() - 1 ? "]" : " ");
  842. // std::cout << (i == permutations.size() - 1 ? "]" : "") << std::endl;
  843. // }
  844. std::vector<std::vector<Proof>> retval, firstHalfTensorProof, secondHalfTensorProof;
  845. CurveBipoint curveG = bgnSystem.get_public_key().get_bipoint_curvegen();
  846. CurveBipoint curveH = bgnSystem.get_public_key().get_bipoint_curve_subgroup_gen();
  847. std::vector<Proof> *individualProofs = new std::vector<Proof>[doUserTallies ? 4 : 3];
  848. std::vector<std::vector<Scalar>> permutationSeeds;
  849. std::thread permutationCommitThread(generate_permutation_commitment_r, this, &permutationCommits, &permutations, &permutationSeeds);
  850. std::vector<std::vector<Scalar>> freshPseudonymSeeds;
  851. std::thread pseudonymCommitThread(generate_pseudonym_commitment_r, this, &freshPseudonymCommits, &permutations, &power, &freshPseudonymSeeds, &freshPseudonymSeedCommits);
  852. std::vector<std::vector<Scalar>> serverTallySeeds;
  853. std::thread serverTallyCommitThread(generate_server_tally_commitment_r, this, &serverTallyCommits, &permutations, &serverTallySeeds);
  854. std::vector<std::vector<std::vector<Scalar>>> partwayVoteMatrixSeeds;
  855. std::vector<std::vector<std::vector<Scalar>>> finalVoteMatrixSeeds;
  856. std::vector<std::vector<TwistBipoint>> partialVoteMatrix;
  857. std::thread voteMatrixCommitThread(generate_matrix_commitment_r, this, &partwayVoteMatrixCommits, &partialVoteMatrix, &finalVoteMatrixCommits, &permutations, &voteMatrix, &partwayVoteMatrixSeeds, &finalVoteMatrixSeeds);
  858. std::vector<Twistpoint> userTallyMasks;
  859. std::vector<Twistpoint> userTallyMessages;
  860. std::vector<std::vector<Scalar>> userTallySeeds;
  861. std::thread *userTallyCommitThread = NULL;
  862. if (doUserTallies)
  863. userTallyCommitThread = new std::thread(generate_user_tally_commitment_r, this, &permutations, &power, &nextGenerator, &currentPseudonyms, &userTallyMasks, &userTallyMaskCommits, &userTallyMessages, &userTallyMessageCommits, &userTallySeeds, &userTallySeedCommits);
  864. permutationCommitThread.join();
  865. std::thread permutationProofThread(generate_permutation_proof_r, this, individualProofs, &permutations, &permutationSeeds, &permutationCommits);
  866. pseudonymCommitThread.join();
  867. std::thread pseudonymProofThread(generate_pseudonym_proof_r, this, individualProofs + 1, &permutations, &power, &permutationSeeds, &freshPseudonymSeeds, &currentPseudonyms, &permutationCommits, &freshPseudonymCommits, &freshPseudonymSeedCommits);
  868. serverTallyCommitThread.join();
  869. std::thread serverTallyProofThread(generate_server_tally_proof_r, this, individualProofs + 2, &permutations, &permutationSeeds, &serverTallySeeds, &previousVoteTallies, &permutationCommits, &serverTallyCommits, &curveG, &curveH);
  870. voteMatrixCommitThread.join();
  871. std::thread firstHalfProofThread(generate_first_half_matrix_proof_r, this, &firstHalfTensorProof, &permutations, &permutationSeeds, &partwayVoteMatrixSeeds, &voteMatrix, &permutationCommits, &partwayVoteMatrixCommits);
  872. std::thread secondHalfProofThread(generate_second_half_matrix_proof_r, this, &secondHalfTensorProof, &permutations, &permutationSeeds, &finalVoteMatrixSeeds, &partialVoteMatrix, &permutationCommits, &finalVoteMatrixCommits);
  873. if (userTallyCommitThread)
  874. {
  875. userTallyCommitThread->join();
  876. delete userTallyCommitThread;
  877. userTallyCommitThread = NULL;
  878. }
  879. std::thread *userTallyProofThread = NULL;
  880. if (doUserTallies)
  881. userTallyProofThread = new std::thread(generate_user_tally_proof_r, this, individualProofs + 3, &permutations, &power, &nextGenerator, &permutationSeeds, &userTallySeeds, &currentPseudonyms, &userTallyMasks, &userTallyMessages, &permutationCommits, &userTallyMaskCommits, &userTallyMessageCommits, &userTallySeedCommits);
  882. permutationProofThread.join();
  883. pseudonymProofThread.join();
  884. serverTallyProofThread.join();
  885. firstHalfProofThread.join();
  886. secondHalfProofThread.join();
  887. if (userTallyProofThread)
  888. {
  889. userTallyProofThread->join();
  890. delete userTallyProofThread;
  891. userTallyProofThread = NULL;
  892. }
  893. retval.push_back(individualProofs[0]);
  894. retval.push_back(individualProofs[1]);
  895. retval.push_back(individualProofs[2]);
  896. for (size_t i = 0; i < firstHalfTensorProof.size(); i++)
  897. retval.push_back(firstHalfTensorProof[i]);
  898. for (size_t i = 0; i < secondHalfTensorProof.size(); i++)
  899. retval.push_back(secondHalfTensorProof[i]);
  900. if (doUserTallies)
  901. retval.push_back(individualProofs[3]);
  902. delete [] individualProofs;
  903. // Replace internal values
  904. update_data(freshPseudonymCommits, serverTallyCommits, finalVoteMatrixCommits, userTallyMaskCommits, userTallyMessageCommits);
  905. return retval;
  906. }
  907. void PrsonaServer::hbc_epoch_calculations(
  908. std::vector<Twistpoint>& newFreshPseudonyms,
  909. std::vector<CurveBipoint>& newServerTallies,
  910. std::vector<std::vector<TwistBipoint>>& newVoteMatrix,
  911. std::vector<EGCiphertext>& newUserTallies,
  912. const Scalar& power,
  913. const Twistpoint& nextGenerator,
  914. bool doUserTallies)
  915. {
  916. std::vector<size_t> shuffleOrder = generate_hbc_shuffle(power);
  917. newFreshPseudonyms.clear();
  918. newServerTallies.clear();
  919. newVoteMatrix.clear();
  920. if (doUserTallies)
  921. newUserTallies.clear();
  922. for (size_t i = 0; i < shuffleOrder.size(); i++)
  923. {
  924. newFreshPseudonyms.push_back(currentPseudonyms[shuffleOrder[i]] * power);
  925. newServerTallies.push_back(bgnSystem.rerandomize(previousVoteTallies[shuffleOrder[i]]));
  926. if (doUserTallies)
  927. {
  928. Scalar r;
  929. r.set_random();
  930. EGCiphertext curr;
  931. curr.mask = currentUserEncryptedTallies[shuffleOrder[i]].mask * power +
  932. newFreshPseudonyms[i] * r;
  933. curr.encryptedMessage = currentUserEncryptedTallies[shuffleOrder[i]].encryptedMessage +
  934. nextGenerator * r;
  935. newUserTallies.push_back(curr);
  936. }
  937. }
  938. hbc_shuffle_vote_matrix(newVoteMatrix, voteMatrix, shuffleOrder);
  939. // Replace internal values
  940. hbc_update_data(newFreshPseudonyms, newServerTallies, newVoteMatrix, newUserTallies);
  941. }
  942. void PrsonaServer::hbc_shuffle_vote_matrix(
  943. std::vector<std::vector<TwistBipoint>>& newVoteMatrix,
  944. const std::vector<std::vector<TwistBipoint>>& oldVoteMatrix,
  945. const std::vector<size_t> shuffleOrder) const
  946. {
  947. for (size_t i = 0; i < shuffleOrder.size(); i++)
  948. {
  949. std::vector<TwistBipoint> currRow;
  950. for (size_t j = 0; j < shuffleOrder.size(); j++)
  951. currRow.push_back(bgnSystem.rerandomize(oldVoteMatrix[shuffleOrder[i]][shuffleOrder[j]]));
  952. newVoteMatrix.push_back(currRow);
  953. }
  954. }
  955. void verify_permutation_r(
  956. const void *a,
  957. void *b,
  958. const void *c,
  959. const void *d)
  960. {
  961. const PrsonaServer *server = (const PrsonaServer *) a;
  962. bool *dst = (bool *) b;
  963. const std::vector<Proof> *pi = (const std::vector<Proof> *) c;
  964. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) d;
  965. *dst = server->verify_valid_permutation_proof(*pi, *permutationCommits);
  966. }
  967. void verify_pseudonym_r(
  968. const void *a,
  969. void *b,
  970. const void *c,
  971. const void *d,
  972. const void *e,
  973. const void *f,
  974. const void *g)
  975. {
  976. const PrsonaServer *server = (const PrsonaServer *) a;
  977. bool *dst = (bool *) b;
  978. const std::vector<Proof> *pi = (const std::vector<Proof> *) c;
  979. const std::vector<Twistpoint> *currentPseudonyms = (const std::vector<Twistpoint> *) d;
  980. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) e;
  981. const std::vector<std::vector<Twistpoint>> *freshPseudonymCommits = (const std::vector<std::vector<Twistpoint>> *) f;
  982. const std::vector<std::vector<Twistpoint>> *freshPseudonymSeedCommits = (const std::vector<std::vector<Twistpoint>> *) g;
  983. *dst = server->verify_proof_of_reordering_plus_power(*pi, *currentPseudonyms, *permutationCommits, *freshPseudonymCommits, *freshPseudonymSeedCommits);
  984. }
  985. void verify_server_tally_r(
  986. const void *a,
  987. void *b,
  988. const void *c,
  989. const void *d,
  990. const void *e,
  991. const void *f,
  992. const void *g,
  993. const void *h)
  994. {
  995. const PrsonaServer *server = (const PrsonaServer *) a;
  996. bool *dst = (bool *) b;
  997. const std::vector<Proof> *pi = (const std::vector<Proof> *) c;
  998. const std::vector<CurveBipoint> *previousVoteTallies = (const std::vector<CurveBipoint> *) d;
  999. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) e;
  1000. const std::vector<std::vector<CurveBipoint>> *serverTallyCommits = (const std::vector<std::vector<CurveBipoint>> *) f;
  1001. const CurveBipoint *curveG = (const CurveBipoint *) g;
  1002. const CurveBipoint *curveH = (const CurveBipoint *) h;
  1003. *dst = server->verify_proof_of_reordering<CurveBipoint>(*pi, *previousVoteTallies, *permutationCommits, *serverTallyCommits, *curveG, *curveH);
  1004. }
  1005. void verify_first_half_matrix_r(
  1006. const void *a,
  1007. void *b,
  1008. const void *c,
  1009. const void *d,
  1010. const void *e,
  1011. const void *f,
  1012. const void *g)
  1013. {
  1014. const PrsonaServer *server = (const PrsonaServer *) a;
  1015. bool *dst = (bool *) b;
  1016. const std::vector<std::vector<Proof>> *pi = (const std::vector<std::vector<Proof>> *) c;
  1017. const size_t *currOffset = (const size_t *) d;
  1018. const std::vector<std::vector<TwistBipoint>> *voteMatrix = (const std::vector<std::vector<TwistBipoint>> *) e;
  1019. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) f;
  1020. const std::vector<std::vector<std::vector<TwistBipoint>>> *partwayVoteMatrixCommits = (const std::vector<std::vector<std::vector<TwistBipoint>>> *) g;
  1021. *dst = server->verify_vote_tensor_proofs(*pi, *currOffset, *voteMatrix, *permutationCommits, *partwayVoteMatrixCommits, false);
  1022. }
  1023. void verify_second_half_matrix_r(
  1024. const void *a,
  1025. void *b,
  1026. const void *c,
  1027. const void *d,
  1028. const void *e,
  1029. const void *f,
  1030. const void *g)
  1031. {
  1032. const PrsonaServer *server = (const PrsonaServer *) a;
  1033. bool *dst = (bool *) b;
  1034. const std::vector<std::vector<Proof>> *pi = (const std::vector<std::vector<Proof>> *) c;
  1035. const size_t *currOffset = (const size_t *) d;
  1036. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) e;
  1037. const std::vector<std::vector<std::vector<TwistBipoint>>> *partwayVoteMatrixCommits = (const std::vector<std::vector<std::vector<TwistBipoint>>> *) f;
  1038. const std::vector<std::vector<std::vector<TwistBipoint>>> *finalVoteMatrixCommits = (const std::vector<std::vector<std::vector<TwistBipoint>>> *) g;
  1039. std::vector<std::vector<TwistBipoint>> partialVoteMatrix = server->calculate_next_vote_matrix(*partwayVoteMatrixCommits);
  1040. *dst = server->verify_vote_tensor_proofs(*pi, *currOffset, partialVoteMatrix, *permutationCommits, *finalVoteMatrixCommits, true);
  1041. }
  1042. void verify_user_tally_r(
  1043. const void *a,
  1044. void *b,
  1045. const void *c,
  1046. const void *d,
  1047. const void *e,
  1048. const void *f,
  1049. const void *g,
  1050. const void *h,
  1051. const void *i,
  1052. const void *j)
  1053. {
  1054. const PrsonaServer *server = (const PrsonaServer *) a;
  1055. bool *dst = (bool *) b;
  1056. const std::vector<Proof> *pi = (const std::vector<Proof> *) c;
  1057. const Twistpoint *nextGenerator = (const Twistpoint *) d;
  1058. const std::vector<Twistpoint> *currentPseudonyms = (const std::vector<Twistpoint> *) e;
  1059. const std::vector<EGCiphertext> *currentUserEncryptedTallies = (const std::vector<EGCiphertext> *) f;
  1060. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) g;
  1061. const std::vector<std::vector<Twistpoint>> *userTallyMaskCommits = (const std::vector<std::vector<Twistpoint>> *) h;
  1062. const std::vector<std::vector<Twistpoint>> *userTallyMessageCommits = (const std::vector<std::vector<Twistpoint>> *) i;
  1063. const std::vector<std::vector<Twistpoint>> *userTallySeedCommits = (const std::vector<std::vector<Twistpoint>> *) j;
  1064. std::vector<Twistpoint> userTallyMasks;
  1065. std::vector<Twistpoint> userTallyMessages;
  1066. for (size_t i = 0; i < currentUserEncryptedTallies->size(); i++)
  1067. {
  1068. userTallyMasks.push_back((*currentUserEncryptedTallies)[i].mask);
  1069. userTallyMessages.push_back((*currentUserEncryptedTallies)[i].encryptedMessage);
  1070. }
  1071. *dst = server->verify_user_tally_proofs(*pi, *nextGenerator, *currentPseudonyms, userTallyMasks, userTallyMessages, *permutationCommits, *userTallyMaskCommits, *userTallyMessageCommits, *userTallySeedCommits);
  1072. }
  1073. bool PrsonaServer::accept_epoch_updates(
  1074. const std::vector<std::vector<Proof>>& pi,
  1075. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1076. const std::vector<std::vector<Twistpoint>>& freshPseudonymCommits,
  1077. const std::vector<std::vector<Twistpoint>>& freshPseudonymSeedCommits,
  1078. const std::vector<std::vector<CurveBipoint>>& serverTallyCommits,
  1079. const std::vector<std::vector<std::vector<TwistBipoint>>>& partwayVoteMatrixCommits,
  1080. const std::vector<std::vector<std::vector<TwistBipoint>>>& finalVoteMatrixCommits,
  1081. const std::vector<std::vector<Twistpoint>>& userTallyMaskCommits,
  1082. const std::vector<std::vector<Twistpoint>>& userTallyMessageCommits,
  1083. const std::vector<std::vector<Twistpoint>>& userTallySeedCommits,
  1084. const Twistpoint& nextGenerator,
  1085. bool doUserTallies)
  1086. {
  1087. bool permutationVerification = false;
  1088. bool pseudonymVerification = false;
  1089. bool serverTallyVerification = false;
  1090. bool firstHalfVerification = false;
  1091. bool secondHalfVerification = false;
  1092. bool userTallyVerification = true;
  1093. if ((userTallyMaskCommits.empty() && doUserTallies) || (!userTallyMaskCommits.empty() && !doUserTallies))
  1094. {
  1095. std::cerr << "User tallies are not in expected state." << std::endl;
  1096. return false;
  1097. }
  1098. if (pi.empty())
  1099. return false;
  1100. size_t currOffset = 0;
  1101. std::thread permutationThread(verify_permutation_r, this, &permutationVerification, &(pi[currOffset]), &permutationCommits);
  1102. currOffset++;
  1103. std::thread pseudonymThread(verify_pseudonym_r, this, &pseudonymVerification, &(pi[currOffset]), &currentPseudonyms, &permutationCommits, &freshPseudonymCommits, &freshPseudonymSeedCommits);
  1104. currOffset++;
  1105. CurveBipoint curveG = bgnSystem.get_public_key().get_bipoint_curvegen();
  1106. CurveBipoint curveH = bgnSystem.get_public_key().get_bipoint_curve_subgroup_gen();
  1107. std::thread serverTallyThread(verify_server_tally_r, this, &serverTallyVerification, &(pi[currOffset]), &previousVoteTallies, &permutationCommits, &serverTallyCommits, &curveG, &curveH);
  1108. currOffset++;
  1109. size_t firstHalfOffset = currOffset;
  1110. std::thread firstHalfThread(verify_first_half_matrix_r, this, &firstHalfVerification, &pi, &firstHalfOffset, &voteMatrix, &permutationCommits, &partwayVoteMatrixCommits);
  1111. currOffset += voteMatrix.size();
  1112. size_t secondHalfOffset = currOffset;
  1113. std::thread secondHalfThread(verify_second_half_matrix_r, this, &secondHalfVerification, &pi, &secondHalfOffset, &permutationCommits, &partwayVoteMatrixCommits, &finalVoteMatrixCommits);
  1114. currOffset += voteMatrix.size();
  1115. std::thread *userTallyThread = NULL;
  1116. if (doUserTallies)
  1117. userTallyThread = new std::thread(verify_user_tally_r, this, &userTallyVerification, &(pi[currOffset]), &nextGenerator, &currentPseudonyms, &currentUserEncryptedTallies, &permutationCommits, &userTallyMaskCommits, &userTallyMessageCommits, &userTallySeedCommits);
  1118. currOffset++;
  1119. permutationThread.join();
  1120. pseudonymThread.join();
  1121. serverTallyThread.join();
  1122. firstHalfThread.join();
  1123. secondHalfThread.join();
  1124. if (userTallyThread)
  1125. {
  1126. userTallyThread->join();
  1127. delete userTallyThread;
  1128. userTallyThread = NULL;
  1129. }
  1130. if (!permutationVerification)
  1131. {
  1132. std::cerr << "Could not verify valid permutation matrix." << std::endl;
  1133. return false;
  1134. }
  1135. if (!pseudonymVerification)
  1136. {
  1137. std::cerr << "Could not verify valid pseudonym vector." << std::endl;
  1138. return false;
  1139. }
  1140. if (!serverTallyVerification)
  1141. {
  1142. std::cerr << "Could not verify valid server tally vector." << std::endl;
  1143. return false;
  1144. }
  1145. if (!userTallyVerification)
  1146. {
  1147. std::cerr << "Could not verify user tallies." << std::endl;
  1148. return false;
  1149. }
  1150. if (!firstHalfVerification)
  1151. {
  1152. std::cerr << "Could not verify first half vote matrix." << std::endl;
  1153. return false;
  1154. }
  1155. if (!secondHalfVerification)
  1156. {
  1157. std::cerr << "Could not verify second half vote matrix." << std::endl;
  1158. return false;
  1159. }
  1160. bool finalVerification = update_data(freshPseudonymCommits, serverTallyCommits, finalVoteMatrixCommits, userTallyMaskCommits, userTallyMessageCommits);
  1161. return finalVerification;
  1162. }
  1163. bool PrsonaServer::hbc_accept_epoch_updates(
  1164. const std::vector<Twistpoint>& newFreshPseudonyms,
  1165. const std::vector<CurveBipoint>& newServerTallies,
  1166. const std::vector<std::vector<TwistBipoint>>& newVoteMatrix,
  1167. const std::vector<EGCiphertext>& newUserTallies,
  1168. bool doUserTallies)
  1169. {
  1170. if ((newUserTallies.empty() && doUserTallies) || (!newUserTallies.empty() && !doUserTallies))
  1171. {
  1172. std::cerr << "User tallies are not in expected state." << std::endl;
  1173. return false;
  1174. }
  1175. return hbc_update_data(newFreshPseudonyms, newServerTallies, newVoteMatrix, newUserTallies);
  1176. }
  1177. std::vector<std::vector<Scalar>> PrsonaServer::generate_permutation_matrix(
  1178. const Scalar& reorderSeed) const
  1179. {
  1180. std::vector<std::vector<Scalar>> retval;
  1181. for (size_t i = 0; i < currentPseudonyms.size(); i++)
  1182. {
  1183. std::vector<Scalar> currRow;
  1184. for (size_t j = 0; j < currentPseudonyms.size(); j++)
  1185. currRow.push_back(Scalar(0));
  1186. retval.push_back(currRow);
  1187. }
  1188. std::vector<Twistpoint> nextPseudonyms;
  1189. for (size_t i = 0; i < currentPseudonyms.size(); i++)
  1190. nextPseudonyms.push_back(currentPseudonyms[i] * reorderSeed);
  1191. std::vector<size_t> order = sort_data(nextPseudonyms);
  1192. for (size_t i = 0; i < order.size(); i++)
  1193. retval[order[i]][i] = Scalar(1);
  1194. return retval;
  1195. }
  1196. std::vector<size_t> PrsonaServer::generate_hbc_shuffle(
  1197. const Scalar& reorderSeed) const
  1198. {
  1199. std::vector<Twistpoint> nextPseudonyms;
  1200. for (size_t i = 0; i < currentPseudonyms.size(); i++)
  1201. nextPseudonyms.push_back(currentPseudonyms[i] * reorderSeed);
  1202. return sort_data(nextPseudonyms);
  1203. }
  1204. std::vector<std::vector<Twistpoint>> PrsonaServer::generate_commitment_matrix(
  1205. const std::vector<std::vector<Scalar>>& permutations,
  1206. std::vector<std::vector<Scalar>>& seeds) const
  1207. {
  1208. std::vector<std::vector<Twistpoint>> retval;
  1209. Twistpoint g = EL_GAMAL_GENERATOR;
  1210. Twistpoint h = elGamalBlindGenerator;
  1211. seeds.clear();
  1212. for (size_t i = 0; i < permutations.size(); i++)
  1213. {
  1214. std::vector<Scalar> currSeeds;
  1215. for (size_t j = 0; j < permutations[i].size(); j++)
  1216. currSeeds.push_back(Scalar(0));
  1217. seeds.push_back(currSeeds);
  1218. }
  1219. for (size_t i = 0; i < permutations.size(); i++)
  1220. {
  1221. std::vector<Twistpoint> currRow;
  1222. size_t last = permutations[i].size() - 1;
  1223. for (size_t j = 0; j < permutations[i].size(); j++)
  1224. {
  1225. Twistpoint element;
  1226. if (j != last)
  1227. {
  1228. seeds[i][j].set_random();
  1229. seeds[i][last] = seeds[i][last] - seeds[i][j];
  1230. }
  1231. element = g * permutations[i][j] + h * seeds[i][j];
  1232. currRow.push_back(element);
  1233. }
  1234. retval.push_back(currRow);
  1235. }
  1236. return retval;
  1237. }
  1238. std::vector<std::vector<Twistpoint>> PrsonaServer::generate_pseudonym_matrix(
  1239. const std::vector<std::vector<Scalar>>& permutations,
  1240. const Scalar& power,
  1241. std::vector<std::vector<Scalar>>& seeds,
  1242. std::vector<std::vector<Twistpoint>>& seedCommits) const
  1243. {
  1244. return generate_reordered_plus_power_matrix<Twistpoint>(permutations, power, currentPseudonyms, seeds, seedCommits, elGamalBlindGenerator);
  1245. }
  1246. std::vector<std::vector<CurveBipoint>> PrsonaServer::generate_server_tally_matrix(
  1247. const std::vector<std::vector<Scalar>>& permutations,
  1248. std::vector<std::vector<Scalar>>& seeds) const
  1249. {
  1250. return generate_reordered_matrix<CurveBipoint>(permutations, previousVoteTallies, seeds, bgnSystem.get_public_key().get_bipoint_curve_subgroup_gen(), false);
  1251. }
  1252. std::vector<std::vector<std::vector<TwistBipoint>>> PrsonaServer::generate_vote_tensor(
  1253. const std::vector<std::vector<Scalar>>& permutations,
  1254. const std::vector<std::vector<TwistBipoint>>& currVoteMatrix,
  1255. std::vector<std::vector<std::vector<Scalar>>>& seeds,
  1256. bool inverted) const
  1257. {
  1258. std::vector<std::vector<std::vector<TwistBipoint>>> retval;
  1259. for (size_t i = 0; i < currVoteMatrix.size(); i++)
  1260. {
  1261. std::vector<std::vector<Scalar>> currSeeds;
  1262. std::vector<TwistBipoint> inputRow;
  1263. if (inverted)
  1264. {
  1265. for (size_t j = 0; j < currVoteMatrix.size(); j++)
  1266. inputRow.push_back(currVoteMatrix[j][i]);
  1267. }
  1268. else
  1269. {
  1270. inputRow = currVoteMatrix[i];
  1271. }
  1272. retval.push_back(generate_reordered_matrix<TwistBipoint>(permutations, inputRow, currSeeds, bgnSystem.get_public_key().get_bipoint_twist_subgroup_gen(), false));
  1273. seeds.push_back(currSeeds);
  1274. }
  1275. return retval;
  1276. }
  1277. std::vector<std::vector<TwistBipoint>> PrsonaServer::calculate_next_vote_matrix(
  1278. const std::vector<std::vector<std::vector<TwistBipoint>>>& voteTensor) const
  1279. {
  1280. std::vector<std::vector<TwistBipoint>> retval;
  1281. for (size_t i = 0; i < voteTensor.size(); i++)
  1282. {
  1283. std::vector<TwistBipoint> currRow;
  1284. for (size_t j = 0; j < voteTensor[i].size(); j++)
  1285. {
  1286. TwistBipoint sum = voteTensor[i][j][0];
  1287. for (size_t k = 1; k < voteTensor[i][j].size(); k++)
  1288. sum = sum + voteTensor[i][j][k];
  1289. currRow.push_back(sum);
  1290. }
  1291. retval.push_back(currRow);
  1292. }
  1293. return retval;
  1294. }
  1295. void generate_tensor_r(
  1296. const void *a,
  1297. void *b,
  1298. const void *c,
  1299. const void *d,
  1300. const void *e,
  1301. const void *f,
  1302. const void *g,
  1303. const void *h,
  1304. const void *i,
  1305. const void *j)
  1306. {
  1307. const PrsonaServer *server = (const PrsonaServer *) a;
  1308. std::vector<Proof> *dst = (std::vector<Proof> *) b;
  1309. const std::vector<std::vector<Scalar>> *permutations = (const std::vector<std::vector<Scalar>> *) c;
  1310. const std::vector<std::vector<Scalar>> *permutationSeeds = (const std::vector<std::vector<Scalar>> *) d;
  1311. const std::vector<std::vector<Scalar>> *matrixSeeds = (const std::vector<std::vector<Scalar>> *) e;
  1312. const std::vector<TwistBipoint> *inputRow = (const std::vector<TwistBipoint> *) f;
  1313. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) g;
  1314. const std::vector<std::vector<TwistBipoint>> *matrixCommits = (const std::vector<std::vector<TwistBipoint>> *) h;
  1315. const TwistBipoint *twistG = (const TwistBipoint *) i;
  1316. const TwistBipoint *twistH = (const TwistBipoint *) j;
  1317. *dst = server->generate_proof_of_reordering<TwistBipoint>(*permutations, *permutationSeeds, *matrixSeeds, *inputRow, *permutationCommits, *matrixCommits, *twistG, *twistH);
  1318. }
  1319. void PrsonaServer::generate_vote_tensor_proofs(
  1320. std::vector<std::vector<Proof>>& pi,
  1321. const std::vector<std::vector<Scalar>>& permutations,
  1322. const std::vector<std::vector<Scalar>>& permutationSeeds,
  1323. const std::vector<std::vector<std::vector<Scalar>>>& matrixSeeds,
  1324. const std::vector<std::vector<TwistBipoint>>& currMatrix,
  1325. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1326. const std::vector<std::vector<std::vector<TwistBipoint>>>& matrixCommits,
  1327. bool inverted) const
  1328. {
  1329. TwistBipoint twistG = bgnSystem.get_public_key().get_bipoint_twistgen();
  1330. TwistBipoint twistH = bgnSystem.get_public_key().get_bipoint_twist_subgroup_gen();
  1331. std::vector<Proof> *intermediaryProofs = new std::vector<Proof>[currMatrix.size()];
  1332. std::vector<TwistBipoint> *inputRows = new std::vector<TwistBipoint>[currMatrix.size()];
  1333. std::vector<std::thread> individualTensorProofThreads;
  1334. for (size_t i = 0; i < currMatrix.size(); i++)
  1335. {
  1336. std::vector<TwistBipoint> currRow;
  1337. if (inverted)
  1338. {
  1339. for (size_t j = 0; j < currMatrix.size(); j++)
  1340. currRow.push_back(currMatrix[j][i]);
  1341. }
  1342. else
  1343. {
  1344. currRow = currMatrix[i];
  1345. }
  1346. inputRows[i] = currRow;
  1347. individualTensorProofThreads.push_back(std::thread(generate_tensor_r, this, intermediaryProofs + i, &permutations, &permutationSeeds, &(matrixSeeds[i]), inputRows + i, &permutationCommits, &(matrixCommits[i]), &twistG, &twistH));
  1348. }
  1349. for (size_t i = 0; i < individualTensorProofThreads.size(); i++)
  1350. {
  1351. individualTensorProofThreads[i].join();
  1352. pi.push_back(intermediaryProofs[i]);
  1353. }
  1354. delete [] inputRows;
  1355. delete [] intermediaryProofs;
  1356. }
  1357. void verify_tensor_r(
  1358. const void *a,
  1359. void *b,
  1360. const void *c,
  1361. const void *d,
  1362. const void *e,
  1363. const void *f,
  1364. const void *g,
  1365. const void *h)
  1366. {
  1367. const PrsonaServer *server = (const PrsonaServer *) a;
  1368. bool *dst = (bool *) b;
  1369. const std::vector<Proof> *pi = (const std::vector<Proof> *) c;
  1370. const std::vector<TwistBipoint> *inputRow = (const std::vector<TwistBipoint> *) d;
  1371. const std::vector<std::vector<Twistpoint>> *permutationCommits = (const std::vector<std::vector<Twistpoint>> *) e;
  1372. const std::vector<std::vector<TwistBipoint>> *matrixCommits = (const std::vector<std::vector<TwistBipoint>> *) f;
  1373. const TwistBipoint *twistG = (const TwistBipoint *) g;
  1374. const TwistBipoint *twistH = (const TwistBipoint *) h;
  1375. *dst = server->verify_proof_of_reordering<TwistBipoint>(*pi, *inputRow, *permutationCommits, *matrixCommits, *twistG, *twistH);
  1376. }
  1377. bool PrsonaServer::verify_vote_tensor_proofs(
  1378. const std::vector<std::vector<Proof>>& pi,
  1379. size_t start_offset,
  1380. const std::vector<std::vector<TwistBipoint>>& currMatrix,
  1381. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1382. const std::vector<std::vector<std::vector<TwistBipoint>>>& matrixCommits,
  1383. bool inverted) const
  1384. {
  1385. TwistBipoint twistG = bgnSystem.get_public_key().get_bipoint_twistgen();
  1386. TwistBipoint twistH = bgnSystem.get_public_key().get_bipoint_twist_subgroup_gen();
  1387. bool *intermediaryValues = new bool[currMatrix.size()];
  1388. std::vector<TwistBipoint> *inputRows = new std::vector<TwistBipoint>[currMatrix.size()];
  1389. std::vector<std::thread> individualTensorProofThreads;
  1390. for (size_t i = 0; i < currMatrix.size(); i++)
  1391. {
  1392. std::vector<TwistBipoint> currRow;
  1393. if (inverted)
  1394. {
  1395. for (size_t j = 0; j < currMatrix.size(); j++)
  1396. currRow.push_back(currMatrix[j][i]);
  1397. }
  1398. else
  1399. {
  1400. currRow = currMatrix[i];
  1401. }
  1402. inputRows[i] = currRow;
  1403. size_t whichProof = i + start_offset;
  1404. individualTensorProofThreads.push_back(std::thread(verify_tensor_r, this, intermediaryValues + i, &(pi[whichProof]), inputRows + i, &permutationCommits, &(matrixCommits[i]), &twistG, &twistH));
  1405. }
  1406. bool retval = true;
  1407. for (size_t i = 0; i < individualTensorProofThreads.size(); i++)
  1408. {
  1409. individualTensorProofThreads[i].join();
  1410. retval = retval && intermediaryValues[i];
  1411. }
  1412. delete [] inputRows;
  1413. delete [] intermediaryValues;
  1414. return retval;
  1415. }
  1416. void PrsonaServer::generate_user_tally_matrix(
  1417. const std::vector<std::vector<Scalar>>& permutations,
  1418. const Scalar& power,
  1419. const Twistpoint& nextGenerator,
  1420. const std::vector<Twistpoint>& currPseudonyms,
  1421. std::vector<Twistpoint>& masks,
  1422. std::vector<std::vector<Twistpoint>>& maskCommits,
  1423. std::vector<Twistpoint>& messages,
  1424. std::vector<std::vector<Twistpoint>>& messageCommits,
  1425. std::vector<std::vector<Scalar>>& userTallySeeds,
  1426. std::vector<std::vector<Twistpoint>>& userTallySeedCommits) const
  1427. {
  1428. masks.clear();
  1429. messages.clear();
  1430. for (size_t i = 0; i < currentUserEncryptedTallies.size(); i++)
  1431. {
  1432. masks.push_back(currentUserEncryptedTallies[i].mask);
  1433. messages.push_back(currentUserEncryptedTallies[i].encryptedMessage);
  1434. }
  1435. maskCommits.clear();
  1436. messageCommits.clear();
  1437. userTallySeeds.clear();
  1438. userTallySeedCommits.clear();
  1439. for (size_t i = 0; i < permutations.size(); i++)
  1440. {
  1441. std::vector<Scalar> currSeeds;
  1442. std::vector<Twistpoint> currRow;
  1443. for (size_t j = 0; j < permutations[i].size(); j++)
  1444. {
  1445. currSeeds.push_back(Scalar(0));
  1446. currRow.push_back(Twistpoint());
  1447. }
  1448. userTallySeeds.push_back(currSeeds);
  1449. maskCommits.push_back(currRow);
  1450. messageCommits.push_back(currRow);
  1451. userTallySeedCommits.push_back(currRow);
  1452. }
  1453. for (size_t i = 0; i < permutations.size(); i++)
  1454. {
  1455. size_t last = permutations[i].size() - 1;
  1456. for (size_t j = 0; j < permutations[i].size(); j++)
  1457. {
  1458. if (j != last)
  1459. {
  1460. userTallySeeds[i][j].set_random();
  1461. userTallySeeds[i][last] = userTallySeeds[i][last] - userTallySeeds[i][j];
  1462. }
  1463. maskCommits[i][j] = masks[j] * permutations[j][i] * power +
  1464. currPseudonyms[j] * power * permutations[j][i] * userTallySeeds[i][j] +
  1465. elGamalBlindGenerator * userTallySeeds[i][j];
  1466. messageCommits[i][j] = messages[j] * permutations[j][i] +
  1467. nextGenerator * permutations[j][i] * userTallySeeds[i][j] +
  1468. elGamalBlindGenerator * userTallySeeds[i][j];
  1469. userTallySeedCommits[i][j] = EL_GAMAL_GENERATOR * userTallySeeds[i][j];
  1470. }
  1471. }
  1472. }
  1473. template <typename T>
  1474. std::vector<std::vector<T>> PrsonaServer::generate_reordered_plus_power_matrix(
  1475. const std::vector<std::vector<Scalar>>& permutations,
  1476. const Scalar& power,
  1477. const std::vector<T>& oldValues,
  1478. std::vector<std::vector<Scalar>>& seeds,
  1479. std::vector<std::vector<Twistpoint>>& seedCommits,
  1480. const T& h) const
  1481. {
  1482. std::vector<std::vector<Scalar>> permutation_plus_power;
  1483. seedCommits.clear();
  1484. for (size_t i = 0; i < permutations.size(); i++)
  1485. {
  1486. std::vector<Scalar> currPermutations;
  1487. std::vector<Twistpoint> currSeedCommits;
  1488. for (size_t j = 0; j < permutations[i].size(); j++)
  1489. {
  1490. currPermutations.push_back(permutations[i][j] * power);
  1491. currSeedCommits.push_back(Twistpoint());
  1492. }
  1493. permutation_plus_power.push_back(currPermutations);
  1494. seedCommits.push_back(currSeedCommits);
  1495. }
  1496. std::vector<std::vector<T>> retval = generate_reordered_matrix<T>(permutation_plus_power, oldValues, seeds, h, true);
  1497. for (size_t i = 0; i < permutations.size(); i++)
  1498. for (size_t j = 0; j < permutations[i].size(); j++)
  1499. seedCommits[i][j] = EL_GAMAL_GENERATOR * seeds[i][j];
  1500. return retval;
  1501. }
  1502. template <typename T>
  1503. std::vector<std::vector<T>> PrsonaServer::generate_reordered_matrix(
  1504. const std::vector<std::vector<Scalar>>& permutations,
  1505. const std::vector<T>& oldValues,
  1506. std::vector<std::vector<Scalar>>& seeds,
  1507. const T& h,
  1508. bool cancelOut) const
  1509. {
  1510. std::vector<std::vector<T>> retval;
  1511. seeds.clear();
  1512. for (size_t i = 0; i < permutations.size(); i++)
  1513. {
  1514. std::vector<Scalar> currSeeds;
  1515. std::vector<T> currRow;
  1516. for (size_t j = 0; j < permutations[i].size(); j++)\
  1517. {
  1518. currSeeds.push_back(Scalar(0));
  1519. currRow.push_back(T());
  1520. }
  1521. seeds.push_back(currSeeds);
  1522. retval.push_back(currRow);
  1523. }
  1524. for (size_t i = 0; i < permutations.size(); i++)
  1525. {
  1526. size_t last = permutations[i].size() - 1;
  1527. for (size_t j = 0; j < permutations[i].size(); j++)
  1528. {
  1529. if (!cancelOut)
  1530. {
  1531. seeds[i][j].set_random();
  1532. }
  1533. else if (j != last)
  1534. {
  1535. seeds[i][j].set_random();
  1536. seeds[i][last] = seeds[i][last] - seeds[i][j];
  1537. }
  1538. retval[i][j] = oldValues[j] * permutations[j][i] + h * seeds[i][j];
  1539. }
  1540. }
  1541. return retval;
  1542. }
  1543. std::vector<size_t> PrsonaServer::sort_data(
  1544. const std::vector<Twistpoint>& inputs) const
  1545. {
  1546. std::vector<size_t> retval;
  1547. // SortingType's index member allows us to replicate the "sort" across
  1548. std::vector<SortingType> sortTracker;
  1549. for (size_t i = 0; i < inputs.size(); i++)
  1550. {
  1551. SortingType curr;
  1552. curr.pseudonym = inputs[i];
  1553. curr.index = i;
  1554. sortTracker.push_back(curr);
  1555. }
  1556. std::sort(sortTracker.begin(), sortTracker.end());
  1557. for (size_t i = 0; i < inputs.size(); i++)
  1558. retval.push_back(sortTracker[i].index);
  1559. return retval;
  1560. }
  1561. bool PrsonaServer::update_data(
  1562. const std::vector<std::vector<Twistpoint>>& freshPseudonymCommits,
  1563. const std::vector<std::vector<CurveBipoint>>& serverTallyCommits,
  1564. const std::vector<std::vector<std::vector<TwistBipoint>>>& voteMatrixCommits,
  1565. const std::vector<std::vector<Twistpoint>>& userTallyMaskCommits,
  1566. const std::vector<std::vector<Twistpoint>>& userTallyMessageCommits)
  1567. {
  1568. std::vector<Twistpoint> newPseudonyms;
  1569. std::vector<CurveBipoint> newVoteTallies;
  1570. std::vector<EGCiphertext> newUserTallies;
  1571. for (size_t i = 0; i < freshPseudonymCommits.size(); i++)
  1572. {
  1573. Twistpoint pseudonymSum = freshPseudonymCommits[i][0];
  1574. CurveBipoint voteTallySum = serverTallyCommits[i][0];
  1575. Twistpoint userTallyMask, userTallyMessage;
  1576. if (!userTallyMaskCommits.empty())
  1577. {
  1578. userTallyMask = userTallyMaskCommits[i][0];
  1579. userTallyMessage = userTallyMessageCommits[i][0];
  1580. }
  1581. for (size_t j = 1; j < freshPseudonymCommits[i].size(); j++)
  1582. {
  1583. pseudonymSum = pseudonymSum + freshPseudonymCommits[i][j];
  1584. voteTallySum = voteTallySum + serverTallyCommits[i][j];
  1585. if (!userTallyMaskCommits.empty())
  1586. {
  1587. userTallyMask = userTallyMask + userTallyMaskCommits[i][j];
  1588. userTallyMessage = userTallyMessage + userTallyMessageCommits[i][j];
  1589. }
  1590. }
  1591. newPseudonyms.push_back(pseudonymSum);
  1592. newVoteTallies.push_back(voteTallySum);
  1593. if (!userTallyMaskCommits.empty())
  1594. newUserTallies.push_back(EGCiphertext(userTallyMask, userTallyMessage));
  1595. }
  1596. if (!pseudonyms_sorted(newPseudonyms))
  1597. {
  1598. std::cerr << "Pseudonyms not sorted correctly." << std::endl;
  1599. return false;
  1600. }
  1601. currentPseudonyms = newPseudonyms;
  1602. previousVoteTallies = newVoteTallies;
  1603. voteMatrix = calculate_next_vote_matrix(voteMatrixCommits);
  1604. currentUserEncryptedTallies = newUserTallies;
  1605. return true;
  1606. }
  1607. bool PrsonaServer::hbc_update_data(
  1608. const std::vector<Twistpoint>& newFreshPseudonyms,
  1609. const std::vector<CurveBipoint>& newServerTallies,
  1610. const std::vector<std::vector<TwistBipoint>>& newVoteMatrix,
  1611. const std::vector<EGCiphertext>& newUserTallies)
  1612. {
  1613. if (!pseudonyms_sorted(newFreshPseudonyms))
  1614. {
  1615. std::cerr << "Pseudonyms not sorted correctly." << std::endl;
  1616. return false;
  1617. }
  1618. currentPseudonyms = newFreshPseudonyms;
  1619. previousVoteTallies = newServerTallies;
  1620. voteMatrix = newVoteMatrix;
  1621. currentUserEncryptedTallies = newUserTallies;
  1622. return true;
  1623. }
  1624. bool PrsonaServer::pseudonyms_sorted(
  1625. const std::vector<Twistpoint> newPseudonyms) const
  1626. {
  1627. bool retval = true;
  1628. for (size_t i = 0; i < newPseudonyms.size() - 1; i++)
  1629. retval = retval && (newPseudonyms[i] < newPseudonyms[i + 1]);
  1630. return retval;
  1631. }
  1632. /*
  1633. * DATA MAINTENANCE
  1634. */
  1635. void PrsonaServer::export_new_user_update(
  1636. std::vector<CurveBipoint>& otherPreviousVoteTallies,
  1637. std::vector<Twistpoint>& otherCurrentPseudonyms,
  1638. std::vector<EGCiphertext>& otherCurrentUserEncryptedTallies,
  1639. std::vector<std::vector<TwistBipoint>>& otherVoteMatrix) const
  1640. {
  1641. otherPreviousVoteTallies = previousVoteTallies;
  1642. otherCurrentPseudonyms = currentPseudonyms;
  1643. otherCurrentUserEncryptedTallies = currentUserEncryptedTallies;
  1644. otherVoteMatrix = voteMatrix;
  1645. }
  1646. bool PrsonaServer::import_new_user_update(
  1647. const std::vector<Proof>& pi,
  1648. const std::vector<CurveBipoint>& otherPreviousVoteTallies,
  1649. const std::vector<Twistpoint>& otherCurrentPseudonyms,
  1650. const std::vector<EGCiphertext>& otherCurrentUserEncryptedTallies,
  1651. const std::vector<std::vector<TwistBipoint>>& otherVoteMatrix)
  1652. {
  1653. size_t newIndex = 0;
  1654. if (!currentPseudonyms.empty())
  1655. while (otherCurrentPseudonyms[newIndex] == currentPseudonyms[newIndex])
  1656. newIndex++;
  1657. Twistpoint shortTermPublicKey = otherCurrentPseudonyms[newIndex];
  1658. 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);
  1659. if (!flag)
  1660. {
  1661. std::cerr << "Other server added new user invalidly, aborting." << std::endl;
  1662. return false;
  1663. }
  1664. for (size_t i = 0; i < otherCurrentPseudonyms.size(); i++)
  1665. {
  1666. if (i == newIndex)
  1667. continue;
  1668. size_t otherI = (i > newIndex ? i - 1 : i);
  1669. flag = flag && otherCurrentPseudonyms[i] == currentPseudonyms[otherI];
  1670. flag = flag && otherCurrentUserEncryptedTallies[i] == currentUserEncryptedTallies[otherI];
  1671. flag = flag && otherPreviousVoteTallies[i] == previousVoteTallies[otherI];
  1672. for (size_t j = 0; j < otherCurrentPseudonyms.size(); j++)
  1673. {
  1674. if (j == newIndex)
  1675. continue;
  1676. size_t otherJ = (j > newIndex ? j - 1 : j);
  1677. flag = flag && otherVoteMatrix[i][j] == voteMatrix[otherI][otherJ];
  1678. }
  1679. }
  1680. if (!flag)
  1681. {
  1682. std::cerr << "Other server illicitly changed other value during new user add." << std::endl;
  1683. return false;
  1684. }
  1685. previousVoteTallies = otherPreviousVoteTallies;
  1686. currentPseudonyms = otherCurrentPseudonyms;
  1687. currentUserEncryptedTallies = otherCurrentUserEncryptedTallies;
  1688. voteMatrix = otherVoteMatrix;
  1689. return true;
  1690. }
  1691. /*
  1692. * DATA SAFEKEEPING
  1693. */
  1694. /* This is what powers the "shuffle"; really, as pseudonyms get updated,
  1695. * the pseudonyms are no longer in the order prescribed by operator<().
  1696. * So, we put them (and everything else) back into that order,
  1697. * effectively shuffling them (and making lookups easier later on). */
  1698. std::vector<size_t> PrsonaServer::order_data()
  1699. {
  1700. std::vector<size_t> retval = sort_data(currentPseudonyms);
  1701. // Order all other data in the same way, for consistency
  1702. std::vector<Twistpoint> newPseudonyms;
  1703. std::vector<CurveBipoint> newVoteTallies;
  1704. std::vector<EGCiphertext> newUserEncryptedTallies;
  1705. std::vector<std::vector<TwistBipoint>> newVoteMatrix;
  1706. for (size_t i = 0; i < retval.size(); i++)
  1707. {
  1708. newPseudonyms.push_back(currentPseudonyms[retval[i]]);
  1709. newVoteTallies.push_back(previousVoteTallies[retval[i]]);
  1710. if (!currentUserEncryptedTallies.empty())
  1711. newUserEncryptedTallies.push_back(currentUserEncryptedTallies[retval[i]]);
  1712. std::vector<TwistBipoint> currNewRow;
  1713. for (size_t j = 0; j < currentPseudonyms.size(); j++)
  1714. currNewRow.push_back(voteMatrix[retval[i]][retval[j]]);
  1715. newVoteMatrix.push_back(currNewRow);
  1716. }
  1717. previousVoteTallies = newVoteTallies;
  1718. currentPseudonyms = newPseudonyms;
  1719. currentUserEncryptedTallies = newUserEncryptedTallies;
  1720. voteMatrix = newVoteMatrix;
  1721. return retval;
  1722. }
  1723. /*
  1724. * BINARY SEARCH
  1725. */
  1726. // Completely normal binary search
  1727. size_t PrsonaServer::binary_search(
  1728. const Twistpoint& index) const
  1729. {
  1730. return PrsonaBase::binary_search(currentPseudonyms, index);
  1731. }
  1732. /*
  1733. * VALID VOTE PROOFS
  1734. */
  1735. bool PrsonaServer::verify_vote_proof(
  1736. const std::vector<Proof>& pi,
  1737. const std::vector<TwistBipoint>& oldVotes,
  1738. const std::vector<TwistBipoint>& newVotes,
  1739. const Twistpoint& shortTermPublicKey) const
  1740. {
  1741. const BGNPublicKey& pubKey = bgnSystem.get_public_key();
  1742. return PrsonaBase::verify_vote_proof(pubKey.get_bipoint_twistgen(), pubKey.get_bipoint_twist_subgroup_gen(), pi, oldVotes, newVotes, currentFreshGenerator, shortTermPublicKey);
  1743. }
  1744. void PrsonaServer::print_scores(
  1745. const std::vector<CurveBipoint>& scores)
  1746. {
  1747. std::unique_lock<std::mutex> lck(*decryptMtx);
  1748. std::cout << "[";
  1749. for (size_t i = 0; i < scores.size(); i++)
  1750. std::cout << bgnSystem.decrypt(scores[i]) << (i == scores.size() - 1 ? "]" : " ");
  1751. std::cout << std::endl;
  1752. }