server.cpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460
  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>>>& freshPseudonymSeedCommits,
  326. std::vector<std::vector<std::vector<TwistBipoint>>>& serverTallyCommits,
  327. std::vector<std::vector<std::vector<std::vector<std::vector<CurveBipoint>>>>>& voteMatrixCommits,
  328. Curvepoint& nextGenerator)
  329. {
  330. nextSeed.set_random();
  331. std::vector<std::vector<Curvepoint>> currPermutationCommits;
  332. std::vector<std::vector<Curvepoint>> currFreshPseudonymCommits;
  333. std::vector<std::vector<Curvepoint>> currFreshPseudonymSeedCommits;
  334. std::vector<std::vector<TwistBipoint>> currServerTallyCommits;
  335. std::vector<std::vector<std::vector<std::vector<CurveBipoint>>>> currVoteMatrixCommits;
  336. std::vector<std::vector<std::vector<Curvepoint>>> currUserTallyCommits;
  337. std::vector<std::vector<Curvepoint>> currUserTallyMaskSeedCommits;
  338. pi.push_back(epoch_calculations(
  339. currPermutationCommits,
  340. currFreshPseudonymCommits,
  341. currFreshPseudonymSeedCommits,
  342. currServerTallyCommits,
  343. currVoteMatrixCommits,
  344. currUserTallyCommits,
  345. currUserTallyMaskSeedCommits,
  346. nextSeed,
  347. false));
  348. permutationCommits.push_back(currPermutationCommits);
  349. freshPseudonymCommits.push_back(currFreshPseudonymCommits);
  350. freshPseudonymSeedCommits.push_back(currFreshPseudonymSeedCommits);
  351. serverTallyCommits.push_back(currServerTallyCommits);
  352. voteMatrixCommits.push_back(currVoteMatrixCommits);
  353. pi[0][0].push_back(
  354. add_to_generator_proof(nextGenerator, nextSeed));
  355. nextGenerator = nextGenerator * nextSeed;
  356. }
  357. // In between these rounds, scores are tallied, decrypted,
  358. // and encrypted to fresh user pseudonyms (possible through weird math)
  359. // The second round, going from A_0.5 to A_1
  360. void PrsonaServer::break_down_midway_pseudonyms(
  361. std::vector<Proof>& generatorProof,
  362. std::vector<std::vector<std::vector<Proof>>>& pi,
  363. std::vector<std::vector<std::vector<Curvepoint>>>& permutationCommits,
  364. std::vector<std::vector<std::vector<Curvepoint>>>& freshPseudonymCommits,
  365. std::vector<std::vector<std::vector<Curvepoint>>>& freshPseudonymSeedCommits,
  366. std::vector<std::vector<std::vector<TwistBipoint>>>& serverTallyCommits,
  367. std::vector<std::vector<std::vector<std::vector<std::vector<CurveBipoint>>>>>& voteMatrixCommits,
  368. std::vector<std::vector<std::vector<std::vector<Curvepoint>>>>& userTallyCommits,
  369. std::vector<std::vector<std::vector<Curvepoint>>>& userTallyMaskSeedCommits,
  370. const Curvepoint& nextGenerator)
  371. {
  372. if (!initialize_fresh_generator(generatorProof, nextGenerator))
  373. {
  374. std::cerr << "New fresh generator could not be verified." << std::endl;
  375. return;
  376. }
  377. Scalar inverseSeed = currentSeed.curveMultInverse();
  378. std::vector<std::vector<Curvepoint>> currPermutationCommits;
  379. std::vector<std::vector<Curvepoint>> currFreshPseudonymCommits;
  380. std::vector<std::vector<Curvepoint>> currFreshPseudonymSeedCommits;
  381. std::vector<std::vector<TwistBipoint>> currServerTallyCommits;
  382. std::vector<std::vector<std::vector<std::vector<CurveBipoint>>>> currVoteMatrixCommits;
  383. std::vector<std::vector<std::vector<Curvepoint>>> currUserTallyCommits;
  384. std::vector<std::vector<Curvepoint>> currUserTallyMaskSeedCommits;
  385. pi.push_back(epoch_calculations(
  386. currPermutationCommits,
  387. currFreshPseudonymCommits,
  388. currFreshPseudonymSeedCommits,
  389. currServerTallyCommits,
  390. currVoteMatrixCommits,
  391. currUserTallyCommits,
  392. currUserTallyMaskSeedCommits,
  393. inverseSeed,
  394. true));
  395. permutationCommits.push_back(currPermutationCommits);
  396. freshPseudonymCommits.push_back(currFreshPseudonymCommits);
  397. freshPseudonymSeedCommits.push_back(currFreshPseudonymSeedCommits);
  398. serverTallyCommits.push_back(currServerTallyCommits);
  399. voteMatrixCommits.push_back(currVoteMatrixCommits);
  400. userTallyCommits.push_back(currUserTallyCommits);
  401. userTallyMaskSeedCommits.push_back(currUserTallyMaskSeedCommits);
  402. currentSeed = nextSeed;
  403. }
  404. /*
  405. * EPOCH HELPERS
  406. */
  407. std::vector<std::vector<Proof>> PrsonaServer::epoch_calculations(
  408. std::vector<std::vector<Curvepoint>>& permutationCommits,
  409. std::vector<std::vector<Curvepoint>>& freshPseudonymCommits,
  410. std::vector<std::vector<Curvepoint>>& freshPseudonymSeedCommits,
  411. std::vector<std::vector<TwistBipoint>>& serverTallyCommits,
  412. std::vector<std::vector<std::vector<std::vector<CurveBipoint>>>>& voteMatrixCommits,
  413. std::vector<std::vector<std::vector<Curvepoint>>>& userTallyCommits,
  414. std::vector<std::vector<Curvepoint>> & userTallyMaskSeedCommits,
  415. const Scalar& power,
  416. bool doUserTallies)
  417. {
  418. std::vector<std::vector<Proof>> retval;
  419. std::vector<Curvepoint> nextPseudonyms;
  420. for (size_t i = 0; i < currentPseudonyms.size(); i++)
  421. nextPseudonyms.push_back(currentPseudonyms[i] * power);
  422. std::vector<size_t> order = sort_data(nextPseudonyms);
  423. for (size_t i = 0; i < currentPseudonyms.size(); i++)
  424. nextPseudonyms[i] = currentPseudonyms[order[i]] * power;
  425. std::cout << "Generating permutation matrix." << std::endl;
  426. std::vector<std::vector<Scalar>> permutations =
  427. generate_permutation_matrix(power);
  428. std::cout << "Generating permutation commitment matrix." << std::endl;
  429. std::vector<std::vector<Scalar>> permutationSeeds;
  430. permutationCommits.clear();
  431. permutationCommits =
  432. generate_commitment_matrix(permutations, permutationSeeds);
  433. std::cout << "Generating permutation proof." << std::endl;
  434. retval.push_back(generate_valid_permutation_proof(
  435. permutations, permutationSeeds, permutationCommits));
  436. std::cout << "Generating pseudonym matrix." << std::endl;
  437. std::vector<std::vector<Scalar>> freshPseudonymSeeds;
  438. freshPseudonymSeedCommits.clear();
  439. freshPseudonymCommits.clear();
  440. freshPseudonymCommits =
  441. generate_pseudonym_matrix(
  442. permutations,
  443. power,
  444. freshPseudonymSeeds,
  445. freshPseudonymSeedCommits);
  446. // for (size_t i = 0; i < freshPseudonymCommits.size(); i++)
  447. // {
  448. // Curvepoint sum = freshPseudonymCommits[i][0];
  449. // for (size_t j = 1; j < freshPseudonymCommits[i].size(); j++)
  450. // sum = sum + freshPseudonymCommits[i][j];
  451. // std::cout << "Fresh pseudonym commit row " << i + 1 << " of " << permutationCommits.size()
  452. // << (sum == nextPseudonyms[i] ? ": PASS" : ": FAIL") << std::endl;
  453. // }
  454. std::cout << "Generating pseudonym proof." << std::endl;
  455. retval.push_back(
  456. generate_proof_of_reordering_plus_power(
  457. permutations,
  458. power,
  459. permutationSeeds,
  460. freshPseudonymSeeds,
  461. currentPseudonyms,
  462. permutationCommits,
  463. freshPseudonymCommits,
  464. freshPseudonymSeedCommits));
  465. std::cout << "Generating server tally matrix." << std::endl;
  466. std::vector<std::vector<Scalar>> serverTallySeeds;
  467. serverTallyCommits.clear();
  468. serverTallyCommits =
  469. generate_server_tally_matrix(
  470. permutations,
  471. serverTallySeeds);
  472. std::cout << "Generating server tally proof." << std::endl;
  473. retval.push_back(
  474. generate_proof_of_reordering<TwistBipoint>(
  475. permutations,
  476. permutationSeeds,
  477. serverTallySeeds,
  478. previousVoteTallies,
  479. permutationCommits,
  480. serverTallyCommits,
  481. bgnSystem.get_public_key().get_bipoint_twistgen(),
  482. bgnSystem.get_public_key().get_bipoint_twist_subgroup_gen()));
  483. std::cout << "Doing V * P." << std::endl;
  484. std::vector<std::vector<std::vector<Scalar>>> firstVoteMatrixSeeds;
  485. std::vector<std::vector<std::vector<Scalar>>> secondVoteMatrixSeeds;
  486. voteMatrixCommits.push_back(
  487. generate_vote_tensor(
  488. permutations,
  489. voteMatrix,
  490. firstVoteMatrixSeeds,
  491. false));
  492. std::cout << "Finishing V * P calculation." << std::endl;
  493. std::vector<std::vector<CurveBipoint>> partialVoteMatrix =
  494. calculate_next_vote_matrix(voteMatrixCommits[0]);
  495. std::cout << "Doing P^T * (V * P)." << std::endl;
  496. voteMatrixCommits.push_back(
  497. generate_vote_tensor(
  498. permutations,
  499. partialVoteMatrix,
  500. secondVoteMatrixSeeds,
  501. true));
  502. std::cout << "Proving V * P." << std::endl;
  503. generate_vote_tensor_proofs(
  504. retval,
  505. permutations,
  506. permutationSeeds,
  507. firstVoteMatrixSeeds,
  508. voteMatrix,
  509. permutationCommits,
  510. voteMatrixCommits[0],
  511. false);
  512. std::cout << "Proving P^T * (V * P)." << std::endl;
  513. generate_vote_tensor_proofs(
  514. retval,
  515. permutations,
  516. permutationSeeds,
  517. secondVoteMatrixSeeds,
  518. partialVoteMatrix,
  519. permutationCommits,
  520. voteMatrixCommits[1],
  521. true);
  522. if (doUserTallies)
  523. {
  524. std::cout << "Generating user tally matrix." << std::endl;
  525. std::vector<Curvepoint> userTallyMasks;
  526. std::vector<std::vector<Scalar>> userTallyMaskSeeds;
  527. userTallyMaskSeedCommits.clear();
  528. std::vector<Curvepoint> userTallyMessages;
  529. std::vector<std::vector<Scalar>> userTallyMessageSeeds;
  530. userTallyCommits =
  531. generate_user_tally_matrix(
  532. permutations,
  533. power,
  534. userTallyMasks,
  535. userTallyMessages,
  536. userTallyMaskSeeds,
  537. userTallyMaskSeedCommits,
  538. userTallyMessageSeeds);
  539. std::cout << "Proving user tally mask matrix." << std::endl;
  540. retval.push_back(
  541. generate_proof_of_reordering_plus_power(
  542. permutations,
  543. power,
  544. permutationSeeds,
  545. userTallyMaskSeeds,
  546. userTallyMasks,
  547. permutationCommits,
  548. userTallyCommits[0],
  549. userTallyMaskSeedCommits));
  550. std::cout << "Proving user tally message matrix." << std::endl;
  551. retval.push_back(
  552. generate_proof_of_reordering<Curvepoint>(
  553. permutations,
  554. permutationSeeds,
  555. userTallyMessageSeeds,
  556. userTallyMessages,
  557. permutationCommits,
  558. userTallyCommits[1],
  559. EL_GAMAL_GENERATOR,
  560. elGamalBlindGenerator));
  561. }
  562. std::cout << "Giving self updates." << std::endl;
  563. // Replace internal values
  564. update_data(
  565. freshPseudonymCommits,
  566. serverTallyCommits,
  567. voteMatrixCommits[1],
  568. userTallyCommits);
  569. return retval;
  570. }
  571. bool PrsonaServer::accept_epoch_updates(
  572. const std::vector<std::vector<Proof>>& pi,
  573. const std::vector<std::vector<Curvepoint>>& permutationCommits,
  574. const std::vector<std::vector<Curvepoint>>& freshPseudonymCommits,
  575. const std::vector<std::vector<Curvepoint>>& freshPseudonymSeedCommits,
  576. const std::vector<std::vector<TwistBipoint>>& serverTallyCommits,
  577. const std::vector<std::vector<std::vector<std::vector<CurveBipoint>>>>& voteMatrixCommits,
  578. const std::vector<std::vector<std::vector<Curvepoint>>>& userTallyCommits,
  579. const std::vector<std::vector<Curvepoint>>& userTallyMaskSeedCommits,
  580. bool doUserTallies)
  581. {
  582. bool verification;
  583. if ((userTallyCommits.empty() && doUserTallies) || (!userTallyCommits.empty() && !doUserTallies))
  584. {
  585. std::cerr << "user tallies are not in expected state." << std::endl;
  586. return false;
  587. }
  588. if (pi.empty())
  589. return false;
  590. std::cout << "Verifying valid permutation matrix." << std::endl;
  591. verification =
  592. verify_valid_permutation_proof(pi[0], permutationCommits);
  593. if (!verification)
  594. {
  595. std::cerr << "Could not verify valid permutation matrix." << std::endl;
  596. return false;
  597. }
  598. std::cout << "Verifying valid pseudonym vector." << std::endl;
  599. verification =
  600. verify_proof_of_reordering_plus_power(
  601. pi[1],
  602. currentPseudonyms,
  603. permutationCommits,
  604. freshPseudonymCommits,
  605. freshPseudonymSeedCommits);
  606. if (!verification)
  607. {
  608. std::cerr << "Could not verify valid pseudonym vector." << std::endl;
  609. return false;
  610. }
  611. std::cout << "Verifying valid server tally vector." << std::endl;
  612. verification =
  613. verify_proof_of_reordering<TwistBipoint>(
  614. pi[2],
  615. previousVoteTallies,
  616. permutationCommits,
  617. serverTallyCommits,
  618. bgnSystem.get_public_key().get_bipoint_twistgen(),
  619. bgnSystem.get_public_key().get_bipoint_twist_subgroup_gen());
  620. if (!verification)
  621. {
  622. std::cerr << "Could not verify valid server tally vector." << std::endl;
  623. return false;
  624. }
  625. std::cout << "Verifying valid first half vote matrix." << std::endl;
  626. size_t currOffset = 3;
  627. verification = verify_vote_tensor_proofs(
  628. pi,
  629. currOffset,
  630. voteMatrix,
  631. permutationCommits,
  632. voteMatrixCommits[0],
  633. false);
  634. if (!verification)
  635. {
  636. std::cerr << "Could not verify first half vote matrix." << std::endl;
  637. return false;
  638. }
  639. std::cout << "Verifying valid second half vote matrix." << std::endl;
  640. std::vector<std::vector<CurveBipoint>> partialVoteMatrix =
  641. calculate_next_vote_matrix(voteMatrixCommits[0]);
  642. currOffset += voteMatrix.size();
  643. verification = verify_vote_tensor_proofs(
  644. pi,
  645. currOffset,
  646. partialVoteMatrix,
  647. permutationCommits,
  648. voteMatrixCommits[1],
  649. true);
  650. if (!verification)
  651. {
  652. std::cerr << "Could not verify second half vote matrix." << std::endl;
  653. return false;
  654. }
  655. currOffset += voteMatrix.size();
  656. if (doUserTallies)
  657. {
  658. std::vector<Curvepoint> userTallyMasks;
  659. std::vector<Curvepoint> userTallyMessages;
  660. for (size_t i = 0; i < currentUserEncryptedTallies.size(); i++)
  661. {
  662. userTallyMasks.push_back(currentUserEncryptedTallies[i].mask);
  663. userTallyMessages.push_back(currentUserEncryptedTallies[i].encryptedMessage);
  664. }
  665. std::cout << "Verifying valid user tally masks." << std::endl;
  666. verification = verify_proof_of_reordering_plus_power(
  667. pi[currOffset],
  668. userTallyMasks,
  669. permutationCommits,
  670. userTallyCommits[0],
  671. userTallyMaskSeedCommits);
  672. if (!verification)
  673. {
  674. std::cerr << "Could not verify user tally masks." << std::endl;
  675. return false;
  676. }
  677. currOffset++;
  678. std::cout << "Verifying valid user tally messages." << std::endl;
  679. verification = verify_proof_of_reordering<Curvepoint>(
  680. pi[currOffset],
  681. userTallyMessages,
  682. permutationCommits,
  683. userTallyCommits[1],
  684. EL_GAMAL_GENERATOR,
  685. elGamalBlindGenerator);
  686. if (!verification)
  687. {
  688. std::cerr << "Could not verify user tally messages." << std::endl;
  689. return false;
  690. }
  691. }
  692. std::cout << "Verifying pseudonyms are all different." << std::endl;
  693. verification = update_data(
  694. freshPseudonymCommits,
  695. serverTallyCommits,
  696. voteMatrixCommits[1],
  697. userTallyCommits);
  698. return verification;
  699. }
  700. std::vector<std::vector<Scalar>> PrsonaServer::generate_permutation_matrix(
  701. const Scalar& reorderSeed) const
  702. {
  703. std::vector<std::vector<Scalar>> retval;
  704. for (size_t i = 0; i < currentPseudonyms.size(); i++)
  705. {
  706. std::vector<Scalar> currRow;
  707. for (size_t j = 0; j < currentPseudonyms.size(); j++)
  708. currRow.push_back(Scalar(0));
  709. retval.push_back(currRow);
  710. }
  711. std::vector<Curvepoint> nextPseudonyms;
  712. for (size_t i = 0; i < currentPseudonyms.size(); i++)
  713. nextPseudonyms.push_back(currentPseudonyms[i] * reorderSeed);
  714. std::vector<size_t> order = sort_data(nextPseudonyms);
  715. for (size_t i = 0; i < order.size(); i++)
  716. retval[order[i]][i] = Scalar(1);
  717. std::cout << "[";
  718. for (size_t i = 0; i < order.size(); i++)
  719. {
  720. std::cout << "[";
  721. for (size_t j = 0; j < order.size(); j++)
  722. {
  723. std::cout << retval[i][j] << (j == order.size() - 1 ? "]" : " ");
  724. }
  725. std::cout << (i == order.size() - 1 ? "]" : " ") << std::endl << (i == order.size() - 1 ? "" : " ");
  726. }
  727. return retval;
  728. }
  729. std::vector<std::vector<Curvepoint>> PrsonaServer::generate_commitment_matrix(
  730. const std::vector<std::vector<Scalar>>& permutations,
  731. std::vector<std::vector<Scalar>>& seeds) const
  732. {
  733. std::vector<std::vector<Curvepoint>> retval;
  734. Curvepoint g = EL_GAMAL_GENERATOR;
  735. Curvepoint h = elGamalBlindGenerator;
  736. seeds.clear();
  737. for (size_t i = 0; i < permutations.size(); i++)
  738. {
  739. std::vector<Scalar> currSeeds;
  740. for (size_t j = 0; j < permutations[i].size(); j++)
  741. currSeeds.push_back(Scalar(0));
  742. seeds.push_back(currSeeds);
  743. }
  744. for (size_t i = 0; i < permutations.size(); i++)
  745. {
  746. std::vector<Curvepoint> currRow;
  747. size_t last = permutations[i].size() - 1;
  748. for (size_t j = 0; j < permutations[i].size(); j++)
  749. {
  750. Curvepoint element;
  751. if (j != last)
  752. {
  753. seeds[i][j].set_random();
  754. seeds[i][last] = seeds[i][last] - seeds[i][j];
  755. }
  756. element = g * permutations[i][j] + h * seeds[i][j];
  757. currRow.push_back(element);
  758. }
  759. retval.push_back(currRow);
  760. }
  761. return retval;
  762. }
  763. std::vector<std::vector<Curvepoint>> PrsonaServer::generate_pseudonym_matrix(
  764. const std::vector<std::vector<Scalar>>& permutations,
  765. const Scalar& power,
  766. std::vector<std::vector<Scalar>>& seeds,
  767. std::vector<std::vector<Curvepoint>>& seedCommits) const
  768. {
  769. return generate_reordered_plus_power_matrix<Curvepoint>(
  770. permutations,
  771. power,
  772. currentPseudonyms,
  773. seeds,
  774. seedCommits,
  775. elGamalBlindGenerator);
  776. }
  777. std::vector<std::vector<TwistBipoint>> PrsonaServer::generate_server_tally_matrix(
  778. const std::vector<std::vector<Scalar>>& permutations,
  779. std::vector<std::vector<Scalar>>& seeds) const
  780. {
  781. return generate_reordered_matrix<TwistBipoint>(
  782. permutations,
  783. previousVoteTallies,
  784. seeds,
  785. bgnSystem.get_public_key().get_bipoint_twist_subgroup_gen(),
  786. false);
  787. }
  788. std::vector<std::vector<std::vector<CurveBipoint>>> PrsonaServer::generate_vote_tensor(
  789. const std::vector<std::vector<Scalar>>& permutations,
  790. const std::vector<std::vector<CurveBipoint>>& currVoteMatrix,
  791. std::vector<std::vector<std::vector<Scalar>>>& seeds,
  792. bool inverted) const
  793. {
  794. std::vector<std::vector<std::vector<CurveBipoint>>> retval;
  795. for (size_t i = 0; i < currVoteMatrix.size(); i++)
  796. {
  797. std::vector<std::vector<Scalar>> currSeeds;
  798. std::vector<CurveBipoint> inputRow;
  799. if (inverted)
  800. {
  801. for (size_t j = 0; j < currVoteMatrix.size(); j++)
  802. inputRow.push_back(currVoteMatrix[j][i]);
  803. }
  804. else
  805. {
  806. inputRow = currVoteMatrix[i];
  807. }
  808. retval.push_back(generate_reordered_matrix<CurveBipoint>(
  809. permutations,
  810. inputRow,
  811. currSeeds,
  812. bgnSystem.get_public_key().get_bipoint_curve_subgroup_gen(),
  813. false));
  814. seeds.push_back(currSeeds);
  815. }
  816. return retval;
  817. }
  818. std::vector<std::vector<CurveBipoint>> PrsonaServer::calculate_next_vote_matrix(
  819. const std::vector<std::vector<std::vector<CurveBipoint>>>& voteTensor) const
  820. {
  821. std::vector<std::vector<CurveBipoint>> retval;
  822. for (size_t i = 0; i < voteTensor.size(); i++)
  823. {
  824. std::vector<CurveBipoint> currRow;
  825. for (size_t j = 0; j < voteTensor[i].size(); j++)
  826. {
  827. CurveBipoint sum = voteTensor[i][j][0];
  828. for (size_t k = 1; k < voteTensor[i][j].size(); k++)
  829. sum = sum + voteTensor[i][j][k];
  830. currRow.push_back(sum);
  831. }
  832. retval.push_back(currRow);
  833. }
  834. return retval;
  835. }
  836. void PrsonaServer::generate_vote_tensor_proofs(
  837. std::vector<std::vector<Proof>>& pi,
  838. const std::vector<std::vector<Scalar>>& permutations,
  839. const std::vector<std::vector<Scalar>>& permutationSeeds,
  840. const std::vector<std::vector<std::vector<Scalar>>>& matrixSeeds,
  841. const std::vector<std::vector<CurveBipoint>>& currMatrix,
  842. const std::vector<std::vector<Curvepoint>>& permutationCommits,
  843. const std::vector<std::vector<std::vector<CurveBipoint>>>& matrixCommits,
  844. bool inverted) const
  845. {
  846. for (size_t i = 0; i < currMatrix.size(); i++)
  847. {
  848. std::vector<CurveBipoint> inputRow;
  849. if (inverted)
  850. {
  851. for (size_t j = 0; j < currMatrix.size(); j++)
  852. inputRow.push_back(currMatrix[j][i]);
  853. }
  854. else
  855. {
  856. inputRow = currMatrix[i];
  857. }
  858. pi.push_back(generate_proof_of_reordering<CurveBipoint>(
  859. permutations,
  860. permutationSeeds,
  861. matrixSeeds[i],
  862. inputRow,
  863. permutationCommits,
  864. matrixCommits[i],
  865. bgnSystem.get_public_key().get_bipoint_curvegen(),
  866. bgnSystem.get_public_key().get_bipoint_curve_subgroup_gen()));
  867. }
  868. }
  869. bool PrsonaServer::verify_vote_tensor_proofs(
  870. const std::vector<std::vector<Proof>>& pi,
  871. size_t start_offset,
  872. const std::vector<std::vector<CurveBipoint>>& currMatrix,
  873. const std::vector<std::vector<Curvepoint>>& permutationCommits,
  874. const std::vector<std::vector<std::vector<CurveBipoint>>>& matrixCommits,
  875. bool inverted) const
  876. {
  877. bool retval = true;
  878. for (size_t i = 0; i < currMatrix.size(); i++)
  879. {
  880. std::vector<CurveBipoint> inputRow;
  881. if (inverted)
  882. {
  883. for (size_t j = 0; j < currMatrix.size(); j++)
  884. inputRow.push_back(currMatrix[j][i]);
  885. }
  886. else
  887. {
  888. inputRow = currMatrix[i];
  889. }
  890. size_t whichProof = i + start_offset;
  891. retval = retval && verify_proof_of_reordering<CurveBipoint>(
  892. pi[whichProof],
  893. inputRow,
  894. permutationCommits,
  895. matrixCommits[i],
  896. bgnSystem.get_public_key().get_bipoint_curvegen(),
  897. bgnSystem.get_public_key().get_bipoint_curve_subgroup_gen());
  898. }
  899. return retval;
  900. }
  901. std::vector<std::vector<std::vector<Curvepoint>>> PrsonaServer::generate_user_tally_matrix(
  902. const std::vector<std::vector<Scalar>>& permutations,
  903. const Scalar& power,
  904. std::vector<Curvepoint>& masks,
  905. std::vector<Curvepoint>& messages,
  906. std::vector<std::vector<Scalar>>& maskSeeds,
  907. std::vector<std::vector<Curvepoint>>& maskSeedCommits,
  908. std::vector<std::vector<Scalar>>& messageSeeds) const
  909. {
  910. std::vector<std::vector<std::vector<Curvepoint>>> retval;
  911. masks.clear();
  912. messages.clear();
  913. for (size_t i = 0; i < currentUserEncryptedTallies.size(); i++)
  914. {
  915. masks.push_back(currentUserEncryptedTallies[i].mask);
  916. messages.push_back(currentUserEncryptedTallies[i].encryptedMessage);
  917. }
  918. retval.push_back(
  919. generate_reordered_plus_power_matrix<Curvepoint>(
  920. permutations,
  921. power,
  922. masks,
  923. maskSeeds,
  924. maskSeedCommits,
  925. elGamalBlindGenerator));
  926. retval.push_back(
  927. generate_reordered_matrix<Curvepoint>(
  928. permutations,
  929. messages,
  930. messageSeeds,
  931. elGamalBlindGenerator,
  932. false));
  933. return retval;
  934. }
  935. template <typename T>
  936. std::vector<std::vector<T>> PrsonaServer::generate_reordered_plus_power_matrix(
  937. const std::vector<std::vector<Scalar>>& permutations,
  938. const Scalar& power,
  939. const std::vector<T>& oldValues,
  940. std::vector<std::vector<Scalar>>& seeds,
  941. std::vector<std::vector<Curvepoint>>& seedCommits,
  942. const T& h) const
  943. {
  944. std::vector<std::vector<Scalar>> permutation_plus_power;
  945. seedCommits.clear();
  946. for (size_t i = 0; i < permutations.size(); i++)
  947. {
  948. std::vector<Scalar> currPermutations;
  949. std::vector<Curvepoint> currSeedCommits;
  950. for (size_t j = 0; j < permutations[i].size(); j++)
  951. {
  952. currPermutations.push_back(permutations[i][j] * power);
  953. currSeedCommits.push_back(Curvepoint());
  954. }
  955. permutation_plus_power.push_back(currPermutations);
  956. seedCommits.push_back(currSeedCommits);
  957. }
  958. std::vector<std::vector<T>> retval =
  959. generate_reordered_matrix<T>(
  960. permutation_plus_power,
  961. oldValues,
  962. seeds,
  963. h,
  964. true);
  965. for (size_t i = 0; i < permutations.size(); i++)
  966. for (size_t j = 0; j < permutations[i].size(); j++)
  967. seedCommits[i][j] = EL_GAMAL_GENERATOR * seeds[i][j];
  968. return retval;
  969. }
  970. template <typename T>
  971. std::vector<std::vector<T>> PrsonaServer::generate_reordered_matrix(
  972. const std::vector<std::vector<Scalar>>& permutations,
  973. const std::vector<T>& oldValues,
  974. std::vector<std::vector<Scalar>>& seeds,
  975. const T& h,
  976. bool cancelOut) const
  977. {
  978. std::vector<std::vector<T>> retval;
  979. seeds.clear();
  980. for (size_t i = 0; i < permutations.size(); i++)
  981. {
  982. std::vector<Scalar> currSeeds;
  983. std::vector<T> currRow;
  984. for (size_t j = 0; j < permutations[i].size(); j++)\
  985. {
  986. currSeeds.push_back(Scalar(0));
  987. currRow.push_back(T());
  988. }
  989. seeds.push_back(currSeeds);
  990. retval.push_back(currRow);
  991. }
  992. for (size_t i = 0; i < permutations.size(); i++)
  993. {
  994. size_t last = permutations[i].size() - 1;
  995. for (size_t j = 0; j < permutations[i].size(); j++)
  996. {
  997. if (!cancelOut)
  998. {
  999. seeds[i][j].set_random();
  1000. }
  1001. else if (j != last)
  1002. {
  1003. seeds[i][j].set_random();
  1004. seeds[i][last] = seeds[i][last] - seeds[i][j];
  1005. }
  1006. retval[i][j] = oldValues[j] * permutations[j][i] + h * seeds[i][j];
  1007. }
  1008. }
  1009. return retval;
  1010. }
  1011. std::vector<size_t> PrsonaServer::sort_data(const std::vector<Curvepoint>& inputs) const
  1012. {
  1013. std::vector<size_t> retval;
  1014. // SortingType's index member allows us to replicate the "sort" across
  1015. std::vector<SortingType> sortTracker;
  1016. for (size_t i = 0; i < inputs.size(); i++)
  1017. {
  1018. SortingType curr;
  1019. curr.pseudonym = inputs[i];
  1020. curr.index = i;
  1021. sortTracker.push_back(curr);
  1022. }
  1023. std::sort(sortTracker.begin(), sortTracker.end());
  1024. for (size_t i = 0; i < inputs.size(); i++)
  1025. retval.push_back(sortTracker[i].index);
  1026. return retval;
  1027. }
  1028. bool PrsonaServer::update_data(
  1029. const std::vector<std::vector<Curvepoint>>& freshPseudonymCommits,
  1030. const std::vector<std::vector<TwistBipoint>>& serverTallyCommits,
  1031. const std::vector<std::vector<std::vector<CurveBipoint>>>& voteMatrixCommits,
  1032. const std::vector<std::vector<std::vector<Curvepoint>>>& userTallyCommits)
  1033. {
  1034. std::vector<Curvepoint> newPseudonyms;
  1035. std::vector<TwistBipoint> newVoteTallies;
  1036. std::vector<EGCiphertext> newUserTallies;
  1037. for (size_t i = 0; i < freshPseudonymCommits.size(); i++)
  1038. {
  1039. Curvepoint pseudonymSum = freshPseudonymCommits[i][0];
  1040. TwistBipoint voteTallySum = serverTallyCommits[i][0];
  1041. Curvepoint userTallyMask, userTallyMessage;
  1042. if (!userTallyCommits.empty())
  1043. {
  1044. userTallyMask = userTallyCommits[i][0][0];
  1045. userTallyMessage = userTallyCommits[i][0][1];
  1046. }
  1047. for (size_t j = 1; j < freshPseudonymCommits[i].size(); j++)
  1048. {
  1049. pseudonymSum = pseudonymSum + freshPseudonymCommits[i][j];
  1050. voteTallySum = voteTallySum + serverTallyCommits[i][j];
  1051. if (!userTallyCommits.empty())
  1052. {
  1053. userTallyMask = userTallyMask +
  1054. userTallyCommits[i][j][0];
  1055. userTallyMessage = userTallyMessage +
  1056. userTallyCommits[i][j][1];
  1057. }
  1058. }
  1059. newPseudonyms.push_back(pseudonymSum);
  1060. newVoteTallies.push_back(voteTallySum);
  1061. if (!userTallyCommits.empty())
  1062. {
  1063. newUserTallies.push_back(
  1064. EGCiphertext(userTallyMask, userTallyMessage));
  1065. }
  1066. }
  1067. if (!pseudonyms_sorted(newPseudonyms))
  1068. {
  1069. std::cerr << "Pseudonyms not sorted correctly." << std::endl;
  1070. return false;
  1071. }
  1072. currentPseudonyms = newPseudonyms;
  1073. previousVoteTallies = newVoteTallies;
  1074. voteMatrix = calculate_next_vote_matrix(voteMatrixCommits);
  1075. currentUserEncryptedTallies = newUserTallies;
  1076. return true;
  1077. }
  1078. bool PrsonaServer::pseudonyms_sorted(
  1079. const std::vector<Curvepoint> newPseudonyms) const
  1080. {
  1081. bool retval = true;
  1082. for (size_t i = 0; i < newPseudonyms.size() - 1; i++)
  1083. retval = retval && (newPseudonyms[i] < newPseudonyms[i + 1]);
  1084. return retval;
  1085. }
  1086. /*
  1087. * DATA MAINTENANCE
  1088. */
  1089. bool PrsonaServer::import_new_user_update(
  1090. const std::vector<Proof>& pi,
  1091. const std::vector<TwistBipoint>& otherPreviousVoteTallies,
  1092. const std::vector<Curvepoint>& otherCurrentPseudonyms,
  1093. const std::vector<EGCiphertext>& otherCurrentUserEncryptedTallies,
  1094. const std::vector<std::vector<CurveBipoint>>& otherVoteMatrix)
  1095. {
  1096. size_t newIndex = 0;
  1097. if (!currentPseudonyms.empty())
  1098. while (otherCurrentPseudonyms[newIndex] == currentPseudonyms[newIndex])
  1099. newIndex++;
  1100. Curvepoint shortTermPublicKey = otherCurrentPseudonyms[newIndex];
  1101. bool flag = verify_proof_of_added_user(
  1102. pi,
  1103. currentFreshGenerator,
  1104. shortTermPublicKey,
  1105. bgnSystem.get_public_key().get_bipoint_curvegen(),
  1106. bgnSystem.get_public_key().get_bipoint_curve_subgroup_gen(),
  1107. bgnSystem.get_public_key().get_bipoint_twistgen(),
  1108. bgnSystem.get_public_key().get_bipoint_twist_subgroup_gen(),
  1109. newIndex,
  1110. otherCurrentUserEncryptedTallies[newIndex],
  1111. otherPreviousVoteTallies[newIndex],
  1112. otherVoteMatrix);
  1113. if (!flag)
  1114. {
  1115. std::cerr << "Other server added new user invalidly, aborting." << std::endl;
  1116. return false;
  1117. }
  1118. for (size_t i = 0; i < otherCurrentPseudonyms.size(); i++)
  1119. {
  1120. if (i == newIndex)
  1121. continue;
  1122. size_t otherI = (i > newIndex ? i - 1 : i);
  1123. flag = flag && otherCurrentPseudonyms[i] ==
  1124. currentPseudonyms[otherI];
  1125. flag = flag && otherCurrentUserEncryptedTallies[i] ==
  1126. currentUserEncryptedTallies[otherI];
  1127. flag = flag && otherPreviousVoteTallies[i] ==
  1128. previousVoteTallies[otherI];
  1129. for (size_t j = 0; j < otherCurrentPseudonyms.size(); j++)
  1130. {
  1131. if (j == newIndex)
  1132. continue;
  1133. size_t otherJ = (j > newIndex ? j - 1 : j);
  1134. flag = flag && otherVoteMatrix[i][j] ==
  1135. voteMatrix[otherI][otherJ];
  1136. }
  1137. }
  1138. if (!flag)
  1139. {
  1140. std::cerr << "Other server illicitly changed other value during new user add." << std::endl;
  1141. return false;
  1142. }
  1143. previousVoteTallies = otherPreviousVoteTallies;
  1144. currentPseudonyms = otherCurrentPseudonyms;
  1145. currentUserEncryptedTallies = otherCurrentUserEncryptedTallies;
  1146. voteMatrix = otherVoteMatrix;
  1147. return true;
  1148. }
  1149. /*
  1150. * DATA SAFEKEEPING
  1151. */
  1152. /* This is what powers the "shuffle"; really, as pseudonyms get updated,
  1153. * the pseudonyms are no longer in the order prescribed by operator<().
  1154. * So, we put them (and everything else) back into that order,
  1155. * effectively shuffling them (and making lookups easier later on). */
  1156. std::vector<size_t> PrsonaServer::order_data()
  1157. {
  1158. std::vector<size_t> retval = sort_data(currentPseudonyms);
  1159. // Order all other data in the same way, for consistency
  1160. std::vector<Curvepoint> newPseudonyms;
  1161. std::vector<TwistBipoint> newVoteTallies;
  1162. std::vector<EGCiphertext> newUserEncryptedTallies;
  1163. std::vector<std::vector<CurveBipoint>> newVoteMatrix;
  1164. for (size_t i = 0; i < retval.size(); i++)
  1165. {
  1166. newPseudonyms.push_back(currentPseudonyms[retval[i]]);
  1167. newVoteTallies.push_back(previousVoteTallies[retval[i]]);
  1168. if (!currentUserEncryptedTallies.empty())
  1169. {
  1170. newUserEncryptedTallies.push_back(
  1171. currentUserEncryptedTallies[retval[i]]);
  1172. }
  1173. std::vector<CurveBipoint> currNewRow;
  1174. for (size_t j = 0; j < currentPseudonyms.size(); j++)
  1175. {
  1176. currNewRow.push_back(
  1177. voteMatrix[retval[i]][retval[j]]);
  1178. }
  1179. newVoteMatrix.push_back(currNewRow);
  1180. }
  1181. previousVoteTallies = newVoteTallies;
  1182. currentPseudonyms = newPseudonyms;
  1183. currentUserEncryptedTallies = newUserEncryptedTallies;
  1184. voteMatrix = newVoteMatrix;
  1185. return retval;
  1186. }
  1187. /*
  1188. * BINARY SEARCH
  1189. */
  1190. // Completely normal binary search
  1191. size_t PrsonaServer::binary_search(const Curvepoint& index) const
  1192. {
  1193. return PrsonaBase::binary_search(currentPseudonyms, index);
  1194. }
  1195. /*
  1196. * VALID VOTE PROOFS
  1197. */
  1198. bool PrsonaServer::verify_vote_proof(
  1199. const std::vector<Proof>& pi,
  1200. const std::vector<CurveBipoint>& oldVotes,
  1201. const std::vector<CurveBipoint>& newVotes,
  1202. const Curvepoint& shortTermPublicKey) const
  1203. {
  1204. const BGNPublicKey& pubKey = bgnSystem.get_public_key();
  1205. return PrsonaBase::verify_vote_proof(
  1206. pubKey.get_bipoint_curvegen(),
  1207. pubKey.get_bipoint_curve_subgroup_gen(),
  1208. pi,
  1209. oldVotes,
  1210. newVotes,
  1211. currentFreshGenerator,
  1212. shortTermPublicKey);
  1213. }
  1214. void PrsonaServer::print_scores(const std::vector<TwistBipoint>& scores)
  1215. {
  1216. std::cout << "[";
  1217. for (size_t i = 0; i < scores.size(); i++)
  1218. {
  1219. std::cout << bgnSystem.decrypt(scores[i])
  1220. << (i == scores.size() - 1 ? "]" : " ");
  1221. }
  1222. std::cout << std::endl;
  1223. }