server.cpp 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567
  1. #include <iostream>
  2. #include "server.hpp"
  3. /********************
  4. * PUBLIC FUNCTIONS *
  5. ********************/
  6. /*
  7. * CONSTRUCTORS
  8. */
  9. // Used to generate the first server; instantiates BGN for the first time
  10. PrsonaServer::PrsonaServer(size_t numServers)
  11. : numServers(numServers)
  12. {
  13. currentSeed.set_random();
  14. }
  15. // Used for all other servers, so they have the same BGN parameters
  16. PrsonaServer::PrsonaServer(size_t numServers, const BGN& otherBgn)
  17. : numServers(numServers), bgnSystem(otherBgn)
  18. {
  19. currentSeed.set_random();
  20. }
  21. /*
  22. * BASIC PUBLIC SYSTEM INFO GETTERS
  23. */
  24. BGNPublicKey PrsonaServer::get_bgn_public_key() const
  25. {
  26. return bgnSystem.get_public_key();
  27. }
  28. size_t PrsonaServer::get_num_clients() const
  29. {
  30. return currentPseudonyms.size();
  31. }
  32. size_t PrsonaServer::get_num_servers() const
  33. {
  34. return numServers;
  35. }
  36. /*
  37. * FRESH GENERATOR CALCULATION
  38. */
  39. // To calculate the current epoch's generator, start from the base generator,
  40. // then have every server call this function on it iteratively (in any order).
  41. Curvepoint PrsonaServer::add_curr_seed_to_generator(
  42. std::vector<Proof>& pi,
  43. const Curvepoint& currGenerator) const
  44. {
  45. pi.push_back(add_to_generator_proof(currGenerator, currentSeed));
  46. return currGenerator * currentSeed;
  47. }
  48. // To calculate the next epoch's generator, start from the base generator,
  49. // then have every server call this function on it iteratively (in any order).
  50. Curvepoint PrsonaServer::add_next_seed_to_generator(
  51. std::vector<Proof>& pi,
  52. const Curvepoint& currGenerator) const
  53. {
  54. pi.push_back(add_to_generator_proof(currGenerator, nextSeed));
  55. return currGenerator * nextSeed;
  56. }
  57. /*
  58. * ENCRYPTED DATA GETTERS
  59. */
  60. /* Call this in order to get the current encrypted votes cast by a given user
  61. * (who is identified by their short term public key).
  62. * In practice, this is intended for clients,
  63. * who need to know their current votes in order to rerandomize them. */
  64. std::vector<CurveBipoint> PrsonaServer::get_current_votes_by(
  65. Proof& pi, const Curvepoint& shortTermPublicKey) const
  66. {
  67. std::vector<CurveBipoint> retval;
  68. size_t voteSubmitter = binary_search(shortTermPublicKey);
  69. retval = voteMatrix[voteSubmitter];
  70. pi = generate_valid_vote_row_proof(retval);
  71. return retval;
  72. }
  73. std::vector<std::vector<CurveBipoint>> PrsonaServer::get_all_current_votes(
  74. Proof& pi) const
  75. {
  76. pi = generate_valid_vote_matrix_proof(voteMatrix);
  77. return voteMatrix;
  78. }
  79. /* Call this in order to get the current encrypted tally of a given user
  80. * (who is identified by their short term public key).
  81. * In practice, this is intended for clients, so that the servers vouch
  82. * for their ciphertexts being valid as part of their reputation proofs. */
  83. EGCiphertext PrsonaServer::get_current_user_encrypted_tally(
  84. Proof& pi, const Curvepoint& shortTermPublicKey) const
  85. {
  86. EGCiphertext retval;
  87. size_t tallyOwner = binary_search(shortTermPublicKey);
  88. retval = currentUserEncryptedTallies[tallyOwner];
  89. pi = generate_valid_user_tally_proof(retval);
  90. return retval;
  91. }
  92. TwistBipoint PrsonaServer::get_current_server_encrypted_tally(
  93. Proof& pi, const Curvepoint& shortTermPublicKey) const
  94. {
  95. TwistBipoint retval;
  96. size_t tallyOwner = binary_search(shortTermPublicKey);
  97. retval = previousVoteTallies[tallyOwner];
  98. pi = generate_valid_server_tally_proof(retval);
  99. return retval;
  100. }
  101. std::vector<Curvepoint> PrsonaServer::get_current_pseudonyms(Proof& pi) const
  102. {
  103. pi = generate_valid_pseudonyms_proof(currentPseudonyms);
  104. return currentPseudonyms;
  105. }
  106. /*
  107. * PROOF COMMITMENT GETTERS
  108. */
  109. Proof PrsonaServer::get_vote_row_commitment(const Curvepoint& request) const
  110. {
  111. size_t requestID = binary_search(request);
  112. return generate_valid_vote_row_proof(voteMatrix[requestID]);
  113. }
  114. Proof PrsonaServer::get_vote_matrix_commitment() const
  115. {
  116. return generate_valid_vote_matrix_proof(voteMatrix);
  117. }
  118. Proof PrsonaServer::get_user_tally_commitment(const Curvepoint& request) const
  119. {
  120. size_t requestID = binary_search(request);
  121. return generate_valid_user_tally_proof(currentUserEncryptedTallies[requestID]);
  122. }
  123. Proof PrsonaServer::get_server_tally_commitment(const Curvepoint& request) const
  124. {
  125. size_t requestID = binary_search(request);
  126. return generate_valid_server_tally_proof(previousVoteTallies[requestID]);
  127. }
  128. Proof PrsonaServer::get_pseudonyms_commitment() const
  129. {
  130. return generate_valid_pseudonyms_proof(currentPseudonyms);
  131. }
  132. /*
  133. * CLIENT INTERACTIONS
  134. */
  135. /* Add a new client (who is identified only by their short term public key)
  136. * One server will do this, then ask all other servers to import their
  137. * (proven) exported data. */
  138. void PrsonaServer::add_new_client(
  139. std::vector<Proof>& proofOfValidAddition,
  140. const Proof& proofOfValidKey,
  141. const Curvepoint& shortTermPublicKey)
  142. {
  143. if (!verify_ownership_proof(
  144. proofOfValidKey, currentFreshGenerator, shortTermPublicKey))
  145. {
  146. std::cerr << "Could not verify proof of valid key." << std::endl;
  147. return;
  148. }
  149. currentPseudonyms.push_back(shortTermPublicKey);
  150. // The first epoch's score for a new user will be low,
  151. // but will typically converge on an average score quickly
  152. Scalar tallySeed;
  153. TwistBipoint encryptedDefaultTally =
  154. bgnSystem.get_public_key().twistEncrypt(tallySeed, DEFAULT_TALLY);
  155. previousVoteTallies.push_back(encryptedDefaultTally);
  156. Scalar seedForUserTally;
  157. seedForUserTally.set_random();
  158. EGCiphertext newUserEncryptedTally;
  159. newUserEncryptedTally.mask = shortTermPublicKey * seedForUserTally;
  160. newUserEncryptedTally.encryptedMessage =
  161. currentFreshGenerator * seedForUserTally +
  162. elGamalBlindGenerator * DEFAULT_TALLY;
  163. currentUserEncryptedTallies.push_back(newUserEncryptedTally);
  164. // Users are defaulted to casting a neutral vote for others.
  165. CurveBipoint encryptedDefaultVote, encryptedSelfVote;
  166. Scalar currDefaultSeed, currSelfSeed;
  167. encryptedDefaultVote =
  168. bgnSystem.get_public_key().curveEncrypt(currDefaultSeed, DEFAULT_VOTE);
  169. encryptedSelfVote =
  170. bgnSystem.get_public_key().curveEncrypt(currSelfSeed, Scalar(MAX_ALLOWED_VOTE));
  171. std::vector<CurveBipoint> newRow;
  172. std::vector<Scalar> userVoteSeeds;
  173. std::vector<Scalar> otherVoteSeeds;
  174. for (size_t i = 0; i < voteMatrix.size(); i++)
  175. {
  176. Scalar addedSeed;
  177. encryptedDefaultVote = bgnSystem.get_public_key().rerandomize(addedSeed, encryptedDefaultVote);
  178. currDefaultSeed = currDefaultSeed + addedSeed;
  179. otherVoteSeeds.push_back(Scalar());
  180. otherVoteSeeds[i] = currDefaultSeed;
  181. voteMatrix[i].push_back(encryptedDefaultVote);
  182. encryptedDefaultVote = bgnSystem.get_public_key().rerandomize(addedSeed, encryptedDefaultVote);
  183. currDefaultSeed = currDefaultSeed + addedSeed;
  184. userVoteSeeds.push_back(Scalar());
  185. userVoteSeeds[i] = currDefaultSeed;
  186. newRow.push_back(encryptedDefaultVote);
  187. }
  188. // Because we are adding the new user to the end (and then sorting it),
  189. // this last element (bottom right corner) is always the self vote.
  190. userVoteSeeds.push_back(Scalar());
  191. userVoteSeeds[newRow.size()] = currSelfSeed;
  192. otherVoteSeeds.push_back(Scalar());
  193. otherVoteSeeds[newRow.size()] = currSelfSeed;
  194. newRow.push_back(encryptedSelfVote);
  195. voteMatrix.push_back(newRow);
  196. std::vector<size_t> sortOrder = order_data();
  197. std::vector<Scalar> newUserVoteSeeds;
  198. std::vector<Scalar> newOtherVoteSeeds;
  199. for (size_t i = 0; i < sortOrder.size(); i++)
  200. {
  201. newUserVoteSeeds.push_back(userVoteSeeds[sortOrder[i]]);
  202. newOtherVoteSeeds.push_back(otherVoteSeeds[sortOrder[i]]);
  203. }
  204. proofOfValidAddition = generate_proof_of_added_user(
  205. tallySeed,
  206. seedForUserTally,
  207. newUserVoteSeeds,
  208. newOtherVoteSeeds);
  209. }
  210. // Receive a new vote row from a user (identified by short term public key).
  211. bool PrsonaServer::receive_vote(
  212. const std::vector<Proof>& pi,
  213. const std::vector<CurveBipoint>& newVotes,
  214. const Curvepoint& shortTermPublicKey)
  215. {
  216. size_t voteSubmitter = binary_search(shortTermPublicKey);
  217. std::vector<CurveBipoint> oldVotes = voteMatrix[voteSubmitter];
  218. if (!verify_vote_proof(pi, oldVotes, newVotes, shortTermPublicKey))
  219. return false;
  220. voteMatrix[voteSubmitter] = newVotes;
  221. return true;
  222. }
  223. /*********************
  224. * PRIVATE FUNCTIONS *
  225. *********************/
  226. /*
  227. * CONSTRUCTOR HELPERS
  228. */
  229. const BGN& PrsonaServer::get_bgn_details() const
  230. {
  231. return bgnSystem;
  232. }
  233. bool PrsonaServer::initialize_fresh_generator(
  234. const std::vector<Proof>& pi,
  235. const Curvepoint& firstGenerator)
  236. {
  237. if (!verify_generator_proof(pi, firstGenerator, numServers))
  238. {
  239. std::cerr << "Could not verify generator proof, aborting." << std::endl;
  240. return false;
  241. }
  242. currentFreshGenerator = firstGenerator;
  243. return true;
  244. }
  245. // To calculate the blind generator for ElGamal, start from the base generator,
  246. // then have every server call this function on it iteratively (in any order).
  247. Curvepoint PrsonaServer::add_rand_seed_to_generator(
  248. std::vector<Proof>& pi,
  249. const Curvepoint& currGenerator) const
  250. {
  251. Scalar lambda;
  252. lambda.set_random();
  253. pi.push_back(add_to_generator_proof(currGenerator, lambda));
  254. return currGenerator * lambda;
  255. }
  256. bool PrsonaServer::set_EG_blind_generator(
  257. const std::vector<Proof>& pi,
  258. const Curvepoint& currGenerator)
  259. {
  260. return PrsonaBase::set_EG_blind_generator(pi, currGenerator, numServers);
  261. }
  262. /*
  263. * SCORE TALLYING
  264. */
  265. /* Calculate scores homomorphically (as an individual server would normally)
  266. * and then decrypt them (which the servers would normally work together to do).
  267. *
  268. * Note that since these calculations are just for us, we don't need to do any
  269. * expensive rerandomizations of intermediate values. */
  270. std::vector<Scalar> PrsonaServer::tally_scores()
  271. {
  272. std::vector<Quadripoint> BGNEncryptedTallies;
  273. std::vector<Scalar> decryptedTallies;
  274. for (size_t i = 0; i < voteMatrix.size(); i++)
  275. {
  276. std::vector<Quadripoint> weightedVotes;
  277. // ZIP
  278. for (size_t j = 0; j < previousVoteTallies.size(); j++)
  279. {
  280. Quadripoint curr =
  281. bgnSystem.homomorphic_multiplication_no_rerandomize(
  282. voteMatrix[j][i], previousVoteTallies[j]);
  283. weightedVotes.push_back(curr);
  284. }
  285. // FOLDL
  286. Quadripoint currEncryptedTally = weightedVotes[0];
  287. for (size_t j = 1; j < weightedVotes.size(); j++)
  288. {
  289. currEncryptedTally =
  290. bgnSystem.homomorphic_addition_no_rerandomize(
  291. currEncryptedTally, weightedVotes[j]);
  292. }
  293. // DECRYPT
  294. decryptedTallies.push_back(bgnSystem.decrypt(currEncryptedTally));
  295. }
  296. return decryptedTallies;
  297. }
  298. /* Calculate what the maximum possible score this round was (that is,
  299. * given the current user weights, what was the highest possible score?).
  300. *
  301. * As with individual scores, this also does the decryption that servers
  302. * would ordinarily work together to form. */
  303. Scalar PrsonaServer::get_max_possible_score()
  304. {
  305. // FOLDL
  306. TwistBipoint currEncryptedVal = previousVoteTallies[0];
  307. for (size_t i = 1; i < previousVoteTallies.size(); i++)
  308. {
  309. currEncryptedVal =
  310. bgnSystem.homomorphic_addition_no_rerandomize(
  311. currEncryptedVal, previousVoteTallies[i]);
  312. }
  313. // DECRYPT
  314. Scalar retval = bgnSystem.decrypt(currEncryptedVal);
  315. return retval;
  316. }
  317. /*
  318. * EPOCH ROUNDS
  319. */
  320. // The first round, going from A_0 to A_0.5
  321. void PrsonaServer::build_up_midway_pseudonyms(
  322. std::vector<std::vector<std::vector<Proof>>>& pi,
  323. std::vector<std::vector<std::vector<Curvepoint>>>& permutationCommits,
  324. std::vector<std::vector<std::vector<Curvepoint>>>& freshPseudonymCommits,
  325. std::vector<std::vector<std::vector<Curvepoint>>>& freshPseudonymPowerCommits,
  326. std::vector<std::vector<std::vector<Curvepoint>>>& freshPseudonymSeedCommits,
  327. std::vector<std::vector<std::vector<TwistBipoint>>>& serverTallyCommits,
  328. std::vector<std::vector<std::vector<std::vector<std::vector<CurveBipoint>>>>>& voteMatrixCommits,
  329. Curvepoint& nextGenerator)
  330. {
  331. nextSeed.set_random();
  332. std::vector<std::vector<Curvepoint>> currPermutationCommits;
  333. std::vector<std::vector<Curvepoint>> currFreshPseudonymCommits;
  334. std::vector<std::vector<Curvepoint>> currFreshPseudonymPowerCommits;
  335. std::vector<std::vector<Curvepoint>> currFreshPseudonymSeedCommits;
  336. std::vector<std::vector<TwistBipoint>> currServerTallyCommits;
  337. std::vector<std::vector<std::vector<std::vector<CurveBipoint>>>> currVoteMatrixCommits;
  338. std::vector<std::vector<std::vector<Curvepoint>>> currUserTallyCommits;
  339. std::vector<std::vector<Curvepoint>> currUserTallyMaskPowerCommits;
  340. std::vector<std::vector<Curvepoint>> currUserTallyMaskSeedCommits;
  341. pi.push_back(epoch_calculations(
  342. currPermutationCommits,
  343. currFreshPseudonymCommits,
  344. currFreshPseudonymPowerCommits,
  345. currFreshPseudonymSeedCommits,
  346. currServerTallyCommits,
  347. currVoteMatrixCommits,
  348. currUserTallyCommits,
  349. currUserTallyMaskPowerCommits,
  350. currUserTallyMaskSeedCommits,
  351. nextSeed,
  352. false));
  353. permutationCommits.push_back(currPermutationCommits);
  354. freshPseudonymCommits.push_back(currFreshPseudonymCommits);
  355. freshPseudonymPowerCommits.push_back(currFreshPseudonymPowerCommits);
  356. freshPseudonymSeedCommits.push_back(currFreshPseudonymSeedCommits);
  357. serverTallyCommits.push_back(currServerTallyCommits);
  358. voteMatrixCommits.push_back(currVoteMatrixCommits);
  359. pi[0][0].push_back(
  360. add_to_generator_proof(nextGenerator, nextSeed));
  361. nextGenerator = nextGenerator * nextSeed;
  362. }
  363. // In between these rounds, scores are tallied, decrypted,
  364. // and encrypted to fresh user pseudonyms (possible through weird math)
  365. // The second round, going from A_0.5 to A_1
  366. void PrsonaServer::break_down_midway_pseudonyms(
  367. std::vector<Proof>& generatorProof,
  368. std::vector<std::vector<std::vector<Proof>>>& pi,
  369. std::vector<std::vector<std::vector<Curvepoint>>>& permutationCommits,
  370. std::vector<std::vector<std::vector<Curvepoint>>>& freshPseudonymCommits,
  371. std::vector<std::vector<std::vector<Curvepoint>>>& freshPseudonymPowerCommits,
  372. std::vector<std::vector<std::vector<Curvepoint>>>& freshPseudonymSeedCommits,
  373. std::vector<std::vector<std::vector<TwistBipoint>>>& serverTallyCommits,
  374. std::vector<std::vector<std::vector<std::vector<std::vector<CurveBipoint>>>>>& voteMatrixCommits,
  375. std::vector<std::vector<std::vector<std::vector<Curvepoint>>>>& userTallyCommits,
  376. std::vector<std::vector<std::vector<Curvepoint>>>& userTallyMaskPowerCommits,
  377. std::vector<std::vector<std::vector<Curvepoint>>>& userTallyMaskSeedCommits,
  378. const Curvepoint& nextGenerator)
  379. {
  380. if (!initialize_fresh_generator(generatorProof, nextGenerator))
  381. {
  382. std::cerr << "New fresh generator could not be verified." << std::endl;
  383. return;
  384. }
  385. Scalar inverseSeed = currentSeed.curveMultInverse();
  386. std::vector<std::vector<Curvepoint>> currPermutationCommits;
  387. std::vector<std::vector<Curvepoint>> currFreshPseudonymCommits;
  388. std::vector<std::vector<Curvepoint>> currFreshPseudonymPowerCommits;
  389. std::vector<std::vector<Curvepoint>> currFreshPseudonymSeedCommits;
  390. std::vector<std::vector<TwistBipoint>> currServerTallyCommits;
  391. std::vector<std::vector<std::vector<std::vector<CurveBipoint>>>> currVoteMatrixCommits;
  392. std::vector<std::vector<std::vector<Curvepoint>>> currUserTallyCommits;
  393. std::vector<std::vector<Curvepoint>> currUserTallyMaskPowerCommits;
  394. std::vector<std::vector<Curvepoint>> currUserTallyMaskSeedCommits;
  395. pi.push_back(epoch_calculations(
  396. currPermutationCommits,
  397. currFreshPseudonymCommits,
  398. currFreshPseudonymPowerCommits,
  399. currFreshPseudonymSeedCommits,
  400. currServerTallyCommits,
  401. currVoteMatrixCommits,
  402. currUserTallyCommits,
  403. currUserTallyMaskPowerCommits,
  404. currUserTallyMaskSeedCommits,
  405. inverseSeed,
  406. true));
  407. permutationCommits.push_back(currPermutationCommits);
  408. freshPseudonymCommits.push_back(currFreshPseudonymCommits);
  409. freshPseudonymPowerCommits.push_back(currFreshPseudonymPowerCommits);
  410. freshPseudonymSeedCommits.push_back(currFreshPseudonymSeedCommits);
  411. serverTallyCommits.push_back(currServerTallyCommits);
  412. voteMatrixCommits.push_back(currVoteMatrixCommits);
  413. userTallyCommits.push_back(currUserTallyCommits);
  414. userTallyMaskPowerCommits.push_back(currUserTallyMaskPowerCommits);
  415. userTallyMaskSeedCommits.push_back(currUserTallyMaskSeedCommits);
  416. currentSeed = nextSeed;
  417. }
  418. /*
  419. * EPOCH HELPERS
  420. */
  421. std::vector<std::vector<Proof>> PrsonaServer::epoch_calculations(
  422. std::vector<std::vector<Curvepoint>>& permutationCommits,
  423. std::vector<std::vector<Curvepoint>>& freshPseudonymCommits,
  424. std::vector<std::vector<Curvepoint>>& freshPseudonymPowerCommits,
  425. std::vector<std::vector<Curvepoint>>& freshPseudonymSeedCommits,
  426. std::vector<std::vector<TwistBipoint>>& serverTallyCommits,
  427. std::vector<std::vector<std::vector<std::vector<CurveBipoint>>>>& voteMatrixCommits,
  428. std::vector<std::vector<std::vector<Curvepoint>>>& userTallyCommits,
  429. std::vector<std::vector<Curvepoint>> & userTallyMaskPowerCommits,
  430. std::vector<std::vector<Curvepoint>> & userTallyMaskSeedCommits,
  431. const Scalar& power,
  432. bool doUserTallies)
  433. {
  434. std::vector<std::vector<Proof>> retval;
  435. std::vector<Curvepoint> nextPseudonyms;
  436. for (size_t i = 0; i < currentPseudonyms.size(); i++)
  437. nextPseudonyms.push_back(currentPseudonyms[i] * power);
  438. std::vector<size_t> order = sort_data(nextPseudonyms);
  439. for (size_t i = 0; i < currentPseudonyms.size(); i++)
  440. nextPseudonyms[i] = currentPseudonyms[order[i]] * power;
  441. // std::cout << "Generating permutation matrix." << std::endl;
  442. std::vector<std::vector<Scalar>> permutations =
  443. generate_permutation_matrix(power);
  444. // std::cout << "Generating permutation commitment matrix." << std::endl;
  445. std::vector<std::vector<Scalar>> permutationSeeds;
  446. permutationCommits.clear();
  447. permutationCommits =
  448. generate_commitment_matrix(permutations, permutationSeeds);
  449. // std::cout << "Generating permutation proof." << std::endl;
  450. retval.push_back(generate_valid_permutation_proof(
  451. permutations, permutationSeeds, permutationCommits));
  452. // std::cout << "Generating pseudonym matrix." << std::endl;
  453. std::vector<std::vector<Scalar>> freshPseudonymSeeds;
  454. freshPseudonymSeedCommits.clear();
  455. freshPseudonymPowerCommits.clear();
  456. freshPseudonymCommits =
  457. generate_pseudonym_matrix(
  458. permutations,
  459. power,
  460. freshPseudonymSeeds,
  461. freshPseudonymPowerCommits,
  462. freshPseudonymSeedCommits);
  463. // for (size_t i = 0; i < freshPseudonymCommits.size(); i++)
  464. // {
  465. // Curvepoint sum = freshPseudonymCommits[i][0];
  466. // for (size_t j = 1; j < freshPseudonymCommits[i].size(); j++)
  467. // sum = sum + freshPseudonymCommits[i][j];
  468. // std::cout << "Fresh pseudonym commit row " << i + 1 << " of " << permutationCommits.size()
  469. // << (sum == nextPseudonyms[i] ? ": PASS" : ": FAIL") << std::endl;
  470. // }
  471. // std::cout << (pseudonyms_sorted(nextPseudonyms) ? "PASS" : "FAIL") << std::endl;
  472. // std::cout << "Generating pseudonym proof." << std::endl;
  473. retval.push_back(
  474. generate_proof_of_reordering_plus_power(
  475. permutations,
  476. power,
  477. permutationSeeds,
  478. freshPseudonymSeeds,
  479. currentPseudonyms,
  480. permutationCommits,
  481. freshPseudonymCommits,
  482. freshPseudonymPowerCommits,
  483. freshPseudonymSeedCommits));
  484. // std::cout << "Generating server tally matrix." << std::endl;
  485. std::vector<std::vector<Scalar>> serverTallySeeds;
  486. serverTallyCommits.clear();
  487. serverTallyCommits =
  488. generate_server_tally_matrix(
  489. permutations,
  490. serverTallySeeds);
  491. // std::cout << "Generating server tally proof." << std::endl;
  492. retval.push_back(
  493. generate_proof_of_reordering<TwistBipoint>(
  494. permutations,
  495. permutationSeeds,
  496. serverTallySeeds,
  497. previousVoteTallies,
  498. permutationCommits,
  499. serverTallyCommits,
  500. bgnSystem.get_public_key().get_bipoint_twistgen(),
  501. bgnSystem.get_public_key().get_bipoint_twist_subgroup_gen(),
  502. false));
  503. // std::cout << "Doing V * P^T." << std::endl;
  504. std::vector<std::vector<std::vector<Scalar>>> firstVoteMatrixSeeds;
  505. std::vector<std::vector<std::vector<Scalar>>> secondVoteMatrixSeeds;
  506. voteMatrixCommits.push_back(
  507. generate_vote_tensor(
  508. permutations,
  509. voteMatrix,
  510. firstVoteMatrixSeeds,
  511. false));
  512. // std::cout << "Finishing V * P^T calculation." << std::endl;
  513. std::vector<std::vector<CurveBipoint>> partialVoteMatrix =
  514. calculate_next_vote_matrix(voteMatrixCommits[0]);
  515. // std::cout << "Doing P * (V * P^T)." << std::endl;
  516. voteMatrixCommits.push_back(
  517. generate_vote_tensor(
  518. permutations,
  519. partialVoteMatrix,
  520. secondVoteMatrixSeeds,
  521. true));
  522. // std::cout << "Proving V * P^T." << std::endl;
  523. generate_vote_tensor_proofs(
  524. retval,
  525. permutations,
  526. permutationSeeds,
  527. firstVoteMatrixSeeds,
  528. voteMatrix,
  529. permutationCommits,
  530. voteMatrixCommits[0],
  531. false);
  532. // std::cout << "Proving P * (V * P^T)." << std::endl;
  533. generate_vote_tensor_proofs(
  534. retval,
  535. permutations,
  536. permutationSeeds,
  537. secondVoteMatrixSeeds,
  538. partialVoteMatrix,
  539. permutationCommits,
  540. voteMatrixCommits[1],
  541. true);
  542. if (doUserTallies)
  543. {
  544. // std::cout << "Generating user tally matrix." << std::endl;
  545. std::vector<Curvepoint> userTallyMasks;
  546. std::vector<std::vector<Scalar>> userTallyMaskSeeds;
  547. userTallyMaskPowerCommits.clear();
  548. userTallyMaskSeedCommits.clear();
  549. std::vector<Curvepoint> userTallyMessages;
  550. std::vector<std::vector<Scalar>> userTallyMessageSeeds;
  551. userTallyCommits =
  552. generate_user_tally_matrix(
  553. permutations,
  554. power,
  555. userTallyMasks,
  556. userTallyMessages,
  557. userTallyMaskSeeds,
  558. userTallyMaskPowerCommits,
  559. userTallyMaskSeedCommits,
  560. userTallyMessageSeeds);
  561. // std::cout << "Proving user tally mask matrix." << std::endl;
  562. retval.push_back(
  563. generate_proof_of_reordering_plus_power(
  564. permutations,
  565. power,
  566. permutationSeeds,
  567. userTallyMaskSeeds,
  568. userTallyMasks,
  569. permutationCommits,
  570. userTallyCommits[0],
  571. userTallyMaskPowerCommits,
  572. userTallyMaskSeedCommits));
  573. // std::cout << "Proving user tally message matrix." << std::endl;
  574. retval.push_back(
  575. generate_proof_of_reordering<Curvepoint>(
  576. permutations,
  577. permutationSeeds,
  578. userTallyMessageSeeds,
  579. userTallyMessages,
  580. permutationCommits,
  581. userTallyCommits[1],
  582. EL_GAMAL_GENERATOR,
  583. elGamalBlindGenerator,
  584. false));
  585. }
  586. else
  587. {
  588. userTallyMaskPowerCommits.clear();
  589. userTallyMaskSeedCommits.clear();
  590. userTallyCommits.push_back(std::vector<std::vector<Curvepoint>>());
  591. userTallyCommits.push_back(std::vector<std::vector<Curvepoint>>());
  592. }
  593. // std::cout << "Giving self updates." << std::endl;
  594. // Replace internal values
  595. update_data(
  596. freshPseudonymPowerCommits,
  597. serverTallyCommits,
  598. voteMatrixCommits[1],
  599. userTallyMaskPowerCommits,
  600. userTallyCommits[1]);
  601. if (!doUserTallies)
  602. userTallyCommits.clear();
  603. return retval;
  604. }
  605. bool PrsonaServer::accept_epoch_updates(
  606. const std::vector<std::vector<Proof>>& pi,
  607. const std::vector<std::vector<Curvepoint>>& permutationCommits,
  608. const std::vector<std::vector<Curvepoint>>& freshPseudonymCommits,
  609. const std::vector<std::vector<Curvepoint>>& freshPseudonymPowerCommits,
  610. const std::vector<std::vector<Curvepoint>>& freshPseudonymSeedCommits,
  611. const std::vector<std::vector<TwistBipoint>>& serverTallyCommits,
  612. const std::vector<std::vector<std::vector<std::vector<CurveBipoint>>>>& voteMatrixCommits,
  613. const std::vector<std::vector<std::vector<Curvepoint>>>& userTallyCommits,
  614. const std::vector<std::vector<Curvepoint>>& userTallyMaskPowerCommits,
  615. const std::vector<std::vector<Curvepoint>>& userTallyMaskSeedCommits,
  616. bool doUserTallies)
  617. {
  618. bool verification;
  619. if ((userTallyCommits.empty() && doUserTallies) || (!userTallyCommits.empty() && !doUserTallies))
  620. {
  621. std::cerr << "user tallies are not in expected state." << std::endl;
  622. return false;
  623. }
  624. if (pi.empty())
  625. return false;
  626. verification =
  627. verify_valid_permutation_proof(pi[0], permutationCommits);
  628. if (!verification)
  629. {
  630. std::cerr << "Could not verify valid permutation matrix." << std::endl;
  631. return false;
  632. }
  633. verification =
  634. verify_proof_of_reordering_plus_power(
  635. pi[1],
  636. currentPseudonyms,
  637. permutationCommits,
  638. freshPseudonymCommits,
  639. freshPseudonymPowerCommits,
  640. freshPseudonymSeedCommits);
  641. if (!verification)
  642. {
  643. std::cerr << "Could not verify valid pseudonym vector." << std::endl;
  644. return false;
  645. }
  646. verification =
  647. verify_proof_of_reordering<TwistBipoint>(
  648. pi[2],
  649. previousVoteTallies,
  650. permutationCommits,
  651. serverTallyCommits,
  652. bgnSystem.get_public_key().get_bipoint_twistgen(),
  653. bgnSystem.get_public_key().get_bipoint_twist_subgroup_gen(),
  654. false);
  655. if (!verification)
  656. {
  657. std::cerr << "Could not verify valid server tally vector." << std::endl;
  658. return false;
  659. }
  660. size_t currOffset = 3;
  661. verification = verify_vote_tensor_proofs(
  662. pi,
  663. currOffset,
  664. voteMatrix,
  665. permutationCommits,
  666. voteMatrixCommits[0],
  667. false);
  668. if (!verification)
  669. {
  670. std::cerr << "Could not verify first half vote matrix." << std::endl;
  671. return false;
  672. }
  673. std::vector<std::vector<CurveBipoint>> partialVoteMatrix =
  674. calculate_next_vote_matrix(voteMatrixCommits[0]);
  675. currOffset += voteMatrix.size();
  676. verification = verify_vote_tensor_proofs(
  677. pi,
  678. currOffset,
  679. partialVoteMatrix,
  680. permutationCommits,
  681. voteMatrixCommits[1],
  682. true);
  683. if (!verification)
  684. {
  685. std::cerr << "Could not verify second half vote matrix." << std::endl;
  686. return false;
  687. }
  688. currOffset += voteMatrix.size();
  689. std::vector<std::vector<Curvepoint>> inputMasks;
  690. std::vector<std::vector<Curvepoint>> inputMessages;
  691. if (doUserTallies)
  692. {
  693. std::vector<Curvepoint> userTallyMasks;
  694. std::vector<Curvepoint> userTallyMessages;
  695. for (size_t i = 0; i < currentUserEncryptedTallies.size(); i++)
  696. {
  697. userTallyMasks.push_back(currentUserEncryptedTallies[i].mask);
  698. userTallyMessages.push_back(currentUserEncryptedTallies[i].encryptedMessage);
  699. }
  700. verification = verify_proof_of_reordering_plus_power(
  701. pi[currOffset],
  702. userTallyMasks,
  703. permutationCommits,
  704. userTallyCommits[0],
  705. userTallyMaskPowerCommits,
  706. userTallyMaskSeedCommits);
  707. if (!verification)
  708. {
  709. std::cerr << "Could not verify user tally masks." << std::endl;
  710. return false;
  711. }
  712. currOffset++;
  713. verification = verify_proof_of_reordering<Curvepoint>(
  714. pi[currOffset],
  715. userTallyMessages,
  716. permutationCommits,
  717. userTallyCommits[1],
  718. EL_GAMAL_GENERATOR,
  719. elGamalBlindGenerator,
  720. false);
  721. if (!verification)
  722. {
  723. std::cerr << "Could not verify user tally messages." << std::endl;
  724. return false;
  725. }
  726. inputMasks = userTallyMaskPowerCommits;
  727. inputMessages = userTallyCommits[1];
  728. }
  729. verification = update_data(
  730. freshPseudonymPowerCommits,
  731. serverTallyCommits,
  732. voteMatrixCommits[1],
  733. inputMasks,
  734. inputMessages);
  735. return verification;
  736. }
  737. std::vector<std::vector<Scalar>> PrsonaServer::generate_permutation_matrix(
  738. const Scalar& reorderSeed) const
  739. {
  740. std::vector<std::vector<Scalar>> retval;
  741. for (size_t i = 0; i < currentPseudonyms.size(); i++)
  742. {
  743. std::vector<Scalar> currRow;
  744. for (size_t j = 0; j < currentPseudonyms.size(); j++)
  745. currRow.push_back(Scalar(0));
  746. retval.push_back(currRow);
  747. }
  748. std::vector<Curvepoint> nextPseudonyms;
  749. for (size_t i = 0; i < currentPseudonyms.size(); i++)
  750. nextPseudonyms.push_back(currentPseudonyms[i] * reorderSeed);
  751. std::vector<size_t> order = sort_data(nextPseudonyms);
  752. for (size_t i = 0; i < order.size(); i++)
  753. retval[order[i]][i] = Scalar(1);
  754. std::cout << "[";
  755. for (size_t i = 0; i < order.size(); i++)
  756. {
  757. std::cout << "[";
  758. for (size_t j = 0; j < order.size(); j++)
  759. {
  760. std::cout << retval[i][j] << (j == order.size() - 1 ? "]" : " ");
  761. }
  762. std::cout << (i == order.size() - 1 ? "]" : " ") << std::endl << (i == order.size() - 1 ? "" : " ");
  763. }
  764. return retval;
  765. }
  766. std::vector<std::vector<Curvepoint>> PrsonaServer::generate_commitment_matrix(
  767. const std::vector<std::vector<Scalar>>& permutations,
  768. std::vector<std::vector<Scalar>>& seeds) const
  769. {
  770. std::vector<std::vector<Curvepoint>> retval;
  771. Curvepoint g = EL_GAMAL_GENERATOR;
  772. Curvepoint h = elGamalBlindGenerator;
  773. seeds.clear();
  774. for (size_t i = 0; i < permutations.size(); i++)
  775. {
  776. std::vector<Scalar> currSeeds;
  777. for (size_t j = 0; j < permutations[i].size(); j++)
  778. currSeeds.push_back(Scalar(0));
  779. seeds.push_back(currSeeds);
  780. }
  781. for (size_t i = 0; i < permutations.size(); i++)
  782. {
  783. std::vector<Curvepoint> currRow;
  784. size_t last = permutations[i].size() - 1;
  785. for (size_t j = 0; j < permutations[i].size(); j++)
  786. {
  787. Curvepoint element;
  788. if (j != last)
  789. {
  790. seeds[i][j].set_random();
  791. seeds[i][last] = seeds[i][last] - seeds[i][j];
  792. }
  793. element = encrypt<Curvepoint>(g, h, permutations[i][j], seeds[i][j]);
  794. currRow.push_back(element);
  795. }
  796. retval.push_back(currRow);
  797. }
  798. return retval;
  799. }
  800. std::vector<std::vector<Curvepoint>> PrsonaServer::generate_pseudonym_matrix(
  801. const std::vector<std::vector<Scalar>>& permutations,
  802. const Scalar& power,
  803. std::vector<std::vector<Scalar>>& seeds,
  804. std::vector<std::vector<Curvepoint>>& powerCommits,
  805. std::vector<std::vector<Curvepoint>>& seedCommits) const
  806. {
  807. return generate_reordered_plus_power_matrix<Curvepoint>(
  808. permutations,
  809. power,
  810. currentPseudonyms,
  811. seeds,
  812. powerCommits,
  813. seedCommits,
  814. elGamalBlindGenerator);
  815. }
  816. std::vector<std::vector<TwistBipoint>> PrsonaServer::generate_server_tally_matrix(
  817. const std::vector<std::vector<Scalar>>& permutations,
  818. std::vector<std::vector<Scalar>>& seeds) const
  819. {
  820. return generate_reordered_matrix<TwistBipoint>(
  821. permutations,
  822. previousVoteTallies,
  823. seeds,
  824. bgnSystem.get_public_key().get_bipoint_twist_subgroup_gen(),
  825. false,
  826. false);
  827. }
  828. std::vector<std::vector<std::vector<CurveBipoint>>> PrsonaServer::generate_vote_tensor(
  829. const std::vector<std::vector<Scalar>>& permutations,
  830. const std::vector<std::vector<CurveBipoint>>& currVoteMatrix,
  831. std::vector<std::vector<std::vector<Scalar>>>& seeds,
  832. bool inverted) const
  833. {
  834. std::vector<std::vector<std::vector<CurveBipoint>>> retval;
  835. for (size_t i = 0; i < currVoteMatrix.size(); i++)
  836. {
  837. std::vector<std::vector<Scalar>> currSeeds;
  838. std::vector<CurveBipoint> inputRow;
  839. if (inverted)
  840. {
  841. for (size_t j = 0; j < currVoteMatrix.size(); j++)
  842. inputRow.push_back(currVoteMatrix[j][i]);
  843. }
  844. else
  845. {
  846. inputRow = currVoteMatrix[i];
  847. }
  848. retval.push_back(generate_reordered_matrix<CurveBipoint>(
  849. permutations,
  850. inputRow,
  851. currSeeds,
  852. bgnSystem.get_public_key().get_bipoint_curve_subgroup_gen(),
  853. true,
  854. false));
  855. seeds.push_back(currSeeds);
  856. }
  857. return retval;
  858. }
  859. std::vector<std::vector<CurveBipoint>> PrsonaServer::calculate_next_vote_matrix(
  860. const std::vector<std::vector<std::vector<CurveBipoint>>>& voteTensor) const
  861. {
  862. std::vector<std::vector<CurveBipoint>> retval;
  863. for (size_t i = 0; i < voteTensor.size(); i++)
  864. {
  865. std::vector<CurveBipoint> currRow;
  866. for (size_t j = 0; j < voteTensor[i].size(); j++)
  867. {
  868. CurveBipoint sum = voteTensor[i][j][0];
  869. for (size_t k = 1; k < voteTensor[i][j].size(); k++)
  870. sum = sum + voteTensor[i][j][k];
  871. currRow.push_back(sum);
  872. }
  873. retval.push_back(currRow);
  874. }
  875. return retval;
  876. }
  877. void PrsonaServer::generate_vote_tensor_proofs(
  878. std::vector<std::vector<Proof>>& pi,
  879. const std::vector<std::vector<Scalar>>& permutations,
  880. const std::vector<std::vector<Scalar>>& permutationSeeds,
  881. const std::vector<std::vector<std::vector<Scalar>>>& matrixSeeds,
  882. const std::vector<std::vector<CurveBipoint>>& currMatrix,
  883. const std::vector<std::vector<Curvepoint>>& permutationCommits,
  884. const std::vector<std::vector<std::vector<CurveBipoint>>>& matrixCommits,
  885. bool inverted) const
  886. {
  887. for (size_t i = 0; i < currMatrix.size(); i++)
  888. {
  889. std::vector<CurveBipoint> inputRow;
  890. if (inverted)
  891. {
  892. for (size_t j = 0; j < currMatrix.size(); j++)
  893. inputRow.push_back(currMatrix[j][i]);
  894. }
  895. else
  896. {
  897. inputRow = currMatrix[i];
  898. }
  899. pi.push_back(generate_proof_of_reordering<CurveBipoint>(
  900. permutations,
  901. permutationSeeds,
  902. matrixSeeds[i],
  903. inputRow,
  904. permutationCommits,
  905. matrixCommits[i],
  906. bgnSystem.get_public_key().get_bipoint_curvegen(),
  907. bgnSystem.get_public_key().get_bipoint_curve_subgroup_gen(),
  908. true));
  909. }
  910. }
  911. bool PrsonaServer::verify_vote_tensor_proofs(
  912. const std::vector<std::vector<Proof>>& pi,
  913. size_t start_offset,
  914. const std::vector<std::vector<CurveBipoint>>& currMatrix,
  915. const std::vector<std::vector<Curvepoint>>& permutationCommits,
  916. const std::vector<std::vector<std::vector<CurveBipoint>>>& matrixCommits,
  917. bool inverted) const
  918. {
  919. bool retval = true;
  920. for (size_t i = 0; i < currMatrix.size(); i++)
  921. {
  922. std::vector<CurveBipoint> inputRow;
  923. if (inverted)
  924. {
  925. for (size_t j = 0; j < currMatrix.size(); j++)
  926. inputRow.push_back(currMatrix[j][i]);
  927. }
  928. else
  929. {
  930. inputRow = currMatrix[i];
  931. }
  932. size_t whichProof = i + start_offset;
  933. retval = retval && verify_proof_of_reordering<CurveBipoint>(
  934. pi[whichProof],
  935. inputRow,
  936. permutationCommits,
  937. matrixCommits[i],
  938. bgnSystem.get_public_key().get_bipoint_curvegen(),
  939. bgnSystem.get_public_key().get_bipoint_curve_subgroup_gen(),
  940. true);
  941. }
  942. return retval;
  943. }
  944. std::vector<std::vector<std::vector<Curvepoint>>> PrsonaServer::generate_user_tally_matrix(
  945. const std::vector<std::vector<Scalar>>& permutations,
  946. const Scalar& power,
  947. std::vector<Curvepoint>& masks,
  948. std::vector<Curvepoint>& messages,
  949. std::vector<std::vector<Scalar>>& maskSeeds,
  950. std::vector<std::vector<Curvepoint>>& maskPowerCommits,
  951. std::vector<std::vector<Curvepoint>>& maskSeedCommits,
  952. std::vector<std::vector<Scalar>>& messageSeeds) const
  953. {
  954. std::vector<std::vector<std::vector<Curvepoint>>> retval;
  955. masks.clear();
  956. messages.clear();
  957. for (size_t i = 0; i < currentUserEncryptedTallies.size(); i++)
  958. {
  959. masks.push_back(currentUserEncryptedTallies[i].mask);
  960. messages.push_back(currentUserEncryptedTallies[i].encryptedMessage);
  961. }
  962. retval.push_back(
  963. generate_reordered_plus_power_matrix<Curvepoint>(
  964. permutations,
  965. power,
  966. masks,
  967. maskSeeds,
  968. maskPowerCommits,
  969. maskSeedCommits,
  970. elGamalBlindGenerator));
  971. retval.push_back(
  972. generate_reordered_matrix<Curvepoint>(
  973. permutations,
  974. messages,
  975. messageSeeds,
  976. elGamalBlindGenerator,
  977. false,
  978. false));
  979. return retval;
  980. }
  981. template <typename T>
  982. std::vector<std::vector<T>> PrsonaServer::generate_reordered_plus_power_matrix(
  983. const std::vector<std::vector<Scalar>>& permutations,
  984. const Scalar& power,
  985. const std::vector<T>& oldValues,
  986. std::vector<std::vector<Scalar>>& seeds,
  987. std::vector<std::vector<T>>& powerCommits,
  988. std::vector<std::vector<Curvepoint>>& seedCommits,
  989. const T& h) const
  990. {
  991. std::vector<std::vector<Scalar>> permutation_plus_power;
  992. std::vector<std::vector<T>> retval;
  993. seedCommits.clear();
  994. powerCommits.clear();
  995. for (size_t i = 0; i < permutations.size(); i++)
  996. {
  997. std::vector<Scalar> currPermutations;
  998. std::vector<Curvepoint> currSeedCommits;
  999. std::vector<T> currRetvalRow;
  1000. for (size_t j = 0; j < permutations[i].size(); j++)
  1001. {
  1002. currPermutations.push_back(permutations[i][j] * power);
  1003. currSeedCommits.push_back(Curvepoint());
  1004. currRetvalRow.push_back(T());
  1005. }
  1006. permutation_plus_power.push_back(currPermutations);
  1007. seedCommits.push_back(currSeedCommits);
  1008. retval.push_back(currRetvalRow);
  1009. }
  1010. powerCommits =
  1011. generate_reordered_matrix<T>(
  1012. permutation_plus_power,
  1013. oldValues,
  1014. seeds,
  1015. h,
  1016. false,
  1017. true);
  1018. Scalar inversePower = power.curveMultInverse();
  1019. for (size_t i = 0; i < permutations.size(); i++)
  1020. {
  1021. for (size_t j = 0; j < permutations[i].size(); j++)
  1022. {
  1023. seedCommits[i][j] = EL_GAMAL_GENERATOR * seeds[i][j];
  1024. seeds[i][j] = seeds[i][j] * inversePower;
  1025. retval[i][j] = powerCommits[i][j] * inversePower;
  1026. }
  1027. }
  1028. return retval;
  1029. }
  1030. template <typename T>
  1031. std::vector<std::vector<T>> PrsonaServer::generate_reordered_matrix(
  1032. const std::vector<std::vector<Scalar>>& permutations,
  1033. const std::vector<T>& oldValues,
  1034. std::vector<std::vector<Scalar>>& seeds,
  1035. const T& h,
  1036. bool inverted,
  1037. bool cancelOut) const
  1038. {
  1039. std::vector<std::vector<T>> retval;
  1040. seeds.clear();
  1041. for (size_t i = 0; i < permutations.size(); i++)
  1042. {
  1043. std::vector<Scalar> currSeeds;
  1044. for (size_t j = 0; j < permutations[i].size(); j++)
  1045. currSeeds.push_back(Scalar(0));
  1046. seeds.push_back(currSeeds);
  1047. }
  1048. for (size_t i = 0; i < permutations.size(); i++)
  1049. {
  1050. std::vector<T> currRow;
  1051. T g = oldValues[i];
  1052. size_t last =
  1053. (inverted ?
  1054. permutations[i].size() - 1 :
  1055. permutations.size() - 1);
  1056. for (size_t j = 0; j < permutations[i].size(); j++)
  1057. {
  1058. T element;
  1059. if (!cancelOut)
  1060. {
  1061. seeds[i][j].set_random();
  1062. }
  1063. else if (inverted && j != last)
  1064. {
  1065. seeds[i][j].set_random();
  1066. seeds[i][last] = seeds[i][last] - seeds[i][j];
  1067. }
  1068. else if (!inverted && i != last)
  1069. {
  1070. seeds[i][j].set_random();
  1071. seeds[last][j] = seeds[last][j] - seeds[i][j];
  1072. }
  1073. element = encrypt<T>(g, h, permutations[i][j], seeds[i][j]);
  1074. currRow.push_back(element);
  1075. }
  1076. retval.push_back(currRow);
  1077. }
  1078. /* We have to transpose here, because the way we calculate things
  1079. * is transposed of the actual result we want
  1080. * (where you can fold a row into the correct value,
  1081. * instead of folding a column).
  1082. * "inverted" is true when we need this transposed version
  1083. * (right multiplication by P^T, or left multiplication by P) */
  1084. if (!inverted)
  1085. {
  1086. retval = transpose_matrix<T>(retval);
  1087. seeds = transpose_matrix<Scalar>(seeds);
  1088. }
  1089. return retval;
  1090. }
  1091. template <typename T>
  1092. std::vector<std::vector<T>> PrsonaServer::transpose_matrix(
  1093. const std::vector<std::vector<T>>& input) const
  1094. {
  1095. std::vector<std::vector<T>> retval;
  1096. for (size_t i = 0; i < input.size(); i++)
  1097. {
  1098. std::vector<T> currRow;
  1099. for (size_t j = 0; j < input[i].size(); j++)
  1100. currRow.push_back(input[j][i]);
  1101. retval.push_back(currRow);
  1102. }
  1103. return retval;
  1104. }
  1105. std::vector<size_t> PrsonaServer::sort_data(const std::vector<Curvepoint>& inputs) const
  1106. {
  1107. std::vector<size_t> retval;
  1108. // SortingType's index member allows us to replicate the "sort" across
  1109. std::vector<SortingType> sortTracker;
  1110. for (size_t i = 0; i < inputs.size(); i++)
  1111. {
  1112. SortingType curr;
  1113. curr.pseudonym = inputs[i];
  1114. curr.index = i;
  1115. sortTracker.push_back(curr);
  1116. }
  1117. std::sort(sortTracker.begin(), sortTracker.end());
  1118. for (size_t i = 0; i < inputs.size(); i++)
  1119. retval.push_back(sortTracker[i].index);
  1120. return retval;
  1121. }
  1122. template <typename T>
  1123. T PrsonaServer::encrypt(
  1124. const T& g, const T& h, const Scalar& plaintext, const Scalar& lambda) const
  1125. {
  1126. return g * plaintext + h * lambda;
  1127. }
  1128. bool PrsonaServer::update_data(
  1129. const std::vector<std::vector<Curvepoint>>& freshPseudonymCommits,
  1130. const std::vector<std::vector<TwistBipoint>>& serverTallyCommits,
  1131. const std::vector<std::vector<std::vector<CurveBipoint>>>& voteMatrixCommits,
  1132. const std::vector<std::vector<Curvepoint>>& userTallyMaskCommits,
  1133. const std::vector<std::vector<Curvepoint>>& userTallyMessageCommits)
  1134. {
  1135. std::vector<Curvepoint> newPseudonyms;
  1136. std::vector<TwistBipoint> newVoteTallies;
  1137. std::vector<EGCiphertext> newUserTallies;
  1138. for (size_t i = 0; i < freshPseudonymCommits.size(); i++)
  1139. {
  1140. Curvepoint pseudonymSum = freshPseudonymCommits[i][0];
  1141. TwistBipoint voteTallySum = serverTallyCommits[i][0];
  1142. Curvepoint userTallyMask, userTallyMessage;
  1143. if (!userTallyMaskCommits.empty())
  1144. {
  1145. userTallyMask = userTallyMaskCommits[i][0];
  1146. userTallyMessage = userTallyMessageCommits[i][0];
  1147. }
  1148. for (size_t j = 1; j < freshPseudonymCommits[i].size(); j++)
  1149. {
  1150. pseudonymSum = pseudonymSum + freshPseudonymCommits[i][j];
  1151. voteTallySum = voteTallySum + serverTallyCommits[i][j];
  1152. if (!userTallyMaskCommits.empty())
  1153. {
  1154. userTallyMask = userTallyMask +
  1155. userTallyMaskCommits[i][j];
  1156. userTallyMessage = userTallyMessage +
  1157. userTallyMessageCommits[i][j];
  1158. }
  1159. }
  1160. newPseudonyms.push_back(pseudonymSum);
  1161. newVoteTallies.push_back(voteTallySum);
  1162. if (!userTallyMaskCommits.empty())
  1163. {
  1164. newUserTallies.push_back(
  1165. EGCiphertext(userTallyMask, userTallyMessage));
  1166. }
  1167. }
  1168. if (!pseudonyms_sorted(newPseudonyms))
  1169. {
  1170. std::cerr << "Pseudonyms not sorted correctly." << std::endl;
  1171. return false;
  1172. }
  1173. currentPseudonyms = newPseudonyms;
  1174. previousVoteTallies = newVoteTallies;
  1175. voteMatrix = calculate_next_vote_matrix(voteMatrixCommits);
  1176. currentUserEncryptedTallies = newUserTallies;
  1177. return true;
  1178. }
  1179. bool PrsonaServer::pseudonyms_sorted(
  1180. const std::vector<Curvepoint> newPseudonyms) const
  1181. {
  1182. bool retval = true;
  1183. for (size_t i = 0; i < newPseudonyms.size() - 1; i++)
  1184. retval = retval && (newPseudonyms[i] < newPseudonyms[i + 1]);
  1185. return retval;
  1186. }
  1187. /*
  1188. * DATA MAINTENANCE
  1189. */
  1190. bool PrsonaServer::import_new_user_update(
  1191. const std::vector<Proof>& pi,
  1192. const std::vector<TwistBipoint>& otherPreviousVoteTallies,
  1193. const std::vector<Curvepoint>& otherCurrentPseudonyms,
  1194. const std::vector<EGCiphertext>& otherCurrentUserEncryptedTallies,
  1195. const std::vector<std::vector<CurveBipoint>>& otherVoteMatrix)
  1196. {
  1197. size_t newIndex = 0;
  1198. if (!currentPseudonyms.empty())
  1199. while (otherCurrentPseudonyms[newIndex] == currentPseudonyms[newIndex])
  1200. newIndex++;
  1201. Curvepoint shortTermPublicKey = otherCurrentPseudonyms[newIndex];
  1202. bool flag = verify_proof_of_added_user(
  1203. pi,
  1204. currentFreshGenerator,
  1205. shortTermPublicKey,
  1206. bgnSystem.get_public_key().get_bipoint_curvegen(),
  1207. bgnSystem.get_public_key().get_bipoint_curve_subgroup_gen(),
  1208. bgnSystem.get_public_key().get_bipoint_twistgen(),
  1209. bgnSystem.get_public_key().get_bipoint_twist_subgroup_gen(),
  1210. newIndex,
  1211. otherCurrentUserEncryptedTallies[newIndex],
  1212. otherPreviousVoteTallies[newIndex],
  1213. otherVoteMatrix);
  1214. if (!flag)
  1215. {
  1216. std::cerr << "Other server added new user invalidly, aborting." << std::endl;
  1217. return false;
  1218. }
  1219. for (size_t i = 0; i < otherCurrentPseudonyms.size(); i++)
  1220. {
  1221. if (i == newIndex)
  1222. continue;
  1223. size_t otherI = (i > newIndex ? i - 1 : i);
  1224. flag = flag && otherCurrentPseudonyms[i] ==
  1225. currentPseudonyms[otherI];
  1226. flag = flag && otherCurrentUserEncryptedTallies[i] ==
  1227. currentUserEncryptedTallies[otherI];
  1228. flag = flag && otherPreviousVoteTallies[i] ==
  1229. previousVoteTallies[otherI];
  1230. for (size_t j = 0; j < otherCurrentPseudonyms.size(); j++)
  1231. {
  1232. if (j == newIndex)
  1233. continue;
  1234. size_t otherJ = (j > newIndex ? j - 1 : j);
  1235. flag = flag && otherVoteMatrix[i][j] ==
  1236. voteMatrix[otherI][otherJ];
  1237. }
  1238. }
  1239. if (!flag)
  1240. {
  1241. std::cerr << "Other server illicitly changed other value during new user add." << std::endl;
  1242. return false;
  1243. }
  1244. previousVoteTallies = otherPreviousVoteTallies;
  1245. currentPseudonyms = otherCurrentPseudonyms;
  1246. currentUserEncryptedTallies = otherCurrentUserEncryptedTallies;
  1247. voteMatrix = otherVoteMatrix;
  1248. return true;
  1249. }
  1250. /*
  1251. * DATA SAFEKEEPING
  1252. */
  1253. /* This is what powers the "shuffle"; really, as pseudonyms get updated,
  1254. * the pseudonyms are no longer in the order prescribed by operator<().
  1255. * So, we put them (and everything else) back into that order,
  1256. * effectively shuffling them (and making lookups easier later on). */
  1257. std::vector<size_t> PrsonaServer::order_data()
  1258. {
  1259. std::vector<size_t> retval = sort_data(currentPseudonyms);
  1260. // Order all other data in the same way, for consistency
  1261. std::vector<Curvepoint> newPseudonyms;
  1262. std::vector<TwistBipoint> newVoteTallies;
  1263. std::vector<EGCiphertext> newUserEncryptedTallies;
  1264. std::vector<std::vector<CurveBipoint>> newVoteMatrix;
  1265. for (size_t i = 0; i < retval.size(); i++)
  1266. {
  1267. newPseudonyms.push_back(currentPseudonyms[retval[i]]);
  1268. newVoteTallies.push_back(previousVoteTallies[retval[i]]);
  1269. if (!currentUserEncryptedTallies.empty())
  1270. {
  1271. newUserEncryptedTallies.push_back(
  1272. currentUserEncryptedTallies[retval[i]]);
  1273. }
  1274. std::vector<CurveBipoint> currNewRow;
  1275. for (size_t j = 0; j < currentPseudonyms.size(); j++)
  1276. {
  1277. currNewRow.push_back(
  1278. voteMatrix[retval[i]][retval[j]]);
  1279. }
  1280. newVoteMatrix.push_back(currNewRow);
  1281. }
  1282. previousVoteTallies = newVoteTallies;
  1283. currentPseudonyms = newPseudonyms;
  1284. currentUserEncryptedTallies = newUserEncryptedTallies;
  1285. voteMatrix = newVoteMatrix;
  1286. return retval;
  1287. }
  1288. /*
  1289. * BINARY SEARCH
  1290. */
  1291. // Completely normal binary search
  1292. size_t PrsonaServer::binary_search(const Curvepoint& index) const
  1293. {
  1294. return PrsonaBase::binary_search(currentPseudonyms, index);
  1295. }
  1296. /*
  1297. * VALID VOTE PROOFS
  1298. */
  1299. bool PrsonaServer::verify_vote_proof(
  1300. const std::vector<Proof>& pi,
  1301. const std::vector<CurveBipoint>& oldVotes,
  1302. const std::vector<CurveBipoint>& newVotes,
  1303. const Curvepoint& shortTermPublicKey) const
  1304. {
  1305. const BGNPublicKey& pubKey = bgnSystem.get_public_key();
  1306. return PrsonaBase::verify_vote_proof(
  1307. pubKey.get_bipoint_curvegen(),
  1308. pubKey.get_bipoint_curve_subgroup_gen(),
  1309. pi,
  1310. oldVotes,
  1311. newVotes,
  1312. currentFreshGenerator,
  1313. shortTermPublicKey);
  1314. }
  1315. void PrsonaServer::print_scores(const std::vector<TwistBipoint>& scores)
  1316. {
  1317. std::cout << "[";
  1318. for (size_t i = 0; i < scores.size(); i++)
  1319. {
  1320. std::cout << bgnSystem.decrypt(scores[i])
  1321. << (i == scores.size() - 1 ? "]" : " ");
  1322. }
  1323. std::cout << std::endl;
  1324. }