decryption.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "decryption.hpp"
  2. int decrypt(const Bipoint<curvepoint_fp_t>& ciphertext, const PublicKey& public_key, const PrivateKey& private_key)
  3. {
  4. static std::unordered_map<Bipoint<curvepoint_fp_t>, int> memoizer;
  5. static int max_checked = 0;
  6. static Bipoint<curvepoint_fp_t> pi_1_curvegen = private_key.pi_1(public_key.get_bipoint_curvegen());
  7. Bipoint<curvepoint_fp_t> pi_1_ciphertext = private_key.pi_1(ciphertext);
  8. auto lookup = memoizer.find(pi_1_ciphertext);
  9. if (lookup != memoizer.end())
  10. {
  11. return lookup->second;
  12. }
  13. Bipoint<curvepoint_fp_t> i = pi_1_curvegen * max_checked;
  14. do
  15. {
  16. memoizer[pi_1_ciphertext] = max_checked++;
  17. i = i + pi_1_curvegen;
  18. } while (i != pi_1_ciphertext);
  19. return max_checked - 1;
  20. }
  21. int decrypt(const Bipoint<twistpoint_fp2_t>& ciphertext, const PrivateKey& private_key) // pour les chiffrés de niveau 1
  22. {
  23. static std::unordered_map<Bipoint<twistpoint_fp2_t>, int> memoizer;
  24. static int max_checked = 0;
  25. static Bipoint<twistpoint_fp2_t> pi_2_twistgen = private_key.pi_2(public_key.get_bipoint_twistgen());
  26. Bipoint<twistpoint_fp2_t> pi_2_ciphertext = private_key.pi_2(ciphertext);
  27. auto lookup = memoizer.find(pi_2_ciphertext);
  28. if (lookup != memoizer.end())
  29. {
  30. return lookup->second;
  31. }
  32. Bipoint<twistpoint_fp2_t> i = pi_2_twistgen * max_checked;
  33. do
  34. {
  35. memoizer[pi_2_ciphertext] = max_checked++;
  36. i = i + pi_2_twistgen;
  37. } while (i != pi_2_ciphertext);
  38. return max_checked - 1;
  39. }
  40. void decrypt(const Quadripoint& ciphertext, const PrivateKey& private_key)
  41. {
  42. static std::unordered_map<Quadripoint, int> memoizer;
  43. static int max_checked = 0;
  44. static Quadripoint pi_T_pairgen = private_key.pi_T(pairing(public_key.get_bipoint_curvegen(), public_key.get_bipoint_twistgen()));
  45. Quadripoint pi_T_ciphertext = private_key.pi_T(ciphertext);
  46. auto lookup = memoizer.find(pi_T_ciphertext);
  47. if (lookup != memoizer.end())
  48. {
  49. return lookup->second;
  50. }
  51. Quadripoint i = pi_T_pairgen ^ max_checked;
  52. do
  53. {
  54. memoizer[pi_2_ciphertext] = max_checked++;
  55. i = i * pi_T_pairgen;
  56. } while (i != pi_T_ciphertext);
  57. return max_checked - 1;
  58. }