You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
1.8 KiB

25 years ago
25 years ago
  1. #include <cln/number.h>
  2. #include <cln/io.h>
  3. #include <cln/integer.h>
  4. #include <cln/integer_io.h>
  5. #include <cln/modinteger.h>
  6. #include <cln/numtheory.h>
  7. #include <cstdlib>
  8. #include <cstring>
  9. #include <cln/timing.h>
  10. using namespace cln;
  11. #include <iostream>
  12. using namespace std;
  13. int main (int argc, char * argv[])
  14. {
  15. int repetitions = 1;
  16. if ((argc >= 3) && !strcmp(argv[1],"-r")) {
  17. repetitions = atoi(argv[2]);
  18. argc -= 2; argv += 2;
  19. }
  20. if (argc < 2)
  21. exit(1);
  22. cl_I len = cl_I(argv[1]);
  23. int e = (argc > 2 ? atoi(argv[2]) : 0);
  24. if (e < 1)
  25. e = 1;
  26. if (len <= e)
  27. exit(0);
  28. cl_I p;
  29. do {
  30. p = ((random_I((cl_I)1 << (len-1-e))*2+1) << e) + 1;
  31. } while (!isprobprime(p));
  32. cout << "p = " << p << endl;
  33. cl_modint_ring R = find_modint_ring(p);
  34. cl_MI x = R->random();
  35. cl_MI a = square(x);
  36. sqrt_mod_p_t sol;
  37. #if 0
  38. extern int cl_sqrt_algo;
  39. cl_sqrt_algo = 1;
  40. { CL_TIMING;
  41. for (int rep = repetitions; rep > 0; rep--)
  42. { sol = sqrt_mod_p(R,a); }
  43. }
  44. if (sol.condition)
  45. cerr << "p not prime!" << endl;
  46. else {
  47. if (sol.solutions == 0)
  48. cerr << "No sqrt found!" << endl;
  49. if (!(sol.solution[0] == x || sol.solution[0] == -x))
  50. cerr << "Wrong result!" << endl;
  51. }
  52. cl_sqrt_algo = 2;
  53. { CL_TIMING;
  54. for (int rep = repetitions; rep > 0; rep--)
  55. { sol = sqrt_mod_p(R,a); }
  56. }
  57. if (sol.condition)
  58. cerr << "p not prime!" << endl;
  59. else {
  60. if (sol.solutions == 0)
  61. cerr << "No sqrt found!" << endl;
  62. if (!(sol.solution[0] == x || sol.solution[0] == -x))
  63. cerr << "Wrong result!" << endl;
  64. }
  65. cl_sqrt_algo = 3;
  66. #endif
  67. { CL_TIMING;
  68. for (int rep = repetitions; rep > 0; rep--)
  69. { sol = sqrt_mod_p(R,a); }
  70. }
  71. if (sol.condition)
  72. cerr << "p not prime!" << endl;
  73. else {
  74. if (sol.solutions == 0)
  75. cerr << "No sqrt found!" << endl;
  76. if (!(sol.solution[0] == x || sol.solution[0] == -x))
  77. cerr << "Wrong result!" << endl;
  78. }
  79. }