1/* tests the montgomery routines */
2#include <tommath.h>
3
4int main(int argc, char **argv)
5{
6   mp_int modulus, R, p, pp;
7   mp_digit mp;
8   long x, y;
9
10#if defined(BARRELFISH)
11   if (argc > 1) {
12       srand(strtoul(argv[1], NULL, 10));
13   }
14#else
15   srand(time(NULL));
16#endif
17   mp_init_multi(&modulus, &R, &p, &pp, NULL);
18
19   /* loop through various sizes */
20   for (x = 4; x < 256; x++) {
21       printf("DIGITS == %3ld...", x); fflush(stdout);
22
23       /* make up the odd modulus */
24       mp_rand(&modulus, x);
25       modulus.dp[0] |= 1;
26
27       /* now find the R value */
28       mp_montgomery_calc_normalization(&R, &modulus);
29       mp_montgomery_setup(&modulus, &mp);
30
31       /* now run through a bunch tests */
32       for (y = 0; y < 1000; y++) {
33           mp_rand(&p, x/2);        /* p = random */
34           mp_mul(&p, &R, &pp);     /* pp = R * p */
35           mp_montgomery_reduce(&pp, &modulus, mp);
36
37           /* should be equal to p */
38           if (mp_cmp(&pp, &p) != MP_EQ) {
39              printf("FAILURE!\n");
40              exit(-1);
41           }
42       }
43       printf("PASSED\n");
44    }
45
46    return 0;
47}
48
49
50
51
52
53
54/* $Source$ */
55/* $Revision$ */
56/* $Date$ */
57