File: test_MI_expt.cc

package info (click to toggle)
cln 1.3.4-4
  • links: PTS
  • area: main
  • in suites: buster
  • size: 10,724 kB
  • sloc: cpp: 80,930; sh: 11,982; ansic: 3,278; makefile: 1,313
file content (52 lines) | stat: -rw-r--r-- 1,521 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "test_MI.h"

int test_MI_expt (int iterations)
{
	int error = 0;
	int i;
	// Check special caSes 0, 1, 2.
	for (i = iterations; i > 0; i--) {
		cl_I m = testrandom_I();
		cl_modint_ring R = find_modint_ring(m);
		cl_MI a = R->canonhom(testrandom_I());
		ASSERT2(expt(a,0) == R->one(), m,a);
		ASSERT2(expt(a,1) == a, m,a);
		ASSERT2(expt(a,2) == a*a, m,a);
	}
	// Check special cases -1, -2.
	for (i = iterations; i > 0; i--) {
		cl_I m = testrandom_I();
		cl_modint_ring R = find_modint_ring(m);
		cl_I ai = testrandom_I();
		if (gcd(m,ai)==1) {
			cl_MI a = R->canonhom(ai);
			cl_MI ar = R->recip(a);
			ASSERT2(expt(a,-1) == ar, m,a);
			ASSERT2(expt(a,-2) == ar*ar, m,a);
		}
	}
	// Check homomorphism (fixed exponent).
	for (i = iterations; i > 0; i--) {
		cl_I m = testrandom_I();
		if (!zerop(m)) { // avoid generating huge numbers
			cl_modint_ring R = find_modint_ring(m);
			cl_MI a = R->canonhom(testrandom_I());
			cl_MI b = R->canonhom(testrandom_I());
			cl_I e = abs(testrandom_I());
			ASSERT4(expt(a,e)*expt(b,e) == expt(a*b,e), m,a,b,e);
		}
	}
	// Check distributive formulas (fixed base).
	for (i = iterations; i > 0; i--) {
		cl_I m = testrandom_I();
		if (!zerop(m)) { // avoid generating huge numbers
			cl_modint_ring R = find_modint_ring(m);
			cl_MI a = R->canonhom(testrandom_I());
			cl_I e = abs(testrandom_I());
			cl_I f = abs(testrandom_I());
			ASSERT4(expt(a,e)*expt(a,f) == expt(a,e+f), m,a,e,f);
			ASSERT4(expt(expt(a,e),f) == expt(a,e*f), m,a,e,f);
		}
	}
	return error;
}