# BRCM_VERSION=3
[bcm963xx.git] / userapps / opensource / sshd / libtommath / mtest / mtest.c
1 /* makes a bignum test harness with NUM tests per operation
2  *
3  * the output is made in the following format [one parameter per line]
4
5 operation
6 operand1
7 operand2
8 [... operandN]
9 result1
10 result2
11 [... resultN]
12
13 So for example "a * b mod n" would be
14
15 mulmod
16 a
17 b
18 n
19 a*b mod n
20
21 e.g. if a=3, b=4 n=11 then
22
23 mulmod
24 3
25 4
26 11
27 1
28
29  */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <time.h>
34 #include "mpi.c"
35
36 FILE *rng;
37
38 void rand_num(mp_int *a)
39 {
40    int n, size;
41    unsigned char buf[2048];
42
43    size = 1 + ((fgetc(rng)<<8) + fgetc(rng)) % 1031;
44    buf[0] = (fgetc(rng)&1)?1:0;
45    fread(buf+1, 1, size, rng);
46    while (buf[1] == 0) buf[1] = fgetc(rng);
47    mp_read_raw(a, buf, 1+size);
48 }
49
50 void rand_num2(mp_int *a)
51 {
52    int n, size;
53    unsigned char buf[2048];
54
55    size = 1 + ((fgetc(rng)<<8) + fgetc(rng)) % 97;
56    buf[0] = (fgetc(rng)&1)?1:0;
57    fread(buf+1, 1, size, rng);
58    while (buf[1] == 0) buf[1] = fgetc(rng);
59    mp_read_raw(a, buf, 1+size);
60 }
61
62 #define mp_to64(a, b) mp_toradix(a, b, 64)
63
64 int main(void)
65 {
66    int n;
67    mp_int a, b, c, d, e;
68    clock_t t1;
69    char buf[4096];
70
71    mp_init(&a);
72    mp_init(&b);
73    mp_init(&c);
74    mp_init(&d);
75    mp_init(&e);
76
77
78    /* initial (2^n - 1)^2 testing, makes sure the comba multiplier works [it has the new carry code] */
79 /*
80    mp_set(&a, 1);
81    for (n = 1; n < 8192; n++) {
82        mp_mul(&a, &a, &c);
83        printf("mul\n");
84        mp_to64(&a, buf);
85        printf("%s\n%s\n", buf, buf);
86        mp_to64(&c, buf);
87        printf("%s\n", buf);
88
89        mp_add_d(&a, 1, &a);
90        mp_mul_2(&a, &a);
91        mp_sub_d(&a, 1, &a);
92    }
93 */
94
95    rng = fopen("/dev/urandom", "rb");
96    if (rng == NULL) {
97       rng = fopen("/dev/random", "rb");
98       if (rng == NULL) {
99          fprintf(stderr, "\nWarning:  stdin used as random source\n\n");
100          rng = stdin;
101       }
102    }
103
104    t1 = clock();
105    for (;;) {
106       if (clock() - t1 > CLOCKS_PER_SEC) {
107          sleep(1);
108          t1 = clock();
109       }
110    
111        n = fgetc(rng) % 13;
112
113    if (n == 0) {
114        /* add tests */
115        rand_num(&a);
116        rand_num(&b);
117        mp_add(&a, &b, &c);
118        printf("add\n");
119        mp_to64(&a, buf);
120        printf("%s\n", buf);
121        mp_to64(&b, buf);
122        printf("%s\n", buf);
123        mp_to64(&c, buf);
124        printf("%s\n", buf);
125    } else if (n == 1) {
126       /* sub tests */
127        rand_num(&a);
128        rand_num(&b);
129        mp_sub(&a, &b, &c);
130        printf("sub\n");
131        mp_to64(&a, buf);
132        printf("%s\n", buf);
133        mp_to64(&b, buf);
134        printf("%s\n", buf);
135        mp_to64(&c, buf);
136        printf("%s\n", buf);
137    } else if (n == 2) {
138        /* mul tests */
139        rand_num(&a);
140        rand_num(&b);
141        mp_mul(&a, &b, &c);
142        printf("mul\n");
143        mp_to64(&a, buf);
144        printf("%s\n", buf);
145        mp_to64(&b, buf);
146        printf("%s\n", buf);
147        mp_to64(&c, buf);
148        printf("%s\n", buf);
149    } else if (n == 3) {
150       /* div tests */
151        rand_num(&a);
152        rand_num(&b);
153        mp_div(&a, &b, &c, &d);
154        printf("div\n");
155        mp_to64(&a, buf);
156        printf("%s\n", buf);
157        mp_to64(&b, buf);
158        printf("%s\n", buf);
159        mp_to64(&c, buf);
160        printf("%s\n", buf);
161        mp_to64(&d, buf);
162        printf("%s\n", buf);
163    } else if (n == 4) {
164       /* sqr tests */
165        rand_num(&a);
166        mp_sqr(&a, &b);
167        printf("sqr\n");
168        mp_to64(&a, buf);
169        printf("%s\n", buf);
170        mp_to64(&b, buf);
171        printf("%s\n", buf);
172    } else if (n == 5) {
173       /* mul_2d test */
174       rand_num(&a);
175       mp_copy(&a, &b);
176       n = fgetc(rng) & 63;
177       mp_mul_2d(&b, n, &b);
178       mp_to64(&a, buf);
179       printf("mul2d\n");
180       printf("%s\n", buf);
181       printf("%d\n", n);
182       mp_to64(&b, buf);
183       printf("%s\n", buf);
184    } else if (n == 6) {
185       /* div_2d test */
186       rand_num(&a);
187       mp_copy(&a, &b);
188       n = fgetc(rng) & 63;
189       mp_div_2d(&b, n, &b, NULL);
190       mp_to64(&a, buf);
191       printf("div2d\n");
192       printf("%s\n", buf);
193       printf("%d\n", n);
194       mp_to64(&b, buf);
195       printf("%s\n", buf);
196    } else if (n == 7) {
197       /* gcd test */
198       rand_num(&a);
199       rand_num(&b);
200       a.sign = MP_ZPOS;
201       b.sign = MP_ZPOS;
202       mp_gcd(&a, &b, &c);
203       printf("gcd\n");
204       mp_to64(&a, buf);
205       printf("%s\n", buf);
206       mp_to64(&b, buf);
207       printf("%s\n", buf);
208       mp_to64(&c, buf);
209       printf("%s\n", buf);
210    } else if (n == 8) {
211       /* lcm test */
212       rand_num(&a);
213       rand_num(&b);
214       a.sign = MP_ZPOS;
215       b.sign = MP_ZPOS;
216       mp_lcm(&a, &b, &c);
217       printf("lcm\n");
218       mp_to64(&a, buf);
219       printf("%s\n", buf);
220       mp_to64(&b, buf);
221       printf("%s\n", buf);
222       mp_to64(&c, buf);
223       printf("%s\n", buf);
224    } else if (n == 9) {
225       /* exptmod test */
226       rand_num2(&a);
227       rand_num2(&b);
228       rand_num2(&c);
229 //      if (c.dp[0]&1) mp_add_d(&c, 1, &c);
230       a.sign = b.sign = c.sign = 0;
231       mp_exptmod(&a, &b, &c, &d);
232       printf("expt\n");
233       mp_to64(&a, buf);
234       printf("%s\n", buf);
235       mp_to64(&b, buf);
236       printf("%s\n", buf);
237       mp_to64(&c, buf);
238       printf("%s\n", buf);
239       mp_to64(&d, buf);
240       printf("%s\n", buf);
241    } else if (n == 10) {
242       /* invmod test */
243       rand_num2(&a);
244       rand_num2(&b);
245       b.sign = MP_ZPOS;
246       a.sign = MP_ZPOS;
247       mp_gcd(&a, &b, &c);
248       if (mp_cmp_d(&c, 1) != 0) continue;
249       if (mp_cmp_d(&b, 1) == 0) continue;
250       mp_invmod(&a, &b, &c);
251       printf("invmod\n");
252       mp_to64(&a, buf);
253       printf("%s\n", buf);
254       mp_to64(&b, buf);
255       printf("%s\n", buf);
256       mp_to64(&c, buf);
257       printf("%s\n", buf);
258    } else if (n == 11) {
259       rand_num(&a);
260       mp_mul_2(&a, &a);
261       mp_div_2(&a, &b);
262       printf("div2\n");
263       mp_to64(&a, buf);
264       printf("%s\n", buf);
265       mp_to64(&b, buf);
266       printf("%s\n", buf);
267    } else if (n == 12) {
268       rand_num2(&a);
269       mp_mul_2(&a, &b);
270       printf("mul2\n");
271       mp_to64(&a, buf);
272       printf("%s\n", buf);
273       mp_to64(&b, buf);
274       printf("%s\n", buf);
275    }
276    }
277    fclose(rng);
278    return 0;
279 }