base.cpp 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749
  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. return true;
  212. if (pi[0].curvepointUniversals[0] != EL_GAMAL_GENERATOR)
  213. return false;
  214. for (size_t i = 0; i < pi.size(); i++)
  215. {
  216. Twistpoint generator = pi[i].curvepointUniversals[0];
  217. Twistpoint commitment = (i == pi.size() - 1 ? currGenerator : pi[i + 1].curvepointUniversals[0]);
  218. Scalar c = pi[i].challengeParts[0];
  219. Scalar z = pi[i].responseParts[0];
  220. retval = retval && schnorr_verification(generator, commitment, c, z);
  221. if (!retval)
  222. std::cerr << "Error in index " << i+1 << " of " << pi.size() << std::endl;
  223. }
  224. return retval;
  225. }
  226. /*
  227. * REPUTATION PROOFS
  228. */
  229. // A pretty straightforward range proof (generation)
  230. std::vector<Proof> PrsonaBase::generate_reputation_proof(
  231. const Proof& ownershipProof,
  232. const EGCiphertext& commitment,
  233. const Scalar& currentScore,
  234. const Scalar& threshold,
  235. const Scalar& inverseKey,
  236. size_t numClients) const
  237. {
  238. std::vector<Proof> retval;
  239. // Base case
  240. if (!CLIENT_IS_MALICIOUS)
  241. {
  242. retval.push_back(Proof("PROOF"));
  243. return retval;
  244. }
  245. // Don't even try if the user asks to make an illegitimate proof
  246. if (threshold.toInt() > (numClients * MAX_ALLOWED_VOTE))
  247. return retval;
  248. // We really have two consecutive proofs in a junction.
  249. // The first is to prove that we are the stpk we claim we are
  250. retval.push_back(ownershipProof);
  251. // The value we're actually using in our proof
  252. mpz_class proofVal = (currentScore - threshold).toInt();
  253. // Top of the range in our proof determined by what scores are even possible
  254. mpz_class proofBits = log2(numClients * MAX_ALLOWED_VOTE - threshold.toInt());
  255. // Don't risk a situation that would divulge our private key
  256. if (proofBits <= 1)
  257. proofBits = 2;
  258. // This seems weird, but remember our base is A_t^r, not g^t
  259. std::vector<Scalar> masksPerBit;
  260. masksPerBit.push_back(inverseKey);
  261. for (size_t i = 1; i < proofBits; i++)
  262. {
  263. Scalar currMask;
  264. currMask.set_random();
  265. masksPerBit.push_back(currMask);
  266. masksPerBit[0] = masksPerBit[0] - (currMask * Scalar(1 << i));
  267. }
  268. // Taken from Fig. 1 in https://eprint.iacr.org/2014/764.pdf
  269. for (size_t i = 0; i < proofBits; i++)
  270. {
  271. Proof currProof;
  272. Twistpoint g, h, C, C_a, C_b;
  273. g = commitment.mask;
  274. h = elGamalBlindGenerator;
  275. mpz_class currBit = bit(proofVal & (1 << i));
  276. Scalar a, s, t, m, r;
  277. a.set_random();
  278. s.set_random();
  279. t.set_random();
  280. m = Scalar(currBit);
  281. r = masksPerBit[i];
  282. C = g * r + h * m;
  283. currProof.curvepointUniversals.push_back(C);
  284. C_a = g * s + h * a;
  285. C_b = g * t + h * a * m;
  286. std::stringstream oracleInput;
  287. oracleInput << g << h << C << C_a << C_b;
  288. Scalar x = oracle(oracleInput.str());
  289. currProof.challengeParts.push_back(x);
  290. Scalar f, z_a, z_b;
  291. f = m * x + a;
  292. z_a = r * x + s;
  293. z_b = r * (x - f) + t;
  294. currProof.responseParts.push_back(f);
  295. currProof.responseParts.push_back(z_a);
  296. currProof.responseParts.push_back(z_b);
  297. retval.push_back(currProof);
  298. }
  299. return retval;
  300. }
  301. // A pretty straightforward range proof (verification)
  302. bool PrsonaBase::verify_reputation_proof(
  303. const std::vector<Proof>& pi,
  304. const Twistpoint& generator,
  305. const Twistpoint& owner,
  306. const EGCiphertext& commitment,
  307. const Scalar& threshold) const
  308. {
  309. // Reject outright if there's no proof to check
  310. if (pi.empty())
  311. {
  312. std::cerr << "Proof was empty, aborting." << std::endl;
  313. return false;
  314. }
  315. // If the range is so big that it wraps around mod n,
  316. // there's a chance the user actually made a proof for a very low reputation
  317. if (pi.size() > 256)
  318. {
  319. std::cerr << "Proof was too big, prover could have cheated." << std::endl;
  320. return false;
  321. }
  322. if (!CLIENT_IS_MALICIOUS)
  323. return pi[0].hbc == "PROOF";
  324. Scalar ownerChallenge, ownerResponse;
  325. ownerChallenge = pi[0].challengeParts[0];
  326. ownerResponse = pi[0].responseParts[0];
  327. // User should be able to prove they are who they say they are
  328. if (!schnorr_verification(generator, owner, ownerChallenge, ownerResponse))
  329. {
  330. std::cerr << "Schnorr proof failed, aborting." << std::endl;
  331. return false;
  332. }
  333. // X is the thing we're going to be checking in on throughout
  334. // to try to get our score commitment back in the end.
  335. Twistpoint X;
  336. for (size_t i = 1; i < pi.size(); i++)
  337. {
  338. Twistpoint C, g, h;
  339. C = pi[i].curvepointUniversals[0];
  340. g = commitment.mask;
  341. h = elGamalBlindGenerator;
  342. X = X + C * Scalar(1 << (i - 1));
  343. Scalar x, f, z_a, z_b;
  344. x = pi[i].challengeParts[0];
  345. f = pi[i].responseParts[0];
  346. z_a = pi[i].responseParts[1];
  347. z_b = pi[i].responseParts[2];
  348. // Taken from Fig. 1 in https://eprint.iacr.org/2014/764.pdf
  349. Twistpoint C_a, C_b;
  350. C_a = g * z_a + h * f - C * x;
  351. C_b = g * z_b - C * (x - f);
  352. std::stringstream oracleInput;
  353. oracleInput << g << h << C << C_a << C_b;
  354. if (oracle(oracleInput.str()) != x)
  355. {
  356. std::cerr << "0 or 1 proof failed at index " << i << " of " << pi.size() - 1 << ", aborting." << std::endl;
  357. return false;
  358. }
  359. }
  360. Twistpoint scoreCommitment = commitment.encryptedMessage + elGamalBlindGenerator * -threshold;
  361. return X == scoreCommitment;
  362. }
  363. /*
  364. * VALID VOTE PROOFS
  365. */
  366. std::vector<Proof> PrsonaBase::generate_vote_proof(
  367. const Proof& ownershipProof,
  368. const TwistBipoint& g,
  369. const TwistBipoint& h,
  370. const std::vector<bool>& replaces,
  371. const std::vector<TwistBipoint>& oldEncryptedVotes,
  372. const std::vector<TwistBipoint>& newEncryptedVotes,
  373. const std::vector<Scalar>& seeds,
  374. const std::vector<Scalar>& votes) const
  375. {
  376. std::vector<Proof> retval;
  377. // Base case
  378. if (!CLIENT_IS_MALICIOUS)
  379. {
  380. retval.push_back(Proof("PROOF"));
  381. return retval;
  382. }
  383. // The first need is to prove that we are the stpk we claim we are
  384. retval.push_back(ownershipProof);
  385. // Then, we iterate over all votes for the proofs that they are correct
  386. for (size_t i = 0; i < replaces.size(); i++)
  387. {
  388. std::stringstream oracleInput;
  389. oracleInput << g << h << oldEncryptedVotes[i] << newEncryptedVotes[i];
  390. Scalar m = votes[i];
  391. Scalar r = seeds[i];
  392. /* This proof structure is documented in my notes.
  393. * It's inspired by the proof in Fig. 1 at
  394. * https://eprint.iacr.org/2014/764.pdf, but adapted so that you prove
  395. * m(m-1)(m-2) = 0 instead of m(m-1) = 0.
  396. *
  397. * The rerandomization part is just a slight variation on an
  398. * ordinary Schnorr proof, so that part's less scary. */
  399. if (replaces[i]) // CASE: Make new vote
  400. {
  401. Proof currProof;
  402. Scalar x_r, z_r, a, s, t_1, t_2;
  403. x_r.set_random();
  404. z_r.set_random();
  405. a.set_random();
  406. s.set_random();
  407. t_1.set_random();
  408. t_2.set_random();
  409. TwistBipoint U = h * z_r +
  410. oldEncryptedVotes[i] * x_r -
  411. newEncryptedVotes[i] * x_r;
  412. TwistBipoint C_a = g * a + h * s;
  413. Scalar power = ((a + a) * m * m - (a + a + a) * m);
  414. TwistBipoint C_b = g * power + h * t_1;
  415. currProof.curveBipointUniversals.push_back(C_b);
  416. TwistBipoint C_c = g * (a * a * m) + h * t_2;
  417. oracleInput << U << C_a << C_b << C_c;
  418. Scalar x = oracle(oracleInput.str());
  419. Scalar x_n = x - x_r;
  420. currProof.challengeParts.push_back(x_r);
  421. currProof.challengeParts.push_back(x_n);
  422. Scalar f = m * x_n + a;
  423. Scalar z_na = r * x_n + s;
  424. Scalar z_nb = r * (f - x_n) * (x_n + x_n - f) + t_1 * x_n + t_2;
  425. currProof.responseParts.push_back(z_r);
  426. currProof.responseParts.push_back(f);
  427. currProof.responseParts.push_back(z_na);
  428. currProof.responseParts.push_back(z_nb);
  429. retval.push_back(currProof);
  430. }
  431. else // CASE: Rerandomize existing vote
  432. {
  433. Proof currProof;
  434. Scalar u, commitmentLambda_1, commitmentLambda_2, x_n, z_na, z_nb, f;
  435. u.set_random();
  436. commitmentLambda_1.set_random();
  437. commitmentLambda_2.set_random();
  438. x_n.set_random();
  439. z_na.set_random();
  440. z_nb.set_random();
  441. f.set_random();
  442. TwistBipoint U = h * u;
  443. TwistBipoint C_a = g * f +
  444. h * z_na -
  445. newEncryptedVotes[i] * x_n;
  446. TwistBipoint C_b = g * commitmentLambda_1 + h * commitmentLambda_2;
  447. currProof.curveBipointUniversals.push_back(C_b);
  448. TwistBipoint C_c = h * z_nb -
  449. newEncryptedVotes[i] * ((f - x_n) * (x_n + x_n - f)) -
  450. C_b * x_n;
  451. oracleInput << U << C_a << C_b << C_c;
  452. Scalar x = oracle(oracleInput.str());
  453. Scalar x_r = x - x_n;
  454. currProof.challengeParts.push_back(x_r);
  455. currProof.challengeParts.push_back(x_n);
  456. Scalar z_r = r * x_r + u;
  457. currProof.responseParts.push_back(z_r);
  458. currProof.responseParts.push_back(f);
  459. currProof.responseParts.push_back(z_na);
  460. currProof.responseParts.push_back(z_nb);
  461. retval.push_back(currProof);
  462. }
  463. }
  464. return retval;
  465. }
  466. bool PrsonaBase::verify_vote_proof(
  467. const TwistBipoint& g,
  468. const TwistBipoint& h,
  469. const std::vector<Proof>& pi,
  470. const std::vector<TwistBipoint>& oldEncryptedVotes,
  471. const std::vector<TwistBipoint>& newEncryptedVotes,
  472. const Twistpoint& freshGenerator,
  473. const Twistpoint& owner) const
  474. {
  475. // Reject outright if there's no proof to check
  476. if (pi.empty())
  477. {
  478. std::cerr << "Proof was empty, aborting." << std::endl;
  479. return false;
  480. }
  481. // Base case
  482. if (!CLIENT_IS_MALICIOUS)
  483. return pi[0].hbc == "PROOF";
  484. // User should be able to prove they are who they say they are
  485. if (!verify_ownership_proof(pi[0], freshGenerator, owner))
  486. {
  487. std::cerr << "Schnorr proof failed, aborting." << std::endl;
  488. return false;
  489. }
  490. /* This proof structure is documented in my notes.
  491. * It's inspired by the proof in Fig. 1 at
  492. * https://eprint.iacr.org/2014/764.pdf, but adapted so that you prove
  493. * m(m-1)(m-2) = 0 instead of m(m-1) = 0.
  494. *
  495. * The rerandomization part is just a slight variation on an
  496. * ordinary Schnorr proof, so that part's less scary. */
  497. for (size_t i = 1; i < pi.size(); i++)
  498. {
  499. size_t voteIndex = i - 1;
  500. TwistBipoint C_b;
  501. C_b = pi[i].curveBipointUniversals[0];
  502. Scalar x_r, x_n, z_r, f, z_na, z_nb;
  503. x_r = pi[i].challengeParts[0];
  504. x_n = pi[i].challengeParts[1];
  505. z_r = pi[i].responseParts[0];
  506. f = pi[i].responseParts[1];
  507. z_na = pi[i].responseParts[2];
  508. z_nb = pi[i].responseParts[3];
  509. TwistBipoint U, C_a, C_c;
  510. U = h * z_r +
  511. oldEncryptedVotes[voteIndex] * x_r -
  512. newEncryptedVotes[voteIndex] * x_r;
  513. C_a = g * f + h * z_na - newEncryptedVotes[voteIndex] * x_n;
  514. C_c = h * z_nb -
  515. newEncryptedVotes[voteIndex] * ((f - x_n) * (x_n + x_n - f)) -
  516. C_b * x_n;
  517. std::stringstream oracleInput;
  518. oracleInput << g << h << oldEncryptedVotes[voteIndex] << newEncryptedVotes[voteIndex] << U << C_a << C_b << C_c;
  519. if (oracle(oracleInput.str()) != x_r + x_n)
  520. return false;
  521. }
  522. return true;
  523. }
  524. /*
  525. * NEW USER PROOFS
  526. */
  527. std::vector<Proof> PrsonaBase::generate_proof_of_added_user(
  528. const Scalar& twistBipointSeed,
  529. const Scalar& EGCiphertextSeed,
  530. const std::vector<Scalar>& curveBipointSelfSeeds,
  531. const std::vector<Scalar>& curveBipointOtherSeeds) const
  532. {
  533. std::vector<Proof> retval;
  534. if (!SERVER_IS_MALICIOUS)
  535. {
  536. retval.push_back(Proof("PROOF"));
  537. return retval;
  538. }
  539. Proof currProof;
  540. currProof.responseParts.push_back(twistBipointSeed);
  541. retval.push_back(currProof);
  542. currProof.responseParts.clear();
  543. currProof.responseParts.push_back(EGCiphertextSeed);
  544. retval.push_back(currProof);
  545. currProof.responseParts.clear();
  546. for (size_t i = 0; i < curveBipointSelfSeeds.size(); i++)
  547. currProof.responseParts.push_back(curveBipointSelfSeeds[i]);
  548. retval.push_back(currProof);
  549. currProof.responseParts.clear();
  550. for (size_t i = 0; i < curveBipointOtherSeeds.size(); i++)
  551. currProof.responseParts.push_back(curveBipointOtherSeeds[i]);
  552. retval.push_back(currProof);
  553. return retval;
  554. }
  555. bool PrsonaBase::verify_proof_of_added_user(
  556. const std::vector<Proof>& pi,
  557. const Twistpoint& currentFreshGenerator,
  558. const Twistpoint& shortTermPublicKey,
  559. const TwistBipoint& curveG,
  560. const TwistBipoint& curveH,
  561. const CurveBipoint& twistG,
  562. const CurveBipoint& twistH,
  563. size_t selfIndex,
  564. const EGCiphertext& userEncryptedScore,
  565. const CurveBipoint& serverEncryptedScore,
  566. const std::vector<std::vector<TwistBipoint>> encryptedVoteMatrix) const
  567. {
  568. if (pi.empty())
  569. {
  570. std::cerr << "Proof empty." << std::endl;
  571. return false;
  572. }
  573. if (!SERVER_IS_MALICIOUS)
  574. return true;
  575. Scalar currSeed = pi[0].responseParts[0];
  576. if (serverEncryptedScore !=
  577. twistG * DEFAULT_TALLY + twistH * currSeed)
  578. {
  579. std::cerr << "Issue in server encrypted score." << std::endl;
  580. return false;
  581. }
  582. currSeed = pi[1].responseParts[0];
  583. if (userEncryptedScore.mask != shortTermPublicKey * currSeed)
  584. {
  585. std::cerr << "Issue in user encrypted score: mask." << std::endl;
  586. return false;
  587. }
  588. if (userEncryptedScore.encryptedMessage !=
  589. currentFreshGenerator * currSeed + elGamalBlindGenerator * DEFAULT_TALLY)
  590. {
  591. std::cerr << "Issue in user encrypted score: value." << std::endl;
  592. return false;
  593. }
  594. for (size_t i = 0; i < pi[2].responseParts.size(); i++)
  595. {
  596. TwistBipoint currVote = encryptedVoteMatrix[selfIndex][i];
  597. currSeed = pi[2].responseParts[i];
  598. if (i == selfIndex)
  599. {
  600. if (currVote !=
  601. curveG * Scalar(MAX_ALLOWED_VOTE) + curveH * currSeed)
  602. {
  603. std::cerr << "Issue in self vote." << std::endl;
  604. return false;
  605. }
  606. }
  607. else
  608. {
  609. if (currVote !=
  610. curveG * DEFAULT_VOTE + curveH * currSeed)
  611. {
  612. std::cerr << "Issue in vote by verifier for user " << i + 1 << " of " << pi[2].responseParts.size() << "." << std::endl;
  613. return false;
  614. }
  615. }
  616. }
  617. for (size_t i = 0; i < pi[3].responseParts.size(); i++)
  618. {
  619. TwistBipoint currVote = encryptedVoteMatrix[i][selfIndex];
  620. currSeed = pi[3].responseParts[i];
  621. if (i != selfIndex)
  622. {
  623. if (currVote !=
  624. curveG * DEFAULT_VOTE + curveH * currSeed)
  625. {
  626. std::cerr << "Issue in vote for verifier by user " << i + 1 << " of " << pi[3].responseParts.size() << "." << std::endl;
  627. return false;
  628. }
  629. }
  630. }
  631. return true;
  632. }
  633. /*
  634. * EPOCH PROOFS
  635. */
  636. std::vector<Proof> PrsonaBase::generate_valid_permutation_proof(
  637. const std::vector<std::vector<Scalar>>& permutations,
  638. const std::vector<std::vector<Scalar>>& seeds,
  639. const std::vector<std::vector<Twistpoint>>& commits) const
  640. {
  641. std::vector<Proof> retval;
  642. if (!SERVER_IS_MALICIOUS)
  643. {
  644. retval.push_back(Proof("PROOF"));
  645. return retval;
  646. }
  647. Twistpoint g, h;
  648. g = EL_GAMAL_GENERATOR;
  649. h = elGamalBlindGenerator;
  650. std::stringstream oracleInput;
  651. oracleInput << g << h;
  652. retval.push_back(Proof());
  653. std::vector<std::vector<Scalar>> a, s, t;
  654. for (size_t i = 0; i < commits.size(); i++)
  655. {
  656. std::vector<Scalar> currA, currS, currT;
  657. for (size_t j = 0; j < commits[i].size(); j++)
  658. {
  659. oracleInput << commits[i][j];
  660. currA.push_back(Scalar());
  661. currS.push_back(Scalar());
  662. currT.push_back(Scalar());
  663. retval.push_back(Proof());
  664. }
  665. a.push_back(currA);
  666. s.push_back(currS);
  667. t.push_back(currT);
  668. }
  669. // Taken from Fig. 1 in https://eprint.iacr.org/2014/764.pdf
  670. for (size_t i = 0; i < permutations.size(); i++)
  671. {
  672. for (size_t j = 0; j < permutations[i].size(); j++)
  673. {
  674. Twistpoint C, C_a, C_b;
  675. Scalar p, r;
  676. a[i][j].set_random();
  677. s[i][j].set_random();
  678. t[i][j].set_random();
  679. p = permutations[i][j];
  680. r = seeds[i][j];
  681. C = commits[i][j];
  682. C_a = g * a[i][j] + h * s[i][j];
  683. C_b = g * a[i][j] * p + h * t[i][j];
  684. oracleInput << C << C_a << C_b;
  685. }
  686. }
  687. Scalar x = oracle(oracleInput.str());
  688. retval[0].challengeParts.push_back(x);
  689. for (size_t i = 0; i < permutations.size(); i++)
  690. {
  691. for (size_t j = 0; j < permutations[i].size(); j++)
  692. {
  693. size_t piIndex = i * permutations.size() + j + 1;
  694. Scalar f, z_a, z_b, p, r;
  695. p = permutations[i][j];
  696. r = seeds[i][j];
  697. f = p * x + a[i][j];
  698. z_a = r * x + s[i][j];
  699. z_b = r * (x - f) + t[i][j];
  700. retval[piIndex].responseParts.push_back(f);
  701. retval[piIndex].responseParts.push_back(z_a);
  702. retval[piIndex].responseParts.push_back(z_b);
  703. }
  704. }
  705. return retval;
  706. }
  707. bool PrsonaBase::verify_valid_permutation_proof(
  708. const std::vector<Proof>& pi,
  709. const std::vector<std::vector<Twistpoint>>& commits) const
  710. {
  711. // Reject outright if there's no proof to check
  712. if (pi.empty())
  713. {
  714. std::cerr << "Proof was empty, aborting." << std::endl;
  715. return false;
  716. }
  717. if (!SERVER_IS_MALICIOUS)
  718. return true;
  719. Twistpoint g, h;
  720. g = EL_GAMAL_GENERATOR;
  721. h = elGamalBlindGenerator;
  722. std::stringstream oracleInput;
  723. oracleInput << g << h;
  724. for (size_t i = 0; i < commits.size(); i++)
  725. for (size_t j = 0; j < commits[i].size(); j++)
  726. oracleInput << commits[i][j];
  727. Scalar x = pi[0].challengeParts[0];
  728. for (size_t i = 0; i < commits.size(); i++)
  729. {
  730. for (size_t j = 0; j < commits[i].size(); j++)
  731. {
  732. size_t piIndex = i * commits.size() + j + 1;
  733. Twistpoint C, C_a, C_b;
  734. C = commits[i][j];
  735. Scalar f, z_a, z_b;
  736. f = pi[piIndex].responseParts[0];
  737. z_a = pi[piIndex].responseParts[1];
  738. z_b = pi[piIndex].responseParts[2];
  739. // Taken from Fig. 1 in https://eprint.iacr.org/2014/764.pdf
  740. C_a = g * f + h * z_a - C * x;
  741. C_b = h * z_b - C * (x - f);
  742. oracleInput << C << C_a << C_b;
  743. }
  744. }
  745. if (oracle(oracleInput.str()) != x)
  746. {
  747. std::cerr << "0 or 1 proof failed, aborting." << std::endl;
  748. return false;
  749. }
  750. for (size_t i = 0; i < commits.size(); i++)
  751. {
  752. Twistpoint sum = commits[i][0];
  753. for (size_t j = 1; j < commits[i].size(); j++)
  754. sum = sum + commits[i][j];
  755. if (sum != g)
  756. {
  757. std::cerr << "Commits did not sum to g, aborting." << std::endl;
  758. return false;
  759. }
  760. }
  761. return true;
  762. }
  763. template <typename T>
  764. std::vector<Proof> PrsonaBase::generate_proof_of_reordering(
  765. const std::vector<std::vector<Scalar>>& permutations,
  766. const std::vector<std::vector<Scalar>>& permutationSeeds,
  767. const std::vector<std::vector<Scalar>>& productSeeds,
  768. const std::vector<T>& oldValues,
  769. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  770. const std::vector<std::vector<T>>& productCommits,
  771. const T& otherG,
  772. const T& otherH) const
  773. {
  774. if (LAMBDA > 0)
  775. return generate_batched_proof_of_reordering<T>(permutations, permutationSeeds, productSeeds, oldValues, permutationCommits, productCommits, otherG, otherH);
  776. else
  777. return generate_unbatched_proof_of_reordering<T>(permutations, permutationSeeds, productSeeds, oldValues, permutationCommits, productCommits, otherG, otherH);
  778. }
  779. template <typename T>
  780. bool PrsonaBase::verify_proof_of_reordering(
  781. const std::vector<Proof>& pi,
  782. const std::vector<T>& oldValues,
  783. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  784. const std::vector<std::vector<T>>& productCommits,
  785. const T& otherG,
  786. const T& otherH) const
  787. {
  788. if (LAMBDA > 0)
  789. return verify_batched_proof_of_reordering<T>(pi, oldValues, permutationCommits, productCommits, otherG, otherH);
  790. else
  791. return verify_unbatched_proof_of_reordering<T>(pi, oldValues, permutationCommits, productCommits, otherG, otherH);
  792. }
  793. template <typename T>
  794. std::vector<Proof> PrsonaBase::generate_unbatched_proof_of_reordering(
  795. const std::vector<std::vector<Scalar>>& permutations,
  796. const std::vector<std::vector<Scalar>>& permutationSeeds,
  797. const std::vector<std::vector<Scalar>>& productSeeds,
  798. const std::vector<T>& oldValues,
  799. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  800. const std::vector<std::vector<T>>& productCommits,
  801. const T& otherG,
  802. const T& otherH) const
  803. {
  804. std::vector<Proof> retval;
  805. if (!SERVER_IS_MALICIOUS)
  806. {
  807. retval.push_back(Proof("PROOF"));
  808. return retval;
  809. }
  810. Proof first;
  811. retval.push_back(first);
  812. Twistpoint g = EL_GAMAL_GENERATOR;
  813. Twistpoint h = elGamalBlindGenerator;
  814. std::stringstream oracleInput;
  815. oracleInput << g << h << otherG << otherH;
  816. for (size_t i = 0; i < oldValues.size(); i++)
  817. oracleInput << oldValues[i];
  818. for (size_t i = 0; i < permutationCommits.size(); i++)
  819. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  820. oracleInput << permutationCommits[i][j];
  821. for (size_t i = 0; i < productCommits.size(); i++)
  822. for (size_t j = 0; j < productCommits[i].size(); j++)
  823. oracleInput << productCommits[i][j];
  824. std::vector<std::vector<Scalar>> a;
  825. std::vector<std::vector<Scalar>> t1;
  826. std::vector<std::vector<Scalar>> t2;
  827. for (size_t i = 0; i < permutationCommits.size(); i++)
  828. {
  829. std::vector<Scalar> curraRow;
  830. std::vector<Scalar> currt1Row;
  831. std::vector<Scalar> currt2Row;
  832. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  833. {
  834. Proof currProof;
  835. Scalar curra;
  836. Scalar currt1;
  837. Scalar currt2;
  838. Twistpoint U1;
  839. T U2;
  840. curra.set_random();
  841. currt1.set_random();
  842. currt2.set_random();
  843. U1 = g * curra + h * currt1;
  844. U2 = oldValues[j] * curra + otherH * currt2;
  845. oracleInput << U1 << U2;
  846. curraRow.push_back(curra);
  847. currt1Row.push_back(currt1);
  848. currt2Row.push_back(currt2);
  849. retval.push_back(currProof);
  850. }
  851. a.push_back(curraRow);
  852. t1.push_back(currt1Row);
  853. t2.push_back(currt2Row);
  854. }
  855. Scalar x = oracle(oracleInput.str());
  856. retval[0].challengeParts.push_back(x);
  857. for (size_t i = 0; i < permutationCommits.size(); i++)
  858. {
  859. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  860. {
  861. size_t piIndex = i * permutationCommits.size() + j + 1;
  862. Scalar f = permutations[j][i] * x + a[i][j];
  863. Scalar z1 = permutationSeeds[j][i] * x + t1[i][j];
  864. Scalar z2 = productSeeds[i][j] * x + t2[i][j];
  865. retval[piIndex].responseParts.push_back(f);
  866. retval[piIndex].responseParts.push_back(z1);
  867. retval[piIndex].responseParts.push_back(z2);
  868. }
  869. }
  870. return retval;
  871. }
  872. template <typename T>
  873. bool PrsonaBase::verify_unbatched_proof_of_reordering(
  874. const std::vector<Proof>& pi,
  875. const std::vector<T>& oldValues,
  876. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  877. const std::vector<std::vector<T>>& productCommits,
  878. const T& otherG,
  879. const T& otherH) const
  880. {
  881. if (pi.empty())
  882. return false;
  883. if (!SERVER_IS_MALICIOUS)
  884. return true;
  885. Twistpoint g = EL_GAMAL_GENERATOR;
  886. Twistpoint h = elGamalBlindGenerator;
  887. std::stringstream oracleInput;
  888. oracleInput << g << h << otherG << otherH;
  889. for (size_t i = 0; i < oldValues.size(); i++)
  890. oracleInput << oldValues[i];
  891. for (size_t i = 0; i < permutationCommits.size(); i++)
  892. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  893. oracleInput << permutationCommits[i][j];
  894. for (size_t i = 0; i < productCommits.size(); i++)
  895. for (size_t j = 0; j < productCommits[i].size(); j++)
  896. oracleInput << productCommits[i][j];
  897. Scalar x = pi[0].challengeParts[0];
  898. for (size_t i = 0; i < permutationCommits.size(); i++)
  899. {
  900. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  901. {
  902. size_t piIndex = i * permutationCommits.size() + j + 1;
  903. Twistpoint U1;
  904. T U2;
  905. Scalar f = pi[piIndex].responseParts[0];
  906. Scalar z1 = pi[piIndex].responseParts[1];
  907. Scalar z2 = pi[piIndex].responseParts[2];
  908. U1 = g * f +
  909. h * z1 -
  910. permutationCommits[j][i] * x;
  911. U2 = oldValues[j] * f +
  912. otherH * z2 -
  913. productCommits[i][j] * x;
  914. oracleInput << U1 << U2;
  915. }
  916. }
  917. if (x != oracle(oracleInput.str()))
  918. {
  919. std::cerr << "Reordered things not generated by permutation matrix." << std::endl;
  920. return false;
  921. }
  922. return true;
  923. }
  924. template <typename T>
  925. std::vector<Proof> PrsonaBase::generate_batched_proof_of_reordering(
  926. const std::vector<std::vector<Scalar>>& permutations,
  927. const std::vector<std::vector<Scalar>>& permutationSeeds,
  928. const std::vector<std::vector<Scalar>>& productSeeds,
  929. const std::vector<T>& oldValues,
  930. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  931. const std::vector<std::vector<T>>& productCommits,
  932. const T& otherG,
  933. const T& otherH) const
  934. {
  935. std::vector<Proof> retval;
  936. if (!SERVER_IS_MALICIOUS)
  937. {
  938. retval.push_back(Proof("PROOF"));
  939. return retval;
  940. }
  941. Proof first;
  942. retval.push_back(first);
  943. Twistpoint g = EL_GAMAL_GENERATOR;
  944. Twistpoint h = elGamalBlindGenerator;
  945. std::stringstream oracleInput;
  946. oracleInput << g << h << otherG << otherH;
  947. for (size_t i = 0; i < oldValues.size(); i++)
  948. oracleInput << oldValues[i];
  949. for (size_t i = 0; i < permutationCommits.size(); i++)
  950. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  951. oracleInput << permutationCommits[i][j];
  952. for (size_t i = 0; i < productCommits.size(); i++)
  953. for (size_t j = 0; j < productCommits[i].size(); j++)
  954. oracleInput << productCommits[i][j];
  955. std::vector<Scalar> a;
  956. std::vector<Scalar> t1;
  957. std::vector<Scalar> t2;
  958. for (size_t i = 0; i < permutationCommits.size(); i++)
  959. {
  960. Proof currProof;
  961. Scalar curra;
  962. Scalar currt1;
  963. Scalar currt2;
  964. Twistpoint U1;
  965. T U2;
  966. curra.set_random();
  967. currt1.set_random();
  968. currt2.set_random();
  969. U1 = g * curra + h * currt1;
  970. U2 = oldValues[i] * curra + otherH * currt2;
  971. oracleInput << U1 << U2;
  972. a.push_back(curra);
  973. t1.push_back(currt1);
  974. t2.push_back(currt2);
  975. retval.push_back(currProof);
  976. }
  977. Scalar x = oracle(oracleInput.str());
  978. retval[0].challengeParts.push_back(x);
  979. for (size_t i = 0; i < permutationCommits.size(); i++)
  980. {
  981. size_t piIndex = i + 1;
  982. Scalar f = a[i];
  983. Scalar z1 = t1[i];
  984. Scalar z2 = t2[i];
  985. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  986. {
  987. std::stringstream currOracle;
  988. currOracle << permutationCommits[i][j] << productCommits[j][i];
  989. Scalar currx = oracle(currOracle.str(), LAMBDA);
  990. f = f + permutations[i][j] * currx;
  991. z1 = z1 + permutationSeeds[i][j] * currx;
  992. z2 = z2 + productSeeds[j][i] * currx;
  993. }
  994. retval[piIndex].responseParts.push_back(f);
  995. retval[piIndex].responseParts.push_back(z1);
  996. retval[piIndex].responseParts.push_back(z2);
  997. }
  998. return retval;
  999. }
  1000. template <typename T>
  1001. bool PrsonaBase::verify_batched_proof_of_reordering(
  1002. const std::vector<Proof>& pi,
  1003. const std::vector<T>& oldValues,
  1004. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1005. const std::vector<std::vector<T>>& productCommits,
  1006. const T& otherG,
  1007. const T& otherH) const
  1008. {
  1009. if (pi.empty())
  1010. return false;
  1011. if (!SERVER_IS_MALICIOUS)
  1012. return true;
  1013. Twistpoint g = EL_GAMAL_GENERATOR;
  1014. Twistpoint h = elGamalBlindGenerator;
  1015. std::stringstream oracleInput;
  1016. oracleInput << g << h << otherG << otherH;
  1017. for (size_t i = 0; i < oldValues.size(); i++)
  1018. oracleInput << oldValues[i];
  1019. for (size_t i = 0; i < permutationCommits.size(); i++)
  1020. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1021. oracleInput << permutationCommits[i][j];
  1022. for (size_t i = 0; i < productCommits.size(); i++)
  1023. for (size_t j = 0; j < productCommits[i].size(); j++)
  1024. oracleInput << productCommits[i][j];
  1025. Scalar x = pi[0].challengeParts[0];
  1026. for (size_t i = 0; i < permutationCommits.size(); i++)
  1027. {
  1028. size_t piIndex = i + 1;
  1029. Scalar f = pi[piIndex].responseParts[0];
  1030. Scalar z1 = pi[piIndex].responseParts[1];
  1031. Scalar z2 = pi[piIndex].responseParts[2];
  1032. Twistpoint U1 = g * f + h * z1;
  1033. T U2 = oldValues[i] * f + otherH * z2;
  1034. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1035. {
  1036. std::stringstream currOracle;
  1037. currOracle << permutationCommits[i][j] << productCommits[j][i];
  1038. Scalar currx = oracle(currOracle.str(), LAMBDA);
  1039. U1 = U1 - permutationCommits[i][j] * currx;
  1040. U2 = U2 - productCommits[j][i] * currx;
  1041. }
  1042. oracleInput << U1 << U2;
  1043. }
  1044. if (x != oracle(oracleInput.str()))
  1045. {
  1046. std::cerr << "Reordered things not generated by permutation matrix." << std::endl;
  1047. return false;
  1048. }
  1049. return true;
  1050. }
  1051. std::vector<Proof> PrsonaBase::generate_proof_of_reordering_plus_power(
  1052. const std::vector<std::vector<Scalar>>& permutations,
  1053. const Scalar& power,
  1054. const std::vector<std::vector<Scalar>>& permutationSeeds,
  1055. const std::vector<std::vector<Scalar>>& productSeeds,
  1056. const std::vector<Twistpoint>& oldValues,
  1057. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1058. const std::vector<std::vector<Twistpoint>>& productCommits,
  1059. const std::vector<std::vector<Twistpoint>>& seedCommits) const
  1060. {
  1061. if (LAMBDA > 0)
  1062. return generate_batched_proof_of_reordering_plus_power(permutations, power, permutationSeeds, productSeeds, oldValues, permutationCommits, productCommits, seedCommits);
  1063. else
  1064. return generate_unbatched_proof_of_reordering_plus_power(permutations, power, permutationSeeds, productSeeds, oldValues, permutationCommits, productCommits, seedCommits);
  1065. }
  1066. bool PrsonaBase::verify_proof_of_reordering_plus_power(
  1067. const std::vector<Proof>& pi,
  1068. const std::vector<Twistpoint>& oldValues,
  1069. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1070. const std::vector<std::vector<Twistpoint>>& productCommits,
  1071. const std::vector<std::vector<Twistpoint>>& seedCommits) const
  1072. {
  1073. if (LAMBDA > 0)
  1074. return verify_batched_proof_of_reordering_plus_power(pi, oldValues, permutationCommits, productCommits, seedCommits);
  1075. else
  1076. return verify_unbatched_proof_of_reordering_plus_power(pi, oldValues, permutationCommits, productCommits, seedCommits);
  1077. }
  1078. std::vector<Proof> PrsonaBase::generate_unbatched_proof_of_reordering_plus_power(
  1079. const std::vector<std::vector<Scalar>>& permutations,
  1080. const Scalar& power,
  1081. const std::vector<std::vector<Scalar>>& permutationSeeds,
  1082. const std::vector<std::vector<Scalar>>& productSeeds,
  1083. const std::vector<Twistpoint>& oldValues,
  1084. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1085. const std::vector<std::vector<Twistpoint>>& productCommits,
  1086. const std::vector<std::vector<Twistpoint>>& seedCommits) const
  1087. {
  1088. std::vector<Proof> retval;
  1089. if (!SERVER_IS_MALICIOUS)
  1090. {
  1091. retval.push_back(Proof("PROOF"));
  1092. return retval;
  1093. }
  1094. Proof first;
  1095. retval.push_back(first);
  1096. Twistpoint g = EL_GAMAL_GENERATOR;
  1097. Twistpoint h = elGamalBlindGenerator;
  1098. std::stringstream oracleInput;
  1099. oracleInput << g << h;
  1100. for (size_t i = 0; i < oldValues.size(); i++)
  1101. oracleInput << oldValues[i];
  1102. for (size_t i = 0; i < permutationCommits.size(); i++)
  1103. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1104. oracleInput << permutationCommits[i][j];
  1105. for (size_t i = 0; i < productCommits.size(); i++)
  1106. for (size_t j = 0; j < productCommits[i].size(); j++)
  1107. oracleInput << productCommits[i][j];
  1108. for (size_t i = 0; i < seedCommits.size(); i++)
  1109. for (size_t j = 0; j < seedCommits[i].size(); j++)
  1110. oracleInput << seedCommits[i][j];
  1111. Scalar b1;
  1112. b1.set_random();
  1113. std::vector<std::vector<Scalar>> b2;
  1114. std::vector<std::vector<Scalar>> t1;
  1115. std::vector<std::vector<Scalar>> t2;
  1116. for (size_t i = 0; i < permutationCommits.size(); i++)
  1117. {
  1118. std::vector<Scalar> currb2Row;
  1119. std::vector<Scalar> currt1Row;
  1120. std::vector<Scalar> currt2Row;
  1121. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1122. {
  1123. Proof currProof;
  1124. Scalar currb2;
  1125. Scalar currt1;
  1126. Scalar currt2;
  1127. Twistpoint U1, U2, U3, U4;
  1128. currb2.set_random();
  1129. currt1.set_random();
  1130. currt2.set_random();
  1131. U1 = g * currb2 + h * currt1;
  1132. U2 = oldValues[j] *
  1133. (b1 * permutations[j][i] + currb2 * power);
  1134. U3 = oldValues[j] * b1 * currb2 + h * currt2;
  1135. U4 = g * currt2;
  1136. currProof.curvepointUniversals.push_back(U2);
  1137. oracleInput << U1 << U2 << U3 << U4;
  1138. currb2Row.push_back(currb2);
  1139. currt1Row.push_back(currt1);
  1140. currt2Row.push_back(currt2);
  1141. retval.push_back(currProof);
  1142. }
  1143. b2.push_back(currb2Row);
  1144. t1.push_back(currt1Row);
  1145. t2.push_back(currt2Row);
  1146. }
  1147. Scalar x = oracle(oracleInput.str());
  1148. retval[0].challengeParts.push_back(x);
  1149. Scalar f1 = power * x + b1;
  1150. retval[0].responseParts.push_back(f1);
  1151. for (size_t i = 0; i < permutationCommits.size(); i++)
  1152. {
  1153. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1154. {
  1155. size_t piIndex = i * permutationCommits.size() + j + 1;
  1156. Scalar f2 = permutations[j][i] * x + b2[i][j];
  1157. Scalar z1 = permutationSeeds[j][i] * x + t1[i][j];
  1158. Scalar z2 = productSeeds[i][j] * x * x + t2[i][j];
  1159. retval[piIndex].responseParts.push_back(f2);
  1160. retval[piIndex].responseParts.push_back(z1);
  1161. retval[piIndex].responseParts.push_back(z2);
  1162. }
  1163. }
  1164. return retval;
  1165. }
  1166. bool PrsonaBase::verify_unbatched_proof_of_reordering_plus_power(
  1167. const std::vector<Proof>& pi,
  1168. const std::vector<Twistpoint>& oldValues,
  1169. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1170. const std::vector<std::vector<Twistpoint>>& productCommits,
  1171. const std::vector<std::vector<Twistpoint>>& seedCommits) const
  1172. {
  1173. if (pi.empty())
  1174. return false;
  1175. if (!SERVER_IS_MALICIOUS)
  1176. return true;
  1177. Twistpoint g = EL_GAMAL_GENERATOR;
  1178. Twistpoint h = elGamalBlindGenerator;
  1179. std::stringstream oracleInput;
  1180. oracleInput << g << h;
  1181. for (size_t i = 0; i < oldValues.size(); i++)
  1182. oracleInput << oldValues[i];
  1183. for (size_t i = 0; i < permutationCommits.size(); i++)
  1184. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1185. oracleInput << permutationCommits[i][j];
  1186. for (size_t i = 0; i < productCommits.size(); i++)
  1187. for (size_t j = 0; j < productCommits[i].size(); j++)
  1188. oracleInput << productCommits[i][j];
  1189. for (size_t i = 0; i < seedCommits.size(); i++)
  1190. for (size_t j = 0; j < seedCommits[i].size(); j++)
  1191. oracleInput << seedCommits[i][j];
  1192. Scalar x = pi[0].challengeParts[0];
  1193. Scalar f1 = pi[0].responseParts[0];
  1194. for (size_t i = 0; i < permutationCommits.size(); i++)
  1195. {
  1196. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1197. {
  1198. size_t piIndex = i * permutationCommits.size() + j + 1;
  1199. Twistpoint U1, U2, U3, U4;
  1200. U2 = pi[piIndex].curvepointUniversals[0];
  1201. Scalar f2 = pi[piIndex].responseParts[0];
  1202. Scalar z1 = pi[piIndex].responseParts[1];
  1203. Scalar z2 = pi[piIndex].responseParts[2];
  1204. U1 = g * f2 + h * z1 - permutationCommits[j][i] * x;
  1205. U3 = oldValues[j] * f1 * f2 +
  1206. h * z2 -
  1207. productCommits[i][j] * x * x -
  1208. U2 * x;
  1209. U4 = g * z2 - seedCommits[i][j] * x * x;
  1210. oracleInput << U1 << U2 << U3 << U4;
  1211. }
  1212. }
  1213. if (x != oracle(oracleInput.str()))
  1214. {
  1215. std::cerr << "Reordered + power things not generated by permutation matrix." << std::endl;
  1216. return false;
  1217. }
  1218. for (size_t i = 0; i < seedCommits.size(); i++)
  1219. {
  1220. Twistpoint sum = seedCommits[i][0];
  1221. for (size_t j = 1; j < seedCommits[i].size(); j++)
  1222. sum = sum + seedCommits[i][j];
  1223. if (sum != Twistpoint())
  1224. {
  1225. std::cerr << "seed commits did not sum to 0, aborting." << std::endl;
  1226. return false;
  1227. }
  1228. }
  1229. return true;
  1230. }
  1231. std::vector<Proof> PrsonaBase::generate_batched_proof_of_reordering_plus_power(
  1232. const std::vector<std::vector<Scalar>>& permutations,
  1233. const Scalar& power,
  1234. const std::vector<std::vector<Scalar>>& permutationSeeds,
  1235. const std::vector<std::vector<Scalar>>& productSeeds,
  1236. const std::vector<Twistpoint>& oldValues,
  1237. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1238. const std::vector<std::vector<Twistpoint>>& productCommits,
  1239. const std::vector<std::vector<Twistpoint>>& seedCommits) const
  1240. {
  1241. std::vector<Proof> retval;
  1242. if (!SERVER_IS_MALICIOUS)
  1243. {
  1244. retval.push_back(Proof("PROOF"));
  1245. return retval;
  1246. }
  1247. Proof first;
  1248. retval.push_back(first);
  1249. Twistpoint g = EL_GAMAL_GENERATOR;
  1250. Twistpoint h = elGamalBlindGenerator;
  1251. std::stringstream oracleInput;
  1252. oracleInput << g << h;
  1253. for (size_t i = 0; i < oldValues.size(); i++)
  1254. oracleInput << oldValues[i];
  1255. for (size_t i = 0; i < permutationCommits.size(); i++)
  1256. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1257. oracleInput << permutationCommits[i][j];
  1258. for (size_t i = 0; i < productCommits.size(); i++)
  1259. for (size_t j = 0; j < productCommits[i].size(); j++)
  1260. oracleInput << productCommits[i][j];
  1261. for (size_t i = 0; i < seedCommits.size(); i++)
  1262. for (size_t j = 0; j < seedCommits[i].size(); j++)
  1263. oracleInput << seedCommits[i][j];
  1264. std::vector<Scalar> b1;
  1265. std::vector<Scalar> b2;
  1266. std::vector<std::vector<Scalar>> b3;
  1267. std::vector<Scalar> t1;
  1268. std::vector<Scalar> t2;
  1269. std::vector<Scalar> t3;
  1270. for (size_t i = 0; i < permutationCommits.size(); i++)
  1271. {
  1272. Proof currProof;
  1273. Scalar currb1;
  1274. Scalar currb2;
  1275. std::vector<Scalar> currb3Row;
  1276. Scalar currt1;
  1277. Scalar currt2;
  1278. Scalar currt3;
  1279. Twistpoint U1, U3, U4;
  1280. std::vector<Twistpoint> U2;
  1281. currb1.set_random();
  1282. currb2.set_random();
  1283. currt1.set_random();
  1284. currt2.set_random();
  1285. currt3.set_random();
  1286. U1 = g * currb2 + h * currt1;
  1287. U3 = oldValues[i] * (currb1 * currb2 + currt3) + h * currt2;
  1288. U4 = g * currt2;
  1289. oracleInput << U1;
  1290. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1291. {
  1292. Twistpoint U2;
  1293. Scalar currb3;
  1294. currb3.set_random();
  1295. Scalar U2mult = (currb1 * permutations[i][j] + currb2 * power + currb3);
  1296. for (size_t k = 0; k < permutationCommits[i].size(); k++)
  1297. {
  1298. if (k == j)
  1299. continue;
  1300. std::stringstream currOracle;
  1301. currOracle << permutationCommits[i][k] << productCommits[k][i] << seedCommits[k][i];
  1302. Scalar currx = oracle(currOracle.str(), LAMBDA);
  1303. U2mult = U2mult + power * permutations[i][j] * currx;
  1304. }
  1305. U2 = oldValues[i] * U2mult;
  1306. currProof.curvepointUniversals.push_back(U2);
  1307. oracleInput << U2;
  1308. currb3Row.push_back(currb3);
  1309. }
  1310. oracleInput << U3 << U4;
  1311. b1.push_back(currb1);
  1312. b2.push_back(currb2);
  1313. b3.push_back(currb3Row);
  1314. t1.push_back(currt1);
  1315. t2.push_back(currt2);
  1316. t3.push_back(currt3);
  1317. retval.push_back(currProof);
  1318. }
  1319. Scalar x = oracle(oracleInput.str());
  1320. retval[0].challengeParts.push_back(x);
  1321. for (size_t i = 0; i < permutationCommits.size(); i++)
  1322. {
  1323. size_t piIndex = i + 1;
  1324. Scalar f1 = b1[i];
  1325. Scalar f2 = b2[i];
  1326. Scalar z1 = t1[i];
  1327. Scalar z2 = t2[i];
  1328. Scalar z3 = t3[i];
  1329. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1330. {
  1331. std::stringstream currOracle;
  1332. currOracle << permutationCommits[i][j] << productCommits[j][i] << seedCommits[j][i];
  1333. Scalar currx = oracle(currOracle.str(), LAMBDA);
  1334. f1 = f1 + power * currx;
  1335. f2 = f2 + permutations[i][j] * currx;
  1336. z1 = z1 + permutationSeeds[i][j] * currx;
  1337. z2 = z2 + productSeeds[j][i] * currx * currx;
  1338. z3 = z3 + b3[i][j] * currx;
  1339. }
  1340. retval[piIndex].responseParts.push_back(f1);
  1341. retval[piIndex].responseParts.push_back(f2);
  1342. retval[piIndex].responseParts.push_back(z1);
  1343. retval[piIndex].responseParts.push_back(z2);
  1344. retval[piIndex].responseParts.push_back(z3);
  1345. }
  1346. return retval;
  1347. }
  1348. bool PrsonaBase::verify_batched_proof_of_reordering_plus_power(
  1349. const std::vector<Proof>& pi,
  1350. const std::vector<Twistpoint>& oldValues,
  1351. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1352. const std::vector<std::vector<Twistpoint>>& productCommits,
  1353. const std::vector<std::vector<Twistpoint>>& seedCommits) const
  1354. {
  1355. if (pi.empty())
  1356. return false;
  1357. if (!SERVER_IS_MALICIOUS)
  1358. return true;
  1359. Twistpoint g = EL_GAMAL_GENERATOR;
  1360. Twistpoint h = elGamalBlindGenerator;
  1361. std::stringstream oracleInput;
  1362. oracleInput << g << h;
  1363. for (size_t i = 0; i < oldValues.size(); i++)
  1364. oracleInput << oldValues[i];
  1365. for (size_t i = 0; i < permutationCommits.size(); i++)
  1366. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1367. oracleInput << permutationCommits[i][j];
  1368. for (size_t i = 0; i < productCommits.size(); i++)
  1369. for (size_t j = 0; j < productCommits[i].size(); j++)
  1370. oracleInput << productCommits[i][j];
  1371. for (size_t i = 0; i < seedCommits.size(); i++)
  1372. for (size_t j = 0; j < seedCommits[i].size(); j++)
  1373. oracleInput << seedCommits[i][j];
  1374. Scalar x = pi[0].challengeParts[0];
  1375. for (size_t i = 0; i < permutationCommits.size(); i++)
  1376. {
  1377. size_t piIndex = i + 1;
  1378. Scalar f1 = pi[piIndex].responseParts[0];
  1379. Scalar f2 = pi[piIndex].responseParts[1];
  1380. Scalar z1 = pi[piIndex].responseParts[2];
  1381. Scalar z2 = pi[piIndex].responseParts[3];
  1382. Scalar z3 = pi[piIndex].responseParts[4];
  1383. Twistpoint U1 = g * f2 + h * z1;
  1384. std::vector<Twistpoint> U2 = pi[piIndex].curvepointUniversals;
  1385. Twistpoint U3 = oldValues[i] * (f1 * f2 + z3) + h * z2;
  1386. Twistpoint U4 = g * z2;
  1387. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1388. {
  1389. std::stringstream currOracle;
  1390. currOracle << permutationCommits[i][j] << productCommits[j][i] << seedCommits[j][i];
  1391. Scalar currx = oracle(currOracle.str(), LAMBDA);
  1392. U1 = U1 - permutationCommits[i][j] * currx;
  1393. U3 = U3 -
  1394. productCommits[j][i] * currx * currx -
  1395. U2[j] * currx;
  1396. U4 = U4 - seedCommits[j][i] * currx * currx;
  1397. }
  1398. oracleInput << U1;
  1399. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1400. oracleInput << U2[j];
  1401. oracleInput << U3 << U4;
  1402. }
  1403. if (x != oracle(oracleInput.str()))
  1404. {
  1405. std::cerr << "Reordered + power things not generated by permutation matrix." << std::endl;
  1406. return false;
  1407. }
  1408. for (size_t i = 0; i < seedCommits.size(); i++)
  1409. {
  1410. Twistpoint sum = seedCommits[i][0];
  1411. for (size_t j = 1; j < seedCommits[i].size(); j++)
  1412. sum = sum + seedCommits[i][j];
  1413. if (sum != Twistpoint())
  1414. {
  1415. std::cerr << "seed commits did not sum to 0, aborting." << std::endl;
  1416. return false;
  1417. }
  1418. }
  1419. return true;
  1420. }
  1421. std::vector<Proof> PrsonaBase::generate_user_tally_proofs(
  1422. const std::vector<std::vector<Scalar>>& permutations,
  1423. const Scalar& power,
  1424. const Twistpoint& nextGenerator,
  1425. const std::vector<std::vector<Scalar>>& permutationSeeds,
  1426. const std::vector<std::vector<Scalar>>& userTallySeeds,
  1427. const std::vector<Twistpoint>& currPseudonyms,
  1428. const std::vector<Twistpoint>& userTallyMasks,
  1429. const std::vector<Twistpoint>& userTallyMessages,
  1430. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1431. const std::vector<std::vector<Twistpoint>>& userTallyMaskCommits,
  1432. const std::vector<std::vector<Twistpoint>>& userTallyMessageCommits,
  1433. const std::vector<std::vector<Twistpoint>>& userTallySeedCommits) const
  1434. {
  1435. if (LAMBDA > 0)
  1436. return generate_batched_user_tally_proofs(permutations, power, nextGenerator, permutationSeeds, userTallySeeds, currPseudonyms, userTallyMasks, userTallyMessages, permutationCommits, userTallyMaskCommits, userTallyMessageCommits, userTallySeedCommits);
  1437. else
  1438. return generate_unbatched_user_tally_proofs(permutations, power, nextGenerator, permutationSeeds, userTallySeeds, currPseudonyms, userTallyMasks, userTallyMessages, permutationCommits, userTallyMaskCommits, userTallyMessageCommits, userTallySeedCommits);
  1439. }
  1440. bool PrsonaBase::verify_user_tally_proofs(
  1441. const std::vector<Proof>& pi,
  1442. const Twistpoint& nextGenerator,
  1443. const std::vector<Twistpoint>& currPseudonyms,
  1444. const std::vector<Twistpoint>& userTallyMasks,
  1445. const std::vector<Twistpoint>& userTallyMessages,
  1446. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1447. const std::vector<std::vector<Twistpoint>>& userTallyMaskCommits,
  1448. const std::vector<std::vector<Twistpoint>>& userTallyMessageCommits,
  1449. const std::vector<std::vector<Twistpoint>>& userTallySeedCommits) const
  1450. {
  1451. if (LAMBDA > 0)
  1452. return verify_batched_user_tally_proofs(pi, nextGenerator, currPseudonyms, userTallyMasks, userTallyMessages, permutationCommits, userTallyMaskCommits, userTallyMessageCommits, userTallySeedCommits);
  1453. else
  1454. return verify_unbatched_user_tally_proofs(pi, nextGenerator, currPseudonyms, userTallyMasks, userTallyMessages, permutationCommits, userTallyMaskCommits, userTallyMessageCommits, userTallySeedCommits);
  1455. }
  1456. std::vector<Proof> PrsonaBase::generate_unbatched_user_tally_proofs(
  1457. const std::vector<std::vector<Scalar>>& permutations,
  1458. const Scalar& power,
  1459. const Twistpoint& nextGenerator,
  1460. const std::vector<std::vector<Scalar>>& permutationSeeds,
  1461. const std::vector<std::vector<Scalar>>& userTallySeeds,
  1462. const std::vector<Twistpoint>& currPseudonyms,
  1463. const std::vector<Twistpoint>& userTallyMasks,
  1464. const std::vector<Twistpoint>& userTallyMessages,
  1465. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1466. const std::vector<std::vector<Twistpoint>>& userTallyMaskCommits,
  1467. const std::vector<std::vector<Twistpoint>>& userTallyMessageCommits,
  1468. const std::vector<std::vector<Twistpoint>>& userTallySeedCommits) const
  1469. {
  1470. std::vector<Proof> retval;
  1471. if (!SERVER_IS_MALICIOUS)
  1472. {
  1473. retval.push_back(Proof("PROOF"));
  1474. return retval;
  1475. }
  1476. Proof first;
  1477. retval.push_back(first);
  1478. Twistpoint g = EL_GAMAL_GENERATOR;
  1479. Twistpoint h = elGamalBlindGenerator;
  1480. std::stringstream oracleInput;
  1481. oracleInput << g << h << nextGenerator;
  1482. for (size_t i = 0; i < currPseudonyms.size(); i++)
  1483. oracleInput << currPseudonyms[i];
  1484. for (size_t i = 0; i < userTallyMasks.size(); i++)
  1485. oracleInput << userTallyMasks[i];
  1486. for (size_t i = 0; i < userTallyMessages.size(); i++)
  1487. oracleInput << userTallyMessages[i];
  1488. for (size_t i = 0; i < permutationCommits.size(); i++)
  1489. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1490. oracleInput << permutationCommits[i][j];
  1491. for (size_t i = 0; i < userTallyMaskCommits.size(); i++)
  1492. for (size_t j = 0; j < userTallyMaskCommits[i].size(); j++)
  1493. oracleInput << userTallyMaskCommits[i][j];
  1494. for (size_t i = 0; i < userTallyMessageCommits.size(); i++)
  1495. for (size_t j = 0; j < userTallyMessageCommits[i].size(); j++)
  1496. oracleInput << userTallyMessageCommits[i][j];
  1497. for (size_t i = 0; i < userTallySeedCommits.size(); i++)
  1498. for (size_t j = 0; j < userTallySeedCommits[i].size(); j++)
  1499. oracleInput << userTallySeedCommits[i][j];
  1500. Scalar b1;
  1501. b1.set_random();
  1502. std::vector<std::vector<Scalar>> b2;
  1503. std::vector<std::vector<Scalar>> t1;
  1504. std::vector<std::vector<Scalar>> t2;
  1505. for (size_t i = 0; i < permutationCommits.size(); i++)
  1506. {
  1507. std::vector<Scalar> currb2Row;
  1508. std::vector<Scalar> currt1Row;
  1509. std::vector<Scalar> currt2Row;
  1510. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1511. {
  1512. Proof currProof;
  1513. Scalar currb2;
  1514. Scalar currt1;
  1515. Scalar currt2;
  1516. Twistpoint U1, U2, U3, U4, U5, U6, U7;
  1517. currb2.set_random();
  1518. currt1.set_random();
  1519. currt2.set_random();
  1520. U1 = g * currb2 + h * currt1;
  1521. U2 = userTallyMasks[j] * (b1 * permutations[j][i] + currb2 * power) +
  1522. currPseudonyms[j] * (permutations[j][i] * b1 * userTallySeeds[i][j] + power * currb2 * userTallySeeds[i][j] + permutations[j][i] * power * currt2) +
  1523. h * currt2;
  1524. U3 = userTallyMasks[j] * (b1 * currb2) +
  1525. currPseudonyms[j] * (b1 * currb2 * userTallySeeds[i][j] + permutations[j][i] * b1 * currt2 + power * currb2 * currt2);
  1526. U4 = currPseudonyms[j] * (b1 * currb2 * currt2);
  1527. U5 = userTallyMessages[j] * currb2 +
  1528. nextGenerator * (permutations[j][i] * currt2 + userTallySeeds[i][j] * currb2) +
  1529. h * currt2;
  1530. U6 = nextGenerator * (currb2 * currt2);
  1531. U7 = g * currt2;
  1532. currProof.curvepointUniversals.push_back(U2);
  1533. currProof.curvepointUniversals.push_back(U3);
  1534. currProof.curvepointUniversals.push_back(U5);
  1535. oracleInput << U1 << U2 << U3 << U4 << U5 << U6 << U7;
  1536. currb2Row.push_back(currb2);
  1537. currt1Row.push_back(currt1);
  1538. currt2Row.push_back(currt2);
  1539. retval.push_back(currProof);
  1540. }
  1541. b2.push_back(currb2Row);
  1542. t1.push_back(currt1Row);
  1543. t2.push_back(currt2Row);
  1544. }
  1545. Scalar x = oracle(oracleInput.str());
  1546. retval[0].challengeParts.push_back(x);
  1547. Scalar f1 = power * x + b1;
  1548. retval[0].responseParts.push_back(f1);
  1549. for (size_t i = 0; i < permutationCommits.size(); i++)
  1550. {
  1551. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1552. {
  1553. size_t piIndex = i * permutationCommits.size() + j + 1;
  1554. Scalar f2 = permutations[j][i] * x + b2[i][j];
  1555. Scalar z1 = permutationSeeds[j][i] * x + t1[i][j];
  1556. Scalar z2 = userTallySeeds[i][j] * x + t2[i][j];
  1557. retval[piIndex].responseParts.push_back(f2);
  1558. retval[piIndex].responseParts.push_back(z1);
  1559. retval[piIndex].responseParts.push_back(z2);
  1560. }
  1561. }
  1562. return retval;
  1563. }
  1564. bool PrsonaBase::verify_unbatched_user_tally_proofs(
  1565. const std::vector<Proof>& pi,
  1566. const Twistpoint& nextGenerator,
  1567. const std::vector<Twistpoint>& currPseudonyms,
  1568. const std::vector<Twistpoint>& userTallyMasks,
  1569. const std::vector<Twistpoint>& userTallyMessages,
  1570. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1571. const std::vector<std::vector<Twistpoint>>& userTallyMaskCommits,
  1572. const std::vector<std::vector<Twistpoint>>& userTallyMessageCommits,
  1573. const std::vector<std::vector<Twistpoint>>& userTallySeedCommits) const
  1574. {
  1575. if (pi.empty())
  1576. return false;
  1577. if (!SERVER_IS_MALICIOUS)
  1578. return true;
  1579. Twistpoint g = EL_GAMAL_GENERATOR;
  1580. Twistpoint h = elGamalBlindGenerator;
  1581. std::stringstream oracleInput;
  1582. oracleInput << g << h << nextGenerator;
  1583. for (size_t i = 0; i < currPseudonyms.size(); i++)
  1584. oracleInput << currPseudonyms[i];
  1585. for (size_t i = 0; i < userTallyMasks.size(); i++)
  1586. oracleInput << userTallyMasks[i];
  1587. for (size_t i = 0; i < userTallyMessages.size(); i++)
  1588. oracleInput << userTallyMessages[i];
  1589. for (size_t i = 0; i < permutationCommits.size(); i++)
  1590. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1591. oracleInput << permutationCommits[i][j];
  1592. for (size_t i = 0; i < userTallyMaskCommits.size(); i++)
  1593. for (size_t j = 0; j < userTallyMaskCommits[i].size(); j++)
  1594. oracleInput << userTallyMaskCommits[i][j];
  1595. for (size_t i = 0; i < userTallyMessageCommits.size(); i++)
  1596. for (size_t j = 0; j < userTallyMessageCommits[i].size(); j++)
  1597. oracleInput << userTallyMessageCommits[i][j];
  1598. for (size_t i = 0; i < userTallySeedCommits.size(); i++)
  1599. for (size_t j = 0; j < userTallySeedCommits[i].size(); j++)
  1600. oracleInput << userTallySeedCommits[i][j];
  1601. Scalar x = pi[0].challengeParts[0];
  1602. Scalar f1 = pi[0].responseParts[0];
  1603. for (size_t i = 0; i < permutationCommits.size(); i++)
  1604. {
  1605. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1606. {
  1607. size_t piIndex = i * permutationCommits.size() + j + 1;
  1608. Twistpoint U1, U2, U3, U4, U5, U6, U7;
  1609. U2 = pi[piIndex].curvepointUniversals[0];
  1610. U3 = pi[piIndex].curvepointUniversals[1];
  1611. U5 = pi[piIndex].curvepointUniversals[2];
  1612. Scalar f2 = pi[piIndex].responseParts[0];
  1613. Scalar z1 = pi[piIndex].responseParts[1];
  1614. Scalar z2 = pi[piIndex].responseParts[2];
  1615. U1 = g * f2 + h * z1 - permutationCommits[j][i] * x;
  1616. U4 = userTallyMasks[j] * (f1 * f2 * x) +
  1617. currPseudonyms[j] * (f1 * f2 * z2) +
  1618. h * (z2 * x * x) -
  1619. userTallyMaskCommits[i][j] * (x * x * x) -
  1620. U2 * (x * x) -
  1621. U3 * x;
  1622. U6 = userTallyMessages[j] * (f2 * x) +
  1623. nextGenerator * (f2 * z2) +
  1624. h * (z2 * x) -
  1625. userTallyMessageCommits[i][j] * (x * x) -
  1626. U5 * x;
  1627. U7 = g * z2 - userTallySeedCommits[i][j] * x;
  1628. oracleInput << U1 << U2 << U3 << U4 << U5 << U6 << U7;
  1629. }
  1630. }
  1631. if (x != oracle(oracleInput.str()))
  1632. {
  1633. std::cerr << "User tallies not generated by permutation matrix." << std::endl;
  1634. return false;
  1635. }
  1636. for (size_t i = 0; i < userTallySeedCommits.size(); i++)
  1637. {
  1638. Twistpoint sum = userTallySeedCommits[i][0];
  1639. for (size_t j = 1; j < userTallySeedCommits[i].size(); j++)
  1640. sum = sum + userTallySeedCommits[i][j];
  1641. if (sum != Twistpoint())
  1642. {
  1643. std::cerr << "seed commits did not sum to 0, aborting." << std::endl;
  1644. return false;
  1645. }
  1646. }
  1647. return true;
  1648. }
  1649. std::vector<Proof> PrsonaBase::generate_batched_user_tally_proofs(
  1650. const std::vector<std::vector<Scalar>>& permutations,
  1651. const Scalar& power,
  1652. const Twistpoint& nextGenerator,
  1653. const std::vector<std::vector<Scalar>>& permutationSeeds,
  1654. const std::vector<std::vector<Scalar>>& userTallySeeds,
  1655. const std::vector<Twistpoint>& currPseudonyms,
  1656. const std::vector<Twistpoint>& userTallyMasks,
  1657. const std::vector<Twistpoint>& userTallyMessages,
  1658. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1659. const std::vector<std::vector<Twistpoint>>& userTallyMaskCommits,
  1660. const std::vector<std::vector<Twistpoint>>& userTallyMessageCommits,
  1661. const std::vector<std::vector<Twistpoint>>& userTallySeedCommits) const
  1662. {
  1663. std::vector<Proof> retval;
  1664. if (!SERVER_IS_MALICIOUS)
  1665. {
  1666. retval.push_back(Proof("PROOF"));
  1667. return retval;
  1668. }
  1669. Proof first;
  1670. retval.push_back(first);
  1671. Twistpoint g = EL_GAMAL_GENERATOR;
  1672. Twistpoint h = elGamalBlindGenerator;
  1673. std::stringstream oracleInput;
  1674. oracleInput << g << h << nextGenerator;
  1675. for (size_t i = 0; i < currPseudonyms.size(); i++)
  1676. oracleInput << currPseudonyms[i];
  1677. for (size_t i = 0; i < userTallyMasks.size(); i++)
  1678. oracleInput << userTallyMasks[i];
  1679. for (size_t i = 0; i < userTallyMessages.size(); i++)
  1680. oracleInput << userTallyMessages[i];
  1681. for (size_t i = 0; i < permutationCommits.size(); i++)
  1682. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1683. oracleInput << permutationCommits[i][j];
  1684. for (size_t i = 0; i < userTallyMaskCommits.size(); i++)
  1685. for (size_t j = 0; j < userTallyMaskCommits[i].size(); j++)
  1686. oracleInput << userTallyMaskCommits[i][j];
  1687. for (size_t i = 0; i < userTallyMessageCommits.size(); i++)
  1688. for (size_t j = 0; j < userTallyMessageCommits[i].size(); j++)
  1689. oracleInput << userTallyMessageCommits[i][j];
  1690. for (size_t i = 0; i < userTallySeedCommits.size(); i++)
  1691. for (size_t j = 0; j < userTallySeedCommits[i].size(); j++)
  1692. oracleInput << userTallySeedCommits[i][j];
  1693. std::vector<Scalar> b1;
  1694. std::vector<Scalar> b2;
  1695. std::vector<Scalar> t1;
  1696. std::vector<Scalar> t2;
  1697. for (size_t i = 0; i < permutationCommits.size(); i++)
  1698. {
  1699. Proof currProof;
  1700. Scalar currb1;
  1701. Scalar currb2;
  1702. Scalar currt1;
  1703. Scalar currt2;
  1704. Twistpoint U1, U4, U6, U7;
  1705. std::vector<Twistpoint> U2, U3, U5;
  1706. currb1.set_random();
  1707. currb2.set_random();
  1708. currt1.set_random();
  1709. currt2.set_random();
  1710. U1 = g * currb2 + h * currt1;
  1711. U4 = currPseudonyms[i] * (currb1 * currb2 * currt2);
  1712. U6 = nextGenerator * (currb2 * currt2);
  1713. U7 = g * currt2;
  1714. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1715. {
  1716. Twistpoint currU2, currU3, currU5;
  1717. Scalar U2MaskMult = power * currb2 + currb1 * permutations[i][j];
  1718. Scalar U2PseudMult =
  1719. power * currt2 * permutations[i][j] +
  1720. power * currb2 * userTallySeeds[j][i] +
  1721. currb1 * permutations[i][j] * userTallySeeds[j][i];
  1722. Scalar U2hMult = currt2;
  1723. Scalar U3MaskMult = currb1 * currb2;
  1724. Scalar U3PseudMult = power * currb2 * currt2 +
  1725. currb1 * currt2 * permutations[i][j] +
  1726. currb1 * currb2 * userTallySeeds[j][i];
  1727. Scalar U3hMult = Scalar(0);
  1728. Scalar U5MessageMult = currb2;
  1729. Scalar U5GenMult = currb2 * userTallySeeds[j][i] + currt2 * permutations[i][j];
  1730. Scalar U5hMult = currt2;
  1731. for (size_t k = 0; k < permutationCommits[i].size(); k++)
  1732. {
  1733. if (k == j)
  1734. continue;
  1735. std::stringstream xkOracle;
  1736. xkOracle << permutationCommits[i][k] << userTallyMaskCommits[k][i] << userTallyMessageCommits[k][i] << userTallySeedCommits[k][i];
  1737. Scalar x_k = oracle(xkOracle.str(), LAMBDA);
  1738. for (size_t l = 0; l < permutationCommits[i].size(); l++)
  1739. {
  1740. if (l == j)
  1741. continue;
  1742. if (l == k)
  1743. continue;
  1744. std::stringstream xlOracle;
  1745. xlOracle << permutationCommits[i][l] << userTallyMaskCommits[l][i] << userTallyMessageCommits[l][i] << userTallySeedCommits[l][i];
  1746. Scalar x_l = oracle(xlOracle.str(), LAMBDA);
  1747. U3MaskMult = U3MaskMult +
  1748. power * permutations[i][j] * x_k * x_l;
  1749. U3PseudMult = U3PseudMult +
  1750. power * permutations[i][j] * userTallySeeds[k][i] * x_k * x_l;
  1751. U3hMult = U3hMult +
  1752. userTallySeeds[j][i] * x_k * x_l;
  1753. }
  1754. U2MaskMult = U2MaskMult +
  1755. Scalar(2) * power * permutations[i][j] * x_k +
  1756. power * permutations[i][k] * x_k;
  1757. U2PseudMult = U2PseudMult +
  1758. power * permutations[i][j] * userTallySeeds[j][i] * x_k +
  1759. power * permutations[i][k] * userTallySeeds[j][i] * x_k +
  1760. power * permutations[i][j] * userTallySeeds[k][i] * x_k;
  1761. U2hMult = U2hMult +
  1762. Scalar(2) * userTallySeeds[j][i] * x_k +
  1763. userTallySeeds[k][i] * x_k;
  1764. U3MaskMult = U3MaskMult +
  1765. power * currb2 * x_k +
  1766. currb1 * permutations[i][j] * x_k;
  1767. U3PseudMult = U3PseudMult +
  1768. power * currt2 * permutations[i][j] * x_k +
  1769. power * currb2 * userTallySeeds[j][i] * x_k +
  1770. currb1 * permutations[i][j] * userTallySeeds[k][i] * x_k;
  1771. U3hMult = U3hMult +
  1772. currt2 * x_k;
  1773. U5MessageMult = U5MessageMult +
  1774. permutations[i][j] * x_k;
  1775. U5GenMult = U5GenMult +
  1776. permutations[i][j] * userTallySeeds[k][i] * x_k;
  1777. U5hMult = U5hMult +
  1778. userTallySeeds[j][i] * x_k;
  1779. }
  1780. currU2 = userTallyMasks[i] * U2MaskMult +
  1781. currPseudonyms[i] * U2PseudMult +
  1782. h * U2hMult;
  1783. currU3 = userTallyMasks[i] * U3MaskMult +
  1784. currPseudonyms[i] * U3PseudMult +
  1785. h * U3hMult;
  1786. currU5 = userTallyMessages[i] * U5MessageMult +
  1787. nextGenerator * U5GenMult +
  1788. h * U5hMult;
  1789. U2.push_back(currU2);
  1790. U3.push_back(currU3);
  1791. U5.push_back(currU5);
  1792. }
  1793. oracleInput << U1;
  1794. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1795. {
  1796. oracleInput << U2[j];
  1797. currProof.curvepointUniversals.push_back(U2[j]);
  1798. }
  1799. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1800. {
  1801. oracleInput << U3[j];
  1802. currProof.curvepointUniversals.push_back(U3[j]);
  1803. }
  1804. oracleInput << U4;
  1805. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1806. {
  1807. oracleInput << U5[j];
  1808. currProof.curvepointUniversals.push_back(U5[j]);
  1809. }
  1810. oracleInput << U6 << U7;
  1811. b1.push_back(currb1);
  1812. b2.push_back(currb2);
  1813. t1.push_back(currt1);
  1814. t2.push_back(currt2);
  1815. retval.push_back(currProof);
  1816. }
  1817. Scalar x = oracle(oracleInput.str());
  1818. retval[0].challengeParts.push_back(x);
  1819. for (size_t i = 0; i < permutationCommits.size(); i++)
  1820. {
  1821. size_t piIndex = i + 1;
  1822. Scalar f1 = b1[i];
  1823. Scalar f2 = b2[i];
  1824. Scalar z1 = t1[i];
  1825. Scalar z2 = t2[i];
  1826. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1827. {
  1828. std::stringstream currOracle;
  1829. currOracle << permutationCommits[i][j] << userTallyMaskCommits[j][i] << userTallyMessageCommits[j][i] << userTallySeedCommits[j][i];
  1830. Scalar currx = oracle(currOracle.str(), LAMBDA);
  1831. f1 = f1 + power * currx;
  1832. f2 = f2 + permutations[i][j] * currx;
  1833. z1 = z1 + permutationSeeds[i][j] * currx;
  1834. z2 = z2 + userTallySeeds[j][i] * currx;
  1835. }
  1836. retval[piIndex].responseParts.push_back(f1);
  1837. retval[piIndex].responseParts.push_back(f2);
  1838. retval[piIndex].responseParts.push_back(z1);
  1839. retval[piIndex].responseParts.push_back(z2);
  1840. }
  1841. return retval;
  1842. }
  1843. bool PrsonaBase::verify_batched_user_tally_proofs(
  1844. const std::vector<Proof>& pi,
  1845. const Twistpoint& nextGenerator,
  1846. const std::vector<Twistpoint>& currPseudonyms,
  1847. const std::vector<Twistpoint>& userTallyMasks,
  1848. const std::vector<Twistpoint>& userTallyMessages,
  1849. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  1850. const std::vector<std::vector<Twistpoint>>& userTallyMaskCommits,
  1851. const std::vector<std::vector<Twistpoint>>& userTallyMessageCommits,
  1852. const std::vector<std::vector<Twistpoint>>& userTallySeedCommits) const
  1853. {
  1854. if (pi.empty())
  1855. return false;
  1856. if (!SERVER_IS_MALICIOUS)
  1857. return true;
  1858. Twistpoint g = EL_GAMAL_GENERATOR;
  1859. Twistpoint h = elGamalBlindGenerator;
  1860. std::stringstream oracleInput;
  1861. oracleInput << g << h << nextGenerator;
  1862. for (size_t i = 0; i < currPseudonyms.size(); i++)
  1863. oracleInput << currPseudonyms[i];
  1864. for (size_t i = 0; i < userTallyMasks.size(); i++)
  1865. oracleInput << userTallyMasks[i];
  1866. for (size_t i = 0; i < userTallyMessages.size(); i++)
  1867. oracleInput << userTallyMessages[i];
  1868. for (size_t i = 0; i < permutationCommits.size(); i++)
  1869. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1870. oracleInput << permutationCommits[i][j];
  1871. for (size_t i = 0; i < userTallyMaskCommits.size(); i++)
  1872. for (size_t j = 0; j < userTallyMaskCommits[i].size(); j++)
  1873. oracleInput << userTallyMaskCommits[i][j];
  1874. for (size_t i = 0; i < userTallyMessageCommits.size(); i++)
  1875. for (size_t j = 0; j < userTallyMessageCommits[i].size(); j++)
  1876. oracleInput << userTallyMessageCommits[i][j];
  1877. for (size_t i = 0; i < userTallySeedCommits.size(); i++)
  1878. for (size_t j = 0; j < userTallySeedCommits[i].size(); j++)
  1879. oracleInput << userTallySeedCommits[i][j];
  1880. Scalar x = pi[0].challengeParts[0];
  1881. for (size_t i = 0; i < permutationCommits.size(); i++)
  1882. {
  1883. Scalar sum_of_sub_xs = Scalar(0);
  1884. std::vector<Scalar> sub_xs;
  1885. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1886. {
  1887. std::stringstream currOracle;
  1888. currOracle << permutationCommits[i][j] << userTallyMaskCommits[j][i] << userTallyMessageCommits[j][i] << userTallySeedCommits[j][i];
  1889. sub_xs.push_back(oracle(currOracle.str(), LAMBDA));
  1890. sum_of_sub_xs = sum_of_sub_xs + sub_xs[j];
  1891. }
  1892. size_t piIndex = i + 1;
  1893. std::vector<Twistpoint> U2(pi[piIndex].curvepointUniversals.begin(), pi[piIndex].curvepointUniversals.begin() + permutationCommits.size());
  1894. std::vector<Twistpoint> U3(pi[piIndex].curvepointUniversals.begin() + permutationCommits.size(), pi[piIndex].curvepointUniversals.begin() + (2 * permutationCommits.size()));
  1895. std::vector<Twistpoint> U5(pi[piIndex].curvepointUniversals.begin() + (2 * permutationCommits.size()), pi[piIndex].curvepointUniversals.end());
  1896. Scalar f1 = pi[piIndex].responseParts[0];
  1897. Scalar f2 = pi[piIndex].responseParts[1];
  1898. Scalar z1 = pi[piIndex].responseParts[2];
  1899. Scalar z2 = pi[piIndex].responseParts[3];
  1900. Twistpoint U1 = g * f2 + h * z1;
  1901. Twistpoint U4 = userTallyMasks[i] * (f1 * f2 * sum_of_sub_xs) +
  1902. currPseudonyms[i] * (f1 * f2 * z2) +
  1903. h * (z2 * sum_of_sub_xs * sum_of_sub_xs);
  1904. Twistpoint U6 = userTallyMessages[i] * (f2 * sum_of_sub_xs) +
  1905. nextGenerator * (f2 * z2) +
  1906. h * (z2 * sum_of_sub_xs);
  1907. Twistpoint U7 = g * z2;
  1908. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1909. {
  1910. U1 = U1 - permutationCommits[i][j] * sub_xs[j];
  1911. U4 = U4 -
  1912. userTallyMaskCommits[j][i] * (sub_xs[j] * sub_xs[j] * sub_xs[j]) -
  1913. U2[j] * (sub_xs[j] * sub_xs[j]) -
  1914. U3[j] * sub_xs[j];
  1915. U6 = U6 -
  1916. userTallyMessageCommits[j][i] * (sub_xs[j] * sub_xs[j]) -
  1917. U5[j] * sub_xs[j];
  1918. U7 = U7 - userTallySeedCommits[j][i] * sub_xs[j];
  1919. }
  1920. oracleInput << U1;
  1921. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1922. oracleInput << U2[j];
  1923. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1924. oracleInput << U3[j];
  1925. oracleInput << U4;
  1926. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1927. oracleInput << U5[j];
  1928. oracleInput << U6 << U7;
  1929. }
  1930. if (x != oracle(oracleInput.str()))
  1931. {
  1932. std::cerr << "User tallies not generated by permutation matrix." << std::endl;
  1933. return false;
  1934. }
  1935. for (size_t i = 0; i < userTallySeedCommits.size(); i++)
  1936. {
  1937. Twistpoint sum = userTallySeedCommits[i][0];
  1938. for (size_t j = 1; j < userTallySeedCommits[i].size(); j++)
  1939. sum = sum + userTallySeedCommits[i][j];
  1940. if (sum != Twistpoint())
  1941. {
  1942. std::cerr << "seed commits did not sum to 0, aborting." << std::endl;
  1943. return false;
  1944. }
  1945. }
  1946. return true;
  1947. }
  1948. /*
  1949. * SERVER AGREEMENT PROOFS
  1950. */
  1951. Proof PrsonaBase::generate_valid_vote_row_proof(
  1952. const std::vector<TwistBipoint>& commitment) const
  1953. {
  1954. Proof retval;
  1955. if (!SERVER_IS_MALICIOUS)
  1956. {
  1957. retval.hbc = "PROOF";
  1958. return retval;
  1959. }
  1960. std::stringstream oracleInput;
  1961. for (size_t i = 0; i < commitment.size(); i++)
  1962. oracleInput << commitment[i];
  1963. Scalar val = oracle(oracleInput.str());
  1964. retval.responseParts.push_back(val);
  1965. return retval;
  1966. }
  1967. Proof PrsonaBase::generate_valid_vote_matrix_proof(
  1968. const std::vector<std::vector<TwistBipoint>>& commitment) const
  1969. {
  1970. Proof retval;
  1971. if (!SERVER_IS_MALICIOUS)
  1972. {
  1973. retval.hbc = "PROOF";
  1974. return retval;
  1975. }
  1976. std::stringstream oracleInput;
  1977. for (size_t i = 0; i < commitment.size(); i++)
  1978. for (size_t j = 0; j < commitment[i].size(); j++)
  1979. oracleInput << commitment[i][j];
  1980. Scalar val = oracle(oracleInput.str());
  1981. retval.responseParts.push_back(val);
  1982. return retval;
  1983. }
  1984. Proof PrsonaBase::generate_valid_user_tally_proof(
  1985. const EGCiphertext& commitment) const
  1986. {
  1987. Proof retval;
  1988. if (!SERVER_IS_MALICIOUS)
  1989. {
  1990. retval.hbc = "PROOF";
  1991. return retval;
  1992. }
  1993. std::stringstream oracleInput;
  1994. oracleInput << commitment;
  1995. Scalar val = oracle(oracleInput.str());
  1996. retval.responseParts.push_back(val);
  1997. return retval;
  1998. }
  1999. Proof PrsonaBase::generate_valid_server_tally_proof(
  2000. const CurveBipoint& commitment) const
  2001. {
  2002. Proof retval;
  2003. if (!SERVER_IS_MALICIOUS)
  2004. {
  2005. retval.hbc = "PROOF";
  2006. return retval;
  2007. }
  2008. std::stringstream oracleInput;
  2009. oracleInput << commitment;
  2010. Scalar val = oracle(oracleInput.str());
  2011. retval.responseParts.push_back(val);
  2012. return retval;
  2013. }
  2014. Proof PrsonaBase::generate_valid_pseudonyms_proof(
  2015. const std::vector<Twistpoint>& commitment) const
  2016. {
  2017. Proof retval;
  2018. if (!SERVER_IS_MALICIOUS)
  2019. {
  2020. retval.hbc = "PROOF";
  2021. return retval;
  2022. }
  2023. std::stringstream oracleInput;
  2024. for (size_t i = 0; i < commitment.size(); i++)
  2025. oracleInput << commitment[i];
  2026. Scalar val = oracle(oracleInput.str());
  2027. retval.responseParts.push_back(val);
  2028. return retval;
  2029. }
  2030. bool PrsonaBase::verify_valid_vote_row_proof(
  2031. const std::vector<Proof>& pi,
  2032. const std::vector<TwistBipoint>& commitment) const
  2033. {
  2034. if (pi.empty())
  2035. return false;
  2036. if (!SERVER_IS_MALICIOUS)
  2037. return true;
  2038. Scalar comparison = pi[0].responseParts[0];
  2039. std::stringstream oracleInput;
  2040. for (size_t i = 0; i < commitment.size(); i++)
  2041. oracleInput << commitment[i];
  2042. if (oracle(oracleInput.str()) != comparison)
  2043. {
  2044. std::cerr << "Server's claimed value doesn't match their own commitment." << std::endl;
  2045. return false;
  2046. }
  2047. size_t agreement = 1;
  2048. for (size_t i = 1; i < pi.size(); i++)
  2049. if (comparison == pi[i].responseParts[0])
  2050. agreement++;
  2051. return agreement * 2 > pi.size();
  2052. }
  2053. bool PrsonaBase::verify_valid_vote_matrix_proof(
  2054. const std::vector<Proof>& pi,
  2055. const std::vector<std::vector<TwistBipoint>>& commitment) const
  2056. {
  2057. if (pi.empty())
  2058. return false;
  2059. if (!SERVER_IS_MALICIOUS)
  2060. return true;
  2061. Scalar comparison = pi[0].responseParts[0];
  2062. std::stringstream oracleInput;
  2063. for (size_t i = 0; i < commitment.size(); i++)
  2064. for (size_t j = 0; j < commitment[i].size(); j++)
  2065. oracleInput << commitment[i][j];
  2066. if (oracle(oracleInput.str()) != comparison)
  2067. {
  2068. std::cerr << "Server's claimed value doesn't match their own commitment." << std::endl;
  2069. return false;
  2070. }
  2071. size_t agreement = 1;
  2072. for (size_t i = 1; i < pi.size(); i++)
  2073. if (comparison == pi[i].responseParts[0])
  2074. agreement++;
  2075. return agreement * 2 > pi.size();
  2076. }
  2077. bool PrsonaBase::verify_valid_user_tally_proof(
  2078. const std::vector<Proof>& pi,
  2079. const EGCiphertext& commitment) const
  2080. {
  2081. if (pi.empty())
  2082. return false;
  2083. if (!SERVER_IS_MALICIOUS)
  2084. return true;
  2085. Scalar comparison = pi[0].responseParts[0];
  2086. std::stringstream oracleInput;
  2087. oracleInput << commitment;
  2088. if (oracle(oracleInput.str()) != comparison)
  2089. {
  2090. std::cerr << "Server's claimed value doesn't match their own commitment." << std::endl;
  2091. return false;
  2092. }
  2093. size_t agreement = 1;
  2094. for (size_t i = 1; i < pi.size(); i++)
  2095. if (comparison == pi[i].responseParts[0])
  2096. agreement++;
  2097. return agreement * 2 > pi.size();
  2098. }
  2099. bool PrsonaBase::verify_valid_server_tally_proof(
  2100. const std::vector<Proof>& pi,
  2101. const CurveBipoint& commitment) const
  2102. {
  2103. if (pi.empty())
  2104. return false;
  2105. if (!SERVER_IS_MALICIOUS)
  2106. return true;
  2107. Scalar comparison = pi[0].responseParts[0];
  2108. std::stringstream oracleInput;
  2109. oracleInput << commitment;
  2110. if (oracle(oracleInput.str()) != comparison)
  2111. {
  2112. std::cerr << "Server's claimed value doesn't match their own commitment." << std::endl;
  2113. return false;
  2114. }
  2115. size_t agreement = 1;
  2116. for (size_t i = 1; i < pi.size(); i++)
  2117. if (comparison == pi[i].responseParts[0])
  2118. agreement++;
  2119. return agreement * 2 > pi.size();
  2120. }
  2121. bool PrsonaBase::verify_valid_pseudonyms_proof(
  2122. const std::vector<Proof>& pi,
  2123. const std::vector<Twistpoint>& commitment) const
  2124. {
  2125. if (pi.empty())
  2126. return false;
  2127. if (!SERVER_IS_MALICIOUS)
  2128. return true;
  2129. Scalar comparison = pi[0].responseParts[0];
  2130. std::stringstream oracleInput;
  2131. for (size_t i = 0; i < commitment.size(); i++)
  2132. oracleInput << commitment[i];
  2133. if (oracle(oracleInput.str()) != comparison)
  2134. {
  2135. std::cerr << "Server's claimed value doesn't match their own commitment." << std::endl;
  2136. return false;
  2137. }
  2138. size_t agreement = 1;
  2139. for (size_t i = 1; i < pi.size(); i++)
  2140. if (comparison == pi[i].responseParts[0])
  2141. agreement++;
  2142. return agreement * 2 > pi.size();
  2143. }
  2144. template std::vector<Proof> PrsonaBase::generate_proof_of_reordering<Twistpoint>(
  2145. const std::vector<std::vector<Scalar>>& permutations,
  2146. const std::vector<std::vector<Scalar>>& permutationSeeds,
  2147. const std::vector<std::vector<Scalar>>& productSeeds,
  2148. const std::vector<Twistpoint>& oldValues,
  2149. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  2150. const std::vector<std::vector<Twistpoint>>& productCommits,
  2151. const Twistpoint& otherG,
  2152. const Twistpoint& otherH) const;
  2153. template std::vector<Proof> PrsonaBase::generate_proof_of_reordering<TwistBipoint>(
  2154. const std::vector<std::vector<Scalar>>& permutations,
  2155. const std::vector<std::vector<Scalar>>& permutationSeeds,
  2156. const std::vector<std::vector<Scalar>>& productSeeds,
  2157. const std::vector<TwistBipoint>& oldValues,
  2158. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  2159. const std::vector<std::vector<TwistBipoint>>& productCommits,
  2160. const TwistBipoint& otherG,
  2161. const TwistBipoint& otherH) const;
  2162. template std::vector<Proof> PrsonaBase::generate_proof_of_reordering<CurveBipoint>(
  2163. const std::vector<std::vector<Scalar>>& permutations,
  2164. const std::vector<std::vector<Scalar>>& permutationSeeds,
  2165. const std::vector<std::vector<Scalar>>& productSeeds,
  2166. const std::vector<CurveBipoint>& oldValues,
  2167. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  2168. const std::vector<std::vector<CurveBipoint>>& productCommits,
  2169. const CurveBipoint& otherG,
  2170. const CurveBipoint& otherH) const;
  2171. template bool PrsonaBase::verify_proof_of_reordering<Twistpoint>(
  2172. const std::vector<Proof>& pi,
  2173. const std::vector<Twistpoint>& oldValues,
  2174. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  2175. const std::vector<std::vector<Twistpoint>>& productCommits,
  2176. const Twistpoint& otherG,
  2177. const Twistpoint& otherH) const;
  2178. template bool PrsonaBase::verify_proof_of_reordering<TwistBipoint>(
  2179. const std::vector<Proof>& pi,
  2180. const std::vector<TwistBipoint>& oldValues,
  2181. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  2182. const std::vector<std::vector<TwistBipoint>>& productCommits,
  2183. const TwistBipoint& otherG,
  2184. const TwistBipoint& otherH) const;
  2185. template bool PrsonaBase::verify_proof_of_reordering<CurveBipoint>(
  2186. const std::vector<Proof>& pi,
  2187. const std::vector<CurveBipoint>& oldValues,
  2188. const std::vector<std::vector<Twistpoint>>& permutationCommits,
  2189. const std::vector<std::vector<CurveBipoint>>& productCommits,
  2190. const CurveBipoint& otherG,
  2191. const CurveBipoint& otherH) const;