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