Scalar.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #ifndef __SCALAR_HPP
  2. #define __SCALAR_HPP
  3. #include <iomanip>
  4. #include <ostream>
  5. #include <stdlib.h>
  6. #include <sstream>
  7. #include <gmpxx.h>
  8. extern "C" {
  9. #include "scalar.h"
  10. #include "curvepoint_fp.h"
  11. #include "twistpoint_fp2.h"
  12. #include "fp12e.h"
  13. }
  14. class Scalar
  15. {
  16. public:
  17. Scalar();
  18. Scalar(const scalar_t& input);
  19. Scalar(mpz_class input);
  20. static void init();
  21. void set(const scalar_t& input);
  22. void set(mpz_class input);
  23. void set_random();
  24. void set_field_random();
  25. mpz_class toInt() const;
  26. Scalar operator+(const Scalar& b) const;
  27. Scalar operator-(const Scalar& b) const;
  28. Scalar operator*(const Scalar& b) const;
  29. Scalar operator/(const Scalar& b) const;
  30. Scalar operator-() const;
  31. Scalar& operator++();
  32. Scalar operator++(int);
  33. Scalar& operator--();
  34. Scalar operator--(int);
  35. Scalar fieldAdd(const Scalar& b) const;
  36. Scalar fieldSub(const Scalar& b) const;
  37. Scalar fieldMult(const Scalar& b) const;
  38. Scalar fieldMultInverse() const;
  39. Scalar curveAdd(const Scalar& b) const;
  40. Scalar curveSub(const Scalar& b) const;
  41. Scalar curveMult(const Scalar& b) const;
  42. Scalar curveMultInverse() const;
  43. void mult(curvepoint_fp_t rop, const curvepoint_fp_t& op1) const;
  44. void mult(twistpoint_fp2_t rop, const twistpoint_fp2_t& op1) const;
  45. void mult(fp12e_t rop, const fp12e_t& op1) const;
  46. bool operator==(const Scalar& b) const;
  47. bool operator<(const Scalar& b) const;
  48. bool operator<=(const Scalar& b) const;
  49. bool operator>(const Scalar& b) const;
  50. bool operator>=(const Scalar& b) const;
  51. bool operator!=(const Scalar& b) const;
  52. friend std::ostream& operator<<(std::ostream& os, const Scalar& output);
  53. friend std::istream& operator>>(std::istream& is, Scalar& input);
  54. private:
  55. class SecretScalar
  56. {
  57. public:
  58. SecretScalar();
  59. SecretScalar(const Scalar& input);
  60. SecretScalar(mpz_class input);
  61. /* Problem: thanks to the magic of weird typedefs, scalar_t is actually an array, which complicates returning it
  62. * Solution: make the return value a reference
  63. *
  64. * This feels bad, I know, but it will only be used in places where the variable remains in scope for the duration of usage
  65. * That's also why this class is private -- so it cannot be misused. */
  66. const scalar_t& expose() const;
  67. private:
  68. void set(mpz_class input);
  69. scalar_t element;
  70. };
  71. SecretScalar to_scalar_t() const;
  72. /* This is the thing everything else is modulused of;
  73. * whenever we do arithmetic of scalars,
  74. * we're doing arithmetic on field elements (\in F_p),
  75. * not directly on curvepoints, so we want p, not n.
  76. * Do keep in mind, though, that this means Scalars shouldn't in general
  77. * have arithmetic done on them prior to interacting with curvepoints,
  78. * if you're calculating something like an exponentiation of products
  79. * of Scalars (or similar). */
  80. static mpz_class mpz_bn_p;
  81. static mpz_class mpz_bn_n;
  82. mpz_class element;
  83. };
  84. #endif