Fp.hpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #ifndef __FP_HPP
  2. #define __FP_HPP
  3. #include <ostream>
  4. #include <sstream>
  5. #include <random>
  6. #include <gmp.h>
  7. #include <gmpxx.h>
  8. #include "mydouble.h"
  9. extern "C" {
  10. #include "fpe.h"
  11. #include "scalar.h"
  12. }
  13. class Fp
  14. {
  15. // Necessary for private and public keys to have access to private members
  16. friend class PrivateKey;
  17. friend class PublicKey;
  18. public:
  19. Fp();
  20. Fp(const fpe_t& input);
  21. Fp(int input);
  22. void set(const fpe_t& input);
  23. void set(int input);
  24. void set_random();
  25. Fp operator-() const;
  26. Fp operator+(const Fp& b) const;
  27. Fp operator-(const Fp& b) const;
  28. Fp operator*(const Fp& b) const;
  29. Fp operator/(const Fp& b) const;
  30. bool operator==(const Fp& b) const;
  31. bool operator!=(const Fp & b) const;
  32. bool Fp::is_zero() const;
  33. // Problem: thanks to the magic of weird typedefs, scalar_t is actually an array, which complicates returning it
  34. // Solution: make the return value a reference
  35. const scalar_t& to_scalar() const;
  36. friend std::ostream& operator<<(std::ostream& os, const Fp& output);
  37. private:
  38. unsigned long long Fp::mpz2ull(const mpz_class& n) const;
  39. fpe_t element;
  40. scalar_t scalar;
  41. bool no_change;
  42. };
  43. #endif