Scalar.hpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #ifndef __SCALAR_HPP
  2. #define __SCALAR_HPP
  3. #include <ostream>
  4. #include <stdlib.h>
  5. #include <sstream>
  6. #include <gmpxx.h>
  7. #include "Bipoint.hpp"
  8. #include "Quadripoint.hpp"
  9. #include "scalar.h"
  10. class Scalar
  11. {
  12. public:
  13. Scalar();
  14. Scalar(const scalar_t& input);
  15. Scalar(mpz_class input);
  16. void set(const scalar_t& input);
  17. void set(mpz_class input);
  18. void set_random();
  19. Scalar operator-() const;
  20. Scalar operator+(const Scalar& b) const;
  21. Scalar operator-(const Scalar& b) const;
  22. Scalar operator*(const Scalar& b) const;
  23. Scalar operator/(const Scalar& b) const;
  24. Scalar& operator++();
  25. Scalar operator++(int);
  26. Scalar& operator--();
  27. Scalar operator--(int);
  28. curvepoint_fp_t operator*(const curvepoint_fp_t& b) const;
  29. twistpoint_fp2_t operator*(const twistpoint_fp2_t& b) const;
  30. fp12e_t operator*(const fp12e_t& b) const;
  31. Bipoint<curvepoint_fp_t> operator*(const Bipoint<curvepoint_fp_t>& b) const;
  32. Bipoint<twistpoint_fp2_t> operator*(const Bipoint<twistpoint_fp2_t>& b) const;
  33. Quadripoint operator*(const Quadripoint& b) 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. private:
  38. class SecretScalar
  39. {
  40. SecretScalar();
  41. SecretScalar(const Scalar& input);
  42. SecretScalar(mpz_class input);
  43. // Problem: thanks to the magic of weird typedefs, scalar_t is actually an array, which complicates returning it
  44. // Solution: make the return value a reference
  45. // This feels bad, I know, but it will only be used in places where the variable remains in scope for the duration of usage
  46. const scalar_t& expose() const;
  47. private:
  48. void set(mpz_class input);
  49. scalar_t element;
  50. };
  51. SecretScalar to_scalar_t() const;
  52. static const mpz_class mpz_bn_n("8FB501E34AA387F9AA6FECB86184DC212E8D8E12F82B39241A2EF45B57AC7261", 16);
  53. mpz_class element;
  54. };
  55. #endif