Scalar.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. void set(const scalar_t& input);
  21. void set(mpz_class input);
  22. void set_random();
  23. Scalar operator+(const Scalar& b) const;
  24. Scalar operator-(const Scalar& b) const;
  25. Scalar operator*(const Scalar& b) const;
  26. Scalar operator/(const Scalar& b) const;
  27. Scalar& operator++();
  28. Scalar operator++(int);
  29. Scalar& operator--();
  30. Scalar operator--(int);
  31. void mult(curvepoint_fp_t rop, const curvepoint_fp_t& op1) const;
  32. void mult(twistpoint_fp2_t rop, const twistpoint_fp2_t& op1) const;
  33. void mult(fp12e_t rop, const fp12e_t& op1) const;
  34. bool operator==(const Scalar& b) const;
  35. bool operator!=(const Scalar& b) const;
  36. friend std::ostream& operator<<(std::ostream& os, const Scalar& output);
  37. friend std::istream& operator>>(std::istream& is, Scalar& input);
  38. private:
  39. class SecretScalar
  40. {
  41. public:
  42. SecretScalar();
  43. SecretScalar(const Scalar& input);
  44. SecretScalar(mpz_class input);
  45. /* Problem: thanks to the magic of weird typedefs, scalar_t is actually an array, which complicates returning it
  46. * Solution: make the return value a reference
  47. *
  48. * This feels bad, I know, but it will only be used in places where the variable remains in scope for the duration of usage
  49. * That's also why this class is private -- so it cannot be misused. */
  50. const scalar_t& expose() const;
  51. private:
  52. void set(mpz_class input);
  53. scalar_t element;
  54. };
  55. SecretScalar to_scalar_t() const;
  56. /* This is the thing everything else is modulused of;
  57. * whenever we do arithmetic of scalars,
  58. * we're doing arithmetic on field elements (\in F_p),
  59. * not directly on curvepoints, so we want p, not n.
  60. * Do keep in mind, though, that this means Scalars shouldn't in general
  61. * have arithmetic done on them prior to interacting with curvepoints,
  62. * if you're calculating something like an exponentiation of products
  63. * of Scalars (or similar). */
  64. static const mpz_class mpz_bn_p;
  65. mpz_class element;
  66. };
  67. #endif