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.

77 lines
1.8 KiB

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