BGN.cpp 2.9 KB

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