main.cpp 26 KB

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