main.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. #include <iostream>
  2. #include <random>
  3. #include <chrono>
  4. #include "BGN.hpp"
  5. using namespace std;
  6. const size_t NUM_RUNS_PER_TEST = 100;
  7. const size_t MAX_VALUE_IN_TEST = 999;
  8. bool testDecrypt(int x)
  9. {
  10. bool retval;
  11. BGN system;
  12. Scalar testVal(x);
  13. Scalar one(1);
  14. Scalar decrypted;
  15. CurveBipoint curveEnc, curveOne;
  16. TwistBipoint twistEnc, twistOne;
  17. Quadripoint quadEncA, quadEncB;
  18. system.encrypt(curveEnc, testVal);
  19. system.encrypt(curveOne, one);
  20. system.encrypt(twistEnc, testVal);
  21. system.encrypt(twistOne, one);
  22. quadEncA = system.homomorphic_multiplication(curveEnc, twistOne);
  23. quadEncB = system.homomorphic_multiplication(curveOne, twistEnc);
  24. decrypted = system.decrypt(curveEnc);
  25. retval = (decrypted == testVal);
  26. decrypted = system.decrypt(twistEnc);
  27. retval = retval && (decrypted == testVal);
  28. decrypted = system.decrypt(quadEncA);
  29. retval = retval && (decrypted == testVal);
  30. decrypted = system.decrypt(quadEncB);
  31. retval = retval && (decrypted == testVal);
  32. return retval;
  33. }
  34. double testCurveEncryptSpeed(default_random_engine& generator)
  35. {
  36. BGN system;
  37. uniform_int_distribution<int> distribution(0, MAX_VALUE_IN_TEST);
  38. vector<Scalar> testVals;
  39. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  40. testVals.push_back(Scalar(distribution(generator)));
  41. vector<CurveBipoint> encryptions(NUM_RUNS_PER_TEST);
  42. chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
  43. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  44. system.encrypt(encryptions[i], testVals[i]);
  45. chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
  46. chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t1 - t0);
  47. return time_span.count();
  48. }
  49. double testTwistEncryptSpeed(default_random_engine& generator)
  50. {
  51. BGN system;
  52. uniform_int_distribution<int> distribution(0, MAX_VALUE_IN_TEST);
  53. vector<Scalar> testVals;
  54. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  55. testVals.push_back(Scalar(distribution(generator)));
  56. vector<TwistBipoint> encryptions(NUM_RUNS_PER_TEST);
  57. chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
  58. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  59. system.encrypt(encryptions[i], testVals[i]);
  60. chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
  61. chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t1 - t0);
  62. return time_span.count();
  63. }
  64. double testCurveDecryptSpeed(default_random_engine& generator)
  65. {
  66. BGN system;
  67. uniform_int_distribution<int> distribution(0, MAX_VALUE_IN_TEST);
  68. vector<Scalar> testVals;
  69. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  70. testVals.push_back(Scalar(distribution(generator)));
  71. vector<CurveBipoint> encryptions(NUM_RUNS_PER_TEST);
  72. vector<Scalar> decryptions(NUM_RUNS_PER_TEST);
  73. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  74. system.encrypt(encryptions[i], testVals[i]);
  75. chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
  76. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  77. decryptions[i] = system.decrypt(encryptions[i]);
  78. chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
  79. chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t1 - t0);
  80. return time_span.count();
  81. }
  82. double testTwistDecryptSpeed(default_random_engine& generator)
  83. {
  84. BGN system;
  85. uniform_int_distribution<int> distribution(0, MAX_VALUE_IN_TEST);
  86. vector<Scalar> testVals;
  87. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  88. testVals.push_back(Scalar(distribution(generator)));
  89. vector<TwistBipoint> encryptions(NUM_RUNS_PER_TEST);
  90. vector<Scalar> decryptions(NUM_RUNS_PER_TEST);
  91. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  92. system.encrypt(encryptions[i], testVals[i]);
  93. chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
  94. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  95. decryptions[i] = system.decrypt(encryptions[i]);
  96. chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
  97. chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t1 - t0);
  98. return time_span.count();
  99. }
  100. double testQuadDecryptSpeed(default_random_engine& generator)
  101. {
  102. BGN system;
  103. uniform_int_distribution<int> distribution(0, MAX_VALUE_IN_TEST);
  104. vector<Scalar> testVals;
  105. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  106. testVals.push_back(Scalar(distribution(generator)));
  107. Scalar one(1);
  108. TwistBipoint oneEncryption;
  109. vector<CurveBipoint> firstEncryptions(NUM_RUNS_PER_TEST);
  110. vector<Quadripoint> realEncryptions(NUM_RUNS_PER_TEST);
  111. vector<Scalar> decryptions(NUM_RUNS_PER_TEST);
  112. system.encrypt(oneEncryption, one);
  113. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  114. {
  115. system.encrypt(firstEncryptions[i], testVals[i]);
  116. realEncryptions[i] = system.homomorphic_multiplication(firstEncryptions[i], oneEncryption);
  117. }
  118. chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
  119. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  120. decryptions[i] = system.decrypt(realEncryptions[i]);
  121. chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
  122. chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t1 - t0);
  123. return time_span.count();
  124. }
  125. double testCurveDecryptSpeed(size_t max)
  126. {
  127. BGN system;
  128. vector<Scalar> testVals;
  129. for (size_t i = 0; i < max; i++)
  130. testVals.push_back(Scalar(i));
  131. vector<CurveBipoint> encryptions(max);
  132. vector<Scalar> decryptions(max);
  133. for (size_t i = 0; i < max; i++)
  134. system.encrypt(encryptions[i], testVals[i]);
  135. chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
  136. for (size_t i = 0; i < max; i++)
  137. decryptions[i] = system.decrypt(encryptions[i]);
  138. chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
  139. chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t1 - t0);
  140. return time_span.count();
  141. }
  142. double testTwistDecryptSpeed(size_t max)
  143. {
  144. BGN system;
  145. vector<Scalar> testVals;
  146. for (size_t i = 0; i < max; i++)
  147. testVals.push_back(Scalar(i));
  148. vector<TwistBipoint> encryptions(max);
  149. vector<Scalar> decryptions(max);
  150. for (size_t i = 0; i < max; i++)
  151. system.encrypt(encryptions[i], testVals[i]);
  152. chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
  153. for (size_t i = 0; i < max; i++)
  154. decryptions[i] = system.decrypt(encryptions[i]);
  155. chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
  156. chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t1 - t0);
  157. return time_span.count();
  158. }
  159. double testQuadDecryptSpeed(size_t max)
  160. {
  161. BGN system;
  162. vector<Scalar> testVals;
  163. for (size_t i = 0; i < max; i++)
  164. testVals.push_back(Scalar(i));
  165. Scalar one(1);
  166. TwistBipoint oneEncryption;
  167. vector<CurveBipoint> firstEncryptions(max);
  168. vector<Quadripoint> realEncryptions(max);
  169. vector<Scalar> decryptions(max);
  170. system.encrypt(oneEncryption, one);
  171. for (size_t i = 0; i < max; i++)
  172. {
  173. system.encrypt(firstEncryptions[i], testVals[i]);
  174. realEncryptions[i] = system.homomorphic_multiplication(firstEncryptions[i], oneEncryption);
  175. }
  176. chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
  177. for (size_t i = 0; i < max; i++)
  178. decryptions[i] = system.decrypt(realEncryptions[i]);
  179. chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
  180. chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t1 - t0);
  181. return time_span.count();
  182. }
  183. bool testAddition(int x, int y)
  184. {
  185. bool retval;
  186. BGN system;
  187. Scalar testX(x);
  188. Scalar testY(y);
  189. Scalar testSum(x + y);
  190. Scalar one(1);
  191. Scalar decrypted;
  192. CurveBipoint curveX, curveY, curveSum, curveOne;
  193. TwistBipoint twistX, twistY, twistSum, twistOne;
  194. Quadripoint quadXA, quadXB, quadYA, quadYB,
  195. quadSumAA, quadSumAB, quadSumBA, quadSumBB;
  196. system.encrypt(curveX, testX);
  197. system.encrypt(curveY, testY);
  198. system.encrypt(curveOne, one);
  199. system.encrypt(twistX, testX);
  200. system.encrypt(twistY, testY);
  201. system.encrypt(twistOne, one);
  202. curveSum = system.homomorphic_addition(curveX, curveY);
  203. twistSum = system.homomorphic_addition(twistX, twistY);
  204. quadXA = system.homomorphic_multiplication(curveX, twistOne);
  205. quadXB = system.homomorphic_multiplication(curveOne, twistX);
  206. quadYA = system.homomorphic_multiplication(curveY, twistOne);
  207. quadYB = system.homomorphic_multiplication(curveOne, twistY);
  208. quadSumAA = system.homomorphic_addition(quadXA, quadYA);
  209. quadSumAB = system.homomorphic_addition(quadXA, quadYB);
  210. quadSumBA = system.homomorphic_addition(quadXB, quadYA);
  211. quadSumBB = system.homomorphic_addition(quadXB, quadYB);
  212. decrypted = system.decrypt(curveSum);
  213. retval = (decrypted == testSum);
  214. decrypted = system.decrypt(twistSum);
  215. retval = retval && (decrypted == testSum);
  216. decrypted = system.decrypt(quadSumAA);
  217. retval = retval && (decrypted == testSum);
  218. decrypted = system.decrypt(quadSumAB);
  219. retval = retval && (decrypted == testSum);
  220. decrypted = system.decrypt(quadSumBA);
  221. retval = retval && (decrypted == testSum);
  222. decrypted = system.decrypt(quadSumBB);
  223. retval = retval && (decrypted == testSum);
  224. return retval;
  225. }
  226. double testCurveAdditionSpeed(default_random_engine& generator)
  227. {
  228. BGN system;
  229. uniform_int_distribution<int> distribution(0, MAX_VALUE_IN_TEST);
  230. vector<Scalar> testXs;
  231. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  232. testXs.push_back(Scalar(distribution(generator)));
  233. vector<Scalar> testYs;
  234. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  235. testYs.push_back(Scalar(distribution(generator)));
  236. vector<CurveBipoint> encXs(NUM_RUNS_PER_TEST);
  237. vector<CurveBipoint> encYs(NUM_RUNS_PER_TEST);
  238. vector<CurveBipoint> encSums(NUM_RUNS_PER_TEST);
  239. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  240. {
  241. system.encrypt(encXs[i], testXs[i]);
  242. system.encrypt(encYs[i], testYs[i]);
  243. }
  244. chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
  245. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  246. encSums[i] = system.homomorphic_addition_no_rerandomize(encXs[i], encYs[i]);
  247. chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
  248. chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t1 - t0);
  249. return time_span.count();
  250. }
  251. double testTwistAdditionSpeed(default_random_engine& generator)
  252. {
  253. BGN system;
  254. uniform_int_distribution<int> distribution(0, MAX_VALUE_IN_TEST);
  255. vector<Scalar> testXs;
  256. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  257. testXs.push_back(Scalar(distribution(generator)));
  258. vector<Scalar> testYs;
  259. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  260. testYs.push_back(Scalar(distribution(generator)));
  261. vector<TwistBipoint> encXs(NUM_RUNS_PER_TEST);
  262. vector<TwistBipoint> encYs(NUM_RUNS_PER_TEST);
  263. vector<TwistBipoint> encSums(NUM_RUNS_PER_TEST);
  264. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  265. {
  266. system.encrypt(encXs[i], testXs[i]);
  267. system.encrypt(encYs[i], testYs[i]);
  268. }
  269. chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
  270. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  271. encSums[i] = system.homomorphic_addition_no_rerandomize(encXs[i], encYs[i]);
  272. chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
  273. chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t1 - t0);
  274. return time_span.count();
  275. }
  276. double testQuadAdditionSpeed(default_random_engine& generator)
  277. {
  278. BGN system;
  279. uniform_int_distribution<int> distribution(0, MAX_VALUE_IN_TEST);
  280. vector<Scalar> testXs;
  281. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  282. testXs.push_back(Scalar(distribution(generator)));
  283. vector<Scalar> testYs;
  284. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  285. testYs.push_back(Scalar(distribution(generator)));
  286. Scalar one(1);
  287. TwistBipoint oneEncryption;
  288. vector<CurveBipoint> firstEncXs(NUM_RUNS_PER_TEST);
  289. vector<Quadripoint> realEncXs(NUM_RUNS_PER_TEST);
  290. vector<CurveBipoint> firstEncYs(NUM_RUNS_PER_TEST);
  291. vector<Quadripoint> realEncYs(NUM_RUNS_PER_TEST);
  292. vector<Quadripoint> encSums(NUM_RUNS_PER_TEST);
  293. system.encrypt(oneEncryption, one);
  294. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  295. {
  296. system.encrypt(firstEncXs[i], testXs[i]);
  297. system.encrypt(firstEncYs[i], testYs[i]);
  298. realEncXs[i] = system.homomorphic_multiplication(firstEncXs[i], oneEncryption);
  299. realEncYs[i] = system.homomorphic_multiplication(firstEncYs[i], oneEncryption);
  300. }
  301. chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
  302. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  303. encSums[i] = system.homomorphic_addition_no_rerandomize(realEncXs[i], realEncYs[i]);
  304. chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
  305. chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t1 - t0);
  306. return time_span.count();
  307. }
  308. bool testMultiplication(int x, int y)
  309. {
  310. bool retval;
  311. BGN system;
  312. Scalar testX(x);
  313. Scalar testY(y);
  314. Scalar testProduct(x * y);
  315. Scalar decrypted;
  316. CurveBipoint curveX, curveY;
  317. TwistBipoint twistX, twistY;
  318. Quadripoint productA, productB;
  319. system.encrypt(curveX, testX);
  320. system.encrypt(curveY, testY);
  321. system.encrypt(twistX, testX);
  322. system.encrypt(twistY, testY);
  323. productA = system.homomorphic_multiplication(curveX, twistY);
  324. productB = system.homomorphic_multiplication(curveY, twistX);
  325. decrypted = system.decrypt(productA);
  326. retval = (decrypted == testProduct);
  327. decrypted = system.decrypt(productB);
  328. retval = retval && (decrypted == testProduct);
  329. return retval;
  330. }
  331. double testMultiplicationSpeed(default_random_engine& generator)
  332. {
  333. BGN system;
  334. uniform_int_distribution<int> distribution(0, MAX_VALUE_IN_TEST);
  335. vector<Scalar> testXs;
  336. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  337. testXs.push_back(Scalar(distribution(generator)));
  338. vector<Scalar> testYs;
  339. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  340. testYs.push_back(Scalar(distribution(generator)));
  341. vector<CurveBipoint> encXs(NUM_RUNS_PER_TEST);
  342. vector<TwistBipoint> encYs(NUM_RUNS_PER_TEST);
  343. vector<Quadripoint> encProducts(NUM_RUNS_PER_TEST);
  344. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  345. {
  346. system.encrypt(encXs[i], testXs[i]);
  347. system.encrypt(encYs[i], testYs[i]);
  348. }
  349. chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
  350. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  351. encProducts[i] = system.homomorphic_multiplication_no_rerandomize(encXs[i], encYs[i]);
  352. chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
  353. chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t1 - t0);
  354. return time_span.count();
  355. }
  356. bool testRerandomize(int x)
  357. {
  358. bool retval;
  359. BGN system;
  360. Scalar testVal(x);
  361. Scalar one(1);
  362. Scalar decrypted;
  363. CurveBipoint curveEnc, curveRand, curveOne;
  364. TwistBipoint twistEnc, twistRand, twistOne;
  365. Quadripoint quadEncA, quadEncB, quadRandA, quadRandB;
  366. system.encrypt(curveEnc, testVal);
  367. system.encrypt(curveOne, one);
  368. system.encrypt(twistEnc, testVal);
  369. system.encrypt(twistOne, one);
  370. quadEncA = system.homomorphic_multiplication(curveEnc, twistOne);
  371. quadEncB = system.homomorphic_multiplication(curveOne, twistEnc);
  372. curveRand = system.rerandomize(curveEnc);
  373. twistRand = system.rerandomize(twistEnc);
  374. quadRandA = system.rerandomize(quadEncA);
  375. quadRandB = system.rerandomize(quadEncB);
  376. decrypted = system.decrypt(curveRand);
  377. retval = (decrypted == testVal);
  378. decrypted = system.decrypt(twistRand);
  379. retval = retval && (decrypted == testVal);
  380. decrypted = system.decrypt(quadRandA);
  381. retval = retval && (decrypted == testVal);
  382. decrypted = system.decrypt(quadRandB);
  383. retval = retval && (decrypted == testVal);
  384. return retval;
  385. }
  386. double testCurveRerandomizeSpeed(default_random_engine& generator)
  387. {
  388. BGN system;
  389. uniform_int_distribution<int> distribution(0, MAX_VALUE_IN_TEST);
  390. vector<Scalar> testVals;
  391. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  392. testVals.push_back(Scalar(distribution(generator)));
  393. vector<CurveBipoint> encryptions(NUM_RUNS_PER_TEST);
  394. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  395. system.encrypt(encryptions[i], testVals[i]);
  396. vector<CurveBipoint> rerandomizations(NUM_RUNS_PER_TEST);
  397. chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
  398. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  399. rerandomizations[i] = system.rerandomize(encryptions[i]);
  400. chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
  401. chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t1 - t0);
  402. return time_span.count();
  403. }
  404. double testTwistRerandomizeSpeed(default_random_engine& generator)
  405. {
  406. BGN system;
  407. uniform_int_distribution<int> distribution(0, MAX_VALUE_IN_TEST);
  408. vector<Scalar> testVals;
  409. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  410. testVals.push_back(Scalar(distribution(generator)));
  411. vector<TwistBipoint> encryptions(NUM_RUNS_PER_TEST);
  412. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  413. system.encrypt(encryptions[i], testVals[i]);
  414. vector<TwistBipoint> rerandomizations(NUM_RUNS_PER_TEST);
  415. chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
  416. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  417. rerandomizations[i] = system.rerandomize(encryptions[i]);
  418. chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
  419. chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t1 - t0);
  420. return time_span.count();
  421. }
  422. double testQuadRerandomizeSpeed(default_random_engine& generator)
  423. {
  424. BGN system;
  425. uniform_int_distribution<int> distribution(0, MAX_VALUE_IN_TEST);
  426. vector<Scalar> testVals;
  427. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  428. testVals.push_back(Scalar(distribution(generator)));
  429. Scalar one(1);
  430. TwistBipoint oneEncryption;
  431. vector<CurveBipoint> firstEncryptions(NUM_RUNS_PER_TEST);
  432. vector<Quadripoint> realEncryptions(NUM_RUNS_PER_TEST);
  433. vector<Quadripoint> rerandomizations(NUM_RUNS_PER_TEST);
  434. system.encrypt(oneEncryption, one);
  435. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  436. {
  437. system.encrypt(firstEncryptions[i], testVals[i]);
  438. realEncryptions[i] = system.homomorphic_multiplication(firstEncryptions[i], oneEncryption);
  439. }
  440. chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
  441. for (size_t i = 0; i < NUM_RUNS_PER_TEST; i++)
  442. rerandomizations[i] = system.rerandomize(realEncryptions[i]);
  443. chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
  444. chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t1 - t0);
  445. return time_span.count();
  446. }
  447. int main(int argc, char *argv[])
  448. {
  449. string seedStr("default");
  450. if (argc > 1)
  451. seedStr = argv[1];
  452. seed_seq seed(seedStr.begin(), seedStr.end());
  453. default_random_engine generator(seed);
  454. uniform_int_distribution<int> distribution(0, MAX_VALUE_IN_TEST);
  455. cout << "test_PointAtInfinity: ";
  456. if (testDecrypt(0))
  457. cout << "PASS" << endl;
  458. else
  459. cout << "FAIL" << endl;
  460. cout << "test_GeneratorPoint: ";
  461. if (testDecrypt(1))
  462. cout << "PASS" << endl;
  463. else
  464. cout << "FAIL" << endl;
  465. int randomPoint = distribution(generator);
  466. cout << "test_RandomPoint (" << randomPoint << "): ";
  467. if (testDecrypt(randomPoint))
  468. cout << "PASS" << endl;
  469. else
  470. cout << "FAIL" << endl;
  471. cout << "test_CurveEncryptSpeed (" << NUM_RUNS_PER_TEST << " runs): ";
  472. cout << testCurveEncryptSpeed(generator) << " seconds" << endl;
  473. cout << "test_TwistEncryptSpeed (" << NUM_RUNS_PER_TEST << " runs): ";
  474. cout << testTwistEncryptSpeed(generator) << " seconds" << endl;
  475. cout << "test_CurveDecryptSpeed (" << NUM_RUNS_PER_TEST << " runs): ";
  476. cout << testCurveDecryptSpeed(generator) << " seconds" << endl;
  477. cout << "test_TwistDecryptSpeed (" << NUM_RUNS_PER_TEST << " runs): ";
  478. cout << testTwistDecryptSpeed(generator) << " seconds" << endl;
  479. cout << "test_QuadDecryptSpeed (" << NUM_RUNS_PER_TEST << " runs): ";
  480. cout << testQuadDecryptSpeed(generator) << " seconds" << endl;
  481. int max = 10;
  482. cout << "test_CurveDecryptSpeed (0 -> " << max << "): ";
  483. cout << testCurveDecryptSpeed(max) << " seconds" << endl;
  484. max = 20;
  485. cout << "test_CurveDecryptSpeed (0 -> " << max << "): ";
  486. cout << testCurveDecryptSpeed(max) << " seconds" << endl;
  487. max = 30;
  488. cout << "test_CurveDecryptSpeed (0 -> " << max << "): ";
  489. cout << testCurveDecryptSpeed(max) << " seconds" << endl;
  490. max = 40;
  491. cout << "test_CurveDecryptSpeed (0 -> " << max << "): ";
  492. cout << testCurveDecryptSpeed(max) << " seconds" << endl;
  493. max = 50;
  494. cout << "test_CurveDecryptSpeed (0 -> " << max << "): ";
  495. cout << testCurveDecryptSpeed(max) << " seconds" << endl;
  496. max = 10;
  497. cout << "test_TwistDecryptSpeed (0 -> " << max << "): ";
  498. cout << testTwistDecryptSpeed(max) << " seconds" << endl;
  499. max = 20;
  500. cout << "test_TwistDecryptSpeed (0 -> " << max << "): ";
  501. cout << testTwistDecryptSpeed(max) << " seconds" << endl;
  502. max = 30;
  503. cout << "test_TwistDecryptSpeed (0 -> " << max << "): ";
  504. cout << testTwistDecryptSpeed(max) << " seconds" << endl;
  505. max = 40;
  506. cout << "test_TwistDecryptSpeed (0 -> " << max << "): ";
  507. cout << testTwistDecryptSpeed(max) << " seconds" << endl;
  508. max = 50;
  509. cout << "test_TwistDecryptSpeed (0 -> " << max << "): ";
  510. cout << testTwistDecryptSpeed(max) << " seconds" << endl;
  511. max = 10;
  512. cout << "test_QuadDecryptSpeed (0 -> " << max << "): ";
  513. cout << testQuadDecryptSpeed(max) << " seconds" << endl;
  514. max = 20;
  515. cout << "test_QuadDecryptSpeed (0 -> " << max << "): ";
  516. cout << testQuadDecryptSpeed(max) << " seconds" << endl;
  517. max = 30;
  518. cout << "test_QuadDecryptSpeed (0 -> " << max << "): ";
  519. cout << testQuadDecryptSpeed(max) << " seconds" << endl;
  520. max = 40;
  521. cout << "test_QuadDecryptSpeed (0 -> " << max << "): ";
  522. cout << testQuadDecryptSpeed(max) << " seconds" << endl;
  523. max = 50;
  524. cout << "test_QuadDecryptSpeed (0 -> " << max << "): ";
  525. cout << testQuadDecryptSpeed(max) << " seconds" << endl;
  526. int addX = distribution(generator);
  527. int addY = distribution(generator);
  528. cout << "test_Addition (" << addX << ", " << addY << "): ";
  529. if (testAddition(addX, addY))
  530. cout << "PASS" << endl;
  531. else
  532. cout << "FAIL" << endl;
  533. cout << "test_CurveAdditionSpeed (" << NUM_RUNS_PER_TEST << " runs): ";
  534. cout << testCurveAdditionSpeed(generator) << " seconds" << endl;
  535. cout << "test_TwistAdditionSpeed (" << NUM_RUNS_PER_TEST << " runs): ";
  536. cout << testTwistAdditionSpeed(generator) << " seconds" << endl;
  537. cout << "test_QuadAdditionSpeed (" << NUM_RUNS_PER_TEST << " runs): ";
  538. cout << testQuadAdditionSpeed(generator) << " seconds" << endl;
  539. int multX = distribution(generator);
  540. int multY = distribution(generator);
  541. cout << "test_Multiplication (" << multX << ", " << multY << "): ";
  542. if (testMultiplication(multX, multY))
  543. cout << "PASS" << endl;
  544. else
  545. cout << "FAIL" << endl;
  546. cout << "test_MultiplicationSpeed (" << NUM_RUNS_PER_TEST << " runs): ";
  547. cout << testMultiplicationSpeed(generator) << " seconds" << endl;
  548. int rerandomizingPoint = distribution(generator);
  549. cout << "test_Rerandomize (" << rerandomizingPoint << "): ";
  550. if (testRerandomize(rerandomizingPoint))
  551. cout << "PASS" << endl;
  552. else
  553. cout << "FAIL" << endl;
  554. cout << "test_CurveRerandomizeSpeed (" << NUM_RUNS_PER_TEST << " runs): ";
  555. cout << testCurveRerandomizeSpeed(generator) << " seconds" << endl;
  556. cout << "test_TwistRerandomizeSpeed (" << NUM_RUNS_PER_TEST << " runs): ";
  557. cout << testTwistRerandomizeSpeed(generator) << " seconds" << endl;
  558. cout << "test_QuadRerandomizeSpeed (" << NUM_RUNS_PER_TEST << " runs): ";
  559. cout << testQuadRerandomizeSpeed(generator) << " seconds" << endl;
  560. return 0;
  561. }