Scalar.hpp 2.1 KB

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