base.cpp 84 KB

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