base.cpp 84 KB

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