Bipoint.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include "Bipoint.hpp"
  2. extern const scalar_t bn_n;
  3. Bipoint<curvepoint_fp_t>::Bipoint()
  4. {
  5. curvepoint_fp_setneutral(point[0]);
  6. curvepoint_fp_setneutral(point[1]);
  7. }
  8. Bipoint<twistpoint_fp2_t>::Bipoint()
  9. {
  10. twistpoint_fp2_setneutral(point[0]);
  11. twistpoint_fp2_setneutral(point[1]);
  12. }
  13. Bipoint<curvepoint_fp_t>::Bipoint(curvepoint_fp_t p1, curvepoint_fp_t p2)
  14. {
  15. curvepoint_fp_set(point[0], p1);
  16. curvepoint_fp_set(point[1], p2);
  17. }
  18. Bipoint<twistpoint_fp2_t>::Bipoint(twistpoint_fp2_t p1, twistpoint_fp2_t p2)
  19. {
  20. twistpoint_fp2_set(point[0], p1);
  21. twistpoint_fp2_set(point[1], p2);
  22. }
  23. void Bipoint<curvepoint_fp_t>::receive_encryption(const scalar_t& cleartext, const PublicKey& public_key)
  24. {
  25. scalar_t lambda;
  26. scalar_setrandom(lambda, bn_n);
  27. Bipoint<curvepoint_fp_t> cleartext_as_element, random_mask;
  28. cleartext_as_element = public_key.get_bipoint_curvegen().scalarmult_vartime(cleartext);
  29. cleartext_as_element.makeaffine();
  30. random_mask = public_key.get_bipoint_curve_groupelt().scalarmult_vartime(lambda);
  31. random_mask.makeaffine();
  32. ciphertext = cleartext_as_element + random_mask;
  33. ciphertext.makeaffine();
  34. point[0] = ciphertext.point[0];
  35. point[1] = ciphertext.point[1];
  36. }
  37. void Bipoint<twistpoint_fp2_t>::receive_encryption(const scalar_t& cleartext, const PublicKey& public_key)
  38. {
  39. scalar_t lambda;
  40. scalar_setrandom(lambda, bn_n);
  41. Bipoint<twistpoint_fp2_t> cleartext_as_element, random_mask;
  42. cleartext_as_element = public_key.get_bipoint_twistgen().scalarmult_vartime(cleartext);
  43. cleartext_as_element.makeaffine();
  44. random_mask = public_key.get_bipoint_twist_groupelt().scalarmult_vartime(lambda);
  45. random_mask.makeaffine();
  46. ciphertext = cleartext_as_element + random_mask;
  47. ciphertext.makeaffine();
  48. point[0] = ciphertext.point[0];
  49. point[1] = ciphertext.point[1];
  50. }
  51. curvepoint_fp_t& Bipoint<curvepoint_fp_t>::operator[](int n)
  52. {
  53. return point[n];
  54. }
  55. twistpoint_fp2_t& Bipoint<twistpoint_fp2_t>::operator[](int n)
  56. {
  57. return point[n];
  58. }
  59. const curvepoint_fp_t& Bipoint<curvepoint_fp_t>::operator[](int n) const
  60. {
  61. return point[n];
  62. }
  63. const twistpoint_fp2_t& Bipoint<twistpoint_fp2_t>::operator[](int n) const
  64. {
  65. return point[n];
  66. }
  67. Bipoint<curvepoint_fp_t> Bipoint<curvepoint_fp_t>::operator+(const Bipoint<curvepoint_fp_t>& b) const
  68. {
  69. Bipoint<curvepoint_fp_t> retval;
  70. curvepoint_fp_add_vartime(retval[0], point[0], b.point[0]);
  71. curvepoint_fp_add_vartime(retval[1], point[1], b.point[1]);
  72. return retval;
  73. }
  74. Bipoint<twistpoint_fp2_t> Bipoint<twistpoint_fp2_t>::operator+(const Bipoint<twistpoint_fp2_t>& b) const
  75. {
  76. Bipoint<twistpoint_fp2_t> retval;
  77. twistpoint_fp2_add_vartime(retval[0], point[0], b.point[0]);
  78. twistpoint_fp2_add_vartime(retval[1], point[1], b.point[1]);
  79. return retval;
  80. }
  81. Bipoint<curvepoint_fp_t> Bipoint<curvepoint_fp_t>::operator*(const scalar_t& mult) const
  82. {
  83. Bipoint<curvepoint_fp_t> retval;
  84. curvepoint_fp_scalarmult_vartime(retval[0], point[0], mult);
  85. curvepoint_fp_scalarmult_vartime(retval[1], point[1], mult);
  86. return retval;
  87. }
  88. Bipoint<twistpoint_fp2_t> Bipoint<twistpoint_fp2_t>::operator*(const scalar_t& mult) const
  89. {
  90. Bipoint<twistpoint_fp2_t> retval;
  91. twistpoint_fp2_scalarmult_vartime(retval[0], point[0], mult);
  92. twistpoint_fp2_scalarmult_vartime(retval[1], point[1], mult);
  93. return retval;
  94. }
  95. bool Bipoint<curvepoint_fp_t>::operator==(const Bipoint<curvepoint_fp_t>& b) const
  96. {
  97. bool retval = fpe_iseq(point[0]->m_x, b[0]->m_x);
  98. retval &&= fpe_iseq(point[0]->m_y, b[0]->m_y);
  99. retval &&= fpe_iseq(point[0]->m_z, b[0]->m_z);
  100. retval &&= fpe_iseq(point[1]->m_x, b[1]->m_x);
  101. retval &&= fpe_iseq(point[1]->m_y, b[1]->m_y);
  102. retval &&= fpe_iseq(point[1]->m_z, b[1]->m_z);
  103. return retval;
  104. }
  105. bool Bipoint<twistpoint_fp2_t>::operator==(const Bipoint<twistpoint_fp2_t>& b) const
  106. {
  107. bool retval = fp2e_iseq(point[0]->m_x, b[0]->m_x);
  108. retval &&= fp2e_iseq(point[0]->m_y, b[0]->m_y);
  109. retval &&= fp2e_iseq(point[0]->m_z, b[0]->m_z);
  110. retval &&= fp2e_iseq(point[1]->m_x, b[1]->m_x);
  111. retval &&= fp2e_iseq(point[1]->m_y, b[1]->m_y);
  112. retval &&= fp2e_iseq(point[1]->m_z, b[1]->m_z);
  113. return retval;
  114. }
  115. Bipoint<curvepoint_fp_t> Bipoint<curvepoint_fp_t>::multBy2() const
  116. {
  117. Bipoint<curvepoint_fp_t> retval;
  118. curvepoint_fp_double(retval[0], point[0]);
  119. curvepoint_fp_double(retval[1], point[1]);
  120. return retval;
  121. }
  122. Bipoint<twistpoint_fp2_t> Bipoint<twistpoint_fp2_t>::multBy2() const
  123. {
  124. Bipoint<twistpoint_fp2_t> retval;
  125. twistpoint_fp2_double(retval[0], point[0]);
  126. twistpoint_fp2_double(retval[1], point[1]);
  127. return retval;
  128. }