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