base.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647
  1. #include <iostream>
  2. #include "base.hpp"
  3. extern const scalar_t bn_n;
  4. extern const curvepoint_fp_t bn_curvegen;
  5. /* These lines need to be here so these static variables are defined,
  6. * but in C++ putting code here doesn't actually execute
  7. * (or at least, with g++, whenever it would execute is not at a useful time)
  8. * so we have an init() function to actually put the correct values in them. */
  9. Curvepoint PrsonaBase::EL_GAMAL_GENERATOR = Curvepoint();
  10. Scalar PrsonaBase::SCALAR_N = Scalar();
  11. Scalar PrsonaBase::DEFAULT_TALLY = Scalar();
  12. Scalar PrsonaBase::DEFAULT_VOTE = Scalar();
  13. bool PrsonaBase::SERVER_IS_MALICIOUS = false;
  14. bool PrsonaBase::CLIENT_IS_MALICIOUS = false;
  15. size_t PrsonaBase::MAX_ALLOWED_VOTE = 2;
  16. // Quick and dirty function to calculate ceil(log base 2) with mpz_class
  17. mpz_class log2(mpz_class x)
  18. {
  19. mpz_class retval = 0;
  20. while (x > 0)
  21. {
  22. retval++;
  23. x = x >> 1;
  24. }
  25. return retval;
  26. }
  27. mpz_class bit(mpz_class x)
  28. {
  29. return x > 0 ? 1 : 0;
  30. }
  31. /********************
  32. * PUBLIC FUNCTIONS *
  33. ********************/
  34. /*
  35. * SETUP FUNCTIONS
  36. */
  37. // Must be called once before any usage of this class
  38. void PrsonaBase::init()
  39. {
  40. EL_GAMAL_GENERATOR = Curvepoint(bn_curvegen);
  41. SCALAR_N = Scalar(bn_n);
  42. DEFAULT_TALLY = Scalar(1);
  43. DEFAULT_VOTE = Scalar(1);
  44. }
  45. // Call this (once) if using malicious-security servers
  46. void PrsonaBase::set_server_malicious()
  47. {
  48. SERVER_IS_MALICIOUS = true;
  49. }
  50. // Call this (once) if using malicious-security clients
  51. void PrsonaBase::set_client_malicious()
  52. {
  53. CLIENT_IS_MALICIOUS = true;
  54. }
  55. /*
  56. * CONST GETTERS
  57. */
  58. size_t PrsonaBase::get_max_allowed_vote()
  59. {
  60. return MAX_ALLOWED_VOTE;
  61. }
  62. Curvepoint PrsonaBase::get_blinding_generator() const
  63. {
  64. return elGamalBlindGenerator;
  65. }
  66. Curvepoint PrsonaBase::get_blinding_generator(std::vector<Proof>& pi) const
  67. {
  68. pi = elGamalBlindGeneratorProof;
  69. return elGamalBlindGenerator;
  70. }
  71. /***********************
  72. * PROTECTED FUNCTIONS *
  73. ***********************/
  74. /*
  75. * PRIVATE ELEMENT SETTER
  76. */
  77. bool PrsonaBase::set_EG_blind_generator(
  78. const std::vector<Proof>& pi,
  79. const Curvepoint& currGenerator,
  80. size_t numServers)
  81. {
  82. if (!verify_generator_proof(pi, currGenerator, numServers))
  83. return false;
  84. elGamalBlindGeneratorProof = pi;
  85. elGamalBlindGenerator = currGenerator;
  86. return true;
  87. }
  88. /*
  89. * BINARY SEARCH
  90. */
  91. /* Completely normal binary search
  92. * There might be a standard function for this in <algorithms>?
  93. * But it returns an iterator, not a size_t, so less useful. */
  94. size_t PrsonaBase::binary_search(
  95. const std::vector<Curvepoint> list, const Curvepoint& index) const
  96. {
  97. size_t lo, hi;
  98. lo = 0;
  99. hi = list.size() - 1;
  100. while (lo < hi)
  101. {
  102. size_t mid = (lo + hi) / 2;
  103. if (list[mid] < index)
  104. lo = mid + 1;
  105. else if (index == list[mid])
  106. return mid;
  107. else if (mid == lo)
  108. return lo;
  109. else hi = mid - 1;
  110. }
  111. return lo;
  112. }
  113. /*
  114. * SCHNORR PROOFS
  115. */
  116. Proof PrsonaBase::schnorr_generation(
  117. const Curvepoint& generator,
  118. const Curvepoint& commitment,
  119. const Scalar& log) const
  120. {
  121. Proof retval;
  122. std::stringstream oracleInput;
  123. Scalar u;
  124. u.set_random();
  125. Curvepoint U = generator * u;
  126. oracleInput << generator << commitment << U;
  127. Scalar x = oracle(oracleInput.str());
  128. Scalar z = log * x + u;
  129. retval.challengeParts.push_back(x);
  130. retval.responseParts.push_back(z);
  131. return retval;
  132. }
  133. bool PrsonaBase::schnorr_verification(
  134. const Curvepoint& generator,
  135. const Curvepoint& commitment,
  136. const Scalar& x,
  137. const Scalar& z) const
  138. {
  139. Curvepoint U = generator * z - commitment * x;
  140. std::stringstream oracleInput;
  141. oracleInput << generator << commitment << U;
  142. return x == oracle(oracleInput.str());
  143. }
  144. /*
  145. * OWNERSHIP PROOFS
  146. */
  147. // Prove ownership of the short term public key
  148. Proof PrsonaBase::generate_ownership_proof(
  149. const Curvepoint& generator,
  150. const Curvepoint& commitment,
  151. const Scalar& log) const
  152. {
  153. if (!CLIENT_IS_MALICIOUS)
  154. {
  155. Proof retval;
  156. retval.hbc = "PROOF";
  157. return retval;
  158. }
  159. return schnorr_generation(generator, commitment, log);
  160. }
  161. bool PrsonaBase::verify_ownership_proof(
  162. const Proof& pi,
  163. const Curvepoint& generator,
  164. const Curvepoint& commitment) const
  165. {
  166. if (!CLIENT_IS_MALICIOUS)
  167. return pi.hbc == "PROOF";
  168. Scalar c = pi.challengeParts[0];
  169. Scalar z = pi.responseParts[0];
  170. return schnorr_verification(generator, commitment, c, z);
  171. }
  172. /*
  173. * ITERATED SCHNORR PROOFS
  174. */
  175. Proof PrsonaBase::add_to_generator_proof(
  176. const Curvepoint& currGenerator,
  177. const Scalar& seed) const
  178. {
  179. Proof retval;
  180. if (!CLIENT_IS_MALICIOUS)
  181. {
  182. retval.hbc = "PROOF";
  183. return retval;
  184. }
  185. Curvepoint nextGenerator = currGenerator * seed;
  186. retval = schnorr_generation(currGenerator, nextGenerator, seed);
  187. retval.curvepointUniversals.push_back(currGenerator);
  188. return retval;
  189. }
  190. bool PrsonaBase::verify_generator_proof(
  191. const std::vector<Proof>& pi,
  192. const Curvepoint& currGenerator,
  193. size_t numServers) const
  194. {
  195. if (pi.size() != numServers || numServers == 0)
  196. return false;
  197. bool retval = true;
  198. if (!SERVER_IS_MALICIOUS)
  199. {
  200. for (size_t i = 0; i < pi.size(); i++)
  201. retval = retval && pi[i].hbc == "PROOF";
  202. return retval;
  203. }
  204. if (pi[0].curvepointUniversals[0] != EL_GAMAL_GENERATOR)
  205. return false;
  206. for (size_t i = 0; i < pi.size(); i++)
  207. {
  208. Curvepoint generator = pi[i].curvepointUniversals[0];
  209. Curvepoint commitment = (i == pi.size() - 1 ?
  210. currGenerator :
  211. pi[i + 1].curvepointUniversals[0]);
  212. Scalar c = pi[i].challengeParts[0];
  213. Scalar z = pi[i].responseParts[0];
  214. retval = retval &&
  215. schnorr_verification(generator, commitment, c, z);
  216. if (!retval)
  217. std::cerr << "Error in index " << i+1 << " of " << pi.size() << std::endl;
  218. }
  219. return retval;
  220. }
  221. /*
  222. * REPUTATION PROOFS
  223. */
  224. // A pretty straightforward range proof (generation)
  225. std::vector<Proof> PrsonaBase::generate_reputation_proof(
  226. const Proof& ownershipProof,
  227. const EGCiphertext& commitment,
  228. const Scalar& currentScore,
  229. const Scalar& threshold,
  230. const Scalar& inverseKey,
  231. size_t numClients) const
  232. {
  233. std::vector<Proof> retval;
  234. // Base case
  235. if (!CLIENT_IS_MALICIOUS)
  236. {
  237. retval.push_back(Proof("PROOF"));
  238. return retval;
  239. }
  240. // Don't even try if the user asks to make an illegitimate proof
  241. if (threshold.toInt() > (numClients * MAX_ALLOWED_VOTE))
  242. return retval;
  243. // We really have two consecutive proofs in a junction.
  244. // The first is to prove that we are the stpk we claim we are
  245. retval.push_back(ownershipProof);
  246. // The value we're actually using in our proof
  247. mpz_class proofVal = (currentScore - threshold).toInt();
  248. // Top of the range in our proof determined by what scores are even possible
  249. mpz_class proofBits =
  250. log2(numClients * MAX_ALLOWED_VOTE - threshold.toInt());
  251. // Don't risk a situation that would divulge our private key
  252. if (proofBits <= 1)
  253. proofBits = 2;
  254. // This seems weird, but remember our base is A_t^r, not g^t
  255. std::vector<Scalar> masksPerBit;
  256. masksPerBit.push_back(inverseKey);
  257. for (size_t i = 1; i < proofBits; i++)
  258. {
  259. Scalar currMask;
  260. currMask.set_random();
  261. masksPerBit.push_back(currMask);
  262. masksPerBit[0] =
  263. masksPerBit[0] - (currMask * Scalar(1 << i));
  264. }
  265. // Taken from Fig. 1 in https://eprint.iacr.org/2014/764.pdf
  266. for (size_t i = 0; i < proofBits; i++)
  267. {
  268. Proof currProof;
  269. Curvepoint g, h, C, C_a, C_b;
  270. g = commitment.mask;
  271. h = elGamalBlindGenerator;
  272. mpz_class currBit = bit(proofVal & (1 << i));
  273. Scalar a, s, t, m, r;
  274. a.set_random();
  275. s.set_random();
  276. t.set_random();
  277. m = Scalar(currBit);
  278. r = masksPerBit[i];
  279. C = g * r + h * m;
  280. currProof.curvepointUniversals.push_back(C);
  281. C_a = g * s + h * a;
  282. C_b = g * t + h * a * m;
  283. std::stringstream oracleInput;
  284. oracleInput << g << h << C << C_a << C_b;
  285. Scalar x = oracle(oracleInput.str());
  286. currProof.challengeParts.push_back(x);
  287. Scalar f, z_a, z_b;
  288. f = m * x + a;
  289. z_a = r * x + s;
  290. z_b = r * (x - f) + t;
  291. currProof.responseParts.push_back(f);
  292. currProof.responseParts.push_back(z_a);
  293. currProof.responseParts.push_back(z_b);
  294. retval.push_back(currProof);
  295. }
  296. return retval;
  297. }
  298. // A pretty straightforward range proof (verification)
  299. bool PrsonaBase::verify_reputation_proof(
  300. const std::vector<Proof>& pi,
  301. const Curvepoint& generator,
  302. const Curvepoint& owner,
  303. const EGCiphertext& commitment,
  304. const Scalar& threshold) const
  305. {
  306. // Reject outright if there's no proof to check
  307. if (pi.empty())
  308. {
  309. std::cerr << "Proof was empty, aborting." << std::endl;
  310. return false;
  311. }
  312. // If the range is so big that it wraps around mod n,
  313. // there's a chance the user actually made a proof for a very low reputation
  314. if (pi.size() > 256)
  315. {
  316. std::cerr << "Proof was too big, prover could have cheated." << std::endl;
  317. return false;
  318. }
  319. if (!CLIENT_IS_MALICIOUS)
  320. return pi[0].hbc == "PROOF";
  321. Scalar ownerChallenge, ownerResponse;
  322. ownerChallenge = pi[0].challengeParts[0];
  323. ownerResponse = pi[0].responseParts[0];
  324. // User should be able to prove they are who they say they are
  325. if (!schnorr_verification(generator, owner, ownerChallenge, ownerResponse))
  326. {
  327. std::cerr << "Schnorr proof failed, aborting." << std::endl;
  328. return false;
  329. }
  330. // X is the thing we're going to be checking in on throughout
  331. // to try to get our score commitment back in the end.
  332. Curvepoint X;
  333. for (size_t i = 1; i < pi.size(); i++)
  334. {
  335. Curvepoint C, g, h;
  336. C = pi[i].curvepointUniversals[0];
  337. g = commitment.mask;
  338. h = elGamalBlindGenerator;
  339. X = X + C * Scalar(1 << (i - 1));
  340. Scalar x, f, z_a, z_b;
  341. x = pi[i].challengeParts[0];
  342. f = pi[i].responseParts[0];
  343. z_a = pi[i].responseParts[1];
  344. z_b = pi[i].responseParts[2];
  345. // Taken from Fig. 1 in https://eprint.iacr.org/2014/764.pdf
  346. Curvepoint C_a, C_b;
  347. C_a = g * z_a + h * f - C * x;
  348. C_b = g * z_b - C * (x - f);
  349. std::stringstream oracleInput;
  350. oracleInput << g << h << C << C_a << C_b;
  351. if (oracle(oracleInput.str()) != x)
  352. {
  353. std::cerr << "0 or 1 proof failed at index " << i << " of " << pi.size() - 1 << ", aborting." << std::endl;
  354. return false;
  355. }
  356. }
  357. Curvepoint scoreCommitment =
  358. commitment.encryptedMessage +
  359. elGamalBlindGenerator * -threshold;
  360. return X == scoreCommitment;
  361. }
  362. /*
  363. * VALID VOTE PROOFS
  364. */
  365. std::vector<Proof> PrsonaBase::generate_vote_proof(
  366. const Proof& ownershipProof,
  367. const CurveBipoint& g,
  368. const CurveBipoint& h,
  369. const std::vector<bool>& replaces,
  370. const std::vector<CurveBipoint>& oldEncryptedVotes,
  371. const std::vector<CurveBipoint>& newEncryptedVotes,
  372. const std::vector<Scalar>& seeds,
  373. const std::vector<Scalar>& votes) const
  374. {
  375. std::vector<Proof> retval;
  376. // Base case
  377. if (!CLIENT_IS_MALICIOUS)
  378. {
  379. retval.push_back(Proof("PROOF"));
  380. return retval;
  381. }
  382. // The first need is to prove that we are the stpk we claim we are
  383. retval.push_back(ownershipProof);
  384. // Then, we iterate over all votes for the proofs that they are correct
  385. for (size_t i = 0; i < replaces.size(); i++)
  386. {
  387. std::stringstream oracleInput;
  388. oracleInput << g << h << oldEncryptedVotes[i] << newEncryptedVotes[i];
  389. Scalar m = votes[i];
  390. Scalar r = seeds[i];
  391. /* This proof structure is documented in my notes.
  392. * It's inspired by the proof in Fig. 1 at
  393. * https://eprint.iacr.org/2014/764.pdf, but adapted so that you prove
  394. * m(m-1)(m-2) = 0 instead of m(m-1) = 0.
  395. *
  396. * The rerandomization part is just a slight variation on an
  397. * ordinary Schnorr proof, so that part's less scary. */
  398. if (replaces[i]) // CASE: Make new vote
  399. {
  400. Proof currProof;
  401. Scalar x_r, z_r, a, s, t_1, t_2;
  402. x_r.set_random();
  403. z_r.set_random();
  404. a.set_random();
  405. s.set_random();
  406. t_1.set_random();
  407. t_2.set_random();
  408. CurveBipoint U = h * z_r +
  409. oldEncryptedVotes[i] * x_r -
  410. newEncryptedVotes[i] * x_r;
  411. CurveBipoint C_a = g * a + h * s;
  412. Scalar power = ((a + a) * m * m - (a + a + a) * m);
  413. CurveBipoint C_b = g * power + h * t_1;
  414. currProof.curveBipointUniversals.push_back(C_b);
  415. CurveBipoint C_c = g * (a * a * m) +
  416. h * t_2;
  417. oracleInput << U << C_a << C_b << C_c;
  418. Scalar x = oracle(oracleInput.str());
  419. Scalar x_n = x - x_r;
  420. currProof.challengeParts.push_back(x_r);
  421. currProof.challengeParts.push_back(x_n);
  422. Scalar f = m * x_n + a;
  423. Scalar z_na = r * x_n + s;
  424. Scalar z_nb =
  425. r * (f - x_n) * (x_n + x_n - f) + t_1 * x_n + t_2;
  426. currProof.responseParts.push_back(z_r);
  427. currProof.responseParts.push_back(f);
  428. currProof.responseParts.push_back(z_na);
  429. currProof.responseParts.push_back(z_nb);
  430. retval.push_back(currProof);
  431. }
  432. else // CASE: Rerandomize existing vote
  433. {
  434. Proof currProof;
  435. Scalar u, commitmentLambda_1, commitmentLambda_2,
  436. x_n, z_na, z_nb, f;
  437. u.set_random();
  438. commitmentLambda_1.set_random();
  439. commitmentLambda_2.set_random();
  440. x_n.set_random();
  441. z_na.set_random();
  442. z_nb.set_random();
  443. f.set_random();
  444. CurveBipoint U = h * u;
  445. CurveBipoint C_a = g * f +
  446. h * z_na -
  447. newEncryptedVotes[i] * x_n;
  448. CurveBipoint C_b = g * commitmentLambda_1 + h * commitmentLambda_2;
  449. currProof.curveBipointUniversals.push_back(C_b);
  450. CurveBipoint C_c =
  451. h * z_nb -
  452. newEncryptedVotes[i] * ((f - x_n) * (x_n + x_n - f)) -
  453. C_b * x_n;
  454. oracleInput << U << C_a << C_b << C_c;
  455. Scalar x = oracle(oracleInput.str());
  456. Scalar x_r = x - x_n;
  457. currProof.challengeParts.push_back(x_r);
  458. currProof.challengeParts.push_back(x_n);
  459. Scalar z_r = r * x_r + u;
  460. currProof.responseParts.push_back(z_r);
  461. currProof.responseParts.push_back(f);
  462. currProof.responseParts.push_back(z_na);
  463. currProof.responseParts.push_back(z_nb);
  464. retval.push_back(currProof);
  465. }
  466. }
  467. return retval;
  468. }
  469. bool PrsonaBase::verify_vote_proof(
  470. const CurveBipoint& g,
  471. const CurveBipoint& h,
  472. const std::vector<Proof>& pi,
  473. const std::vector<CurveBipoint>& oldEncryptedVotes,
  474. const std::vector<CurveBipoint>& newEncryptedVotes,
  475. const Curvepoint& freshGenerator,
  476. const Curvepoint& owner) const
  477. {
  478. // Reject outright if there's no proof to check
  479. if (pi.empty())
  480. {
  481. std::cerr << "Proof was empty, aborting." << std::endl;
  482. return false;
  483. }
  484. // Base case
  485. if (!CLIENT_IS_MALICIOUS)
  486. return pi[0].hbc == "PROOF";
  487. // User should be able to prove they are who they say they are
  488. if (!verify_ownership_proof(pi[0], freshGenerator, owner))
  489. {
  490. std::cerr << "Schnorr proof failed, aborting." << std::endl;
  491. return false;
  492. }
  493. /* This proof structure is documented in my notes.
  494. * It's inspired by the proof in Fig. 1 at
  495. * https://eprint.iacr.org/2014/764.pdf, but adapted so that you prove
  496. * m(m-1)(m-2) = 0 instead of m(m-1) = 0.
  497. *
  498. * The rerandomization part is just a slight variation on an
  499. * ordinary Schnorr proof, so that part's less scary. */
  500. for (size_t i = 1; i < pi.size(); i++)
  501. {
  502. size_t voteIndex = i - 1;
  503. CurveBipoint C_b;
  504. C_b = pi[i].curveBipointUniversals[0];
  505. Scalar x_r, x_n, z_r, f, z_na, z_nb;
  506. x_r = pi[i].challengeParts[0];
  507. x_n = pi[i].challengeParts[1];
  508. z_r = pi[i].responseParts[0];
  509. f = pi[i].responseParts[1];
  510. z_na = pi[i].responseParts[2];
  511. z_nb = pi[i].responseParts[3];
  512. CurveBipoint U, C_a, C_c;
  513. U = h * z_r +
  514. oldEncryptedVotes[voteIndex] * x_r -
  515. newEncryptedVotes[voteIndex] * x_r;
  516. C_a = g * f + h * z_na - newEncryptedVotes[voteIndex] * x_n;
  517. C_c = h * z_nb -
  518. newEncryptedVotes[voteIndex] * ((f - x_n) * (x_n + x_n - f)) -
  519. C_b * x_n;
  520. std::stringstream oracleInput;
  521. oracleInput << g << h
  522. << oldEncryptedVotes[voteIndex] << newEncryptedVotes[voteIndex]
  523. << U << C_a << C_b << C_c;
  524. if (oracle(oracleInput.str()) != x_r + x_n)
  525. return false;
  526. }
  527. return true;
  528. }
  529. /*
  530. * NEW USER PROOFS
  531. */
  532. std::vector<Proof> PrsonaBase::generate_proof_of_added_user(
  533. const Scalar& twistBipointSeed,
  534. const Scalar& EGCiphertextSeed,
  535. const std::vector<Scalar>& curveBipointSelfSeeds,
  536. const std::vector<Scalar>& curveBipointOtherSeeds) const
  537. {
  538. std::vector<Proof> retval;
  539. if (!SERVER_IS_MALICIOUS)
  540. {
  541. retval.push_back(Proof("PROOF"));
  542. return retval;
  543. }
  544. Proof currProof;
  545. currProof.responseParts.push_back(twistBipointSeed);
  546. retval.push_back(currProof);
  547. currProof.responseParts.clear();
  548. currProof.responseParts.push_back(EGCiphertextSeed);
  549. retval.push_back(currProof);
  550. currProof.responseParts.clear();
  551. for (size_t i = 0; i < curveBipointSelfSeeds.size(); i++)
  552. currProof.responseParts.push_back(curveBipointSelfSeeds[i]);
  553. retval.push_back(currProof);
  554. currProof.responseParts.clear();
  555. for (size_t i = 0; i < curveBipointOtherSeeds.size(); i++)
  556. currProof.responseParts.push_back(curveBipointOtherSeeds[i]);
  557. retval.push_back(currProof);
  558. return retval;
  559. }
  560. bool PrsonaBase::verify_proof_of_added_user(
  561. const std::vector<Proof>& pi,
  562. const Curvepoint& currentFreshGenerator,
  563. const Curvepoint& shortTermPublicKey,
  564. const CurveBipoint& curveG,
  565. const CurveBipoint& curveH,
  566. const TwistBipoint& twistG,
  567. const TwistBipoint& twistH,
  568. size_t selfIndex,
  569. const EGCiphertext& userEncryptedScore,
  570. const TwistBipoint& serverEncryptedScore,
  571. const std::vector<std::vector<CurveBipoint>> encryptedVoteMatrix) const
  572. {
  573. if (pi.empty())
  574. {
  575. std::cerr << "Proof empty." << std::endl;
  576. return false;
  577. }
  578. if (!SERVER_IS_MALICIOUS)
  579. return pi[0].hbc == "PROOF";
  580. Scalar currSeed = pi[0].responseParts[0];
  581. if (serverEncryptedScore !=
  582. twistG * DEFAULT_TALLY + twistH * currSeed)
  583. {
  584. std::cerr << "Issue in server encrypted score." << std::endl;
  585. return false;
  586. }
  587. currSeed = pi[1].responseParts[0];
  588. if (userEncryptedScore.mask != shortTermPublicKey * currSeed)
  589. {
  590. std::cerr << "Issue in user encrypted score: mask." << std::endl;
  591. return false;
  592. }
  593. if (userEncryptedScore.encryptedMessage !=
  594. currentFreshGenerator * currSeed + elGamalBlindGenerator * DEFAULT_TALLY)
  595. {
  596. std::cerr << "Issue in user encrypted score: value." << std::endl;
  597. return false;
  598. }
  599. for (size_t i = 0; i < pi[2].responseParts.size(); i++)
  600. {
  601. CurveBipoint currVote = encryptedVoteMatrix[selfIndex][i];
  602. currSeed = pi[2].responseParts[i];
  603. if (i == selfIndex)
  604. {
  605. if (currVote !=
  606. curveG * Scalar(MAX_ALLOWED_VOTE) + curveH * currSeed)
  607. {
  608. std::cerr << "Issue in self vote." << std::endl;
  609. return false;
  610. }
  611. }
  612. else
  613. {
  614. if (currVote !=
  615. curveG * DEFAULT_VOTE + curveH * currSeed)
  616. {
  617. std::cerr << "Issue in vote by verifier for user " << i + 1
  618. << " of " << pi[2].responseParts.size() << "." << std::endl;
  619. return false;
  620. }
  621. }
  622. }
  623. for (size_t i = 0; i < pi[3].responseParts.size(); i++)
  624. {
  625. CurveBipoint currVote = encryptedVoteMatrix[i][selfIndex];
  626. currSeed = pi[3].responseParts[i];
  627. if (i != selfIndex)
  628. {
  629. if (currVote !=
  630. curveG * DEFAULT_VOTE + curveH * currSeed)
  631. {
  632. std::cerr << "Issue in vote for verifier by user " << i + 1
  633. << " of " << pi[3].responseParts.size() << "." << std::endl;
  634. return false;
  635. }
  636. }
  637. }
  638. return true;
  639. }
  640. /*
  641. * EPOCH PROOFS
  642. */
  643. std::vector<Proof> PrsonaBase::generate_valid_permutation_proof(
  644. const std::vector<std::vector<Scalar>>& permutations,
  645. const std::vector<std::vector<Scalar>>& seeds,
  646. const std::vector<std::vector<Curvepoint>>& commits) const
  647. {
  648. std::vector<Proof> retval;
  649. if (!SERVER_IS_MALICIOUS)
  650. {
  651. retval.push_back(Proof("PROOF"));
  652. return retval;
  653. }
  654. // Taken from Fig. 1 in https://eprint.iacr.org/2014/764.pdf
  655. for (size_t i = 0; i < permutations.size(); i++)
  656. {
  657. for (size_t j = 0; j < permutations[i].size(); j++)
  658. {
  659. Proof currProof;
  660. Curvepoint g, h, C, C_a, C_b;
  661. g = EL_GAMAL_GENERATOR;
  662. h = elGamalBlindGenerator;
  663. Scalar a, s, t, p, r;
  664. a.set_random();
  665. s.set_random();
  666. t.set_random();
  667. p = permutations[i][j];
  668. r = seeds[i][j];
  669. C = commits[i][j];
  670. C_a = g * a + h * s;
  671. C_b = g * a * p + h * t;
  672. std::stringstream oracleInput;
  673. oracleInput << g << h << C << C_a << C_b;
  674. Scalar x = oracle(oracleInput.str());
  675. currProof.challengeParts.push_back(x);
  676. Scalar f, z_a, z_b;
  677. f = p * x + a;
  678. z_a = r * x + s;
  679. z_b = r * (x - f) + t;
  680. currProof.responseParts.push_back(f);
  681. currProof.responseParts.push_back(z_a);
  682. currProof.responseParts.push_back(z_b);
  683. retval.push_back(currProof);
  684. }
  685. }
  686. return retval;
  687. }
  688. bool PrsonaBase::verify_valid_permutation_proof(
  689. const std::vector<Proof>& pi,
  690. const std::vector<std::vector<Curvepoint>>& commits) const
  691. {
  692. // Reject outright if there's no proof to check
  693. if (pi.empty())
  694. {
  695. std::cerr << "Proof was empty, aborting." << std::endl;
  696. return false;
  697. }
  698. if (!SERVER_IS_MALICIOUS)
  699. return pi[0].hbc == "PROOF";
  700. Curvepoint g, h;
  701. g = EL_GAMAL_GENERATOR;
  702. h = elGamalBlindGenerator;
  703. for (size_t i = 0; i < commits.size(); i++)
  704. {
  705. for (size_t j = 0; j < commits[i].size(); j++)
  706. {
  707. size_t piIndex = i * commits.size() + j;
  708. Curvepoint C, C_a, C_b;
  709. C = commits[i][j];
  710. Scalar x, f, z_a, z_b;
  711. x = pi[piIndex].challengeParts[0];
  712. f = pi[piIndex].responseParts[0];
  713. z_a = pi[piIndex].responseParts[1];
  714. z_b = pi[piIndex].responseParts[2];
  715. // Taken from Fig. 1 in https://eprint.iacr.org/2014/764.pdf
  716. C_a = g * f + h * z_a - C * x;
  717. C_b = h * z_b - C * (x - f);
  718. std::stringstream oracleInput;
  719. oracleInput << g << h << C << C_a << C_b;
  720. if (oracle(oracleInput.str()) != x)
  721. {
  722. std::cerr << "0 or 1 proof failed at index " << i << " of " << pi.size() - 1 << ", aborting." << std::endl;
  723. return false;
  724. }
  725. }
  726. }
  727. for (size_t i = 0; i < commits.size(); i++)
  728. {
  729. Curvepoint sum = commits[i][0];
  730. for (size_t j = 1; j < commits[i].size(); j++)
  731. sum = sum + commits[i][j];
  732. if (sum != g)
  733. {
  734. std::cerr << "Commits did not sum to g, aborting." << std::endl;
  735. return false;
  736. }
  737. }
  738. return true;
  739. }
  740. std::vector<Proof> PrsonaBase::generate_proof_of_reordering_plus_power(
  741. const std::vector<std::vector<Scalar>>& permutations,
  742. const Scalar& power,
  743. const std::vector<std::vector<Scalar>>& permutationSeeds,
  744. const std::vector<std::vector<Scalar>>& productSeeds,
  745. const std::vector<Curvepoint>& oldValues,
  746. const std::vector<std::vector<Curvepoint>>& permutationCommits,
  747. const std::vector<std::vector<Curvepoint>>& productCommits,
  748. const std::vector<std::vector<Curvepoint>>& seedCommits) const
  749. {
  750. std::vector<Proof> retval;
  751. if (!SERVER_IS_MALICIOUS)
  752. {
  753. retval.push_back(Proof("PROOF"));
  754. return retval;
  755. }
  756. Proof first;
  757. retval.push_back(first);
  758. Curvepoint g = EL_GAMAL_GENERATOR;
  759. Curvepoint h = elGamalBlindGenerator;
  760. std::stringstream oracleInput;
  761. oracleInput << g << h;
  762. for (size_t i = 0; i < permutationCommits.size(); i++)
  763. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  764. oracleInput << permutationCommits[i][j];
  765. for (size_t i = 0; i < productCommits.size(); i++)
  766. for (size_t j = 0; j < productCommits[i].size(); j++)
  767. oracleInput << productCommits[i][j];
  768. for (size_t i = 0; i < seedCommits.size(); i++)
  769. for (size_t j = 0; j < seedCommits[i].size(); j++)
  770. oracleInput << seedCommits[i][j];
  771. std::cout << "Pre: " << oracle(oracleInput.str()) << std::endl;
  772. Scalar b1;
  773. b1.set_random();
  774. std::vector<std::vector<Scalar>> b2;
  775. std::vector<std::vector<Scalar>> t1;
  776. std::vector<std::vector<Scalar>> t2;
  777. for (size_t i = 0; i < permutationCommits.size(); i++)
  778. {
  779. std::vector<Scalar> currb2Row;
  780. std::vector<Scalar> currt1Row;
  781. std::vector<Scalar> currt2Row;
  782. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  783. {
  784. Proof currProof;
  785. Scalar currb2;
  786. Scalar currt1;
  787. Scalar currt2;
  788. Curvepoint U1, U2, U3, U4;
  789. currb2.set_random();
  790. currt1.set_random();
  791. currt2.set_random();
  792. // Permutations go in weird order because of
  793. // matrix multiplication stuff
  794. U1 = g * currb2 + h * currt1;
  795. U2 = oldValues[i] *
  796. (b1 * permutations[j][i] + currb2 * power);
  797. U3 = oldValues[i] * b1 * currb2 + h * currt2;
  798. U4 = g * currt2;
  799. currProof.curvepointUniversals.push_back(U2);
  800. oracleInput << U1 << U2 << U3 << U4;
  801. std::cout << i * permutationCommits.size() + j + 1 << ": " << oracle(oracleInput.str()) << std::endl;
  802. std::stringstream out1, out2, out3, out4;
  803. out1 << U1;
  804. out2 << U2;
  805. out3 << U3;
  806. out4 << U4;
  807. std::cout << "U1: " << oracle(out1.str()) << std::endl;
  808. std::cout << "U2: " << oracle(out2.str()) << std::endl;
  809. std::cout << "U3: " << oracle(out3.str()) << std::endl;
  810. std::cout << "U4: " << oracle(out4.str()) << std::endl;
  811. currb2Row.push_back(currb2);
  812. currt1Row.push_back(currt1);
  813. currt2Row.push_back(currt2);
  814. retval.push_back(currProof);
  815. }
  816. b2.push_back(currb2Row);
  817. t1.push_back(currt1Row);
  818. t2.push_back(currt2Row);
  819. }
  820. Scalar x = oracle(oracleInput.str());
  821. retval[0].challengeParts.push_back(x);
  822. Scalar f1 = power * x + b1;
  823. retval[0].responseParts.push_back(f1);
  824. for (size_t i = 0; i < permutationCommits.size(); i++)
  825. {
  826. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  827. {
  828. size_t piIndex = i * permutationCommits.size() + j + 1;
  829. // Permutations go in weird order because of
  830. // matrix multiplication stuff
  831. Scalar f2 = permutations[j][i] * x + b2[i][j];
  832. Scalar z1 = permutationSeeds[j][i] * x + t1[i][j];
  833. Scalar z2 = productSeeds[i][j] * x * x + t2[i][j];
  834. retval[piIndex].responseParts.push_back(f2);
  835. retval[piIndex].responseParts.push_back(z1);
  836. retval[piIndex].responseParts.push_back(z2);
  837. }
  838. }
  839. return retval;
  840. }
  841. bool PrsonaBase::verify_proof_of_reordering_plus_power(
  842. const std::vector<Proof>& pi,
  843. const std::vector<Curvepoint>& oldValues,
  844. const std::vector<std::vector<Curvepoint>>& permutationCommits,
  845. const std::vector<std::vector<Curvepoint>>& productCommits,
  846. const std::vector<std::vector<Curvepoint>>& seedCommits) const
  847. {
  848. if (pi.empty())
  849. return false;
  850. if (!SERVER_IS_MALICIOUS)
  851. return pi[0].hbc == "PROOF";
  852. Curvepoint g = EL_GAMAL_GENERATOR;
  853. Curvepoint h = elGamalBlindGenerator;
  854. std::stringstream oracleInput;
  855. oracleInput << g << h;
  856. for (size_t i = 0; i < permutationCommits.size(); i++)
  857. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  858. oracleInput << permutationCommits[i][j];
  859. for (size_t i = 0; i < productCommits.size(); i++)
  860. for (size_t j = 0; j < productCommits[i].size(); j++)
  861. oracleInput << productCommits[i][j];
  862. for (size_t i = 0; i < seedCommits.size(); i++)
  863. for (size_t j = 0; j < seedCommits[i].size(); j++)
  864. oracleInput << seedCommits[i][j];
  865. std::cout << "Pre: " << oracle(oracleInput.str()) << std::endl;
  866. Scalar x = pi[0].challengeParts[0];
  867. Scalar f1 = pi[0].responseParts[0];
  868. for (size_t i = 0; i < permutationCommits.size(); i++)
  869. {
  870. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  871. {
  872. size_t piIndex = i * permutationCommits.size() + j + 1;
  873. Curvepoint U1, U2, U3, U4;
  874. U2 = pi[piIndex].curvepointUniversals[0];
  875. Scalar f2 = pi[piIndex].responseParts[0];
  876. Scalar z1 = pi[piIndex].responseParts[1];
  877. Scalar z2 = pi[piIndex].responseParts[2];
  878. // Permutations go in weird order because of
  879. // matrix multiplication stuff
  880. U1 = g * f2 + h * z1 - permutationCommits[j][i] * x;
  881. U3 = oldValues[i] * f1 * f2 +
  882. h * z2 -
  883. productCommits[i][j] * x * x -
  884. U2 * x;
  885. U4 = g * z2 - seedCommits[i][j] * x * x;
  886. oracleInput << U1 << U2 << U3 << U4;
  887. std::cout << i * permutationCommits.size() + j + 1 << ": " << oracle(oracleInput.str()) << std::endl;
  888. std::stringstream out1, out2, out3, out4;
  889. out1 << U1;
  890. out2 << U2;
  891. out3 << U3;
  892. out4 << U4;
  893. std::cout << "U1: " << oracle(out1.str()) << std::endl;
  894. std::cout << "U2: " << oracle(out2.str()) << std::endl;
  895. std::cout << "U3: " << oracle(out3.str()) << std::endl;
  896. std::cout << "U4: " << oracle(out4.str()) << std::endl;
  897. }
  898. }
  899. if (x != oracle(oracleInput.str()))
  900. {
  901. std::cerr << "Fresh pseudonyms not generated by permutation matrix." << std::endl;
  902. return false;
  903. }
  904. for (size_t i = 0; i < seedCommits.size(); i++)
  905. {
  906. Curvepoint sum = seedCommits[i][0];
  907. for (size_t j = 1; j < seedCommits[i].size(); j++)
  908. sum = sum + seedCommits[i][j];
  909. if (sum != Curvepoint())
  910. {
  911. std::cerr << "seed commits did not sum to 0, aborting." << std::endl;
  912. return false;
  913. }
  914. }
  915. return true;
  916. }
  917. template <typename T>
  918. std::vector<Proof> PrsonaBase::generate_proof_of_reordering(
  919. const std::vector<std::vector<Scalar>>& permutations,
  920. const std::vector<std::vector<Scalar>>& permutationSeeds,
  921. const std::vector<std::vector<Scalar>>& productSeeds,
  922. const std::vector<T>& oldValues,
  923. const std::vector<std::vector<Curvepoint>>& permutationCommits,
  924. const std::vector<std::vector<T>>& productCommits,
  925. const T& otherG,
  926. const T& otherH,
  927. bool inverted) const
  928. {
  929. std::vector<Proof> retval;
  930. if (!SERVER_IS_MALICIOUS)
  931. {
  932. retval.push_back(Proof("PROOF"));
  933. return retval;
  934. }
  935. Proof first;
  936. retval.push_back(first);
  937. Curvepoint g = EL_GAMAL_GENERATOR;
  938. Curvepoint h = elGamalBlindGenerator;
  939. std::stringstream oracleInput;
  940. oracleInput << g << h << otherG << otherH;
  941. for (size_t i = 0; i < permutationCommits.size(); i++)
  942. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  943. oracleInput << permutationCommits[i][j];
  944. for (size_t i = 0; i < productCommits.size(); i++)
  945. for (size_t j = 0; j < productCommits[i].size(); j++)
  946. oracleInput << productCommits[i][j];
  947. std::vector<std::vector<Scalar>> a;
  948. std::vector<std::vector<Scalar>> t1;
  949. std::vector<std::vector<Scalar>> t2;
  950. for (size_t i = 0; i < permutationCommits.size(); i++)
  951. {
  952. std::vector<Scalar> curraRow;
  953. std::vector<Scalar> currt1Row;
  954. std::vector<Scalar> currt2Row;
  955. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  956. {
  957. Proof currProof;
  958. Scalar curra;
  959. Scalar currt1;
  960. Scalar currt2;
  961. Curvepoint U1;
  962. T U2;
  963. curra.set_random();
  964. currt1.set_random();
  965. currt2.set_random();
  966. U1 = g * curra + h * currt1;
  967. U2 = oldValues[i] * curra + otherH * currt2;
  968. oracleInput << U1 << U2;
  969. curraRow.push_back(curra);
  970. currt1Row.push_back(currt1);
  971. currt2Row.push_back(currt2);
  972. retval.push_back(currProof);
  973. }
  974. a.push_back(curraRow);
  975. t1.push_back(currt1Row);
  976. t2.push_back(currt2Row);
  977. }
  978. Scalar x = oracle(oracleInput.str());
  979. retval[0].challengeParts.push_back(x);
  980. for (size_t i = 0; i < permutationCommits.size(); i++)
  981. {
  982. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  983. {
  984. size_t piIndex = i * permutationCommits.size() + j + 1;
  985. size_t permI, permJ;
  986. // Permutations normally go in weird order because of
  987. // matrix multiplication stuff
  988. if (inverted)
  989. {
  990. permI = i;
  991. permJ = j;
  992. }
  993. else
  994. {
  995. permI = j;
  996. permJ = i;
  997. }
  998. Scalar f = permutations[permI][permJ] * x + a[i][j];
  999. Scalar z1 = permutationSeeds[permI][permJ] * x + t1[i][j];
  1000. Scalar z2 = productSeeds[i][j] * x + t2[i][j];
  1001. retval[piIndex].responseParts.push_back(f);
  1002. retval[piIndex].responseParts.push_back(z1);
  1003. retval[piIndex].responseParts.push_back(z2);
  1004. }
  1005. }
  1006. return retval;
  1007. }
  1008. template <typename T>
  1009. bool PrsonaBase::verify_proof_of_reordering(
  1010. const std::vector<Proof>& pi,
  1011. const std::vector<T>& oldValues,
  1012. const std::vector<std::vector<Curvepoint>>& permutationCommits,
  1013. const std::vector<std::vector<T>>& productCommits,
  1014. const T& otherG,
  1015. const T& otherH,
  1016. bool inverted) const
  1017. {
  1018. if (pi.empty())
  1019. return false;
  1020. if (!SERVER_IS_MALICIOUS)
  1021. return pi[0].hbc == "PROOF";
  1022. Curvepoint g = EL_GAMAL_GENERATOR;
  1023. Curvepoint h = elGamalBlindGenerator;
  1024. std::stringstream oracleInput;
  1025. oracleInput << g << h << otherG << otherH;
  1026. for (size_t i = 0; i < permutationCommits.size(); i++)
  1027. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1028. oracleInput << permutationCommits[i][j];
  1029. for (size_t i = 0; i < productCommits.size(); i++)
  1030. for (size_t j = 0; j < productCommits[i].size(); j++)
  1031. oracleInput << productCommits[i][j];
  1032. Scalar x = pi[0].challengeParts[0];
  1033. for (size_t i = 0; i < permutationCommits.size(); i++)
  1034. {
  1035. for (size_t j = 0; j < permutationCommits[i].size(); j++)
  1036. {
  1037. size_t piIndex = i * permutationCommits.size() + j + 1;
  1038. Curvepoint U1;
  1039. T U2;
  1040. Scalar f = pi[piIndex].responseParts[0];
  1041. Scalar z1 = pi[piIndex].responseParts[1];
  1042. Scalar z2 = pi[piIndex].responseParts[2];
  1043. size_t permI, permJ;
  1044. // Permutations normally go in weird order because of
  1045. // matrix multiplication stuff
  1046. if (inverted)
  1047. {
  1048. permI = i;
  1049. permJ = j;
  1050. }
  1051. else
  1052. {
  1053. permI = j;
  1054. permJ = i;
  1055. }
  1056. U1 = g * f + h * z1 -
  1057. permutationCommits[permI][permJ] * x;
  1058. U2 = oldValues[i] * f + otherH * z2 -
  1059. productCommits[i][j] * x;
  1060. oracleInput << U1 << U2;
  1061. }
  1062. }
  1063. if (x != oracle(oracleInput.str()))
  1064. {
  1065. std::cerr << "Fresh pseudonyms not generated by permutation matrix." << std::endl;
  1066. return false;
  1067. }
  1068. return true;
  1069. }
  1070. /*
  1071. * SERVER AGREEMENT PROOFS
  1072. */
  1073. Proof PrsonaBase::generate_valid_vote_row_proof(
  1074. const std::vector<CurveBipoint>& commitment) const
  1075. {
  1076. Proof retval;
  1077. if (!SERVER_IS_MALICIOUS)
  1078. {
  1079. retval.hbc = "PROOF";
  1080. return retval;
  1081. }
  1082. std::stringstream oracleInput;
  1083. for (size_t i = 0; i < commitment.size(); i++)
  1084. oracleInput << commitment[i];
  1085. Scalar val = oracle(oracleInput.str());
  1086. retval.responseParts.push_back(val);
  1087. return retval;
  1088. }
  1089. Proof PrsonaBase::generate_valid_vote_matrix_proof(
  1090. const std::vector<std::vector<CurveBipoint>>& commitment) const
  1091. {
  1092. Proof retval;
  1093. if (!SERVER_IS_MALICIOUS)
  1094. {
  1095. retval.hbc = "PROOF";
  1096. return retval;
  1097. }
  1098. std::stringstream oracleInput;
  1099. for (size_t i = 0; i < commitment.size(); i++)
  1100. for (size_t j = 0; j < commitment[i].size(); j++)
  1101. oracleInput << commitment[i][j];
  1102. Scalar val = oracle(oracleInput.str());
  1103. retval.responseParts.push_back(val);
  1104. return retval;
  1105. }
  1106. Proof PrsonaBase::generate_valid_user_tally_proof(
  1107. const EGCiphertext& commitment) const
  1108. {
  1109. Proof retval;
  1110. if (!SERVER_IS_MALICIOUS)
  1111. {
  1112. retval.hbc = "PROOF";
  1113. return retval;
  1114. }
  1115. std::stringstream oracleInput;
  1116. oracleInput << commitment;
  1117. Scalar val = oracle(oracleInput.str());
  1118. retval.responseParts.push_back(val);
  1119. return retval;
  1120. }
  1121. Proof PrsonaBase::generate_valid_server_tally_proof(
  1122. const TwistBipoint& commitment) const
  1123. {
  1124. Proof retval;
  1125. if (!SERVER_IS_MALICIOUS)
  1126. {
  1127. retval.hbc = "PROOF";
  1128. return retval;
  1129. }
  1130. std::stringstream oracleInput;
  1131. oracleInput << commitment;
  1132. Scalar val = oracle(oracleInput.str());
  1133. retval.responseParts.push_back(val);
  1134. return retval;
  1135. }
  1136. Proof PrsonaBase::generate_valid_pseudonyms_proof(
  1137. const std::vector<Curvepoint>& commitment) const
  1138. {
  1139. Proof retval;
  1140. if (!SERVER_IS_MALICIOUS)
  1141. {
  1142. retval.hbc = "PROOF";
  1143. return retval;
  1144. }
  1145. std::stringstream oracleInput;
  1146. for (size_t i = 0; i < commitment.size(); i++)
  1147. oracleInput << commitment[i];
  1148. Scalar val = oracle(oracleInput.str());
  1149. retval.responseParts.push_back(val);
  1150. return retval;
  1151. }
  1152. bool PrsonaBase::verify_valid_vote_row_proof(
  1153. const std::vector<Proof>& pi,
  1154. const std::vector<CurveBipoint>& commitment) const
  1155. {
  1156. if (pi.empty())
  1157. return false;
  1158. if (!SERVER_IS_MALICIOUS)
  1159. return pi[0].hbc == "PROOF";
  1160. Scalar comparison = pi[0].responseParts[0];
  1161. std::stringstream oracleInput;
  1162. for (size_t i = 0; i < commitment.size(); i++)
  1163. oracleInput << commitment[i];
  1164. if (oracle(oracleInput.str()) != comparison)
  1165. {
  1166. std::cerr << "Server's claimed value doesn't match their own commitment." << std::endl;
  1167. return false;
  1168. }
  1169. size_t agreement = 1;
  1170. for (size_t i = 1; i < pi.size(); i++)
  1171. if (comparison == pi[i].responseParts[0])
  1172. agreement++;
  1173. return agreement * 2 > pi.size();
  1174. }
  1175. bool PrsonaBase::verify_valid_vote_matrix_proof(
  1176. const std::vector<Proof>& pi,
  1177. const std::vector<std::vector<CurveBipoint>>& commitment) const
  1178. {
  1179. if (pi.empty())
  1180. return false;
  1181. if (!SERVER_IS_MALICIOUS)
  1182. return pi[0].hbc == "PROOF";
  1183. Scalar comparison = pi[0].responseParts[0];
  1184. std::stringstream oracleInput;
  1185. for (size_t i = 0; i < commitment.size(); i++)
  1186. for (size_t j = 0; j < commitment[i].size(); j++)
  1187. oracleInput << commitment[i][j];
  1188. if (oracle(oracleInput.str()) != comparison)
  1189. {
  1190. std::cerr << "Server's claimed value doesn't match their own commitment." << std::endl;
  1191. return false;
  1192. }
  1193. size_t agreement = 1;
  1194. for (size_t i = 1; i < pi.size(); i++)
  1195. if (comparison == pi[i].responseParts[0])
  1196. agreement++;
  1197. return agreement * 2 > pi.size();
  1198. }
  1199. bool PrsonaBase::verify_valid_user_tally_proof(
  1200. const std::vector<Proof>& pi,
  1201. const EGCiphertext& commitment) const
  1202. {
  1203. if (pi.empty())
  1204. return false;
  1205. if (!SERVER_IS_MALICIOUS)
  1206. return pi[0].hbc == "PROOF";
  1207. Scalar comparison = pi[0].responseParts[0];
  1208. std::stringstream oracleInput;
  1209. oracleInput << commitment;
  1210. if (oracle(oracleInput.str()) != comparison)
  1211. {
  1212. std::cerr << "Server's claimed value doesn't match their own commitment." << std::endl;
  1213. return false;
  1214. }
  1215. size_t agreement = 1;
  1216. for (size_t i = 1; i < pi.size(); i++)
  1217. if (comparison == pi[i].responseParts[0])
  1218. agreement++;
  1219. return agreement * 2 > pi.size();
  1220. }
  1221. bool PrsonaBase::verify_valid_server_tally_proof(
  1222. const std::vector<Proof>& pi,
  1223. const TwistBipoint& commitment) const
  1224. {
  1225. if (pi.empty())
  1226. return false;
  1227. if (!SERVER_IS_MALICIOUS)
  1228. return pi[0].hbc == "PROOF";
  1229. Scalar comparison = pi[0].responseParts[0];
  1230. std::stringstream oracleInput;
  1231. oracleInput << commitment;
  1232. if (oracle(oracleInput.str()) != comparison)
  1233. {
  1234. std::cerr << "Server's claimed value doesn't match their own commitment." << std::endl;
  1235. return false;
  1236. }
  1237. size_t agreement = 1;
  1238. for (size_t i = 1; i < pi.size(); i++)
  1239. if (comparison == pi[i].responseParts[0])
  1240. agreement++;
  1241. return agreement * 2 > pi.size();
  1242. }
  1243. bool PrsonaBase::verify_valid_pseudonyms_proof(
  1244. const std::vector<Proof>& pi,
  1245. const std::vector<Curvepoint>& commitment) const
  1246. {
  1247. if (pi.empty())
  1248. return false;
  1249. if (!SERVER_IS_MALICIOUS)
  1250. return pi[0].hbc == "PROOF";
  1251. Scalar comparison = pi[0].responseParts[0];
  1252. std::stringstream oracleInput;
  1253. for (size_t i = 0; i < commitment.size(); i++)
  1254. oracleInput << commitment[i];
  1255. if (oracle(oracleInput.str()) != comparison)
  1256. {
  1257. std::cerr << "Server's claimed value doesn't match their own commitment." << std::endl;
  1258. return false;
  1259. }
  1260. size_t agreement = 1;
  1261. for (size_t i = 1; i < pi.size(); i++)
  1262. if (comparison == pi[i].responseParts[0])
  1263. agreement++;
  1264. return agreement * 2 > pi.size();
  1265. }
  1266. template std::vector<Proof> PrsonaBase::generate_proof_of_reordering<Curvepoint>(
  1267. const std::vector<std::vector<Scalar>>& permutations,
  1268. const std::vector<std::vector<Scalar>>& permutationSeeds,
  1269. const std::vector<std::vector<Scalar>>& productSeeds,
  1270. const std::vector<Curvepoint>& oldValues,
  1271. const std::vector<std::vector<Curvepoint>>& permutationCommits,
  1272. const std::vector<std::vector<Curvepoint>>& productCommits,
  1273. const Curvepoint& otherG,
  1274. const Curvepoint& otherH,
  1275. bool inverted) const;
  1276. template std::vector<Proof> PrsonaBase::generate_proof_of_reordering<CurveBipoint>(
  1277. const std::vector<std::vector<Scalar>>& permutations,
  1278. const std::vector<std::vector<Scalar>>& permutationSeeds,
  1279. const std::vector<std::vector<Scalar>>& productSeeds,
  1280. const std::vector<CurveBipoint>& oldValues,
  1281. const std::vector<std::vector<Curvepoint>>& permutationCommits,
  1282. const std::vector<std::vector<CurveBipoint>>& productCommits,
  1283. const CurveBipoint& otherG,
  1284. const CurveBipoint& otherH,
  1285. bool inverted) const;
  1286. template std::vector<Proof> PrsonaBase::generate_proof_of_reordering<TwistBipoint>(
  1287. const std::vector<std::vector<Scalar>>& permutations,
  1288. const std::vector<std::vector<Scalar>>& permutationSeeds,
  1289. const std::vector<std::vector<Scalar>>& productSeeds,
  1290. const std::vector<TwistBipoint>& oldValues,
  1291. const std::vector<std::vector<Curvepoint>>& permutationCommits,
  1292. const std::vector<std::vector<TwistBipoint>>& productCommits,
  1293. const TwistBipoint& otherG,
  1294. const TwistBipoint& otherH,
  1295. bool inverted) const;
  1296. template bool PrsonaBase::verify_proof_of_reordering<Curvepoint>(
  1297. const std::vector<Proof>& pi,
  1298. const std::vector<Curvepoint>& oldValues,
  1299. const std::vector<std::vector<Curvepoint>>& permutationCommits,
  1300. const std::vector<std::vector<Curvepoint>>& productCommits,
  1301. const Curvepoint& otherG,
  1302. const Curvepoint& otherH,
  1303. bool inverted) const;
  1304. template bool PrsonaBase::verify_proof_of_reordering<CurveBipoint>(
  1305. const std::vector<Proof>& pi,
  1306. const std::vector<CurveBipoint>& oldValues,
  1307. const std::vector<std::vector<Curvepoint>>& permutationCommits,
  1308. const std::vector<std::vector<CurveBipoint>>& productCommits,
  1309. const CurveBipoint& otherG,
  1310. const CurveBipoint& otherH,
  1311. bool inverted) const;
  1312. template bool PrsonaBase::verify_proof_of_reordering<TwistBipoint>(
  1313. const std::vector<Proof>& pi,
  1314. const std::vector<TwistBipoint>& oldValues,
  1315. const std::vector<std::vector<Curvepoint>>& permutationCommits,
  1316. const std::vector<std::vector<TwistBipoint>>& productCommits,
  1317. const TwistBipoint& otherG,
  1318. const TwistBipoint& otherH,
  1319. bool inverted) const;