Quadripoint.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include "Quadripoint.hpp"
  2. Quadripoint::Quadripoint()
  3. {
  4. fp12e_setone(point[0]);
  5. fp12e_setone(point[1]);
  6. fp12e_setone(point[2]);
  7. fp12e_setone(point[3]);
  8. }
  9. Quadripoint::Quadripoint(const fp12e_t& p1, const fp12e_t& p2, const fp12e_t& p3, const fp12e_t& p4)
  10. {
  11. fp12e_set(point[0], p1);
  12. fp12e_set(point[1], p2);
  13. fp12e_set(point[2], p3);
  14. fp12e_set(point[3], p4);
  15. }
  16. fp12e_t& Quadripoint::operator[](int n)
  17. {
  18. return point[n];
  19. }
  20. const fp12e_t& Quadripoint::operator[](int n) const
  21. {
  22. return point[n];
  23. }
  24. Quadripoint Quadripoint::operator+(const Quadripoint& b) const
  25. {
  26. Quadripoint retval;
  27. if (fp12e_iseq(point[0], b[0]))
  28. fp12e_square(retval[0], point[0]);
  29. else
  30. fp12e_mul(retval[0], point[0], b[0]);
  31. if (fp12e_iseq(point[1], b[1]))
  32. fp12e_square(retval[1], point[1]);
  33. else
  34. fp12e_mul(retval[1], point[1], b[1]);
  35. if (fp12e_iseq(point[2], b[2]))
  36. fp12e_square(retval[2], point[2]);
  37. else
  38. fp12e_mul(retval[2], point[2], b[2]);
  39. if (fp12e_iseq(point[3], b[3]))
  40. fp12e_square(retval[3], point[3]);
  41. else
  42. fp12e_mul(retval[3], point[3], b[3]);
  43. return retval;
  44. }
  45. Quadripoint Quadripoint::operator*(const Scalar& exp) const
  46. {
  47. Quadripoint retval;
  48. if (exp == Scalar(0))
  49. {
  50. fp12e_setone(retval[0]);
  51. fp12e_setone(retval[1]);
  52. fp12e_setone(retval[2]);
  53. fp12e_setone(retval[3]);
  54. } else {
  55. exp.mult(retval[0], point[0]);
  56. exp.mult(retval[1], point[1]);
  57. exp.mult(retval[2], point[2]);
  58. exp.mult(retval[3], point[3]);
  59. }
  60. return retval;
  61. }
  62. bool Quadripoint::operator==(const Quadripoint& b) const
  63. {
  64. bool retval;
  65. retval = fp12e_iseq(point[0], b[0]);
  66. retval = retval && fp12e_iseq(point[1], b[1]);
  67. retval = retval && fp12e_iseq(point[2], b[2]);
  68. retval = retval && fp12e_iseq(point[3], b[3]);
  69. return retval;
  70. }
  71. bool Quadripoint::operator!=(const Quadripoint& b) const
  72. {
  73. return !(*this == b);
  74. }
  75. std::ostream& operator<<(std::ostream& os, const Quadripoint& output)
  76. {
  77. for (int i = 0; i < 4; i++)
  78. os << Fp12e(output[i]);
  79. return os;
  80. }
  81. std::istream& operator>>(std::istream& is, Quadripoint& input)
  82. {
  83. Fp12e realIn[4];
  84. for (int i = 0; i < 4; i++)
  85. {
  86. is >> realIn[i];
  87. fp12e_set(input[i], realIn[i].data);
  88. }
  89. return is;
  90. }
  91. size_t QuadripointHash::operator()(const Quadripoint& x) const
  92. {
  93. size_t retval = 0;
  94. std::hash<double> hasher;
  95. for (int i = 0; i < 4; i++)
  96. {
  97. for (int j = 0; j < 24; j++)
  98. {
  99. /* I'm so sorry for pointer hell here,
  100. * the types are just like this */
  101. retval ^= hasher(x[i]->m_a->m_a->v[j]);
  102. retval ^= hasher(x[i]->m_a->m_b->v[j]);
  103. retval ^= hasher(x[i]->m_a->m_c->v[j]);
  104. retval ^= hasher(x[i]->m_b->m_a->v[j]);
  105. retval ^= hasher(x[i]->m_b->m_b->v[j]);
  106. retval ^= hasher(x[i]->m_b->m_c->v[j]);
  107. }
  108. }
  109. return retval;
  110. }