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.

52 lines
1.5 KiB

25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
  1. #include "test_MI.h"
  2. int test_MI_expt (int iterations)
  3. {
  4. int error = 0;
  5. int i;
  6. // Check special caSes 0, 1, 2.
  7. for (i = iterations; i > 0; i--) {
  8. cl_I m = testrandom_I();
  9. cl_modint_ring R = find_modint_ring(m);
  10. cl_MI a = R->canonhom(testrandom_I());
  11. ASSERT2(expt(a,0) == R->one(), m,a);
  12. ASSERT2(expt(a,1) == a, m,a);
  13. ASSERT2(expt(a,2) == a*a, m,a);
  14. }
  15. // Check special cases -1, -2.
  16. for (i = iterations; i > 0; i--) {
  17. cl_I m = testrandom_I();
  18. cl_modint_ring R = find_modint_ring(m);
  19. cl_I ai = testrandom_I();
  20. if (gcd(m,ai)==1) {
  21. cl_MI a = R->canonhom(ai);
  22. cl_MI ar = R->recip(a);
  23. ASSERT2(expt(a,-1) == ar, m,a);
  24. ASSERT2(expt(a,-2) == ar*ar, m,a);
  25. }
  26. }
  27. // Check homomorphism (fixed exponent).
  28. for (i = iterations; i > 0; i--) {
  29. cl_I m = testrandom_I();
  30. if (!zerop(m)) { // avoid generating huge numbers
  31. cl_modint_ring R = find_modint_ring(m);
  32. cl_MI a = R->canonhom(testrandom_I());
  33. cl_MI b = R->canonhom(testrandom_I());
  34. cl_I e = abs(testrandom_I());
  35. ASSERT4(expt(a,e)*expt(b,e) == expt(a*b,e), m,a,b,e);
  36. }
  37. }
  38. // Check distributive formulas (fixed base).
  39. for (i = iterations; i > 0; i--) {
  40. cl_I m = testrandom_I();
  41. if (!zerop(m)) { // avoid generating huge numbers
  42. cl_modint_ring R = find_modint_ring(m);
  43. cl_MI a = R->canonhom(testrandom_I());
  44. cl_I e = abs(testrandom_I());
  45. cl_I f = abs(testrandom_I());
  46. ASSERT4(expt(a,e)*expt(a,f) == expt(a,e+f), m,a,e,f);
  47. ASSERT4(expt(expt(a,e),f) == expt(a,e*f), m,a,e,f);
  48. }
  49. }
  50. return error;
  51. }