Scalar.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include "Scalar.hpp"
  2. extern const scalar_t bn_n;
  3. Scalar::Scalar()
  4. {
  5. element = 0;
  6. }
  7. Scalar::Scalar(const scalar_t& input)
  8. {
  9. set(input);
  10. }
  11. Scalar::Scalar(mpz_class input)
  12. {
  13. element = input;
  14. }
  15. void Scalar::set(const scalar_t& input)
  16. {
  17. std::stringstream buffer;
  18. std::string temp;
  19. buffer << std::hex << input[3] << input[2] << input[1] << input[0];
  20. buffer >> temp;
  21. element.set_str(temp, 16);
  22. }
  23. void Scalar::set(mpz_class input)
  24. {
  25. element = input;
  26. }
  27. void Scalar::set_random()
  28. {
  29. scalar_t temp;
  30. scalar_setrandom(temp, bn_n);
  31. set(temp);
  32. }
  33. Scalar Scalar::operator+(const Scalar& b) const
  34. {
  35. mpz_class temp = element + b.element;
  36. mpz_mod(temp.get_mpz_t(), temp.get_mpz_t(), mpz_bn_n.get_mpz_t());
  37. return Scalar(temp);
  38. }
  39. Scalar Scalar::operator-(const Scalar& b) const
  40. {
  41. mpz_class temp = element - b.element;
  42. mpz_mod(temp.get_mpz_t(), temp.get_mpz_t(), mpz_bn_n.get_mpz_t());
  43. return Scalar(temp);
  44. }
  45. Scalar Scalar::operator*(const Scalar& b) const
  46. {
  47. mpz_class temp = element * b.element;
  48. mpz_mod(temp.get_mpz_t(), temp.get_mpz_t(), mpz_bn_n.get_mpz_t());
  49. return Scalar(temp);
  50. }
  51. Scalar Scalar::operator/(const Scalar& b) const
  52. {
  53. mpz_class temp;
  54. mpz_invert(temp.get_mpz_t(), b.element.get_mpz_t(), mpz_bn_n.get_mpz_t());
  55. temp *= element;
  56. mpz_mod(temp.get_mpz_t(), temp.get_mpz_t(), mpz_bn_n.get_mpz_t());
  57. return Scalar(temp);
  58. }
  59. Scalar& Scalar::operator++()
  60. {
  61. element++;
  62. mpz_mod(element.get_mpz_t(), element.get_mpz_t(), mpz_bn_n.get_mpz_t());
  63. return *this;
  64. }
  65. Scalar Scalar::operator++(int)
  66. {
  67. Scalar retval = *this;
  68. element++;
  69. mpz_mod(element.get_mpz_t(), element.get_mpz_t(), mpz_bn_n.get_mpz_t());
  70. return retval;
  71. }
  72. Scalar& Scalar::operator--()
  73. {
  74. element--;
  75. mpz_mod(element.get_mpz_t(), element.get_mpz_t(), mpz_bn_n.get_mpz_t());
  76. return *this;
  77. }
  78. Scalar Scalar::operator--(int)
  79. {
  80. Scalar retval = *this;
  81. element--;
  82. mpz_mod(element.get_mpz_t(), element.get_mpz_t(), mpz_bn_n.get_mpz_t());
  83. return retval;
  84. }
  85. curvepoint_fp_t Scalar::operator*(const curvepoint_fp_t& b) const
  86. {
  87. curvepoint_fp_t retval;
  88. curvepoint_fp_scalarmult_vartime(retval, b, element.to_scalar_t().expose());
  89. return retval;
  90. }
  91. twistpoint_fp2_t Scalar::operator*(const twistpoint_fp2_t& b) const
  92. {
  93. twistpoint_fp2_t retval;
  94. twistpoint_fp2_scalarmult_vartime(retval, b, element.to_scalar_t().expose());
  95. return retval;
  96. }
  97. fp12e_t Scalar::operator*(const fp12e_t& b) const
  98. {
  99. fp12e_t retval;
  100. fp12e_pow_vartime(retval, b, element.to_scalar_t().expose());
  101. return retval;
  102. }
  103. Bipoint<curvepoint_fp_t> Scalar::operator*(const Bipoint<curvepoint_fp_t>& b) const
  104. {
  105. return b * *this;
  106. }
  107. Bipoint<twistpoint_fp2_t> Scalar::operator*(const Bipoint<twistpoint_fp2_t>& b) const
  108. {
  109. return b * *this;
  110. }
  111. Quadripoint Scalar::operator*(const Quadripoint& b) const
  112. {
  113. return b * *this;
  114. }
  115. bool Scalar::operator==(const Scalar& b) const
  116. {
  117. return element == b.element;
  118. }
  119. bool Scalar::operator!=(const Scalar& b) const
  120. {
  121. return element != b.element;
  122. }
  123. Scalar::SecretScalar::SecretScalar()
  124. {
  125. element = {0,0,0,0};
  126. }
  127. Scalar::SecretScalar::SecretScalar(const Scalar& input)
  128. {
  129. set(input.element);
  130. }
  131. Scalar::SecretScalar::SecretScalar(mpz_class input)
  132. {
  133. set(input);
  134. }
  135. const scalar_t& Scalar::SecretScalar::expose() const
  136. {
  137. return element;
  138. }
  139. void Scalar::SecretScalar::set(mpz_class input)
  140. {
  141. std::stringstream buffer;
  142. char temp[17];
  143. buffer << std::setfill('0') << std::setw(64) << input.get_string(16);
  144. for (int i = 3; i >= 0; i--)
  145. {
  146. buffer.get(temp, 17);
  147. element[i] = strtoull(temp, NULL, 16);
  148. }
  149. }
  150. Scalar::SecretScalar Scalar::to_scalar_t() const
  151. {
  152. return SecretScalar(element);
  153. }
  154. std::ostream& operator<<(std::ostream& os, const Scalar& output)
  155. {
  156. os << output.element;
  157. return os;
  158. }