BGN.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "BGN.hpp"
  2. BGN::BGN()
  3. {
  4. Scalar a1, b1, c1, d1, a2, b2, c2, d2;
  5. while (true)
  6. {
  7. a1.set_random();
  8. b1.set_random();
  9. c1.set_random();
  10. if (a1 != 0)
  11. {
  12. d1 = (b1 * c1 + Scalar(1)) / a1;
  13. break;
  14. }
  15. }
  16. while (true)
  17. {
  18. a2.set_random();
  19. b2.set_random();
  20. c2.set_random();
  21. if (a2 != 0)
  22. {
  23. d2 = (b2 * c2 + Scalar(1)) / a2;
  24. break;
  25. }
  26. }
  27. Scalar r1, r2, r3, r4;
  28. r1.set_random();
  29. r2.set_random();
  30. r3.set_random();
  31. r4.set_random();
  32. curvepoint_fp_t g_part1, g_part2, g_a1, g_b1;
  33. g_part1 = r1 * bn_curvegen;
  34. g_part2 = r2 * bn_curvegen;
  35. g_a1 = a1 * bn_curvegen;
  36. g_b1 = b1 * bn_curvegen;
  37. Bipoint<curvepoint_fp_t> full_g(g_part1, g_part2);
  38. Bipoint<curvepoint_fp_t> full_g1(g_a1, g_b1);
  39. twistpoint_fp2_t h_part1, h_part2, h_a2, h_b2;
  40. h_part1 = r3 * bn_twistgen;
  41. h_part2 = r4 * bn_twistgen;
  42. h_a2 = a2 * bn_twistgen;
  43. h_b2 = b2 * bn_twistgen;
  44. Bipoint<twistpoint_fp2_t> full_h(h_part1, h_part2);
  45. Bipoint<twistpoint_fp2_t> full_h1(h_a2, h_b2);
  46. full_g.make_affine();
  47. full_g1.make_affine();
  48. full_h.make_affine();
  49. full_h1.make_affine();
  50. public_key.set(full_g, full_h, full_g1, full_h1);
  51. private_key.set(public_key, a1, b1, c1, d1, a2, b2, c2, d2);
  52. }
  53. void BGN::encrypt(Bipoint<curvepoint_fp_t>& G_element, const Scalar& cleartext) const
  54. {
  55. public_key.encrypt(G_element, cleartext);
  56. }
  57. void BGN::encrypt(Bipoint<twistpoint_fp2_t>& H_element, const Scalar& cleartext) const
  58. {
  59. public_key.encrypt(H_element, cleartext);
  60. }
  61. void BGN::encrypt(Bipoint<curvepoint_fp_t>& G_element, Bipoint<twistpoint_fp2_t>& H_element, const Scalar& cleartext) const
  62. {
  63. public_key.encrypt(G_element, H_element, cleartext);
  64. }
  65. Bipoint<curvepoint_fp_t> BGN::homomorphic_addition(const Bipoint<curvepoint_fp_t>& a, const Bipoint<curvepoint_fp_t>& b) const
  66. {
  67. return public_key.homomorphic_addition(a, b);
  68. }
  69. Bipoint<twistpoint_fp2_t> BGN::homomorphic_addition(const Bipoint<twistpoint_fp2_t>& a, const Bipoint<twistpoint_fp2_t>& b) const
  70. {
  71. return public_key.homomorphic_addition(a, b);
  72. }
  73. Quadripoint BGN::homomorphic_addition(const Quadripoint& a, const Quadripoint& b) const
  74. {
  75. return public_key.homomorphic_addition(a, b);
  76. }
  77. Quadripoint BGN::homomorphic_multiplication(const Bipoint<curvepoint_fp_t>& a, const Bipoint<twistpoint_fp2_t>& b) const
  78. {
  79. return public_key.homomorphic_multiplication(a, b);
  80. }
  81. Scalar BGN::decrypt(const Bipoint<curvepoint_fp_t>& ciphertext) const
  82. {
  83. return private_key.decrypt(ciphertext);
  84. }
  85. Scalar BGN::decrypt(const Bipoint<twistpoint_fp2_t>& ciphertext) const
  86. {
  87. return private_key.decrypt(ciphertext);
  88. }
  89. Scalar BGN::decrypt(const Quadripoint& ciphertext) const
  90. {
  91. return private_key.decrypt(ciphertext);
  92. }
  93. PublicKey BGN::get_public_key() const
  94. {
  95. return public_key;
  96. }