BGN.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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_field_random();
  10. b1.set_field_random();
  11. c1.set_field_random();
  12. if (a1 != Scalar(0))
  13. {
  14. d1 = (b1.fieldMult(c1).fieldAdd(Scalar(1))).fieldMult(a1.fieldMultInverse());
  15. break;
  16. }
  17. }
  18. while (true)
  19. {
  20. a2.set_field_random();
  21. b2.set_field_random();
  22. c2.set_field_random();
  23. if (a2 != Scalar(0))
  24. {
  25. d2 = (b2.fieldMult(c2).fieldAdd(Scalar(1))).fieldMult(a2.fieldMultInverse());
  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. CurveBipoint BGN::homomorphic_addition_no_rerandomize(const CurveBipoint& a, const CurveBipoint& b) const
  84. {
  85. return public_key.homomorphic_addition_no_rerandomize(a, b);
  86. }
  87. TwistBipoint BGN::homomorphic_addition_no_rerandomize(const TwistBipoint& a, const TwistBipoint& b) const
  88. {
  89. return public_key.homomorphic_addition_no_rerandomize(a, b);
  90. }
  91. Quadripoint BGN::homomorphic_addition_no_rerandomize(const Quadripoint& a, const Quadripoint& b) const
  92. {
  93. return public_key.homomorphic_addition_no_rerandomize(a, b);
  94. }
  95. Quadripoint BGN::homomorphic_multiplication_no_rerandomize(const CurveBipoint& a, const TwistBipoint& b) const
  96. {
  97. return public_key.homomorphic_multiplication_no_rerandomize(a, b);
  98. }
  99. CurveBipoint BGN::rerandomize(const CurveBipoint& G_element) const
  100. {
  101. return public_key.rerandomize(G_element);
  102. }
  103. TwistBipoint BGN::rerandomize(const TwistBipoint& H_element) const
  104. {
  105. return public_key.rerandomize(H_element);
  106. }
  107. Quadripoint BGN::rerandomize(const Quadripoint& Gt_element) const
  108. {
  109. return public_key.rerandomize(Gt_element);
  110. }
  111. Scalar BGN::decrypt(const CurveBipoint& ciphertext)
  112. {
  113. return private_key.decrypt(ciphertext);
  114. }
  115. Scalar BGN::decrypt(const TwistBipoint& ciphertext)
  116. {
  117. return private_key.decrypt(ciphertext);
  118. }
  119. Scalar BGN::decrypt(const Quadripoint& ciphertext)
  120. {
  121. return private_key.decrypt(ciphertext);
  122. }
  123. const BGNPublicKey& BGN::get_public_key() const
  124. {
  125. return public_key;
  126. }
  127. const BGNPrivateKey& BGN::get_private_key() const
  128. {
  129. return private_key;
  130. }
  131. std::ostream& operator<<(std::ostream& os, const BGN& output)
  132. {
  133. os << output.public_key;
  134. os << output.private_key;
  135. return os;
  136. }
  137. std::istream& operator>>(std::istream& is, BGN& input)
  138. {
  139. is >> input.public_key;
  140. is >> input.private_key;
  141. return is;
  142. }