base.cpp 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350
  1. #include <iostream>
  2. #include <fstream>
  3. #include "base.hpp"
  4. extern const scalar_t bn_n;
  5. extern const twistpoint_fp2_t bn_twistgen;
  6. /* These lines need to be here so these static variables are defined,
  7. * but in C++ putting code here doesn't actually execute
  8. * (or at least, with g++, whenever it would execute is not at a useful time)
  9. * so we have an init() function to actually put the correct values in them. */
  10. size_t PrsonaBase::MAX_ALLOWED_VOTE = 2;
  11. Twistpoint PrsonaBase::EL_GAMAL_GENERATOR = Twistpoint();
  12. Scalar PrsonaBase::SCALAR_N = Scalar();
  13. Scalar PrsonaBase::DEFAULT_TALLY = Scalar();
  14. Scalar PrsonaBase::DEFAULT_VOTE = Scalar();
  15. bool PrsonaBase::SERVER_IS_MALICIOUS = false;
  16. bool PrsonaBase::CLIENT_IS_MALICIOUS = false;
  17. size_t PrsonaBase::LAMBDA = 0;
  18. // Quick and dirty function to calculate ceil(log base 2) with mpz_class
  19. mpz_class log2(mpz_class x)
  20. {
  21. mpz_class retval = 0;
  22. while (x > 0)
  23. {
  24. retval++;
  25. x = x >> 1;
  26. }
  27. return retval;
  28. }
  29. mpz_class bit(mpz_class x)
  30. {
  31. return x > 0 ? 1 : 0;
  32. }
  33. /********************
  34. * PUBLIC FUNCTIONS *
  35. ********************/
  36. /*
  37. * SETUP FUNCTIONS
  38. */
  39. // Must be called once before any usage of this class
  40. void PrsonaBase::init()
  41. {
  42. EL_GAMAL_GENERATOR = Twistpoint(bn_twistgen);
  43. SCALAR_N = Scalar(bn_n);
  44. DEFAULT_TALLY = Scalar(1);
  45. DEFAULT_VOTE = Scalar(1);
  46. }
  47. // Call this (once) if using malicious-security servers
  48. void PrsonaBase::set_server_malicious()
  49. {
  50. SERVER_IS_MALICIOUS = true;
  51. }
  52. // Call this (once) if using malicious-security clients
  53. void PrsonaBase::set_client_malicious()
  54. {
  55. CLIENT_IS_MALICIOUS = true;
  56. }
  57. // Call this (once) if using proof batching
  58. void PrsonaBase::set_lambda(size_t lambda)
  59. {
  60. LAMBDA = lambda;
  61. }
  62. /*
  63. * CONST GETTERS
  64. */
  65. size_t PrsonaBase::get_max_allowed_vote()
  66. {
  67. return MAX_ALLOWED_VOTE;
  68. }
  69. Twistpoint PrsonaBase::get_blinding_generator() const
  70. {
  71. return elGamalBlindGenerator;
  72. }
  73. Twistpoint PrsonaBase::get_blinding_generator(
  74. std::vector<Proof>& pi) const
  75. {
  76. pi = elGamalBlindGeneratorProof;
  77. return elGamalBlindGenerator;
  78. }
  79. /***********************
  80. * PROTECTED FUNCTIONS *
  81. ***********************/
  82. /*
  83. * PRIVATE ELEMENT SETTER
  84. */
  85. bool PrsonaBase::set_EG_blind_generator(
  86. const std::vector<Proof>& pi,
  87. const Twistpoint& currGenerator,
  88. size_t numServers)
  89. {
  90. if (!verify_generator_proof(pi, currGenerator, numServers))
  91. {
  92. std::cerr << "Could not set EG blind generator correctly." << std::endl;
  93. return false;
  94. }
  95. elGamalBlindGeneratorProof = pi;
  96. elGamalBlindGenerator = currGenerator;
  97. return true;
  98. }
  99. /*
  100. * BINARY SEARCH
  101. */
  102. /* Completely normal binary search
  103. * There might be a standard function for this in <algorithms>?
  104. * But it returns an iterator, not a size_t, so less useful. */
  105. size_t PrsonaBase::binary_search(
  106. const std::vector<Twistpoint> list,
  107. const Twistpoint& index) const
  108. {
  109. size_t lo, hi;
  110. lo = 0;
  111. hi = list.size() - 1;
  112. while (lo < hi)
  113. {
  114. size_t mid = (lo + hi) / 2;
  115. if (list[mid] < index)
  116. lo = mid + 1;
  117. else if (index == list[mid])
  118. return mid;
  119. else if (mid == lo)
  120. return lo;
  121. else hi = mid - 1;
  122. }
  123. return lo;
  124. }
  125. /*
  126. * SCHNORR PROOFS
  127. */
  128. Proof PrsonaBase::schnorr_generation(
  129. const Twistpoint& generator,
  130. const Twistpoint& commitment,
  131. const Scalar& log) const
  132. {
  133. Proof retval;
  134. std::stringstream oracleInput;
  135. Scalar u;
  136. u.set_random();
  137. Twistpoint U = generator * u;
  138. oracleInput << generator << commitment << U;
  139. Scalar x = oracle(oracleInput.str());
  140. Scalar z = log * x + u;
  141. retval.challengeParts.push_back(x);
  142. retval.responseParts.push_back(z);
  143. return retval;
  144. }
  145. bool PrsonaBase::schnorr_verification(
  146. const Twistpoint& generator,
  147. const Twistpoint& commitment,
  148. const Scalar& x,
  149. const Scalar& z) const
  150. {
  151. Twistpoint U = generator * z - commitment * x;
  152. std::stringstream oracleInput;
  153. oracleInput << generator << commitment << U;
  154. return x == oracle(oracleInput.str());
  155. }
  156. /*
  157. * OWNERSHIP PROOFS
  158. */
  159. // Prove ownership of the short term public key
  160. Proof PrsonaBase::generate_ownership_proof(
  161. const Twistpoint& generator,
  162. const Twistpoint& commitment,
  163. const Scalar& log) const
  164. {
  165. if (!CLIENT_IS_MALICIOUS)
  166. {
  167. Proof retval;
  168. retval.hbc = "PROOF";
  169. return retval;
  170. }
  171. return schnorr_generation(generator, commitment, log);
  172. }
  173. bool PrsonaBase::verify_ownership_proof(
  174. const Proof& pi,
  175. const Twistpoint& generator,
  176. const Twistpoint& commitment) const
  177. {
  178. if (!CLIENT_IS_MALICIOUS)
  179. return pi.hbc == "PROOF";
  180. Scalar c = pi.challengeParts[0];
  181. Scalar z = pi.responseParts[0];
  182. return schnorr_verification(generator, commitment, c, z);
  183. }
  184. /*
  185. * ITERATED SCHNORR PROOFS
  186. */
  187. Proof PrsonaBase::add_to_generator_proof(
  188. const Twistpoint& currGenerator,
  189. const Scalar& seed) const
  190. {
  191. Proof retval;
  192. if (!SERVER_IS_MALICIOUS)
  193. {
  194. retval.hbc = "PROOF";
  195. return retval;
  196. }
  197. Twistpoint nextGenerator = currGenerator * seed;
  198. retval = schnorr_generation(currGenerator, nextGenerator, seed);
  199. retval.curvepointUniversals.push_back(currGenerator);
  200. return retval;
  201. }
  202. bool PrsonaBase::verify_generator_proof(
  203. const std::vector<Proof>& pi,
  204. const Twistpoint& currGenerator,
  205. size_t numServers) const
  206. {
  207. if (pi.size() != numServers || numServers == 0)
  208. return false;
  209. bool retval = true;
  210. if (!SERVER_IS_MALICIOUS)
  211. {
  212. for (size_t i = 0; i < pi.size(); i++)
  213. retval = retval && pi[i].hbc == "PROOF";
  214. return retval;
  215. }
  216. if (pi[0].curvepointUniversals[0] != EL_GAMAL_GENERATOR)
  217. return false;
  218. for (size_t i = 0; i < pi.size(); i++)
  219. {
  220. Twistpoint generator = pi[i].curvepointUniversals[0];
  221. Twistpoint commitment = (i == pi.size() - 1 ? currGenerator : pi[i + 1].curvepointUniversals[0]);
  222. Scalar c = pi[i].challengeParts[0];
  223. Scalar z = pi[i].responseParts[0];
  224. retval = retval && schnorr_verification(generator, commitment, c, z);
  225. if (!retval)
  226. std::cerr << "Error in index " << i+1 << " of " << pi.size() << std::endl;
  227. }
  228. return retval;
  229. }
  230. /*
  231. * REPUTATION PROOFS
  232. */
  233. // A pretty straightforward range proof (generation)
  234. std::vector<Proof> PrsonaBase::generate_reputation_proof(
  235. const Proof& ownershipProof,
  236. const EGCiphertext& commitment,
  237. const Scalar& currentScore,
  238. const Scalar& threshold,
  239. const Scalar& inverseKey,
  240. size_t numClients) const
  241. {
  242. std::vector<Proof> retval;
  243. // Base case
  244. if (!CLIENT_IS_MALICIOUS)
  245. {
  246. retval.push_back(Proof("PROOF"));
  247. return retval;
  248. }
  249. // Don't even try if the user asks to make an illegitimate proof
  250. if (threshold.toInt() > (numClients * MAX_ALLOWED_VOTE))
  251. return retval;
  252. // We really have two consecutive proofs in a junction.
  253. // The first is to prove that we are the stpk we claim we are
  254. retval.push_back(ownershipProof);
  255. // The value we're actually using in our proof
  256. mpz_class proofVal = (currentScore - threshold).toInt();
  257. // Top of the range in our proof determined by what scores are even possible
  258. mpz_class proofBits = log2(numClients * MAX_ALLOWED_VOTE - threshold.toInt());
  259. // Don't risk a situation that would divulge our private key
  260. if (proofBits <= 1)
  261. proofBits = 2;
  262. // This seems weird, but remember our base is A_t^r, not g^t
  263. std::vector<Scalar> masksPerBit;
  264. masksPerBit.push_back(inverseKey);
  265. for (size_t i = 1; i < proofBits; i++)
  266. {
  267. Scalar currMask;
  268. currMask.set_random();
  269. masksPerBit.push_back(currMask);
  270. masksPerBit[0] = masksPerBit[0] - (currMask * Scalar(1 << i));
  271. }
  272. // Taken from Fig. 1 in https://eprint.iacr.org/2014/764.pdf
  273. for (size_t i = 0; i < proofBits; i++)
  274. {
  275. Proof currProof;
  276. Twistpoint g, h, C, C_a, C_b;
  277. g = commitment.mask;
  278. h = elGamalBlindGenerator;
  279. mpz_class currBit = bit(proofVal & (1 << i));
  280. Scalar a, s, t, m, r;
  281. a.set_random();
  282. s.set_random();
  283. t.set_random();
  284. m = Scalar(currBit);
  285. r = masksPerBit[i];
  286. C = g * r + h * m;
  287. currProof.curvepointUniversals.push_back(C);
  288. C_a = g * s + h * a;
  289. C_b = g * t + h * a * m;
  290. std::stringstream oracleInput;
  291. oracleInput << g << h << C << C_a << C_b;
  292. Scalar x = oracle(oracleInput.str());
  293. currProof.challengeParts.push_back(x);
  294. Scalar f, z_a, z_b;
  295. f = m * x + a;
  296. z_a = r * x + s;
  297. z_b = r * (x - f) + t;
  298. currProof.responseParts.push_back(f);
  299. currProof.responseParts.push_back(z_a);
  300. currProof.responseParts.push_back(z_b);
  301. retval.push_back(currProof);
  302. }
  303. return retval;
  304. }
  305. // A pretty straightforward range proof (verification)
  306. bool PrsonaBase::verify_reputation_proof(
  307. const std::vector<Proof>& pi,
  308. const Twistpoint& generator,
  309. const Twistpoint& owner,
  310. const EGCiphertext& commitment,
  311. const Scalar& threshold) const
  312. {
  313. // Reject outright if there's no proof to check
  314. if (pi.empty())
  315. {
  316. std::cerr << "Proof was empty, aborting." << std::endl;
  317. return false;
  318. }
  319. // If the range is so big that it wraps around mod n,
  320. // there's a chance the user actually made a proof for a very low reputation
  321. if (pi.size() > 256)
  322. {
  323. std::cerr << "Proof was too big, prover could have cheated." << std::endl;
  324. return false;
  325. }
  326. if (!CLIENT_IS_MALICIOUS)
  327. return pi[0].hbc == "PROOF";
  328. Scalar ownerChallenge, ownerResponse;
  329. ownerChallenge = pi[0].challengeParts[0];
  330. ownerResponse = pi[0].responseParts[0];
  331. // User should be able to prove they are who they say they are
  332. if (!schnorr_verification(generator, owner, ownerChallenge, ownerResponse))
  333. {
  334. std::cerr << "Schnorr proof failed, aborting." << std::endl;
  335. return false;
  336. }
  337. // X is the thing we're going to be checking in on throughout
  338. // to try to get our score commitment back in the end.
  339. Twistpoint X;
  340. for (size_t i = 1; i < pi.size(); i++)
  341. {
  342. Twistpoint C, g, h;
  343. C = pi[i].curvepointUniversals[0];
  344. g = commitment.mask;
  345. h = elGamalBlindGenerator;
  346. X = X + C * Scalar(1 << (i - 1));
  347. Scalar x, f, z_a, z_b;
  348. x = pi[i].challengeParts[0];
  349. f = pi[i].responseParts[0];
  350. z_a = pi[i].responseParts[1];
  351. z_b = pi[i].responseParts[2];
  352. // Taken from Fig. 1 in https://eprint.iacr.org/2014/764.pdf
  353. Twistpoint C_a, C_b;
  354. C_a = g * z_a + h * f - C * x;
  355. C_b = g * z_b - C * (x - f);
  356. std::stringstream oracleInput;
  357. oracleInput << g << h << C << C_a << C_b;
  358. if (oracle(oracleInput.str()) != x)
  359. {
  360. std::cerr << "0 or 1 proof failed at index " << i << " of " << pi.size() - 1 << ", aborting." << std::endl;
  361. return false;
  362. }
  363. }
  364. Twistpoint scoreCommitment = commitment.encryptedMessage + elGamalBlindGenerator * -threshold;
  365. return X == scoreCommitment;
  366. }
  367. /*
  368. * VALID VOTE PROOFS
  369. */
  370. std::vector<Proof> PrsonaBase::generate_vote_proof(
  371. const Proof& ownershipProof,
  372. const TwistBipoint& g,
  373. const TwistBipoint& h,
  374. const std::vector<bool>& replaces,
  375. const std::vector<TwistBipoint>& oldEncryptedVotes,
  376. const std::vector<TwistBipoint>& newEncryptedVotes,
  377. const std::vector<Scalar>& seeds,
  378. const std::vector<Scalar>& votes) const
  379. {
  380. std::vector<Proof> retval;
  381. // Base case
  382. if (!CLIENT_IS_MALICIOUS)
  383. {
  384. retval.push_back(Proof("PROOF"));
  385. return retval;
  386. }
  387. // The first need is to prove that we are the stpk we claim we are
  388. retval.push_back(ownershipProof);
  389. // Then, we iterate over all votes for the proofs that they are correct
  390. for (size_t i = 0; i < replaces.size(); i++)
  391. {
  392. std::stringstream oracleInput;
  393. oracleInput << g << h << oldEncryptedVotes[i] << newEncryptedVotes[i];
  394. Scalar m = votes[i];
  395. Scalar r = seeds[i];
  396. /* This proof structure is documented in my notes.
  397. * It's inspired by the proof in Fig. 1 at
  398. * https://eprint.iacr.org/2014/764.pdf, but adapted so that you prove
  399. * m(m-1)(m-2) = 0 instead of m(m-1) = 0.
  400. *
  401. * The rerandomization part is just a slight variation on an
  402. * ordinary Schnorr proof, so that part's less scary. */
  403. if (replaces[i]) // CASE: Make new vote
  404. {
  405. Proof currProof;
  406. Scalar x_r, z_r, a, s, t_1, t_2;
  407. x_r.set_random();
  408. z_r.set_random();
  409. a.set_random();
  410. s.set_random();
  411. t_1.set_random();
  412. t_2.set_random();
  413. TwistBipoint U = h * z_r +
  414. oldEncryptedVotes[i] * x_r -
  415. newEncryptedVotes[i] * x_r;
  416. TwistBipoint C_a = g * a + h * s;
  417. Scalar power = ((a + a) * m * m - (a + a + a) * m);
  418. TwistBipoint C_b = g * power + h * t_1;
  419. currProof.curveBipointUniversals.push_back(C_b);
  420. TwistBipoint C_c = g * (a * a * m) + h * t_2;
  421. oracleInput << U << C_a << C_b << C_c;
  422. Scalar x = oracle(oracleInput.str());
  423. Scalar x_n = x - x_r;
  424. currProof.challengeParts.push_back(x_r);
  425. currProof.challengeParts.push_back(x_n);
  426. Scalar f = m * x_n + a;
  427. Scalar z_na = r * x_n + s;
  428. Scalar z_nb = r * (f - x_n) * (x_n + x_n - f) + t_1 * x_n + t_2;
  429. currProof.responseParts.push_back(z_r);
  430. currProof.responseParts.push_back(f);
  431. currProof.responseParts.push_back(z_na);
  432. currProof.responseParts.push_back(z_nb);
  433. retval.push_back(currProof);
  434. }
  435. else // CASE: Rerandomize existing vote
  436. {
  437. Proof currProof;
  438. Scalar u, commitmentLambda_1, commitmentLambda_2, x_n, z_na, z_nb, f;
  439. u.set_random();
  440. commitmentLambda_1.set_random();
  441. commitmentLambda_2.set_random();
  442. x_n.set_random();
  443. z_na.set_random();
  444. z_nb.set_random();
  445. f.set_random();
  446. TwistBipoint U = h * u;
  447. TwistBipoint C_a = g * f +
  448. h * z_na -
  449. newEncryptedVotes[i] * x_n;
  450. TwistBipoint C_b = g * commitmentLambda_1 + h * commitmentLambda_2;
  451. currProof.curveBipointUniversals.push_back(C_b);
  452. TwistBipoint C_c = h * z_nb -
  453. newEncryptedVotes[i] * ((f - x_n) * (x_n + x_n - f)) -
  454. C_b * x_n;
  455. oracleInput << U << C_a << C_b << C_c;
  456. Scalar x = oracle(oracleInput.str());
  457. Scalar x_r = x - x_n;
  458. currProof.challengeParts.push_back(x_r);
  459. currProof.challengeParts.push_back(x_n);
  460. Scalar z_r = r * x_r + u;
  461. currProof.responseParts.push_back(z_r);
  462. currProof.responseParts.push_back(f);
  463. currProof.responseParts.push_back(z_na);
  464. currProof.responseParts.push_back(z_nb);
  465. retval.push_back(currProof);
  466. }
  467. }
  468. return retval;
  469. }
  470. bool PrsonaBase::verify_vote_proof(
  471. const TwistBipoint& g,
  472. const TwistBipoint& h,
  473. const std::vector<Proof>& pi,
  474. const std::vector<TwistBipoint>& oldEncryptedVotes,
  475. const std::vector<TwistBipoint>& newEncryptedVotes,
  476. const Twistpoint& freshGenerator,
  477. const Twistpoint& owner) const
  478. {
  479. // Reject outright if there's no proof to check
  480. if (pi.empty())
  481. {
  482. std::cerr << "Proof was empty, aborting." << std::endl;
  483. return false;
  484. }
  485. // Base case
  486. if (!CLIENT_IS_MALICIOUS)
  487. return pi[0].hbc == "PROOF";
  488. // User should be able to prove they are who they say they are
  489. if (!verify_ownership_proof(pi[0], freshGenerator, owner))
  490. {
  491. std::cerr << "Schnorr proof failed, aborting." << std::endl;
  492. return false;
  493. }
  494. /* This proof structure is documented in my notes.
  495. * It's inspired by the proof in Fig. 1 at
  496. * https://eprint.iacr.org/2014/764.pdf, but adapted so that you prove
  497. * m(m-1)(m-2) = 0 instead of m(m-1) = 0.
  498. *
  499. * The rerandomization part is just a slight variation on an
  500. * ordinary Schnorr proof, so that part's less scary. */
  501. for (size_t i = 1; i < pi.size(); i++)
  502. {
  503. size_t voteIndex = i - 1;
  504. TwistBipoint C_b;
  505. C_b = pi[i].curveBipointUniversals[0];
  506. Scalar x_r, x_n, z_r, f, z_na, z_nb;
  507. x_r = pi[i].challengeParts[0];
  508. x_n = pi[i].challengeParts[1];
  509. z_r = pi[i].responseParts[0];
  510. f = pi[i].responseParts[1];
  511. z_na = pi[i].responseParts[2];
  512. z_nb = pi[i].responseParts[3];
  513. TwistBipoint U, C_a, C_c;
  514. U = h * z_r +
  515. oldEncryptedVotes[voteIndex] * x_r -
  516. newEncryptedVotes[voteIndex] * x_r;
  517. C_a = g * f + h * z_na - newEncryptedVotes[voteIndex] * x_n;
  518. C_c = h * z_nb -
  519. newEncryptedVotes[voteIndex] * ((f - x_n) * (x_n + x_n - f)) -
  520. C_b * x_n;
  521. std::stringstream oracleInput;
  522. oracleInput << g << h << oldEncryptedVotes[voteIndex] << newEncryptedVotes[voteIndex] << U << C_a << C_b << C_c;
  523. if (oracle(oracleInput.str()) != x_r + x_n)
  524. return false;
  525. }
  526. return true;
  527. }
  528. /*
  529. * NEW USER PROOFS
  530. */
  531. std::vector<Proof> PrsonaBase::generate_proof_of_added_user(
  532. const Scalar& twistBipointSeed,
  533. const Scalar& EGCiphertextSeed,
  534. const std::vector<Scalar>& curveBipointSelfSeeds,
  535. const std::vector<Scalar>& curveBipointOtherSeeds) const
  536. {
  537. std::vector<Proof> retval;
  538. if (!SERVER_IS_MALICIOUS)
  539. {
  540. retval.push_back(Proof("PROOF"));
  541. return retval;
  542. }
  543. Proof currProof;
  544. currProof.responseParts.push_back(twistBipointSeed);
  545. retval.push_back(currProof);
  546. currProof.responseParts.clear();
  547. currProof.responseParts.push_back(EGCiphertextSeed);
  548. retval.push_back(currProof);
  549. currProof.responseParts.clear();
  550. for (size_t i = 0; i < curveBipointSelfSeeds.size(); i++)
  551. currProof.responseParts.push_back(curveBipointSelfSeeds[i]);
  552. retval.push_back(currProof);
  553. currProof.responseParts.clear();
  554. for (size_t i = 0; i < curveBipointOtherSeeds.size(); i++)
  555. currProof.responseParts.push_back(curveBipointOtherSeeds[i]);
  556. retval.push_back(currProof);
  557. return retval;
  558. }
  559. bool PrsonaBase::verify_proof_of_added_user(
  560. const std::vector<Proof>& pi,
  561. const Twistpoint& currentFreshGenerator,
  562. const Twistpoint& shortTermPublicKey,
  563. const TwistBipoint& curveG,
  564. const TwistBipoint& curveH,
  565. const CurveBipoint& twistG,
  566. const CurveBipoint& twistH,
  567. size_t selfIndex,
  568. const EGCiphertext& userEncryptedScore,
  569. const CurveBipoint& serverEncryptedScore,
  570. const std::vector<std::vector<TwistBipoint>> encryptedVoteMatrix) const
  571. {
  572. if (pi.empty())
  573. {
  574. std::cerr << "Proof empty." << std::endl;
  575. return false;
  576. }
  577. if (!SERVER_IS_MALICIOUS)
  578. return pi[0].hbc == "PROOF";
  579. Scalar currSeed = pi[0].responseParts[0];
  580. if (serverEncryptedScore !=
  581. twistG * DEFAULT_TALLY + twistH * currSeed)
  582. {
  583. std::cerr << "Issue in server encrypted score." << std::endl;
  584. return false;
  585. }
  586. currSeed = pi[1].responseParts[0];
  587. if (userEncryptedScore.mask != shortTermPublicKey * currSeed)
  588. {
  589. std::cerr << "Issue in user encrypted score: mask." << std::endl;
  590. return false;
  591. }
  592. if (userEncryptedScore.encryptedMessage !=
  593. currentFreshGenerator * currSeed + elGamalBlindGenerator * DEFAULT_TALLY)
  594. {
  595. std::cerr << "Issue in user encrypted score: value." << std::endl;
  596. return false;
  597. }
  598. for (size_t i = 0; i < pi[2].responseParts.size(); i++)
  599. {
  600. TwistBipoint currVote = encryptedVoteMatrix[selfIndex][i];
  601. currSeed = pi[2].responseParts[i];
  602. if (i == selfIndex)
  603. {
  604. if (currVote !=
  605. curveG * Scalar(MAX_ALLOWED_VOTE) + curveH * currSeed)
  606. {
  607. std::cerr << "Issue in self vote." << std::endl;
  608. return false;
  609. }
  610. }
  611. else
  612. {
  613. if (currVote !=
  614. curveG * DEFAULT_VOTE + curveH * currSeed)
  615. {
  616. std::cerr << "Issue in vote by verifier for user " << i + 1 << " of " << pi[2].responseParts.size() << "." << std::endl;
  617. return false;
  618. }
  619. }
  620. }
  621. for (size_t i = 0; i < pi[3].responseParts.size(); i++)
  622. {
  623. TwistBipoint currVote = encryptedVoteMatrix[i][selfIndex];
  624. currSeed = pi[3].responseParts[i];
  625. if (i != selfIndex)
  626. {
  627. if (currVote !=
  628. curveG * DEFAULT_VOTE + curveH * currSeed)
  629. {
  630. std::cerr << "Issue in vote for verifier by user " << i + 1 << " of " << pi[3].responseParts.size() << "." << std::endl;
  631. return false;
  632. }
  633. }
  634. }
  635. return true;
  636. }
  637. /*
  638. * EPOCH PROOFS
  639. */
  640. std::vector<Proof> PrsonaBase::generate_valid_permutation_proof(
  641. const std::vector<std::vector<Scalar>>& permutations,
  642. const std::vector<std::vector<Scalar>>& seeds,
  643. const std::vector<std::vector<Twistpoint>>& commits) const
  644. {
  645. std::vector<Proof> retval;
  646. if (!SERVER_IS_MALICIOUS)
  647. {
  648. retval.push_back(Proof("PROOF"));
  649. return retval;
  650. }
  651. Twistpoint g, h;
  652. g = EL_GAMAL_GENERATOR;
  653. h = elGamalBlindGenerator;
  654. std::stringstream oracleInput;
  655. oracleInput << g << h;
  656. retval.push_back(Proof());
  657. std::vector<std::vector<Scalar>> a, s, t;
  658. for (size_t i = 0; i < commits.size(); i++)
  659. {
  660. std::vector<Scalar> currA, currS, currT;
  661. for (size_t j = 0; j < commits[i].size(); j++)
  662. {
  663. oracleInput << commits[i][j];
  664. currA.push_back(Scalar());
  665. currS.push_back(Scalar());
  666. currT.push_back(Scalar());
  667. retval.push_back(Proof());
  668. }
  669. a.push_back(currA);
  670. s.push_back(currS);
  671. t.push_back(currT);
  672. }
  673. // Taken from Fig. 1 in https://eprint.iacr.org/2014/764.pdf
  674. for (size_t i = 0; i < permutations.size(); i++)
  675. {
  676. for (size_t j = 0; j < permutations[i].size(); j++)
  677. {
  678. Twistpoint C, C_a, C_b;
  679. Scalar p, r;
  680. a[i][j].set_random();
  681. s[i][j].set_random();
  682. t[i][j].set_random();
  683. p = permutations[i][j];
  684. r = seeds[i][j];
  685. C = commits[i][j];
  686. C_a = g * a[i][j] + h * s[i][j];
  687. C_b = g * a[i][j] * p + h * t[i][j];
  688. oracleInput << C << C_a << C_b;
  689. }
  690. }
  691. Scalar x = oracle(oracleInput.str());
  692. retval[0].challengeParts.push_back(x);
  693. for (size_t i = 0; i < permutations.size(); i++)
  694. {
  695. for (size_t j = 0; j < permutations[i].size(); j++)
  696. {
  697. size_t piIndex = i * permutations.size() + j + 1;
  698. Scalar f, z_a, z_b, p, r;
  699. p = permutations[i][j];
  700. r = seeds[i][j];
  701. f = p * x + a[i][j];
  702. z_a = r * x + s[i][j];
  703. z_b = r * (x - f) + t[i][j];
  704. retval[piIndex].responseParts.push_back(f);
  705. retval[piIndex].responseParts.push_back(z_a);
  706. retval[piIndex].responseParts.push_back(z_b);
  707. }
  708. }
  709. return retval;
  710. }
  711. bool PrsonaBase::verify_valid_permutation_proof(
  712. const std::vector<Proof>& pi,
  713. const std::vector<std::vector<Twistpoint>>& commits) const
  714. {
  715. // Reject outright if there's no proof to check
  716. if (pi.empty())
  717. {
  718. std::cerr << "Proof was empty, aborting." << std::endl;
  719. return false;
  720. }
  721. if (!SERVER_IS_MALICIOUS)
  722. return pi[0].hbc == "PROOF";
  723. Twistpoint g, h;
  724. g = EL_GAMAL_GENERATOR;
  725. h = elGamalBlindGenerator;
  726. std::stringstream oracleInput;
  727. oracleInput << g << h;
  728. for (size_t i = 0; i < commits.size(); i++)
  729. for (size_t j = 0; j < commits[i].size(); j++)
  730. oracleInput << commits[i][j];
  731. Scalar x = pi[0].challengeParts[0];
  732. for (size_t i = 0; i < commits.size(); i++)
  733. {
  734. for (size_t j = 0; j < commits[i].size(); j++)
  735. {
  736. size_t piIndex = i * commits.size() + j + 1;
  737. Twistpoint C, C_a, C_b;
  738. C = commits[i][j];
  739. Scalar f, z_a, z_b;
  740. f = pi[piIndex].responseParts[0];
  741. z_a = pi[piIndex].responseParts[1];
  742. z_b = pi[piIndex].responseParts[2];
  743. // Taken from Fig. 1 in https://eprint.iacr.org/2014/764.pdf
  744. C_a = g * f + h * z_a - C * x;
  745. C_b = h * z_b - C * (x - f);
  746. oracleInput << C << C_a << C_b;
  747. }
  748. }
  749. if (oracle(oracleInput.str()) != x)
  750. {
  751. std::cerr << "0 or 1 proof failed, aborting." << std::endl;
  752. return false;
  753. }
  754. for (size_t i = 0; i < commits.size(); i++)
  755. {
  756. Twistpoint sum = commits[i][0];
  757. for (size_t j = 1; j < commits[i].size(); j++)
  758. sum = sum + commits[i][j];
  759. if (sum != g)
  760. {
  761. std::cerr << "Commits did not sum to g, aborting." << std::endl;
  762. return false;
  763. }
  764. }
  765. return true;
  766. }
  767. template <typename T>
  768. std::vector<Proof> PrsonaBase::generate_proof_of_reordering(
  769. const std::vector<std::vector<Scalar>>& permutations,
  770. const std::vector<std::vector<Scalar>>& permutationSeeds,
  771. const std::vector<std::vector<Scalar>>& productSeeds,
  772. const std::vector<T>& oldValues,
  773. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  774. const std::vector<std::vector<T>>& productCommits,
  775. const T& otherG,
  776. const T& otherH) const
  777. {
  778. if (LAMBDA > 0)
  779. return generate_batched_proof_of_reordering<T>(permutations, permutationSeeds, productSeeds, oldValues, permutationCommits, productCommits, otherG, otherH);
  780. else
  781. return generate_unbatched_proof_of_reordering<T>(permutations, permutationSeeds, productSeeds, oldValues, permutationCommits, productCommits, otherG, otherH);
  782. }
  783. template <typename T>
  784. bool PrsonaBase::verify_proof_of_reordering(
  785. const std::vector<Proof>& pi,
  786. const std::vector<T>& oldValues,
  787. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  788. const std::vector<std::vector<T>>& productCommits,
  789. const T& otherG,
  790. const T& otherH) const
  791. {
  792. if (LAMBDA > 0)
  793. return verify_batched_proof_of_reordering<T>(pi, oldValues, permutationCommits, productCommits, otherG, otherH);
  794. else
  795. return verify_unbatched_proof_of_reordering<T>(pi, oldValues, permutationCommits, productCommits, otherG, otherH);
  796. }
  797. template <typename T>
  798. std::vector<Proof> PrsonaBase::generate_unbatched_proof_of_reordering(
  799. const std::vector<std::vector<Scalar>>& permutations,
  800. const std::vector<std::vector<Scalar>>& permutationSeeds,
  801. const std::vector<std::vector<Scalar>>& productSeeds,
  802. const std::vector<T>& oldValues,
  803. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  804. const std::vector<std::vector<T>>& productCommits,
  805. const T& otherG,
  806. const T& otherH) const
  807. {
  808. std::vector<Proof> retval;
  809. if (!SERVER_IS_MALICIOUS)
  810. {
  811. retval.push_back(Proof("PROOF"));
  812. return retval;
  813. }
  814. Proof first;
  815. retval.push_back(first);
  816. Twistpoint g = EL_GAMAL_GENERATOR;
  817. Twistpoint h = elGamalBlindGenerator;
  818. std::stringstream oracleInput;
  819. oracleInput << g << h << otherG << otherH;
  820. for (size_t i = 0; i < oldValues.size(); i++)
  821. oracleInput << oldValues[i];
  822. for (size_t i = 0; i < permutationCommits.size(); i++)
  823. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  824. oracleInput << permutationCommits[i][j];
  825. for (size_t i = 0; i < productCommits.size(); i++)
  826. for (size_t j = 0; j < productCommits[i].size(); j++)
  827. oracleInput << productCommits[i][j];
  828. std::vector<std::vector<Scalar>> a;
  829. std::vector<std::vector<Scalar>> t1;
  830. std::vector<std::vector<Scalar>> t2;
  831. for (size_t i = 0; i < permutationCommits.size(); i++)
  832. {
  833. std::vector<Scalar> curraRow;
  834. std::vector<Scalar> currt1Row;
  835. std::vector<Scalar> currt2Row;
  836. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  837. {
  838. Proof currProof;
  839. Scalar curra;
  840. Scalar currt1;
  841. Scalar currt2;
  842. Twistpoint U1;
  843. T U2;
  844. curra.set_random();
  845. currt1.set_random();
  846. currt2.set_random();
  847. U1 = g * curra + h * currt1;
  848. U2 = oldValues[j] * curra + otherH * currt2;
  849. oracleInput << U1 << U2;
  850. curraRow.push_back(curra);
  851. currt1Row.push_back(currt1);
  852. currt2Row.push_back(currt2);
  853. retval.push_back(currProof);
  854. }
  855. a.push_back(curraRow);
  856. t1.push_back(currt1Row);
  857. t2.push_back(currt2Row);
  858. }
  859. Scalar x = oracle(oracleInput.str());
  860. retval[0].challengeParts.push_back(x);
  861. for (size_t i = 0; i < permutationCommits.size(); i++)
  862. {
  863. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  864. {
  865. size_t piIndex = i * permutationCommits.size() + j + 1;
  866. Scalar f = permutations[j][i] * x + a[i][j];
  867. Scalar z1 = permutationSeeds[j][i] * x + t1[i][j];
  868. Scalar z2 = productSeeds[i][j] * x + t2[i][j];
  869. retval[piIndex].responseParts.push_back(f);
  870. retval[piIndex].responseParts.push_back(z1);
  871. retval[piIndex].responseParts.push_back(z2);
  872. }
  873. }
  874. return retval;
  875. }
  876. template <typename T>
  877. bool PrsonaBase::verify_unbatched_proof_of_reordering(
  878. const std::vector<Proof>& pi,
  879. const std::vector<T>& oldValues,
  880. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  881. const std::vector<std::vector<T>>& productCommits,
  882. const T& otherG,
  883. const T& otherH) const
  884. {
  885. if (pi.empty())
  886. return false;
  887. if (!SERVER_IS_MALICIOUS)
  888. return pi[0].hbc == "PROOF";
  889. Twistpoint g = EL_GAMAL_GENERATOR;
  890. Twistpoint h = elGamalBlindGenerator;
  891. std::stringstream oracleInput;
  892. oracleInput << g << h << otherG << otherH;
  893. for (size_t i = 0; i < oldValues.size(); i++)
  894. oracleInput << oldValues[i];
  895. for (size_t i = 0; i < permutationCommits.size(); i++)
  896. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  897. oracleInput << permutationCommits[i][j];
  898. for (size_t i = 0; i < productCommits.size(); i++)
  899. for (size_t j = 0; j < productCommits[i].size(); j++)
  900. oracleInput << productCommits[i][j];
  901. Scalar x = pi[0].challengeParts[0];
  902. for (size_t i = 0; i < permutationCommits.size(); i++)
  903. {
  904. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  905. {
  906. size_t piIndex = i * permutationCommits.size() + j + 1;
  907. Twistpoint U1;
  908. T U2;
  909. Scalar f = pi[piIndex].responseParts[0];
  910. Scalar z1 = pi[piIndex].responseParts[1];
  911. Scalar z2 = pi[piIndex].responseParts[2];
  912. U1 = g * f +
  913. h * z1 -
  914. permutationCommits[j][i] * x;
  915. U2 = oldValues[j] * f +
  916. otherH * z2 -
  917. productCommits[i][j] * x;
  918. oracleInput << U1 << U2;
  919. }
  920. }
  921. if (x != oracle(oracleInput.str()))
  922. {
  923. std::cerr << "Reordered things not generated by permutation matrix." << std::endl;
  924. return false;
  925. }
  926. return true;
  927. }
  928. template <typename T>
  929. std::vector<Proof> PrsonaBase::generate_batched_proof_of_reordering(
  930. const std::vector<std::vector<Scalar>>& permutations,
  931. const std::vector<std::vector<Scalar>>& permutationSeeds,
  932. const std::vector<std::vector<Scalar>>& productSeeds,
  933. const std::vector<T>& oldValues,
  934. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  935. const std::vector<std::vector<T>>& productCommits,
  936. const T& otherG,
  937. const T& otherH) const
  938. {
  939. std::vector<Proof> retval;
  940. if (!SERVER_IS_MALICIOUS)
  941. {
  942. retval.push_back(Proof("PROOF"));
  943. return retval;
  944. }
  945. Proof first;
  946. retval.push_back(first);
  947. Twistpoint g = EL_GAMAL_GENERATOR;
  948. Twistpoint h = elGamalBlindGenerator;
  949. std::stringstream oracleInput;
  950. oracleInput << g << h << otherG << otherH;
  951. for (size_t i = 0; i < oldValues.size(); i++)
  952. oracleInput << oldValues[i];
  953. for (size_t i = 0; i < permutationCommits.size(); i++)
  954. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  955. oracleInput << permutationCommits[i][j];
  956. for (size_t i = 0; i < productCommits.size(); i++)
  957. for (size_t j = 0; j < productCommits[i].size(); j++)
  958. oracleInput << productCommits[i][j];
  959. std::vector<Scalar> a;
  960. std::vector<Scalar> t1;
  961. std::vector<Scalar> t2;
  962. for (size_t i = 0; i < permutationCommits.size(); i++)
  963. {
  964. Proof currProof;
  965. Scalar curra;
  966. Scalar currt1;
  967. Scalar currt2;
  968. Twistpoint U1;
  969. T U2;
  970. curra.set_random();
  971. currt1.set_random();
  972. currt2.set_random();
  973. U1 = g * curra + h * currt1;
  974. U2 = oldValues[i] * curra + otherH * currt2;
  975. oracleInput << U1 << U2;
  976. a.push_back(curra);
  977. t1.push_back(currt1);
  978. t2.push_back(currt2);
  979. retval.push_back(currProof);
  980. }
  981. Scalar x = oracle(oracleInput.str());
  982. retval[0].challengeParts.push_back(x);
  983. for (size_t i = 0; i < permutationCommits.size(); i++)
  984. {
  985. size_t piIndex = i + 1;
  986. Scalar f = a[i];
  987. Scalar z1 = t1[i];
  988. Scalar z2 = t2[i];
  989. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  990. {
  991. std::stringstream currOracle;
  992. currOracle << permutationCommits[i][j] << productCommits[j][i];
  993. Scalar currx = oracle(currOracle.str(), LAMBDA);
  994. f = f + permutations[i][j] * currx;
  995. z1 = z1 + permutationSeeds[i][j] * currx;
  996. z2 = z2 + productSeeds[j][i] * currx;
  997. }
  998. retval[piIndex].responseParts.push_back(f);
  999. retval[piIndex].responseParts.push_back(z1);
  1000. retval[piIndex].responseParts.push_back(z2);
  1001. }
  1002. return retval;
  1003. }
  1004. template <typename T>
  1005. bool PrsonaBase::verify_batched_proof_of_reordering(
  1006. const std::vector<Proof>& pi,
  1007. const std::vector<T>& oldValues,
  1008. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1009. const std::vector<std::vector<T>>& productCommits,
  1010. const T& otherG,
  1011. const T& otherH) const
  1012. {
  1013. if (pi.empty())
  1014. return false;
  1015. if (!SERVER_IS_MALICIOUS)
  1016. return pi[0].hbc == "PROOF";
  1017. Twistpoint g = EL_GAMAL_GENERATOR;
  1018. Twistpoint h = elGamalBlindGenerator;
  1019. std::stringstream oracleInput;
  1020. oracleInput << g << h << otherG << otherH;
  1021. for (size_t i = 0; i < oldValues.size(); i++)
  1022. oracleInput << oldValues[i];
  1023. for (size_t i = 0; i < permutationCommits.size(); i++)
  1024. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1025. oracleInput << permutationCommits[i][j];
  1026. for (size_t i = 0; i < productCommits.size(); i++)
  1027. for (size_t j = 0; j < productCommits[i].size(); j++)
  1028. oracleInput << productCommits[i][j];
  1029. Scalar x = pi[0].challengeParts[0];
  1030. for (size_t i = 0; i < permutationCommits.size(); i++)
  1031. {
  1032. size_t piIndex = i + 1;
  1033. Scalar f = pi[piIndex].responseParts[0];
  1034. Scalar z1 = pi[piIndex].responseParts[1];
  1035. Scalar z2 = pi[piIndex].responseParts[2];
  1036. Twistpoint U1 = g * f + h * z1;
  1037. T U2 = oldValues[i] * f + otherH * z2;
  1038. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1039. {
  1040. std::stringstream currOracle;
  1041. currOracle << permutationCommits[i][j] << productCommits[j][i];
  1042. Scalar currx = oracle(currOracle.str(), LAMBDA);
  1043. U1 = U1 - permutationCommits[i][j] * currx;
  1044. U2 = U2 - productCommits[j][i] * currx;
  1045. }
  1046. oracleInput << U1 << U2;
  1047. }
  1048. if (x != oracle(oracleInput.str()))
  1049. {
  1050. std::cerr << "Reordered things not generated by permutation matrix." << std::endl;
  1051. return false;
  1052. }
  1053. return true;
  1054. }
  1055. std::vector<Proof> PrsonaBase::generate_proof_of_reordering_plus_power(
  1056. const std::vector<std::vector<Scalar>>& permutations,
  1057. const Scalar& power,
  1058. const std::vector<std::vector<Scalar>>& permutationSeeds,
  1059. const std::vector<std::vector<Scalar>>& productSeeds,
  1060. const std::vector<Twistpoint>& oldValues,
  1061. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1062. const std::vector<std::vector<Twistpoint>>& productCommits,
  1063. const std::vector<std::vector<Twistpoint>>& seedCommits) const
  1064. {
  1065. if (LAMBDA > 0)
  1066. return generate_batched_proof_of_reordering_plus_power(permutations, power, permutationSeeds, productSeeds, oldValues, permutationCommits, productCommits, seedCommits);
  1067. else
  1068. return generate_unbatched_proof_of_reordering_plus_power(permutations, power, permutationSeeds, productSeeds, oldValues, permutationCommits, productCommits, seedCommits);
  1069. }
  1070. bool PrsonaBase::verify_proof_of_reordering_plus_power(
  1071. const std::vector<Proof>& pi,
  1072. const std::vector<Twistpoint>& oldValues,
  1073. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1074. const std::vector<std::vector<Twistpoint>>& productCommits,
  1075. const std::vector<std::vector<Twistpoint>>& seedCommits) const
  1076. {
  1077. if (LAMBDA > 0)
  1078. return verify_batched_proof_of_reordering_plus_power(pi, oldValues, permutationCommits, productCommits, seedCommits);
  1079. else
  1080. return verify_unbatched_proof_of_reordering_plus_power(pi, oldValues, permutationCommits, productCommits, seedCommits);
  1081. }
  1082. std::vector<Proof> PrsonaBase::generate_unbatched_proof_of_reordering_plus_power(
  1083. const std::vector<std::vector<Scalar>>& permutations,
  1084. const Scalar& power,
  1085. const std::vector<std::vector<Scalar>>& permutationSeeds,
  1086. const std::vector<std::vector<Scalar>>& productSeeds,
  1087. const std::vector<Twistpoint>& oldValues,
  1088. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1089. const std::vector<std::vector<Twistpoint>>& productCommits,
  1090. const std::vector<std::vector<Twistpoint>>& seedCommits) const
  1091. {
  1092. std::vector<Proof> retval;
  1093. if (!SERVER_IS_MALICIOUS)
  1094. {
  1095. retval.push_back(Proof("PROOF"));
  1096. return retval;
  1097. }
  1098. Proof first;
  1099. retval.push_back(first);
  1100. Twistpoint g = EL_GAMAL_GENERATOR;
  1101. Twistpoint h = elGamalBlindGenerator;
  1102. std::stringstream oracleInput;
  1103. oracleInput << g << h;
  1104. for (size_t i = 0; i < oldValues.size(); i++)
  1105. oracleInput << oldValues[i];
  1106. for (size_t i = 0; i < permutationCommits.size(); i++)
  1107. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1108. oracleInput << permutationCommits[i][j];
  1109. for (size_t i = 0; i < productCommits.size(); i++)
  1110. for (size_t j = 0; j < productCommits[i].size(); j++)
  1111. oracleInput << productCommits[i][j];
  1112. for (size_t i = 0; i < seedCommits.size(); i++)
  1113. for (size_t j = 0; j < seedCommits[i].size(); j++)
  1114. oracleInput << seedCommits[i][j];
  1115. Scalar b1;
  1116. b1.set_random();
  1117. std::vector<std::vector<Scalar>> b2;
  1118. std::vector<std::vector<Scalar>> t1;
  1119. std::vector<std::vector<Scalar>> t2;
  1120. for (size_t i = 0; i < permutationCommits.size(); i++)
  1121. {
  1122. std::vector<Scalar> currb2Row;
  1123. std::vector<Scalar> currt1Row;
  1124. std::vector<Scalar> currt2Row;
  1125. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1126. {
  1127. Proof currProof;
  1128. Scalar currb2;
  1129. Scalar currt1;
  1130. Scalar currt2;
  1131. Twistpoint U1, U2, U3, U4;
  1132. currb2.set_random();
  1133. currt1.set_random();
  1134. currt2.set_random();
  1135. U1 = g * currb2 + h * currt1;
  1136. U2 = oldValues[j] *
  1137. (b1 * permutations[j][i] + currb2 * power);
  1138. U3 = oldValues[j] * b1 * currb2 + h * currt2;
  1139. U4 = g * currt2;
  1140. currProof.curvepointUniversals.push_back(U2);
  1141. oracleInput << U1 << U2 << U3 << U4;
  1142. currb2Row.push_back(currb2);
  1143. currt1Row.push_back(currt1);
  1144. currt2Row.push_back(currt2);
  1145. retval.push_back(currProof);
  1146. }
  1147. b2.push_back(currb2Row);
  1148. t1.push_back(currt1Row);
  1149. t2.push_back(currt2Row);
  1150. }
  1151. Scalar x = oracle(oracleInput.str());
  1152. retval[0].challengeParts.push_back(x);
  1153. Scalar f1 = power * x + b1;
  1154. retval[0].responseParts.push_back(f1);
  1155. for (size_t i = 0; i < permutationCommits.size(); i++)
  1156. {
  1157. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1158. {
  1159. size_t piIndex = i * permutationCommits.size() + j + 1;
  1160. Scalar f2 = permutations[j][i] * x + b2[i][j];
  1161. Scalar z1 = permutationSeeds[j][i] * x + t1[i][j];
  1162. Scalar z2 = productSeeds[i][j] * x * x + t2[i][j];
  1163. retval[piIndex].responseParts.push_back(f2);
  1164. retval[piIndex].responseParts.push_back(z1);
  1165. retval[piIndex].responseParts.push_back(z2);
  1166. }
  1167. }
  1168. return retval;
  1169. }
  1170. bool PrsonaBase::verify_unbatched_proof_of_reordering_plus_power(
  1171. const std::vector<Proof>& pi,
  1172. const std::vector<Twistpoint>& oldValues,
  1173. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1174. const std::vector<std::vector<Twistpoint>>& productCommits,
  1175. const std::vector<std::vector<Twistpoint>>& seedCommits) const
  1176. {
  1177. if (pi.empty())
  1178. return false;
  1179. if (!SERVER_IS_MALICIOUS)
  1180. return pi[0].hbc == "PROOF";
  1181. Twistpoint g = EL_GAMAL_GENERATOR;
  1182. Twistpoint h = elGamalBlindGenerator;
  1183. std::stringstream oracleInput;
  1184. oracleInput << g << h;
  1185. for (size_t i = 0; i < oldValues.size(); i++)
  1186. oracleInput << oldValues[i];
  1187. for (size_t i = 0; i < permutationCommits.size(); i++)
  1188. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1189. oracleInput << permutationCommits[i][j];
  1190. for (size_t i = 0; i < productCommits.size(); i++)
  1191. for (size_t j = 0; j < productCommits[i].size(); j++)
  1192. oracleInput << productCommits[i][j];
  1193. for (size_t i = 0; i < seedCommits.size(); i++)
  1194. for (size_t j = 0; j < seedCommits[i].size(); j++)
  1195. oracleInput << seedCommits[i][j];
  1196. Scalar x = pi[0].challengeParts[0];
  1197. Scalar f1 = pi[0].responseParts[0];
  1198. for (size_t i = 0; i < permutationCommits.size(); i++)
  1199. {
  1200. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1201. {
  1202. size_t piIndex = i * permutationCommits.size() + j + 1;
  1203. Twistpoint U1, U2, U3, U4;
  1204. U2 = pi[piIndex].curvepointUniversals[0];
  1205. Scalar f2 = pi[piIndex].responseParts[0];
  1206. Scalar z1 = pi[piIndex].responseParts[1];
  1207. Scalar z2 = pi[piIndex].responseParts[2];
  1208. U1 = g * f2 + h * z1 - permutationCommits[j][i] * x;
  1209. U3 = oldValues[j] * f1 * f2 +
  1210. h * z2 -
  1211. productCommits[i][j] * x * x -
  1212. U2 * x;
  1213. U4 = g * z2 - seedCommits[i][j] * x * x;
  1214. oracleInput << U1 << U2 << U3 << U4;
  1215. }
  1216. }
  1217. if (x != oracle(oracleInput.str()))
  1218. {
  1219. std::cerr << "Reordered + power things not generated by permutation matrix." << std::endl;
  1220. return false;
  1221. }
  1222. for (size_t i = 0; i < seedCommits.size(); i++)
  1223. {
  1224. Twistpoint sum = seedCommits[i][0];
  1225. for (size_t j = 1; j < seedCommits[i].size(); j++)
  1226. sum = sum + seedCommits[i][j];
  1227. if (sum != Twistpoint())
  1228. {
  1229. std::cerr << "seed commits did not sum to 0, aborting." << std::endl;
  1230. return false;
  1231. }
  1232. }
  1233. return true;
  1234. }
  1235. std::vector<Proof> PrsonaBase::generate_batched_proof_of_reordering_plus_power(
  1236. const std::vector<std::vector<Scalar>>& permutations,
  1237. const Scalar& power,
  1238. const std::vector<std::vector<Scalar>>& permutationSeeds,
  1239. const std::vector<std::vector<Scalar>>& productSeeds,
  1240. const std::vector<Twistpoint>& oldValues,
  1241. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1242. const std::vector<std::vector<Twistpoint>>& productCommits,
  1243. const std::vector<std::vector<Twistpoint>>& seedCommits) const
  1244. {
  1245. std::vector<Proof> retval;
  1246. if (!SERVER_IS_MALICIOUS)
  1247. {
  1248. retval.push_back(Proof("PROOF"));
  1249. return retval;
  1250. }
  1251. Proof first;
  1252. retval.push_back(first);
  1253. Twistpoint g = EL_GAMAL_GENERATOR;
  1254. Twistpoint h = elGamalBlindGenerator;
  1255. std::stringstream oracleInput;
  1256. oracleInput << g << h;
  1257. for (size_t i = 0; i < oldValues.size(); i++)
  1258. oracleInput << oldValues[i];
  1259. for (size_t i = 0; i < permutationCommits.size(); i++)
  1260. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1261. oracleInput << permutationCommits[i][j];
  1262. for (size_t i = 0; i < productCommits.size(); i++)
  1263. for (size_t j = 0; j < productCommits[i].size(); j++)
  1264. oracleInput << productCommits[i][j];
  1265. for (size_t i = 0; i < seedCommits.size(); i++)
  1266. for (size_t j = 0; j < seedCommits[i].size(); j++)
  1267. oracleInput << seedCommits[i][j];
  1268. std::vector<Scalar> b1;
  1269. std::vector<Scalar> b2;
  1270. std::vector<std::vector<Scalar>> b3;
  1271. std::vector<Scalar> t1;
  1272. std::vector<Scalar> t2;
  1273. std::vector<Scalar> t3;
  1274. for (size_t i = 0; i < permutationCommits.size(); i++)
  1275. {
  1276. Proof currProof;
  1277. Scalar currb1;
  1278. Scalar currb2;
  1279. std::vector<Scalar> currb3Row;
  1280. Scalar currt1;
  1281. Scalar currt2;
  1282. Scalar currt3;
  1283. Twistpoint U1, U3, U4;
  1284. std::vector<Twistpoint> U2;
  1285. currb1.set_random();
  1286. currb2.set_random();
  1287. currt1.set_random();
  1288. currt2.set_random();
  1289. currt3.set_random();
  1290. U1 = g * currb2 + h * currt1;
  1291. U3 = oldValues[i] * (currb1 * currb2 + currt3) + h * currt2;
  1292. U4 = g * currt2;
  1293. oracleInput << U1;
  1294. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1295. {
  1296. Twistpoint U2;
  1297. Scalar currb3;
  1298. currb3.set_random();
  1299. Scalar U2mult = (currb1 * permutations[i][j] + currb2 * power + currb3);
  1300. for (size_t k = 0; k < permutationCommits[i].size(); k++)
  1301. {
  1302. if (k == j)
  1303. continue;
  1304. std::stringstream currOracle;
  1305. currOracle << permutationCommits[i][k] << productCommits[k][i] << seedCommits[k][i];
  1306. Scalar currx = oracle(currOracle.str(), LAMBDA);
  1307. U2mult = U2mult + power * permutations[i][j] * currx;
  1308. }
  1309. U2 = oldValues[i] * U2mult;
  1310. currProof.curvepointUniversals.push_back(U2);
  1311. oracleInput << U2;
  1312. currb3Row.push_back(currb3);
  1313. }
  1314. oracleInput << U3 << U4;
  1315. b1.push_back(currb1);
  1316. b2.push_back(currb2);
  1317. b3.push_back(currb3Row);
  1318. t1.push_back(currt1);
  1319. t2.push_back(currt2);
  1320. t3.push_back(currt3);
  1321. retval.push_back(currProof);
  1322. }
  1323. Scalar x = oracle(oracleInput.str());
  1324. retval[0].challengeParts.push_back(x);
  1325. for (size_t i = 0; i < permutationCommits.size(); i++)
  1326. {
  1327. size_t piIndex = i + 1;
  1328. Scalar f1 = b1[i];
  1329. Scalar f2 = b2[i];
  1330. Scalar z1 = t1[i];
  1331. Scalar z2 = t2[i];
  1332. Scalar z3 = t3[i];
  1333. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1334. {
  1335. std::stringstream currOracle;
  1336. currOracle << permutationCommits[i][j] << productCommits[j][i] << seedCommits[j][i];
  1337. Scalar currx = oracle(currOracle.str(), LAMBDA);
  1338. f1 = f1 + power * currx;
  1339. f2 = f2 + permutations[i][j] * currx;
  1340. z1 = z1 + permutationSeeds[i][j] * currx;
  1341. z2 = z2 + productSeeds[j][i] * currx * currx;
  1342. z3 = z3 + b3[i][j] * currx;
  1343. }
  1344. retval[piIndex].responseParts.push_back(f1);
  1345. retval[piIndex].responseParts.push_back(f2);
  1346. retval[piIndex].responseParts.push_back(z1);
  1347. retval[piIndex].responseParts.push_back(z2);
  1348. retval[piIndex].responseParts.push_back(z3);
  1349. }
  1350. return retval;
  1351. }
  1352. bool PrsonaBase::verify_batched_proof_of_reordering_plus_power(
  1353. const std::vector<Proof>& pi,
  1354. const std::vector<Twistpoint>& oldValues,
  1355. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1356. const std::vector<std::vector<Twistpoint>>& productCommits,
  1357. const std::vector<std::vector<Twistpoint>>& seedCommits) const
  1358. {
  1359. if (pi.empty())
  1360. return false;
  1361. if (!SERVER_IS_MALICIOUS)
  1362. return pi[0].hbc == "PROOF";
  1363. Twistpoint g = EL_GAMAL_GENERATOR;
  1364. Twistpoint h = elGamalBlindGenerator;
  1365. std::stringstream oracleInput;
  1366. oracleInput << g << h;
  1367. for (size_t i = 0; i < oldValues.size(); i++)
  1368. oracleInput << oldValues[i];
  1369. for (size_t i = 0; i < permutationCommits.size(); i++)
  1370. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1371. oracleInput << permutationCommits[i][j];
  1372. for (size_t i = 0; i < productCommits.size(); i++)
  1373. for (size_t j = 0; j < productCommits[i].size(); j++)
  1374. oracleInput << productCommits[i][j];
  1375. for (size_t i = 0; i < seedCommits.size(); i++)
  1376. for (size_t j = 0; j < seedCommits[i].size(); j++)
  1377. oracleInput << seedCommits[i][j];
  1378. Scalar x = pi[0].challengeParts[0];
  1379. for (size_t i = 0; i < permutationCommits.size(); i++)
  1380. {
  1381. size_t piIndex = i + 1;
  1382. Scalar f1 = pi[piIndex].responseParts[0];
  1383. Scalar f2 = pi[piIndex].responseParts[1];
  1384. Scalar z1 = pi[piIndex].responseParts[2];
  1385. Scalar z2 = pi[piIndex].responseParts[3];
  1386. Scalar z3 = pi[piIndex].responseParts[4];
  1387. Twistpoint U1 = g * f2 + h * z1;
  1388. std::vector<Twistpoint> U2 = pi[piIndex].curvepointUniversals;
  1389. Twistpoint U3 = oldValues[i] * (f1 * f2 + z3) + h * z2;
  1390. Twistpoint U4 = g * z2;
  1391. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1392. {
  1393. std::stringstream currOracle;
  1394. currOracle << permutationCommits[i][j] << productCommits[j][i] << seedCommits[j][i];
  1395. Scalar currx = oracle(currOracle.str(), LAMBDA);
  1396. U1 = U1 - permutationCommits[i][j] * currx;
  1397. U3 = U3 -
  1398. productCommits[j][i] * currx * currx -
  1399. U2[j] * currx;
  1400. U4 = U4 - seedCommits[j][i] * currx * currx;
  1401. }
  1402. oracleInput << U1;
  1403. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1404. oracleInput << U2[j];
  1405. oracleInput << U3 << U4;
  1406. }
  1407. if (x != oracle(oracleInput.str()))
  1408. {
  1409. std::cerr << "Reordered + power things not generated by permutation matrix." << std::endl;
  1410. return false;
  1411. }
  1412. for (size_t i = 0; i < seedCommits.size(); i++)
  1413. {
  1414. Twistpoint sum = seedCommits[i][0];
  1415. for (size_t j = 1; j < seedCommits[i].size(); j++)
  1416. sum = sum + seedCommits[i][j];
  1417. if (sum != Twistpoint())
  1418. {
  1419. std::cerr << "seed commits did not sum to 0, aborting." << std::endl;
  1420. return false;
  1421. }
  1422. }
  1423. return true;
  1424. }
  1425. std::vector<Proof> PrsonaBase::generate_user_tally_proofs(
  1426. const std::vector<std::vector<Scalar>>& permutations,
  1427. const Scalar& power,
  1428. const Twistpoint& nextGenerator,
  1429. const std::vector<std::vector<Scalar>>& permutationSeeds,
  1430. const std::vector<std::vector<Scalar>>& userTallySeeds,
  1431. const std::vector<Twistpoint>& currPseudonyms,
  1432. const std::vector<Twistpoint>& userTallyMasks,
  1433. const std::vector<Twistpoint>& userTallyMessages,
  1434. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1435. const std::vector<std::vector<Twistpoint>>& userTallyMaskCommits,
  1436. const std::vector<std::vector<Twistpoint>>& userTallyMessageCommits,
  1437. const std::vector<std::vector<Twistpoint>>& userTallySeedCommits) const
  1438. {
  1439. std::vector<Proof> retval;
  1440. if (!SERVER_IS_MALICIOUS)
  1441. {
  1442. retval.push_back(Proof("PROOF"));
  1443. return retval;
  1444. }
  1445. Proof first;
  1446. retval.push_back(first);
  1447. Twistpoint g = EL_GAMAL_GENERATOR;
  1448. Twistpoint h = elGamalBlindGenerator;
  1449. std::stringstream oracleInput;
  1450. oracleInput << g << h << nextGenerator;
  1451. for (size_t i = 0; i < currPseudonyms.size(); i++)
  1452. oracleInput << currPseudonyms[i];
  1453. for (size_t i = 0; i < userTallyMasks.size(); i++)
  1454. oracleInput << userTallyMasks[i];
  1455. for (size_t i = 0; i < userTallyMessages.size(); i++)
  1456. oracleInput << userTallyMessages[i];
  1457. for (size_t i = 0; i < permutationCommits.size(); i++)
  1458. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1459. oracleInput << permutationCommits[i][j];
  1460. for (size_t i = 0; i < userTallyMaskCommits.size(); i++)
  1461. for (size_t j = 0; j < userTallyMaskCommits[i].size(); j++)
  1462. oracleInput << userTallyMaskCommits[i][j];
  1463. for (size_t i = 0; i < userTallyMessageCommits.size(); i++)
  1464. for (size_t j = 0; j < userTallyMessageCommits[i].size(); j++)
  1465. oracleInput << userTallyMessageCommits[i][j];
  1466. for (size_t i = 0; i < userTallySeedCommits.size(); i++)
  1467. for (size_t j = 0; j < userTallySeedCommits[i].size(); j++)
  1468. oracleInput << userTallySeedCommits[i][j];
  1469. Scalar b1;
  1470. b1.set_random();
  1471. std::vector<std::vector<Scalar>> b2;
  1472. std::vector<std::vector<Scalar>> t1;
  1473. std::vector<std::vector<Scalar>> t2;
  1474. for (size_t i = 0; i < permutationCommits.size(); i++)
  1475. {
  1476. std::vector<Scalar> currb2Row;
  1477. std::vector<Scalar> currt1Row;
  1478. std::vector<Scalar> currt2Row;
  1479. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1480. {
  1481. Proof currProof;
  1482. Scalar currb2;
  1483. Scalar currt1;
  1484. Scalar currt2;
  1485. Twistpoint U1, U2, U3, U4, U5, U6, U7;
  1486. currb2.set_random();
  1487. currt1.set_random();
  1488. currt2.set_random();
  1489. U1 = g * currb2 + h * currt1;
  1490. U2 = userTallyMasks[j] * (b1 * permutations[j][i] + currb2 * power) +
  1491. currPseudonyms[j] * (permutations[j][i] * b1 * userTallySeeds[i][j] + power * currb2 * userTallySeeds[i][j] + permutations[j][i] * power * currt2) +
  1492. h * currt2;
  1493. U3 = userTallyMasks[j] * (b1 * currb2) +
  1494. currPseudonyms[j] * (b1 * currb2 * userTallySeeds[i][j] + permutations[j][i] * b1 * currt2 + power * currb2 * currt2);
  1495. U4 = currPseudonyms[j] * (b1 * currb2 * currt2);
  1496. U5 = userTallyMessages[j] * currb2 +
  1497. nextGenerator * (permutations[j][i] * currt2 + userTallySeeds[i][j] * currb2) +
  1498. h * currt2;
  1499. U6 = nextGenerator * (currb2 * currt2);
  1500. U7 = g * currt2;
  1501. currProof.curvepointUniversals.push_back(U2);
  1502. currProof.curvepointUniversals.push_back(U3);
  1503. currProof.curvepointUniversals.push_back(U5);
  1504. oracleInput << U1 << U2 << U3 << U4 << U5 << U6 << U7;
  1505. currb2Row.push_back(currb2);
  1506. currt1Row.push_back(currt1);
  1507. currt2Row.push_back(currt2);
  1508. retval.push_back(currProof);
  1509. }
  1510. b2.push_back(currb2Row);
  1511. t1.push_back(currt1Row);
  1512. t2.push_back(currt2Row);
  1513. }
  1514. Scalar x = oracle(oracleInput.str());
  1515. retval[0].challengeParts.push_back(x);
  1516. Scalar f1 = power * x + b1;
  1517. retval[0].responseParts.push_back(f1);
  1518. for (size_t i = 0; i < permutationCommits.size(); i++)
  1519. {
  1520. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1521. {
  1522. size_t piIndex = i * permutationCommits.size() + j + 1;
  1523. Scalar f2 = permutations[j][i] * x + b2[i][j];
  1524. Scalar z1 = permutationSeeds[j][i] * x + t1[i][j];
  1525. Scalar z2 = userTallySeeds[i][j] * x + t2[i][j];
  1526. retval[piIndex].responseParts.push_back(f2);
  1527. retval[piIndex].responseParts.push_back(z1);
  1528. retval[piIndex].responseParts.push_back(z2);
  1529. }
  1530. }
  1531. return retval;
  1532. }
  1533. bool PrsonaBase::verify_user_tally_proofs(
  1534. const std::vector<Proof>& pi,
  1535. const Twistpoint& nextGenerator,
  1536. const std::vector<Twistpoint>& currPseudonyms,
  1537. const std::vector<Twistpoint>& userTallyMasks,
  1538. const std::vector<Twistpoint>& userTallyMessages,
  1539. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1540. const std::vector<std::vector<Twistpoint>>& userTallyMaskCommits,
  1541. const std::vector<std::vector<Twistpoint>>& userTallyMessageCommits,
  1542. const std::vector<std::vector<Twistpoint>>& userTallySeedCommits) const
  1543. {
  1544. if (pi.empty())
  1545. return false;
  1546. if (!SERVER_IS_MALICIOUS)
  1547. return pi[0].hbc == "PROOF";
  1548. Twistpoint g = EL_GAMAL_GENERATOR;
  1549. Twistpoint h = elGamalBlindGenerator;
  1550. std::stringstream oracleInput;
  1551. oracleInput << g << h << nextGenerator;
  1552. for (size_t i = 0; i < currPseudonyms.size(); i++)
  1553. oracleInput << currPseudonyms[i];
  1554. for (size_t i = 0; i < userTallyMasks.size(); i++)
  1555. oracleInput << userTallyMasks[i];
  1556. for (size_t i = 0; i < userTallyMessages.size(); i++)
  1557. oracleInput << userTallyMessages[i];
  1558. for (size_t i = 0; i < permutationCommits.size(); i++)
  1559. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1560. oracleInput << permutationCommits[i][j];
  1561. for (size_t i = 0; i < userTallyMaskCommits.size(); i++)
  1562. for (size_t j = 0; j < userTallyMaskCommits[i].size(); j++)
  1563. oracleInput << userTallyMaskCommits[i][j];
  1564. for (size_t i = 0; i < userTallyMessageCommits.size(); i++)
  1565. for (size_t j = 0; j < userTallyMessageCommits[i].size(); j++)
  1566. oracleInput << userTallyMessageCommits[i][j];
  1567. for (size_t i = 0; i < userTallySeedCommits.size(); i++)
  1568. for (size_t j = 0; j < userTallySeedCommits[i].size(); j++)
  1569. oracleInput << userTallySeedCommits[i][j];
  1570. Scalar x = pi[0].challengeParts[0];
  1571. Scalar f1 = pi[0].responseParts[0];
  1572. for (size_t i = 0; i < permutationCommits.size(); i++)
  1573. {
  1574. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1575. {
  1576. size_t piIndex = i * permutationCommits.size() + j + 1;
  1577. Twistpoint U1, U2, U3, U4, U5, U6, U7;
  1578. U2 = pi[piIndex].curvepointUniversals[0];
  1579. U3 = pi[piIndex].curvepointUniversals[1];
  1580. U5 = pi[piIndex].curvepointUniversals[2];
  1581. Scalar f2 = pi[piIndex].responseParts[0];
  1582. Scalar z1 = pi[piIndex].responseParts[1];
  1583. Scalar z2 = pi[piIndex].responseParts[2];
  1584. U1 = g * f2 + h * z1 - permutationCommits[j][i] * x;
  1585. U4 = userTallyMasks[j] * (f1 * f2 * x) +
  1586. currPseudonyms[j] * (f1 * f2 * z2) +
  1587. h * (z2 * x * x) -
  1588. userTallyMaskCommits[i][j] * (x * x * x) -
  1589. U2 * (x * x) -
  1590. U3 * x;
  1591. U6 = userTallyMessages[j] * (f2 * x) +
  1592. nextGenerator * (f2 * z2) +
  1593. h * (z2 * x) -
  1594. userTallyMessageCommits[i][j] * (x * x) -
  1595. U5 * x;
  1596. U7 = g * z2 - userTallySeedCommits[i][j] * x;
  1597. oracleInput << U1 << U2 << U3 << U4 << U5 << U6 << U7;
  1598. }
  1599. }
  1600. if (x != oracle(oracleInput.str()))
  1601. {
  1602. std::cerr << "User tallies not generated by permutation matrix." << std::endl;
  1603. return false;
  1604. }
  1605. for (size_t i = 0; i < userTallySeedCommits.size(); i++)
  1606. {
  1607. Twistpoint sum = userTallySeedCommits[i][0];
  1608. for (size_t j = 1; j < userTallySeedCommits[i].size(); j++)
  1609. sum = sum + userTallySeedCommits[i][j];
  1610. if (sum != Twistpoint())
  1611. {
  1612. std::cerr << "seed commits did not sum to 0, aborting." << std::endl;
  1613. return false;
  1614. }
  1615. }
  1616. return true;
  1617. }
  1618. /*
  1619. * SERVER AGREEMENT PROOFS
  1620. */
  1621. Proof PrsonaBase::generate_valid_vote_row_proof(
  1622. const std::vector<TwistBipoint>& commitment) const
  1623. {
  1624. Proof retval;
  1625. if (!SERVER_IS_MALICIOUS)
  1626. {
  1627. retval.hbc = "PROOF";
  1628. return retval;
  1629. }
  1630. std::stringstream oracleInput;
  1631. for (size_t i = 0; i < commitment.size(); i++)
  1632. oracleInput << commitment[i];
  1633. Scalar val = oracle(oracleInput.str());
  1634. retval.responseParts.push_back(val);
  1635. return retval;
  1636. }
  1637. Proof PrsonaBase::generate_valid_vote_matrix_proof(
  1638. const std::vector<std::vector<TwistBipoint>>& commitment) const
  1639. {
  1640. Proof retval;
  1641. if (!SERVER_IS_MALICIOUS)
  1642. {
  1643. retval.hbc = "PROOF";
  1644. return retval;
  1645. }
  1646. std::stringstream oracleInput;
  1647. for (size_t i = 0; i < commitment.size(); i++)
  1648. for (size_t j = 0; j < commitment[i].size(); j++)
  1649. oracleInput << commitment[i][j];
  1650. Scalar val = oracle(oracleInput.str());
  1651. retval.responseParts.push_back(val);
  1652. return retval;
  1653. }
  1654. Proof PrsonaBase::generate_valid_user_tally_proof(
  1655. const EGCiphertext& commitment) const
  1656. {
  1657. Proof retval;
  1658. if (!SERVER_IS_MALICIOUS)
  1659. {
  1660. retval.hbc = "PROOF";
  1661. return retval;
  1662. }
  1663. std::stringstream oracleInput;
  1664. oracleInput << commitment;
  1665. Scalar val = oracle(oracleInput.str());
  1666. retval.responseParts.push_back(val);
  1667. return retval;
  1668. }
  1669. Proof PrsonaBase::generate_valid_server_tally_proof(
  1670. const CurveBipoint& commitment) const
  1671. {
  1672. Proof retval;
  1673. if (!SERVER_IS_MALICIOUS)
  1674. {
  1675. retval.hbc = "PROOF";
  1676. return retval;
  1677. }
  1678. std::stringstream oracleInput;
  1679. oracleInput << commitment;
  1680. Scalar val = oracle(oracleInput.str());
  1681. retval.responseParts.push_back(val);
  1682. return retval;
  1683. }
  1684. Proof PrsonaBase::generate_valid_pseudonyms_proof(
  1685. const std::vector<Twistpoint>& commitment) const
  1686. {
  1687. Proof retval;
  1688. if (!SERVER_IS_MALICIOUS)
  1689. {
  1690. retval.hbc = "PROOF";
  1691. return retval;
  1692. }
  1693. std::stringstream oracleInput;
  1694. for (size_t i = 0; i < commitment.size(); i++)
  1695. oracleInput << commitment[i];
  1696. Scalar val = oracle(oracleInput.str());
  1697. retval.responseParts.push_back(val);
  1698. return retval;
  1699. }
  1700. bool PrsonaBase::verify_valid_vote_row_proof(
  1701. const std::vector<Proof>& pi,
  1702. const std::vector<TwistBipoint>& commitment) const
  1703. {
  1704. if (pi.empty())
  1705. return false;
  1706. if (!SERVER_IS_MALICIOUS)
  1707. return pi[0].hbc == "PROOF";
  1708. Scalar comparison = pi[0].responseParts[0];
  1709. std::stringstream oracleInput;
  1710. for (size_t i = 0; i < commitment.size(); i++)
  1711. oracleInput << commitment[i];
  1712. if (oracle(oracleInput.str()) != comparison)
  1713. {
  1714. std::cerr << "Server's claimed value doesn't match their own commitment." << std::endl;
  1715. return false;
  1716. }
  1717. size_t agreement = 1;
  1718. for (size_t i = 1; i < pi.size(); i++)
  1719. if (comparison == pi[i].responseParts[0])
  1720. agreement++;
  1721. return agreement * 2 > pi.size();
  1722. }
  1723. bool PrsonaBase::verify_valid_vote_matrix_proof(
  1724. const std::vector<Proof>& pi,
  1725. const std::vector<std::vector<TwistBipoint>>& commitment) const
  1726. {
  1727. if (pi.empty())
  1728. return false;
  1729. if (!SERVER_IS_MALICIOUS)
  1730. return pi[0].hbc == "PROOF";
  1731. Scalar comparison = pi[0].responseParts[0];
  1732. std::stringstream oracleInput;
  1733. for (size_t i = 0; i < commitment.size(); i++)
  1734. for (size_t j = 0; j < commitment[i].size(); j++)
  1735. oracleInput << commitment[i][j];
  1736. if (oracle(oracleInput.str()) != comparison)
  1737. {
  1738. std::cerr << "Server's claimed value doesn't match their own commitment." << std::endl;
  1739. return false;
  1740. }
  1741. size_t agreement = 1;
  1742. for (size_t i = 1; i < pi.size(); i++)
  1743. if (comparison == pi[i].responseParts[0])
  1744. agreement++;
  1745. return agreement * 2 > pi.size();
  1746. }
  1747. bool PrsonaBase::verify_valid_user_tally_proof(
  1748. const std::vector<Proof>& pi,
  1749. const EGCiphertext& commitment) const
  1750. {
  1751. if (pi.empty())
  1752. return false;
  1753. if (!SERVER_IS_MALICIOUS)
  1754. return pi[0].hbc == "PROOF";
  1755. Scalar comparison = pi[0].responseParts[0];
  1756. std::stringstream oracleInput;
  1757. oracleInput << commitment;
  1758. if (oracle(oracleInput.str()) != comparison)
  1759. {
  1760. std::cerr << "Server's claimed value doesn't match their own commitment." << std::endl;
  1761. return false;
  1762. }
  1763. size_t agreement = 1;
  1764. for (size_t i = 1; i < pi.size(); i++)
  1765. if (comparison == pi[i].responseParts[0])
  1766. agreement++;
  1767. return agreement * 2 > pi.size();
  1768. }
  1769. bool PrsonaBase::verify_valid_server_tally_proof(
  1770. const std::vector<Proof>& pi,
  1771. const CurveBipoint& commitment) const
  1772. {
  1773. if (pi.empty())
  1774. return false;
  1775. if (!SERVER_IS_MALICIOUS)
  1776. return pi[0].hbc == "PROOF";
  1777. Scalar comparison = pi[0].responseParts[0];
  1778. std::stringstream oracleInput;
  1779. oracleInput << commitment;
  1780. if (oracle(oracleInput.str()) != comparison)
  1781. {
  1782. std::cerr << "Server's claimed value doesn't match their own commitment." << std::endl;
  1783. return false;
  1784. }
  1785. size_t agreement = 1;
  1786. for (size_t i = 1; i < pi.size(); i++)
  1787. if (comparison == pi[i].responseParts[0])
  1788. agreement++;
  1789. return agreement * 2 > pi.size();
  1790. }
  1791. bool PrsonaBase::verify_valid_pseudonyms_proof(
  1792. const std::vector<Proof>& pi,
  1793. const std::vector<Twistpoint>& commitment) const
  1794. {
  1795. if (pi.empty())
  1796. return false;
  1797. if (!SERVER_IS_MALICIOUS)
  1798. return pi[0].hbc == "PROOF";
  1799. Scalar comparison = pi[0].responseParts[0];
  1800. std::stringstream oracleInput;
  1801. for (size_t i = 0; i < commitment.size(); i++)
  1802. oracleInput << commitment[i];
  1803. if (oracle(oracleInput.str()) != comparison)
  1804. {
  1805. std::cerr << "Server's claimed value doesn't match their own commitment." << std::endl;
  1806. return false;
  1807. }
  1808. size_t agreement = 1;
  1809. for (size_t i = 1; i < pi.size(); i++)
  1810. if (comparison == pi[i].responseParts[0])
  1811. agreement++;
  1812. return agreement * 2 > pi.size();
  1813. }
  1814. template std::vector<Proof> PrsonaBase::generate_proof_of_reordering<Twistpoint>(
  1815. const std::vector<std::vector<Scalar>>& permutations,
  1816. const std::vector<std::vector<Scalar>>& permutationSeeds,
  1817. const std::vector<std::vector<Scalar>>& productSeeds,
  1818. const std::vector<Twistpoint>& oldValues,
  1819. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1820. const std::vector<std::vector<Twistpoint>>& productCommits,
  1821. const Twistpoint& otherG,
  1822. const Twistpoint& otherH) const;
  1823. template std::vector<Proof> PrsonaBase::generate_proof_of_reordering<TwistBipoint>(
  1824. const std::vector<std::vector<Scalar>>& permutations,
  1825. const std::vector<std::vector<Scalar>>& permutationSeeds,
  1826. const std::vector<std::vector<Scalar>>& productSeeds,
  1827. const std::vector<TwistBipoint>& oldValues,
  1828. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1829. const std::vector<std::vector<TwistBipoint>>& productCommits,
  1830. const TwistBipoint& otherG,
  1831. const TwistBipoint& otherH) const;
  1832. template std::vector<Proof> PrsonaBase::generate_proof_of_reordering<CurveBipoint>(
  1833. const std::vector<std::vector<Scalar>>& permutations,
  1834. const std::vector<std::vector<Scalar>>& permutationSeeds,
  1835. const std::vector<std::vector<Scalar>>& productSeeds,
  1836. const std::vector<CurveBipoint>& oldValues,
  1837. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1838. const std::vector<std::vector<CurveBipoint>>& productCommits,
  1839. const CurveBipoint& otherG,
  1840. const CurveBipoint& otherH) const;
  1841. template bool PrsonaBase::verify_proof_of_reordering<Twistpoint>(
  1842. const std::vector<Proof>& pi,
  1843. const std::vector<Twistpoint>& oldValues,
  1844. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1845. const std::vector<std::vector<Twistpoint>>& productCommits,
  1846. const Twistpoint& otherG,
  1847. const Twistpoint& otherH) const;
  1848. template bool PrsonaBase::verify_proof_of_reordering<TwistBipoint>(
  1849. const std::vector<Proof>& pi,
  1850. const std::vector<TwistBipoint>& oldValues,
  1851. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1852. const std::vector<std::vector<TwistBipoint>>& productCommits,
  1853. const TwistBipoint& otherG,
  1854. const TwistBipoint& otherH) const;
  1855. template bool PrsonaBase::verify_proof_of_reordering<CurveBipoint>(
  1856. const std::vector<Proof>& pi,
  1857. const std::vector<CurveBipoint>& oldValues,
  1858. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1859. const std::vector<std::vector<CurveBipoint>>& productCommits,
  1860. const CurveBipoint& otherG,
  1861. const CurveBipoint& otherH) const;